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