MDL-37894 Amend tests with new openclose rule.
[moodle.git] / lib / tests / eventslib_test.php
blob82e61e5e6786e05c41c111e741692aaccd2fb847
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 subsystems
20 * @package core
21 * @subpackage event
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 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.
37 * @return void
39 protected function setUp() {
40 parent::setUp();
41 // Set global category settings to -1 (not force)
42 eventslib_sample_function_handler('reset');
43 eventslib_sample_handler_class::static_method('reset');
44 events_update_definition('unittest');
46 $this->resetAfterTest(true);
49 /**
50 * Tests the installation of event handlers from file
51 * @return void
53 public function test_events_update_definition__install() {
54 global $CFG, $DB;
56 $dbcount = $DB->count_records('events_handlers', array('component'=>'unittest'));
57 $handlers = array();
58 require(__DIR__.'/fixtures/events.php');
59 $filecount = count($handlers);
60 $this->assertEquals($dbcount, $filecount, 'Equal number of handlers in file and db: %s');
63 /**
64 * Tests the uninstallation of event handlers from file
65 * @return void
67 public function test_events_update_definition__uninstall() {
68 global $DB;
70 events_uninstall('unittest');
71 $this->assertEquals(0, $DB->count_records('events_handlers', array('component'=>'unittest')), 'All handlers should be uninstalled: %s');
74 /**
75 * Tests the update of event handlers from file
76 * @return void
78 public function test_events_update_definition__update() {
79 global $DB;
80 // first modify directly existing handler
81 $handler = $DB->get_record('events_handlers', array('component'=>'unittest', 'eventname'=>'test_instant'));
83 $original = $handler->handlerfunction;
85 // change handler in db
86 $DB->set_field('events_handlers', 'handlerfunction', serialize('some_other_function_handler'), array('id'=>$handler->id));
88 // update the definition, it should revert the handler back
89 events_update_definition('unittest');
90 $handler = $DB->get_record('events_handlers', array('component'=>'unittest', 'eventname'=>'test_instant'));
91 $this->assertEquals($handler->handlerfunction, $original, 'update should sync db with file definition: %s');
94 /**
95 * tests events_trigger_is_registered funtion()
96 * @return void
98 public function test_events_is_registered() {
99 $this->assertTrue(events_is_registered('test_instant', 'unittest'));
103 * tests events_trigger funtion()
104 * @return void
106 public function test_events_trigger__instant() {
107 $this->assertEquals(0, events_trigger('test_instant', 'ok'));
108 $this->assertEquals(0, events_trigger('test_instant', 'ok'));
109 $this->assertEquals(2, eventslib_sample_function_handler('status'));
113 * tests events_trigger funtion()
114 * @return void
116 public function test_events_trigger__cron() {
117 $this->assertEquals(0, events_trigger('test_cron', 'ok'));
118 $this->assertEquals(0, eventslib_sample_handler_class::static_method('status'));
119 events_cron('test_cron');
120 $this->assertEquals(1, eventslib_sample_handler_class::static_method('status'));
124 * tests events_pending_count()
125 * @return void
127 public function test_events_pending_count() {
128 events_trigger('test_cron', 'ok');
129 events_trigger('test_cron', 'ok');
130 events_cron('test_cron');
131 $this->assertEquals(0, events_pending_count('test_cron'), 'all messages should be already dequeued: %s');
135 * tests events_trigger funtion() when instant handler fails
136 * @return void
138 public function test_events_trigger__failed_instant() {
139 $this->assertEquals(1, events_trigger('test_instant', 'fail'), 'fail first event: %s');
140 $this->assertEquals(1, events_trigger('test_instant', 'ok'), 'this one should fail too: %s');
141 $this->assertEquals(0, events_cron('test_instant'), 'all events should stay in queue: %s');
142 $this->assertEquals(2, events_pending_count('test_instant'), 'two events should in queue: %s');
143 $this->assertEquals(0, eventslib_sample_function_handler('status'), 'verify no event dispatched yet: %s');
144 eventslib_sample_function_handler('ignorefail'); //ignore "fail" eventdata from now on
145 $this->assertEquals(1, events_trigger('test_instant', 'ok'), 'this one should go to queue directly: %s');
146 $this->assertEquals(3, events_pending_count('test_instant'), 'three events should in queue: %s');
147 $this->assertEquals(0, eventslib_sample_function_handler('status'), 'verify previous event was not dispatched: %s');
148 $this->assertEquals(3, events_cron('test_instant'), 'all events should be dispatched: %s');
149 $this->assertEquals(3, eventslib_sample_function_handler('status'), 'verify three events were dispatched: %s');
150 $this->assertEquals(0, events_pending_count('test_instant'), 'no events should in queue: %s');
151 $this->assertEquals(0, events_trigger('test_instant', 'ok'), 'this event should be dispatched immediately: %s');
152 $this->assertEquals(4, eventslib_sample_function_handler('status'), 'verify event was dispatched: %s');
153 $this->assertEquals(0, events_pending_count('test_instant'), 'no events should in queue: %s');
158 // test handler function
159 function eventslib_sample_function_handler($eventdata) {
160 static $called = 0;
161 static $ignorefail = false;
163 if ($eventdata == 'status') {
164 return $called;
166 } else if ($eventdata == 'reset') {
167 $called = 0;
168 $ignorefail = false;
169 return;
171 } else if ($eventdata == 'fail') {
172 if ($ignorefail) {
173 $called++;
174 return true;
175 } else {
176 return false;
179 } else if ($eventdata == 'ignorefail') {
180 $ignorefail = true;
181 return;
183 } else if ($eventdata == 'ok') {
184 $called++;
185 return true;
188 print_error('invalideventdata', '', '', $eventdata);
192 // test handler class with static method
193 class eventslib_sample_handler_class {
194 static function static_method($eventdata) {
195 static $called = 0;
196 static $ignorefail = false;
198 if ($eventdata == 'status') {
199 return $called;
201 } else if ($eventdata == 'reset') {
202 $called = 0;
203 $ignorefail = false;
204 return;
206 } else if ($eventdata == 'fail') {
207 if ($ignorefail) {
208 $called++;
209 return true;
210 } else {
211 return false;
214 } else if ($eventdata == 'ignorefail') {
215 $ignorefail = true;
216 return;
218 } else if ($eventdata == 'ok') {
219 $called++;
220 return true;
223 print_error('invalideventdata', '', '', $eventdata);