2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Unit tests for Web service events.
22 * @copyright 2013 Frédéric Massart
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 * Unit tests for Web service events.
33 * @copyright 2013 Frédéric Massart
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class core_webservice_events_testcase
extends advanced_testcase
{
38 public function setUp() {
39 $this->resetAfterTest();
42 public function test_function_called() {
43 // The Web service API doesn't allow the testing of the events directly by
44 // calling some functions which trigger the events, so what we are going here
45 // is just checking that the event returns the expected information.
47 $sink = $this->redirectEvents();
49 $fakelogdata = array(1, 'B', true, null);
52 'function' => 'A function'
55 $event = \core\event\webservice_function_called
::create($params);
56 $event->set_legacy_logdata($fakelogdata);
59 $events = $sink->get_events();
60 $this->assertCount(1, $events);
61 $event = reset($events);
63 $this->assertEquals(context_system
::instance(), $event->get_context());
64 $this->assertEquals('A function', $event->other
['function']);
65 $this->assertEventLegacyLogData($fakelogdata, $event);
68 public function test_login_failed() {
69 // The Web service API doesn't allow the testing of the events directly by
70 // calling some functions which trigger the events, so what we are going here
71 // is just checking that the event returns the expected information.
73 $sink = $this->redirectEvents();
75 $fakelogdata = array(1, 'B', true, null);
78 'reason' => 'Unit Test',
79 'method' => 'Some method',
83 $event = \core\event\webservice_login_failed
::create($params);
84 $event->set_legacy_logdata($fakelogdata);
87 $events = $sink->get_events();
88 $this->assertCount(1, $events);
89 $event = reset($events);
91 $this->assertEquals(context_system
::instance(), $event->get_context());
92 $this->assertEquals($params['other']['reason'], $event->other
['reason']);
93 $this->assertEquals($params['other']['method'], $event->other
['method']);
94 $this->assertEquals($params['other']['tokenid'], $event->other
['tokenid']);
95 $this->assertEventLegacyLogData($fakelogdata, $event);
97 // We cannot set the token in the other properties.
98 $params['other']['token'] = 'I should not be set';
100 $event = \core\event\webservice_login_failed
::create($params);
101 $this->fail('The token cannot be allowed in \core\event\webservice_login_failed');
102 } catch (coding_exception
$e) {
106 public function test_service_created() {
109 // The Web service API doesn't allow the testing of the events directly by
110 // calling some functions which trigger the events, so what we are going here
111 // is just checking that the event returns the expected information.
113 $sink = $this->redirectEvents();
115 // Creating a fake service.
116 $service = (object) array(
119 'requiredcapability' => '',
120 'restrictedusers' => 0,
122 'timecreated' => time(),
123 'timemodified' => time(),
125 'downloadfiles' => 0,
128 $service->id
= $DB->insert_record('external_services', $service);
130 // Trigger the event.
132 'objectid' => $service->id
,
134 $event = \core\event\webservice_service_created
::create($params);
135 $event->add_record_snapshot('external_services', $service);
138 $events = $sink->get_events();
139 $this->assertCount(1, $events);
140 $event = reset($events);
142 // Assert that the event contains the right information.
143 $this->assertEquals(context_system
::instance(), $event->get_context());
144 $this->assertEquals($service->id
, $event->objectid
);
145 $returnurl = $CFG->wwwroot
. "/" . $CFG->admin
. "/settings.php?section=externalservices";
146 $expected = array(SITEID
, 'webservice', 'add', $returnurl, get_string('addservice', 'webservice', $service));
147 $this->assertEventLegacyLogData($expected, $event);
150 public function test_service_updated() {
153 // The Web service API doesn't allow the testing of the events directly by
154 // calling some functions which trigger the events, so what we are going here
155 // is just checking that the event returns the expected information.
157 $sink = $this->redirectEvents();
159 // Creating a fake service.
160 $service = (object) array(
163 'requiredcapability' => '',
164 'restrictedusers' => 0,
166 'timecreated' => time(),
167 'timemodified' => time(),
169 'downloadfiles' => 0,
172 $service->id
= $DB->insert_record('external_services', $service);
174 // Trigger the event.
176 'objectid' => $service->id
,
178 $event = \core\event\webservice_service_updated
::create($params);
179 $event->add_record_snapshot('external_services', $service);
182 $events = $sink->get_events();
183 $this->assertCount(1, $events);
184 $event = reset($events);
186 // Assert that the event contains the right information.
187 $this->assertEquals(context_system
::instance(), $event->get_context());
188 $this->assertEquals($service->id
, $event->objectid
);
189 $returnurl = $CFG->wwwroot
. "/" . $CFG->admin
. "/settings.php?section=externalservices";
190 $expected = array(SITEID
, 'webservice', 'edit', $returnurl, get_string('editservice', 'webservice', $service));
191 $this->assertEventLegacyLogData($expected, $event);
194 public function test_service_deleted() {
197 // The Web service API doesn't allow the testing of the events directly by
198 // calling some functions which trigger the events, so what we are going here
199 // is just checking that the event returns the expected information.
201 $sink = $this->redirectEvents();
203 // Creating a fake service.
204 $service = (object) array(
207 'requiredcapability' => '',
208 'restrictedusers' => 0,
210 'timecreated' => time(),
211 'timemodified' => time(),
213 'downloadfiles' => 0,
216 $service->id
= $DB->insert_record('external_services', $service);
218 // Trigger the event.
220 'objectid' => $service->id
,
222 $event = \core\event\webservice_service_deleted
::create($params);
223 $event->add_record_snapshot('external_services', $service);
226 $events = $sink->get_events();
227 $this->assertCount(1, $events);
228 $event = reset($events);
230 // Assert that the event contains the right information.
231 $this->assertEquals(context_system
::instance(), $event->get_context());
232 $this->assertEquals($service->id
, $event->objectid
);
233 $returnurl = $CFG->wwwroot
. "/" . $CFG->admin
. "/settings.php?section=externalservices";
234 $expected = array(SITEID
, 'webservice', 'delete', $returnurl, get_string('deleteservice', 'webservice', $service));
235 $this->assertEventLegacyLogData($expected, $event);
238 public function test_service_user_added() {
241 // The Web service API doesn't allow the testing of the events directly by
242 // calling some functions which trigger the events, so what we are going here
243 // is just checking that the event returns the expected information.
245 $sink = $this->redirectEvents();
251 $event = \core\event\webservice_service_user_added
::create($params);
254 $events = $sink->get_events();
255 $this->assertCount(1, $events);
256 $event = reset($events);
258 $this->assertEquals(context_system
::instance(), $event->get_context());
259 $this->assertEquals(1, $event->objectid
);
260 $this->assertEquals(2, $event->relateduserid
);
261 $expected = array(SITEID
, 'core', 'assign', $CFG->admin
. '/webservice/service_users.php?id=' . $params['objectid'],
262 'add', '', $params['relateduserid']);
263 $this->assertEventLegacyLogData($expected, $event);
266 public function test_service_user_removed() {
269 // The Web service API doesn't allow the testing of the events directly by
270 // calling some functions which trigger the events, so what we are going here
271 // is just checking that the event returns the expected information.
273 $sink = $this->redirectEvents();
279 $event = \core\event\webservice_service_user_removed
::create($params);
282 $events = $sink->get_events();
283 $this->assertCount(1, $events);
284 $event = reset($events);
286 $this->assertEquals(context_system
::instance(), $event->get_context());
287 $this->assertEquals(1, $event->objectid
);
288 $this->assertEquals(2, $event->relateduserid
);
289 $expected = array(SITEID
, 'core', 'assign', $CFG->admin
. '/webservice/service_users.php?id=' . $params['objectid'],
290 'remove', '', $params['relateduserid']);
291 $this->assertEventLegacyLogData($expected, $event);
294 public function test_token_created() {
295 // The Web service API doesn't allow the testing of the events directly by
296 // calling some functions which trigger the events, so what we are going here
297 // is just checking that the event returns the expected information.
299 $sink = $this->redirectEvents();
303 'relateduserid' => 2,
308 $event = \core\event\webservice_token_created
::create($params);
311 $events = $sink->get_events();
312 $this->assertCount(1, $events);
313 $event = reset($events);
315 $this->assertEquals(context_system
::instance(), $event->get_context());
316 $this->assertEquals(1, $event->objectid
);
317 $this->assertEquals(2, $event->relateduserid
);
318 $expected = array(SITEID
, 'webservice', 'automatically create user token', '' , 'User ID: ' . 2);
319 $this->assertEventLegacyLogData($expected, $event);
322 public function test_token_sent() {
323 $user = $this->getDataGenerator()->create_user();
324 $this->setUser($user);
326 // The Web service API doesn't allow the testing of the events directly by
327 // calling some functions which trigger the events, so what we are going here
328 // is just checking that the event returns the expected information.
330 $sink = $this->redirectEvents();
338 $event = \core\event\webservice_token_sent
::create($params);
341 $events = $sink->get_events();
342 $this->assertCount(1, $events);
343 $event = reset($events);
345 $this->assertEquals(context_system
::instance(), $event->get_context());
346 $this->assertEquals(1, $event->objectid
);
347 $expected = array(SITEID
, 'webservice', 'sending requested user token', '' , 'User ID: ' . $user->id
);
348 $this->assertEventLegacyLogData($expected, $event);