[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/tests/ -> eventslib_test.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1