2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Event container tests.
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
25 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->dirroot . '/calendar/lib.php');
31 use core_calendar\local\event\entities\action_event;
32 use core_calendar\local\event\entities\event;
33 use core_calendar\local\event\entities\event_interface;
34 use core_calendar\local\event\factories\event_factory;
35 use core_calendar\local\event\factories\event_factory_interface;
36 use core_calendar\local\event\mappers\event_mapper;
37 use core_calendar\local\event\mappers\event_mapper_interface;
40 * Core container testcase.
42 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 class core_calendar_container_testcase extends advanced_testcase {
50 public function setUp() {
51 $this->resetAfterTest();
52 $this->setAdminUser();
56 * Test getting the event factory.
58 public function test_get_event_factory() {
59 $factory = \core_calendar\local\event\container::get_event_factory();
61 // Test that the container is returning the right type.
62 $this->assertInstanceOf(event_factory_interface::class, $factory);
63 // Test that the container is returning the right implementation.
64 $this->assertInstanceOf(event_factory::class, $factory);
66 // Test that getting the factory a second time returns the same instance.
67 $factory2 = \core_calendar\local\event\container::get_event_factory();
68 $this->assertTrue($factory === $factory2);
72 * Test that the event factory correctly creates instances of events.
74 * @dataProvider get_event_factory_testcases()
75 * @param \stdClass $dbrow Row from the "database".
77 public function test_event_factory_create_instance($dbrow) {
78 $legacyevent = $this->create_event($dbrow);
79 $factory = \core_calendar\local\event\container::get_event_factory();
80 $course = $this->getDataGenerator()->create_course();
81 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
82 $moduleinstance = $generator->create_instance(['course' => $course->id]);
84 // Set some of the fake dbrow properties to match real data in the DB
85 // this is necessary as the factory hides things that modinfo doesn't
87 $dbrow->id = $legacyevent->id;
88 $dbrow->courseid = $course->id;
89 $dbrow->instance = $moduleinstance->id;
90 $dbrow->modulename = 'assign';
91 $event = $factory->create_instance($dbrow);
93 // Test that the factory is returning the right type.
94 $this->assertInstanceOf(event_interface::class, $event);
95 // Test that the factory is returning the right implementation.
96 $this->assertTrue($event instanceof event || $event instanceof action_event);
98 // Test that the event created has the correct properties.
99 $this->assertEquals($legacyevent->id, $event->get_id());
100 $this->assertEquals($dbrow->description, $event->get_description()->get_value());
101 $this->assertEquals($dbrow->format, $event->get_description()->get_format());
102 $this->assertEquals($dbrow->courseid, $event->get_course()->get_id());
104 if ($dbrow->groupid == 0) {
105 $this->assertNull($event->get_group());
107 $this->assertEquals($dbrow->groupid, $event->get_group()->get_id());
110 $this->assertEquals($dbrow->userid, $event->get_user()->get_id());
111 $this->assertEquals($legacyevent->id, $event->get_repeats()->get_id());
112 $this->assertEquals($dbrow->modulename, $event->get_course_module()->get('modname'));
113 $this->assertEquals($dbrow->instance, $event->get_course_module()->get('instance'));
114 $this->assertEquals($dbrow->timestart, $event->get_times()->get_start_time()->getTimestamp());
115 $this->assertEquals($dbrow->timemodified, $event->get_times()->get_modified_time()->getTimestamp());
116 $this->assertEquals($dbrow->timesort, $event->get_times()->get_sort_time()->getTimestamp());
118 if ($dbrow->visible == 1) {
119 $this->assertTrue($event->is_visible());
121 $this->assertFalse($event->is_visible());
124 if (!$dbrow->subscriptionid) {
125 $this->assertNull($event->get_subscription());
127 $this->assertEquals($event->get_subscription()->get_id());
132 * Test that the event factory deals with invisible modules properly as admin.
134 * @dataProvider get_event_factory_testcases()
135 * @param \stdClass $dbrow Row from the "database".
137 public function test_event_factory_when_module_visibility_is_toggled_as_admin($dbrow) {
138 $legacyevent = $this->create_event($dbrow);
139 $factory = \core_calendar\local\event\container::get_event_factory();
140 $course = $this->getDataGenerator()->create_course();
141 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
142 $moduleinstance = $generator->create_instance(['course' => $course->id]);
144 $dbrow->id = $legacyevent->id;
145 $dbrow->courseid = $course->id;
146 $dbrow->instance = $moduleinstance->id;
147 $dbrow->modulename = 'assign';
149 set_coursemodule_visible($moduleinstance->cmid, 0);
151 $event = $factory->create_instance($dbrow);
153 // Test that the factory is returning an event as the admin can see hidden course modules.
154 $this->assertInstanceOf(event_interface::class, $event);
158 * Test that the event factory deals with invisible modules properly as a guest.
160 * @dataProvider get_event_factory_testcases()
161 * @param \stdClass $dbrow Row from the "database".
163 public function test_event_factory_when_module_visibility_is_toggled_as_guest($dbrow) {
164 $legacyevent = $this->create_event($dbrow);
165 $factory = \core_calendar\local\event\container::get_event_factory();
166 $course = $this->getDataGenerator()->create_course();
167 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
168 $moduleinstance = $generator->create_instance(['course' => $course->id]);
170 $dbrow->id = $legacyevent->id;
171 $dbrow->courseid = $course->id;
172 $dbrow->instance = $moduleinstance->id;
173 $dbrow->modulename = 'assign';
175 set_coursemodule_visible($moduleinstance->cmid, 0);
177 // Set to a user who can not view hidden course modules.
178 $this->setGuestUser();
180 $event = $factory->create_instance($dbrow);
182 // Module is invisible to guest users so this should return null.
183 $this->assertNull($event);
187 * Test that the event factory deals with completion related events properly.
189 public function test_event_factory_with_completion_related_event() {
192 $CFG->enablecompletion = true;
194 // Create the course we will be using.
195 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
197 // Add the assignment.
198 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
199 $assign = $generator->create_instance(array('course' => $course->id), array('completion' => 1));
201 // Create a completion event.
202 $event = new \stdClass();
203 $event->name = 'An event';
204 $event->description = 'Event description';
205 $event->format = FORMAT_HTML;
206 $event->eventtype = \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED;
208 $event->modulename = 'assign';
209 $event->instance = $assign->id;
210 $event->courseid = $course->id;
212 $event->timestart = time();
213 $event->timesort = time();
214 $event->timemodified = time();
215 $event->timeduration = 0;
216 $event->subscriptionid = null;
217 $event->repeatid = 0;
218 $legacyevent = $this->create_event($event);
220 // Update the id of the event that was created.
221 $event->id = $legacyevent->id;
223 // Create the factory we are going to be testing the behaviour of.
224 $factory = \core_calendar\local\event\container::get_event_factory();
226 // Check that we get the correct instance.
227 $this->assertInstanceOf(event_interface::class, $factory->create_instance($event));
229 // Now, disable completion.
230 $CFG->enablecompletion = false;
232 // The result should now be null since we have disabled completion.
233 $this->assertNull($factory->create_instance($event));
237 * Test that the event factory only returns an event if the logged in user
238 * is enrolled in the course.
240 public function test_event_factory_unenrolled_user() {
241 $user = $this->getDataGenerator()->create_user();
242 // Create the course we will be using.
243 $course = $this->getDataGenerator()->create_course();
245 // Add the assignment.
246 $generator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
247 $lesson = $generator->create_instance(array('course' => $course->id));
249 // Create a user override event for the lesson.
250 $event = new \stdClass();
251 $event->name = 'An event';
252 $event->description = 'Event description';
253 $event->format = FORMAT_HTML;
254 $event->eventtype = 'close';
255 $event->userid = $user->id;
256 $event->modulename = 'lesson';
257 $event->instance = $lesson->id;
258 $event->courseid = $course->id;
260 $event->timestart = time();
261 $event->timesort = time();
262 $event->timemodified = time();
263 $event->timeduration = 0;
264 $event->subscriptionid = null;
265 $event->repeatid = 0;
266 $legacyevent = $this->create_event($event);
268 // Update the id of the event that was created.
269 $event->id = $legacyevent->id;
271 // Set the logged in user to the one we created.
272 $this->setUser($user);
274 // Create the factory we are going to be testing the behaviour of.
275 $factory = \core_calendar\local\event\container::get_event_factory();
277 // The result should be null since the user is not enrolled in the
278 // course the event is for.
279 $this->assertNull($factory->create_instance($event));
281 // Now enrol the user in the course.
282 $this->getDataGenerator()->enrol_user($user->id, $course->id);
284 // Check that we get the correct instance.
285 $this->assertInstanceOf(event_interface::class, $factory->create_instance($event));
289 * Test getting the event mapper.
291 public function test_get_event_mapper() {
292 $mapper = \core_calendar\local\event\container::get_event_mapper();
294 $this->assertInstanceOf(event_mapper_interface::class, $mapper);
295 $this->assertInstanceOf(event_mapper::class, $mapper);
297 $mapper2 = \core_calendar\local\event\container::get_event_mapper();
299 $this->assertTrue($mapper === $mapper2);
303 * Test cases for the get event factory test.
305 public function get_event_factory_testcases() {
309 'name' => 'Test event',
310 'description' => 'Hello',
316 'modulename' => 'assign',
318 'eventtype' => 'due',
319 'timestart' => 1486396800,
321 'timesort' => 1486396800,
323 'timemodified' => 1485793098,
324 'subscriptionid' => null
330 'name' => 'Test event',
331 'description' => 'Hello',
337 'modulename' => 'assign',
339 'eventtype' => 'due',
340 'timestart' => 1486396800,
342 'timesort' => 1486396800,
344 'timemodified' => 1485793098,
345 'subscriptionid' => null
352 * Helper function to create calendar events using the old code.
354 * @param array $properties A list of calendar event properties to set
355 * @return calendar_event|bool
357 protected function create_event($properties = []) {
358 $record = new \stdClass();
359 $record->name = 'event name';
360 $record->eventtype = 'global';
361 $record->timestart = time();
362 $record->timeduration = 0;
363 $record->timesort = 0;
365 $record->courseid = 0;
367 foreach ($properties as $name => $value) {
368 $record->$name = $value;
371 $event = new calendar_event($record);
372 return $event->create($record, false);