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