MDL-43736 Events: event->contextinstanceid should be used where possible
[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
ef1987dc
PS
32 protected function setUp() {
33 global $CFG;
34 // No need to always modify log table here.
35 $CFG->loglifetime = '-1';
36 }
37
38 protected function tearDown() {
39 global $CFG;
40 $CFG->loglifetime = '0';
41 }
42
605a8c33
PS
43 public function test_event_properties() {
44 global $USER;
45
46 $system = \context_system::instance();
c4297815 47 $event = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>$system, 'objectid'=>5, 'other'=>array('sample'=>null, 'xx'=>10)));
605a8c33
PS
48
49 $this->assertSame('\core_tests\event\unittest_executed', $event->eventname);
50 $this->assertSame('core_tests', $event->component);
51 $this->assertSame('executed', $event->action);
660f049f 52 $this->assertSame('unittest', $event->target);
605a8c33
PS
53 $this->assertSame(5, $event->objectid);
54 $this->assertSame('u', $event->crud);
3345e24f 55 $this->assertSame(\core\event\base::LEVEL_PARTICIPATING, $event->edulevel);
605a8c33 56
96deb950 57 $this->assertEquals($system, $event->get_context());
605a8c33
PS
58 $this->assertSame($system->id, $event->contextid);
59 $this->assertSame($system->contextlevel, $event->contextlevel);
60 $this->assertSame($system->instanceid, $event->contextinstanceid);
61
62 $this->assertSame($USER->id, $event->userid);
63 $this->assertSame(1, $event->courseid);
64
65 $this->assertNull($event->relateduserid);
66 $this->assertFalse(isset($event->relateduserid));
67
c4297815
PS
68 $this->assertSame(array('sample'=>null, 'xx'=>10), $event->other);
69 $this->assertTrue(isset($event->other['xx']));
70 $this->assertFalse(isset($event->other['sample']));
605a8c33
PS
71
72 $this->assertLessThanOrEqual(time(), $event->timecreated);
73
74 try {
75 $event->courseid = 2;
76 $this->fail('Exception expected on event modification');
77 } catch (\moodle_exception $e) {
78 $this->assertInstanceOf('coding_exception', $e);
79 }
80
81 try {
82 $event->xxxx = 1;
83 $this->fail('Exception expected on event modification');
84 } catch (\moodle_exception $e) {
85 $this->assertInstanceOf('coding_exception', $e);
86 }
27af3e62
PS
87
88 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'contextid'=>$system->id, 'objectid'=>5, 'other'=>array('sample'=>null, 'xx'=>10)));
96deb950 89 $this->assertEquals($event->get_context(), $event2->get_context());
605a8c33
PS
90 }
91
513d8793
FM
92 public function test_event_properties_guessing() {
93 global $USER;
94 $this->resetAfterTest();
95
96 $course = $this->getDataGenerator()->create_course();
97 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
98 $context = context_module::instance($forum->cmid);
99 $event = \core_tests\event\unittest_executed::create(array('context' => $context, 'objectid' => 5));
100
101 // Check guessed course ID, and default properties.
102 $this->assertSame('\core_tests\event\unittest_executed', $event->eventname);
103 $this->assertSame('core_tests', $event->component);
104 $this->assertSame('executed', $event->action);
105 $this->assertSame('unittest', $event->target);
106 $this->assertSame(5, $event->objectid);
107 $this->assertEquals($context, $event->get_context());
108 $this->assertEquals($course->id, $event->courseid);
109 $this->assertSame($USER->id, $event->userid);
110 $this->assertNull($event->relateduserid);
111
112 $user = $this->getDataGenerator()->create_user();
113 $context = context_user::instance($user->id);
114 $event = \core_tests\event\unittest_executed::create(array('contextid' => $context->id, 'objectid' => 5));
115
116 // Check guessing on contextid, and user context level.
117 $this->assertEquals($context, $event->get_context());
118 $this->assertEquals($context->id, $event->contextid);
119 $this->assertEquals($context->contextlevel, $event->contextlevel);
120 $this->assertSame(0, $event->courseid);
121 $this->assertSame($USER->id, $event->userid);
122 $this->assertSame($user->id, $event->relateduserid);
123 }
124
d8a1f426 125 public function test_observers_parsing() {
111c6496 126 global $CFG;
d8a1f426
PS
127
128 $observers = array(
2d1884d9
PS
129 array(
130 'eventname' => '*',
131 'callback' => array('\core_tests\event\unittest_observer', 'observe_all_alt'),
132 ),
d8a1f426
PS
133 array(
134 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 135 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
136 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
137 ),
138 array(
139 'eventname' => '*',
4b734e74 140 'callback' => array('\core_tests\event\unittest_observer', 'observe_all'),
d8a1f426
PS
141 'includefile' => null,
142 'internal' => 1,
2d1884d9 143 'priority' => 10,
d8a1f426
PS
144 ),
145 array(
146 'eventname' => '\core\event\unknown_executed',
4b734e74 147 'callback' => '\core_tests\event\unittest_observer::broken_observer',
d8a1f426
PS
148 'priority' => 100,
149 ),
150 array(
151 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 152 'callback' => '\core_tests\event\unittest_observer::external_observer',
d8a1f426
PS
153 'priority' => 200,
154 'internal' => 0,
155 ),
156 );
157
158 $result = \core\event\manager::phpunit_replace_observers($observers);
d8a1f426 159 $this->assertCount(3, $result);
d8a1f426
PS
160
161 $expected = array();
162 $observer = new stdClass();
d8a1f426
PS
163 $observer->callable = '\core_tests\event\unittest_observer::external_observer';
164 $observer->priority = 200;
165 $observer->internal = false;
166 $observer->includefile = null;
2d1884d9 167 $expected[0] = $observer;
d8a1f426
PS
168 $observer = new stdClass();
169 $observer->callable = '\core_tests\event\unittest_observer::observe_one';
170 $observer->priority = 0;
171 $observer->internal = true;
111c6496 172 $observer->includefile = $CFG->dirroot.'/lib/tests/fixtures/event_fixtures.php';
2d1884d9 173 $expected[1] = $observer;
d8a1f426
PS
174
175 $this->assertEquals($expected, $result['\core_tests\event\unittest_executed']);
176
177 $expected = array();
178 $observer = new stdClass();
d8a1f426
PS
179 $observer->callable = '\core_tests\event\unittest_observer::broken_observer';
180 $observer->priority = 100;
181 $observer->internal = true;
182 $observer->includefile = null;
2d1884d9 183 $expected[0] = $observer;
d8a1f426
PS
184
185 $this->assertEquals($expected, $result['\core\event\unknown_executed']);
186
187 $expected = array();
188 $observer = new stdClass();
4b734e74 189 $observer->callable = array('\core_tests\event\unittest_observer', 'observe_all');
2d1884d9 190 $observer->priority = 10;
d8a1f426
PS
191 $observer->internal = true;
192 $observer->includefile = null;
193 $expected[0] = $observer;
2d1884d9
PS
194 $observer = new stdClass();
195 $observer->callable = array('\core_tests\event\unittest_observer', 'observe_all_alt');
196 $observer->priority = 0;
197 $observer->internal = true;
198 $observer->includefile = null;
199 $expected[1] = $observer;
d8a1f426 200
2d1884d9 201 $this->assertEquals($expected, $result['\core\event\base']);
d8a1f426 202
d8a1f426
PS
203 // Now test broken stuff...
204
205 $observers = array(
206 array(
207 'eventname' => 'core_tests\event\unittest_executed', // Fix leading backslash.
4b734e74 208 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
209 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
210 'internal' => 1, // Cast to bool.
211 ),
212 );
213 $result = \core\event\manager::phpunit_replace_observers($observers);
214 $this->assertCount(1, $result);
215 $expected = array();
216 $observer = new stdClass();
217 $observer->callable = '\core_tests\event\unittest_observer::observe_one';
218 $observer->priority = 0;
219 $observer->internal = true;
111c6496 220 $observer->includefile = $CFG->dirroot.'/lib/tests/fixtures/event_fixtures.php';
d8a1f426
PS
221 $expected[0] = $observer;
222 $this->assertEquals($expected, $result['\core_tests\event\unittest_executed']);
223
224 $observers = array(
225 array(
226 // Missing eventclass.
4b734e74 227 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
228 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
229 ),
230 );
231 $result = \core\event\manager::phpunit_replace_observers($observers);
232 $this->assertCount(0, $result);
233 $this->assertDebuggingCalled();
234
235 $observers = array(
236 array(
237 'eventname' => '', // Empty eventclass.
4b734e74 238 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
239 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
240 ),
241 );
242 $result = \core\event\manager::phpunit_replace_observers($observers);
243 $this->assertCount(0, $result);
244 $this->assertDebuggingCalled();
245
246 $observers = array(
247 array(
248 'eventname' => '\core_tests\event\unittest_executed',
249 // Missing callable.
250 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
251 ),
252 );
253 $result = \core\event\manager::phpunit_replace_observers($observers);
254 $this->assertCount(0, $result);
255 $this->assertDebuggingCalled();
256
257 $observers = array(
258 array(
259 'eventname' => '\core_tests\event\unittest_executed',
96deb950 260 'callback' => '', // Empty callable.
d8a1f426
PS
261 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
262 ),
263 );
264 $result = \core\event\manager::phpunit_replace_observers($observers);
265 $this->assertCount(0, $result);
266 $this->assertDebuggingCalled();
267
268 $observers = array(
269 array(
270 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 271 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
272 'includefile' => 'lib/tests/fixtures/event_fixtures.php_xxx', // Missing file.
273 ),
274 );
275 $result = \core\event\manager::phpunit_replace_observers($observers);
276 $this->assertCount(0, $result);
277 $this->assertDebuggingCalled();
278 }
279
280 public function test_normal_dispatching() {
281 $observers = array(
282 array(
283 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 284 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
285 ),
286 array(
287 'eventname' => '*',
4b734e74 288 'callback' => '\core_tests\event\unittest_observer::observe_all',
d8a1f426
PS
289 'includefile' => null,
290 'internal' => 1,
291 'priority' => 9999,
292 ),
293 );
294
295 \core\event\manager::phpunit_replace_observers($observers);
296 \core_tests\event\unittest_observer::reset();
297
c4297815 298 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
299 $event1->nest = 1;
300 $this->assertFalse($event1->is_triggered());
22626564 301 $this->assertFalse($event1->is_dispatched());
d8a1f426
PS
302 $this->assertFalse($event1->is_restored());
303 $event1->trigger();
304 $this->assertTrue($event1->is_triggered());
22626564 305 $this->assertTrue($event1->is_dispatched());
d8a1f426
PS
306 $this->assertFalse($event1->is_restored());
307
c4297815 308 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
309 $event1->trigger();
310
311 $this->assertSame(
312 array('observe_all-nesting-1', 'observe_one-1', 'observe_all-3', 'observe_one-3', 'observe_all-2', 'observe_one-2'),
313 \core_tests\event\unittest_observer::$info);
314 }
315
62401e8f
PS
316 public function test_event_sink() {
317 $sink = $this->redirectEvents();
318 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
319 $event1->trigger();
320 $this->assertSame(1, $sink->count());
321 $retult = $sink->get_events();
322 $this->assertSame($event1, $retult[0]);
323
324 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
325 $event2->trigger();
326 $this->assertSame(2, $sink->count());
327 $retult = $sink->get_events();
328 $this->assertSame($event1, $retult[0]);
329 $this->assertSame($event2, $retult[1]);
330
331 $sink->clear();
332 $this->assertSame(0, $sink->count());
333 $this->assertSame(array(), $sink->get_events());
334
335 $event3 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>3, 'xx'=>10)));
336 $event3->trigger();
337 $this->assertSame(1, $sink->count());
338 $retult = $sink->get_events();
339 $this->assertSame($event3, $retult[0]);
340
341 $sink->close();
342 $event4 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>4, 'xx'=>10)));
343 $event4->trigger();
344 $this->assertSame(1, $sink->count());
345 $retult = $sink->get_events();
346 $this->assertSame($event3, $retult[0]);
347 }
348
d8a1f426
PS
349 public function test_ignore_exceptions() {
350 $observers = array(
351
352 array(
353 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 354 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
355 ),
356
357 array(
358 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 359 'callback' => '\core_tests\event\unittest_observer::broken_observer',
d8a1f426
PS
360 'priority' => 100,
361 ),
362 );
363
364 \core\event\manager::phpunit_replace_observers($observers);
365 \core_tests\event\unittest_observer::reset();
366
c4297815 367 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
368 $event1->trigger();
369 $this->assertDebuggingCalled();
370
c4297815 371 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
372 $event1->trigger();
373 $this->assertDebuggingCalled();
374
375 $this->assertSame(
376 array('broken_observer-1', 'observe_one-1', 'broken_observer-2', 'observe_one-2'),
377 \core_tests\event\unittest_observer::$info);
378 }
379
380 public function test_external_buffer() {
381 global $DB;
382
383 $this->preventResetByRollback();
384
385 $observers = array(
386
387 array(
388 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 389 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
390 ),
391
392 array(
393 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 394 'callback' => '\core_tests\event\unittest_observer::external_observer',
d8a1f426
PS
395 'priority' => 200,
396 'internal' => 0,
397 ),
398 );
399
400 \core\event\manager::phpunit_replace_observers($observers);
401 \core_tests\event\unittest_observer::reset();
402
c4297815 403 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426 404 $event1->trigger();
c4297815 405 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
406 $event2->trigger();
407
408 $this->assertSame(
409 array('external_observer-1', 'observe_one-1', 'external_observer-2', 'observe_one-2'),
410 \core_tests\event\unittest_observer::$info);
411
412 \core\event\manager::phpunit_replace_observers($observers);
413 \core_tests\event\unittest_observer::reset();
414
415 $this->assertSame(array(), \core_tests\event\unittest_observer::$info);
416
417 $trans = $DB->start_delegated_transaction();
418
c4297815 419 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426 420 $event1->trigger();
c4297815 421 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
422 $event2->trigger();
423
424 $this->assertSame(
425 array('observe_one-1', 'observe_one-2'),
426 \core_tests\event\unittest_observer::$info);
427
428 $trans->allow_commit();
429
430 $this->assertSame(
431 array('observe_one-1', 'observe_one-2', 'external_observer-1', 'external_observer-2'),
432 \core_tests\event\unittest_observer::$info);
433
434 \core\event\manager::phpunit_replace_observers($observers);
435 \core_tests\event\unittest_observer::reset();
436
c4297815 437 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
438 $event1->trigger();
439 $trans = $DB->start_delegated_transaction();
c4297815 440 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
441 $event2->trigger();
442 try {
443 $trans->rollback(new \moodle_exception('xxx'));
444 $this->fail('Expecting exception');
445 } catch (\moodle_exception $e) {
96deb950 446 $this->assertInstanceOf('moodle_exception', $e);
d8a1f426
PS
447 }
448
449 $this->assertSame(
450 array('external_observer-1', 'observe_one-1', 'observe_one-2'),
451 \core_tests\event\unittest_observer::$info);
452 }
453
3345e24f
454 public function test_deprecated() {
455 global $DB;
456
457 $this->resetAfterTest(true);
458
459 $event = \core_tests\event\deprecated_event1::create();
460 $this->assertDebuggingCalled('level property is deprecated, use edulevel property instead');
461
462 $this->assertSame($event::LEVEL_TEACHING, $event->level);
463 $this->assertDebuggingCalled('level property is deprecated, use edulevel property instead');
464
465 $this->assertTrue(isset($event->level));
466 $this->assertDebuggingCalled('level property is deprecated, use edulevel property instead');
467
468 $this->assertSame($event::LEVEL_TEACHING, $event->edulevel);
469 }
470
d8a1f426
PS
471 public function test_legacy() {
472 global $DB;
473
474 $this->resetAfterTest(true);
475
476 $observers = array(
477 array(
478 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 479 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
480 ),
481 array(
482 'eventname' => '*',
4b734e74 483 'callback' => '\core_tests\event\unittest_observer::observe_all',
d8a1f426
PS
484 'includefile' => null,
485 'internal' => 1,
486 'priority' => 9999,
487 ),
488 );
489
490 $DB->delete_records('log', array());
491 events_update_definition('unittest');
492 $DB->delete_records_select('events_handlers', "component <> 'unittest'");
493 events_get_handlers('reset');
494 $this->assertEquals(3, $DB->count_records('events_handlers'));
495 set_config('loglifetime', 60*60*24*5);
496
497 \core\event\manager::phpunit_replace_observers($observers);
498 \core_tests\event\unittest_observer::reset();
499
c4297815 500 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>5, 'xx'=>10)));
d8a1f426
PS
501 $event1->trigger();
502
c4297815 503 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'other'=>array('sample'=>6, 'xx'=>11)));
d8a1f426
PS
504 $event2->nest = true;
505 $event2->trigger();
506
d8a1f426
PS
507 $this->assertSame(
508 array('observe_all-1', 'observe_one-1', 'legacy_handler-1', 'observe_all-nesting-2', 'legacy_handler-3', 'observe_one-2', 'observe_all-3', 'observe_one-3', 'legacy_handler-2'),
509 \core_tests\event\unittest_observer::$info);
510
511 $this->assertSame($event1, \core_tests\event\unittest_observer::$event[0]);
512 $this->assertSame($event1, \core_tests\event\unittest_observer::$event[1]);
513 $this->assertSame(array(1, 5), \core_tests\event\unittest_observer::$event[2]);
514
d8a1f426
PS
515 $logs = $DB->get_records('log', array(), 'id ASC');
516 $this->assertCount(3, $logs);
517
518 $log = array_shift($logs);
519 $this->assertEquals(1, $log->course);
520 $this->assertSame('core_unittest', $log->module);
521 $this->assertSame('view', $log->action);
522
523 $log = array_shift($logs);
524 $this->assertEquals(2, $log->course);
525 $this->assertSame('core_unittest', $log->module);
526 $this->assertSame('view', $log->action);
527
528 $log = array_shift($logs);
529 $this->assertEquals(3, $log->course);
530 $this->assertSame('core_unittest', $log->module);
531 $this->assertSame('view', $log->action);
532 }
533
534 public function test_restore_event() {
c4297815 535 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
536 $data1 = $event1->get_data();
537
4b734e74 538 $event2 = \core\event\base::restore($data1, array('origin'=>'clid'));
d8a1f426
PS
539 $data2 = $event2->get_data();
540
541 $this->assertTrue($event2->is_triggered());
542 $this->assertTrue($event2->is_restored());
543 $this->assertEquals($data1, $data2);
544 $this->assertInstanceOf('core_tests\event\unittest_executed', $event2);
545
546 $this->assertEquals($event1->get_context(), $event2->get_context());
547
548 // Now test problematic data.
549 $data3 = $data1;
550 $data3['eventname'] = '\\a\\b\\c';
4b734e74 551 $event3 = \core\event\base::restore($data3, array());
d8a1f426
PS
552 $this->assertFalse($event3, 'Class name must match');
553
554 $data4 = $data1;
555 unset($data4['userid']);
4b734e74 556 $event4 = \core\event\base::restore($data4, array());
d8a1f426
PS
557 $this->assertInstanceOf('core_tests\event\unittest_executed', $event4);
558 $this->assertDebuggingCalled();
559
560 $data5 = $data1;
561 $data5['xx'] = 'xx';
4b734e74 562 $event5 = \core\event\base::restore($data5, array());
d8a1f426 563 $this->assertInstanceOf('core_tests\event\unittest_executed', $event5);
4b734e74 564 $this->assertDebuggingCalled();
d8a1f426
PS
565
566 }
567
568 public function test_trigger_problems() {
c4297815 569 $event = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>5, 'xx'=>10)));
d8a1f426
PS
570 $event->trigger();
571 try {
572 $event->trigger();
573 $this->fail('Exception expected on double trigger');
96deb950 574 } catch (\moodle_exception $e) {
d8a1f426
PS
575 $this->assertInstanceOf('coding_exception', $e);
576 }
577
578 $data = $event->get_data();
4b734e74 579 $restored = \core_tests\event\unittest_executed::restore($data, array());
d8a1f426
PS
580 $this->assertTrue($restored->is_triggered());
581 $this->assertTrue($restored->is_restored());
582
583 try {
584 $restored->trigger();
585 $this->fail('Exception expected on triggering of restored event');
586 } catch (\moodle_exception $e) {
587 $this->assertInstanceOf('coding_exception', $e);
588 }
22626564 589
c4297815 590 $event = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>5, 'xx'=>10)));
22626564
PS
591 try {
592 \core\event\manager::dispatch($event);
593 $this->fail('Exception expected on manual event dispatching');
594 } catch (\moodle_exception $e) {
595 $this->assertInstanceOf('coding_exception', $e);
596 }
d8a1f426
PS
597 }
598
599 public function test_bad_events() {
27af3e62
PS
600 try {
601 $event = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'other'=>array('sample'=>5, 'xx'=>10)));
602 $this->fail('Exception expected when context and contextid missing');
96deb950 603 } catch (\moodle_exception $e) {
27af3e62
PS
604 $this->assertInstanceOf('coding_exception', $e);
605 }
606
607 $event = \core_tests\event\bad_event1::create(array('context'=>\context_system::instance()));
d8a1f426
PS
608 try {
609 $event->trigger();
610 $this->fail('Exception expected when $data not valid');
611 } catch (\moodle_exception $e) {
612 $this->assertInstanceOf('\coding_exception', $e);
613 }
614
27af3e62 615 $event = \core_tests\event\bad_event2::create(array('context'=>\context_system::instance()));
d8a1f426
PS
616 try {
617 $event->trigger();
618 $this->fail('Exception expected when $data not valid');
619 } catch (\moodle_exception $e) {
620 $this->assertInstanceOf('\coding_exception', $e);
621 }
622
38d6fbfa
FM
623 $event = \core_tests\event\bad_event2b::create(array('context'=>\context_system::instance()));
624 @$event->trigger();
625 $this->assertDebuggingCalled();
626
27af3e62 627 $event = \core_tests\event\bad_event3::create(array('context'=>\context_system::instance()));
4b734e74 628 @$event->trigger();
d8a1f426
PS
629 $this->assertDebuggingCalled();
630
27af3e62 631 $event = \core_tests\event\bad_event4::create(array('context'=>\context_system::instance()));
4b734e74 632 @$event->trigger();
d8a1f426
PS
633 $this->assertDebuggingCalled();
634
27af3e62 635 $event = \core_tests\event\bad_event5::create(array('context'=>\context_system::instance()));
4b734e74 636 @$event->trigger();
d8a1f426 637 $this->assertDebuggingCalled();
a85258ca 638
31e571cd 639 $event = \core_tests\event\bad_event6::create(array('objectid'=>1, 'context'=>\context_system::instance()));
a85258ca 640 $event->trigger();
31e571cd 641 $this->assertDebuggingCalled('Unknown table specified in objecttable field');
a85258ca 642
27af3e62 643 $event = \core_tests\event\bad_event7::create(array('objectid'=>1, 'context'=>\context_system::instance()));
a85258ca
PS
644 try {
645 $event->trigger();
31e571cd 646 $this->fail('Exception expected when $data contains objectid but objecttable not specified');
a85258ca
PS
647 } catch (\moodle_exception $e) {
648 $this->assertInstanceOf('\coding_exception', $e);
649 }
31e571cd
FM
650
651 $event = \core_tests\event\bad_event8::create(array('context'=>\context_system::instance()));
652 $event->trigger();
653 $this->assertDebuggingCalled('Event property objectid must be set when objecttable is defined');
d8a1f426
PS
654 }
655
656 public function test_problematic_events() {
657 global $CFG;
27af3e62 658 $event1 = \core_tests\event\problematic_event1::create(array('context'=>\context_system::instance()));
d8a1f426 659 $this->assertDebuggingNotCalled();
4b734e74
PS
660 $this->assertNull($event1->xxx);
661 $this->assertDebuggingCalled();
d8a1f426 662
27af3e62 663 $event2 = \core_tests\event\problematic_event1::create(array('xxx'=>0, 'context'=>\context_system::instance()));
d8a1f426
PS
664 $this->assertDebuggingCalled();
665
96f81ea3 666 set_debugging(DEBUG_NONE);
27af3e62 667 $event3 = \core_tests\event\problematic_event1::create(array('xxx'=>0, 'context'=>\context_system::instance()));
d8a1f426 668 $this->assertDebuggingNotCalled();
96f81ea3 669 set_debugging(DEBUG_DEVELOPER);
d8a1f426 670
27af3e62 671 $event4 = \core_tests\event\problematic_event1::create(array('context'=>\context_system::instance(), 'other'=>array('a'=>1)));
d8a1f426
PS
672 $event4->trigger();
673 $this->assertDebuggingNotCalled();
674
27af3e62 675 $event5 = \core_tests\event\problematic_event1::create(array('context'=>\context_system::instance(), 'other'=>(object)array('a'=>1)));
d8a1f426
PS
676 $this->assertDebuggingNotCalled();
677 $event5->trigger();
678 $this->assertDebuggingCalled();
679
680 $url = new moodle_url('/admin/');
27af3e62 681 $event6 = \core_tests\event\problematic_event1::create(array('context'=>\context_system::instance(), 'other'=>array('a'=>$url)));
d8a1f426
PS
682 $this->assertDebuggingNotCalled();
683 $event6->trigger();
684 $this->assertDebuggingCalled();
fddd1018 685
5bb7158d
AG
686 // Check that whole float numbers do not trigger debugging messages.
687 $event7 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(),
688 'other' => array('wholenumber' => 90.0000, 'numberwithdecimals' => 54.7656, 'sample' => 1)));
689 $event7->trigger();
690 $this->assertDebuggingNotCalled();
691
f8a39659 692 $event = \core_tests\event\problematic_event2::create(array());
097011c4 693 $this->assertDebuggingNotCalled();
f8a39659 694 $event = \core_tests\event\problematic_event2::create(array('context'=>\context_system::instance()));
097011c4 695 $this->assertDebuggingCalled();
fddd1018 696
f8a39659 697 $event = \core_tests\event\problematic_event3::create(array('other'=>1));
fddd1018 698 $this->assertDebuggingNotCalled();
f8a39659 699 $event = \core_tests\event\problematic_event3::create(array());
fddd1018 700 $this->assertDebuggingCalled();
d8a1f426
PS
701 }
702
5fef139c 703 public function test_record_snapshots() {
d8a1f426
PS
704 global $DB;
705
c4297815 706 $event = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
707 $course1 = $DB->get_record('course', array('id'=>1));
708 $this->assertNotEmpty($course1);
709
5fef139c 710 $event->add_record_snapshot('course', $course1);
d8a1f426 711
5fef139c 712 $result = $event->get_record_snapshot('course', 1, $course1);
d8a1f426
PS
713 $this->assertSame($course1, $result);
714
5fef139c 715 $user = $event->get_record_snapshot('user', 1);
d8a1f426
PS
716 $this->assertEquals(1, $user->id);
717 $this->assertSame('guest', $user->username);
300dbc66
PS
718
719 $event->add_record_snapshot('course', $course1);
720 $event->trigger();
721 try {
722 $event->add_record_snapshot('course', $course1);
723 $this->fail('Updating of snapshots after trigger is not ok');;
724 } catch (\moodle_exception $e) {
725 $this->assertInstanceOf('\coding_exception', $e);
726 }
4ad6d5c5
PS
727
728 $event2 = \core_tests\event\unittest_executed::restore($event->get_data(), array());
729 try {
730 $event2->get_record_snapshot('course', 1, $course1);
731 $this->fail('Reading of snapshots from restored events is not ok');;
732 } catch (\moodle_exception $e) {
733 $this->assertInstanceOf('\coding_exception', $e);
734 }
d8a1f426 735 }
ed17808d 736
1ccc1268
AA
737 public function test_get_name() {
738 $event = \core_tests\event\noname_event::create(array('courseid' => 1, 'other' => array('sample' => 1, 'xx' => 10)));
739 $this->assertEquals("core_tests: noname event", $event->get_name());
740 }
741
ed17808d
PS
742 public function test_iteration() {
743 $event = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
744
745 $data = array();
746 foreach ($event as $k => $v) {
747 $data[$k] = $v;
748 }
749
750 $this->assertSame($event->get_data(), $data);
751 }
605a8c33 752}