MDL-69454 core_search: consistent activity chooser search
[moodle.git] / webservice / tests / events_test.php
blob955d49b1fcf82aef262de959500b1b30dc305c24
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Unit tests for Web service events.
20 * @package webservice
21 * @category phpunit
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();
28 /**
29 * Unit tests for Web service events.
31 * @package webservice
32 * @category phpunit
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);
50 $params = array(
51 'other' => array(
52 'function' => 'A function'
55 $event = \core\event\webservice_function_called::create($params);
56 $event->set_legacy_logdata($fakelogdata);
57 $event->trigger();
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);
66 $this->assertEventContextNotUsed($event);
69 public function test_login_failed() {
70 // The Web service API doesn't allow the testing of the events directly by
71 // calling some functions which trigger the events, so what we are going here
72 // is just checking that the event returns the expected information.
74 $sink = $this->redirectEvents();
76 $fakelogdata = array(1, 'B', true, null);
77 $params = array(
78 'other' => array(
79 'reason' => 'Unit Test',
80 'method' => 'Some method',
81 'tokenid' => '123'
84 $event = \core\event\webservice_login_failed::create($params);
85 $event->set_legacy_logdata($fakelogdata);
86 $event->trigger();
88 $events = $sink->get_events();
89 $this->assertCount(1, $events);
90 $event = reset($events);
92 $this->assertEquals(context_system::instance(), $event->get_context());
93 $this->assertEquals($params['other']['reason'], $event->other['reason']);
94 $this->assertEquals($params['other']['method'], $event->other['method']);
95 $this->assertEquals($params['other']['tokenid'], $event->other['tokenid']);
96 $this->assertEventLegacyLogData($fakelogdata, $event);
98 // We cannot set the token in the other properties.
99 $params['other']['token'] = 'I should not be set';
100 try {
101 $event = \core\event\webservice_login_failed::create($params);
102 $this->fail('The token cannot be allowed in \core\event\webservice_login_failed');
103 } catch (coding_exception $e) {
105 $this->assertEventContextNotUsed($event);
108 public function test_service_created() {
109 global $CFG, $DB;
111 // The Web service API doesn't allow the testing of the events directly by
112 // calling some functions which trigger the events, so what we are going here
113 // is just checking that the event returns the expected information.
115 $sink = $this->redirectEvents();
117 // Creating a fake service.
118 $service = (object) array(
119 'name' => 'Test',
120 'enabled' => 1,
121 'requiredcapability' => '',
122 'restrictedusers' => 0,
123 'component' => null,
124 'timecreated' => time(),
125 'timemodified' => time(),
126 'shortname' => null,
127 'downloadfiles' => 0,
128 'uploadfiles' => 0
130 $service->id = $DB->insert_record('external_services', $service);
132 // Trigger the event.
133 $params = array(
134 'objectid' => $service->id,
136 $event = \core\event\webservice_service_created::create($params);
137 $event->add_record_snapshot('external_services', $service);
138 $event->trigger();
140 $events = $sink->get_events();
141 $this->assertCount(1, $events);
142 $event = reset($events);
144 // Assert that the event contains the right information.
145 $this->assertEquals(context_system::instance(), $event->get_context());
146 $this->assertEquals($service->id, $event->objectid);
147 $returnurl = $CFG->wwwroot . "/" . $CFG->admin . "/settings.php?section=externalservices";
148 $expected = array(SITEID, 'webservice', 'add', $returnurl, get_string('addservice', 'webservice', $service));
149 $this->assertEventLegacyLogData($expected, $event);
150 $this->assertEventContextNotUsed($event);
153 public function test_service_updated() {
154 global $CFG, $DB;
156 // The Web service API doesn't allow the testing of the events directly by
157 // calling some functions which trigger the events, so what we are going here
158 // is just checking that the event returns the expected information.
160 $sink = $this->redirectEvents();
162 // Creating a fake service.
163 $service = (object) array(
164 'name' => 'Test',
165 'enabled' => 1,
166 'requiredcapability' => '',
167 'restrictedusers' => 0,
168 'component' => null,
169 'timecreated' => time(),
170 'timemodified' => time(),
171 'shortname' => null,
172 'downloadfiles' => 0,
173 'uploadfiles' => 0
175 $service->id = $DB->insert_record('external_services', $service);
177 // Trigger the event.
178 $params = array(
179 'objectid' => $service->id,
181 $event = \core\event\webservice_service_updated::create($params);
182 $event->add_record_snapshot('external_services', $service);
183 $event->trigger();
185 $events = $sink->get_events();
186 $this->assertCount(1, $events);
187 $event = reset($events);
189 // Assert that the event contains the right information.
190 $this->assertEquals(context_system::instance(), $event->get_context());
191 $this->assertEquals($service->id, $event->objectid);
192 $returnurl = $CFG->wwwroot . "/" . $CFG->admin . "/settings.php?section=externalservices";
193 $expected = array(SITEID, 'webservice', 'edit', $returnurl, get_string('editservice', 'webservice', $service));
194 $this->assertEventLegacyLogData($expected, $event);
195 $this->assertEventContextNotUsed($event);
198 public function test_service_deleted() {
199 global $CFG, $DB;
201 // The Web service API doesn't allow the testing of the events directly by
202 // calling some functions which trigger the events, so what we are going here
203 // is just checking that the event returns the expected information.
205 $sink = $this->redirectEvents();
207 // Creating a fake service.
208 $service = (object) array(
209 'name' => 'Test',
210 'enabled' => 1,
211 'requiredcapability' => '',
212 'restrictedusers' => 0,
213 'component' => null,
214 'timecreated' => time(),
215 'timemodified' => time(),
216 'shortname' => null,
217 'downloadfiles' => 0,
218 'uploadfiles' => 0
220 $service->id = $DB->insert_record('external_services', $service);
222 // Trigger the event.
223 $params = array(
224 'objectid' => $service->id,
226 $event = \core\event\webservice_service_deleted::create($params);
227 $event->add_record_snapshot('external_services', $service);
228 $event->trigger();
230 $events = $sink->get_events();
231 $this->assertCount(1, $events);
232 $event = reset($events);
234 // Assert that the event contains the right information.
235 $this->assertEquals(context_system::instance(), $event->get_context());
236 $this->assertEquals($service->id, $event->objectid);
237 $returnurl = $CFG->wwwroot . "/" . $CFG->admin . "/settings.php?section=externalservices";
238 $expected = array(SITEID, 'webservice', 'delete', $returnurl, get_string('deleteservice', 'webservice', $service));
239 $this->assertEventLegacyLogData($expected, $event);
240 $this->assertEventContextNotUsed($event);
243 public function test_service_user_added() {
244 global $CFG;
246 // The Web service API doesn't allow the testing of the events directly by
247 // calling some functions which trigger the events, so what we are going here
248 // is just checking that the event returns the expected information.
250 $sink = $this->redirectEvents();
252 $params = array(
253 'objectid' => 1,
254 'relateduserid' => 2
256 $event = \core\event\webservice_service_user_added::create($params);
257 $event->trigger();
259 $events = $sink->get_events();
260 $this->assertCount(1, $events);
261 $event = reset($events);
263 $this->assertEquals(context_system::instance(), $event->get_context());
264 $this->assertEquals(1, $event->objectid);
265 $this->assertEquals(2, $event->relateduserid);
266 $expected = array(SITEID, 'core', 'assign', $CFG->admin . '/webservice/service_users.php?id=' . $params['objectid'],
267 'add', '', $params['relateduserid']);
268 $this->assertEventLegacyLogData($expected, $event);
269 $this->assertEventContextNotUsed($event);
272 public function test_service_user_removed() {
273 global $CFG;
275 // The Web service API doesn't allow the testing of the events directly by
276 // calling some functions which trigger the events, so what we are going here
277 // is just checking that the event returns the expected information.
279 $sink = $this->redirectEvents();
281 $params = array(
282 'objectid' => 1,
283 'relateduserid' => 2
285 $event = \core\event\webservice_service_user_removed::create($params);
286 $event->trigger();
288 $events = $sink->get_events();
289 $this->assertCount(1, $events);
290 $event = reset($events);
292 $this->assertEquals(context_system::instance(), $event->get_context());
293 $this->assertEquals(1, $event->objectid);
294 $this->assertEquals(2, $event->relateduserid);
295 $expected = array(SITEID, 'core', 'assign', $CFG->admin . '/webservice/service_users.php?id=' . $params['objectid'],
296 'remove', '', $params['relateduserid']);
297 $this->assertEventLegacyLogData($expected, $event);
298 $this->assertEventContextNotUsed($event);
301 public function test_token_created() {
302 // The Web service API doesn't allow the testing of the events directly by
303 // calling some functions which trigger the events, so what we are going here
304 // is just checking that the event returns the expected information.
306 $sink = $this->redirectEvents();
308 $params = array(
309 'objectid' => 1,
310 'relateduserid' => 2,
311 'other' => array(
312 'auto' => true
315 $event = \core\event\webservice_token_created::create($params);
316 $event->trigger();
318 $events = $sink->get_events();
319 $this->assertCount(1, $events);
320 $event = reset($events);
322 $this->assertEquals(context_system::instance(), $event->get_context());
323 $this->assertEquals(1, $event->objectid);
324 $this->assertEquals(2, $event->relateduserid);
325 $expected = array(SITEID, 'webservice', 'automatically create user token', '' , 'User ID: ' . 2);
326 $this->assertEventLegacyLogData($expected, $event);
327 $this->assertEventContextNotUsed($event);
330 public function test_token_sent() {
331 $user = $this->getDataGenerator()->create_user();
332 $this->setUser($user);
334 // The Web service API doesn't allow the testing of the events directly by
335 // calling some functions which trigger the events, so what we are going here
336 // is just checking that the event returns the expected information.
338 $sink = $this->redirectEvents();
340 $params = array(
341 'objectid' => 1,
342 'other' => array(
343 'auto' => true
346 $event = \core\event\webservice_token_sent::create($params);
347 $event->trigger();
349 $events = $sink->get_events();
350 $this->assertCount(1, $events);
351 $event = reset($events);
353 $this->assertEquals(context_system::instance(), $event->get_context());
354 $this->assertEquals(1, $event->objectid);
355 $expected = array(SITEID, 'webservice', 'sending requested user token', '' , 'User ID: ' . $user->id);
356 $this->assertEventLegacyLogData($expected, $event);
357 $this->assertEventContextNotUsed($event);