Moodle release 2.8rc1
[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;
7c084af9
AG
162 $observer->plugintype = null;
163 $observer->plugin = null;
2d1884d9 164 $expected[0] = $observer;
d8a1f426
PS
165 $observer = new stdClass();
166 $observer->callable = '\core_tests\event\unittest_observer::observe_one';
167 $observer->priority = 0;
168 $observer->internal = true;
111c6496 169 $observer->includefile = $CFG->dirroot.'/lib/tests/fixtures/event_fixtures.php';
7c084af9
AG
170 $observer->plugintype = null;
171 $observer->plugin = null;
2d1884d9 172 $expected[1] = $observer;
d8a1f426
PS
173
174 $this->assertEquals($expected, $result['\core_tests\event\unittest_executed']);
175
176 $expected = array();
177 $observer = new stdClass();
d8a1f426
PS
178 $observer->callable = '\core_tests\event\unittest_observer::broken_observer';
179 $observer->priority = 100;
180 $observer->internal = true;
181 $observer->includefile = null;
7c084af9
AG
182 $observer->plugintype = null;
183 $observer->plugin = null;
2d1884d9 184 $expected[0] = $observer;
d8a1f426
PS
185
186 $this->assertEquals($expected, $result['\core\event\unknown_executed']);
187
188 $expected = array();
189 $observer = new stdClass();
4b734e74 190 $observer->callable = array('\core_tests\event\unittest_observer', 'observe_all');
2d1884d9 191 $observer->priority = 10;
d8a1f426
PS
192 $observer->internal = true;
193 $observer->includefile = null;
7c084af9
AG
194 $observer->plugintype = null;
195 $observer->plugin = null;
d8a1f426 196 $expected[0] = $observer;
2d1884d9
PS
197 $observer = new stdClass();
198 $observer->callable = array('\core_tests\event\unittest_observer', 'observe_all_alt');
199 $observer->priority = 0;
200 $observer->internal = true;
201 $observer->includefile = null;
7c084af9
AG
202 $observer->plugintype = null;
203 $observer->plugin = null;
2d1884d9 204 $expected[1] = $observer;
d8a1f426 205
2d1884d9 206 $this->assertEquals($expected, $result['\core\event\base']);
d8a1f426 207
d8a1f426
PS
208 // Now test broken stuff...
209
210 $observers = array(
211 array(
212 'eventname' => 'core_tests\event\unittest_executed', // Fix leading backslash.
4b734e74 213 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
214 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
215 'internal' => 1, // Cast to bool.
216 ),
217 );
218 $result = \core\event\manager::phpunit_replace_observers($observers);
219 $this->assertCount(1, $result);
220 $expected = array();
221 $observer = new stdClass();
222 $observer->callable = '\core_tests\event\unittest_observer::observe_one';
223 $observer->priority = 0;
224 $observer->internal = true;
111c6496 225 $observer->includefile = $CFG->dirroot.'/lib/tests/fixtures/event_fixtures.php';
7c084af9
AG
226 $observer->plugintype = null;
227 $observer->plugin = null;
d8a1f426
PS
228 $expected[0] = $observer;
229 $this->assertEquals($expected, $result['\core_tests\event\unittest_executed']);
230
231 $observers = array(
232 array(
233 // Missing eventclass.
4b734e74 234 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
235 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
236 ),
237 );
238 $result = \core\event\manager::phpunit_replace_observers($observers);
239 $this->assertCount(0, $result);
240 $this->assertDebuggingCalled();
241
242 $observers = array(
243 array(
244 'eventname' => '', // Empty eventclass.
4b734e74 245 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
246 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
247 ),
248 );
249 $result = \core\event\manager::phpunit_replace_observers($observers);
250 $this->assertCount(0, $result);
251 $this->assertDebuggingCalled();
252
253 $observers = array(
254 array(
255 'eventname' => '\core_tests\event\unittest_executed',
256 // Missing callable.
257 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
258 ),
259 );
260 $result = \core\event\manager::phpunit_replace_observers($observers);
261 $this->assertCount(0, $result);
262 $this->assertDebuggingCalled();
263
264 $observers = array(
265 array(
266 'eventname' => '\core_tests\event\unittest_executed',
96deb950 267 'callback' => '', // Empty callable.
d8a1f426
PS
268 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
269 ),
270 );
271 $result = \core\event\manager::phpunit_replace_observers($observers);
272 $this->assertCount(0, $result);
273 $this->assertDebuggingCalled();
274
275 $observers = array(
276 array(
277 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 278 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
279 'includefile' => 'lib/tests/fixtures/event_fixtures.php_xxx', // Missing file.
280 ),
281 );
282 $result = \core\event\manager::phpunit_replace_observers($observers);
283 $this->assertCount(0, $result);
284 $this->assertDebuggingCalled();
285 }
286
287 public function test_normal_dispatching() {
288 $observers = array(
289 array(
290 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 291 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
292 ),
293 array(
294 'eventname' => '*',
4b734e74 295 'callback' => '\core_tests\event\unittest_observer::observe_all',
d8a1f426
PS
296 'includefile' => null,
297 'internal' => 1,
298 'priority' => 9999,
299 ),
300 );
301
302 \core\event\manager::phpunit_replace_observers($observers);
303 \core_tests\event\unittest_observer::reset();
304
9ede00db 305 $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
306 $event1->nest = 1;
307 $this->assertFalse($event1->is_triggered());
22626564 308 $this->assertFalse($event1->is_dispatched());
d8a1f426
PS
309 $this->assertFalse($event1->is_restored());
310 $event1->trigger();
311 $this->assertTrue($event1->is_triggered());
22626564 312 $this->assertTrue($event1->is_dispatched());
d8a1f426
PS
313 $this->assertFalse($event1->is_restored());
314
9ede00db 315 $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
316 $event1->trigger();
317
318 $this->assertSame(
9ede00db 319 array('observe_all-nesting-1', 'observe_one-1', 'observe_all-666', 'observe_one-666', 'observe_all-2', 'observe_one-2'),
d8a1f426
PS
320 \core_tests\event\unittest_observer::$info);
321 }
322
62401e8f
PS
323 public function test_event_sink() {
324 $sink = $this->redirectEvents();
9ede00db 325 $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
62401e8f
PS
326 $event1->trigger();
327 $this->assertSame(1, $sink->count());
328 $retult = $sink->get_events();
329 $this->assertSame($event1, $retult[0]);
330
9ede00db 331 $event2 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
62401e8f
PS
332 $event2->trigger();
333 $this->assertSame(2, $sink->count());
334 $retult = $sink->get_events();
335 $this->assertSame($event1, $retult[0]);
336 $this->assertSame($event2, $retult[1]);
337
338 $sink->clear();
339 $this->assertSame(0, $sink->count());
340 $this->assertSame(array(), $sink->get_events());
341
9ede00db 342 $event3 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>3, 'xx'=>10)));
62401e8f
PS
343 $event3->trigger();
344 $this->assertSame(1, $sink->count());
345 $retult = $sink->get_events();
346 $this->assertSame($event3, $retult[0]);
347
348 $sink->close();
9ede00db 349 $event4 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>4, 'xx'=>10)));
62401e8f
PS
350 $event4->trigger();
351 $this->assertSame(1, $sink->count());
352 $retult = $sink->get_events();
353 $this->assertSame($event3, $retult[0]);
354 }
355
d8a1f426
PS
356 public function test_ignore_exceptions() {
357 $observers = array(
358
359 array(
360 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 361 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
362 ),
363
364 array(
365 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 366 'callback' => '\core_tests\event\unittest_observer::broken_observer',
d8a1f426
PS
367 'priority' => 100,
368 ),
369 );
370
371 \core\event\manager::phpunit_replace_observers($observers);
372 \core_tests\event\unittest_observer::reset();
373
9ede00db 374 $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
375 $event1->trigger();
376 $this->assertDebuggingCalled();
377
9ede00db 378 $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
379 $event1->trigger();
380 $this->assertDebuggingCalled();
381
382 $this->assertSame(
383 array('broken_observer-1', 'observe_one-1', 'broken_observer-2', 'observe_one-2'),
384 \core_tests\event\unittest_observer::$info);
385 }
386
387 public function test_external_buffer() {
388 global $DB;
389
390 $this->preventResetByRollback();
391
392 $observers = array(
393
394 array(
395 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 396 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
397 ),
398
399 array(
400 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 401 'callback' => '\core_tests\event\unittest_observer::external_observer',
d8a1f426
PS
402 'priority' => 200,
403 'internal' => 0,
404 ),
405 );
406
407 \core\event\manager::phpunit_replace_observers($observers);
408 \core_tests\event\unittest_observer::reset();
409
9ede00db 410 $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426 411 $event1->trigger();
9ede00db 412 $event2 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
413 $event2->trigger();
414
415 $this->assertSame(
416 array('external_observer-1', 'observe_one-1', 'external_observer-2', 'observe_one-2'),
417 \core_tests\event\unittest_observer::$info);
418
419 \core\event\manager::phpunit_replace_observers($observers);
420 \core_tests\event\unittest_observer::reset();
421
422 $this->assertSame(array(), \core_tests\event\unittest_observer::$info);
423
424 $trans = $DB->start_delegated_transaction();
425
9ede00db 426 $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426 427 $event1->trigger();
9ede00db 428 $event2 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
429 $event2->trigger();
430
431 $this->assertSame(
432 array('observe_one-1', 'observe_one-2'),
433 \core_tests\event\unittest_observer::$info);
434
435 $trans->allow_commit();
436
437 $this->assertSame(
438 array('observe_one-1', 'observe_one-2', 'external_observer-1', 'external_observer-2'),
439 \core_tests\event\unittest_observer::$info);
440
441 \core\event\manager::phpunit_replace_observers($observers);
442 \core_tests\event\unittest_observer::reset();
443
9ede00db 444 $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
445 $event1->trigger();
446 $trans = $DB->start_delegated_transaction();
9ede00db 447 $event2 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>2, 'xx'=>10)));
d8a1f426
PS
448 $event2->trigger();
449 try {
450 $trans->rollback(new \moodle_exception('xxx'));
451 $this->fail('Expecting exception');
452 } catch (\moodle_exception $e) {
96deb950 453 $this->assertInstanceOf('moodle_exception', $e);
d8a1f426
PS
454 }
455
456 $this->assertSame(
457 array('external_observer-1', 'observe_one-1', 'observe_one-2'),
458 \core_tests\event\unittest_observer::$info);
459 }
460
3345e24f
461 public function test_deprecated() {
462 global $DB;
463
464 $this->resetAfterTest(true);
465
466 $event = \core_tests\event\deprecated_event1::create();
467 $this->assertDebuggingCalled('level property is deprecated, use edulevel property instead');
468
469 $this->assertSame($event::LEVEL_TEACHING, $event->level);
470 $this->assertDebuggingCalled('level property is deprecated, use edulevel property instead');
471
472 $this->assertTrue(isset($event->level));
473 $this->assertDebuggingCalled('level property is deprecated, use edulevel property instead');
474
475 $this->assertSame($event::LEVEL_TEACHING, $event->edulevel);
476 }
477
d8a1f426 478 public function test_legacy() {
770eac98 479 global $DB, $CFG;
d8a1f426
PS
480
481 $this->resetAfterTest(true);
482
483 $observers = array(
484 array(
485 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 486 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
487 ),
488 array(
489 'eventname' => '*',
4b734e74 490 'callback' => '\core_tests\event\unittest_observer::observe_all',
d8a1f426
PS
491 'includefile' => null,
492 'internal' => 1,
493 'priority' => 9999,
494 ),
495 );
496
497 $DB->delete_records('log', array());
498 events_update_definition('unittest');
499 $DB->delete_records_select('events_handlers', "component <> 'unittest'");
500 events_get_handlers('reset');
501 $this->assertEquals(3, $DB->count_records('events_handlers'));
502 set_config('loglifetime', 60*60*24*5);
503
504 \core\event\manager::phpunit_replace_observers($observers);
505 \core_tests\event\unittest_observer::reset();
506
9ede00db 507 $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>5, 'xx'=>10)));
d8a1f426
PS
508 $event1->trigger();
509
9ede00db 510 $event2 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>6, 'xx'=>11)));
d8a1f426
PS
511 $event2->nest = true;
512 $event2->trigger();
513
d8a1f426 514 $this->assertSame(
9ede00db 515 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
516 \core_tests\event\unittest_observer::$info);
517
518 $this->assertSame($event1, \core_tests\event\unittest_observer::$event[0]);
519 $this->assertSame($event1, \core_tests\event\unittest_observer::$event[1]);
9ede00db 520 $this->assertSame(array(0, 5), \core_tests\event\unittest_observer::$event[2]);
d8a1f426 521
d8a1f426 522 $logs = $DB->get_records('log', array(), 'id ASC');
bdaf91fd 523 $this->assertCount(0, $logs);
d8a1f426
PS
524 }
525
526 public function test_restore_event() {
9ede00db 527 $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
528 $data1 = $event1->get_data();
529
4b734e74 530 $event2 = \core\event\base::restore($data1, array('origin'=>'clid'));
d8a1f426
PS
531 $data2 = $event2->get_data();
532
533 $this->assertTrue($event2->is_triggered());
534 $this->assertTrue($event2->is_restored());
535 $this->assertEquals($data1, $data2);
536 $this->assertInstanceOf('core_tests\event\unittest_executed', $event2);
537
538 $this->assertEquals($event1->get_context(), $event2->get_context());
539
540 // Now test problematic data.
541 $data3 = $data1;
542 $data3['eventname'] = '\\a\\b\\c';
4b734e74 543 $event3 = \core\event\base::restore($data3, array());
d8a1f426
PS
544 $this->assertFalse($event3, 'Class name must match');
545
546 $data4 = $data1;
547 unset($data4['userid']);
4b734e74 548 $event4 = \core\event\base::restore($data4, array());
d8a1f426
PS
549 $this->assertInstanceOf('core_tests\event\unittest_executed', $event4);
550 $this->assertDebuggingCalled();
551
552 $data5 = $data1;
553 $data5['xx'] = 'xx';
4b734e74 554 $event5 = \core\event\base::restore($data5, array());
d8a1f426 555 $this->assertInstanceOf('core_tests\event\unittest_executed', $event5);
4b734e74 556 $this->assertDebuggingCalled();
d8a1f426
PS
557
558 }
559
560 public function test_trigger_problems() {
7eaca5a8
561 $this->resetAfterTest(true);
562
9ede00db 563 $event = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>5, 'xx'=>10)));
d8a1f426
PS
564 $event->trigger();
565 try {
566 $event->trigger();
567 $this->fail('Exception expected on double trigger');
96deb950 568 } catch (\moodle_exception $e) {
d8a1f426
PS
569 $this->assertInstanceOf('coding_exception', $e);
570 }
571
572 $data = $event->get_data();
4b734e74 573 $restored = \core_tests\event\unittest_executed::restore($data, array());
d8a1f426
PS
574 $this->assertTrue($restored->is_triggered());
575 $this->assertTrue($restored->is_restored());
576
577 try {
578 $restored->trigger();
579 $this->fail('Exception expected on triggering of restored event');
580 } catch (\moodle_exception $e) {
581 $this->assertInstanceOf('coding_exception', $e);
582 }
22626564 583
9ede00db 584 $event = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>5, 'xx'=>10)));
22626564
PS
585 try {
586 \core\event\manager::dispatch($event);
587 $this->fail('Exception expected on manual event dispatching');
588 } catch (\moodle_exception $e) {
589 $this->assertInstanceOf('coding_exception', $e);
590 }
d8a1f426
PS
591 }
592
593 public function test_bad_events() {
7eaca5a8
594 $this->resetAfterTest(true);
595
27af3e62 596 try {
9ede00db 597 $event = \core_tests\event\unittest_executed::create(array('other'=>array('sample'=>5, 'xx'=>10)));
27af3e62 598 $this->fail('Exception expected when context and contextid missing');
96deb950 599 } catch (\moodle_exception $e) {
27af3e62
PS
600 $this->assertInstanceOf('coding_exception', $e);
601 }
602
603 $event = \core_tests\event\bad_event1::create(array('context'=>\context_system::instance()));
d8a1f426
PS
604 try {
605 $event->trigger();
606 $this->fail('Exception expected when $data not valid');
607 } catch (\moodle_exception $e) {
608 $this->assertInstanceOf('\coding_exception', $e);
609 }
610
27af3e62 611 $event = \core_tests\event\bad_event2::create(array('context'=>\context_system::instance()));
d8a1f426
PS
612 try {
613 $event->trigger();
614 $this->fail('Exception expected when $data not valid');
615 } catch (\moodle_exception $e) {
616 $this->assertInstanceOf('\coding_exception', $e);
617 }
618
38d6fbfa
FM
619 $event = \core_tests\event\bad_event2b::create(array('context'=>\context_system::instance()));
620 @$event->trigger();
621 $this->assertDebuggingCalled();
622
27af3e62 623 $event = \core_tests\event\bad_event3::create(array('context'=>\context_system::instance()));
4b734e74 624 @$event->trigger();
d8a1f426
PS
625 $this->assertDebuggingCalled();
626
27af3e62 627 $event = \core_tests\event\bad_event4::create(array('context'=>\context_system::instance()));
4b734e74 628 @$event->trigger();
d8a1f426
PS
629 $this->assertDebuggingCalled();
630
27af3e62 631 $event = \core_tests\event\bad_event5::create(array('context'=>\context_system::instance()));
4b734e74 632 @$event->trigger();
d8a1f426 633 $this->assertDebuggingCalled();
a85258ca 634
31e571cd 635 $event = \core_tests\event\bad_event6::create(array('objectid'=>1, 'context'=>\context_system::instance()));
a85258ca 636 $event->trigger();
31e571cd 637 $this->assertDebuggingCalled('Unknown table specified in objecttable field');
a85258ca 638
27af3e62 639 $event = \core_tests\event\bad_event7::create(array('objectid'=>1, 'context'=>\context_system::instance()));
a85258ca
PS
640 try {
641 $event->trigger();
31e571cd 642 $this->fail('Exception expected when $data contains objectid but objecttable not specified');
a85258ca
PS
643 } catch (\moodle_exception $e) {
644 $this->assertInstanceOf('\coding_exception', $e);
645 }
31e571cd
FM
646
647 $event = \core_tests\event\bad_event8::create(array('context'=>\context_system::instance()));
648 $event->trigger();
649 $this->assertDebuggingCalled('Event property objectid must be set when objecttable is defined');
d8a1f426
PS
650 }
651
652 public function test_problematic_events() {
7eaca5a8
653 $this->resetAfterTest(true);
654
27af3e62 655 $event1 = \core_tests\event\problematic_event1::create(array('context'=>\context_system::instance()));
d8a1f426 656 $this->assertDebuggingNotCalled();
4b734e74
PS
657 $this->assertNull($event1->xxx);
658 $this->assertDebuggingCalled();
d8a1f426 659
27af3e62 660 $event2 = \core_tests\event\problematic_event1::create(array('xxx'=>0, 'context'=>\context_system::instance()));
d8a1f426
PS
661 $this->assertDebuggingCalled();
662
96f81ea3 663 set_debugging(DEBUG_NONE);
27af3e62 664 $event3 = \core_tests\event\problematic_event1::create(array('xxx'=>0, 'context'=>\context_system::instance()));
d8a1f426 665 $this->assertDebuggingNotCalled();
96f81ea3 666 set_debugging(DEBUG_DEVELOPER);
d8a1f426 667
27af3e62 668 $event4 = \core_tests\event\problematic_event1::create(array('context'=>\context_system::instance(), 'other'=>array('a'=>1)));
d8a1f426
PS
669 $event4->trigger();
670 $this->assertDebuggingNotCalled();
671
27af3e62 672 $event5 = \core_tests\event\problematic_event1::create(array('context'=>\context_system::instance(), 'other'=>(object)array('a'=>1)));
d8a1f426
PS
673 $this->assertDebuggingNotCalled();
674 $event5->trigger();
675 $this->assertDebuggingCalled();
676
677 $url = new moodle_url('/admin/');
27af3e62 678 $event6 = \core_tests\event\problematic_event1::create(array('context'=>\context_system::instance(), 'other'=>array('a'=>$url)));
d8a1f426
PS
679 $this->assertDebuggingNotCalled();
680 $event6->trigger();
681 $this->assertDebuggingCalled();
fddd1018 682
5bb7158d 683 // Check that whole float numbers do not trigger debugging messages.
9ede00db 684 $event7 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(),
5bb7158d
AG
685 'other' => array('wholenumber' => 90.0000, 'numberwithdecimals' => 54.7656, 'sample' => 1)));
686 $event7->trigger();
687 $this->assertDebuggingNotCalled();
688
f8a39659 689 $event = \core_tests\event\problematic_event2::create(array());
097011c4 690 $this->assertDebuggingNotCalled();
f8a39659 691 $event = \core_tests\event\problematic_event2::create(array('context'=>\context_system::instance()));
097011c4 692 $this->assertDebuggingCalled();
fddd1018 693
f8a39659 694 $event = \core_tests\event\problematic_event3::create(array('other'=>1));
fddd1018 695 $this->assertDebuggingNotCalled();
f8a39659 696 $event = \core_tests\event\problematic_event3::create(array());
fddd1018 697 $this->assertDebuggingCalled();
d8a1f426
PS
698 }
699
5fef139c 700 public function test_record_snapshots() {
d8a1f426
PS
701 global $DB;
702
7eaca5a8
703 $this->resetAfterTest(true);
704
9ede00db 705 $event = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
d8a1f426
PS
706 $course1 = $DB->get_record('course', array('id'=>1));
707 $this->assertNotEmpty($course1);
708
5fef139c 709 $event->add_record_snapshot('course', $course1);
d8a1f426 710
9ede00db 711 $result = $event->get_record_snapshot('course', $course1->id);
5e70ea26
MG
712 // Convert to arrays because record snapshot returns a clone of the object.
713 $this->assertSame((array)$course1, (array)$result);
d8a1f426 714
5fef139c 715 $user = $event->get_record_snapshot('user', 1);
d8a1f426
PS
716 $this->assertEquals(1, $user->id);
717 $this->assertSame('guest', $user->username);
300dbc66
PS
718
719 $event->add_record_snapshot('course', $course1);
720 $event->trigger();
721 try {
722 $event->add_record_snapshot('course', $course1);
723 $this->fail('Updating of snapshots after trigger is not ok');;
724 } catch (\moodle_exception $e) {
725 $this->assertInstanceOf('\coding_exception', $e);
726 }
4ad6d5c5
PS
727
728 $event2 = \core_tests\event\unittest_executed::restore($event->get_data(), array());
729 try {
9ede00db 730 $event2->get_record_snapshot('course', $course1->id);
4ad6d5c5
PS
731 $this->fail('Reading of snapshots from restored events is not ok');;
732 } catch (\moodle_exception $e) {
733 $this->assertInstanceOf('\coding_exception', $e);
734 }
d8a1f426 735 }
ed17808d 736
1ccc1268 737 public function test_get_name() {
9ede00db 738 $event = \core_tests\event\noname_event::create(array('other' => array('sample' => 1, 'xx' => 10)));
1ccc1268
AA
739 $this->assertEquals("core_tests: noname event", $event->get_name());
740 }
741
ed17808d 742 public function test_iteration() {
9ede00db 743 $event = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>1, 'xx'=>10)));
ed17808d
PS
744
745 $data = array();
746 foreach ($event as $k => $v) {
747 $data[$k] = $v;
748 }
749
750 $this->assertSame($event->get_data(), $data);
751 }
623a32e5
RT
752
753 /**
754 * @expectedException PHPUnit_Framework_Error_Notice
755 */
756 public function test_context_not_used() {
9ede00db 757 $event = \core_tests\event\context_used_in_event::create(array('other' => array('sample' => 1, 'xx' => 10)));
623a32e5
RT
758 $this->assertEventContextNotUsed($event);
759
760 $eventcontext = phpunit_event_mock::testable_get_event_context($event);
761 phpunit_event_mock::testable_set_event_context($event, null);
762 $this->assertEventContextNotUsed($event);
763 }
7c084af9
AG
764
765 /**
766 * Test that all observer information is returned correctly.
767 */
768 public function test_get_all_observers() {
769 // Retrieve all observers.
770 $observers = \core\event\manager::get_all_observers();
771
772 // Expected information from the workshop allocation scheduled observer.
773 $expected = array();
774 $observer = new stdClass();
775 $observer->callable = '\workshopallocation_scheduled\observer::workshop_viewed';
776 $observer->priority = 0;
777 $observer->internal = true;
778 $observer->includefile = null;
779 $observer->plugintype = 'workshopallocation';
780 $observer->plugin = 'scheduled';
781 $expected[0] = $observer;
782
783 $this->assertEquals($expected, $observers['\mod_workshop\event\course_module_viewed']);
784 }
75cab92b
AG
785
786 /**
787 * Test formatting of the get_explanation method.
788 * This formats the information from an events class docblock.
789 */
790 public function test_get_explanation() {
791 $explanation = \core_tests\event\full_docblock::get_explanation();
792
793 $expected = "This is an explanation of the event.
794 - I'm making a point here.
795 - I have a second {@link something} point here.
796 - whitespace is intentional to test it's removal.
797I have something else *Yeah* that.";
798
799 $this->assertEquals($explanation, $expected);
800
801 $explanation = \core_tests\event\docblock_test2::get_explanation();
802
803 $expected = "We have only the description in the docblock
804and nothing else.";
805
806 $this->assertEquals($explanation, $expected);
807
808 $explanation = \core_tests\event\docblock_test3::get_explanation();
809 $expected = "Calendar event created event.";
810 $this->assertEquals($explanation, $expected);
811
812 }
813
814 /**
815 * Test that general information about an event is returned
816 * by the get_static_info() method.
817 */
818 public function test_get_static_info() {
819 $staticinfo = \core_tests\event\static_info_viewing::get_static_info();
820
821 $expected = array(
822 'eventname' => '\\core_tests\\event\\static_info_viewing',
823 'component' => 'core_tests',
824 'target' => 'static_info',
825 'action' => 'viewing',
826 'crud' => 'r',
827 'edulevel' => 0,
828 'objecttable' => 'mod_unittest'
829 );
830 $this->assertEquals($staticinfo, $expected);
831 }
605a8c33 832}