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