MDL-39846 add logging TODOs
[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',
4b734e74 37 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
38 'includefile' => 'lib/tests/fixtures/event_fixtures.php',
39 ),
40 array(
41 'eventname' => '*',
4b734e74 42 'callback' => array('\core_tests\event\unittest_observer', 'observe_all'),
d8a1f426
PS
43 'includefile' => null,
44 'internal' => 1,
45 'priority' => 9999,
46 ),
47 array(
48 'eventname' => '\core\event\unknown_executed',
4b734e74 49 'callback' => '\core_tests\event\unittest_observer::broken_observer',
d8a1f426
PS
50 'priority' => 100,
51 ),
52 array(
53 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 54 'callback' => '\core_tests\event\unittest_observer::external_observer',
d8a1f426
PS
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();
4b734e74 68 $observer->callable = array('\core_tests\event\unittest_observer', 'observe_all');
d8a1f426
PS
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();
4b734e74 90 $observer->callable = array('\core_tests\event\unittest_observer', 'observe_all');
d8a1f426
PS
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();
4b734e74 106 $observer->callable = array('\core_tests\event\unittest_observer', 'observe_all');
d8a1f426
PS
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.
4b734e74 120 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
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.
4b734e74 139 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
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.
4b734e74 150 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
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',
4b734e74 172 'callback' => '', // empty callable
d8a1f426
PS
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',
4b734e74 183 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
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',
4b734e74 196 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
197 ),
198 array(
199 'eventname' => '*',
4b734e74 200 'callback' => '\core_tests\event\unittest_observer::observe_all',
d8a1f426
PS
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());
22626564 213 $this->assertFalse($event1->is_dispatched());
d8a1f426
PS
214 $this->assertFalse($event1->is_restored());
215 $event1->trigger();
216 $this->assertTrue($event1->is_triggered());
22626564 217 $this->assertTrue($event1->is_dispatched());
d8a1f426
PS
218 $this->assertFalse($event1->is_restored());
219
220 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>2, 'xx'=>10)));
221 $event1->trigger();
222
223 $this->assertSame(
224 array('observe_all-nesting-1', 'observe_one-1', 'observe_all-3', 'observe_one-3', 'observe_all-2', 'observe_one-2'),
225 \core_tests\event\unittest_observer::$info);
226 }
227
228 public function test_ignore_exceptions() {
229 $observers = array(
230
231 array(
232 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 233 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
234 ),
235
236 array(
237 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 238 'callback' => '\core_tests\event\unittest_observer::broken_observer',
d8a1f426
PS
239 'priority' => 100,
240 ),
241 );
242
243 \core\event\manager::phpunit_replace_observers($observers);
244 \core_tests\event\unittest_observer::reset();
245
246 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>1, 'xx'=>10)));
247 $event1->trigger();
248 $this->assertDebuggingCalled();
249
250 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>2, 'xx'=>10)));
251 $event1->trigger();
252 $this->assertDebuggingCalled();
253
254 $this->assertSame(
255 array('broken_observer-1', 'observe_one-1', 'broken_observer-2', 'observe_one-2'),
256 \core_tests\event\unittest_observer::$info);
257 }
258
259 public function test_external_buffer() {
260 global $DB;
261
262 $this->preventResetByRollback();
263
264 $observers = array(
265
266 array(
267 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 268 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
269 ),
270
271 array(
272 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 273 'callback' => '\core_tests\event\unittest_observer::external_observer',
d8a1f426
PS
274 'priority' => 200,
275 'internal' => 0,
276 ),
277 );
278
279 \core\event\manager::phpunit_replace_observers($observers);
280 \core_tests\event\unittest_observer::reset();
281
282 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>1, 'xx'=>10)));
283 $event1->trigger();
284 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>2, 'xx'=>10)));
285 $event2->trigger();
286
287 $this->assertSame(
288 array('external_observer-1', 'observe_one-1', 'external_observer-2', 'observe_one-2'),
289 \core_tests\event\unittest_observer::$info);
290
291 \core\event\manager::phpunit_replace_observers($observers);
292 \core_tests\event\unittest_observer::reset();
293
294 $this->assertSame(array(), \core_tests\event\unittest_observer::$info);
295
296 $trans = $DB->start_delegated_transaction();
297
298 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>1, 'xx'=>10)));
299 $event1->trigger();
300 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>2, 'xx'=>10)));
301 $event2->trigger();
302
303 $this->assertSame(
304 array('observe_one-1', 'observe_one-2'),
305 \core_tests\event\unittest_observer::$info);
306
307 $trans->allow_commit();
308
309 $this->assertSame(
310 array('observe_one-1', 'observe_one-2', 'external_observer-1', 'external_observer-2'),
311 \core_tests\event\unittest_observer::$info);
312
313 \core\event\manager::phpunit_replace_observers($observers);
314 \core_tests\event\unittest_observer::reset();
315
316 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>1, 'xx'=>10)));
317 $event1->trigger();
318 $trans = $DB->start_delegated_transaction();
319 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>2, 'xx'=>10)));
320 $event2->trigger();
321 try {
322 $trans->rollback(new \moodle_exception('xxx'));
323 $this->fail('Expecting exception');
324 } catch (\moodle_exception $e) {
325 }
326
327 $this->assertSame(
328 array('external_observer-1', 'observe_one-1', 'observe_one-2'),
329 \core_tests\event\unittest_observer::$info);
330 }
331
332 public function test_legacy() {
333 global $DB;
334
335 $this->resetAfterTest(true);
336
337 $observers = array(
338 array(
339 'eventname' => '\core_tests\event\unittest_executed',
4b734e74 340 'callback' => '\core_tests\event\unittest_observer::observe_one',
d8a1f426
PS
341 ),
342 array(
343 'eventname' => '*',
4b734e74 344 'callback' => '\core_tests\event\unittest_observer::observe_all',
d8a1f426
PS
345 'includefile' => null,
346 'internal' => 1,
347 'priority' => 9999,
348 ),
349 );
350
351 $DB->delete_records('log', array());
352 events_update_definition('unittest');
353 $DB->delete_records_select('events_handlers', "component <> 'unittest'");
354 events_get_handlers('reset');
355 $this->assertEquals(3, $DB->count_records('events_handlers'));
356 set_config('loglifetime', 60*60*24*5);
357
358 \core\event\manager::phpunit_replace_observers($observers);
359 \core_tests\event\unittest_observer::reset();
360
361 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>5, 'xx'=>10)));
362 $event1->trigger();
363
364 $event2 = \core_tests\event\unittest_executed::create(array('courseid'=>2, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>6, 'xx'=>11)));
365 $event2->nest = true;
366 $event2->trigger();
367
368
369 $this->assertSame(
370 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'),
371 \core_tests\event\unittest_observer::$info);
372
373 $this->assertSame($event1, \core_tests\event\unittest_observer::$event[0]);
374 $this->assertSame($event1, \core_tests\event\unittest_observer::$event[1]);
375 $this->assertSame(array(1, 5), \core_tests\event\unittest_observer::$event[2]);
376
377
378 $logs = $DB->get_records('log', array(), 'id ASC');
379 $this->assertCount(3, $logs);
380
381 $log = array_shift($logs);
382 $this->assertEquals(1, $log->course);
383 $this->assertSame('core_unittest', $log->module);
384 $this->assertSame('view', $log->action);
385
386 $log = array_shift($logs);
387 $this->assertEquals(2, $log->course);
388 $this->assertSame('core_unittest', $log->module);
389 $this->assertSame('view', $log->action);
390
391 $log = array_shift($logs);
392 $this->assertEquals(3, $log->course);
393 $this->assertSame('core_unittest', $log->module);
394 $this->assertSame('view', $log->action);
395 }
396
397 public function test_restore_event() {
398 $event1 = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>1, 'xx'=>10)));
399 $data1 = $event1->get_data();
400
4b734e74 401 $event2 = \core\event\base::restore($data1, array('origin'=>'clid'));
d8a1f426
PS
402 $data2 = $event2->get_data();
403
404 $this->assertTrue($event2->is_triggered());
405 $this->assertTrue($event2->is_restored());
406 $this->assertEquals($data1, $data2);
407 $this->assertInstanceOf('core_tests\event\unittest_executed', $event2);
408
409 $this->assertEquals($event1->get_context(), $event2->get_context());
410
411 // Now test problematic data.
412 $data3 = $data1;
413 $data3['eventname'] = '\\a\\b\\c';
4b734e74 414 $event3 = \core\event\base::restore($data3, array());
d8a1f426
PS
415 $this->assertFalse($event3, 'Class name must match');
416
417 $data4 = $data1;
418 unset($data4['userid']);
4b734e74 419 $event4 = \core\event\base::restore($data4, array());
d8a1f426
PS
420 $this->assertInstanceOf('core_tests\event\unittest_executed', $event4);
421 $this->assertDebuggingCalled();
422
423 $data5 = $data1;
424 $data5['xx'] = 'xx';
4b734e74 425 $event5 = \core\event\base::restore($data5, array());
d8a1f426 426 $this->assertInstanceOf('core_tests\event\unittest_executed', $event5);
4b734e74 427 $this->assertDebuggingCalled();
d8a1f426
PS
428
429 }
430
431 public function test_trigger_problems() {
432 $event = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>5, 'xx'=>10)));
433 $event->trigger();
434 try {
435 $event->trigger();
436 $this->fail('Exception expected on double trigger');
437 } catch (Exception $e) {
438 $this->assertInstanceOf('coding_exception', $e);
439 }
440
441 $data = $event->get_data();
4b734e74 442 $restored = \core_tests\event\unittest_executed::restore($data, array());
d8a1f426
PS
443 $this->assertTrue($restored->is_triggered());
444 $this->assertTrue($restored->is_restored());
445
446 try {
447 $restored->trigger();
448 $this->fail('Exception expected on triggering of restored event');
449 } catch (\moodle_exception $e) {
450 $this->assertInstanceOf('coding_exception', $e);
451 }
22626564
PS
452
453 $event = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>5, 'xx'=>10)));
454 try {
455 \core\event\manager::dispatch($event);
456 $this->fail('Exception expected on manual event dispatching');
457 } catch (\moodle_exception $e) {
458 $this->assertInstanceOf('coding_exception', $e);
459 }
d8a1f426
PS
460 }
461
462 public function test_bad_events() {
463 $event = \core_tests\event\bad_event1::create();
464 try {
465 $event->trigger();
466 $this->fail('Exception expected when $data not valid');
467 } catch (\moodle_exception $e) {
468 $this->assertInstanceOf('\coding_exception', $e);
469 }
470
471 $event = \core_tests\event\bad_event2::create();
472 try {
473 $event->trigger();
474 $this->fail('Exception expected when $data not valid');
475 } catch (\moodle_exception $e) {
476 $this->assertInstanceOf('\coding_exception', $e);
477 }
478
479 $event = \core_tests\event\bad_event3::create();
4b734e74 480 @$event->trigger();
d8a1f426
PS
481 $this->assertDebuggingCalled();
482
483 $event = \core_tests\event\bad_event4::create();
4b734e74 484 @$event->trigger();
d8a1f426
PS
485 $this->assertDebuggingCalled();
486
487 $event = \core_tests\event\bad_event5::create();
4b734e74 488 @$event->trigger();
d8a1f426
PS
489 $this->assertDebuggingCalled();
490 }
491
492 public function test_problematic_events() {
493 global $CFG;
494 $event1 = \core_tests\event\problematic_event1::create();
495 $this->assertDebuggingNotCalled();
4b734e74
PS
496 $this->assertNull($event1->xxx);
497 $this->assertDebuggingCalled();
d8a1f426
PS
498
499 $event2 = \core_tests\event\problematic_event1::create(array('xxx'=>0));
500 $this->assertDebuggingCalled();
501
502 $CFG->debug = 0;
503 $event3 = \core_tests\event\problematic_event1::create(array('xxx'=>0));
504 $this->assertDebuggingNotCalled();
505 $CFG->debug = E_ALL | E_STRICT;
506
507 $event4 = \core_tests\event\problematic_event1::create(array('extra'=>array('a'=>1)));
508 $event4->trigger();
509 $this->assertDebuggingNotCalled();
510
511 $event5 = \core_tests\event\problematic_event1::create(array('extra'=>(object)array('a'=>1)));
512 $this->assertDebuggingNotCalled();
513 $event5->trigger();
514 $this->assertDebuggingCalled();
515
516 $url = new moodle_url('/admin/');
517 $event6 = \core_tests\event\problematic_event1::create(array('extra'=>array('a'=>$url)));
518 $this->assertDebuggingNotCalled();
519 $event6->trigger();
520 $this->assertDebuggingCalled();
521 }
522
523 public function test_record_cache() {
524 global $DB;
525
526 $event = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>\context_system::instance(), 'extra'=>array('sample'=>1, 'xx'=>10)));
527 $course1 = $DB->get_record('course', array('id'=>1));
528 $this->assertNotEmpty($course1);
529
530 $event->add_cached_record('course', $course1);
531
532 $result = $event->get_cached_record('course', 1, $course1);
533 $this->assertSame($course1, $result);
534
535 $user = $event->get_cached_record('user', 1);
536 $this->assertEquals(1, $user->id);
537 $this->assertSame('guest', $user->username);
538 }
4b734e74 539 }