MDL-49294 logging: Improve cleanup tests
[moodle.git] / lib / tests / eventslib_test.php
blob0428744a24fddff01163030b9e976fa81698e898
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.
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/>.
17 /**
18 * Tests events subsystem.
20 * @package core_event
21 * @subpackage phpunit
22 * @copyright 2007 onwards Martin Dougiamas (http://dougiamas.com)
23 * @author Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
30 class core_eventslib_testcase extends advanced_testcase {
32 /**
33 * Create temporary entries in the database for these tests.
34 * These tests have to work no matter the data currently in the database
35 * (meaning they should run on a brand new site). This means several items of
36 * data have to be artificially inseminated (:-) in the DB.
38 protected function setUp() {
39 parent::setUp();
40 // Set global category settings to -1 (not force).
41 eventslib_sample_function_handler('reset');
42 eventslib_sample_handler_class::static_method('reset');
43 events_update_definition('unittest');
45 $this->resetAfterTest();
48 /**
49 * Tests the installation of event handlers from file
51 public function test_events_update_definition__install() {
52 global $DB;
54 $dbcount = $DB->count_records('events_handlers', array('component'=>'unittest'));
55 $handlers = array();
56 require(__DIR__.'/fixtures/events.php');
57 $this->assertCount($dbcount, $handlers, 'Equal number of handlers in file and db: %s');
60 /**
61 * Tests the uninstallation of event handlers from file.
63 public function test_events_update_definition__uninstall() {
64 global $DB;
66 events_uninstall('unittest');
67 $this->assertEquals(0, $DB->count_records('events_handlers', array('component'=>'unittest')), 'All handlers should be uninstalled: %s');
70 /**
71 * Tests the update of event handlers from file.
73 public function test_events_update_definition__update() {
74 global $DB;
75 // First modify directly existing handler.
76 $handler = $DB->get_record('events_handlers', array('component'=>'unittest', 'eventname'=>'test_instant'));
78 $original = $handler->handlerfunction;
80 // Change handler in db.
81 $DB->set_field('events_handlers', 'handlerfunction', serialize('some_other_function_handler'), array('id'=>$handler->id));
83 // Update the definition, it should revert the handler back.
84 events_update_definition('unittest');
85 $handler = $DB->get_record('events_handlers', array('component'=>'unittest', 'eventname'=>'test_instant'));
86 $this->assertSame($handler->handlerfunction, $original, 'update should sync db with file definition: %s');
89 /**
90 * Tests events_trigger_is_registered() function.
92 public function test_events_is_registered() {
93 $this->assertTrue(events_is_registered('test_instant', 'unittest'));
96 /**
97 * Tests events_trigger_legacy() function.
99 public function test_events_trigger_legacy_instant() {
100 $this->assertEquals(0, events_trigger_legacy('test_instant', 'ok'));
101 $this->assertEquals(0, events_trigger_legacy('test_instant', 'ok'));
102 $this->assertEquals(2, eventslib_sample_function_handler('status'));
106 * Tests events_trigger_legacy() function.
108 public function test_events_trigger__cron() {
109 $this->assertEquals(0, events_trigger_legacy('test_cron', 'ok'));
110 $this->assertEquals(0, eventslib_sample_handler_class::static_method('status'));
111 events_cron('test_cron');
112 $this->assertEquals(1, eventslib_sample_handler_class::static_method('status'));
116 * Tests events_pending_count() function.
118 public function test_events_pending_count() {
119 events_trigger_legacy('test_cron', 'ok');
120 events_trigger_legacy('test_cron', 'ok');
121 events_cron('test_cron');
122 $this->assertEquals(0, events_pending_count('test_cron'), 'all messages should be already dequeued: %s');
126 * Tests events_trigger_legacy() function when instant handler fails.
128 public function test_events_trigger__failed_instant() {
129 global $CFG;
130 $olddebug = $CFG->debug;
132 $this->assertEquals(1, events_trigger_legacy('test_instant', 'fail'), 'fail first event: %s');
133 $this->assertEquals(1, events_trigger_legacy('test_instant', 'ok'), 'this one should fail too: %s');
135 $this->assertEquals(0, events_cron('test_instant'), 'all events should stay in queue: %s');
136 $this->assertDebuggingCalled();
138 $this->assertEquals(2, events_pending_count('test_instant'), 'two events should in queue: %s');
139 $this->assertEquals(0, eventslib_sample_function_handler('status'), 'verify no event dispatched yet: %s');
140 eventslib_sample_function_handler('ignorefail'); // Ignore "fail" eventdata from now on.
141 $this->assertEquals(1, events_trigger_legacy('test_instant', 'ok'), 'this one should go to queue directly: %s');
142 $this->assertEquals(3, events_pending_count('test_instant'), 'three events should in queue: %s');
143 $this->assertEquals(0, eventslib_sample_function_handler('status'), 'verify previous event was not dispatched: %s');
144 $this->assertEquals(3, events_cron('test_instant'), 'all events should be dispatched: %s');
145 $this->assertEquals(3, eventslib_sample_function_handler('status'), 'verify three events were dispatched: %s');
146 $this->assertEquals(0, events_pending_count('test_instant'), 'no events should in queue: %s');
147 $this->assertEquals(0, events_trigger_legacy('test_instant', 'ok'), 'this event should be dispatched immediately: %s');
148 $this->assertEquals(4, eventslib_sample_function_handler('status'), 'verify event was dispatched: %s');
149 $this->assertEquals(0, events_pending_count('test_instant'), 'no events should in queue: %s');
153 * Tests events_trigger() function.
155 public function test_events_trigger_debugging() {
156 $this->assertEquals(0, events_trigger('test_instant', 'ok'));
157 $this->assertDebuggingCalled();
162 * Test handler function.
164 function eventslib_sample_function_handler($eventdata) {
165 static $called = 0;
166 static $ignorefail = false;
168 if ($eventdata == 'status') {
169 return $called;
171 } else if ($eventdata == 'reset') {
172 $called = 0;
173 $ignorefail = false;
174 return;
176 } else if ($eventdata == 'fail') {
177 if ($ignorefail) {
178 $called++;
179 return true;
180 } else {
181 return false;
184 } else if ($eventdata == 'ignorefail') {
185 $ignorefail = true;
186 return;
188 } else if ($eventdata == 'ok') {
189 $called++;
190 return true;
193 print_error('invalideventdata', '', '', $eventdata);
198 * Test handler class with static method.
200 class eventslib_sample_handler_class {
201 public static function static_method($eventdata) {
202 static $called = 0;
203 static $ignorefail = false;
205 if ($eventdata == 'status') {
206 return $called;
208 } else if ($eventdata == 'reset') {
209 $called = 0;
210 $ignorefail = false;
211 return;
213 } else if ($eventdata == 'fail') {
214 if ($ignorefail) {
215 $called++;
216 return true;
217 } else {
218 return false;
221 } else if ($eventdata == 'ignorefail') {
222 $ignorefail = true;
223 return;
225 } else if ($eventdata == 'ok') {
226 $called++;
227 return true;
230 print_error('invalideventdata', '', '', $eventdata);