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