8f4f5a7f47f9fe91ec57535623df848e1f2155bc
[moodle.git] / calendar / tests / container_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  * Event container 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;
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;
39 /**
40  * Core container testcase.
41  *
42  * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
43  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44  */
45 class core_calendar_container_testcase extends advanced_testcase {
47     /**
48      * Test setup.
49      */
50     public function setUp() {
51         $this->resetAfterTest();
52         $this->setAdminUser();
53     }
55     /**
56      * Test getting the event factory.
57      */
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);
69     }
71     /**
72      * Test that the event factory correctly creates instances of events.
73      *
74      * @dataProvider get_event_factory_testcases()
75      * @param \stdClass $dbrow Row from the "database".
76      */
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
86         // know about.
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());
106         } else {
107             $this->assertEquals($dbrow->groupid, $event->get_group()->get_id());
108         }
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());
120         } else {
121             $this->assertFalse($event->is_visible());
122         }
124         if (!$dbrow->subscriptionid) {
125             $this->assertNull($event->get_subscription());
126         } else {
127             $this->assertEquals($event->get_subscription()->get_id());
128         }
129     }
131     /**
132      * Test that the event factory deals with invisible modules properly as admin.
133      *
134      * @dataProvider get_event_factory_testcases()
135      * @param \stdClass $dbrow Row from the "database".
136      */
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);
155     }
157     /**
158      * Test that the event factory deals with invisible modules properly as a guest.
159      *
160      * @dataProvider get_event_factory_testcases()
161      * @param \stdClass $dbrow Row from the "database".
162      */
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);
184     }
186     /**
187      * Test that the event factory deals with completion related events properly.
188      */
189     public function test_event_factory_with_completion_related_event() {
190         global $CFG;
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;
207         $event->userid = 1;
208         $event->modulename = 'assign';
209         $event->instance = $assign->id;
210         $event->courseid = $course->id;
211         $event->groupid = 0;
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));
234     }
236     /**
237      * Test getting the event mapper.
238      */
239     public function test_get_event_mapper() {
240         $mapper = \core_calendar\local\event\container::get_event_mapper();
242         $this->assertInstanceOf(event_mapper_interface::class, $mapper);
243         $this->assertInstanceOf(event_mapper::class, $mapper);
245         $mapper2 = \core_calendar\local\event\container::get_event_mapper();
247         $this->assertTrue($mapper === $mapper2);
248     }
250     /**
251      * Test cases for the get event factory test.
252      */
253     public function get_event_factory_testcases() {
254         return [
255             'Data set 1' => [
256                 'dbrow' => (object)[
257                     'name' => 'Test event',
258                     'description' => 'Hello',
259                     'format' => 1,
260                     'courseid' => 1,
261                     'groupid' => 0,
262                     'userid' => 1,
263                     'repeatid' => 0,
264                     'modulename' => 'assign',
265                     'instance' => 2,
266                     'eventtype' => 'due',
267                     'timestart' => 1486396800,
268                     'timeduration' => 0,
269                     'timesort' => 1486396800,
270                     'visible' => 1,
271                     'timemodified' => 1485793098,
272                     'subscriptionid' => null
273                 ]
274             ],
276             'Data set 2' => [
277                 'dbrow' => (object)[
278                     'name' => 'Test event',
279                     'description' => 'Hello',
280                     'format' => 1,
281                     'courseid' => 1,
282                     'groupid' => 1,
283                     'userid' => 1,
284                     'repeatid' => 0,
285                     'modulename' => 'assign',
286                     'instance' => 2,
287                     'eventtype' => 'due',
288                     'timestart' => 1486396800,
289                     'timeduration' => 0,
290                     'timesort' => 1486396800,
291                     'visible' => 1,
292                     'timemodified' => 1485793098,
293                     'subscriptionid' => null
294                 ]
295             ]
296         ];
297     }
299     /**
300      * Helper function to create calendar events using the old code.
301      *
302      * @param array $properties A list of calendar event properties to set
303      * @return calendar_event|bool
304      */
305     protected function create_event($properties = []) {
306         $record = new \stdClass();
307         $record->name = 'event name';
308         $record->eventtype = 'global';
309         $record->timestart = time();
310         $record->timeduration = 0;
311         $record->timesort = 0;
312         $record->type = 1;
313         $record->courseid = 0;
315         foreach ($properties as $name => $value) {
316             $record->$name = $value;
317         }
319         $event = new calendar_event($record);
320         return $event->create($record, false);
321     }