Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/simpletest/testeventslib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00029 defined('MOODLE_INTERNAL') || die();
00030 
00031 // test handler function
00032 function sample_function_handler($eventdata) {
00033     static $called = 0;
00034     static $ignorefail = false;
00035 
00036     if ($eventdata == 'status') {
00037         return $called;
00038 
00039     } else if ($eventdata == 'reset') {
00040         $called = 0;
00041         $ignorefail = false;
00042         return;
00043 
00044     } else if ($eventdata == 'fail') {
00045         if ($ignorefail) {
00046             $called++;
00047             return true;
00048         } else {
00049             return false;
00050         }
00051 
00052     } else if ($eventdata == 'ignorefail') {
00053         $ignorefail = true;
00054         return;
00055 
00056     } else if ($eventdata == 'ok') {
00057         $called++;
00058         return true;
00059     }
00060 
00061     print_error('invalideventdata', '', '', $eventdata);
00062 }
00063 
00064 // test handler class with static method
00065 class sample_handler_class {
00066     function static_method($eventdata) {
00067         static $called = 0;
00068         static $ignorefail = false;
00069 
00070         if ($eventdata == 'status') {
00071             return $called;
00072 
00073         } else if ($eventdata == 'reset') {
00074             $called = 0;
00075             $ignorefail = false;
00076             return;
00077 
00078         } else if ($eventdata == 'fail') {
00079             if ($ignorefail) {
00080                 $called++;
00081                 return true;
00082             } else {
00083                 return false;
00084             }
00085 
00086         } else if ($eventdata == 'ignorefail') {
00087             $ignorefail = true;
00088             return;
00089 
00090         } else if ($eventdata == 'ok') {
00091             $called++;
00092             return true;
00093         }
00094 
00095         print_error('invalideventdata', '', '', $eventdata);
00096     }
00097 }
00098 
00099 class eventslib_test extends UnitTestCase {
00100 
00101     public  static $includecoverage = array('lib/eventslib.php');
00102 
00109     function setUp() {
00110         // Set global category settings to -1 (not force)
00111 
00112         events_uninstall('unittest');
00113         sample_function_handler('reset');
00114         sample_handler_class::static_method('reset');
00115         events_update_definition('unittest');
00116     }
00117 
00121     function tearDown() {
00122        events_uninstall('unittest');
00123     }
00124 
00128     function test__events_update_definition__install() {
00129         global $CFG, $DB;
00130 
00131         $dbcount = $DB->count_records('events_handlers', array('component'=>'unittest'));
00132         $handlers = array();
00133         require($CFG->libdir.'/simpletest/fixtures/events.php');
00134         $filecount = count($handlers);
00135         $this->assertEqual($dbcount, $filecount, 'Equal number of handlers in file and db: %s');
00136     }
00137 
00141     function test__events_update_definition__uninstall() {
00142         global $DB;
00143 
00144         events_uninstall('unittest');
00145         $this->assertEqual(0, $DB->count_records('events_handlers', array('component'=>'unittest')), 'All handlers should be uninstalled: %s');
00146     }
00147 
00151     function test__events_update_definition__update() {
00152         global $DB;
00153         // first modify directly existing handler
00154         $handler = $DB->get_record('events_handlers', array('component'=>'unittest', 'eventname'=>'test_instant'));
00155 
00156         $original = $handler->handlerfunction;
00157 
00158         // change handler in db
00159         $DB->set_field('events_handlers', 'handlerfunction', serialize('some_other_function_handler'), array('id'=>$handler->id));
00160 
00161         // update the definition, it should revert the handler back
00162         events_update_definition('unittest');
00163         $handler = $DB->get_record('events_handlers', array('component'=>'unittest', 'eventname'=>'test_instant'));
00164         $this->assertEqual($handler->handlerfunction, $original, 'update should sync db with file definition: %s');
00165     }
00166 
00170     function test__events_is_registered() {
00171         $this->assertTrue(events_is_registered('test_instant', 'unittest'));
00172     }
00173 
00177     function test__events_trigger__instant() {
00178         $this->assertEqual(0, events_trigger('test_instant', 'ok'));
00179         $this->assertEqual(0, events_trigger('test_instant', 'ok'));
00180         $this->assertEqual(2, sample_function_handler('status'));
00181     }
00182 
00186     function test__events_trigger__cron() {
00187         $this->assertEqual(0, events_trigger('test_cron', 'ok'));
00188         $this->assertEqual(0, sample_handler_class::static_method('status'));
00189         events_cron('test_cron');
00190         $this->assertEqual(1, sample_handler_class::static_method('status'));
00191     }
00192 
00196     function test__events_pending_count() {
00197         events_trigger('test_cron', 'ok');
00198         events_trigger('test_cron', 'ok');
00199         events_cron('test_cron');
00200         $this->assertEqual(0, events_pending_count('test_cron'), 'all messages should be already dequeued: %s');
00201     }
00202 
00206     function test__events_trigger__failed_instant() {
00207         $this->assertEqual(1, events_trigger('test_instant', 'fail'), 'fail first event: %s');
00208         $this->assertEqual(1, events_trigger('test_instant', 'ok'), 'this one should fail too: %s');
00209         $this->assertEqual(0, events_cron('test_instant'), 'all events should stay in queue: %s');
00210         $this->assertEqual(2, events_pending_count('test_instant'), 'two events should in queue: %s');
00211         $this->assertEqual(0, sample_function_handler('status'), 'verify no event dispatched yet: %s');
00212         sample_function_handler('ignorefail'); //ignore "fail" eventdata from now on
00213         $this->assertEqual(1, events_trigger('test_instant', 'ok'), 'this one should go to queue directly: %s');
00214         $this->assertEqual(3, events_pending_count('test_instant'), 'three events should in queue: %s');
00215         $this->assertEqual(0, sample_function_handler('status'), 'verify previous event was not dispatched: %s');
00216         $this->assertEqual(3, events_cron('test_instant'), 'all events should be dispatched: %s');
00217         $this->assertEqual(3, sample_function_handler('status'), 'verify three events were dispatched: %s');
00218         $this->assertEqual(0, events_pending_count('test_instant'), 'no events should in queue: %s');
00219         $this->assertEqual(0, events_trigger('test_instant', 'ok'), 'this event should be dispatched immediately: %s');
00220         $this->assertEqual(4, sample_function_handler('status'), 'verify event was dispatched: %s');
00221         $this->assertEqual(0, events_pending_count('test_instant'), 'no events should in queue: %s');
00222     }
00223 }
00224 
00225 
 All Data Structures Namespaces Files Functions Variables Enumerations