Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 402] [Versions 310 and 403]

   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  namespace core_tests\event;
  18  
  19  /**
  20   * Fixtures for new event testing.
  21   *
  22   * @package    core
  23   * @category   phpunit
  24   * @copyright  2013 Petr Skoda {@link http://skodak.org}
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  
  31  class unittest_executed extends \core\event\base {
  32      public $nest = false;
  33  
  34      public static function get_name() {
  35          return 'xxx';
  36      }
  37  
  38      public function get_description() {
  39          return 'yyy';
  40      }
  41  
  42      protected function init() {
  43          $this->data['crud'] = 'u';
  44          $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
  45      }
  46  
  47      public function get_url() {
  48          return new \moodle_url('/somepath/somefile.php', array('id'=>$this->data['other']['sample']));
  49      }
  50  
  51      public static function get_legacy_eventname() {
  52          return 'test_legacy';
  53      }
  54  
  55      protected function get_legacy_eventdata() {
  56          return array($this->data['courseid'], $this->data['other']['sample']);
  57      }
  58  
  59      protected function get_legacy_logdata() {
  60          return array($this->data['courseid'], 'core_unittest', 'view', 'unittest.php?id='.$this->data['other']['sample']);
  61      }
  62  }
  63  
  64  
  65  class unittest_observer {
  66      public static $info = array();
  67      public static $event = array();
  68  
  69      public static function reset() {
  70          self::$info = array();
  71          self::$event = array();
  72      }
  73  
  74      public static function observe_one(unittest_executed $event) {
  75          self::$info[] = 'observe_one-'.$event->other['sample'];
  76          self::$event[] = $event;
  77      }
  78  
  79      public static function external_observer(unittest_executed $event) {
  80          self::$info[] = 'external_observer-'.$event->other['sample'];
  81          self::$event[] = $event;
  82      }
  83  
  84      public static function broken_observer(unittest_executed $event) {
  85          self::$info[] = 'broken_observer-'.$event->other['sample'];
  86          self::$event[] = $event;
  87          throw new \Exception('someerror');
  88      }
  89  
  90      public static function observe_all(\core\event\base $event) {
  91          if (!($event instanceof unittest_executed)) {
  92              self::$info[] = 'observe_all-unknown';
  93              self::$event[] = $event;
  94              return;
  95          }
  96          self::$event[] = $event;
  97          if (!empty($event->nest)) {
  98              self::$info[] = 'observe_all-nesting-'.$event->other['sample'];
  99              unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>666, 'xx'=>666)))->trigger();
 100          } else {
 101              self::$info[] = 'observe_all-'.$event->other['sample'];
 102          }
 103      }
 104  
 105      public static function observe_all_alt(\core\event\base $event) {
 106          self::$info[] = 'observe_all_alt';
 107          self::$event[] = $event;
 108      }
 109  
 110      public static function legacy_handler($data) {
 111          self::$info[] = 'legacy_handler-'.$data[0];
 112          self::$event[] = $data;
 113      }
 114  }
 115  
 116  class bad_event1 extends \core\event\base {
 117      protected function init() {
 118          //$this->data['crud'] = 'u';
 119          $this->data['edulevel'] = self::LEVEL_OTHER;
 120      }
 121  }
 122  
 123  class bad_event2 extends \core\event\base {
 124      protected function init() {
 125          $this->data['crud'] = 'u';
 126          //$this->data['edulevel'] = 10;
 127      }
 128  }
 129  
 130  class bad_event2b extends \core\event\base {
 131      protected function init() {
 132          $this->data['crud'] = 'u';
 133          // Invalid level value.
 134          $this->data['edulevel'] = -1;
 135      }
 136  }
 137  
 138  class bad_event3 extends \core\event\base {
 139      protected function init() {
 140          $this->data['crud'] = 'u';
 141          $this->data['edulevel'] = self::LEVEL_OTHER;
 142          unset($this->data['courseid']);
 143      }
 144  }
 145  
 146  class bad_event4 extends \core\event\base {
 147      protected function init() {
 148          $this->data['crud'] = 'u';
 149          $this->data['edulevel'] = self::LEVEL_OTHER;
 150          $this->data['xxx'] = 1;
 151      }
 152  }
 153  
 154  class bad_event5 extends \core\event\base {
 155      protected function init() {
 156          $this->data['crud'] = 'x';
 157          $this->data['edulevel'] = self::LEVEL_OTHER;
 158      }
 159  }
 160  
 161  class bad_event6 extends \core\event\base {
 162      protected function init() {
 163          $this->data['crud'] = 'c';
 164          $this->data['edulevel'] = self::LEVEL_OTHER;
 165          $this->data['objecttable'] = 'xxx_xxx_xx';
 166      }
 167  }
 168  
 169  class bad_event7 extends \core\event\base {
 170      protected function init() {
 171          $this->data['crud'] = 'c';
 172          $this->data['edulevel'] = self::LEVEL_OTHER;
 173          $this->data['objecttable'] = null;
 174      }
 175  }
 176  
 177  class bad_event8 extends \core\event\base {
 178      protected function init() {
 179          $this->data['crud'] = 'c';
 180          $this->data['edulevel'] = self::LEVEL_OTHER;
 181          $this->data['objecttable'] = 'user';
 182      }
 183  }
 184  
 185  class problematic_event1 extends \core\event\base {
 186      protected function init() {
 187          $this->data['crud'] = 'u';
 188          $this->data['edulevel'] = self::LEVEL_OTHER;
 189      }
 190  }
 191  
 192  class problematic_event2 extends \core\event\base {
 193      protected function init() {
 194          $this->data['crud'] = 'c';
 195          $this->data['edulevel'] = self::LEVEL_OTHER;
 196          $this->context = \context_system::instance();
 197      }
 198  }
 199  
 200  class problematic_event3 extends \core\event\base {
 201      protected function init() {
 202          $this->data['crud'] = 'c';
 203          $this->data['edulevel'] = self::LEVEL_OTHER;
 204          $this->context = \context_system::instance();
 205      }
 206  
 207      protected function validate_data() {
 208          parent::validate_data();
 209          if (empty($this->data['other'])) {
 210              debugging('other is missing');
 211          }
 212      }
 213  }
 214  
 215  class deprecated_event1 extends \core\event\base {
 216      protected function init() {
 217          $this->data['crud'] = 'c';
 218          $this->data['level'] = self::LEVEL_TEACHING; // Tests edulevel hint.
 219          $this->context = \context_system::instance();
 220      }
 221  }
 222  
 223  class noname_event extends \core\event\base {
 224  
 225      protected function init() {
 226          $this->data['crud'] = 'c';
 227          $this->data['edulevel'] = self::LEVEL_OTHER;
 228          $this->context = \context_system::instance();
 229      }
 230  }
 231  
 232  /**
 233   * Class course_module_viewed.
 234   *
 235   * Wrapper for testing \core\event\course_module_viewed.
 236   */
 237  class course_module_viewed extends \core\event\course_module_viewed {
 238      protected function init() {
 239          $this->data['crud'] = 'r';
 240          $this->data['edulevel'] = self::LEVEL_OTHER;
 241          $this->data['objecttable'] = 'feedback';
 242      }
 243  }
 244  
 245  /**
 246   * Class course_module_viewed_noinit.
 247   *
 248   * Wrapper for testing \core\event\course_module_viewed.
 249   */
 250  class course_module_viewed_noinit extends \core\event\course_module_viewed {
 251  }
 252  
 253  /**
 254   * Event for testing core\event\grade_report_viewed.
 255   */
 256  class grade_report_viewed extends \core\event\grade_report_viewed {
 257  }
 258  
 259  /**
 260   * Event to test context used in event functions
 261   */
 262  class context_used_in_event extends \core\event\base {
 263      public function get_description() {
 264          return $this->context->instanceid . " Description";
 265      }
 266  
 267      protected function init() {
 268          $this->data['crud'] = 'u';
 269          $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
 270          $this->context = \context_system::instance();
 271      }
 272  
 273      public function get_url() {
 274          return new \moodle_url('/somepath/somefile.php', array('id' => $this->context->instanceid));
 275      }
 276  
 277      protected function get_legacy_eventdata() {
 278          return array($this->data['courseid'], $this->context->instanceid);
 279      }
 280  
 281      protected function get_legacy_logdata() {
 282          return array($this->data['courseid'], 'core_unittest', 'view', 'unittest.php?id=' . $this->context->instanceid);
 283      }
 284  }
 285  
 286  /**
 287   * This is an explanation of the event.
 288   *      - I'm making a point here.
 289   *      - I have a second {@link something}  point here.
 290   *      - whitespace is intentional to test it's removal.
 291   *
 292   *
 293   * I have something else *Yeah* that.
 294   *
 295   *
 296   *
 297   * @package    core
 298   * @category   phpunit
 299   * @copyright  2014 Adrian Greeve <adrian@moodle.com>
 300   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 301   */
 302  class full_docblock extends \core\event\base {
 303  
 304      protected function init() {
 305  
 306      }
 307  }
 308  
 309  /**
 310   * We have only the description in the docblock
 311   * and nothing else.
 312   */
 313  class docblock_test2 extends \core\event\base {
 314  
 315      protected function init() {
 316  
 317      }
 318  }
 319  
 320  /**
 321   * Calendar event created event.
 322   *
 323   * @property-read array $other {
 324   *      Extra information about the event.
 325   *
 326   *      - int timestart: timestamp for event time start.
 327   *      - string name: Name of the event.
 328   *      - int repeatid: Id of the parent event if present, else 0.
 329   * }
 330   *
 331   * @package    core
 332   * @since      Moodle 2.7
 333   * @copyright  2014 onwards Adrian Greeve
 334   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 335   */
 336  class docblock_test3 extends \core\event\base {
 337  
 338      protected function init() {
 339  
 340      }
 341  }
 342  
 343  class static_info_viewing extends \core\event\base {
 344  
 345      protected function init() {
 346          $this->data['crud'] = 'r';
 347          $this->data['edulevel'] = self::LEVEL_OTHER;
 348          $this->data['objecttable'] = 'mod_unittest';
 349      }
 350  }