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 37 and 310] [Versions 37 and 311]

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