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