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 * External mod_resource functions unit tests
20 * @package mod_resource
22 * @copyright 2015 Juan Leyva <juan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
31 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
34 * External mod_resource functions unit tests
36 * @package mod_resource
38 * @copyright 2015 Juan Leyva <juan@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class mod_resource_external_testcase extends externallib_advanced_testcase {
47 public function test_view_resource() {
50 $this->resetAfterTest(true);
52 $this->setAdminUser();
54 $course = $this->getDataGenerator()->create_course();
55 $resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id));
56 $context = context_module::instance($resource->cmid);
57 $cm = get_coursemodule_from_instance('resource', $resource->id);
59 // Test invalid instance id.
61 mod_resource_external::view_resource(0);
62 $this->fail('Exception expected due to invalid mod_resource instance id.');
63 } catch (moodle_exception $e) {
64 $this->assertEquals('invalidrecord', $e->errorcode);
67 // Test not-enrolled user.
68 $user = self::getDataGenerator()->create_user();
69 $this->setUser($user);
71 mod_resource_external::view_resource($resource->id);
72 $this->fail('Exception expected due to not enrolled user.');
73 } catch (moodle_exception $e) {
74 $this->assertEquals('requireloginerror', $e->errorcode);
77 // Test user with full capabilities.
78 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
79 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
81 // Trigger and capture the event.
82 $sink = $this->redirectEvents();
84 $result = mod_resource_external::view_resource($resource->id);
85 $result = external_api::clean_returnvalue(mod_resource_external::view_resource_returns(), $result);
87 $events = $sink->get_events();
88 $this->assertCount(1, $events);
89 $event = array_shift($events);
91 // Checking that the event contains the expected values.
92 $this->assertInstanceOf('\mod_resource\event\course_module_viewed', $event);
93 $this->assertEquals($context, $event->get_context());
94 $moodleurl = new \moodle_url('/mod/resource/view.php', array('id' => $cm->id));
95 $this->assertEquals($moodleurl, $event->get_url());
96 $this->assertEventContextNotUsed($event);
97 $this->assertNotEmpty($event->get_name());
99 // Test user with no capabilities.
100 // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
101 assign_capability('mod/resource:view', CAP_PROHIBIT, $studentrole->id, $context->id);
102 // Empty all the caches that may be affected by this change.
103 accesslib_clear_all_caches_for_unit_testing();
104 course_modinfo::clear_instance_cache();
107 mod_resource_external::view_resource($resource->id);
108 $this->fail('Exception expected due to missing capability.');
109 } catch (moodle_exception $e) {
110 $this->assertEquals('requireloginerror', $e->errorcode);
116 * Test test_mod_resource_get_resources_by_courses
118 public function test_mod_resource_get_resources_by_courses() {
121 $this->resetAfterTest(true);
123 $course1 = self::getDataGenerator()->create_course();
124 $course2 = self::getDataGenerator()->create_course();
126 $student = self::getDataGenerator()->create_user();
127 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
128 $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id);
130 self::setUser($student);
133 $record = new stdClass();
134 $record->course = $course1->id;
135 $resource1 = self::getDataGenerator()->create_module('resource', $record);
138 $record = new stdClass();
139 $record->course = $course2->id;
140 $resource2 = self::getDataGenerator()->create_module('resource', $record);
142 // Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
143 $enrol = enrol_get_plugin('manual');
144 $enrolinstances = enrol_get_instances($course2->id, true);
145 foreach ($enrolinstances as $courseenrolinstance) {
146 if ($courseenrolinstance->enrol == "manual") {
147 $instance2 = $courseenrolinstance;
151 $enrol->enrol_user($instance2, $student->id, $studentrole->id);
153 $returndescription = mod_resource_external::get_resources_by_courses_returns();
155 // Create what we expect to be returned when querying the two courses.
156 $expectedfields = array('id', 'course', 'name', 'intro', 'introformat', 'introfiles',
157 'contentfiles', 'tobemigrated', 'legacyfiles', 'legacyfileslast', 'display', 'displayoptions',
158 'filterfiles', 'revision', 'timemodified', 'section', 'visible', 'groupmode', 'groupingid');
160 // Add expected coursemodule and data.
161 $resource1->coursemodule = $resource1->cmid;
162 $resource1->introformat = 1;
163 $resource1->contentformat = 1;
164 $resource1->section = 0;
165 $resource1->visible = true;
166 $resource1->groupmode = 0;
167 $resource1->groupingid = 0;
168 $resource1->introfiles = [];
169 $resource1->contentfiles = [];
171 $resource2->coursemodule = $resource2->cmid;
172 $resource2->introformat = 1;
173 $resource2->contentformat = 1;
174 $resource2->section = 0;
175 $resource2->visible = true;
176 $resource2->groupmode = 0;
177 $resource2->groupingid = 0;
178 $resource2->introfiles = [];
179 $resource2->contentfiles = [];
181 foreach ($expectedfields as $field) {
182 $expected1[$field] = $resource1->{$field};
183 $expected2[$field] = $resource2->{$field};
186 $expectedresources = array($expected2, $expected1);
188 // Call the external function passing course ids.
189 $result = mod_resource_external::get_resources_by_courses(array($course2->id, $course1->id));
190 $result = external_api::clean_returnvalue($returndescription, $result);
192 // Remove the contentfiles (to be checked bellow).
193 $result['resources'][0]['contentfiles'] = [];
194 $result['resources'][1]['contentfiles'] = [];
196 // Now, check that we retrieve the same data we created.
197 $this->assertEquals($expectedresources, $result['resources']);
198 $this->assertCount(0, $result['warnings']);
200 // Call the external function without passing course id.
201 $result = mod_resource_external::get_resources_by_courses();
202 $result = external_api::clean_returnvalue($returndescription, $result);
204 // Remove the contentfiles (to be checked bellow).
205 $result['resources'][0]['contentfiles'] = [];
206 $result['resources'][1]['contentfiles'] = [];
208 // Check that without course ids we still get the correct data.
209 $this->assertEquals($expectedresources, $result['resources']);
210 $this->assertCount(0, $result['warnings']);
212 // Add a file to the intro.
213 $fileintroname = "fileintro.txt";
214 $filerecordinline = array(
215 'contextid' => context_module::instance($resource2->cmid)->id,
216 'component' => 'mod_resource',
217 'filearea' => 'intro',
220 'filename' => $fileintroname,
222 $fs = get_file_storage();
224 $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
226 $result = mod_resource_external::get_resources_by_courses(array($course2->id, $course1->id));
227 $result = external_api::clean_returnvalue($returndescription, $result);
229 // Check that we receive correctly the files.
230 $this->assertCount(1, $result['resources'][0]['introfiles']);
231 $this->assertEquals($fileintroname, $result['resources'][0]['introfiles'][0]['filename']);
232 $this->assertCount(1, $result['resources'][0]['contentfiles']);
233 $this->assertCount(1, $result['resources'][1]['contentfiles']);
234 // Test autogenerated resource.
235 $this->assertEquals('resource2.txt', $result['resources'][0]['contentfiles'][0]['filename']);
236 $this->assertEquals('resource1.txt', $result['resources'][1]['contentfiles'][0]['filename']);
238 // Unenrol user from second course.
239 $enrol->unenrol_user($instance2, $student->id);
240 array_shift($expectedresources);
242 // Call the external function without passing course id.
243 $result = mod_resource_external::get_resources_by_courses();
244 $result = external_api::clean_returnvalue($returndescription, $result);
246 // Remove the contentfiles (to be checked bellow).
247 $result['resources'][0]['contentfiles'] = [];
248 $this->assertEquals($expectedresources, $result['resources']);
250 // Call for the second course we unenrolled the user from, expected warning.
251 $result = mod_resource_external::get_resources_by_courses(array($course2->id));
252 $this->assertCount(1, $result['warnings']);
253 $this->assertEquals('1', $result['warnings'][0]['warningcode']);
254 $this->assertEquals($course2->id, $result['warnings'][0]['itemid']);