Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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 core_course; 18 19 defined('MOODLE_INTERNAL') || die(); 20 21 global $CFG; 22 require_once($CFG->dirroot . '/course/lib.php'); 23 require_once($CFG->dirroot . '/course/modlib.php'); 24 25 /** 26 * Module lib related unit tests 27 * 28 * @package core_course 29 * @category test 30 * @copyright 2016 Juan Leyva 31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 32 */ 33 class modlib_test extends \advanced_testcase { 34 35 /** 36 * Test prepare_new_moduleinfo_data 37 */ 38 public function test_prepare_new_moduleinfo_data() { 39 global $DB; 40 $this->resetAfterTest(true); 41 42 $this->setAdminUser(); 43 $course = self::getDataGenerator()->create_course(); 44 $coursecontext = \context_course::instance($course->id); 45 // Test with a complex module, like assign. 46 $assignmodule = $DB->get_record('modules', array('name' => 'assign'), '*', MUST_EXIST); 47 $sectionnumber = 1; 48 49 list($module, $context, $cw, $cm, $data) = prepare_new_moduleinfo_data($course, $assignmodule->name, $sectionnumber); 50 $this->assertEquals($assignmodule, $module); 51 $this->assertEquals($coursecontext, $context); 52 $this->assertNull($cm); // Not cm yet. 53 54 $expecteddata = new \stdClass(); 55 $expecteddata->section = $sectionnumber; 56 $expecteddata->visible = 1; 57 $expecteddata->course = $course->id; 58 $expecteddata->module = $module->id; 59 $expecteddata->modulename = $module->name; 60 $expecteddata->groupmode = $course->groupmode; 61 $expecteddata->groupingid = $course->defaultgroupingid; 62 $expecteddata->id = ''; 63 $expecteddata->instance = ''; 64 $expecteddata->coursemodule = ''; 65 $expecteddata->advancedgradingmethod_submissions = ''; // Not grading methods enabled by default. 66 $expecteddata->completion = 0; 67 $expecteddata->downloadcontent = DOWNLOAD_COURSE_CONTENT_ENABLED; 68 69 // Unset untestable. 70 unset($data->introeditor); 71 unset($data->_advancedgradingdata); 72 73 $this->assertEquals($expecteddata, $data); 74 75 // Create a viewer user. Not able to edit. 76 $viewer = self::getDataGenerator()->create_user(); 77 $this->getDataGenerator()->enrol_user($viewer->id, $course->id); 78 $this->setUser($viewer); 79 $this->expectException('required_capability_exception'); 80 prepare_new_moduleinfo_data($course, $assignmodule->name, $sectionnumber); 81 } 82 83 /** 84 * Test prepare_new_moduleinfo_data with suffix (which is currently only used by the completion rules). 85 * @covers ::prepare_new_moduleinfo_data 86 */ 87 public function test_prepare_new_moduleinfo_data_with_suffix() { 88 global $DB; 89 $this->resetAfterTest(true); 90 91 $this->setAdminUser(); 92 $course = self::getDataGenerator()->create_course(); 93 $coursecontext = \context_course::instance($course->id); 94 // Test with a complex module, like assign. 95 $assignmodule = $DB->get_record('modules', ['name' => 'assign'], '*', MUST_EXIST); 96 $sectionnumber = 1; 97 98 $suffix = 'mysuffix'; 99 [$module, $context, $cw, $cm, $data] = prepare_new_moduleinfo_data($course, $assignmodule->name, $sectionnumber, $suffix); 100 $this->assertEquals($assignmodule, $module); 101 $this->assertEquals($coursecontext, $context); 102 $this->assertNull($cm); // Not cm yet. 103 104 $expecteddata = new \stdClass(); 105 $expecteddata->section = $sectionnumber; 106 $expecteddata->visible = 1; 107 $expecteddata->course = $course->id; 108 $expecteddata->module = $module->id; 109 $expecteddata->modulename = $module->name; 110 $expecteddata->groupmode = $course->groupmode; 111 $expecteddata->groupingid = $course->defaultgroupingid; 112 $expecteddata->id = ''; 113 $expecteddata->instance = ''; 114 $expecteddata->coursemodule = ''; 115 $expecteddata->advancedgradingmethod_submissions = ''; // Not grading methods enabled by default. 116 $expecteddata->{'completion' . $suffix} = 0; 117 $expecteddata->downloadcontent = DOWNLOAD_COURSE_CONTENT_ENABLED; 118 119 // Unset untestable. 120 unset($data->introeditor); 121 unset($data->_advancedgradingdata); 122 123 $this->assertEquals($expecteddata, $data); 124 $this->assertFalse(property_exists($data, 'completion')); 125 } 126 127 /** 128 * Test get_moduleinfo_data 129 */ 130 public function test_get_moduleinfo_data() { 131 global $DB; 132 $this->resetAfterTest(true); 133 $this->setAdminUser(); 134 $course = self::getDataGenerator()->create_course(); 135 $assignmodule = $DB->get_record('modules', array('name' => 'assign'), '*', MUST_EXIST); 136 $assign = self::getDataGenerator()->create_module('assign', array('course' => $course->id)); 137 $assigncm = get_coursemodule_from_id('assign', $assign->cmid); 138 $assigncontext = \context_module::instance($assign->cmid); 139 140 list($cm, $context, $module, $data, $cw) = get_moduleinfo_data($assigncm, $course); 141 $this->assertEquals($assigncm, $cm); 142 $this->assertEquals($assigncontext, $context); 143 $this->assertEquals($assignmodule, $module); 144 145 // Prepare expected data. 146 $expecteddata = clone $assign; 147 $expecteddata->coursemodule = $assigncm->id; 148 $expecteddata->section = $cw->section; 149 $expecteddata->visible = $assigncm->visible; 150 $expecteddata->visibleoncoursepage = $assigncm->visibleoncoursepage; 151 $expecteddata->cmidnumber = $assigncm->idnumber; 152 $expecteddata->groupmode = groups_get_activity_groupmode($cm); 153 $expecteddata->groupingid = $assigncm->groupingid; 154 $expecteddata->course = $course->id; 155 $expecteddata->module = $module->id; 156 $expecteddata->modulename = $module->name; 157 $expecteddata->instance = $assigncm->instance; 158 $expecteddata->completion = $assigncm->completion; 159 $expecteddata->completionview = $assigncm->completionview; 160 $expecteddata->completionexpected = $assigncm->completionexpected; 161 $expecteddata->completionusegrade = is_null($assigncm->completiongradeitemnumber) ? 0 : 1; 162 $expecteddata->completionpassgrade = $assigncm->completionpassgrade; 163 $expecteddata->completiongradeitemnumber = null; 164 $expecteddata->showdescription = $assigncm->showdescription; 165 $expecteddata->downloadcontent = $assigncm->downloadcontent; 166 $expecteddata->tags = \core_tag_tag::get_item_tags_array('core', 'course_modules', $assigncm->id); 167 $expecteddata->lang = null; 168 $expecteddata->availabilityconditionsjson = null; 169 $expecteddata->advancedgradingmethod_submissions = null; 170 if ($items = \grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => 'assign', 171 'iteminstance' => $assign->id, 'courseid' => $course->id))) { 172 // set category if present 173 $gradecat = false; 174 foreach ($items as $item) { 175 if ($gradecat === false) { 176 $gradecat = $item->categoryid; 177 continue; 178 } 179 if ($gradecat != $item->categoryid) { 180 //mixed categories 181 $gradecat = false; 182 break; 183 } 184 } 185 if ($gradecat !== false) { 186 // do not set if mixed categories present 187 $expecteddata->gradecat = $gradecat; 188 } 189 } 190 $expecteddata->gradepass = '0.00'; 191 $expecteddata->completionpassgrade = $assigncm->completionpassgrade; 192 193 // Unset untestable. 194 unset($expecteddata->cmid); 195 unset($data->introeditor); 196 unset($data->_advancedgradingdata); 197 198 $this->assertEquals($expecteddata, $data); 199 200 // Create a viewer user. Not able to edit. 201 $viewer = self::getDataGenerator()->create_user(); 202 $this->getDataGenerator()->enrol_user($viewer->id, $course->id); 203 $this->setUser($viewer); 204 $this->expectException('required_capability_exception'); 205 get_moduleinfo_data($assigncm, $course); 206 } 207 208 /** 209 * Test add_moduleinfo (only beforemod parameter for now). 210 * 211 * @covers \add_moduleinfo 212 */ 213 public function test_add_moduleinfo() { 214 global $DB; 215 $this->resetAfterTest(true); 216 217 $this->setAdminUser(); 218 $course = self::getDataGenerator()->create_course(); 219 $labelmodule = $DB->get_record('modules', ['name' => 'label'], '*', MUST_EXIST); 220 $sectionnumber = 1; 221 $modules = []; 222 $moduleinfo = []; 223 224 for ($i = 0; $i < 4; $i++) { 225 $modules[$i] = self::getDataGenerator()->create_module('label', ['course' => $course->id, 'section' => $sectionnumber]); 226 $modulescm[$i] = get_coursemodule_from_id('label', $modules[$i]->cmid); 227 } 228 229 $modules[4] = self::getDataGenerator()->create_module('label', ['course' => $course->id, 'section' => $sectionnumber + 1]); 230 $modulescm[4] = get_coursemodule_from_id('label', $modules[4]->cmid); 231 232 // The beforemod attribute is not set, should be null afterwards. 233 list($module, $context, $cw, $cm, $data) = prepare_new_moduleinfo_data($course, $labelmodule->name, $sectionnumber); 234 $moduleinfo[0] = add_moduleinfo($data, $course); 235 $this->assertEquals(null, $moduleinfo[0]->beforemod); 236 237 // Insert before the first module. 238 list($module, $context, $cw, $cm, $data) = prepare_new_moduleinfo_data($course, $labelmodule->name, $sectionnumber); 239 $data->beforemod = $modulescm[0]->id; 240 $moduleinfo[1] = add_moduleinfo($data, $course); 241 $this->assertEquals($modulescm[0]->id, $moduleinfo[1]->beforemod); 242 243 // Insert between the two last modules. 244 list($module, $context, $cw, $cm, $data) = prepare_new_moduleinfo_data($course, $labelmodule->name, $sectionnumber); 245 $data->beforemod = $modulescm[3]->id; 246 $moduleinfo[2] = add_moduleinfo($data, $course); 247 $this->assertEquals($modulescm[3]->id, $moduleinfo[2]->beforemod); 248 249 // Insert before a not existing module. 250 course_delete_module($modulescm[2]->id); 251 252 list($module, $context, $cw, $cm, $data) = prepare_new_moduleinfo_data($course, $labelmodule->name, $sectionnumber); 253 $data->beforemod = $modulescm[2]->id; 254 $moduleinfo[3] = add_moduleinfo($data, $course); 255 $this->assertEquals($modulescm[2]->id, $moduleinfo[3]->beforemod); 256 257 // Insert before a module that is in another section. 258 list($module, $context, $cw, $cm, $data) = prepare_new_moduleinfo_data($course, $labelmodule->name, $sectionnumber); 259 $data->beforemod = $modulescm[4]->id; 260 $moduleinfo[4] = add_moduleinfo($data, $course); 261 $this->assertEquals($modulescm[4]->id, $moduleinfo[4]->beforemod); 262 263 $modinfo = get_fast_modinfo($course); 264 265 $expectedorder = [ 266 $moduleinfo[1]->coursemodule, 267 $modulescm[0]->id, 268 $modulescm[1]->id, 269 $moduleinfo[2]->coursemodule, 270 $modulescm[3]->id, 271 $moduleinfo[0]->coursemodule, 272 $moduleinfo[3]->coursemodule, 273 $moduleinfo[4]->coursemodule, 274 ]; 275 276 $this->assertEquals($expectedorder, $modinfo->get_sections()[$sectionnumber]); 277 } 278 279 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body