MDL-39846 implement new event dispatching and base event class
[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
32 public function test_observers_parsing() {
33
34 $observers = array(
35 array(
36 'eventname' => '\core_tests\event\unittest_executed',
37 'callable' => '\core_tests\event\unittest_observer::observe_one',
38 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
39 ),
40 array(
41 'eventname' => '*',
42 'callable' => '\core_tests\event\unittest_observer::observe_all',
43 'includefile' => null,
44 'internal' => 1,
45 'priority' => 9999,
46 ),
47 array(
48 'eventname' => '\core\event\unknown_executed',
49 'callable' => '\core_tests\event\unittest_observer::broken_observer',
50 'priority' => 100,
51 ),
52 array(
53 'eventname' => '\core_tests\event\unittest_executed',
54 'callable' => '\core_tests\event\unittest_observer::external_observer',
55 'priority' => 200,
56 'internal' => 0,
57 ),
58 );
59
60 $result = \core\event\manager::phpunit_replace_observers($observers);
61
62 $this->assertCount(3, $result);
63 end($result);
64 $this->assertSame('*', key($result));
65
66 $expected = array();
67 $observer = new stdClass();
68 $observer->callable = '\core_tests\event\unittest_observer::observe_all';
69 $observer->priority = 9999;
70 $observer->internal = true;
71 $observer->includefile = null;
72 $expected[0] = $observer;
73 $observer = new stdClass();
74 $observer->callable = '\core_tests\event\unittest_observer::external_observer';
75 $observer->priority = 200;
76 $observer->internal = false;
77 $observer->includefile = null;
78 $expected[1] = $observer;
79 $observer = new stdClass();
80 $observer->callable = '\core_tests\event\unittest_observer::observe_one';
81 $observer->priority = 0;
82 $observer->internal = true;
83 $observer->includefile = 'lib/tests/fixtures/event_fixtures.php';
84 $expected[2] = $observer;
85
86 $this->assertEquals($expected, $result['\core_tests\event\unittest_executed']);
87
88 $expected = array();
89 $observer = new stdClass();
90 $observer->callable = '\core_tests\event\unittest_observer::observe_all';
91 $observer->priority = 9999;
92 $observer->internal = true;
93 $observer->includefile = null;
94 $expected[0] = $observer;
95 $observer = new stdClass();
96 $observer->callable = '\core_tests\event\unittest_observer::broken_observer';
97 $observer->priority = 100;
98 $observer->internal = true;
99 $observer->includefile = null;
100 $expected[1] = $observer;
101
102 $this->assertEquals($expected, $result['\core\event\unknown_executed']);
103
104 $expected = array();
105 $observer = new stdClass();
106 $observer->callable = '\core_tests\event\unittest_observer::observe_all';
107 $observer->priority = 9999;
108 $observer->internal = true;
109 $observer->includefile = null;
110 $expected[0] = $observer;
111
112 $this->assertEquals($expected, $result['*']);
113
114
115 // Now test broken stuff...
116
117 $observers = array(
118 array(
119 'eventname' => 'core_tests\event\unittest_executed', // Fix leading backslash.
120 'callable' => '\core_tests\event\unittest_observer::observe_one',
121 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
122 'internal' => 1, // Cast to bool.
123 ),
124 );
125 $result = \core\event\manager::phpunit_replace_observers($observers);
126 $this->assertCount(1, $result);
127 $expected = array();
128 $observer = new stdClass();
129 $observer->callable = '\core_tests\event\unittest_observer::observe_one';
130 $observer->priority = 0;
131 $observer->internal = true;
132 $observer->includefile = 'lib/tests/fixtures/event_fixtures.php';
133 $expected[0] = $observer;
134 $this->assertEquals($expected, $result['\core_tests\event\unittest_executed']);
135
136 $observers = array(
137 array(
138 // Missing eventclass.
139 'callable' => '\core_tests\event\unittest_observer::observe_one',
140 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
141 ),
142 );
143 $result = \core\event\manager::phpunit_replace_observers($observers);
144 $this->assertCount(0, $result);
145 $this->assertDebuggingCalled();
146
147 $observers = array(
148 array(
149 'eventname' => '', // Empty eventclass.
150 'callable' => '\core_tests\event\unittest_observer::observe_one',
151 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
152 ),
153 );
154 $result = \core\event\manager::phpunit_replace_observers($observers);
155 $this->assertCount(0, $result);
156 $this->assertDebuggingCalled();
157
158 $observers = array(
159 array(
160 'eventname' => '\core_tests\event\unittest_executed',
161 // Missing callable.
162 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
163 ),
164 );
165 $result = \core\event\manager::phpunit_replace_observers($observers);
166 $this->assertCount(0, $result);
167 $this->assertDebuggingCalled();
168
169 $observers = array(
170 array(
171 'eventname' => '\core_tests\event\unittest_executed',
172 'callable' => '', // empty callable
173 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
174 ),
175 );
176 $result = \core\event\manager::phpunit_replace_observers($observers);
177 $this->assertCount(0, $result);
178 $this->assertDebuggingCalled();
179
180 $observers = array(
181 array(
182 'eventname' => '\core_tests\event\unittest_executed',
183 'callable' => '\core_tests\event\unittest_observer::observe_one',
184 'includefile' => 'lib/tests/fixtures/event_fixtures.php_xxx', // Missing file.
185 ),
186 );
187 $result = \core\event\manager::phpunit_replace_observers($observers);
188 $this->assertCount(0, $result);
189 $this->assertDebuggingCalled();
190 }
191
192 public function test_normal_dispatching() {
193 $observers = array(
194 array(
195 'eventname' => '\core_tests\event\unittest_executed',
196 'callable' => '\core_tests\event\unittest_observer::observe_one',
197 ),
198 array(
199 'eventname' => '*',
200 'callable' => '\core_tests\event\unittest_observer::observe_all',
201 'includefile' => null,
202 'internal' => 1,
203 'priority' => 9999,
204 ),
205 );
206
207 \core\event\manager::phpunit_replace_observers($observers);
208 \core_tests\event\unittest_observer::reset();
209
210 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>1, 'xx'=>10)));
211 $event1->nest = 1;
212 $this->assertFalse($event1->is_triggered());
213 $this->assertFalse($event1->is_restored());
214 $event1->trigger();
215 $this->assertTrue($event1->is_triggered());
216 $this->assertFalse($event1->is_restored());
217
218 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>2, 'xx'=>10)));
219 $event1->trigger();
220
221 $this->assertSame(
222 array('observe_all-nesting-1', 'observe_one-1', 'observe_all-3', 'observe_one-3', 'observe_all-2', 'observe_one-2'),
223 \core_tests\event\unittest_observer::$info);
224 }
225
226 public function test_ignore_exceptions() {
227 $observers = array(
228
229 array(
230 'eventname' => '\core_tests\event\unittest_executed',
231 'callable' => '\core_tests\event\unittest_observer::observe_one',
232 ),
233
234 array(
235 'eventname' => '\core_tests\event\unittest_executed',
236 'callable' => '\core_tests\event\unittest_observer::broken_observer',
237 'priority' => 100,
238 ),
239 );
240
241 \core\event\manager::phpunit_replace_observers($observers);
242 \core_tests\event\unittest_observer::reset();
243
244 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>1, 'xx'=>10)));
245 $event1->trigger();
246 $this->assertDebuggingCalled();
247
248 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>2, 'xx'=>10)));
249 $event1->trigger();
250 $this->assertDebuggingCalled();
251
252 $this->assertSame(
253 array('broken_observer-1', 'observe_one-1', 'broken_observer-2', 'observe_one-2'),
254 \core_tests\event\unittest_observer::$info);
255 }
256
257 public function test_external_buffer() {
258 global $DB;
259
260 $this->preventResetByRollback();
261
262 $observers = array(
263
264 array(
265 'eventname' => '\core_tests\event\unittest_executed',
266 'callable' => '\core_tests\event\unittest_observer::observe_one',
267 ),
268
269 array(
270 'eventname' => '\core_tests\event\unittest_executed',
271 'callable' => '\core_tests\event\unittest_observer::external_observer',
272 'priority' => 200,
273 'internal' => 0,
274 ),
275 );
276
277 \core\event\manager::phpunit_replace_observers($observers);
278 \core_tests\event\unittest_observer::reset();
279
280 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>1, 'xx'=>10)));
281 $event1->trigger();
282 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>2, 'xx'=>10)));
283 $event2->trigger();
284
285 $this->assertSame(
286 array('external_observer-1', 'observe_one-1', 'external_observer-2', 'observe_one-2'),
287 \core_tests\event\unittest_observer::$info);
288
289 \core\event\manager::phpunit_replace_observers($observers);
290 \core_tests\event\unittest_observer::reset();
291
292 $this->assertSame(array(), \core_tests\event\unittest_observer::$info);
293
294 $trans = $DB->start_delegated_transaction();
295
296 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>1, 'xx'=>10)));
297 $event1->trigger();
298 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>2, 'xx'=>10)));
299 $event2->trigger();
300
301 $this->assertSame(
302 array('observe_one-1', 'observe_one-2'),
303 \core_tests\event\unittest_observer::$info);
304
305 $trans->allow_commit();
306
307 $this->assertSame(
308 array('observe_one-1', 'observe_one-2', 'external_observer-1', 'external_observer-2'),
309 \core_tests\event\unittest_observer::$info);
310
311 \core\event\manager::phpunit_replace_observers($observers);
312 \core_tests\event\unittest_observer::reset();
313
314 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>1, 'xx'=>10)));
315 $event1->trigger();
316 $trans = $DB->start_delegated_transaction();
317 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>2, 'xx'=>10)));
318 $event2->trigger();
319 try {
320 $trans->rollback(new \moodle_exception('xxx'));
321 $this->fail('Expecting exception');
322 } catch (\moodle_exception $e) {
323 }
324
325 $this->assertSame(
326 array('external_observer-1', 'observe_one-1', 'observe_one-2'),
327 \core_tests\event\unittest_observer::$info);
328 }
329
330 public function test_legacy() {
331 global $DB;
332
333 $this->resetAfterTest(true);
334
335 $observers = array(
336 array(
337 'eventname' => '\core_tests\event\unittest_executed',
338 'callable' => '\core_tests\event\unittest_observer::observe_one',
339 ),
340 array(
341 'eventname' => '*',
342 'callable' => '\core_tests\event\unittest_observer::observe_all',
343 'includefile' => null,
344 'internal' => 1,
345 'priority' => 9999,
346 ),
347 );
348
349 $DB->delete_records('log', array());
350 events_update_definition('unittest');
351 $DB->delete_records_select('events_handlers', "component <> 'unittest'");
352 events_get_handlers('reset');
353 $this->assertEquals(3, $DB->count_records('events_handlers'));
354 set_config('loglifetime', 60*60*24*5);
355
356 \core\event\manager::phpunit_replace_observers($observers);
357 \core_tests\event\unittest_observer::reset();
358
359 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>5, 'xx'=>10)));
360 $event1->trigger();
361
362 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>6, 'xx'=>11)));
363 $event2->nest = true;
364 $event2->trigger();
365
366
367 $this->assertSame(
368 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'),
369 \core_tests\event\unittest_observer::$info);
370
371 $this->assertSame($event1, \core_tests\event\unittest_observer::$event[0]);
372 $this->assertSame($event1, \core_tests\event\unittest_observer::$event[1]);
373 $this->assertSame(array(1, 5), \core_tests\event\unittest_observer::$event[2]);
374
375
376 $logs = $DB->get_records('log', array(), 'id ASC');
377 $this->assertCount(3, $logs);
378
379 $log = array_shift($logs);
380 $this->assertEquals(1, $log->course);
381 $this->assertSame('core_unittest', $log->module);
382 $this->assertSame('view', $log->action);
383
384 $log = array_shift($logs);
385 $this->assertEquals(2, $log->course);
386 $this->assertSame('core_unittest', $log->module);
387 $this->assertSame('view', $log->action);
388
389 $log = array_shift($logs);
390 $this->assertEquals(3, $log->course);
391 $this->assertSame('core_unittest', $log->module);
392 $this->assertSame('view', $log->action);
393 }
394
395 public function test_restore_event() {
396 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>1, 'xx'=>10)));
397 $data1 = $event1->get_data();
398
399 $event2 = \core\event\base::restore($data1);
400 $data2 = $event2->get_data();
401
402 $this->assertTrue($event2->is_triggered());
403 $this->assertTrue($event2->is_restored());
404 $this->assertEquals($data1, $data2);
405 $this->assertInstanceOf('core_tests\event\unittest_executed', $event2);
406
407 $this->assertEquals($event1->get_context(), $event2->get_context());
408
409 // Now test problematic data.
410 $data3 = $data1;
411 $data3['eventname'] = '\\a\\b\\c';
412 $event3 = \core\event\base::restore($data3);
413 $this->assertFalse($event3, 'Class name must match');
414
415 $data4 = $data1;
416 unset($data4['userid']);
417 $event4 = \core\event\base::restore($data4);
418 $this->assertInstanceOf('core_tests\event\unittest_executed', $event4);
419 $this->assertDebuggingCalled();
420
421 $data5 = $data1;
422 $data5['xx'] = 'xx';
423 $event5 = \core\event\base::restore($data5);
424 $this->assertInstanceOf('core_tests\event\unittest_executed', $event5);
425 $this->assertDebuggingNotCalled();
426
427 }
428
429 public function test_trigger_problems() {
430 $event = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>5, 'xx'=>10)));
431 $event->trigger();
432 try {
433 $event->trigger();
434 $this->fail('Exception expected on double trigger');
435 } catch (Exception $e) {
436 $this->assertInstanceOf('coding_exception', $e);
437 }
438
439 $data = $event->get_data();
440 $restored = \core_tests\event\unittest_executed::restore($data);
441 $this->assertTrue($restored->is_triggered());
442 $this->assertTrue($restored->is_restored());
443
444 try {
445 $restored->trigger();
446 $this->fail('Exception expected on triggering of restored event');
447 } catch (\moodle_exception $e) {
448 $this->assertInstanceOf('coding_exception', $e);
449 }
450 }
451
452 public function test_bad_events() {
453 $event = \core_tests\event\bad_event1::create();
454 try {
455 $event->trigger();
456 $this->fail('Exception expected when $data not valid');
457 } catch (\moodle_exception $e) {
458 $this->assertInstanceOf('\coding_exception', $e);
459 }
460
461 $event = \core_tests\event\bad_event2::create();
462 try {
463 $event->trigger();
464 $this->fail('Exception expected when $data not valid');
465 } catch (\moodle_exception $e) {
466 $this->assertInstanceOf('\coding_exception', $e);
467 }
468
469 $event = \core_tests\event\bad_event3::create();
470 $event->trigger();
471 $this->assertDebuggingCalled();
472
473 $event = \core_tests\event\bad_event4::create();
474 $event->trigger();
475 $this->assertDebuggingCalled();
476
477 $event = \core_tests\event\bad_event5::create();
478 $event->trigger();
479 $this->assertDebuggingCalled();
480 }
481
482 public function test_problematic_events() {
483 global $CFG;
484 $event1 = \core_tests\event\problematic_event1::create();
485 $this->assertDebuggingNotCalled();
486
487 $event2 = \core_tests\event\problematic_event1::create(array('xxx'=>0));
488 $this->assertDebuggingCalled();
489
490 $CFG->debug = 0;
491 $event3 = \core_tests\event\problematic_event1::create(array('xxx'=>0));
492 $this->assertDebuggingNotCalled();
493 $CFG->debug = E_ALL | E_STRICT;
494
495 $event4 = \core_tests\event\problematic_event1::create(array('extra'=>array('a'=>1)));
496 $event4->trigger();
497 $this->assertDebuggingNotCalled();
498
499 $event5 = \core_tests\event\problematic_event1::create(array('extra'=>(object)array('a'=>1)));
500 $this->assertDebuggingNotCalled();
501 $event5->trigger();
502 $this->assertDebuggingCalled();
503
504 $url = new moodle_url('/admin/');
505 $event6 = \core_tests\event\problematic_event1::create(array('extra'=>array('a'=>$url)));
506 $this->assertDebuggingNotCalled();
507 $event6->trigger();
508 $this->assertDebuggingCalled();
509 }
510
511 public function test_record_cache() {
512 global $DB;
513
514 $event = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>1, 'xx'=>10)));
515 $course1 = $DB->get_record('course', array('id'=>1));
516 $this->assertNotEmpty($course1);
517
518 $event->add_cached_record('course', $course1);
519
520 $result = $event->get_cached_record('course', 1, $course1);
521 $this->assertSame($course1, $result);
522
523 $user = $event->get_cached_record('user', 1);
524 $this->assertEquals(1, $user->id);
525 $this->assertSame('guest', $user->username);
526 }
527}