1 |
efrain |
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 |
|
|
|
26 |
namespace core_webservice\event;
|
|
|
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 |
*/
|
|
|
36 |
class events_test extends \advanced_testcase {
|
|
|
37 |
|
|
|
38 |
public function setUp(): void {
|
|
|
39 |
$this->resetAfterTest();
|
|
|
40 |
}
|
|
|
41 |
|
11 |
efrain |
42 |
public function test_function_called(): void {
|
1 |
efrain |
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 |
$params = array(
|
|
|
50 |
'other' => array(
|
|
|
51 |
'function' => 'A function'
|
|
|
52 |
)
|
|
|
53 |
);
|
|
|
54 |
$event = \core\event\webservice_function_called::create($params);
|
|
|
55 |
$event->trigger();
|
|
|
56 |
|
|
|
57 |
$events = $sink->get_events();
|
|
|
58 |
$this->assertCount(1, $events);
|
|
|
59 |
$event = reset($events);
|
|
|
60 |
|
|
|
61 |
$this->assertEquals(\context_system::instance(), $event->get_context());
|
|
|
62 |
$this->assertEquals('A function', $event->other['function']);
|
|
|
63 |
$this->assertEventContextNotUsed($event);
|
|
|
64 |
}
|
|
|
65 |
|
11 |
efrain |
66 |
public function test_login_failed(): void {
|
1 |
efrain |
67 |
// The Web service API doesn't allow the testing of the events directly by
|
|
|
68 |
// calling some functions which trigger the events, so what we are going here
|
|
|
69 |
// is just checking that the event returns the expected information.
|
|
|
70 |
|
|
|
71 |
$sink = $this->redirectEvents();
|
|
|
72 |
|
|
|
73 |
$params = array(
|
|
|
74 |
'other' => array(
|
|
|
75 |
'reason' => 'Unit Test',
|
|
|
76 |
'method' => 'Some method',
|
|
|
77 |
'tokenid' => '123'
|
|
|
78 |
)
|
|
|
79 |
);
|
|
|
80 |
$event = \core\event\webservice_login_failed::create($params);
|
|
|
81 |
$event->trigger();
|
|
|
82 |
|
|
|
83 |
$events = $sink->get_events();
|
|
|
84 |
$this->assertCount(1, $events);
|
|
|
85 |
$event = reset($events);
|
|
|
86 |
|
|
|
87 |
$this->assertEquals(\context_system::instance(), $event->get_context());
|
|
|
88 |
$this->assertEquals($params['other']['reason'], $event->other['reason']);
|
|
|
89 |
$this->assertEquals($params['other']['method'], $event->other['method']);
|
|
|
90 |
$this->assertEquals($params['other']['tokenid'], $event->other['tokenid']);
|
|
|
91 |
|
|
|
92 |
// We cannot set the token in the other properties.
|
|
|
93 |
$params['other']['token'] = 'I should not be set';
|
|
|
94 |
try {
|
|
|
95 |
$event = \core\event\webservice_login_failed::create($params);
|
|
|
96 |
$this->fail('The token cannot be allowed in \core\event\webservice_login_failed');
|
|
|
97 |
} catch (\coding_exception $e) {
|
|
|
98 |
}
|
|
|
99 |
$this->assertEventContextNotUsed($event);
|
|
|
100 |
}
|
|
|
101 |
|
11 |
efrain |
102 |
public function test_service_created(): void {
|
1 |
efrain |
103 |
global $CFG, $DB;
|
|
|
104 |
|
|
|
105 |
// The Web service API doesn't allow the testing of the events directly by
|
|
|
106 |
// calling some functions which trigger the events, so what we are going here
|
|
|
107 |
// is just checking that the event returns the expected information.
|
|
|
108 |
|
|
|
109 |
$sink = $this->redirectEvents();
|
|
|
110 |
|
|
|
111 |
// Creating a fake service.
|
|
|
112 |
$service = (object) array(
|
|
|
113 |
'name' => 'Test',
|
|
|
114 |
'enabled' => 1,
|
|
|
115 |
'requiredcapability' => '',
|
|
|
116 |
'restrictedusers' => 0,
|
|
|
117 |
'component' => null,
|
|
|
118 |
'timecreated' => time(),
|
|
|
119 |
'timemodified' => time(),
|
|
|
120 |
'shortname' => null,
|
|
|
121 |
'downloadfiles' => 0,
|
|
|
122 |
'uploadfiles' => 0
|
|
|
123 |
);
|
|
|
124 |
$service->id = $DB->insert_record('external_services', $service);
|
|
|
125 |
|
|
|
126 |
// Trigger the event.
|
|
|
127 |
$params = array(
|
|
|
128 |
'objectid' => $service->id,
|
|
|
129 |
);
|
|
|
130 |
$event = \core\event\webservice_service_created::create($params);
|
|
|
131 |
$event->add_record_snapshot('external_services', $service);
|
|
|
132 |
$event->trigger();
|
|
|
133 |
|
|
|
134 |
$events = $sink->get_events();
|
|
|
135 |
$this->assertCount(1, $events);
|
|
|
136 |
$event = reset($events);
|
|
|
137 |
|
|
|
138 |
// Assert that the event contains the right information.
|
|
|
139 |
$this->assertEquals(\context_system::instance(), $event->get_context());
|
|
|
140 |
$this->assertEquals($service->id, $event->objectid);
|
|
|
141 |
$this->assertEventContextNotUsed($event);
|
|
|
142 |
}
|
|
|
143 |
|
11 |
efrain |
144 |
public function test_service_updated(): void {
|
1 |
efrain |
145 |
global $CFG, $DB;
|
|
|
146 |
|
|
|
147 |
// The Web service API doesn't allow the testing of the events directly by
|
|
|
148 |
// calling some functions which trigger the events, so what we are going here
|
|
|
149 |
// is just checking that the event returns the expected information.
|
|
|
150 |
|
|
|
151 |
$sink = $this->redirectEvents();
|
|
|
152 |
|
|
|
153 |
// Creating a fake service.
|
|
|
154 |
$service = (object) array(
|
|
|
155 |
'name' => 'Test',
|
|
|
156 |
'enabled' => 1,
|
|
|
157 |
'requiredcapability' => '',
|
|
|
158 |
'restrictedusers' => 0,
|
|
|
159 |
'component' => null,
|
|
|
160 |
'timecreated' => time(),
|
|
|
161 |
'timemodified' => time(),
|
|
|
162 |
'shortname' => null,
|
|
|
163 |
'downloadfiles' => 0,
|
|
|
164 |
'uploadfiles' => 0
|
|
|
165 |
);
|
|
|
166 |
$service->id = $DB->insert_record('external_services', $service);
|
|
|
167 |
|
|
|
168 |
// Trigger the event.
|
|
|
169 |
$params = array(
|
|
|
170 |
'objectid' => $service->id,
|
|
|
171 |
);
|
|
|
172 |
$event = \core\event\webservice_service_updated::create($params);
|
|
|
173 |
$event->add_record_snapshot('external_services', $service);
|
|
|
174 |
$event->trigger();
|
|
|
175 |
|
|
|
176 |
$events = $sink->get_events();
|
|
|
177 |
$this->assertCount(1, $events);
|
|
|
178 |
$event = reset($events);
|
|
|
179 |
|
|
|
180 |
// Assert that the event contains the right information.
|
|
|
181 |
$this->assertEquals(\context_system::instance(), $event->get_context());
|
|
|
182 |
$this->assertEquals($service->id, $event->objectid);
|
|
|
183 |
$this->assertEventContextNotUsed($event);
|
|
|
184 |
}
|
|
|
185 |
|
11 |
efrain |
186 |
public function test_service_deleted(): void {
|
1 |
efrain |
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 |
$this->assertEventContextNotUsed($event);
|
|
|
226 |
}
|
|
|
227 |
|
11 |
efrain |
228 |
public function test_service_user_added(): void {
|
1 |
efrain |
229 |
global $CFG;
|
|
|
230 |
|
|
|
231 |
// The Web service API doesn't allow the testing of the events directly by
|
|
|
232 |
// calling some functions which trigger the events, so what we are going here
|
|
|
233 |
// is just checking that the event returns the expected information.
|
|
|
234 |
|
|
|
235 |
$sink = $this->redirectEvents();
|
|
|
236 |
|
|
|
237 |
$params = array(
|
|
|
238 |
'objectid' => 1,
|
|
|
239 |
'relateduserid' => 2
|
|
|
240 |
);
|
|
|
241 |
$event = \core\event\webservice_service_user_added::create($params);
|
|
|
242 |
$event->trigger();
|
|
|
243 |
|
|
|
244 |
$events = $sink->get_events();
|
|
|
245 |
$this->assertCount(1, $events);
|
|
|
246 |
$event = reset($events);
|
|
|
247 |
|
|
|
248 |
$this->assertEquals(\context_system::instance(), $event->get_context());
|
|
|
249 |
$this->assertEquals(1, $event->objectid);
|
|
|
250 |
$this->assertEquals(2, $event->relateduserid);
|
|
|
251 |
$this->assertEventContextNotUsed($event);
|
|
|
252 |
}
|
|
|
253 |
|
11 |
efrain |
254 |
public function test_service_user_removed(): void {
|
1 |
efrain |
255 |
global $CFG;
|
|
|
256 |
|
|
|
257 |
// The Web service API doesn't allow the testing of the events directly by
|
|
|
258 |
// calling some functions which trigger the events, so what we are going here
|
|
|
259 |
// is just checking that the event returns the expected information.
|
|
|
260 |
|
|
|
261 |
$sink = $this->redirectEvents();
|
|
|
262 |
|
|
|
263 |
$params = array(
|
|
|
264 |
'objectid' => 1,
|
|
|
265 |
'relateduserid' => 2
|
|
|
266 |
);
|
|
|
267 |
$event = \core\event\webservice_service_user_removed::create($params);
|
|
|
268 |
$event->trigger();
|
|
|
269 |
|
|
|
270 |
$events = $sink->get_events();
|
|
|
271 |
$this->assertCount(1, $events);
|
|
|
272 |
$event = reset($events);
|
|
|
273 |
|
|
|
274 |
$this->assertEquals(\context_system::instance(), $event->get_context());
|
|
|
275 |
$this->assertEquals(1, $event->objectid);
|
|
|
276 |
$this->assertEquals(2, $event->relateduserid);
|
|
|
277 |
$this->assertEventContextNotUsed($event);
|
|
|
278 |
}
|
|
|
279 |
|
11 |
efrain |
280 |
public function test_token_created(): void {
|
1 |
efrain |
281 |
// The Web service API doesn't allow the testing of the events directly by
|
|
|
282 |
// calling some functions which trigger the events, so what we are going here
|
|
|
283 |
// is just checking that the event returns the expected information.
|
|
|
284 |
|
|
|
285 |
$sink = $this->redirectEvents();
|
|
|
286 |
|
|
|
287 |
$params = array(
|
|
|
288 |
'objectid' => 1,
|
|
|
289 |
'relateduserid' => 2,
|
|
|
290 |
'other' => array(
|
|
|
291 |
'auto' => true
|
|
|
292 |
)
|
|
|
293 |
);
|
|
|
294 |
$event = \core\event\webservice_token_created::create($params);
|
|
|
295 |
$event->trigger();
|
|
|
296 |
|
|
|
297 |
$events = $sink->get_events();
|
|
|
298 |
$this->assertCount(1, $events);
|
|
|
299 |
$event = reset($events);
|
|
|
300 |
|
|
|
301 |
$this->assertEquals(\context_system::instance(), $event->get_context());
|
|
|
302 |
$this->assertEquals(1, $event->objectid);
|
|
|
303 |
$this->assertEquals(2, $event->relateduserid);
|
|
|
304 |
$this->assertEventContextNotUsed($event);
|
|
|
305 |
}
|
|
|
306 |
|
11 |
efrain |
307 |
public function test_token_sent(): void {
|
1 |
efrain |
308 |
$user = $this->getDataGenerator()->create_user();
|
|
|
309 |
$this->setUser($user);
|
|
|
310 |
|
|
|
311 |
// The Web service API doesn't allow the testing of the events directly by
|
|
|
312 |
// calling some functions which trigger the events, so what we are going here
|
|
|
313 |
// is just checking that the event returns the expected information.
|
|
|
314 |
|
|
|
315 |
$sink = $this->redirectEvents();
|
|
|
316 |
|
|
|
317 |
$params = array(
|
|
|
318 |
'objectid' => 1,
|
|
|
319 |
'other' => array(
|
|
|
320 |
'auto' => true
|
|
|
321 |
)
|
|
|
322 |
);
|
|
|
323 |
$event = \core\event\webservice_token_sent::create($params);
|
|
|
324 |
$event->trigger();
|
|
|
325 |
|
|
|
326 |
$events = $sink->get_events();
|
|
|
327 |
$this->assertCount(1, $events);
|
|
|
328 |
$event = reset($events);
|
|
|
329 |
|
|
|
330 |
$this->assertEquals(\context_system::instance(), $event->get_context());
|
|
|
331 |
$this->assertEquals(1, $event->objectid);
|
|
|
332 |
$this->assertEventContextNotUsed($event);
|
|
|
333 |
}
|
|
|
334 |
}
|