[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 /** 18 * Tests events subsystem. 19 * 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 25 */ 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 30 class core_eventslib_testcase extends advanced_testcase { 31 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 */ 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'); 44 45 $this->resetAfterTest(); 46 } 47 48 /** 49 * Tests the installation of event handlers from file 50 */ 51 public function test_events_update_definition__install() { 52 global $DB; 53 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'); 58 } 59 60 /** 61 * Tests the uninstallation of event handlers from file. 62 */ 63 public function test_events_update_definition__uninstall() { 64 global $DB; 65 66 events_uninstall('unittest'); 67 $this->assertEquals(0, $DB->count_records('events_handlers', array('component'=>'unittest')), 'All handlers should be uninstalled: %s'); 68 } 69 70 /** 71 * Tests the update of event handlers from file. 72 */ 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')); 77 78 $original = $handler->handlerfunction; 79 80 // Change handler in db. 81 $DB->set_field('events_handlers', 'handlerfunction', serialize('some_other_function_handler'), array('id'=>$handler->id)); 82 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'); 87 } 88 89 /** 90 * Tests events_trigger_is_registered() function. 91 */ 92 public function test_events_is_registered() { 93 $this->assertTrue(events_is_registered('test_instant', 'unittest')); 94 } 95 96 /** 97 * Tests events_trigger_legacy() function. 98 */ 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')); 103 } 104 105 /** 106 * Tests events_trigger_legacy() function. 107 */ 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')); 113 } 114 115 /** 116 * Tests events_pending_count() function. 117 */ 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'); 123 } 124 125 /** 126 * Tests events_trigger_legacy() function when instant handler fails. 127 */ 128 public function test_events_trigger__failed_instant() { 129 global $CFG; 130 $olddebug = $CFG->debug; 131 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'); 134 135 $this->assertEquals(0, events_cron('test_instant'), 'all events should stay in queue: %s'); 136 $this->assertDebuggingCalled(); 137 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'); 150 } 151 152 /** 153 * Tests events_trigger() function. 154 */ 155 public function test_events_trigger_debugging() { 156 $this->assertEquals(0, events_trigger('test_instant', 'ok')); 157 $this->assertDebuggingCalled(); 158 } 159 } 160 161 /** 162 * Test handler function. 163 */ 164 function eventslib_sample_function_handler($eventdata) { 165 static $called = 0; 166 static $ignorefail = false; 167 168 if ($eventdata == 'status') { 169 return $called; 170 171 } else if ($eventdata == 'reset') { 172 $called = 0; 173 $ignorefail = false; 174 return; 175 176 } else if ($eventdata == 'fail') { 177 if ($ignorefail) { 178 $called++; 179 return true; 180 } else { 181 return false; 182 } 183 184 } else if ($eventdata == 'ignorefail') { 185 $ignorefail = true; 186 return; 187 188 } else if ($eventdata == 'ok') { 189 $called++; 190 return true; 191 } 192 193 print_error('invalideventdata', '', '', $eventdata); 194 } 195 196 197 /** 198 * Test handler class with static method. 199 */ 200 class eventslib_sample_handler_class { 201 public static function static_method($eventdata) { 202 static $called = 0; 203 static $ignorefail = false; 204 205 if ($eventdata == 'status') { 206 return $called; 207 208 } else if ($eventdata == 'reset') { 209 $called = 0; 210 $ignorefail = false; 211 return; 212 213 } else if ($eventdata == 'fail') { 214 if ($ignorefail) { 215 $called++; 216 return true; 217 } else { 218 return false; 219 } 220 221 } else if ($eventdata == 'ignorefail') { 222 $ignorefail = true; 223 return; 224 225 } else if ($eventdata == 'ok') { 226 $called++; 227 return true; 228 } 229 230 print_error('invalideventdata', '', '', $eventdata); 231 } 232 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |