MDL-26502 check browser - fix for Symbian (backported)
[moodle.git] / lib / simpletest / testeventslib.php
blob4934a25a66d93bcede63e2edb37ea3dab53413b0
1 <?php
3 /* $Id$ */
5 if (!defined('MOODLE_INTERNAL')) {
6 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
9 // test handler function
10 function sample_function_handler($eventdata) {
11 static $called = 0;
12 static $ignorefail = false;
14 if ($eventdata == 'status') {
15 return $called;
17 } else if ($eventdata == 'reset') {
18 $called = 0;
19 $ignorefail = false;
20 return;
22 } else if ($eventdata == 'fail') {
23 if ($ignorefail) {
24 $called++;
25 return true;
26 } else {
27 return false;
30 } else if ($eventdata == 'ignorefail') {
31 $ignorefail = true;
32 return;
34 } else if ($eventdata == 'ok') {
35 $called++;
36 return true;
39 error('Incorrect eventadata submitted: '.$eventdata);
42 // test handler class with static method
43 class sample_handler_class {
44 function static_method($eventdata) {
45 static $called = 0;
46 static $ignorefail = false;
48 if ($eventdata == 'status') {
49 return $called;
51 } else if ($eventdata == 'reset') {
52 $called = 0;
53 $ignorefail = false;
54 return;
56 } else if ($eventdata == 'fail') {
57 if ($ignorefail) {
58 $called++;
59 return true;
60 } else {
61 return false;
64 } else if ($eventdata == 'ignorefail') {
65 $ignorefail = true;
66 return;
68 } else if ($eventdata == 'ok') {
69 $called++;
70 return true;
73 error('Incorrect eventadata submitted: '.$eventdata);
77 class eventslib_test extends UnitTestCase {
79 /**
80 * Create temporary entries in the database for these tests.
81 * These tests have to work no matter the data currently in the database
82 * (meaning they should run on a brand new site). This means several items of
83 * data have to be artificially inseminated (:-) in the DB.
85 function setUp() {
86 events_uninstall('unittest');
87 sample_function_handler('reset');
88 sample_handler_class::static_method('reset');
89 events_update_definition('unittest');
92 /**
93 * Delete temporary entries from the database
95 function tearDown() {
96 events_uninstall('unittest');
99 /**
100 * Tests the installation of event handlers from file
102 function test__events_update_definition__install() {
103 global $CFG;
105 $dbcount = count_records('events_handlers', 'handlermodule', 'unittest');
106 $handlers = array();
107 require($CFG->libdir.'/simpletest/fixtures/events.php');
108 $filecount = count($handlers);
109 $this->assertEqual($dbcount, $filecount, 'Equal number of handlers in file and db: %s');
113 * Tests the uninstallation of event handlers from file
115 function test__events_update_definition__uninstall() {
116 events_uninstall('unittest');
117 $this->assertEqual(0, count_records('events_handlers', 'handlermodule', 'unittest'), 'All handlers should be uninstalled: %s');
121 * Tests the update of event handlers from file
123 function test__events_update_definition__update() {
124 // first modify directly existing handler
125 $handler = get_record('events_handlers', 'handlermodule', 'unittest', 'eventname', 'test_instant');
127 $original = $handler->handlerfunction;
129 // change handler in db
130 set_field('events_handlers', 'handlerfunction', serialize('some_other_function_handler'), 'id', $handler->id);
132 // update the definition, it should revert the handler back
133 events_update_definition('unittest');
134 $handler = get_record('events_handlers', 'handlermodule', 'unittest', 'eventname', 'test_instant');
135 $this->assertEqual($handler->handlerfunction, $original, 'update should sync db with file definition: %s');
139 * tests events_trigger_is_registered funtion()
141 function test__events_is_registered() {
142 $this->assertTrue(events_is_registered('test_instant', 'unittest'));
146 * tests events_trigger funtion()
148 function test__events_trigger__instant() {
149 $this->assertEqual(0, events_trigger('test_instant', 'ok'));
150 $this->assertEqual(0, events_trigger('test_instant', 'ok'));
151 $this->assertEqual(2, sample_function_handler('status'));
155 * tests events_trigger funtion()
157 function test__events_trigger__cron() {
158 $this->assertEqual(0, events_trigger('test_cron', 'ok'));
159 $this->assertEqual(0, sample_handler_class::static_method('status'));
160 events_cron();
161 $this->assertEqual(1, sample_handler_class::static_method('status'));
165 * tests events_pending_count()
167 function test__events_pending_count() {
168 events_trigger('test_cron', 'ok');
169 events_trigger('test_cron', 'ok');
170 $this->assertEqual(2, events_pending_count('test_cron'), 'two events should in queue: %s');
171 events_cron('test_cron');
172 $this->assertEqual(0, events_pending_count('test_cron'), 'all messages should be already dequeued: %s');
176 * tests events_trigger funtion() when instant handler fails
178 function test__events_trigger__failed_instant() {
179 $this->assertEqual(1, events_trigger('test_instant', 'fail'), 'fail first event: %s');
180 $this->assertEqual(1, events_trigger('test_instant', 'ok'), 'this one should fail too: %s');
181 $this->assertEqual(0, events_cron('test_instant'), 'all events should stay in queue: %s');
182 $this->assertEqual(2, events_pending_count('test_instant'), 'two events should in queue: %s');
183 $this->assertEqual(0, sample_function_handler('status'), 'verify no event dispatched yet: %s');
184 sample_function_handler('ignorefail'); //ignore "fail" eventdata from now on
185 $this->assertEqual(1, events_trigger('test_instant', 'ok'), 'this one should go to queue directly: %s');
186 $this->assertEqual(3, events_pending_count('test_instant'), 'three events should in queue: %s');
187 $this->assertEqual(0, sample_function_handler('status'), 'verify previous event was not dispatched: %s');
188 $this->assertEqual(3, events_cron('test_instant'), 'all events should be dispatched: %s');
189 $this->assertEqual(3, sample_function_handler('status'), 'verify three events were dispatched: %s');
190 $this->assertEqual(0, events_pending_count('test_instant'), 'no events should in queue: %s');
191 $this->assertEqual(0, events_trigger('test_instant', 'ok'), 'this event should be dispatched immediately: %s');
192 $this->assertEqual(4, sample_function_handler('status'), 'verify event was dispatched: %s');
193 $this->assertEqual(0, events_pending_count('test_instant'), 'no events should in queue: %s');