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