Merge branch 'MDL-33509-master' of git://github.com/mihailges/moodle
[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                 'location' => 'Test',
57                 'format' => 1,
58                 'courseid' => $course->id,
59                 'groupid' => 0,
60                 'userid' => 2,
61                 'modulename' => 'assign',
62                 'instance' => 1,
63                 'eventtype' => 'due',
64                 'timestart' => time(),
65                 'timeduration' => 86400,
66                 'visible' => 1
67             ], [
68                 'name' => 'Start of lesson',
69                 'description' => '',
70                 'location' => 'Test',
71                 'format' => 1,
72                 'courseid' => $course->id,
73                 'groupid' => 0,
74                 'userid' => 2,
75                 'modulename' => 'lesson',
76                 'instance' => 1,
77                 'eventtype' => 'end',
78                 'timestart' => time(),
79                 'timeduration' => 86400,
80                 'visible' => 1
81             ]
82         ];
84         foreach ($events as $event) {
85             calendar_event::create($event, false);
86         }
88         // Get all events.
89         $events = $retrievalstrategy->get_raw_events(null, [0], null);
90         $this->assertCount(2, $events);
92         // Disable the lesson module.
93         $modulerecord = $DB->get_record('modules', ['name' => 'lesson']);
94         $modulerecord->visible = 0;
95         $DB->update_record('modules', $modulerecord);
97         // Check that we only return the assign event.
98         $events = $retrievalstrategy->get_raw_events(null, [0], null);
99         $this->assertCount(1, $events);
100         $event = reset($events);
101         $this->assertEquals('assign', $event->modulename);
102     }
104     /**
105      * Test retrieval strategy when there are overrides.
106      */
107     public function test_get_raw_event_strategy_with_overrides() {
108         $this->resetAfterTest();
110         $retrievalstrategy = new raw_event_retrieval_strategy();
111         $generator = $this->getDataGenerator();
112         $course = $generator->create_course();
113         $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
115         $instance = $plugingenerator->create_instance(['course' => $course->id]);
117         // Create users.
118         $useroverridestudent = $generator->create_user();
119         $group1student = $generator->create_user();
120         $group2student = $generator->create_user();
121         $group12student = $generator->create_user();
122         $nogroupstudent = $generator->create_user();
124         // Enrol users.
125         $generator->enrol_user($useroverridestudent->id, $course->id, 'student');
126         $generator->enrol_user($group1student->id, $course->id, 'student');
127         $generator->enrol_user($group2student->id, $course->id, 'student');
128         $generator->enrol_user($group12student->id, $course->id, 'student');
130         $generator->enrol_user($nogroupstudent->id, $course->id, 'student');
132         // Create groups.
133         $group1 = $generator->create_group(['courseid' => $course->id, 'name' => 'Group 1']);
134         $group2 = $generator->create_group(['courseid' => $course->id, 'name' => 'Group 2']);
136         // Add members to groups.
137         $generator->create_group_member(['groupid' => $group1->id, 'userid' => $group1student->id]);
138         $generator->create_group_member(['groupid' => $group2->id, 'userid' => $group2student->id]);
139         $generator->create_group_member(['groupid' => $group1->id, 'userid' => $group12student->id]);
140         $generator->create_group_member(['groupid' => $group2->id, 'userid' => $group12student->id]);
142         $now = time();
144         // Events with the same module name, instance and event type.
145         $events = [
146             [
147                 'name' => 'Assignment 1 due date',
148                 'description' => '',
149                 'location' => 'Test',
150                 'format' => 0,
151                 'courseid' => $course->id,
152                 'groupid' => 0,
153                 'userid' => 2,
154                 'modulename' => 'assign',
155                 'instance' => $instance->id,
156                 'eventtype' => 'due',
157                 'timestart' => $now,
158                 'timeduration' => 0,
159                 'visible' => 1
160             ], [
161                 'name' => 'Assignment 1 due date - User override',
162                 'description' => '',
163                 'location' => 'Test',
164                 'format' => 1,
165                 'courseid' => 0,
166                 'groupid' => 0,
167                 'userid' => $useroverridestudent->id,
168                 'modulename' => 'assign',
169                 'instance' => $instance->id,
170                 'eventtype' => 'due',
171                 'timestart' => $now + 86400,
172                 'timeduration' => 0,
173                 'visible' => 1,
174                 'priority' => CALENDAR_EVENT_USER_OVERRIDE_PRIORITY
175             ], [
176                 'name' => 'Assignment 1 due date - Group A override',
177                 'description' => '',
178                 'location' => 'Test',
179                 'format' => 1,
180                 'courseid' => $course->id,
181                 'groupid' => $group1->id,
182                 'userid' => 2,
183                 'modulename' => 'assign',
184                 'instance' => $instance->id,
185                 'eventtype' => 'due',
186                 'timestart' => $now + (2 * 86400),
187                 'timeduration' => 0,
188                 'visible' => 1,
189                 'priority' => 1,
190             ], [
191                 'name' => 'Assignment 1 due date - Group B override',
192                 'description' => '',
193                 'location' => 'Test',
194                 'format' => 1,
195                 'courseid' => $course->id,
196                 'groupid' => $group2->id,
197                 'userid' => 2,
198                 'modulename' => 'assign',
199                 'instance' => $instance->id,
200                 'eventtype' => 'due',
201                 'timestart' => $now + (3 * 86400),
202                 'timeduration' => 0,
203                 'visible' => 1,
204                 'priority' => 2,
205             ],
206         ];
208         foreach ($events as $event) {
209             calendar_event::create($event, false);
210         }
212         $timestart = $now - 100;
213         $timeend = $now + (3 * 86400);
214         $groups = [$group1->id, $group2->id];
216         // Get user override events.
217         $this->setUser($useroverridestudent);
218         $events = $retrievalstrategy->get_raw_events([$useroverridestudent->id], $groups, [$course->id]);
219         $this->assertCount(1, $events);
220         $event = reset($events);
221         $this->assertEquals('Assignment 1 due date - User override', $event->name);
223         // Get events for user that does not belong to any group and has no user override events.
224         $this->setUser($nogroupstudent);
225         $events = $retrievalstrategy->get_raw_events([$nogroupstudent->id], $groups, [$course->id]);
226         $this->assertCount(1, $events);
227         $event = reset($events);
228         $this->assertEquals('Assignment 1 due date', $event->name);
230         // Get events for user that belongs to groups A and B and has no user override events.
231         $this->setUser($group12student);
232         $events = $retrievalstrategy->get_raw_events([$group12student->id], $groups, [$course->id]);
233         $this->assertCount(1, $events);
234         $event = reset($events);
235         $this->assertEquals('Assignment 1 due date - Group A override', $event->name);
237         // Get events for user that belongs to group A and has no user override events.
238         $this->setUser($group1student);
239         $events = $retrievalstrategy->get_raw_events([$group1student->id], $groups, [$course->id]);
240         $this->assertCount(1, $events);
241         $event = reset($events);
242         $this->assertEquals('Assignment 1 due date - Group A override', $event->name);
244         // Add repeating events.
245         $repeatingevents = [
246             [
247                 'name' => 'Repeating site event',
248                 'description' => '',
249                 'location' => 'Test',
250                 'format' => 1,
251                 'courseid' => SITEID,
252                 'groupid' => 0,
253                 'userid' => 2,
254                 'repeatid' => 1,
255                 'modulename' => '0',
256                 'instance' => 0,
257                 'eventtype' => 'site',
258                 'timestart' => $now + 86400,
259                 'timeduration' => 0,
260                 'visible' => 1,
261             ],
262             [
263                 'name' => 'Repeating site event',
264                 'description' => '',
265                 'location' => 'Test',
266                 'format' => 1,
267                 'courseid' => SITEID,
268                 'groupid' => 0,
269                 'userid' => 2,
270                 'repeatid' => 1,
271                 'modulename' => '0',
272                 'instance' => 0,
273                 'eventtype' => 'site',
274                 'timestart' => $now + (2 * 86400),
275                 'timeduration' => 0,
276                 'visible' => 1,
277             ],
278         ];
280         foreach ($repeatingevents as $event) {
281             calendar_event::create($event, false);
282         }
284         // Make sure repeating events are not filtered out.
285         $events = $retrievalstrategy->get_raw_events();
286         $this->assertCount(3, $events);
287     }
289     /**
290      * Test retrieval strategy with category specifications.
291      */
292     public function test_get_raw_events_category() {
293         global $DB;
295         $this->resetAfterTest();
296         $retrievalstrategy = new raw_event_retrieval_strategy();
297         $generator = $this->getDataGenerator();
298         $category1 = $generator->create_category();
299         $category2 = $generator->create_category();
300         $events = [
301             [
302                 'name' => 'E1',
303                 'eventtype' => 'category',
304                 'description' => '',
305                 'location' => 'Test',
306                 'format' => 1,
307                 'categoryid' => $category1->id,
308                 'userid' => 2,
309                 'timestart' => time(),
310             ],
311             [
312                 'name' => 'E2',
313                 'eventtype' => 'category',
314                 'description' => '',
315                 'location' => 'Test',
316                 'format' => 1,
317                 'categoryid' => $category2->id,
318                 'userid' => 2,
319                 'timestart' => time() + 1,
320             ],
321         ];
323         foreach ($events as $event) {
324             calendar_event::create($event, false);
325         }
327         // Get all events.
328         $events = $retrievalstrategy->get_raw_events(null, null, null, null);
329         $this->assertCount(2, $events);
331         $event = array_shift($events);
332         $this->assertEquals('E1', $event->name);
333         $event = array_shift($events);
334         $this->assertEquals('E2', $event->name);
336         // Get events for C1 events.
337         $events = $retrievalstrategy->get_raw_events(null, null, null, [$category1->id]);
338         $this->assertCount(1, $events);
340         $event = array_shift($events);
341         $this->assertEquals('E1', $event->name);
343         // Get events for C2 events.
344         $events = $retrievalstrategy->get_raw_events(null, null, null, [$category2->id]);
345         $this->assertCount(1, $events);
347         $event = array_shift($events);
348         $this->assertEquals('E2', $event->name);
350         // Get events for several categories.
351         $events = $retrievalstrategy->get_raw_events(null, null, null, [$category1->id, $category2->id]);
352         $this->assertCount(2, $events);
353     }