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);
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);
79 'reason' => 'Unit Test',
80 'method' => 'Some method',
84 $event = \core\event\webservice_login_failed
::create($params);
85 $event->set_legacy_logdata($fakelogdata);
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';
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() {
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(
121 'requiredcapability' => '',
122 'restrictedusers' => 0,
124 'timecreated' => time(),
125 'timemodified' => time(),
127 'downloadfiles' => 0,
130 $service->id
= $DB->insert_record('external_services', $service);
132 // Trigger the event.
134 'objectid' => $service->id
,
136 $event = \core\event\webservice_service_created
::create($params);
137 $event->add_record_snapshot('external_services', $service);
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() {
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(
166 'requiredcapability' => '',
167 'restrictedusers' => 0,
169 'timecreated' => time(),
170 'timemodified' => time(),
172 'downloadfiles' => 0,
175 $service->id
= $DB->insert_record('external_services', $service);
177 // Trigger the event.
179 'objectid' => $service->id
,
181 $event = \core\event\webservice_service_updated
::create($params);
182 $event->add_record_snapshot('external_services', $service);
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() {
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(
211 'requiredcapability' => '',
212 'restrictedusers' => 0,
214 'timecreated' => time(),
215 'timemodified' => time(),
217 'downloadfiles' => 0,
220 $service->id
= $DB->insert_record('external_services', $service);
222 // Trigger the event.
224 'objectid' => $service->id
,
226 $event = \core\event\webservice_service_deleted
::create($params);
227 $event->add_record_snapshot('external_services', $service);
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() {
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();
256 $event = \core\event\webservice_service_user_added
::create($params);
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() {
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();
285 $event = \core\event\webservice_service_user_removed
::create($params);
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();
310 'relateduserid' => 2,
315 $event = \core\event\webservice_token_created
::create($params);
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();
346 $event = \core\event\webservice_token_sent
::create($params);
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);