on-demand release 3.2dev+
[moodle.git] / lib / tests / event_test.php
CommitLineData
d8a1f426
PS
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 * Tests for event manager, base event and observers.
19 *
20 * @package core
21 * @category phpunit
22 * @copyright 2013 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28require_once(__DIR__.'/fixtures/event_fixtures.php');
29
30class core_event_testcase extends advanced_testcase {
31
20ff2fba
DM
32 const DEBUGGING_MSG = 'Events API using $handlers array has been deprecated in favour of Events 2 API, please use it instead.';
33
605a8c33
PS
34 public function test_event_properties() {
35 global $USER;
36
37 $system = \context_system::instance();
9ede00db 38 $event = \core_tests\event\unittest_executed::create(array('context'=>$system, 'objectid'=>5, 'other'=>array('sample'=>null, 'xx'=>10)));
605a8c33
PS
39
40 $this->assertSame('\core_tests\event\unittest_executed', $event->eventname);
41 $this->assertSame('core_tests', $event->component);
42 $this->assertSame('executed', $event->action);
660f049f 43 $this->assertSame('unittest', $event->target);
605a8c33
PS
44 $this->assertSame(5, $event->objectid);
45 $this->assertSame('u', $event->crud);
3345e24f 46 $this->assertSame(\core\event\base::LEVEL_PARTICIPATING, $event->edulevel);
605a8c33 47
96deb950 48 $this->assertEquals($system, $event->get_context());
605a8c33
PS
49 $this->assertSame($system->id, $event->contextid);
50 $this->assertSame($system->contextlevel, $event->contextlevel);
51 $this->assertSame($system->instanceid, $event->contextinstanceid);
52
53 $this->assertSame($USER->id, $event->userid);
9ede00db 54 $this->assertSame(0, $event->courseid);
605a8c33
PS
55
56 $this->assertNull($event->relateduserid);
57 $this->assertFalse(isset($event->relateduserid));
58
bc293202
59 $this->assertSame(0, $event->anonymous);
60
c4297815
PS
61 $this->assertSame(array('sample'=>null, 'xx'=>10), $event->other);
62 $this->assertTrue(isset($event->other['xx']));
63 $this->assertFalse(isset($event->other['sample']));
605a8c33
PS
64
65 $this->assertLessThanOrEqual(time(), $event->timecreated);
66
67 try {
68 $event->courseid = 2;
69 $this->fail('Exception expected on event modification');
70 } catch (\moodle_exception $e) {
71 $this->assertInstanceOf('coding_exception', $e);
72 }
73
74 try {
75 $event->xxxx = 1;
76 $this->fail('Exception expected on event modification');
77 } catch (\moodle_exception $e) {
78 $this->assertInstanceOf('coding_exception', $e);
79 }
27af3e62 80
bc293202 81 $event2 = \core_tests\event\unittest_executed::create(array('contextid'=>$system->id, 'objectid'=>5, 'anonymous'=>1, 'other'=>array('sample'=>null, 'xx'=>10)));
96deb950 82 $this->assertEquals($event->get_context(), $event2->get_context());
bc293202
83 $this->assertSame(1, $event2->anonymous);
84
85 $event3 = \core_tests\event\unittest_executed::create(array('contextid'=>$system->id, 'objectid'=>5, 'anonymous'=>true, 'other'=>array('sample'=>null, 'xx'=>10)));
86 $this->assertSame(1, $event3->anonymous);
605a8c33
PS
87 }
88
513d8793
FM
89 public function test_event_properties_guessing() {
90 global $USER;
91 $this->resetAfterTest();
92
93 $course = $this->getDataGenerator()->create_course();
94 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
95 $context = context_module::instance($forum->cmid);
96 $event = \core_tests\event\unittest_executed::create(array('context' => $context, 'objectid' => 5));
97
98 // Check guessed course ID, and default properties.
99 $this->assertSame('\core_tests\event\unittest_executed', $event->eventname);
100 $this->assertSame('core_tests', $event->component);
101 $this->assertSame('executed', $event->action);
102 $this->assertSame('unittest', $event->target);
103 $this->assertSame(5, $event->objectid);
104 $this->assertEquals($context, $event->get_context());
105 $this->assertEquals($course->id, $event->courseid);
106 $this->assertSame($USER->id, $event->userid);
107 $this->assertNull($event->relateduserid);
108
109 $user = $this->getDataGenerator()->create_user();
110 $context = context_user::instance($user->id);
111 $event = \core_tests\event\unittest_executed::create(array('contextid' => $context->id, 'objectid' => 5));
112
113 // Check guessing on contextid, and user context level.
114 $this->assertEquals($context, $event->get_context());
115 $this->assertEquals($context->id, $event->contextid);
116 $this->assertEquals($context->contextlevel, $event->contextlevel);
117 $this->assertSame(0, $event->courseid);
118 $this->assertSame($USER->id, $event->userid);
119 $this->assertSame($user->id, $event->relateduserid);
120 }
121
d8a1f426 122 public function test_observers_parsing() {
111c6496 123 global $CFG;
d8a1f426
PS
124
125 $observers = array(
2d1884d9
PS
126 array(
127 'eventname' => '*',
128 'callback' => array('\core_tests\event\unittest_observer', 'observe_all_alt'),
129 ),
d8a1f426
PS
130 array(
131 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 132 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
133 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
134 ),
135 array(
136 'eventname' => '*',
4b734e74 137 'callback' => array('\core_tests\event\unittest_observer', 'observe_all'),
d8a1f426
PS
138 'includefile' => null,
139 'internal' => 1,
2d1884d9 140 'priority' => 10,
d8a1f426
PS
141 ),
142 array(
143 'eventname' => '\core\event\unknown_executed',
4b734e74 144 'callback' => '\core_tests\event\unittest_observer::broken_observer',
d8a1f426
PS
145 'priority' => 100,
146 ),
147 array(
148 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 149 'callback' => '\core_tests\event\unittest_observer::external_observer',
d8a1f426
PS
150 'priority' => 200,
151 'internal' => 0,
152 ),
153 );
154
155 $result = \core\event\manager::phpunit_replace_observers($observers);
d8a1f426 156 $this->assertCount(3, $result);
d8a1f426
PS
157
158 $expected = array();
159 $observer = new stdClass();
d8a1f426
PS
160 $observer->callable = '\core_tests\event\unittest_observer::external_observer';
161 $observer->priority = 200;
162 $observer->internal = false;
163 $observer->includefile = null;
7c084af9
AG
164 $observer->plugintype = null;
165 $observer->plugin = null;
2d1884d9 166 $expected[0] = $observer;
d8a1f426
PS
167 $observer = new stdClass();
168 $observer->callable = '\core_tests\event\unittest_observer::observe_one';
169 $observer->priority = 0;
170 $observer->internal = true;
111c6496 171 $observer->includefile = $CFG->dirroot.'/lib/tests/fixtures/event_fixtures.php';
7c084af9
AG
172 $observer->plugintype = null;
173 $observer->plugin = null;
2d1884d9 174 $expected[1] = $observer;
d8a1f426
PS
175
176 $this->assertEquals($expected, $result['\core_tests\event\unittest_executed']);
177
178 $expected = array();
179 $observer = new stdClass();
d8a1f426
PS
180 $observer->callable = '\core_tests\event\unittest_observer::broken_observer';
181 $observer->priority = 100;
182 $observer->internal = true;
183 $observer->includefile = null;
7c084af9
AG
184 $observer->plugintype = null;
185 $observer->plugin = null;
2d1884d9 186 $expected[0] = $observer;
d8a1f426
PS
187
188 $this->assertEquals($expected, $result['\core\event\unknown_executed']);
189
190 $expected = array();
191 $observer = new stdClass();
4b734e74 192 $observer->callable = array('\core_tests\event\unittest_observer', 'observe_all');
2d1884d9 193 $observer->priority = 10;
d8a1f426
PS
194 $observer->internal = true;
195 $observer->includefile = null;
7c084af9
AG
196 $observer->plugintype = null;
197 $observer->plugin = null;
d8a1f426 198 $expected[0] = $observer;
2d1884d9
PS
199 $observer = new stdClass();
200 $observer->callable = array('\core_tests\event\unittest_observer', 'observe_all_alt');
201 $observer->priority = 0;
202 $observer->internal = true;
203 $observer->includefile = null;
7c084af9
AG
204 $observer->plugintype = null;
205 $observer->plugin = null;
2d1884d9 206 $expected[1] = $observer;
d8a1f426 207
2d1884d9 208 $this->assertEquals($expected, $result['\core\event\base']);
d8a1f426 209
d8a1f426
PS
210 // Now test broken stuff...
211
212 $observers = array(
213 array(
214 'eventname' => 'core_tests\event\unittest_executed', // Fix leading backslash.
4b734e74 215 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
216 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
217 'internal' => 1, // Cast to bool.
218 ),
219 );
220 $result = \core\event\manager::phpunit_replace_observers($observers);
221 $this->assertCount(1, $result);
222 $expected = array();
223 $observer = new stdClass();
224 $observer->callable = '\core_tests\event\unittest_observer::observe_one';
225 $observer->priority = 0;
226 $observer->internal = true;
111c6496 227 $observer->includefile = $CFG->dirroot.'/lib/tests/fixtures/event_fixtures.php';
7c084af9
AG
228 $observer->plugintype = null;
229 $observer->plugin = null;
d8a1f426
PS
230 $expected[0] = $observer;
231 $this->assertEquals($expected, $result['\core_tests\event\unittest_executed']);
232
233 $observers = array(
234 array(
235 // Missing eventclass.
4b734e74 236 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
237 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
238 ),
239 );
240 $result = \core\event\manager::phpunit_replace_observers($observers);
241 $this->assertCount(0, $result);
242 $this->assertDebuggingCalled();
243
244 $observers = array(
245 array(
246 'eventname' => '', // Empty eventclass.
4b734e74 247 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
248 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
249 ),
250 );
251 $result = \core\event\manager::phpunit_replace_observers($observers);
252 $this->assertCount(0, $result);
253 $this->assertDebuggingCalled();
254
255 $observers = array(
256 array(
257 'eventname' => '\core_tests\event\unittest_executed',
258 // Missing callable.
259 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
260 ),
261 );
262 $result = \core\event\manager::phpunit_replace_observers($observers);
263 $this->assertCount(0, $result);
264 $this->assertDebuggingCalled();
265
266 $observers = array(
267 array(
268 'eventname' => '\core_tests\event\unittest_executed',
96deb950 269 'callback' => '', // Empty callable.
d8a1f426
PS
270 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
271 ),
272 );
273 $result = \core\event\manager::phpunit_replace_observers($observers);
274 $this->assertCount(0, $result);
275 $this->assertDebuggingCalled();
276
277 $observers = array(
278 array(
279 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 280 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
281 'includefile' => 'lib/tests/fixtures/event_fixtures.php_xxx', // Missing file.
282 ),
283 );
284 $result = \core\event\manager::phpunit_replace_observers($observers);
285 $this->assertCount(0, $result);
286 $this->assertDebuggingCalled();
287 }
288
289 public function test_normal_dispatching() {
290 $observers = array(
291 array(
292 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 293 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
294 ),
295 array(
296 'eventname' => '*',
4b734e74 297 'callback' => '\core_tests\event\unittest_observer::observe_all',
d8a1f426
PS
298 'includefile' => null,
299 'internal' => 1,
300 'priority' => 9999,
301 ),
302 );
303
304 \core\event\manager::phpunit_replace_observers($observers);
305 \core_tests\event\unittest_observer::reset();
306
9ede00db 307 $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
308 $event1->nest = 1;
309 $this->assertFalse($event1->is_triggered());
22626564 310 $this->assertFalse($event1->is_dispatched());
d8a1f426
PS
311 $this->assertFalse($event1->is_restored());
312 $event1->trigger();
313 $this->assertTrue($event1->is_triggered());
22626564 314 $this->assertTrue($event1->is_dispatched());
d8a1f426
PS
315 $this->assertFalse($event1->is_restored());
316
9ede00db 317 $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
318 $event1->trigger();
319
320 $this->assertSame(
9ede00db 321 array('observe_all-nesting-1', 'observe_one-1', 'observe_all-666', 'observe_one-666', 'observe_all-2', 'observe_one-2'),
d8a1f426
PS
322 \core_tests\event\unittest_observer::$info);
323 }
324
62401e8f
PS
325 public function test_event_sink() {
326 $sink = $this->redirectEvents();
9ede00db 327 $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
62401e8f
PS
328 $event1->trigger();
329 $this->assertSame(1, $sink->count());
330 $retult = $sink->get_events();
331 $this->assertSame($event1, $retult[0]);
332
9ede00db 333 $event2 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
62401e8f
PS
334 $event2->trigger();
335 $this->assertSame(2, $sink->count());
336 $retult = $sink->get_events();
337 $this->assertSame($event1, $retult[0]);
338 $this->assertSame($event2, $retult[1]);
339
340 $sink->clear();
341 $this->assertSame(0, $sink->count());
342 $this->assertSame(array(), $sink->get_events());
343
9ede00db 344 $event3 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>3, 'xx'=>10)));
62401e8f
PS
345 $event3->trigger();
346 $this->assertSame(1, $sink->count());
347 $retult = $sink->get_events();
348 $this->assertSame($event3, $retult[0]);
349
350 $sink->close();
9ede00db 351 $event4 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>4, 'xx'=>10)));
62401e8f
PS
352 $event4->trigger();
353 $this->assertSame(1, $sink->count());
354 $retult = $sink->get_events();
355 $this->assertSame($event3, $retult[0]);
356 }
357
d8a1f426
PS
358 public function test_ignore_exceptions() {
359 $observers = array(
360
361 array(
362 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 363 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
364 ),
365
366 array(
367 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 368 'callback' => '\core_tests\event\unittest_observer::broken_observer',
d8a1f426
PS
369 'priority' => 100,
370 ),
371 );
372
373 \core\event\manager::phpunit_replace_observers($observers);
374 \core_tests\event\unittest_observer::reset();
375
9ede00db 376 $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
377 $event1->trigger();
378 $this->assertDebuggingCalled();
379
9ede00db 380 $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
381 $event1->trigger();
382 $this->assertDebuggingCalled();
383
384 $this->assertSame(
385 array('broken_observer-1', 'observe_one-1', 'broken_observer-2', 'observe_one-2'),
386 \core_tests\event\unittest_observer::$info);
387 }
388
389 public function test_external_buffer() {
390 global $DB;
391
392 $this->preventResetByRollback();
393
394 $observers = array(
395
396 array(
397 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 398 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
399 ),
400
401 array(
402 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 403 'callback' => '\core_tests\event\unittest_observer::external_observer',
d8a1f426
PS
404 'priority' => 200,
405 'internal' => 0,
406 ),
407 );
408
409 \core\event\manager::phpunit_replace_observers($observers);
410 \core_tests\event\unittest_observer::reset();
411
9ede00db 412 $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426 413 $event1->trigger();
9ede00db 414 $event2 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
415 $event2->trigger();
416
417 $this->assertSame(
418 array('external_observer-1', 'observe_one-1', 'external_observer-2', 'observe_one-2'),
419 \core_tests\event\unittest_observer::$info);
420
421 \core\event\manager::phpunit_replace_observers($observers);
422 \core_tests\event\unittest_observer::reset();
423
424 $this->assertSame(array(), \core_tests\event\unittest_observer::$info);
425
426 $trans = $DB->start_delegated_transaction();
427
9ede00db 428 $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426 429 $event1->trigger();
9ede00db 430 $event2 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
431 $event2->trigger();
432
433 $this->assertSame(
434 array('observe_one-1', 'observe_one-2'),
435 \core_tests\event\unittest_observer::$info);
436
437 $trans->allow_commit();
438
439 $this->assertSame(
440 array('observe_one-1', 'observe_one-2', 'external_observer-1', 'external_observer-2'),
441 \core_tests\event\unittest_observer::$info);
442
443 \core\event\manager::phpunit_replace_observers($observers);
444 \core_tests\event\unittest_observer::reset();
445
9ede00db 446 $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
447 $event1->trigger();
448 $trans = $DB->start_delegated_transaction();
9ede00db 449 $event2 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
450 $event2->trigger();
451 try {
452 $trans->rollback(new \moodle_exception('xxx'));
453 $this->fail('Expecting exception');
454 } catch (\moodle_exception $e) {
96deb950 455 $this->assertInstanceOf('moodle_exception', $e);
d8a1f426
PS
456 }
457
458 $this->assertSame(
459 array('external_observer-1', 'observe_one-1', 'observe_one-2'),
460 \core_tests\event\unittest_observer::$info);
461 }
462
5cf50f96
PS
463 public function test_rollback() {
464 global $DB;
465
466 $this->resetAfterTest();
467 $this->preventResetByRollback();
468
469 $observers = array(
470 array(
471 'eventname' => '\core_tests\event\unittest_executed',
472 'callback' => '\core_tests\event\unittest_observer::external_observer',
473 'internal' => 0,
474 ),
475 );
476
477 \core\event\manager::phpunit_replace_observers($observers);
478 \core_tests\event\unittest_observer::reset();
479
480 $this->assertCount(0, \core_tests\event\unittest_observer::$event);
481
482 \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)))->trigger();
483 $this->assertCount(1, \core_tests\event\unittest_observer::$event);
484 \core_tests\event\unittest_observer::reset();
485
486 $transaction1 = $DB->start_delegated_transaction();
487
488 \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)))->trigger();
489 $this->assertCount(0, \core_tests\event\unittest_observer::$event);
490
491 $transaction2 = $DB->start_delegated_transaction();
492
493 \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)))->trigger();
494 $this->assertCount(0, \core_tests\event\unittest_observer::$event);
495
496 try {
497 $transaction2->rollback(new Exception('x'));
498 $this->fail('Expecting exception');
499 } catch (Exception $e) {}
500 $this->assertCount(0, \core_tests\event\unittest_observer::$event);
501
502 $this->assertTrue($DB->is_transaction_started());
503
504 try {
505 $transaction1->rollback(new Exception('x'));
506 $this->fail('Expecting exception');
507 } catch (Exception $e) {}
508 $this->assertCount(0, \core_tests\event\unittest_observer::$event);
509
510 $this->assertFalse($DB->is_transaction_started());
511
512 \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)))->trigger();
513 $this->assertCount(1, \core_tests\event\unittest_observer::$event);
514 }
515
516 public function test_forced_rollback() {
517 global $DB;
518
519 $this->resetAfterTest();
520 $this->preventResetByRollback();
521
522 $observers = array(
523 array(
524 'eventname' => '\core_tests\event\unittest_executed',
525 'callback' => '\core_tests\event\unittest_observer::external_observer',
526 'internal' => 0,
527 ),
528 );
529
530 \core\event\manager::phpunit_replace_observers($observers);
531 \core_tests\event\unittest_observer::reset();
532
533 $this->assertCount(0, \core_tests\event\unittest_observer::$event);
534
535 \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)))->trigger();
536 $this->assertCount(1, \core_tests\event\unittest_observer::$event);
537 \core_tests\event\unittest_observer::reset();
538
539 $transaction1 = $DB->start_delegated_transaction();
540
541 \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)))->trigger();
542 $this->assertCount(0, \core_tests\event\unittest_observer::$event);
543
544 $transaction2 = $DB->start_delegated_transaction();
545
546 \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)))->trigger();
547 $this->assertCount(0, \core_tests\event\unittest_observer::$event);
548
549 $DB->force_transaction_rollback();
550 $this->assertCount(0, \core_tests\event\unittest_observer::$event);
551
552 $this->assertFalse($DB->is_transaction_started());
553
554 \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)))->trigger();
555 $this->assertCount(1, \core_tests\event\unittest_observer::$event);
556 }
557
3345e24f
558 public function test_deprecated() {
559 global $DB;
560
561 $this->resetAfterTest(true);
562
563 $event = \core_tests\event\deprecated_event1::create();
564 $this->assertDebuggingCalled('level property is deprecated, use edulevel property instead');
565
566 $this->assertSame($event::LEVEL_TEACHING, $event->level);
567 $this->assertDebuggingCalled('level property is deprecated, use edulevel property instead');
568
569 $this->assertTrue(isset($event->level));
570 $this->assertDebuggingCalled('level property is deprecated, use edulevel property instead');
571
572 $this->assertSame($event::LEVEL_TEACHING, $event->edulevel);
573 }
574
d8a1f426 575 public function test_legacy() {
770eac98 576 global $DB, $CFG;
d8a1f426
PS
577
578 $this->resetAfterTest(true);
579
580 $observers = array(
581 array(
582 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 583 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
584 ),
585 array(
586 'eventname' => '*',
4b734e74 587 'callback' => '\core_tests\event\unittest_observer::observe_all',
d8a1f426
PS
588 'includefile' => null,
589 'internal' => 1,
590 'priority' => 9999,
591 ),
592 );
593
594 $DB->delete_records('log', array());
595 events_update_definition('unittest');
20ff2fba 596 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
d8a1f426
PS
597 $DB->delete_records_select('events_handlers', "component <> 'unittest'");
598 events_get_handlers('reset');
599 $this->assertEquals(3, $DB->count_records('events_handlers'));
600 set_config('loglifetime', 60*60*24*5);
601
602 \core\event\manager::phpunit_replace_observers($observers);
603 \core_tests\event\unittest_observer::reset();
604
9ede00db 605 $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>5, 'xx'=>10)));
d8a1f426 606 $event1->trigger();
20ff2fba 607 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
d8a1f426 608
9ede00db 609 $event2 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>6, 'xx'=>11)));
d8a1f426
PS
610 $event2->nest = true;
611 $event2->trigger();
20ff2fba 612 $this->assertDebuggingCalledCount(2, array(self::DEBUGGING_MSG, self::DEBUGGING_MSG), array(DEBUG_DEVELOPER, DEBUG_DEVELOPER));
d8a1f426 613
d8a1f426 614 $this->assertSame(
9ede00db 615 array('observe_all-5', 'observe_one-5', 'legacy_handler-0', 'observe_all-nesting-6', 'legacy_handler-0', 'observe_one-6', 'observe_all-666', 'observe_one-666', 'legacy_handler-0'),
d8a1f426
PS
616 \core_tests\event\unittest_observer::$info);
617
618 $this->assertSame($event1, \core_tests\event\unittest_observer::$event[0]);
619 $this->assertSame($event1, \core_tests\event\unittest_observer::$event[1]);
9ede00db 620 $this->assertSame(array(0, 5), \core_tests\event\unittest_observer::$event[2]);
d8a1f426 621
d8a1f426 622 $logs = $DB->get_records('log', array(), 'id ASC');
bdaf91fd 623 $this->assertCount(0, $logs);
d8a1f426
PS
624 }
625
626 public function test_restore_event() {
9ede00db 627 $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
628 $data1 = $event1->get_data();
629
4b734e74 630 $event2 = \core\event\base::restore($data1, array('origin'=>'clid'));
d8a1f426
PS
631 $data2 = $event2->get_data();
632
633 $this->assertTrue($event2->is_triggered());
634 $this->assertTrue($event2->is_restored());
635 $this->assertEquals($data1, $data2);
636 $this->assertInstanceOf('core_tests\event\unittest_executed', $event2);
637
638 $this->assertEquals($event1->get_context(), $event2->get_context());
639
640 // Now test problematic data.
641 $data3 = $data1;
642 $data3['eventname'] = '\\a\\b\\c';
4b734e74 643 $event3 = \core\event\base::restore($data3, array());
d8a1f426
PS
644 $this->assertFalse($event3, 'Class name must match');
645
646 $data4 = $data1;
647 unset($data4['userid']);
4b734e74 648 $event4 = \core\event\base::restore($data4, array());
d8a1f426
PS
649 $this->assertInstanceOf('core_tests\event\unittest_executed', $event4);
650 $this->assertDebuggingCalled();
651
652 $data5 = $data1;
653 $data5['xx'] = 'xx';
4b734e74 654 $event5 = \core\event\base::restore($data5, array());
d8a1f426 655 $this->assertInstanceOf('core_tests\event\unittest_executed', $event5);
4b734e74 656 $this->assertDebuggingCalled();
d8a1f426
PS
657
658 }
659
660 public function test_trigger_problems() {
7eaca5a8
661 $this->resetAfterTest(true);
662
9ede00db 663 $event = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>5, 'xx'=>10)));
d8a1f426
PS
664 $event->trigger();
665 try {
666 $event->trigger();
667 $this->fail('Exception expected on double trigger');
96deb950 668 } catch (\moodle_exception $e) {
d8a1f426
PS
669 $this->assertInstanceOf('coding_exception', $e);
670 }
671
672 $data = $event->get_data();
4b734e74 673 $restored = \core_tests\event\unittest_executed::restore($data, array());
d8a1f426
PS
674 $this->assertTrue($restored->is_triggered());
675 $this->assertTrue($restored->is_restored());
676
677 try {
678 $restored->trigger();
679 $this->fail('Exception expected on triggering of restored event');
680 } catch (\moodle_exception $e) {
681 $this->assertInstanceOf('coding_exception', $e);
682 }
22626564 683
9ede00db 684 $event = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>5, 'xx'=>10)));
22626564
PS
685 try {
686 \core\event\manager::dispatch($event);
687 $this->fail('Exception expected on manual event dispatching');
688 } catch (\moodle_exception $e) {
689 $this->assertInstanceOf('coding_exception', $e);
690 }
d8a1f426
PS
691 }
692
693 public function test_bad_events() {
7eaca5a8
694 $this->resetAfterTest(true);
695
27af3e62 696 try {
9ede00db 697 $event = \core_tests\event\unittest_executed::create(array('other'=>array('sample'=>5, 'xx'=>10)));
27af3e62 698 $this->fail('Exception expected when context and contextid missing');
96deb950 699 } catch (\moodle_exception $e) {
27af3e62
PS
700 $this->assertInstanceOf('coding_exception', $e);
701 }
702
703 $event = \core_tests\event\bad_event1::create(array('context'=>\context_system::instance()));
d8a1f426
PS
704 try {
705 $event->trigger();
706 $this->fail('Exception expected when $data not valid');
707 } catch (\moodle_exception $e) {
708 $this->assertInstanceOf('\coding_exception', $e);
709 }
710
27af3e62 711 $event = \core_tests\event\bad_event2::create(array('context'=>\context_system::instance()));
d8a1f426
PS
712 try {
713 $event->trigger();
714 $this->fail('Exception expected when $data not valid');
715 } catch (\moodle_exception $e) {
716 $this->assertInstanceOf('\coding_exception', $e);
717 }
718
38d6fbfa
FM
719 $event = \core_tests\event\bad_event2b::create(array('context'=>\context_system::instance()));
720 @$event->trigger();
721 $this->assertDebuggingCalled();
722
27af3e62 723 $event = \core_tests\event\bad_event3::create(array('context'=>\context_system::instance()));
4b734e74 724 @$event->trigger();
d8a1f426
PS
725 $this->assertDebuggingCalled();
726
27af3e62 727 $event = \core_tests\event\bad_event4::create(array('context'=>\context_system::instance()));
4b734e74 728 @$event->trigger();
d8a1f426
PS
729 $this->assertDebuggingCalled();
730
27af3e62 731 $event = \core_tests\event\bad_event5::create(array('context'=>\context_system::instance()));
4b734e74 732 @$event->trigger();
d8a1f426 733 $this->assertDebuggingCalled();
a85258ca 734
31e571cd 735 $event = \core_tests\event\bad_event6::create(array('objectid'=>1, 'context'=>\context_system::instance()));
a85258ca 736 $event->trigger();
31e571cd 737 $this->assertDebuggingCalled('Unknown table specified in objecttable field');
a85258ca 738
27af3e62 739 $event = \core_tests\event\bad_event7::create(array('objectid'=>1, 'context'=>\context_system::instance()));
a85258ca
PS
740 try {
741 $event->trigger();
31e571cd 742 $this->fail('Exception expected when $data contains objectid but objecttable not specified');
a85258ca
PS
743 } catch (\moodle_exception $e) {
744 $this->assertInstanceOf('\coding_exception', $e);
745 }
31e571cd
FM
746
747 $event = \core_tests\event\bad_event8::create(array('context'=>\context_system::instance()));
748 $event->trigger();
749 $this->assertDebuggingCalled('Event property objectid must be set when objecttable is defined');
d8a1f426
PS
750 }
751
752 public function test_problematic_events() {
7eaca5a8
753 $this->resetAfterTest(true);
754
27af3e62 755 $event1 = \core_tests\event\problematic_event1::create(array('context'=>\context_system::instance()));
d8a1f426 756 $this->assertDebuggingNotCalled();
4b734e74
PS
757 $this->assertNull($event1->xxx);
758 $this->assertDebuggingCalled();
d8a1f426 759
27af3e62 760 $event2 = \core_tests\event\problematic_event1::create(array('xxx'=>0, 'context'=>\context_system::instance()));
d8a1f426
PS
761 $this->assertDebuggingCalled();
762
96f81ea3 763 set_debugging(DEBUG_NONE);
27af3e62 764 $event3 = \core_tests\event\problematic_event1::create(array('xxx'=>0, 'context'=>\context_system::instance()));
d8a1f426 765 $this->assertDebuggingNotCalled();
96f81ea3 766 set_debugging(DEBUG_DEVELOPER);
d8a1f426 767
27af3e62 768 $event4 = \core_tests\event\problematic_event1::create(array('context'=>\context_system::instance(), 'other'=>array('a'=>1)));
d8a1f426
PS
769 $event4->trigger();
770 $this->assertDebuggingNotCalled();
771
27af3e62 772 $event5 = \core_tests\event\problematic_event1::create(array('context'=>\context_system::instance(), 'other'=>(object)array('a'=>1)));
d8a1f426
PS
773 $this->assertDebuggingNotCalled();
774 $event5->trigger();
775 $this->assertDebuggingCalled();
776
777 $url = new moodle_url('/admin/');
27af3e62 778 $event6 = \core_tests\event\problematic_event1::create(array('context'=>\context_system::instance(), 'other'=>array('a'=>$url)));
d8a1f426
PS
779 $this->assertDebuggingNotCalled();
780 $event6->trigger();
781 $this->assertDebuggingCalled();
fddd1018 782
5bb7158d 783 // Check that whole float numbers do not trigger debugging messages.
9ede00db 784 $event7 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(),
5bb7158d
AG
785 'other' => array('wholenumber' => 90.0000, 'numberwithdecimals' => 54.7656, 'sample' => 1)));
786 $event7->trigger();
787 $this->assertDebuggingNotCalled();
788
f8a39659 789 $event = \core_tests\event\problematic_event2::create(array());
097011c4 790 $this->assertDebuggingNotCalled();
f8a39659 791 $event = \core_tests\event\problematic_event2::create(array('context'=>\context_system::instance()));
097011c4 792 $this->assertDebuggingCalled();
fddd1018 793
f8a39659 794 $event = \core_tests\event\problematic_event3::create(array('other'=>1));
fddd1018 795 $this->assertDebuggingNotCalled();
f8a39659 796 $event = \core_tests\event\problematic_event3::create(array());
fddd1018 797 $this->assertDebuggingCalled();
d8a1f426
PS
798 }
799
5fef139c 800 public function test_record_snapshots() {
d8a1f426
PS
801 global $DB;
802
7eaca5a8
803 $this->resetAfterTest(true);
804
9ede00db 805 $event = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
806 $course1 = $DB->get_record('course', array('id'=>1));
807 $this->assertNotEmpty($course1);
808
5fef139c 809 $event->add_record_snapshot('course', $course1);
d8a1f426 810
9ede00db 811 $result = $event->get_record_snapshot('course', $course1->id);
5e70ea26
MG
812 // Convert to arrays because record snapshot returns a clone of the object.
813 $this->assertSame((array)$course1, (array)$result);
d8a1f426 814
5fef139c 815 $user = $event->get_record_snapshot('user', 1);
d8a1f426
PS
816 $this->assertEquals(1, $user->id);
817 $this->assertSame('guest', $user->username);
300dbc66
PS
818
819 $event->add_record_snapshot('course', $course1);
820 $event->trigger();
821 try {
822 $event->add_record_snapshot('course', $course1);
823 $this->fail('Updating of snapshots after trigger is not ok');;
824 } catch (\moodle_exception $e) {
825 $this->assertInstanceOf('\coding_exception', $e);
826 }
4ad6d5c5
PS
827
828 $event2 = \core_tests\event\unittest_executed::restore($event->get_data(), array());
829 try {
9ede00db 830 $event2->get_record_snapshot('course', $course1->id);
4ad6d5c5
PS
831 $this->fail('Reading of snapshots from restored events is not ok');;
832 } catch (\moodle_exception $e) {
833 $this->assertInstanceOf('\coding_exception', $e);
834 }
d8a1f426 835 }
ed17808d 836
1ccc1268 837 public function test_get_name() {
9ede00db 838 $event = \core_tests\event\noname_event::create(array('other' => array('sample' => 1, 'xx' => 10)));
1ccc1268
AA
839 $this->assertEquals("core_tests: noname event", $event->get_name());
840 }
841
ed17808d 842 public function test_iteration() {
9ede00db 843 $event = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
ed17808d
PS
844
845 $data = array();
846 foreach ($event as $k => $v) {
847 $data[$k] = $v;
848 }
849
850 $this->assertSame($event->get_data(), $data);
851 }
623a32e5
RT
852
853 /**
854 * @expectedException PHPUnit_Framework_Error_Notice
855 */
856 public function test_context_not_used() {
9ede00db 857 $event = \core_tests\event\context_used_in_event::create(array('other' => array('sample' => 1, 'xx' => 10)));
623a32e5
RT
858 $this->assertEventContextNotUsed($event);
859
860 $eventcontext = phpunit_event_mock::testable_get_event_context($event);
861 phpunit_event_mock::testable_set_event_context($event, null);
862 $this->assertEventContextNotUsed($event);
863 }
7c084af9
AG
864
865 /**
866 * Test that all observer information is returned correctly.
867 */
868 public function test_get_all_observers() {
869 // Retrieve all observers.
870 $observers = \core\event\manager::get_all_observers();
871
872 // Expected information from the workshop allocation scheduled observer.
873 $expected = array();
874 $observer = new stdClass();
875 $observer->callable = '\workshopallocation_scheduled\observer::workshop_viewed';
876 $observer->priority = 0;
877 $observer->internal = true;
878 $observer->includefile = null;
879 $observer->plugintype = 'workshopallocation';
880 $observer->plugin = 'scheduled';
881 $expected[0] = $observer;
882
883 $this->assertEquals($expected, $observers['\mod_workshop\event\course_module_viewed']);
884 }
75cab92b
AG
885
886 /**
887 * Test formatting of the get_explanation method.
888 * This formats the information from an events class docblock.
889 */
890 public function test_get_explanation() {
891 $explanation = \core_tests\event\full_docblock::get_explanation();
892
893 $expected = "This is an explanation of the event.
894 - I'm making a point here.
895 - I have a second {@link something} point here.
896 - whitespace is intentional to test it's removal.
897I have something else *Yeah* that.";
898
899 $this->assertEquals($explanation, $expected);
900
901 $explanation = \core_tests\event\docblock_test2::get_explanation();
902
903 $expected = "We have only the description in the docblock
904and nothing else.";
905
906 $this->assertEquals($explanation, $expected);
907
908 $explanation = \core_tests\event\docblock_test3::get_explanation();
909 $expected = "Calendar event created event.";
910 $this->assertEquals($explanation, $expected);
911
912 }
913
914 /**
915 * Test that general information about an event is returned
916 * by the get_static_info() method.
917 */
918 public function test_get_static_info() {
919 $staticinfo = \core_tests\event\static_info_viewing::get_static_info();
920
921 $expected = array(
922 'eventname' => '\\core_tests\\event\\static_info_viewing',
923 'component' => 'core_tests',
924 'target' => 'static_info',
925 'action' => 'viewing',
926 'crud' => 'r',
927 'edulevel' => 0,
928 'objecttable' => 'mod_unittest'
929 );
930 $this->assertEquals($staticinfo, $expected);
931 }
2df73956
MN
932
933 /**
934 * This tests the internal method of \core\event\manager::get_observing_classes.
935 *
936 * What we are testing is if we can subscribe to a parent event class, instead of only
937 * the base event class or the final, implemented event class. This enables us to subscribe
938 * to things like all course module view events, all comment created events, etc.
939 */
940 public function test_observe_parent_event() {
941 $this->resetAfterTest();
942
943 // Ensure this has been reset prior to using it.
944 \core_tests\event\unittest_observer::reset();
945
946 $course = $this->getDataGenerator()->create_course();
947 $feed = $this->getDataGenerator()->create_module('feedback', ['course' => $course->id]);
948 $context = context_module::instance($feed->cmid);
949 $data = [
950 'context' => $context,
951 'courseid' => $course->id,
952 'objectid' => $feed->id
953 ];
954
955 // This assertion ensures that basic observe use case did not break.
956 \core\event\manager::phpunit_replace_observers([[
957 'eventname' => '\core_tests\event\course_module_viewed',
958 'callback' => ['\core_tests\event\unittest_observer', 'observe_all_alt'],
959 ]]);
960
961 $pageevent = \core_tests\event\course_module_viewed::create($data);
962 $pageevent->trigger();
963
dc9afaba 964 $this->assertSame(['observe_all_alt'], \core_tests\event\unittest_observer::$info, 'Error observing triggered event');
2df73956
MN
965
966 \core_tests\event\unittest_observer::reset();
967
968 // This assertion tests that we can observe an abstract (parent) class instead of the implemented class.
969 \core\event\manager::phpunit_replace_observers([[
970 'eventname' => '\core\event\course_module_viewed',
971 'callback' => ['\core_tests\event\unittest_observer', 'observe_all_alt'],
972 ]]);
973
974 $pageevent = \core_tests\event\course_module_viewed::create($data);
975 $pageevent->trigger();
976
dc9afaba 977 $this->assertSame(['observe_all_alt'], \core_tests\event\unittest_observer::$info, 'Error observing parent class event');
2df73956
MN
978
979 \core_tests\event\unittest_observer::reset();
980 }
605a8c33 981}