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