Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   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_calendar;
  18  
  19  use core_calendar\external\events_related_objects_cache;
  20  use core_calendar\local\event\container;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  require_once (__DIR__ . '/helpers.php');
  25  
  26  /**
  27   * Tests for the events_related_objects_cache.
  28   *
  29   * @package    core_calendar
  30   * @copyright  2017 Ryan Wyllie <ryan@moodle.com>
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class events_related_objects_cache_test extends \advanced_testcase {
  34  
  35      /**
  36       * Tests set up
  37       */
  38      protected function setUp(): void {
  39          $this->resetAfterTest();
  40      }
  41  
  42      /**
  43       * An event with no module should return null when trying to retrieve
  44       * the module instance.
  45       */
  46      public function test_get_module_instance_no_module() {
  47          $this->setAdminUser();
  48          $mapper = container::get_event_mapper();
  49          $legacyevent = create_event([
  50              'modulename' => '',
  51              'instance' => 0
  52          ]);
  53          $event = $mapper->from_legacy_event_to_event($legacyevent);
  54          $cache = new events_related_objects_cache([$event]);
  55  
  56          $this->assertNull($cache->get_module_instance($event));
  57      }
  58  
  59      /**
  60       * The get_module_instance should return the correct module instances
  61       * for the given set of events in the cache.
  62       */
  63      public function test_get_module_instance_with_modules() {
  64          $this->setAdminUser();
  65          $mapper = container::get_event_mapper();
  66          $generator = $this->getDataGenerator();
  67          $course = $generator->create_course();
  68          $plugingenerator = $generator->get_plugin_generator('mod_assign');
  69          $instance1 = $plugingenerator->create_instance(['course' => $course->id]);
  70          $instance2 = $plugingenerator->create_instance(['course' => $course->id]);
  71          unset($instance1->cmid);
  72          unset($instance2->cmid);
  73  
  74          $params = [
  75              'type' => CALENDAR_EVENT_TYPE_ACTION,
  76              'courseid' => $course->id,
  77              'modulename' => 'assign',
  78              'userid' => 0,
  79              'eventtype' => 'due',
  80              'repeats' => 0,
  81              'timestart' => 1,
  82          ];
  83  
  84          $legacyevent1 = create_event(array_merge($params, ['name' => 'Event 1', 'instance' => $instance1->id]));
  85          $legacyevent2 = create_event(array_merge($params, ['name' => 'Event 2', 'instance' => $instance1->id]));
  86          $legacyevent3 = create_event(array_merge($params, ['name' => 'Event 3', 'instance' => $instance2->id]));
  87          $event1 = $mapper->from_legacy_event_to_event($legacyevent1);
  88          $event2 = $mapper->from_legacy_event_to_event($legacyevent2);
  89          $event3 = $mapper->from_legacy_event_to_event($legacyevent3);
  90          $cache = new events_related_objects_cache([$event1, $event2, $event3]);
  91  
  92          $eventinstance1 = $cache->get_module_instance($event1);
  93          $eventinstance2 = $cache->get_module_instance($event2);
  94          $eventinstance3 = $cache->get_module_instance($event3);
  95  
  96          $this->assertEquals($instance1, $eventinstance1);
  97          $this->assertEquals($instance1, $eventinstance2);
  98          $this->assertEquals($instance2, $eventinstance3);
  99      }
 100  
 101      /**
 102       * Trying to load the course module of an event that isn't in
 103       * the cache should return null.
 104       */
 105      public function test_module_instance_unknown_event() {
 106          $this->setAdminUser();
 107          $mapper = container::get_event_mapper();
 108          $generator = $this->getDataGenerator();
 109          $course = $generator->create_course();
 110          $plugingenerator = $generator->get_plugin_generator('mod_assign');
 111          $instance1 = $plugingenerator->create_instance(['course' => $course->id]);
 112          $instance2 = $plugingenerator->create_instance(['course' => $course->id]);
 113          unset($instance1->cmid);
 114          unset($instance2->cmid);
 115  
 116          $params = [
 117              'type' => CALENDAR_EVENT_TYPE_ACTION,
 118              'courseid' => $course->id,
 119              'modulename' => 'assign',
 120              'userid' => 0,
 121              'eventtype' => 'due',
 122              'repeats' => 0,
 123              'timestart' => 1,
 124          ];
 125  
 126          $legacyevent1 = create_event(array_merge($params, ['name' => 'Event 1', 'instance' => $instance1->id]));
 127          $legacyevent2 = create_event(array_merge($params, ['name' => 'Event 2', 'instance' => $instance2->id]));
 128          $event1 = $mapper->from_legacy_event_to_event($legacyevent1);
 129          $event2 = $mapper->from_legacy_event_to_event($legacyevent2);
 130          $cache = new events_related_objects_cache([$event1]);
 131  
 132          $this->assertNull($cache->get_module_instance($event2));
 133      }
 134  }