See Release Notes
Long Term Support Release
Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]
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_folder; 18 19 use externallib_advanced_testcase; 20 use mod_folder_external; 21 22 defined('MOODLE_INTERNAL') || die(); 23 24 global $CFG; 25 26 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 27 28 /** 29 * External mod_folder functions unit tests 30 * 31 * @package mod_folder 32 * @category external 33 * @copyright 2015 Juan Leyva <juan@moodle.com> 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 * @since Moodle 3.0 36 */ 37 class externallib_test extends externallib_advanced_testcase { 38 39 /** 40 * Test view_folder 41 */ 42 public function test_view_folder() { 43 global $DB; 44 45 $this->resetAfterTest(true); 46 47 $this->setAdminUser(); 48 // Setup test data. 49 $course = $this->getDataGenerator()->create_course(); 50 $folder = $this->getDataGenerator()->create_module('folder', array('course' => $course->id)); 51 $context = \context_module::instance($folder->cmid); 52 $cm = get_coursemodule_from_instance('folder', $folder->id); 53 54 // Test invalid instance id. 55 try { 56 mod_folder_external::view_folder(0); 57 $this->fail('Exception expected due to invalid mod_folder instance id.'); 58 } catch (\moodle_exception $e) { 59 $this->assertEquals('invalidrecord', $e->errorcode); 60 } 61 62 // Test not-enrolled user. 63 $user = self::getDataGenerator()->create_user(); 64 $this->setUser($user); 65 try { 66 mod_folder_external::view_folder($folder->id); 67 $this->fail('Exception expected due to not enrolled user.'); 68 } catch (\moodle_exception $e) { 69 $this->assertEquals('requireloginerror', $e->errorcode); 70 } 71 72 // Test user with full capabilities. 73 $studentrole = $DB->get_record('role', array('shortname' => 'student')); 74 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id); 75 76 // Trigger and capture the event. 77 $sink = $this->redirectEvents(); 78 79 $result = mod_folder_external::view_folder($folder->id); 80 $result = \external_api::clean_returnvalue(mod_folder_external::view_folder_returns(), $result); 81 82 $events = $sink->get_events(); 83 $this->assertCount(1, $events); 84 $event = array_shift($events); 85 86 // Checking that the event contains the expected values. 87 $this->assertInstanceOf('\mod_folder\event\course_module_viewed', $event); 88 $this->assertEquals($context, $event->get_context()); 89 $moodlefolder = new \moodle_url('/mod/folder/view.php', array('id' => $cm->id)); 90 $this->assertEquals($moodlefolder, $event->get_url()); 91 $this->assertEventContextNotUsed($event); 92 $this->assertNotEmpty($event->get_name()); 93 94 // Test user with no capabilities. 95 // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles. 96 assign_capability('mod/folder:view', CAP_PROHIBIT, $studentrole->id, $context->id); 97 // Empty all the caches that may be affected by this change. 98 accesslib_clear_all_caches_for_unit_testing(); 99 \course_modinfo::clear_instance_cache(); 100 101 try { 102 mod_folder_external::view_folder($folder->id); 103 $this->fail('Exception expected due to missing capability.'); 104 } catch (\moodle_exception $e) { 105 $this->assertEquals('requireloginerror', $e->errorcode); 106 } 107 } 108 109 /** 110 * Test test_mod_folder_get_folders_by_courses 111 */ 112 public function test_mod_folder_get_folders_by_courses() { 113 global $DB; 114 115 $this->resetAfterTest(true); 116 117 $course1 = self::getDataGenerator()->create_course(); 118 $course2 = self::getDataGenerator()->create_course(); 119 120 $student = self::getDataGenerator()->create_user(); 121 $studentrole = $DB->get_record('role', array('shortname' => 'student')); 122 $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id); 123 124 self::setUser($student); 125 126 // First folder. 127 $record = new \stdClass(); 128 $record->course = $course1->id; 129 $record->forcedownload = 1; 130 $folder1 = self::getDataGenerator()->create_module('folder', $record); 131 132 // Second folder. 133 $record = new \stdClass(); 134 $record->course = $course2->id; 135 $record->forcedownload = 0; 136 $folder2 = self::getDataGenerator()->create_module('folder', $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_folder_external::get_folders_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', 'revision', 153 'timemodified', 'display', 'showexpanded', 'showdownloadfolder', 'section', 'visible', 154 'forcedownload', 'groupmode', 'groupingid'); 155 156 // Add expected coursemodule and data. 157 $folder1->coursemodule = $folder1->cmid; 158 $folder1->introformat = 1; 159 $folder1->section = 0; 160 $folder1->visible = true; 161 $folder1->groupmode = 0; 162 $folder1->groupingid = 0; 163 $folder1->introfiles = []; 164 $folder1->lang = ''; 165 166 $folder2->coursemodule = $folder2->cmid; 167 $folder2->introformat = 1; 168 $folder2->section = 0; 169 $folder2->visible = true; 170 $folder2->groupmode = 0; 171 $folder2->groupingid = 0; 172 $folder2->introfiles = []; 173 $folder2->lang = ''; 174 175 foreach ($expectedfields as $field) { 176 $expected1[$field] = $folder1->{$field}; 177 $expected2[$field] = $folder2->{$field}; 178 } 179 180 $expectedfolders = array($expected2, $expected1); 181 182 // Call the external function passing course ids. 183 $result = mod_folder_external::get_folders_by_courses(array($course2->id, $course1->id)); 184 $result = \external_api::clean_returnvalue($returndescription, $result); 185 186 $this->assertEquals($expectedfolders, $result['folders']); 187 $this->assertCount(0, $result['warnings']); 188 189 // Call the external function without passing course id. 190 $result = mod_folder_external::get_folders_by_courses(); 191 $result = \external_api::clean_returnvalue($returndescription, $result); 192 193 $this->assertEquals($expectedfolders, $result['folders']); 194 $this->assertCount(0, $result['warnings']); 195 196 // Add a file to the intro. 197 $fileintroname = "fileintro.txt"; 198 $filerecordinline = array( 199 'contextid' => \context_module::instance($folder2->cmid)->id, 200 'component' => 'mod_folder', 201 'filearea' => 'intro', 202 'itemid' => 0, 203 'filepath' => '/', 204 'filename' => $fileintroname, 205 ); 206 $fs = get_file_storage(); 207 $timepost = time(); 208 $fs->create_file_from_string($filerecordinline, 'image contents (not really)'); 209 210 $result = mod_folder_external::get_folders_by_courses(array($course2->id, $course1->id)); 211 $result = \external_api::clean_returnvalue($returndescription, $result); 212 213 $this->assertCount(1, $result['folders'][0]['introfiles']); 214 $this->assertEquals($fileintroname, $result['folders'][0]['introfiles'][0]['filename']); 215 216 // Unenrol user from second course. 217 $enrol->unenrol_user($instance2, $student->id); 218 array_shift($expectedfolders); 219 220 // Call the external function without passing course id. 221 $result = mod_folder_external::get_folders_by_courses(); 222 $result = \external_api::clean_returnvalue($returndescription, $result); 223 224 $this->assertEquals($expectedfolders, $result['folders']); 225 226 // Call for the second course we unenrolled the user from, expected warning. 227 $result = mod_folder_external::get_folders_by_courses(array($course2->id)); 228 $this->assertCount(1, $result['warnings']); 229 $this->assertEquals('1', $result['warnings'][0]['warningcode']); 230 $this->assertEquals($course2->id, $result['warnings'][0]['itemid']); 231 } 232 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body