MDL-55188 events: First deprecation of eventslib.php
[moodle.git] / lib / tests / eventslib_test.php
CommitLineData
a3d5830a
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/**
e6e632ec 18 * Tests events subsystem.
a3d5830a 19 *
e6e632ec
PS
20 * @package core_event
21 * @subpackage phpunit
a3d5830a
PS
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
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
a3d5830a 29
d8a1f426 30class core_eventslib_testcase extends advanced_testcase {
a3d5830a 31
20ff2fba
DM
32 const DEBUGGING_MSG = 'Events API using $handlers array has been deprecated in favour of Events 2 API, please use it instead.';
33
a3d5830a
PS
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.
a3d5830a 39 */
1cbf2a20 40 protected function setUp() {
a3d5830a 41 parent::setUp();
e6e632ec 42 // Set global category settings to -1 (not force).
4ca04fb5
PS
43 eventslib_sample_function_handler('reset');
44 eventslib_sample_handler_class::static_method('reset');
a3d5830a 45
e6e632ec 46 $this->resetAfterTest();
a3d5830a
PS
47 }
48
a3d5830a
PS
49 /**
50 * Tests the installation of event handlers from file
a3d5830a
PS
51 */
52 public function test_events_update_definition__install() {
e6e632ec 53 global $DB;
a3d5830a 54
20ff2fba 55 events_update_definition('unittest');
5454e72c 56 $this->assertDebuggingCalledCount(4);
20ff2fba 57
a3d5830a
PS
58 $dbcount = $DB->count_records('events_handlers', array('component'=>'unittest'));
59 $handlers = array();
60 require(__DIR__.'/fixtures/events.php');
e6e632ec 61 $this->assertCount($dbcount, $handlers, 'Equal number of handlers in file and db: %s');
a3d5830a
PS
62 }
63
64 /**
e6e632ec 65 * Tests the uninstallation of event handlers from file.
a3d5830a
PS
66 */
67 public function test_events_update_definition__uninstall() {
68 global $DB;
69
20ff2fba 70 events_update_definition('unittest');
5454e72c 71 $this->assertDebuggingCalledCount(4);
20ff2fba 72
a3d5830a 73 events_uninstall('unittest');
5454e72c 74 $this->assertDebuggingCalledCount(4);
a3d5830a
PS
75 $this->assertEquals(0, $DB->count_records('events_handlers', array('component'=>'unittest')), 'All handlers should be uninstalled: %s');
76 }
77
78 /**
e6e632ec 79 * Tests the update of event handlers from file.
a3d5830a
PS
80 */
81 public function test_events_update_definition__update() {
82 global $DB;
20ff2fba
DM
83
84 events_update_definition('unittest');
5454e72c 85 $this->assertDebuggingCalledCount(4);
20ff2fba 86
e6e632ec 87 // First modify directly existing handler.
a3d5830a
PS
88 $handler = $DB->get_record('events_handlers', array('component'=>'unittest', 'eventname'=>'test_instant'));
89
90 $original = $handler->handlerfunction;
91
e6e632ec 92 // Change handler in db.
a3d5830a
PS
93 $DB->set_field('events_handlers', 'handlerfunction', serialize('some_other_function_handler'), array('id'=>$handler->id));
94
e6e632ec 95 // Update the definition, it should revert the handler back.
a3d5830a 96 events_update_definition('unittest');
5454e72c 97 $this->assertDebuggingCalledCount(4);
a3d5830a 98 $handler = $DB->get_record('events_handlers', array('component'=>'unittest', 'eventname'=>'test_instant'));
e6e632ec 99 $this->assertSame($handler->handlerfunction, $original, 'update should sync db with file definition: %s');
a3d5830a
PS
100 }
101
102 /**
e6e632ec 103 * Tests events_trigger_is_registered() function.
a3d5830a
PS
104 */
105 public function test_events_is_registered() {
20ff2fba
DM
106
107 events_update_definition('unittest');
108 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
109
a3d5830a 110 $this->assertTrue(events_is_registered('test_instant', 'unittest'));
20ff2fba
DM
111 $this->assertDebuggingCalled('events_is_registered() has been deprecated along with all Events 1 API in favour of Events 2' .
112 ' API, please use it instead.', DEBUG_DEVELOPER);
a3d5830a
PS
113 }
114
115 /**
9634712a 116 * Tests events_trigger_legacy() function.
a3d5830a 117 */
9634712a 118 public function test_events_trigger_legacy_instant() {
20ff2fba
DM
119
120 events_update_definition('unittest');
121 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
122
9634712a 123 $this->assertEquals(0, events_trigger_legacy('test_instant', 'ok'));
20ff2fba 124 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
9634712a 125 $this->assertEquals(0, events_trigger_legacy('test_instant', 'ok'));
20ff2fba 126 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
4ca04fb5 127 $this->assertEquals(2, eventslib_sample_function_handler('status'));
a3d5830a
PS
128 }
129
130 /**
9634712a 131 * Tests events_trigger_legacy() function.
a3d5830a
PS
132 */
133 public function test_events_trigger__cron() {
20ff2fba
DM
134
135 events_update_definition('unittest');
136 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
137
9634712a 138 $this->assertEquals(0, events_trigger_legacy('test_cron', 'ok'));
4ca04fb5 139 $this->assertEquals(0, eventslib_sample_handler_class::static_method('status'));
a3d5830a 140 events_cron('test_cron');
20ff2fba
DM
141 // The events_cron one + one for each triggered event above (triggered in events_dispatch).
142 $this->assertDebuggingCalledCount(2, array(self::DEBUGGING_MSG, self::DEBUGGING_MSG),
143 array(DEBUG_DEVELOPER, DEBUG_DEVELOPER));
4ca04fb5 144 $this->assertEquals(1, eventslib_sample_handler_class::static_method('status'));
a3d5830a
PS
145 }
146
147 /**
e6e632ec 148 * Tests events_pending_count() function.
a3d5830a
PS
149 */
150 public function test_events_pending_count() {
20ff2fba
DM
151
152 events_update_definition('unittest');
153 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
154
9634712a 155 events_trigger_legacy('test_cron', 'ok');
20ff2fba 156 $this->assertDebuggingNotCalled();
9634712a 157 events_trigger_legacy('test_cron', 'ok');
20ff2fba 158 $this->assertDebuggingNotCalled();
a3d5830a 159 events_cron('test_cron');
20ff2fba
DM
160 // The events_cron one + one for each triggered event above (triggered in events_dispatch).
161 $this->assertDebuggingCalledCount(3);
a3d5830a 162 $this->assertEquals(0, events_pending_count('test_cron'), 'all messages should be already dequeued: %s');
20ff2fba
DM
163 $this->assertDebuggingCalled('events_pending_count() has been deprecated along with all Events 1 API in favour of Events 2' .
164 ' API, please use it instead.', DEBUG_DEVELOPER);
a3d5830a
PS
165 }
166
167 /**
9634712a 168 * Tests events_trigger_legacy() function when instant handler fails.
a3d5830a
PS
169 */
170 public function test_events_trigger__failed_instant() {
d451eb4d 171 global $CFG;
20ff2fba
DM
172
173 events_update_definition('unittest');
174 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
175
d451eb4d
SH
176 $olddebug = $CFG->debug;
177
9634712a 178 $this->assertEquals(1, events_trigger_legacy('test_instant', 'fail'), 'fail first event: %s');
20ff2fba 179 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
9634712a 180 $this->assertEquals(1, events_trigger_legacy('test_instant', 'ok'), 'this one should fail too: %s');
20ff2fba 181 $this->assertDebuggingNotCalled();
d451eb4d 182
a3d5830a 183 $this->assertEquals(0, events_cron('test_instant'), 'all events should stay in queue: %s');
20ff2fba
DM
184 // events_cron + one for each dispatched event.
185 $this->assertDebuggingCalledCount(3);
d451eb4d 186
a3d5830a 187 $this->assertEquals(2, events_pending_count('test_instant'), 'two events should in queue: %s');
20ff2fba
DM
188 $this->assertDebuggingCalled('events_pending_count() has been deprecated along with all Events 1 API in favour of Events 2' .
189 ' API, please use it instead.', DEBUG_DEVELOPER);
190
4ca04fb5 191 $this->assertEquals(0, eventslib_sample_function_handler('status'), 'verify no event dispatched yet: %s');
e6e632ec 192 eventslib_sample_function_handler('ignorefail'); // Ignore "fail" eventdata from now on.
9634712a 193 $this->assertEquals(1, events_trigger_legacy('test_instant', 'ok'), 'this one should go to queue directly: %s');
20ff2fba
DM
194 $this->assertDebuggingNotCalled();
195
a3d5830a 196 $this->assertEquals(3, events_pending_count('test_instant'), 'three events should in queue: %s');
20ff2fba
DM
197 $this->assertDebuggingCalled('events_pending_count() has been deprecated along with all Events 1 API in favour of Events 2' .
198 ' API, please use it instead.', DEBUG_DEVELOPER);
199
4ca04fb5 200 $this->assertEquals(0, eventslib_sample_function_handler('status'), 'verify previous event was not dispatched: %s');
a3d5830a 201 $this->assertEquals(3, events_cron('test_instant'), 'all events should be dispatched: %s');
20ff2fba
DM
202 // events_cron + one for each dispatched event.
203 $this->assertDebuggingCalledCount(4);
204
4ca04fb5 205 $this->assertEquals(3, eventslib_sample_function_handler('status'), 'verify three events were dispatched: %s');
a3d5830a 206 $this->assertEquals(0, events_pending_count('test_instant'), 'no events should in queue: %s');
20ff2fba
DM
207 $this->assertDebuggingCalled('events_pending_count() has been deprecated along with all Events 1 API in favour of Events 2' .
208 ' API, please use it instead.', DEBUG_DEVELOPER);
209
9634712a 210 $this->assertEquals(0, events_trigger_legacy('test_instant', 'ok'), 'this event should be dispatched immediately: %s');
20ff2fba
DM
211 $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
212
4ca04fb5 213 $this->assertEquals(4, eventslib_sample_function_handler('status'), 'verify event was dispatched: %s');
a3d5830a 214 $this->assertEquals(0, events_pending_count('test_instant'), 'no events should in queue: %s');
20ff2fba
DM
215 $this->assertDebuggingCalled('events_pending_count() has been deprecated along with all Events 1 API in favour of Events 2' .
216 ' API, please use it instead.', DEBUG_DEVELOPER);
a3d5830a 217 }
a3d5830a 218
9634712a 219 /**
2769bf31 220 * Tests that events_trigger throws an exception.
9634712a 221 */
2769bf31 222 public function test_events_trigger_exception() {
223 $this->expectException('coding_exception');
224 events_trigger('test_instant', 'ok');
9634712a
AA
225 }
226}
a3d5830a 227
e6e632ec
PS
228/**
229 * Test handler function.
230 */
4ca04fb5
PS
231function eventslib_sample_function_handler($eventdata) {
232 static $called = 0;
233 static $ignorefail = false;
234
235 if ($eventdata == 'status') {
236 return $called;
237
238 } else if ($eventdata == 'reset') {
239 $called = 0;
240 $ignorefail = false;
241 return;
242
243 } else if ($eventdata == 'fail') {
244 if ($ignorefail) {
245 $called++;
246 return true;
247 } else {
248 return false;
249 }
250
251 } else if ($eventdata == 'ignorefail') {
252 $ignorefail = true;
253 return;
254
255 } else if ($eventdata == 'ok') {
256 $called++;
257 return true;
258 }
259
260 print_error('invalideventdata', '', '', $eventdata);
261}
262
263
e6e632ec
PS
264/**
265 * Test handler class with static method.
266 */
4ca04fb5 267class eventslib_sample_handler_class {
e6e632ec 268 public static function static_method($eventdata) {
4ca04fb5
PS
269 static $called = 0;
270 static $ignorefail = false;
271
272 if ($eventdata == 'status') {
273 return $called;
274
275 } else if ($eventdata == 'reset') {
276 $called = 0;
277 $ignorefail = false;
278 return;
279
280 } else if ($eventdata == 'fail') {
281 if ($ignorefail) {
282 $called++;
283 return true;
284 } else {
285 return false;
286 }
287
288 } else if ($eventdata == 'ignorefail') {
289 $ignorefail = true;
290 return;
291
292 } else if ($eventdata == 'ok') {
293 $called++;
294 return true;
295 }
296
297 print_error('invalideventdata', '', '', $eventdata);
298 }
299}