Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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  /**
  18   * Repeat event collection tests.
  19   *
  20   * @package    core_calendar
  21   * @copyright  2017 Ryan Wyllie <ryan@moodle.com>
  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  
  29  require_once($CFG->dirroot . '/calendar/lib.php');
  30  
  31  use core_calendar\local\event\entities\event;
  32  use core_calendar\local\event\entities\repeat_event_collection;
  33  use core_calendar\local\event\proxies\coursecat_proxy;
  34  use core_calendar\local\event\proxies\std_proxy;
  35  use core_calendar\local\event\value_objects\event_description;
  36  use core_calendar\local\event\value_objects\event_times;
  37  use core_calendar\local\event\factories\event_factory_interface;
  38  
  39  /**
  40   * Repeat event collection tests.
  41   *
  42   * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
  43   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  44   */
  45  class core_calendar_repeat_event_collection_testcase extends advanced_testcase {
  46      /**
  47       * Test that the collection id is set to the parent id if the repeat id
  48       * is falsey.
  49       */
  50      public function test_parent_id_no_repeat_id() {
  51          $this->resetAfterTest(true);
  52          $dbrow = (object) [
  53              'id' => 123122131,
  54              'repeatid' => null
  55          ];
  56          $factory = new core_calendar_repeat_event_collection_event_test_factory();
  57          $collection = new repeat_event_collection($dbrow, $factory);
  58  
  59          $this->assertEquals($dbrow->id, $collection->get_id());
  60      }
  61  
  62      /**
  63       * Test that the repeat id is set to the parent id if the repeat id
  64       * is not falsey (even if the parent id is provided).
  65       */
  66      public function test_parent_id_and_repeat_id() {
  67          $this->resetAfterTest(true);
  68          $dbrow = (object) [
  69              'id' => 123122131,
  70              'repeatid' => 5647839
  71          ];
  72          $factory = new core_calendar_repeat_event_collection_event_test_factory();
  73          $collection = new repeat_event_collection($dbrow, $factory);
  74  
  75          $this->assertEquals($dbrow->repeatid, $collection->get_id());
  76      }
  77  
  78      /**
  79       * Test that an empty collection is valid.
  80       */
  81      public function test_empty_collection() {
  82          $this->resetAfterTest(true);
  83          $this->setAdminUser();
  84  
  85          $event = $this->create_event([
  86              // This causes the code to set the repeat id on this record
  87              // but not create any repeat event records.
  88              'repeat' => 1,
  89              'repeats' => 0
  90          ]);
  91          $dbrow = (object) [
  92              'id' => $event->id,
  93              'repeatid' => null
  94          ];
  95          $factory = new core_calendar_repeat_event_collection_event_test_factory();
  96  
  97          // Event collection with no repeats.
  98          $collection = new repeat_event_collection($dbrow, $factory);
  99  
 100          $this->assertEquals($event->id, $collection->get_id());
 101          $this->assertEquals(0, $collection->get_num());
 102          $this->assertNull($collection->getIterator()->next());
 103      }
 104  
 105      /**
 106       * Test that a collection with values behaves correctly.
 107       */
 108      public function test_values_collection() {
 109          $this->resetAfterTest(true);
 110          $this->setAdminUser();
 111  
 112          $factory = new core_calendar_repeat_event_collection_event_test_factory();
 113          $event = $this->create_event([
 114              // This causes the code to set the repeat id on this record
 115              // but not create any repeat event records.
 116              'repeat' => 1,
 117              'repeats' => 0
 118          ]);
 119          $parentid = $event->id;
 120          $dbrow = (object) [
 121              'id' => $parentid,
 122              'repeatid' => null
 123          ];
 124          $repeats = [];
 125  
 126          for ($i = 1; $i < 4; $i++) {
 127              $record = $this->create_event([
 128                  'name' => sprintf('repeat %d', $i),
 129                  'repeatid' => $parentid
 130              ]);
 131  
 132              // Index by name so that we don't have to rely on sorting
 133              // when doing the comparison later.
 134              $repeats[$record->name] = $record;
 135          }
 136  
 137          // Event collection with no repeats.
 138          $collection = new repeat_event_collection($dbrow, $factory);
 139  
 140          $this->assertEquals($parentid, $collection->get_id());
 141          $this->assertEquals(count($repeats), $collection->get_num());
 142  
 143          foreach ($collection as $index => $event) {
 144              $name = $event->get_name();
 145              $this->assertEquals($repeats[$name]->name, $name);
 146          }
 147      }
 148  
 149      /**
 150       * Helper function to create calendar events using the old code.
 151       *
 152       * @param array $properties A list of calendar event properties to set
 153       * @return calendar_event
 154       */
 155      protected function create_event($properties = []) {
 156          $record = new \stdClass();
 157          $record->name = 'event name';
 158          $record->eventtype = 'site';
 159          $record->repeat = 0;
 160          $record->repeats = 0;
 161          $record->timestart = time();
 162          $record->timeduration = 0;
 163          $record->timesort = 0;
 164          $record->type = 1;
 165          $record->courseid = 0;
 166          $record->categoryid = 0;
 167  
 168          foreach ($properties as $name => $value) {
 169              $record->$name = $value;
 170          }
 171  
 172          $event = new calendar_event($record);
 173          return $event->create($record, false);
 174      }
 175  }
 176  
 177  /**
 178   * Test event factory.
 179   *
 180   * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
 181   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 182   */
 183  class core_calendar_repeat_event_collection_event_test_factory implements event_factory_interface {
 184  
 185      public function create_instance(\stdClass $dbrow) {
 186          $identity = function($id) {
 187              return $id;
 188          };
 189          return new event(
 190              $dbrow->id,
 191              $dbrow->name,
 192              new event_description($dbrow->description, $dbrow->format),
 193              new coursecat_proxy($dbrow->categoryid),
 194              new std_proxy($dbrow->courseid, $identity),
 195              new std_proxy($dbrow->groupid, $identity),
 196              new std_proxy($dbrow->userid, $identity),
 197              $dbrow->repeatid ? new repeat_event_collection($dbrow, $this) : null,
 198              new std_proxy($dbrow->instance, $identity),
 199              $dbrow->type,
 200              new event_times(
 201                  (new \DateTimeImmutable())->setTimestamp($dbrow->timestart),
 202                  (new \DateTimeImmutable())->setTimestamp($dbrow->timestart + $dbrow->timeduration),
 203                  (new \DateTimeImmutable())->setTimestamp($dbrow->timesort ? $dbrow->timesort : $dbrow->timestart),
 204                  (new \DateTimeImmutable())->setTimestamp($dbrow->timemodified)
 205              ),
 206              !empty($dbrow->visible),
 207              new std_proxy($dbrow->subscriptionid, $identity),
 208              $dbrow->location,
 209              $dbrow->component
 210          );
 211      }
 212  }