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 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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   * Event mapper test.
  19   *
  20   * @package    core_calendar
  21   * @copyright  2017 Cameron Ball <cameron@cameron1729.xyz>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once($CFG->dirroot . '/calendar/lib.php');
  29  
  30  use core_calendar\local\event\mappers\event_mapper;
  31  use core_calendar\local\event\value_objects\action;
  32  use core_calendar\local\event\value_objects\event_description;
  33  use core_calendar\local\event\value_objects\event_times;
  34  use core_calendar\local\event\factories\action_factory_interface;
  35  use core_calendar\local\event\entities\event_collection_interface;
  36  use core_calendar\local\event\factories\event_factory_interface;
  37  use core_calendar\local\event\entities\event_interface;
  38  use core_calendar\local\event\entities\action_event_interface;
  39  use core_calendar\local\event\proxies\proxy_interface;
  40  
  41  /**
  42   * Event mapper testcase.
  43   *
  44   * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
  45   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  46   */
  47  class core_calendar_event_mapper_testcase extends advanced_testcase {
  48      /**
  49       * Test legacy event -> event.
  50       */
  51      public function test_from_legacy_event_to_event() {
  52          $this->resetAfterTest(true);
  53          $this->setAdminUser();
  54          $legacyevent = $this->create_event();
  55          $mapper = new event_mapper(
  56              new event_mapper_test_event_factory()
  57          );
  58          $event = $mapper->from_legacy_event_to_event($legacyevent);
  59          $this->assertInstanceOf(event_interface::class, $event);
  60      }
  61  
  62      /**
  63       * Test event -> legacy event.
  64       */
  65      public function test_from_event_to_legacy_event() {
  66          $this->resetAfterTest(true);
  67          $this->setAdminUser();
  68          $legacyevent = $this->create_event(['modname' => 'assign', 'instance' => 1]);
  69          $event = new event_mapper_test_event($legacyevent);
  70          $mapper = new event_mapper(
  71              new event_mapper_test_event_factory()
  72          );
  73          $legacyevent = $mapper->from_event_to_legacy_event($event);
  74          $this->assertInstanceOf(calendar_event::class, $legacyevent);
  75      }
  76  
  77      /**
  78       * Test event -> stdClass.
  79       */
  80      public function test_from_event_to_stdclass() {
  81          $this->resetAfterTest(true);
  82          $this->setAdminUser();
  83          $legacyevent = $this->create_event(['modname' => 'assign', 'instance' => 1]);
  84          $event = new event_mapper_test_event($legacyevent);
  85          $mapper = new event_mapper(
  86              new event_mapper_test_event_factory()
  87          );
  88          $obj = $mapper->from_event_to_stdclass($event);
  89          $this->assertInstanceOf(\stdClass::class, $obj);
  90          $this->assertEquals($obj->name, $event->get_name());
  91          $this->assertEquals($obj->eventtype, $event->get_type());
  92          $this->assertEquals($obj->timestart, $event->get_times()->get_start_time()->getTimestamp());
  93      }
  94  
  95      /**
  96       * Test event -> array.
  97       */
  98      public function test_from_event_to_assoc_array() {
  99          $this->resetAfterTest(true);
 100          $this->setAdminUser();
 101          $legacyevent = $this->create_event(['modname' => 'assign', 'instance' => 1]);
 102          $event = new event_mapper_test_event($legacyevent);
 103          $mapper = new event_mapper(
 104              new event_mapper_test_event_factory()
 105          );
 106          $arr = $mapper->from_event_to_assoc_array($event);
 107          $this->assertTrue(is_array($arr));
 108          $this->assertEquals($arr['name'], $event->get_name());
 109          $this->assertEquals($arr['eventtype'], $event->get_type());
 110          $this->assertEquals($arr['timestart'], $event->get_times()->get_start_time()->getTimestamp());
 111      }
 112  
 113      /**
 114       * Test for action event -> legacy event.
 115       */
 116      public function test_from_action_event_to_legacy_event() {
 117          $this->resetAfterTest(true);
 118          $this->setAdminUser();
 119          $legacyevent = $this->create_event(['modname' => 'assign', 'instance' => 1]);
 120          $event = new event_mapper_test_action_event(
 121              new event_mapper_test_event($legacyevent)
 122          );
 123          $mapper = new event_mapper(
 124              new event_mapper_test_event_factory()
 125          );
 126          $legacyevent = $mapper->from_event_to_legacy_event($event);
 127  
 128          $this->assertInstanceOf(calendar_event::class, $legacyevent);
 129          $this->assertEquals($legacyevent->actionname, 'test action');
 130          $this->assertInstanceOf(\moodle_url::class, $legacyevent->actionurl);
 131          $this->assertEquals($legacyevent->actionnum, 1729);
 132          $this->assertEquals($legacyevent->actionactionable, $event->get_action()->is_actionable());
 133      }
 134  
 135      /**
 136       * Helper function to create calendar events using the old code.
 137       *
 138       * @param array $properties A list of calendar event properties to set
 139       * @return calendar_event
 140       */
 141      protected function create_event($properties = []) {
 142          $record = new \stdClass();
 143          $record->name = 'event name';
 144          $record->eventtype = 'site';
 145          $record->timestart = time();
 146          $record->timeduration = 0;
 147          $record->timesort = 0;
 148          $record->type = 1;
 149          $record->courseid = 0;
 150          $record->categoryid = 0;
 151  
 152          foreach ($properties as $name => $value) {
 153              $record->$name = $value;
 154          }
 155  
 156          $event = new calendar_event($record);
 157          return $event->create($record, false);
 158      }
 159  }
 160  
 161  /**
 162   * A test event factory.
 163   *
 164   * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
 165   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 166   */
 167  class event_mapper_test_event_factory implements event_factory_interface {
 168  
 169      public function create_instance(\stdClass $dbrow) {
 170          return new event_mapper_test_event();
 171      }
 172  }
 173  
 174  /**
 175   * A test action event
 176   *
 177   * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
 178   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 179   */
 180  class event_mapper_test_action_event implements action_event_interface {
 181      /**
 182       * @var event_interface $event The event to delegate to.
 183       */
 184      protected $event;
 185  
 186      /**
 187       * event_mapper_test_action_event constructor.
 188       * @param event_interface $event
 189       */
 190      public function __construct(event_interface $event) {
 191          $this->event = $event;
 192      }
 193  
 194      public function get_id() {
 195          return $this->event->get_id();
 196      }
 197  
 198      public function get_name() {
 199          return $this->event->get_name();
 200      }
 201  
 202      public function get_description() {
 203          return $this->event->get_description();
 204      }
 205  
 206      public function get_location() {
 207          return $this->event->get_location();
 208      }
 209  
 210      public function get_category() {
 211          return $this->event->get_category();
 212      }
 213  
 214      public function get_course() {
 215          return $this->event->get_course();
 216      }
 217  
 218      public function get_course_module() {
 219          return $this->event->get_course_module();
 220      }
 221  
 222      public function get_group() {
 223          return $this->event->get_group();
 224      }
 225  
 226      public function get_user() {
 227          return $this->event->get_user();
 228      }
 229  
 230      public function get_type() {
 231          return $this->event->get_type();
 232      }
 233  
 234      public function get_times() {
 235          return $this->event->get_times();
 236      }
 237  
 238      public function get_repeats() {
 239          return $this->event->get_repeats();
 240      }
 241  
 242      public function get_subscription() {
 243          return $this->event->get_subscription();
 244      }
 245  
 246      public function is_visible() {
 247          return $this->event->is_visible();
 248      }
 249  
 250      public function get_action() {
 251          return new action(
 252              'test action',
 253              new \moodle_url('http://example.com'),
 254              1729,
 255              true
 256          );
 257      }
 258  
 259      /**
 260       * Component
 261       * @return string|null
 262       */
 263      public function get_component() {
 264          return $this->event->get_component();
 265      }
 266  }
 267  
 268  /**
 269   * A test event.
 270   *
 271   * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
 272   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 273   */
 274  class event_mapper_test_event implements event_interface {
 275      /**
 276       * @var proxy_interface $categoryproxy Category proxy.
 277       */
 278      protected $categoryproxy;
 279  
 280      /**
 281       * @var proxy_interface $courseproxy Course proxy.
 282       */
 283      protected $courseproxy;
 284  
 285      /**
 286       * @var proxy_interface $cmproxy Course module proxy.
 287       */
 288      protected $cmproxy;
 289  
 290      /**
 291       * @var proxy_interface $groupproxy Group proxy.
 292       */
 293      protected $groupproxy;
 294  
 295      /**
 296       * @var proxy_interface $userproxy User proxy.
 297       */
 298      protected $userproxy;
 299  
 300      /**
 301       * @var proxy_interface $subscriptionproxy Subscription proxy.
 302       */
 303      protected $subscriptionproxy;
 304  
 305      /**
 306       * Constructor.
 307       *
 308       * @param calendar_event $legacyevent Legacy event to extract IDs etc from.
 309       */
 310      public function __construct($legacyevent = null) {
 311          if ($legacyevent) {
 312              $this->courseproxy = new event_mapper_test_proxy($legacyevent->courseid);
 313              $this->cmproxy = new event_mapper_test_proxy(1729,
 314                      [
 315                          'modname' => $legacyevent->modname,
 316                          'instance' => $legacyevent->instance
 317                      ]
 318              );
 319              $this->groupproxy = new event_mapper_test_proxy(0);
 320              $this->userproxy = new event_mapper_test_proxy($legacyevent->userid);
 321              $this->subscriptionproxy = new event_mapper_test_proxy(null);
 322          }
 323      }
 324  
 325      public function get_id() {
 326          return 1729;
 327      }
 328  
 329      public function get_name() {
 330          return 'Jeff';
 331      }
 332  
 333      public function get_description() {
 334          return new event_description('asdf', 1);
 335      }
 336  
 337      public function get_location() {
 338          return 'Cube office';
 339      }
 340  
 341      public function get_category() {
 342          return $this->categoryproxy;
 343      }
 344  
 345      public function get_course() {
 346          return $this->courseproxy;
 347      }
 348  
 349      public function get_course_module() {
 350          return $this->cmproxy;
 351      }
 352  
 353      public function get_group() {
 354          return $this->groupproxy;
 355      }
 356  
 357      public function get_user() {
 358          return $this->userproxy;
 359      }
 360  
 361      public function get_type() {
 362          return 'asdf';
 363      }
 364  
 365      public function get_times() {
 366          return new event_times(
 367              (new \DateTimeImmutable())->setTimestamp(-386380800),
 368              (new \DateTimeImmutable())->setTimestamp(115776000),
 369              (new \DateTimeImmutable())->setTimestamp(115776000),
 370              (new \DateTimeImmutable())->setTimestamp(time()),
 371              (new \DateTimeImmutable())->setTimestamp(115776000)
 372          );
 373      }
 374  
 375      public function get_repeats() {
 376          return new core_calendar_event_mapper_test_event_collection();
 377      }
 378  
 379      public function get_subscription() {
 380          return $this->subscriptionproxy;
 381      }
 382  
 383      public function is_visible() {
 384          return true;
 385      }
 386  
 387      /**
 388       * Component
 389       * @return string|null
 390       */
 391      public function get_component() {
 392          return null;
 393      }
 394  }
 395  
 396  /**
 397   * A test proxy.
 398   *
 399   * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
 400   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 401   */
 402  class event_mapper_test_proxy implements proxy_interface {
 403      /**
 404       * @var int $id Proxied ID.
 405       */
 406      protected $id;
 407  
 408      /**
 409       * @var array $params Params to proxy.
 410       */
 411      protected $params;
 412  
 413      /**
 414       * Constructor.
 415       *
 416       * @param int   $id Proxied ID.
 417       * @param array $params Params to proxy.
 418       */
 419      public function __construct($id, $params = []) {
 420          $this->params = $params;
 421      }
 422  
 423      public function get($member) {
 424          if ($member === 'id') {
 425              return $this->id;
 426          }
 427          return isset($params[$member]) ? $params[$member] : null;
 428      }
 429  
 430      public function get_proxied_instance() {
 431      }
 432  }
 433  
 434  /**
 435   * A test event.
 436   *
 437   * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
 438   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 439   */
 440  class core_calendar_event_mapper_test_event_collection implements event_collection_interface {
 441      /**
 442       * @var array $events Array of events.
 443       */
 444      protected $events;
 445  
 446      /**
 447       * Constructor.
 448       */
 449      public function __construct() {
 450          $this->events = [
 451              'not really an event hahaha',
 452              'also not really. gottem.'
 453          ];
 454      }
 455  
 456      public function get_id() {
 457          return 1729;
 458      }
 459  
 460      public function get_num() {
 461          return 2;
 462      }
 463  
 464      public function getIterator() {
 465          foreach ($this->events as $event) {
 466              yield $event;
 467          }
 468      }
 469  }