Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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 /** 18 * Module lib related unit tests 19 * 20 * @package core 21 * @category phpunit 22 * @copyright 2016 Juan Leyva 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 require_once($CFG->dirroot . '/course/lib.php'); 30 require_once($CFG->dirroot . '/course/modlib.php'); 31 32 class core_course_modlib_testcase extends advanced_testcase { 33 34 /** 35 * Test prepare_new_moduleinfo_data 36 */ 37 public function test_prepare_new_moduleinfo_data() { 38 global $DB; 39 $this->resetAfterTest(true); 40 41 $this->setAdminUser(); 42 $course = self::getDataGenerator()->create_course(); 43 $coursecontext = context_course::instance($course->id); 44 // Test with a complex module, like assign. 45 $assignmodule = $DB->get_record('modules', array('name' => 'assign'), '*', MUST_EXIST); 46 $sectionnumber = 1; 47 48 list($module, $context, $cw, $cm, $data) = prepare_new_moduleinfo_data($course, $assignmodule->name, $sectionnumber); 49 $this->assertEquals($assignmodule, $module); 50 $this->assertEquals($coursecontext, $context); 51 $this->assertNull($cm); // Not cm yet. 52 53 $expecteddata = new stdClass(); 54 $expecteddata->section = $sectionnumber; 55 $expecteddata->visible = 1; 56 $expecteddata->course = $course->id; 57 $expecteddata->module = $module->id; 58 $expecteddata->modulename = $module->name; 59 $expecteddata->groupmode = $course->groupmode; 60 $expecteddata->groupingid = $course->defaultgroupingid; 61 $expecteddata->id = ''; 62 $expecteddata->instance = ''; 63 $expecteddata->coursemodule = ''; 64 $expecteddata->advancedgradingmethod_submissions = ''; // Not grading methods enabled by default. 65 $expecteddata->completion = 0; 66 // Unset untestable. 67 unset($data->introeditor); 68 unset($data->_advancedgradingdata); 69 70 $this->assertEquals($expecteddata, $data); 71 72 // Create a viewer user. Not able to edit. 73 $viewer = self::getDataGenerator()->create_user(); 74 $this->getDataGenerator()->enrol_user($viewer->id, $course->id); 75 $this->setUser($viewer); 76 $this->expectException('required_capability_exception'); 77 prepare_new_moduleinfo_data($course, $assignmodule->name, $sectionnumber); 78 } 79 80 /** 81 * Test get_moduleinfo_data 82 */ 83 public function test_get_moduleinfo_data() { 84 global $DB; 85 $this->resetAfterTest(true); 86 $this->setAdminUser(); 87 $course = self::getDataGenerator()->create_course(); 88 $assignmodule = $DB->get_record('modules', array('name' => 'assign'), '*', MUST_EXIST); 89 $assign = self::getDataGenerator()->create_module('assign', array('course' => $course->id)); 90 $assigncm = get_coursemodule_from_id('assign', $assign->cmid); 91 $assigncontext = context_module::instance($assign->cmid); 92 93 list($cm, $context, $module, $data, $cw) = get_moduleinfo_data($assigncm, $course); 94 $this->assertEquals($assigncm, $cm); 95 $this->assertEquals($assigncontext, $context); 96 $this->assertEquals($assignmodule, $module); 97 98 // Prepare expected data. 99 $expecteddata = clone $assign; 100 $expecteddata->coursemodule = $assigncm->id; 101 $expecteddata->section = $cw->section; 102 $expecteddata->visible = $assigncm->visible; 103 $expecteddata->visibleoncoursepage = $assigncm->visibleoncoursepage; 104 $expecteddata->cmidnumber = $assigncm->idnumber; 105 $expecteddata->groupmode = groups_get_activity_groupmode($cm); 106 $expecteddata->groupingid = $assigncm->groupingid; 107 $expecteddata->course = $course->id; 108 $expecteddata->module = $module->id; 109 $expecteddata->modulename = $module->name; 110 $expecteddata->instance = $assigncm->instance; 111 $expecteddata->completion = $assigncm->completion; 112 $expecteddata->completionview = $assigncm->completionview; 113 $expecteddata->completionexpected = $assigncm->completionexpected; 114 $expecteddata->completionusegrade = is_null($assigncm->completiongradeitemnumber) ? 0 : 1; 115 $expecteddata->completiongradeitemnumber = null; 116 $expecteddata->showdescription = $assigncm->showdescription; 117 $expecteddata->tags = core_tag_tag::get_item_tags_array('core', 'course_modules', $assigncm->id); 118 $expecteddata->availabilityconditionsjson = null; 119 $expecteddata->advancedgradingmethod_submissions = null; 120 if ($items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => 'assign', 121 'iteminstance' => $assign->id, 'courseid' => $course->id))) { 122 // set category if present 123 $gradecat = false; 124 foreach ($items as $item) { 125 if ($gradecat === false) { 126 $gradecat = $item->categoryid; 127 continue; 128 } 129 if ($gradecat != $item->categoryid) { 130 //mixed categories 131 $gradecat = false; 132 break; 133 } 134 } 135 if ($gradecat !== false) { 136 // do not set if mixed categories present 137 $expecteddata->gradecat = $gradecat; 138 } 139 } 140 $expecteddata->gradepass = '0.00'; 141 142 // Unset untestable. 143 unset($expecteddata->cmid); 144 unset($data->introeditor); 145 unset($data->_advancedgradingdata); 146 147 $this->assertEquals($expecteddata, $data); 148 149 // Create a viewer user. Not able to edit. 150 $viewer = self::getDataGenerator()->create_user(); 151 $this->getDataGenerator()->enrol_user($viewer->id, $course->id); 152 $this->setUser($viewer); 153 $this->expectException('required_capability_exception'); 154 get_moduleinfo_data($assigncm, $course); 155 } 156 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body