MDL-33509 forms: Change button type in url resource editing page
[moodle.git] / calendar / tests / raw_event_retrieval_strategy_test.php
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/>.
17 /**
18  * Raw event retrieval strategy tests.
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  */
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->dirroot . '/calendar/tests/helpers.php');
30 use core_calendar\local\event\strategies\raw_event_retrieval_strategy;
32 /**
33  * Raw event retrieval strategy testcase.
34  *
35  * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
36  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37  */
38 class core_calendar_raw_event_retrieval_strategy_testcase extends advanced_testcase {
39     /**
40      * Test retrieval strategy when module is disabled.
41      */
42     public function test_get_raw_events_with_disabled_module() {
43         global $DB;
45         $this->resetAfterTest();
46         $retrievalstrategy = new raw_event_retrieval_strategy();
47         $generator = $this->getDataGenerator();
48         $course = $generator->create_course();
49         $student = $generator->create_user();
50         $generator->enrol_user($student->id, $course->id, 'student');
51         $this->setUser($student);
52         $events = [
53             [
54                 'name' => 'Start of assignment',
55                 'description' => '',
56                 'format' => 1,
57                 'courseid' => $course->id,
58                 'groupid' => 0,
59                 'userid' => 2,
60                 'modulename' => 'assign',
61                 'instance' => 1,
62                 'eventtype' => 'due',
63                 'timestart' => time(),
64                 'timeduration' => 86400,
65                 'visible' => 1
66             ], [
67                 'name' => 'Start of lesson',
68                 'description' => '',
69                 'format' => 1,
70                 'courseid' => $course->id,
71                 'groupid' => 0,
72                 'userid' => 2,
73                 'modulename' => 'lesson',
74                 'instance' => 1,
75                 'eventtype' => 'end',
76                 'timestart' => time(),
77                 'timeduration' => 86400,
78                 'visible' => 1
79             ]
80         ];
82         foreach ($events as $event) {
83             calendar_event::create($event, false);
84         }
86         // Get all events.
87         $events = $retrievalstrategy->get_raw_events(null, [0], null);
88         $this->assertCount(2, $events);
90         // Disable the lesson module.
91         $modulerecord = $DB->get_record('modules', ['name' => 'lesson']);
92         $modulerecord->visible = 0;
93         $DB->update_record('modules', $modulerecord);
95         // Check that we only return the assign event.
96         $events = $retrievalstrategy->get_raw_events(null, [0], null);
97         $this->assertCount(1, $events);
98         $event = reset($events);
99         $this->assertEquals('assign', $event->modulename);
100     }
102     /**
103      * Test retrieval strategy when there are overrides.
104      */
105     public function test_get_raw_event_strategy_with_overrides() {
106         $this->resetAfterTest();
108         $retrievalstrategy = new raw_event_retrieval_strategy();
109         $generator = $this->getDataGenerator();
110         $course = $generator->create_course();
111         $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
113         $instance = $plugingenerator->create_instance(['course' => $course->id]);
115         // Create users.
116         $useroverridestudent = $generator->create_user();
117         $group1student = $generator->create_user();
118         $group2student = $generator->create_user();
119         $group12student = $generator->create_user();
120         $nogroupstudent = $generator->create_user();
122         // Enrol users.
123         $generator->enrol_user($useroverridestudent->id, $course->id, 'student');
124         $generator->enrol_user($group1student->id, $course->id, 'student');
125         $generator->enrol_user($group2student->id, $course->id, 'student');
126         $generator->enrol_user($group12student->id, $course->id, 'student');
128         $generator->enrol_user($nogroupstudent->id, $course->id, 'student');
130         // Create groups.
131         $group1 = $generator->create_group(['courseid' => $course->id, 'name' => 'Group 1']);
132         $group2 = $generator->create_group(['courseid' => $course->id, 'name' => 'Group 2']);
134         // Add members to groups.
135         $generator->create_group_member(['groupid' => $group1->id, 'userid' => $group1student->id]);
136         $generator->create_group_member(['groupid' => $group2->id, 'userid' => $group2student->id]);
137         $generator->create_group_member(['groupid' => $group1->id, 'userid' => $group12student->id]);
138         $generator->create_group_member(['groupid' => $group2->id, 'userid' => $group12student->id]);
140         $now = time();
142         // Events with the same module name, instance and event type.
143         $events = [
144             [
145                 'name' => 'Assignment 1 due date',
146                 'description' => '',
147                 'format' => 0,
148                 'courseid' => $course->id,
149                 'groupid' => 0,
150                 'userid' => 2,
151                 'modulename' => 'assign',
152                 'instance' => $instance->id,
153                 'eventtype' => 'due',
154                 'timestart' => $now,
155                 'timeduration' => 0,
156                 'visible' => 1
157             ], [
158                 'name' => 'Assignment 1 due date - User override',
159                 'description' => '',
160                 'format' => 1,
161                 'courseid' => 0,
162                 'groupid' => 0,
163                 'userid' => $useroverridestudent->id,
164                 'modulename' => 'assign',
165                 'instance' => $instance->id,
166                 'eventtype' => 'due',
167                 'timestart' => $now + 86400,
168                 'timeduration' => 0,
169                 'visible' => 1,
170                 'priority' => CALENDAR_EVENT_USER_OVERRIDE_PRIORITY
171             ], [
172                 'name' => 'Assignment 1 due date - Group A override',
173                 'description' => '',
174                 'format' => 1,
175                 'courseid' => $course->id,
176                 'groupid' => $group1->id,
177                 'userid' => 2,
178                 'modulename' => 'assign',
179                 'instance' => $instance->id,
180                 'eventtype' => 'due',
181                 'timestart' => $now + (2 * 86400),
182                 'timeduration' => 0,
183                 'visible' => 1,
184                 'priority' => 1,
185             ], [
186                 'name' => 'Assignment 1 due date - Group B override',
187                 'description' => '',
188                 'format' => 1,
189                 'courseid' => $course->id,
190                 'groupid' => $group2->id,
191                 'userid' => 2,
192                 'modulename' => 'assign',
193                 'instance' => $instance->id,
194                 'eventtype' => 'due',
195                 'timestart' => $now + (3 * 86400),
196                 'timeduration' => 0,
197                 'visible' => 1,
198                 'priority' => 2,
199             ],
200         ];
202         foreach ($events as $event) {
203             calendar_event::create($event, false);
204         }
206         $timestart = $now - 100;
207         $timeend = $now + (3 * 86400);
208         $groups = [$group1->id, $group2->id];
210         // Get user override events.
211         $this->setUser($useroverridestudent);
212         $events = $retrievalstrategy->get_raw_events([$useroverridestudent->id], $groups, [$course->id]);
213         $this->assertCount(1, $events);
214         $event = reset($events);
215         $this->assertEquals('Assignment 1 due date - User override', $event->name);
217         // Get events for user that does not belong to any group and has no user override events.
218         $this->setUser($nogroupstudent);
219         $events = $retrievalstrategy->get_raw_events([$nogroupstudent->id], $groups, [$course->id]);
220         $this->assertCount(1, $events);
221         $event = reset($events);
222         $this->assertEquals('Assignment 1 due date', $event->name);
224         // Get events for user that belongs to groups A and B and has no user override events.
225         $this->setUser($group12student);
226         $events = $retrievalstrategy->get_raw_events([$group12student->id], $groups, [$course->id]);
227         $this->assertCount(1, $events);
228         $event = reset($events);
229         $this->assertEquals('Assignment 1 due date - Group A override', $event->name);
231         // Get events for user that belongs to group A and has no user override events.
232         $this->setUser($group1student);
233         $events = $retrievalstrategy->get_raw_events([$group1student->id], $groups, [$course->id]);
234         $this->assertCount(1, $events);
235         $event = reset($events);
236         $this->assertEquals('Assignment 1 due date - Group A override', $event->name);
238         // Add repeating events.
239         $repeatingevents = [
240             [
241                 'name' => 'Repeating site event',
242                 'description' => '',
243                 'format' => 1,
244                 'courseid' => SITEID,
245                 'groupid' => 0,
246                 'userid' => 2,
247                 'repeatid' => 1,
248                 'modulename' => '0',
249                 'instance' => 0,
250                 'eventtype' => 'site',
251                 'timestart' => $now + 86400,
252                 'timeduration' => 0,
253                 'visible' => 1,
254             ],
255             [
256                 'name' => 'Repeating site event',
257                 'description' => '',
258                 'format' => 1,
259                 'courseid' => SITEID,
260                 'groupid' => 0,
261                 'userid' => 2,
262                 'repeatid' => 1,
263                 'modulename' => '0',
264                 'instance' => 0,
265                 'eventtype' => 'site',
266                 'timestart' => $now + (2 * 86400),
267                 'timeduration' => 0,
268                 'visible' => 1,
269             ],
270         ];
272         foreach ($repeatingevents as $event) {
273             calendar_event::create($event, false);
274         }
276         // Make sure repeating events are not filtered out.
277         $events = $retrievalstrategy->get_raw_events();
278         $this->assertCount(3, $events);
279     }
281     /**
282      * Test retrieval strategy with category specifications.
283      */
284     public function test_get_raw_events_category() {
285         global $DB;
287         $this->resetAfterTest();
288         $retrievalstrategy = new raw_event_retrieval_strategy();
289         $generator = $this->getDataGenerator();
290         $category1 = $generator->create_category();
291         $category2 = $generator->create_category();
292         $events = [
293             [
294                 'name' => 'E1',
295                 'eventtype' => 'category',
296                 'description' => '',
297                 'format' => 1,
298                 'categoryid' => $category1->id,
299                 'userid' => 2,
300                 'timestart' => time(),
301             ],
302             [
303                 'name' => 'E2',
304                 'eventtype' => 'category',
305                 'description' => '',
306                 'format' => 1,
307                 'categoryid' => $category2->id,
308                 'userid' => 2,
309                 'timestart' => time() + 1,
310             ],
311         ];
313         foreach ($events as $event) {
314             calendar_event::create($event, false);
315         }
317         // Get all events.
318         $events = $retrievalstrategy->get_raw_events(null, null, null, null);
319         $this->assertCount(2, $events);
321         $event = array_shift($events);
322         $this->assertEquals('E1', $event->name);
323         $event = array_shift($events);
324         $this->assertEquals('E2', $event->name);
326         // Get events for C1 events.
327         $events = $retrievalstrategy->get_raw_events(null, null, null, [$category1->id]);
328         $this->assertCount(1, $events);
330         $event = array_shift($events);
331         $this->assertEquals('E1', $event->name);
333         // Get events for C2 events.
334         $events = $retrievalstrategy->get_raw_events(null, null, null, [$category2->id]);
335         $this->assertCount(1, $events);
337         $event = array_shift($events);
338         $this->assertEquals('E2', $event->name);
340         // Get events for several categories.
341         $events = $retrievalstrategy->get_raw_events(null, null, null, [$category1->id, $category2->id]);
342         $this->assertCount(2, $events);
343     }