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