MDL-62384 privacy: Modify user contexts query for auth_oauth2
[moodle.git] / calendar / tests / events_related_objects_cache_test.php
blobf0799db5347e25bafa0b48d57cffc58aef0b117c
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.
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/>.
17 /**
18 * Tests for the events_related_objects_cache.
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
25 defined('MOODLE_INTERNAL') || die();
27 require_once(__DIR__ . '/helpers.php');
29 use \core_calendar\external\events_related_objects_cache;
30 use \core_calendar\local\event\container;
32 /**
33 * Tests for the events_related_objects_cache.
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
39 class core_calendar_events_related_objects_cache_testcase extends advanced_testcase {
41 /**
42 * Tests set up
44 protected function setUp() {
45 $this->resetAfterTest();
48 /**
49 * An event with no module should return null when trying to retrieve
50 * the module instance.
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]);
62 $this->assertNull($cache->get_module_instance($event));
65 /**
66 * The get_module_instance should return the correct module instances
67 * for the given set of events in the cache.
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);
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,
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]);
98 $eventinstance1 = $cache->get_module_instance($event1);
99 $eventinstance2 = $cache->get_module_instance($event2);
100 $eventinstance3 = $cache->get_module_instance($event3);
102 $this->assertEquals($instance1, $eventinstance1);
103 $this->assertEquals($instance1, $eventinstance2);
104 $this->assertEquals($instance2, $eventinstance3);
108 * Trying to load the course module of an event that isn't in
109 * the cache should return null.
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);
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,
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]);
138 $this->assertNull($cache->get_module_instance($event2));