MDL-40050 webservice: Replace add_to_log() with new events
[moodle.git] / webservice / tests / events.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 */
36class webservice_events_testcase extends advanced_testcase {
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',
80 'token' => 'A fake token'
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']);
94 $this->assertEquals($params['other']['token'], $event->other['token']);
95 $this->assertEventLegacyLogData($fakelogdata, $event);
96 }
97
98 public function test_service_created() {
99 global $CFG, $DB;
100
101 // The Web service API doesn't allow the testing of the events directly by
102 // calling some functions which trigger the events, so what we are going here
103 // is just checking that the event returns the expected information.
104
105 $sink = $this->redirectEvents();
106
107 // Creating a fake service.
108 $service = (object) array(
109 'name' => 'Test',
110 'enabled' => 1,
111 'requiredcapability' => '',
112 'restrictedusers' => 0,
113 'component' => null,
114 'timecreated' => time(),
115 'timemodified' => time(),
116 'shortname' => null,
117 'downloadfiles' => 0,
118 'uploadfiles' => 0
119 );
120 $service->id = $DB->insert_record('external_services', $service);
121
122 // Trigger the event.
123 $params = array(
124 'objectid' => $service->id,
125 );
126 $event = \core\event\webservice_service_created::create($params);
127 $event->add_record_snapshot('external_services', $service);
128 $event->trigger();
129
130 $events = $sink->get_events();
131 $this->assertCount(1, $events);
132 $event = reset($events);
133
134 // Assert that the event contains the right information.
135 $this->assertEquals(context_system::instance(), $event->get_context());
136 $this->assertEquals($service->id, $event->objectid);
137 $returnurl = $CFG->wwwroot . "/" . $CFG->admin . "/settings.php?section=externalservices";
138 $expected = array(SITEID, 'webservice', 'add', $returnurl, get_string('addservice', 'webservice', $service));
139 $this->assertEventLegacyLogData($expected, $event);
140 }
141
142 public function test_service_updated() {
143 global $CFG, $DB;
144
145 // The Web service API doesn't allow the testing of the events directly by
146 // calling some functions which trigger the events, so what we are going here
147 // is just checking that the event returns the expected information.
148
149 $sink = $this->redirectEvents();
150
151 // Creating a fake service.
152 $service = (object) array(
153 'name' => 'Test',
154 'enabled' => 1,
155 'requiredcapability' => '',
156 'restrictedusers' => 0,
157 'component' => null,
158 'timecreated' => time(),
159 'timemodified' => time(),
160 'shortname' => null,
161 'downloadfiles' => 0,
162 'uploadfiles' => 0
163 );
164 $service->id = $DB->insert_record('external_services', $service);
165
166 // Trigger the event.
167 $params = array(
168 'objectid' => $service->id,
169 );
170 $event = \core\event\webservice_service_updated::create($params);
171 $event->add_record_snapshot('external_services', $service);
172 $event->trigger();
173
174 $events = $sink->get_events();
175 $this->assertCount(1, $events);
176 $event = reset($events);
177
178 // Assert that the event contains the right information.
179 $this->assertEquals(context_system::instance(), $event->get_context());
180 $this->assertEquals($service->id, $event->objectid);
181 $returnurl = $CFG->wwwroot . "/" . $CFG->admin . "/settings.php?section=externalservices";
182 $expected = array(SITEID, 'webservice', 'edit', $returnurl, get_string('editservice', 'webservice', $service));
183 $this->assertEventLegacyLogData($expected, $event);
184 }
185
186 public function test_service_deleted() {
187 global $CFG, $DB;
188
189 // The Web service API doesn't allow the testing of the events directly by
190 // calling some functions which trigger the events, so what we are going here
191 // is just checking that the event returns the expected information.
192
193 $sink = $this->redirectEvents();
194
195 // Creating a fake service.
196 $service = (object) array(
197 'name' => 'Test',
198 'enabled' => 1,
199 'requiredcapability' => '',
200 'restrictedusers' => 0,
201 'component' => null,
202 'timecreated' => time(),
203 'timemodified' => time(),
204 'shortname' => null,
205 'downloadfiles' => 0,
206 'uploadfiles' => 0
207 );
208 $service->id = $DB->insert_record('external_services', $service);
209
210 // Trigger the event.
211 $params = array(
212 'objectid' => $service->id,
213 );
214 $event = \core\event\webservice_service_deleted::create($params);
215 $event->add_record_snapshot('external_services', $service);
216 $event->trigger();
217
218 $events = $sink->get_events();
219 $this->assertCount(1, $events);
220 $event = reset($events);
221
222 // Assert that the event contains the right information.
223 $this->assertEquals(context_system::instance(), $event->get_context());
224 $this->assertEquals($service->id, $event->objectid);
225 $returnurl = $CFG->wwwroot . "/" . $CFG->admin . "/settings.php?section=externalservices";
226 $expected = array(SITEID, 'webservice', 'delete', $returnurl, get_string('deleteservice', 'webservice', $service));
227 $this->assertEventLegacyLogData($expected, $event);
228 }
229
230 public function test_service_user_added() {
231 global $CFG;
232
233 // The Web service API doesn't allow the testing of the events directly by
234 // calling some functions which trigger the events, so what we are going here
235 // is just checking that the event returns the expected information.
236
237 $sink = $this->redirectEvents();
238
239 $params = array(
240 'objectid' => 1,
241 'relateduserid' => 2
242 );
243 $event = \core\event\webservice_service_user_added::create($params);
244 $event->trigger();
245
246 $events = $sink->get_events();
247 $this->assertCount(1, $events);
248 $event = reset($events);
249
250 $this->assertEquals(context_system::instance(), $event->get_context());
251 $this->assertEquals(1, $event->objectid);
252 $this->assertEquals(2, $event->relateduserid);
253 $expected = array(SITEID, 'core', 'assign', $CFG->admin . '/webservice/service_users.php?id=' . $params['objectid'],
254 'add', '', $params['relateduserid']);
255 $this->assertEventLegacyLogData($expected, $event);
256 }
257
258 public function test_service_user_removed() {
259 global $CFG;
260
261 // The Web service API doesn't allow the testing of the events directly by
262 // calling some functions which trigger the events, so what we are going here
263 // is just checking that the event returns the expected information.
264
265 $sink = $this->redirectEvents();
266
267 $params = array(
268 'objectid' => 1,
269 'relateduserid' => 2
270 );
271 $event = \core\event\webservice_service_user_removed::create($params);
272 $event->trigger();
273
274 $events = $sink->get_events();
275 $this->assertCount(1, $events);
276 $event = reset($events);
277
278 $this->assertEquals(context_system::instance(), $event->get_context());
279 $this->assertEquals(1, $event->objectid);
280 $this->assertEquals(2, $event->relateduserid);
281 $expected = array(SITEID, 'core', 'assign', $CFG->admin . '/webservice/service_users.php?id=' . $params['objectid'],
282 'remove', '', $params['relateduserid']);
283 $this->assertEventLegacyLogData($expected, $event);
284 }
285
286 public function test_token_created() {
287 // The Web service API doesn't allow the testing of the events directly by
288 // calling some functions which trigger the events, so what we are going here
289 // is just checking that the event returns the expected information.
290
291 $sink = $this->redirectEvents();
292
293 $params = array(
294 'objectid' => 1,
295 'relateduserid' => 2,
296 'other' => array(
297 'auto' => true
298 )
299 );
300 $event = \core\event\webservice_token_created::create($params);
301 $event->trigger();
302
303 $events = $sink->get_events();
304 $this->assertCount(1, $events);
305 $event = reset($events);
306
307 $this->assertEquals(context_system::instance(), $event->get_context());
308 $this->assertEquals(1, $event->objectid);
309 $this->assertEquals(2, $event->relateduserid);
310 $expected = array(SITEID, 'webservice', 'automatically create user token', '' , 'User ID: ' . 2);
311 $this->assertEventLegacyLogData($expected, $event);
312 }
313
314 public function test_token_sent() {
315 $user = $this->getDataGenerator()->create_user();
316 $this->setUser($user);
317
318 // The Web service API doesn't allow the testing of the events directly by
319 // calling some functions which trigger the events, so what we are going here
320 // is just checking that the event returns the expected information.
321
322 $sink = $this->redirectEvents();
323
324 $params = array(
325 'objectid' => 1,
326 'other' => array(
327 'auto' => true
328 )
329 );
330 $event = \core\event\webservice_token_sent::create($params);
331 $event->trigger();
332
333 $events = $sink->get_events();
334 $this->assertCount(1, $events);
335 $event = reset($events);
336
337 $this->assertEquals(context_system::instance(), $event->get_context());
338 $this->assertEquals(1, $event->objectid);
339 $expected = array(SITEID, 'webservice', 'sending requested user token', '' , 'User ID: ' . $user->id);
340 $this->assertEventLegacyLogData($expected, $event);
341 }
342}