Merge branch 'MDL-55609-master' of git://github.com/andrewnicols/moodle
[moodle.git] / lib / tests / eventslib_test.php
blobc62d5cf2e4fe04be5673b139541a3654c204a128
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 const DEBUGGING_MSG = 'Events API using $handlers array has been deprecated in favour of Events 2 API, please use it instead.';
34 /**
35 * Create temporary entries in the database for these tests.
36 * These tests have to work no matter the data currently in the database
37 * (meaning they should run on a brand new site). This means several items of
38 * data have to be artificially inseminated (:-) in the DB.
40 protected function setUp() {
41 parent::setUp();
42 // Set global category settings to -1 (not force).
43 eventslib_sample_function_handler('reset');
44 eventslib_sample_handler_class::static_method('reset');
46 $this->resetAfterTest();
49 /**
50 * Tests the installation of event handlers from file
52 public function test_events_update_definition__install() {
53 global $DB;
55 events_update_definition('unittest');
56 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
58 $dbcount = $DB->count_records('events_handlers', array('component'=>'unittest'));
59 $handlers = array();
60 require(__DIR__.'/fixtures/events.php');
61 $this->assertCount($dbcount, $handlers, 'Equal number of handlers in file and db: %s');
64 /**
65 * Tests the uninstallation of event handlers from file.
67 public function test_events_update_definition__uninstall() {
68 global $DB;
70 events_update_definition('unittest');
71 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
73 events_uninstall('unittest');
74 $this->assertEquals(0, $DB->count_records('events_handlers', array('component'=>'unittest')), 'All handlers should be uninstalled: %s');
77 /**
78 * Tests the update of event handlers from file.
80 public function test_events_update_definition__update() {
81 global $DB;
83 events_update_definition('unittest');
84 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
86 // First modify directly existing handler.
87 $handler = $DB->get_record('events_handlers', array('component'=>'unittest', 'eventname'=>'test_instant'));
89 $original = $handler->handlerfunction;
91 // Change handler in db.
92 $DB->set_field('events_handlers', 'handlerfunction', serialize('some_other_function_handler'), array('id'=>$handler->id));
94 // Update the definition, it should revert the handler back.
95 events_update_definition('unittest');
96 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
97 $handler = $DB->get_record('events_handlers', array('component'=>'unittest', 'eventname'=>'test_instant'));
98 $this->assertSame($handler->handlerfunction, $original, 'update should sync db with file definition: %s');
102 * Tests events_trigger_is_registered() function.
104 public function test_events_is_registered() {
106 events_update_definition('unittest');
107 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
109 $this->assertTrue(events_is_registered('test_instant', 'unittest'));
110 $this->assertDebuggingCalled('events_is_registered() has been deprecated along with all Events 1 API in favour of Events 2' .
111 ' API, please use it instead.', DEBUG_DEVELOPER);
115 * Tests events_trigger_legacy() function.
117 public function test_events_trigger_legacy_instant() {
119 events_update_definition('unittest');
120 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
122 $this->assertEquals(0, events_trigger_legacy('test_instant', 'ok'));
123 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
124 $this->assertEquals(0, events_trigger_legacy('test_instant', 'ok'));
125 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
126 $this->assertEquals(2, eventslib_sample_function_handler('status'));
130 * Tests events_trigger_legacy() function.
132 public function test_events_trigger__cron() {
134 events_update_definition('unittest');
135 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
137 $this->assertEquals(0, events_trigger_legacy('test_cron', 'ok'));
138 $this->assertEquals(0, eventslib_sample_handler_class::static_method('status'));
139 events_cron('test_cron');
140 // The events_cron one + one for each triggered event above (triggered in events_dispatch).
141 $this->assertDebuggingCalledCount(2, array(self::DEBUGGING_MSG, self::DEBUGGING_MSG),
142 array(DEBUG_DEVELOPER, DEBUG_DEVELOPER));
143 $this->assertEquals(1, eventslib_sample_handler_class::static_method('status'));
147 * Tests events_pending_count() function.
149 public function test_events_pending_count() {
151 events_update_definition('unittest');
152 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
154 events_trigger_legacy('test_cron', 'ok');
155 $this->assertDebuggingNotCalled();
156 events_trigger_legacy('test_cron', 'ok');
157 $this->assertDebuggingNotCalled();
158 events_cron('test_cron');
159 // The events_cron one + one for each triggered event above (triggered in events_dispatch).
160 $this->assertDebuggingCalledCount(3);
161 $this->assertEquals(0, events_pending_count('test_cron'), 'all messages should be already dequeued: %s');
162 $this->assertDebuggingCalled('events_pending_count() has been deprecated along with all Events 1 API in favour of Events 2' .
163 ' API, please use it instead.', DEBUG_DEVELOPER);
167 * Tests events_trigger_legacy() function when instant handler fails.
169 public function test_events_trigger__failed_instant() {
170 global $CFG;
172 events_update_definition('unittest');
173 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
175 $olddebug = $CFG->debug;
177 $this->assertEquals(1, events_trigger_legacy('test_instant', 'fail'), 'fail first event: %s');
178 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
179 $this->assertEquals(1, events_trigger_legacy('test_instant', 'ok'), 'this one should fail too: %s');
180 $this->assertDebuggingNotCalled();
182 $this->assertEquals(0, events_cron('test_instant'), 'all events should stay in queue: %s');
183 // events_cron + one for each dispatched event.
184 $this->assertDebuggingCalledCount(3);
186 $this->assertEquals(2, events_pending_count('test_instant'), 'two events should in queue: %s');
187 $this->assertDebuggingCalled('events_pending_count() has been deprecated along with all Events 1 API in favour of Events 2' .
188 ' API, please use it instead.', DEBUG_DEVELOPER);
190 $this->assertEquals(0, eventslib_sample_function_handler('status'), 'verify no event dispatched yet: %s');
191 eventslib_sample_function_handler('ignorefail'); // Ignore "fail" eventdata from now on.
192 $this->assertEquals(1, events_trigger_legacy('test_instant', 'ok'), 'this one should go to queue directly: %s');
193 $this->assertDebuggingNotCalled();
195 $this->assertEquals(3, events_pending_count('test_instant'), 'three events should in queue: %s');
196 $this->assertDebuggingCalled('events_pending_count() has been deprecated along with all Events 1 API in favour of Events 2' .
197 ' API, please use it instead.', DEBUG_DEVELOPER);
199 $this->assertEquals(0, eventslib_sample_function_handler('status'), 'verify previous event was not dispatched: %s');
200 $this->assertEquals(3, events_cron('test_instant'), 'all events should be dispatched: %s');
201 // events_cron + one for each dispatched event.
202 $this->assertDebuggingCalledCount(4);
204 $this->assertEquals(3, eventslib_sample_function_handler('status'), 'verify three events were dispatched: %s');
205 $this->assertEquals(0, events_pending_count('test_instant'), 'no events should in queue: %s');
206 $this->assertDebuggingCalled('events_pending_count() has been deprecated along with all Events 1 API in favour of Events 2' .
207 ' API, please use it instead.', DEBUG_DEVELOPER);
209 $this->assertEquals(0, events_trigger_legacy('test_instant', 'ok'), 'this event should be dispatched immediately: %s');
210 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
212 $this->assertEquals(4, eventslib_sample_function_handler('status'), 'verify event was dispatched: %s');
213 $this->assertEquals(0, events_pending_count('test_instant'), 'no events should in queue: %s');
214 $this->assertDebuggingCalled('events_pending_count() has been deprecated along with all Events 1 API in favour of Events 2' .
215 ' API, please use it instead.', DEBUG_DEVELOPER);
219 * Tests events_trigger() function.
221 public function test_events_trigger_debugging() {
223 events_update_definition('unittest');
224 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
226 $this->assertEquals(0, events_trigger('test_instant', 'ok'));
227 $debugmessages = array('events_trigger() is deprecated, please use new events instead', self::DEBUGGING_MSG);
228 $this->assertDebuggingCalledCount(2, $debugmessages, array(DEBUG_DEVELOPER, DEBUG_DEVELOPER));
233 * Test handler function.
235 function eventslib_sample_function_handler($eventdata) {
236 static $called = 0;
237 static $ignorefail = false;
239 if ($eventdata == 'status') {
240 return $called;
242 } else if ($eventdata == 'reset') {
243 $called = 0;
244 $ignorefail = false;
245 return;
247 } else if ($eventdata == 'fail') {
248 if ($ignorefail) {
249 $called++;
250 return true;
251 } else {
252 return false;
255 } else if ($eventdata == 'ignorefail') {
256 $ignorefail = true;
257 return;
259 } else if ($eventdata == 'ok') {
260 $called++;
261 return true;
264 print_error('invalideventdata', '', '', $eventdata);
269 * Test handler class with static method.
271 class eventslib_sample_handler_class {
272 public static function static_method($eventdata) {
273 static $called = 0;
274 static $ignorefail = false;
276 if ($eventdata == 'status') {
277 return $called;
279 } else if ($eventdata == 'reset') {
280 $called = 0;
281 $ignorefail = false;
282 return;
284 } else if ($eventdata == 'fail') {
285 if ($ignorefail) {
286 $called++;
287 return true;
288 } else {
289 return false;
292 } else if ($eventdata == 'ignorefail') {
293 $ignorefail = true;
294 return;
296 } else if ($eventdata == 'ok') {
297 $called++;
298 return true;
301 print_error('invalideventdata', '', '', $eventdata);