MDL-40050 webservice: Moving new events test file to right location
[moodle.git] / webservice / tests / events_test.php
CommitLineData
d733a8cc
FM
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.
13//
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/>.
16
17/**
18 * Unit tests for Web service events.
19 *
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
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28/**
29 * Unit tests for Web service events.
30 *
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
35 */
26422a66 36class core_webservice_events_testcase extends advanced_testcase {
d733a8cc
FM
37
38 public function setUp() {
39 $this->resetAfterTest();
40 }
41
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.
46
47 $sink = $this->redirectEvents();
48
49 $fakelogdata = array(1, 'B', true, null);
50 $params = array(
51 'other' => array(
52 'function' => 'A function'
53 )
54 );
55 $event = \core\event\webservice_function_called::create($params);
56 $event->set_legacy_logdata($fakelogdata);
57 $event->trigger();
58
59 $events = $sink->get_events();
60 $this->assertCount(1, $events);
61 $event = reset($events);
62
63 $this->assertEquals(context_system::instance(), $event->get_context());
64 $this->assertEquals('A function', $event->other['function']);
65 $this->assertEventLegacyLogData($fakelogdata, $event);
66 }
67
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.
72
73 $sink = $this->redirectEvents();
74
75 $fakelogdata = array(1, 'B', true, null);
76 $params = array(
77 'other' => array(
78 'reason' => 'Unit Test',
79 'method' => 'Some method',
ee2df1a8 80 'tokenid' => '123'
d733a8cc
FM
81 )
82 );
83 $event = \core\event\webservice_login_failed::create($params);
84 $event->set_legacy_logdata($fakelogdata);
85 $event->trigger();
86
87 $events = $sink->get_events();
88 $this->assertCount(1, $events);
89 $event = reset($events);
90
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']);
ee2df1a8 94 $this->assertEquals($params['other']['tokenid'], $event->other['tokenid']);
d733a8cc 95 $this->assertEventLegacyLogData($fakelogdata, $event);
ee2df1a8
FM
96
97 // We cannot set the token in the other properties.
98 $params['other']['token'] = 'I should not be set';
99 try {
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) {
103 }
d733a8cc
FM
104 }
105
106 public function test_service_created() {
107 global $CFG, $DB;
108
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.
112
113 $sink = $this->redirectEvents();
114
115 // Creating a fake service.
116 $service = (object) array(
117 'name' => 'Test',
118 'enabled' => 1,
119 'requiredcapability' => '',
120 'restrictedusers' => 0,
121 'component' => null,
122 'timecreated' => time(),
123 'timemodified' => time(),
124 'shortname' => null,
125 'downloadfiles' => 0,
126 'uploadfiles' => 0
127 );
128 $service->id = $DB->insert_record('external_services', $service);
129
130 // Trigger the event.
131 $params = array(
132 'objectid' => $service->id,
133 );
134 $event = \core\event\webservice_service_created::create($params);
135 $event->add_record_snapshot('external_services', $service);
136 $event->trigger();
137
138 $events = $sink->get_events();
139 $this->assertCount(1, $events);
140 $event = reset($events);
141
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);
148 }
149
150 public function test_service_updated() {
151 global $CFG, $DB;
152
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.
156
157 $sink = $this->redirectEvents();
158
159 // Creating a fake service.
160 $service = (object) array(
161 'name' => 'Test',
162 'enabled' => 1,
163 'requiredcapability' => '',
164 'restrictedusers' => 0,
165 'component' => null,
166 'timecreated' => time(),
167 'timemodified' => time(),
168 'shortname' => null,
169 'downloadfiles' => 0,
170 'uploadfiles' => 0
171 );
172 $service->id = $DB->insert_record('external_services', $service);
173
174 // Trigger the event.
175 $params = array(
176 'objectid' => $service->id,
177 );
178 $event = \core\event\webservice_service_updated::create($params);
179 $event->add_record_snapshot('external_services', $service);
180 $event->trigger();
181
182 $events = $sink->get_events();
183 $this->assertCount(1, $events);
184 $event = reset($events);
185
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);
192 }
193
194 public function test_service_deleted() {
195 global $CFG, $DB;
196
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.
200
201 $sink = $this->redirectEvents();
202
203 // Creating a fake service.
204 $service = (object) array(
205 'name' => 'Test',
206 'enabled' => 1,
207 'requiredcapability' => '',
208 'restrictedusers' => 0,
209 'component' => null,
210 'timecreated' => time(),
211 'timemodified' => time(),
212 'shortname' => null,
213 'downloadfiles' => 0,
214 'uploadfiles' => 0
215 );
216 $service->id = $DB->insert_record('external_services', $service);
217
218 // Trigger the event.
219 $params = array(
220 'objectid' => $service->id,
221 );
222 $event = \core\event\webservice_service_deleted::create($params);
223 $event->add_record_snapshot('external_services', $service);
224 $event->trigger();
225
226 $events = $sink->get_events();
227 $this->assertCount(1, $events);
228 $event = reset($events);
229
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);
236 }
237
238 public function test_service_user_added() {
239 global $CFG;
240
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.
244
245 $sink = $this->redirectEvents();
246
247 $params = array(
248 'objectid' => 1,
249 'relateduserid' => 2
250 );
251 $event = \core\event\webservice_service_user_added::create($params);
252 $event->trigger();
253
254 $events = $sink->get_events();
255 $this->assertCount(1, $events);
256 $event = reset($events);
257
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);
264 }
265
266 public function test_service_user_removed() {
267 global $CFG;
268
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.
272
273 $sink = $this->redirectEvents();
274
275 $params = array(
276 'objectid' => 1,
277 'relateduserid' => 2
278 );
279 $event = \core\event\webservice_service_user_removed::create($params);
280 $event->trigger();
281
282 $events = $sink->get_events();
283 $this->assertCount(1, $events);
284 $event = reset($events);
285
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);
292 }
293
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.
298
299 $sink = $this->redirectEvents();
300
301 $params = array(
302 'objectid' => 1,
303 'relateduserid' => 2,
304 'other' => array(
305 'auto' => true
306 )
307 );
308 $event = \core\event\webservice_token_created::create($params);
309 $event->trigger();
310
311 $events = $sink->get_events();
312 $this->assertCount(1, $events);
313 $event = reset($events);
314
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);
320 }
321
322 public function test_token_sent() {
323 $user = $this->getDataGenerator()->create_user();
324 $this->setUser($user);
325
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.
329
330 $sink = $this->redirectEvents();
331
332 $params = array(
333 'objectid' => 1,
334 'other' => array(
335 'auto' => true
336 )
337 );
338 $event = \core\event\webservice_token_sent::create($params);
339 $event->trigger();
340
341 $events = $sink->get_events();
342 $this->assertCount(1, $events);
343 $event = reset($events);
344
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);
349 }
350}