MDL-63147 mod_resource: Accept userid in resource event_action
[moodle.git] / mod / resource / tests / lib_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  * Unit tests for mod_resource lib
19  *
20  * @package    mod_resource
21  * @category   external
22  * @copyright  2015 Juan Leyva <juan@moodle.com>
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  * @since      Moodle 3.0
25  */
27 defined('MOODLE_INTERNAL') || die();
30 /**
31  * Unit tests for mod_resource lib
32  *
33  * @package    mod_resource
34  * @category   external
35  * @copyright  2015 Juan Leyva <juan@moodle.com>
36  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37  * @since      Moodle 3.0
38  */
39 class mod_resource_lib_testcase extends advanced_testcase {
41     /**
42      * Prepares things before this test case is initialised
43      * @return void
44      */
45     public static function setUpBeforeClass() {
46         global $CFG;
47         require_once($CFG->dirroot . '/mod/resource/lib.php');
48     }
50     /**
51      * Test resource_view
52      * @return void
53      */
54     public function test_resource_view() {
55         global $CFG;
57         $CFG->enablecompletion = 1;
58         $this->resetAfterTest();
60         $this->setAdminUser();
61         // Setup test data.
62         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
63         $resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id),
64                                                             array('completion' => 2, 'completionview' => 1));
65         $context = context_module::instance($resource->cmid);
66         $cm = get_coursemodule_from_instance('resource', $resource->id);
68         // Trigger and capture the event.
69         $sink = $this->redirectEvents();
71         resource_view($resource, $course, $cm, $context);
73         $events = $sink->get_events();
74         // 2 additional events thanks to completion.
75         $this->assertCount(3, $events);
76         $event = array_shift($events);
78         // Checking that the event contains the expected values.
79         $this->assertInstanceOf('\mod_resource\event\course_module_viewed', $event);
80         $this->assertEquals($context, $event->get_context());
81         $moodleurl = new \moodle_url('/mod/resource/view.php', array('id' => $cm->id));
82         $this->assertEquals($moodleurl, $event->get_url());
83         $this->assertEventContextNotUsed($event);
84         $this->assertNotEmpty($event->get_name());
86         // Check completion status.
87         $completion = new completion_info($course);
88         $completiondata = $completion->get_data($cm);
89         $this->assertEquals(1, $completiondata->completionstate);
91     }
93     /**
94      * Tests the resource_get_coursemodule_info function.
95      *
96      * Note: This currently doesn't test every aspect of the function, mainly focusing on the icon.
97      */
98     public function test_get_coursemodule_info() {
99         global $DB, $USER;
101         $this->resetAfterTest();
102         $this->setAdminUser();
104         // Create course.
105         $generator = $this->getDataGenerator();
106         $course = $generator->create_course();
108         // Create a resource with no files.
109         $draftid = file_get_unused_draft_itemid();
110         $resource1 = $generator->create_module('resource', array('course' => $course->id,
111                 'name' => 'R1', 'files' => $draftid));
113         // Create a resource with one file.
114         $draftid = file_get_unused_draft_itemid();
115         $contextid = context_user::instance($USER->id)->id;
116         $filerecord = array('component' => 'user', 'filearea' => 'draft', 'contextid' => $contextid,
117                 'itemid' => $draftid, 'filename' => 'r2.txt', 'filepath' => '/');
118         $fs = get_file_storage();
119         $fs->create_file_from_string($filerecord, 'Test');
120         $resource2 = $generator->create_module('resource', array('course' => $course->id,
121                 'name' => 'R2', 'files' => $draftid));
123         // Create a resource with two files.
124         $draftid = file_get_unused_draft_itemid();
125         $filerecord = array('component' => 'user', 'filearea' => 'draft', 'contextid' => $contextid,
126                 'itemid' => $draftid, 'filename' => 'r3.txt', 'filepath' => '/', 'sortorder' => 1);
127         $fs->create_file_from_string($filerecord, 'Test');
128         $filerecord['filename'] = 'r3.doc';
129         $filerecord['sortorder'] = 2;
130         $fs->create_file_from_string($filerecord, 'Test');
131         $resource3 = $generator->create_module('resource', array('course' => $course->id,
132                 'name' => 'R3', 'files' => $draftid));
134         // Try get_coursemodule_info for first one.
135         $info = resource_get_coursemodule_info(
136                 $DB->get_record('course_modules', array('id' => $resource1->cmid)));
138         // The name should be set. There is no overridden icon.
139         $this->assertEquals('R1', $info->name);
140         $this->assertEmpty($info->icon);
142         // For second one, there should be an overridden icon.
143         $info = resource_get_coursemodule_info(
144                 $DB->get_record('course_modules', array('id' => $resource2->cmid)));
145         $this->assertEquals('R2', $info->name);
146         $this->assertEquals('f/text-24', $info->icon);
148         // For third one, it should use the highest sortorder icon.
149         $info = resource_get_coursemodule_info(
150                 $DB->get_record('course_modules', array('id' => $resource3->cmid)));
151         $this->assertEquals('R3', $info->name);
152         $this->assertEquals('f/document-24', $info->icon);
153     }
155     public function test_resource_core_calendar_provide_event_action() {
156         $this->resetAfterTest();
157         $this->setAdminUser();
159         // Create the activity.
160         $course = $this->getDataGenerator()->create_course();
161         $resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id));
163         // Create a calendar event.
164         $event = $this->create_action_event($course->id, $resource->id,
165             \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
167         // Create an action factory.
168         $factory = new \core_calendar\action_factory();
170         // Decorate action event.
171         $actionevent = mod_resource_core_calendar_provide_event_action($event, $factory);
173         // Confirm the event was decorated.
174         $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
175         $this->assertEquals(get_string('view'), $actionevent->get_name());
176         $this->assertInstanceOf('moodle_url', $actionevent->get_url());
177         $this->assertEquals(1, $actionevent->get_item_count());
178         $this->assertTrue($actionevent->is_actionable());
179     }
181     public function test_resource_core_calendar_provide_event_action_already_completed() {
182         global $CFG;
184         $this->resetAfterTest();
185         $this->setAdminUser();
187         $CFG->enablecompletion = 1;
189         // Create the activity.
190         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
191         $resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id),
192             array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
194         // Get some additional data.
195         $cm = get_coursemodule_from_instance('resource', $resource->id);
197         // Create a calendar event.
198         $event = $this->create_action_event($course->id, $resource->id,
199             \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
201         // Mark the activity as completed.
202         $completion = new completion_info($course);
203         $completion->set_module_viewed($cm);
205         // Create an action factory.
206         $factory = new \core_calendar\action_factory();
208         // Decorate action event.
209         $actionevent = mod_resource_core_calendar_provide_event_action($event, $factory);
211         // Ensure result was null.
212         $this->assertNull($actionevent);
213     }
215     /**
216      * Test mod_resource_core_calendar_provide_event_action with user override
217      */
218     public function test_resource_core_calendar_provide_event_action_user_override() {
219         global $CFG, $USER;
221         $this->resetAfterTest();
222         $this->setAdminUser();
223         $user = $this->getDataGenerator()->create_user();
224         $CFG->enablecompletion = 1;
226         // Create the activity.
227         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
228         $resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id),
229             array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
231         // Get some additional data.
232         $cm = get_coursemodule_from_instance('resource', $resource->id);
234         // Create a calendar event.
235         $event = $this->create_action_event($course->id, $resource->id,
236             \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
238         // Mark the activity as completed.
239         $completion = new completion_info($course);
240         $completion->set_module_viewed($cm);
242         // Create an action factory.
243         $factory = new \core_calendar\action_factory();
245         // Decorate action event.
246         $actionevent = mod_resource_core_calendar_provide_event_action($event, $factory, $USER->id);
248         // Decorate action with a userid override.
249         $actionevent2 = mod_resource_core_calendar_provide_event_action($event, $factory, $user->id);
251         // Ensure result was null because it has been marked as completed for the associated user.
252         // Logic was brought across from the "_already_completed" function.
253         $this->assertNull($actionevent);
255         // Confirm the event was decorated.
256         $this->assertNotNull($actionevent2);
257         $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent2);
258         $this->assertEquals(get_string('view'), $actionevent2->get_name());
259         $this->assertInstanceOf('moodle_url', $actionevent2->get_url());
260         $this->assertEquals(1, $actionevent2->get_item_count());
261         $this->assertTrue($actionevent2->is_actionable());
262     }
264     /**
265      * Creates an action event.
266      *
267      * @param int $courseid The course id.
268      * @param int $instanceid The instance id.
269      * @param string $eventtype The event type.
270      * @return bool|calendar_event
271      */
272     private function create_action_event($courseid, $instanceid, $eventtype) {
273         $event = new stdClass();
274         $event->name = 'Calendar event';
275         $event->modulename  = 'resource';
276         $event->courseid = $courseid;
277         $event->instance = $instanceid;
278         $event->type = CALENDAR_EVENT_TYPE_ACTION;
279         $event->eventtype = $eventtype;
280         $event->timestart = time();
282         return calendar_event::create($event);
283     }