MDL-39846 require context when creating events
[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();
c4297815 36 $event = \core_tests\event\unittest_executed::create(array('courseid'=>1, '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);
41 $this->assertSame('unittest', $event->object);
42 $this->assertSame(5, $event->objectid);
43 $this->assertSame('u', $event->crud);
44 $this->assertSame(10, $event->level);
45
46 $this->assertSame($system, $event->get_context());
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);
52 $this->assertSame(1, $event->courseid);
53
54 $this->assertNull($event->relateduserid);
55 $this->assertFalse(isset($event->relateduserid));
56
c4297815
PS
57 $this->assertSame(array('sample'=>null, 'xx'=>10), $event->other);
58 $this->assertTrue(isset($event->other['xx']));
59 $this->assertFalse(isset($event->other['sample']));
605a8c33
PS
60
61 $this->assertLessThanOrEqual(time(), $event->timecreated);
62
63 try {
64 $event->courseid = 2;
65 $this->fail('Exception expected on event modification');
66 } catch (\moodle_exception $e) {
67 $this->assertInstanceOf('coding_exception', $e);
68 }
69
70 try {
71 $event->xxxx = 1;
72 $this->fail('Exception expected on event modification');
73 } catch (\moodle_exception $e) {
74 $this->assertInstanceOf('coding_exception', $e);
75 }
27af3e62
PS
76
77 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'contextid'=>$system->id, 'objectid'=>5, 'other'=>array('sample'=>null, 'xx'=>10)));
78 $this->assertSame($event->get_context(), $event2->get_context());
79
80 $event3 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'contextid'=>999, 'contextlevel'=>CONTEXT_COURSE, 'contextinstanceid'=>4554645, 'objectid'=>5, 'other'=>array('sample'=>null, 'xx'=>10)));
81 $this->assertSame(false, $event3->get_context());
605a8c33
PS
82 }
83
d8a1f426
PS
84 public function test_observers_parsing() {
85
86 $observers = array(
87 array(
88 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 89 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
90 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
91 ),
92 array(
93 'eventname' => '*',
4b734e74 94 'callback' => array('\core_tests\event\unittest_observer', 'observe_all'),
d8a1f426
PS
95 'includefile' => null,
96 'internal' => 1,
97 'priority' => 9999,
98 ),
99 array(
100 'eventname' => '\core\event\unknown_executed',
4b734e74 101 'callback' => '\core_tests\event\unittest_observer::broken_observer',
d8a1f426
PS
102 'priority' => 100,
103 ),
104 array(
105 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 106 'callback' => '\core_tests\event\unittest_observer::external_observer',
d8a1f426
PS
107 'priority' => 200,
108 'internal' => 0,
109 ),
110 );
111
112 $result = \core\event\manager::phpunit_replace_observers($observers);
113
114 $this->assertCount(3, $result);
115 end($result);
116 $this->assertSame('*', key($result));
117
118 $expected = array();
119 $observer = new stdClass();
4b734e74 120 $observer->callable = array('\core_tests\event\unittest_observer', 'observe_all');
d8a1f426
PS
121 $observer->priority = 9999;
122 $observer->internal = true;
123 $observer->includefile = null;
124 $expected[0] = $observer;
125 $observer = new stdClass();
126 $observer->callable = '\core_tests\event\unittest_observer::external_observer';
127 $observer->priority = 200;
128 $observer->internal = false;
129 $observer->includefile = null;
130 $expected[1] = $observer;
131 $observer = new stdClass();
132 $observer->callable = '\core_tests\event\unittest_observer::observe_one';
133 $observer->priority = 0;
134 $observer->internal = true;
135 $observer->includefile = 'lib/tests/fixtures/event_fixtures.php';
136 $expected[2] = $observer;
137
138 $this->assertEquals($expected, $result['\core_tests\event\unittest_executed']);
139
140 $expected = array();
141 $observer = new stdClass();
4b734e74 142 $observer->callable = array('\core_tests\event\unittest_observer', 'observe_all');
d8a1f426
PS
143 $observer->priority = 9999;
144 $observer->internal = true;
145 $observer->includefile = null;
146 $expected[0] = $observer;
147 $observer = new stdClass();
148 $observer->callable = '\core_tests\event\unittest_observer::broken_observer';
149 $observer->priority = 100;
150 $observer->internal = true;
151 $observer->includefile = null;
152 $expected[1] = $observer;
153
154 $this->assertEquals($expected, $result['\core\event\unknown_executed']);
155
156 $expected = array();
157 $observer = new stdClass();
4b734e74 158 $observer->callable = array('\core_tests\event\unittest_observer', 'observe_all');
d8a1f426
PS
159 $observer->priority = 9999;
160 $observer->internal = true;
161 $observer->includefile = null;
162 $expected[0] = $observer;
163
164 $this->assertEquals($expected, $result['*']);
165
166
167 // Now test broken stuff...
168
169 $observers = array(
170 array(
171 'eventname' => 'core_tests\event\unittest_executed', // Fix leading backslash.
4b734e74 172 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
173 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
174 'internal' => 1, // Cast to bool.
175 ),
176 );
177 $result = \core\event\manager::phpunit_replace_observers($observers);
178 $this->assertCount(1, $result);
179 $expected = array();
180 $observer = new stdClass();
181 $observer->callable = '\core_tests\event\unittest_observer::observe_one';
182 $observer->priority = 0;
183 $observer->internal = true;
184 $observer->includefile = 'lib/tests/fixtures/event_fixtures.php';
185 $expected[0] = $observer;
186 $this->assertEquals($expected, $result['\core_tests\event\unittest_executed']);
187
188 $observers = array(
189 array(
190 // Missing eventclass.
4b734e74 191 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
192 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
193 ),
194 );
195 $result = \core\event\manager::phpunit_replace_observers($observers);
196 $this->assertCount(0, $result);
197 $this->assertDebuggingCalled();
198
199 $observers = array(
200 array(
201 'eventname' => '', // Empty eventclass.
4b734e74 202 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
203 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
204 ),
205 );
206 $result = \core\event\manager::phpunit_replace_observers($observers);
207 $this->assertCount(0, $result);
208 $this->assertDebuggingCalled();
209
210 $observers = array(
211 array(
212 'eventname' => '\core_tests\event\unittest_executed',
213 // Missing callable.
214 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
215 ),
216 );
217 $result = \core\event\manager::phpunit_replace_observers($observers);
218 $this->assertCount(0, $result);
219 $this->assertDebuggingCalled();
220
221 $observers = array(
222 array(
223 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 224 'callback' => '', // empty callable
d8a1f426
PS
225 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
226 ),
227 );
228 $result = \core\event\manager::phpunit_replace_observers($observers);
229 $this->assertCount(0, $result);
230 $this->assertDebuggingCalled();
231
232 $observers = array(
233 array(
234 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 235 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
236 'includefile' => 'lib/tests/fixtures/event_fixtures.php_xxx', // Missing file.
237 ),
238 );
239 $result = \core\event\manager::phpunit_replace_observers($observers);
240 $this->assertCount(0, $result);
241 $this->assertDebuggingCalled();
242 }
243
244 public function test_normal_dispatching() {
245 $observers = array(
246 array(
247 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 248 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
249 ),
250 array(
251 'eventname' => '*',
4b734e74 252 'callback' => '\core_tests\event\unittest_observer::observe_all',
d8a1f426
PS
253 'includefile' => null,
254 'internal' => 1,
255 'priority' => 9999,
256 ),
257 );
258
259 \core\event\manager::phpunit_replace_observers($observers);
260 \core_tests\event\unittest_observer::reset();
261
c4297815 262 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
263 $event1->nest = 1;
264 $this->assertFalse($event1->is_triggered());
22626564 265 $this->assertFalse($event1->is_dispatched());
d8a1f426
PS
266 $this->assertFalse($event1->is_restored());
267 $event1->trigger();
268 $this->assertTrue($event1->is_triggered());
22626564 269 $this->assertTrue($event1->is_dispatched());
d8a1f426
PS
270 $this->assertFalse($event1->is_restored());
271
c4297815 272 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
273 $event1->trigger();
274
275 $this->assertSame(
276 array('observe_all-nesting-1', 'observe_one-1', 'observe_all-3', 'observe_one-3', 'observe_all-2', 'observe_one-2'),
277 \core_tests\event\unittest_observer::$info);
278 }
279
62401e8f
PS
280 public function test_event_sink() {
281 $sink = $this->redirectEvents();
282 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
283 $event1->trigger();
284 $this->assertSame(1, $sink->count());
285 $retult = $sink->get_events();
286 $this->assertSame($event1, $retult[0]);
287
288 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
289 $event2->trigger();
290 $this->assertSame(2, $sink->count());
291 $retult = $sink->get_events();
292 $this->assertSame($event1, $retult[0]);
293 $this->assertSame($event2, $retult[1]);
294
295 $sink->clear();
296 $this->assertSame(0, $sink->count());
297 $this->assertSame(array(), $sink->get_events());
298
299 $event3 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>3, 'xx'=>10)));
300 $event3->trigger();
301 $this->assertSame(1, $sink->count());
302 $retult = $sink->get_events();
303 $this->assertSame($event3, $retult[0]);
304
305 $sink->close();
306 $event4 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>4, 'xx'=>10)));
307 $event4->trigger();
308 $this->assertSame(1, $sink->count());
309 $retult = $sink->get_events();
310 $this->assertSame($event3, $retult[0]);
311 }
312
d8a1f426
PS
313 public function test_ignore_exceptions() {
314 $observers = array(
315
316 array(
317 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 318 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
319 ),
320
321 array(
322 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 323 'callback' => '\core_tests\event\unittest_observer::broken_observer',
d8a1f426
PS
324 'priority' => 100,
325 ),
326 );
327
328 \core\event\manager::phpunit_replace_observers($observers);
329 \core_tests\event\unittest_observer::reset();
330
c4297815 331 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
332 $event1->trigger();
333 $this->assertDebuggingCalled();
334
c4297815 335 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
336 $event1->trigger();
337 $this->assertDebuggingCalled();
338
339 $this->assertSame(
340 array('broken_observer-1', 'observe_one-1', 'broken_observer-2', 'observe_one-2'),
341 \core_tests\event\unittest_observer::$info);
342 }
343
344 public function test_external_buffer() {
345 global $DB;
346
347 $this->preventResetByRollback();
348
349 $observers = array(
350
351 array(
352 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 353 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
354 ),
355
356 array(
357 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 358 'callback' => '\core_tests\event\unittest_observer::external_observer',
d8a1f426
PS
359 'priority' => 200,
360 'internal' => 0,
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 368 $event1->trigger();
c4297815 369 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
370 $event2->trigger();
371
372 $this->assertSame(
373 array('external_observer-1', 'observe_one-1', 'external_observer-2', 'observe_one-2'),
374 \core_tests\event\unittest_observer::$info);
375
376 \core\event\manager::phpunit_replace_observers($observers);
377 \core_tests\event\unittest_observer::reset();
378
379 $this->assertSame(array(), \core_tests\event\unittest_observer::$info);
380
381 $trans = $DB->start_delegated_transaction();
382
c4297815 383 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426 384 $event1->trigger();
c4297815 385 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
386 $event2->trigger();
387
388 $this->assertSame(
389 array('observe_one-1', 'observe_one-2'),
390 \core_tests\event\unittest_observer::$info);
391
392 $trans->allow_commit();
393
394 $this->assertSame(
395 array('observe_one-1', 'observe_one-2', 'external_observer-1', 'external_observer-2'),
396 \core_tests\event\unittest_observer::$info);
397
398 \core\event\manager::phpunit_replace_observers($observers);
399 \core_tests\event\unittest_observer::reset();
400
c4297815 401 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
402 $event1->trigger();
403 $trans = $DB->start_delegated_transaction();
c4297815 404 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
405 $event2->trigger();
406 try {
407 $trans->rollback(new \moodle_exception('xxx'));
408 $this->fail('Expecting exception');
409 } catch (\moodle_exception $e) {
410 }
411
412 $this->assertSame(
413 array('external_observer-1', 'observe_one-1', 'observe_one-2'),
414 \core_tests\event\unittest_observer::$info);
415 }
416
417 public function test_legacy() {
418 global $DB;
419
420 $this->resetAfterTest(true);
421
422 $observers = array(
423 array(
424 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 425 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
426 ),
427 array(
428 'eventname' => '*',
4b734e74 429 'callback' => '\core_tests\event\unittest_observer::observe_all',
d8a1f426
PS
430 'includefile' => null,
431 'internal' => 1,
432 'priority' => 9999,
433 ),
434 );
435
436 $DB->delete_records('log', array());
437 events_update_definition('unittest');
438 $DB->delete_records_select('events_handlers', "component <> 'unittest'");
439 events_get_handlers('reset');
440 $this->assertEquals(3, $DB->count_records('events_handlers'));
441 set_config('loglifetime', 60*60*24*5);
442
443 \core\event\manager::phpunit_replace_observers($observers);
444 \core_tests\event\unittest_observer::reset();
445
c4297815 446 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>5, 'xx'=>10)));
d8a1f426
PS
447 $event1->trigger();
448
c4297815 449 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'other'=>array('sample'=>6, 'xx'=>11)));
d8a1f426
PS
450 $event2->nest = true;
451 $event2->trigger();
452
453
454 $this->assertSame(
455 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'),
456 \core_tests\event\unittest_observer::$info);
457
458 $this->assertSame($event1, \core_tests\event\unittest_observer::$event[0]);
459 $this->assertSame($event1, \core_tests\event\unittest_observer::$event[1]);
460 $this->assertSame(array(1, 5), \core_tests\event\unittest_observer::$event[2]);
461
462
463 $logs = $DB->get_records('log', array(), 'id ASC');
464 $this->assertCount(3, $logs);
465
466 $log = array_shift($logs);
467 $this->assertEquals(1, $log->course);
468 $this->assertSame('core_unittest', $log->module);
469 $this->assertSame('view', $log->action);
470
471 $log = array_shift($logs);
472 $this->assertEquals(2, $log->course);
473 $this->assertSame('core_unittest', $log->module);
474 $this->assertSame('view', $log->action);
475
476 $log = array_shift($logs);
477 $this->assertEquals(3, $log->course);
478 $this->assertSame('core_unittest', $log->module);
479 $this->assertSame('view', $log->action);
480 }
481
482 public function test_restore_event() {
c4297815 483 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
484 $data1 = $event1->get_data();
485
4b734e74 486 $event2 = \core\event\base::restore($data1, array('origin'=>'clid'));
d8a1f426
PS
487 $data2 = $event2->get_data();
488
489 $this->assertTrue($event2->is_triggered());
490 $this->assertTrue($event2->is_restored());
491 $this->assertEquals($data1, $data2);
492 $this->assertInstanceOf('core_tests\event\unittest_executed', $event2);
493
494 $this->assertEquals($event1->get_context(), $event2->get_context());
495
496 // Now test problematic data.
497 $data3 = $data1;
498 $data3['eventname'] = '\\a\\b\\c';
4b734e74 499 $event3 = \core\event\base::restore($data3, array());
d8a1f426
PS
500 $this->assertFalse($event3, 'Class name must match');
501
502 $data4 = $data1;
503 unset($data4['userid']);
4b734e74 504 $event4 = \core\event\base::restore($data4, array());
d8a1f426
PS
505 $this->assertInstanceOf('core_tests\event\unittest_executed', $event4);
506 $this->assertDebuggingCalled();
507
508 $data5 = $data1;
509 $data5['xx'] = 'xx';
4b734e74 510 $event5 = \core\event\base::restore($data5, array());
d8a1f426 511 $this->assertInstanceOf('core_tests\event\unittest_executed', $event5);
4b734e74 512 $this->assertDebuggingCalled();
d8a1f426
PS
513
514 }
515
516 public function test_trigger_problems() {
c4297815 517 $event = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>5, 'xx'=>10)));
d8a1f426
PS
518 $event->trigger();
519 try {
520 $event->trigger();
521 $this->fail('Exception expected on double trigger');
522 } catch (Exception $e) {
523 $this->assertInstanceOf('coding_exception', $e);
524 }
525
526 $data = $event->get_data();
4b734e74 527 $restored = \core_tests\event\unittest_executed::restore($data, array());
d8a1f426
PS
528 $this->assertTrue($restored->is_triggered());
529 $this->assertTrue($restored->is_restored());
530
531 try {
532 $restored->trigger();
533 $this->fail('Exception expected on triggering of restored event');
534 } catch (\moodle_exception $e) {
535 $this->assertInstanceOf('coding_exception', $e);
536 }
22626564 537
c4297815 538 $event = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>5, 'xx'=>10)));
22626564
PS
539 try {
540 \core\event\manager::dispatch($event);
541 $this->fail('Exception expected on manual event dispatching');
542 } catch (\moodle_exception $e) {
543 $this->assertInstanceOf('coding_exception', $e);
544 }
d8a1f426
PS
545 }
546
547 public function test_bad_events() {
27af3e62
PS
548 try {
549 $event = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'other'=>array('sample'=>5, 'xx'=>10)));
550 $this->fail('Exception expected when context and contextid missing');
551 } catch (Exception $e) {
552 $this->assertInstanceOf('coding_exception', $e);
553 }
554
555 $event = \core_tests\event\bad_event1::create(array('context'=>\context_system::instance()));
d8a1f426
PS
556 try {
557 $event->trigger();
558 $this->fail('Exception expected when $data not valid');
559 } catch (\moodle_exception $e) {
560 $this->assertInstanceOf('\coding_exception', $e);
561 }
562
27af3e62 563 $event = \core_tests\event\bad_event2::create(array('context'=>\context_system::instance()));
d8a1f426
PS
564 try {
565 $event->trigger();
566 $this->fail('Exception expected when $data not valid');
567 } catch (\moodle_exception $e) {
568 $this->assertInstanceOf('\coding_exception', $e);
569 }
570
27af3e62 571 $event = \core_tests\event\bad_event3::create(array('context'=>\context_system::instance()));
4b734e74 572 @$event->trigger();
d8a1f426
PS
573 $this->assertDebuggingCalled();
574
27af3e62 575 $event = \core_tests\event\bad_event4::create(array('context'=>\context_system::instance()));
4b734e74 576 @$event->trigger();
d8a1f426
PS
577 $this->assertDebuggingCalled();
578
27af3e62 579 $event = \core_tests\event\bad_event5::create(array('context'=>\context_system::instance()));
4b734e74 580 @$event->trigger();
d8a1f426 581 $this->assertDebuggingCalled();
a85258ca 582
27af3e62 583 $event = \core_tests\event\bad_event6::create(array('context'=>\context_system::instance()));
a85258ca
PS
584 $event->trigger();
585 $this->assertDebuggingCalled();
586
27af3e62 587 $event = \core_tests\event\bad_event7::create(array('objectid'=>1, 'context'=>\context_system::instance()));
a85258ca
PS
588 try {
589 $event->trigger();
590 $this->fail('Exception expected when $data contains objectid by objecttable not specified');
591 } catch (\moodle_exception $e) {
592 $this->assertInstanceOf('\coding_exception', $e);
593 }
d8a1f426
PS
594 }
595
596 public function test_problematic_events() {
597 global $CFG;
27af3e62 598 $event1 = \core_tests\event\problematic_event1::create(array('context'=>\context_system::instance()));
d8a1f426 599 $this->assertDebuggingNotCalled();
4b734e74
PS
600 $this->assertNull($event1->xxx);
601 $this->assertDebuggingCalled();
d8a1f426 602
27af3e62 603 $event2 = \core_tests\event\problematic_event1::create(array('xxx'=>0, 'context'=>\context_system::instance()));
d8a1f426
PS
604 $this->assertDebuggingCalled();
605
606 $CFG->debug = 0;
27af3e62 607 $event3 = \core_tests\event\problematic_event1::create(array('xxx'=>0, 'context'=>\context_system::instance()));
d8a1f426
PS
608 $this->assertDebuggingNotCalled();
609 $CFG->debug = E_ALL | E_STRICT;
610
27af3e62 611 $event4 = \core_tests\event\problematic_event1::create(array('context'=>\context_system::instance(), 'other'=>array('a'=>1)));
d8a1f426
PS
612 $event4->trigger();
613 $this->assertDebuggingNotCalled();
614
27af3e62 615 $event5 = \core_tests\event\problematic_event1::create(array('context'=>\context_system::instance(), 'other'=>(object)array('a'=>1)));
d8a1f426
PS
616 $this->assertDebuggingNotCalled();
617 $event5->trigger();
618 $this->assertDebuggingCalled();
619
620 $url = new moodle_url('/admin/');
27af3e62 621 $event6 = \core_tests\event\problematic_event1::create(array('context'=>\context_system::instance(), 'other'=>array('a'=>$url)));
d8a1f426
PS
622 $this->assertDebuggingNotCalled();
623 $event6->trigger();
624 $this->assertDebuggingCalled();
625 }
626
5fef139c 627 public function test_record_snapshots() {
d8a1f426
PS
628 global $DB;
629
c4297815 630 $event = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
631 $course1 = $DB->get_record('course', array('id'=>1));
632 $this->assertNotEmpty($course1);
633
5fef139c 634 $event->add_record_snapshot('course', $course1);
d8a1f426 635
5fef139c 636 $result = $event->get_record_snapshot('course', 1, $course1);
d8a1f426
PS
637 $this->assertSame($course1, $result);
638
5fef139c 639 $user = $event->get_record_snapshot('user', 1);
d8a1f426
PS
640 $this->assertEquals(1, $user->id);
641 $this->assertSame('guest', $user->username);
642 }
605a8c33 643}