Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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