MDL-39846 introduce new objecttable property
[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
274 public function test_ignore_exceptions() {
275 $observers = array(
276
277 array(
278 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 279 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
280 ),
281
282 array(
283 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 284 'callback' => '\core_tests\event\unittest_observer::broken_observer',
d8a1f426
PS
285 'priority' => 100,
286 ),
287 );
288
289 \core\event\manager::phpunit_replace_observers($observers);
290 \core_tests\event\unittest_observer::reset();
291
c4297815 292 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
293 $event1->trigger();
294 $this->assertDebuggingCalled();
295
c4297815 296 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
297 $event1->trigger();
298 $this->assertDebuggingCalled();
299
300 $this->assertSame(
301 array('broken_observer-1', 'observe_one-1', 'broken_observer-2', 'observe_one-2'),
302 \core_tests\event\unittest_observer::$info);
303 }
304
305 public function test_external_buffer() {
306 global $DB;
307
308 $this->preventResetByRollback();
309
310 $observers = array(
311
312 array(
313 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 314 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
315 ),
316
317 array(
318 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 319 'callback' => '\core_tests\event\unittest_observer::external_observer',
d8a1f426
PS
320 'priority' => 200,
321 'internal' => 0,
322 ),
323 );
324
325 \core\event\manager::phpunit_replace_observers($observers);
326 \core_tests\event\unittest_observer::reset();
327
c4297815 328 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426 329 $event1->trigger();
c4297815 330 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
331 $event2->trigger();
332
333 $this->assertSame(
334 array('external_observer-1', 'observe_one-1', 'external_observer-2', 'observe_one-2'),
335 \core_tests\event\unittest_observer::$info);
336
337 \core\event\manager::phpunit_replace_observers($observers);
338 \core_tests\event\unittest_observer::reset();
339
340 $this->assertSame(array(), \core_tests\event\unittest_observer::$info);
341
342 $trans = $DB->start_delegated_transaction();
343
c4297815 344 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426 345 $event1->trigger();
c4297815 346 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
347 $event2->trigger();
348
349 $this->assertSame(
350 array('observe_one-1', 'observe_one-2'),
351 \core_tests\event\unittest_observer::$info);
352
353 $trans->allow_commit();
354
355 $this->assertSame(
356 array('observe_one-1', 'observe_one-2', 'external_observer-1', 'external_observer-2'),
357 \core_tests\event\unittest_observer::$info);
358
359 \core\event\manager::phpunit_replace_observers($observers);
360 \core_tests\event\unittest_observer::reset();
361
c4297815 362 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
363 $event1->trigger();
364 $trans = $DB->start_delegated_transaction();
c4297815 365 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
366 $event2->trigger();
367 try {
368 $trans->rollback(new \moodle_exception('xxx'));
369 $this->fail('Expecting exception');
370 } catch (\moodle_exception $e) {
371 }
372
373 $this->assertSame(
374 array('external_observer-1', 'observe_one-1', 'observe_one-2'),
375 \core_tests\event\unittest_observer::$info);
376 }
377
378 public function test_legacy() {
379 global $DB;
380
381 $this->resetAfterTest(true);
382
383 $observers = array(
384 array(
385 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 386 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
387 ),
388 array(
389 'eventname' => '*',
4b734e74 390 'callback' => '\core_tests\event\unittest_observer::observe_all',
d8a1f426
PS
391 'includefile' => null,
392 'internal' => 1,
393 'priority' => 9999,
394 ),
395 );
396
397 $DB->delete_records('log', array());
398 events_update_definition('unittest');
399 $DB->delete_records_select('events_handlers', "component <> 'unittest'");
400 events_get_handlers('reset');
401 $this->assertEquals(3, $DB->count_records('events_handlers'));
402 set_config('loglifetime', 60*60*24*5);
403
404 \core\event\manager::phpunit_replace_observers($observers);
405 \core_tests\event\unittest_observer::reset();
406
c4297815 407 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>5, 'xx'=>10)));
d8a1f426
PS
408 $event1->trigger();
409
c4297815 410 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'other'=>array('sample'=>6, 'xx'=>11)));
d8a1f426
PS
411 $event2->nest = true;
412 $event2->trigger();
413
414
415 $this->assertSame(
416 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'),
417 \core_tests\event\unittest_observer::$info);
418
419 $this->assertSame($event1, \core_tests\event\unittest_observer::$event[0]);
420 $this->assertSame($event1, \core_tests\event\unittest_observer::$event[1]);
421 $this->assertSame(array(1, 5), \core_tests\event\unittest_observer::$event[2]);
422
423
424 $logs = $DB->get_records('log', array(), 'id ASC');
425 $this->assertCount(3, $logs);
426
427 $log = array_shift($logs);
428 $this->assertEquals(1, $log->course);
429 $this->assertSame('core_unittest', $log->module);
430 $this->assertSame('view', $log->action);
431
432 $log = array_shift($logs);
433 $this->assertEquals(2, $log->course);
434 $this->assertSame('core_unittest', $log->module);
435 $this->assertSame('view', $log->action);
436
437 $log = array_shift($logs);
438 $this->assertEquals(3, $log->course);
439 $this->assertSame('core_unittest', $log->module);
440 $this->assertSame('view', $log->action);
441 }
442
443 public function test_restore_event() {
c4297815 444 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
445 $data1 = $event1->get_data();
446
4b734e74 447 $event2 = \core\event\base::restore($data1, array('origin'=>'clid'));
d8a1f426
PS
448 $data2 = $event2->get_data();
449
450 $this->assertTrue($event2->is_triggered());
451 $this->assertTrue($event2->is_restored());
452 $this->assertEquals($data1, $data2);
453 $this->assertInstanceOf('core_tests\event\unittest_executed', $event2);
454
455 $this->assertEquals($event1->get_context(), $event2->get_context());
456
457 // Now test problematic data.
458 $data3 = $data1;
459 $data3['eventname'] = '\\a\\b\\c';
4b734e74 460 $event3 = \core\event\base::restore($data3, array());
d8a1f426
PS
461 $this->assertFalse($event3, 'Class name must match');
462
463 $data4 = $data1;
464 unset($data4['userid']);
4b734e74 465 $event4 = \core\event\base::restore($data4, array());
d8a1f426
PS
466 $this->assertInstanceOf('core_tests\event\unittest_executed', $event4);
467 $this->assertDebuggingCalled();
468
469 $data5 = $data1;
470 $data5['xx'] = 'xx';
4b734e74 471 $event5 = \core\event\base::restore($data5, array());
d8a1f426 472 $this->assertInstanceOf('core_tests\event\unittest_executed', $event5);
4b734e74 473 $this->assertDebuggingCalled();
d8a1f426
PS
474
475 }
476
477 public function test_trigger_problems() {
c4297815 478 $event = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>5, 'xx'=>10)));
d8a1f426
PS
479 $event->trigger();
480 try {
481 $event->trigger();
482 $this->fail('Exception expected on double trigger');
483 } catch (Exception $e) {
484 $this->assertInstanceOf('coding_exception', $e);
485 }
486
487 $data = $event->get_data();
4b734e74 488 $restored = \core_tests\event\unittest_executed::restore($data, array());
d8a1f426
PS
489 $this->assertTrue($restored->is_triggered());
490 $this->assertTrue($restored->is_restored());
491
492 try {
493 $restored->trigger();
494 $this->fail('Exception expected on triggering of restored event');
495 } catch (\moodle_exception $e) {
496 $this->assertInstanceOf('coding_exception', $e);
497 }
22626564 498
c4297815 499 $event = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>5, 'xx'=>10)));
22626564
PS
500 try {
501 \core\event\manager::dispatch($event);
502 $this->fail('Exception expected on manual event dispatching');
503 } catch (\moodle_exception $e) {
504 $this->assertInstanceOf('coding_exception', $e);
505 }
d8a1f426
PS
506 }
507
508 public function test_bad_events() {
509 $event = \core_tests\event\bad_event1::create();
510 try {
511 $event->trigger();
512 $this->fail('Exception expected when $data not valid');
513 } catch (\moodle_exception $e) {
514 $this->assertInstanceOf('\coding_exception', $e);
515 }
516
517 $event = \core_tests\event\bad_event2::create();
518 try {
519 $event->trigger();
520 $this->fail('Exception expected when $data not valid');
521 } catch (\moodle_exception $e) {
522 $this->assertInstanceOf('\coding_exception', $e);
523 }
524
525 $event = \core_tests\event\bad_event3::create();
4b734e74 526 @$event->trigger();
d8a1f426
PS
527 $this->assertDebuggingCalled();
528
529 $event = \core_tests\event\bad_event4::create();
4b734e74 530 @$event->trigger();
d8a1f426
PS
531 $this->assertDebuggingCalled();
532
533 $event = \core_tests\event\bad_event5::create();
4b734e74 534 @$event->trigger();
d8a1f426 535 $this->assertDebuggingCalled();
a85258ca
PS
536
537 $event = \core_tests\event\bad_event6::create();
538 $event->trigger();
539 $this->assertDebuggingCalled();
540
541 $event = \core_tests\event\bad_event7::create(array('objectid'=>1));
542 try {
543 $event->trigger();
544 $this->fail('Exception expected when $data contains objectid by objecttable not specified');
545 } catch (\moodle_exception $e) {
546 $this->assertInstanceOf('\coding_exception', $e);
547 }
d8a1f426
PS
548 }
549
550 public function test_problematic_events() {
551 global $CFG;
552 $event1 = \core_tests\event\problematic_event1::create();
553 $this->assertDebuggingNotCalled();
4b734e74
PS
554 $this->assertNull($event1->xxx);
555 $this->assertDebuggingCalled();
d8a1f426
PS
556
557 $event2 = \core_tests\event\problematic_event1::create(array('xxx'=>0));
558 $this->assertDebuggingCalled();
559
560 $CFG->debug = 0;
561 $event3 = \core_tests\event\problematic_event1::create(array('xxx'=>0));
562 $this->assertDebuggingNotCalled();
563 $CFG->debug = E_ALL | E_STRICT;
564
c4297815 565 $event4 = \core_tests\event\problematic_event1::create(array('other'=>array('a'=>1)));
d8a1f426
PS
566 $event4->trigger();
567 $this->assertDebuggingNotCalled();
568
c4297815 569 $event5 = \core_tests\event\problematic_event1::create(array('other'=>(object)array('a'=>1)));
d8a1f426
PS
570 $this->assertDebuggingNotCalled();
571 $event5->trigger();
572 $this->assertDebuggingCalled();
573
574 $url = new moodle_url('/admin/');
c4297815 575 $event6 = \core_tests\event\problematic_event1::create(array('other'=>array('a'=>$url)));
d8a1f426
PS
576 $this->assertDebuggingNotCalled();
577 $event6->trigger();
578 $this->assertDebuggingCalled();
579 }
580
5fef139c 581 public function test_record_snapshots() {
d8a1f426
PS
582 global $DB;
583
c4297815 584 $event = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
585 $course1 = $DB->get_record('course', array('id'=>1));
586 $this->assertNotEmpty($course1);
587
5fef139c 588 $event->add_record_snapshot('course', $course1);
d8a1f426 589
5fef139c 590 $result = $event->get_record_snapshot('course', 1, $course1);
d8a1f426
PS
591 $this->assertSame($course1, $result);
592
5fef139c 593 $user = $event->get_record_snapshot('user', 1);
d8a1f426
PS
594 $this->assertEquals(1, $user->id);
595 $this->assertSame('guest', $user->username);
596 }
605a8c33 597}