Search moodle.org's
Developer Documentation

  • Bug fixes for general core bugs in 3.7.x will end 11 May 2020 (12 months).
  • Bug fixes for security issues in 3.7.x will end 9 November 2020 (18 months) - Support has ended.
  • minimum PHP 7.1.0 Note: minimum PHP version has increased since Moodle 3.6. PHP 7.2.x and 7.3.x are supported too. PHP 7.x could have some engine limitations.
  • Differences Between: [Versions 35 and 37] [Versions 37 and 310] [Versions 37 and 311] [Versions 37 and 38] [Versions 37 and 39]

       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 = 'global';
     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  /**
     261   * A test event.
     262   *
     263   * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
     264   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
     265   */
     266  class event_mapper_test_event implements event_interface {
     267      /**
     268       * @var proxy_interface $categoryproxy Category proxy.
     269       */
     270      protected $categoryproxy;
     271  
     272      /**
     273       * @var proxy_interface $courseproxy Course proxy.
     274       */
     275      protected $courseproxy;
     276  
     277      /**
     278       * @var proxy_interface $cmproxy Course module proxy.
     279       */
     280      protected $cmproxy;
     281  
     282      /**
     283       * @var proxy_interface $groupproxy Group proxy.
     284       */
     285      protected $groupproxy;
     286  
     287      /**
     288       * @var proxy_interface $userproxy User proxy.
     289       */
     290      protected $userproxy;
     291  
     292      /**
     293       * @var proxy_interface $subscriptionproxy Subscription proxy.
     294       */
     295      protected $subscriptionproxy;
     296  
     297      /**
     298       * Constructor.
     299       *
     300       * @param calendar_event $legacyevent Legacy event to extract IDs etc from.
     301       */
     302      public function __construct($legacyevent = null) {
     303          if ($legacyevent) {
     304              $this->courseproxy = new event_mapper_test_proxy($legacyevent->courseid);
     305              $this->cmproxy = new event_mapper_test_proxy(1729,
     306                      [
     307                          'modname' => $legacyevent->modname,
     308                          'instance' => $legacyevent->instance
     309                      ]
     310              );
     311              $this->groupproxy = new event_mapper_test_proxy(0);
     312              $this->userproxy = new event_mapper_test_proxy($legacyevent->userid);
     313              $this->subscriptionproxy = new event_mapper_test_proxy(null);
     314          }
     315      }
     316  
     317      public function get_id() {
     318          return 1729;
     319      }
     320  
     321      public function get_name() {
     322          return 'Jeff';
     323      }
     324  
     325      public function get_description() {
     326          return new event_description('asdf', 1);
     327      }
     328  
     329      public function get_location() {
     330          return 'Cube office';
     331      }
     332  
     333      public function get_category() {
     334          return $this->categoryproxy;
     335      }
     336  
     337      public function get_course() {
     338          return $this->courseproxy;
     339      }
     340  
     341      public function get_course_module() {
     342          return $this->cmproxy;
     343      }
     344  
     345      public function get_group() {
     346          return $this->groupproxy;
     347      }
     348  
     349      public function get_user() {
     350          return $this->userproxy;
     351      }
     352  
     353      public function get_type() {
     354          return 'asdf';
     355      }
     356  
     357      public function get_times() {
     358          return new event_times(
     359              (new \DateTimeImmutable())->setTimestamp(-386380800),
     360              (new \DateTimeImmutable())->setTimestamp(115776000),
     361              (new \DateTimeImmutable())->setTimestamp(115776000),
     362              (new \DateTimeImmutable())->setTimestamp(time())
     363          );
     364      }
     365  
     366      public function get_repeats() {
     367          return new core_calendar_event_mapper_test_event_collection();
     368      }
     369  
     370      public function get_subscription() {
     371          return $this->subscriptionproxy;
     372      }
     373  
     374      public function is_visible() {
     375          return true;
     376      }
     377  }
     378  
     379  /**
     380   * A test proxy.
     381   *
     382   * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
     383   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
     384   */
     385  class event_mapper_test_proxy implements proxy_interface {
     386      /**
     387       * @var int $id Proxied ID.
     388       */
     389      protected $id;
     390  
     391      /**
     392       * @var array $params Params to proxy.
     393       */
     394      protected $params;
     395  
     396      /**
     397       * Constructor.
     398       *
     399       * @param int   $id Proxied ID.
     400       * @param array $params Params to proxy.
     401       */
     402      public function __construct($id, $params = []) {
     403          $this->params = $params;
     404      }
     405  
     406      public function get($member) {
     407          if ($member === 'id') {
     408              return $this->id;
     409          }
     410          return isset($params[$member]) ? $params[$member] : null;
     411      }
     412  
     413      public function get_proxied_instance() {
     414      }
     415  }
     416  
     417  /**
     418   * A test event.
     419   *
     420   * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
     421   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
     422   */
     423  class core_calendar_event_mapper_test_event_collection implements event_collection_interface {
     424      /**
     425       * @var array $events Array of events.
     426       */
     427      protected $events;
     428  
     429      /**
     430       * Constructor.
     431       */
     432      public function __construct() {
     433          $this->events = [
     434              'not really an event hahaha',
     435              'also not really. gottem.'
     436          ];
     437      }
     438  
     439      public function get_id() {
     440          return 1729;
     441      }
     442  
     443      public function get_num() {
     444          return 2;
     445      }
     446  
     447      public function getIterator() {
     448          foreach ($this->events as $event) {
     449              yield $event;
     450          }
     451      }
     452  }