1 |
efrain |
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 |
use core_courseformat\formatactions;
|
|
|
20 |
|
|
|
21 |
defined('MOODLE_INTERNAL') || die();
|
|
|
22 |
|
|
|
23 |
global $CFG;
|
|
|
24 |
require_once($CFG->dirroot . '/course/lib.php');
|
|
|
25 |
require_once($CFG->dirroot . '/course/modlib.php');
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Module lib related unit tests
|
|
|
29 |
*
|
|
|
30 |
* @package core_course
|
|
|
31 |
* @category test
|
|
|
32 |
* @copyright 2016 Juan Leyva
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
*/
|
|
|
35 |
class modlib_test extends \advanced_testcase {
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Test prepare_new_moduleinfo_data
|
|
|
39 |
*/
|
11 |
efrain |
40 |
public function test_prepare_new_moduleinfo_data(): void {
|
1 |
efrain |
41 |
global $DB;
|
|
|
42 |
$this->resetAfterTest(true);
|
|
|
43 |
|
|
|
44 |
$this->setAdminUser();
|
|
|
45 |
$course = self::getDataGenerator()->create_course();
|
|
|
46 |
$coursecontext = \context_course::instance($course->id);
|
|
|
47 |
// Test with a complex module, like assign.
|
|
|
48 |
$assignmodule = $DB->get_record('modules', array('name' => 'assign'), '*', MUST_EXIST);
|
|
|
49 |
$sectionnumber = 1;
|
|
|
50 |
|
|
|
51 |
list($module, $context, $cw, $cm, $data) = prepare_new_moduleinfo_data($course, $assignmodule->name, $sectionnumber);
|
|
|
52 |
$this->assertEquals($assignmodule, $module);
|
|
|
53 |
$this->assertEquals($coursecontext, $context);
|
|
|
54 |
$this->assertNull($cm); // Not cm yet.
|
|
|
55 |
|
|
|
56 |
$expecteddata = new \stdClass();
|
|
|
57 |
$expecteddata->section = $sectionnumber;
|
|
|
58 |
$expecteddata->visible = 1;
|
|
|
59 |
$expecteddata->course = $course->id;
|
|
|
60 |
$expecteddata->module = $module->id;
|
|
|
61 |
$expecteddata->modulename = $module->name;
|
|
|
62 |
$expecteddata->groupmode = $course->groupmode;
|
|
|
63 |
$expecteddata->groupingid = $course->defaultgroupingid;
|
|
|
64 |
$expecteddata->id = '';
|
|
|
65 |
$expecteddata->instance = '';
|
|
|
66 |
$expecteddata->coursemodule = '';
|
|
|
67 |
$expecteddata->advancedgradingmethod_submissions = ''; // Not grading methods enabled by default.
|
|
|
68 |
$expecteddata->completion = 0;
|
|
|
69 |
$expecteddata->downloadcontent = DOWNLOAD_COURSE_CONTENT_ENABLED;
|
|
|
70 |
|
|
|
71 |
// Unset untestable.
|
|
|
72 |
unset($data->introeditor);
|
|
|
73 |
unset($data->_advancedgradingdata);
|
|
|
74 |
|
|
|
75 |
$this->assertEquals($expecteddata, $data);
|
|
|
76 |
|
|
|
77 |
// Create a viewer user. Not able to edit.
|
|
|
78 |
$viewer = self::getDataGenerator()->create_user();
|
|
|
79 |
$this->getDataGenerator()->enrol_user($viewer->id, $course->id);
|
|
|
80 |
$this->setUser($viewer);
|
|
|
81 |
$this->expectException('required_capability_exception');
|
|
|
82 |
prepare_new_moduleinfo_data($course, $assignmodule->name, $sectionnumber);
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Test prepare_new_moduleinfo_data with suffix (which is currently only used by the completion rules).
|
|
|
87 |
* @covers ::prepare_new_moduleinfo_data
|
|
|
88 |
*/
|
11 |
efrain |
89 |
public function test_prepare_new_moduleinfo_data_with_suffix(): void {
|
1 |
efrain |
90 |
global $DB;
|
|
|
91 |
$this->resetAfterTest(true);
|
|
|
92 |
|
|
|
93 |
$this->setAdminUser();
|
|
|
94 |
$course = self::getDataGenerator()->create_course();
|
|
|
95 |
$coursecontext = \context_course::instance($course->id);
|
|
|
96 |
// Test with a complex module, like assign.
|
|
|
97 |
$assignmodule = $DB->get_record('modules', ['name' => 'assign'], '*', MUST_EXIST);
|
|
|
98 |
$sectionnumber = 1;
|
|
|
99 |
|
|
|
100 |
$suffix = 'mysuffix';
|
|
|
101 |
[$module, $context, $cw, $cm, $data] = prepare_new_moduleinfo_data($course, $assignmodule->name, $sectionnumber, $suffix);
|
|
|
102 |
$this->assertEquals($assignmodule, $module);
|
|
|
103 |
$this->assertEquals($coursecontext, $context);
|
|
|
104 |
$this->assertNull($cm); // Not cm yet.
|
|
|
105 |
|
|
|
106 |
$expecteddata = new \stdClass();
|
|
|
107 |
$expecteddata->section = $sectionnumber;
|
|
|
108 |
$expecteddata->visible = 1;
|
|
|
109 |
$expecteddata->course = $course->id;
|
|
|
110 |
$expecteddata->module = $module->id;
|
|
|
111 |
$expecteddata->modulename = $module->name;
|
|
|
112 |
$expecteddata->groupmode = $course->groupmode;
|
|
|
113 |
$expecteddata->groupingid = $course->defaultgroupingid;
|
|
|
114 |
$expecteddata->id = '';
|
|
|
115 |
$expecteddata->instance = '';
|
|
|
116 |
$expecteddata->coursemodule = '';
|
|
|
117 |
$expecteddata->advancedgradingmethod_submissions = ''; // Not grading methods enabled by default.
|
|
|
118 |
$expecteddata->{'completion' . $suffix} = 0;
|
|
|
119 |
$expecteddata->downloadcontent = DOWNLOAD_COURSE_CONTENT_ENABLED;
|
|
|
120 |
|
|
|
121 |
// Unset untestable.
|
|
|
122 |
unset($data->introeditor);
|
|
|
123 |
unset($data->_advancedgradingdata);
|
|
|
124 |
|
|
|
125 |
$this->assertEquals($expecteddata, $data);
|
|
|
126 |
$this->assertFalse(property_exists($data, 'completion'));
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Test get_moduleinfo_data
|
|
|
131 |
*/
|
11 |
efrain |
132 |
public function test_get_moduleinfo_data(): void {
|
1 |
efrain |
133 |
global $DB;
|
|
|
134 |
$this->resetAfterTest(true);
|
|
|
135 |
$this->setAdminUser();
|
|
|
136 |
$course = self::getDataGenerator()->create_course();
|
|
|
137 |
$assignmodule = $DB->get_record('modules', array('name' => 'assign'), '*', MUST_EXIST);
|
|
|
138 |
$assign = self::getDataGenerator()->create_module('assign', array('course' => $course->id));
|
|
|
139 |
$assigncm = get_coursemodule_from_id('assign', $assign->cmid);
|
|
|
140 |
$assigncontext = \context_module::instance($assign->cmid);
|
|
|
141 |
|
|
|
142 |
list($cm, $context, $module, $data, $cw) = get_moduleinfo_data($assigncm, $course);
|
|
|
143 |
$this->assertEquals($assigncm, $cm);
|
|
|
144 |
$this->assertEquals($assigncontext, $context);
|
|
|
145 |
$this->assertEquals($assignmodule, $module);
|
|
|
146 |
|
|
|
147 |
// Prepare expected data.
|
|
|
148 |
$expecteddata = clone $assign;
|
|
|
149 |
$expecteddata->coursemodule = $assigncm->id;
|
|
|
150 |
$expecteddata->section = $cw->section;
|
|
|
151 |
$expecteddata->visible = $assigncm->visible;
|
|
|
152 |
$expecteddata->visibleoncoursepage = $assigncm->visibleoncoursepage;
|
|
|
153 |
$expecteddata->cmidnumber = $assigncm->idnumber;
|
|
|
154 |
$expecteddata->groupmode = groups_get_activity_groupmode($cm);
|
|
|
155 |
$expecteddata->groupingid = $assigncm->groupingid;
|
|
|
156 |
$expecteddata->course = $course->id;
|
|
|
157 |
$expecteddata->module = $module->id;
|
|
|
158 |
$expecteddata->modulename = $module->name;
|
|
|
159 |
$expecteddata->instance = $assigncm->instance;
|
|
|
160 |
$expecteddata->completion = $assigncm->completion;
|
|
|
161 |
$expecteddata->completionview = $assigncm->completionview;
|
|
|
162 |
$expecteddata->completionexpected = $assigncm->completionexpected;
|
|
|
163 |
$expecteddata->completionusegrade = is_null($assigncm->completiongradeitemnumber) ? 0 : 1;
|
|
|
164 |
$expecteddata->completionpassgrade = $assigncm->completionpassgrade;
|
|
|
165 |
$expecteddata->completiongradeitemnumber = null;
|
|
|
166 |
$expecteddata->showdescription = $assigncm->showdescription;
|
|
|
167 |
$expecteddata->downloadcontent = $assigncm->downloadcontent;
|
|
|
168 |
$expecteddata->tags = \core_tag_tag::get_item_tags_array('core', 'course_modules', $assigncm->id);
|
|
|
169 |
$expecteddata->lang = null;
|
|
|
170 |
$expecteddata->availabilityconditionsjson = null;
|
|
|
171 |
$expecteddata->advancedgradingmethod_submissions = null;
|
|
|
172 |
if ($items = \grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => 'assign',
|
|
|
173 |
'iteminstance' => $assign->id, 'courseid' => $course->id))) {
|
|
|
174 |
// set category if present
|
|
|
175 |
$gradecat = false;
|
|
|
176 |
foreach ($items as $item) {
|
|
|
177 |
if ($gradecat === false) {
|
|
|
178 |
$gradecat = $item->categoryid;
|
|
|
179 |
continue;
|
|
|
180 |
}
|
|
|
181 |
if ($gradecat != $item->categoryid) {
|
|
|
182 |
//mixed categories
|
|
|
183 |
$gradecat = false;
|
|
|
184 |
break;
|
|
|
185 |
}
|
|
|
186 |
}
|
|
|
187 |
if ($gradecat !== false) {
|
|
|
188 |
// do not set if mixed categories present
|
|
|
189 |
$expecteddata->gradecat = $gradecat;
|
|
|
190 |
}
|
|
|
191 |
}
|
|
|
192 |
$expecteddata->gradepass = '0.00';
|
|
|
193 |
$expecteddata->completionpassgrade = $assigncm->completionpassgrade;
|
|
|
194 |
|
|
|
195 |
// Unset untestable.
|
|
|
196 |
unset($expecteddata->cmid);
|
|
|
197 |
unset($data->introeditor);
|
|
|
198 |
unset($data->_advancedgradingdata);
|
|
|
199 |
|
|
|
200 |
$this->assertEquals($expecteddata, $data);
|
|
|
201 |
|
|
|
202 |
// Create a viewer user. Not able to edit.
|
|
|
203 |
$viewer = self::getDataGenerator()->create_user();
|
|
|
204 |
$this->getDataGenerator()->enrol_user($viewer->id, $course->id);
|
|
|
205 |
$this->setUser($viewer);
|
|
|
206 |
$this->expectException('required_capability_exception');
|
|
|
207 |
get_moduleinfo_data($assigncm, $course);
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
/**
|
|
|
211 |
* Test add_moduleinfo (only beforemod parameter for now).
|
|
|
212 |
*
|
|
|
213 |
* @covers \add_moduleinfo
|
|
|
214 |
*/
|
11 |
efrain |
215 |
public function test_add_moduleinfo(): void {
|
1 |
efrain |
216 |
global $DB;
|
|
|
217 |
$this->resetAfterTest(true);
|
|
|
218 |
|
|
|
219 |
$this->setAdminUser();
|
|
|
220 |
$course = self::getDataGenerator()->create_course();
|
|
|
221 |
$labelmodule = $DB->get_record('modules', ['name' => 'label'], '*', MUST_EXIST);
|
|
|
222 |
$sectionnumber = 1;
|
|
|
223 |
$modules = [];
|
|
|
224 |
$moduleinfo = [];
|
|
|
225 |
|
|
|
226 |
for ($i = 0; $i < 4; $i++) {
|
|
|
227 |
$modules[$i] = self::getDataGenerator()->create_module('label', ['course' => $course->id, 'section' => $sectionnumber]);
|
|
|
228 |
$modulescm[$i] = get_coursemodule_from_id('label', $modules[$i]->cmid);
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
$modules[4] = self::getDataGenerator()->create_module('label', ['course' => $course->id, 'section' => $sectionnumber + 1]);
|
|
|
232 |
$modulescm[4] = get_coursemodule_from_id('label', $modules[4]->cmid);
|
|
|
233 |
|
|
|
234 |
// The beforemod attribute is not set, should be null afterwards.
|
|
|
235 |
list($module, $context, $cw, $cm, $data) = prepare_new_moduleinfo_data($course, $labelmodule->name, $sectionnumber);
|
|
|
236 |
$moduleinfo[0] = add_moduleinfo($data, $course);
|
|
|
237 |
$this->assertEquals(null, $moduleinfo[0]->beforemod);
|
|
|
238 |
|
|
|
239 |
// Insert before the first module.
|
|
|
240 |
list($module, $context, $cw, $cm, $data) = prepare_new_moduleinfo_data($course, $labelmodule->name, $sectionnumber);
|
|
|
241 |
$data->beforemod = $modulescm[0]->id;
|
|
|
242 |
$moduleinfo[1] = add_moduleinfo($data, $course);
|
|
|
243 |
$this->assertEquals($modulescm[0]->id, $moduleinfo[1]->beforemod);
|
|
|
244 |
|
|
|
245 |
// Insert between the two last modules.
|
|
|
246 |
list($module, $context, $cw, $cm, $data) = prepare_new_moduleinfo_data($course, $labelmodule->name, $sectionnumber);
|
|
|
247 |
$data->beforemod = $modulescm[3]->id;
|
|
|
248 |
$moduleinfo[2] = add_moduleinfo($data, $course);
|
|
|
249 |
$this->assertEquals($modulescm[3]->id, $moduleinfo[2]->beforemod);
|
|
|
250 |
|
|
|
251 |
// Insert before a not existing module.
|
|
|
252 |
course_delete_module($modulescm[2]->id);
|
|
|
253 |
|
|
|
254 |
list($module, $context, $cw, $cm, $data) = prepare_new_moduleinfo_data($course, $labelmodule->name, $sectionnumber);
|
|
|
255 |
$data->beforemod = $modulescm[2]->id;
|
|
|
256 |
$moduleinfo[3] = add_moduleinfo($data, $course);
|
|
|
257 |
$this->assertEquals($modulescm[2]->id, $moduleinfo[3]->beforemod);
|
|
|
258 |
|
|
|
259 |
// Insert before a module that is in another section.
|
|
|
260 |
list($module, $context, $cw, $cm, $data) = prepare_new_moduleinfo_data($course, $labelmodule->name, $sectionnumber);
|
|
|
261 |
$data->beforemod = $modulescm[4]->id;
|
|
|
262 |
$moduleinfo[4] = add_moduleinfo($data, $course);
|
|
|
263 |
$this->assertEquals($modulescm[4]->id, $moduleinfo[4]->beforemod);
|
|
|
264 |
|
|
|
265 |
$modinfo = get_fast_modinfo($course);
|
|
|
266 |
|
|
|
267 |
$expectedorder = [
|
|
|
268 |
$moduleinfo[1]->coursemodule,
|
|
|
269 |
$modulescm[0]->id,
|
|
|
270 |
$modulescm[1]->id,
|
|
|
271 |
$moduleinfo[2]->coursemodule,
|
|
|
272 |
$modulescm[3]->id,
|
|
|
273 |
$moduleinfo[0]->coursemodule,
|
|
|
274 |
$moduleinfo[3]->coursemodule,
|
|
|
275 |
$moduleinfo[4]->coursemodule,
|
|
|
276 |
];
|
|
|
277 |
|
|
|
278 |
$this->assertEquals($expectedorder, $modinfo->get_sections()[$sectionnumber]);
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
/**
|
|
|
282 |
* Test for can_add_moduleinfo on a non-existing module.
|
|
|
283 |
*
|
|
|
284 |
* @covers \can_add_moduleinfo
|
|
|
285 |
*/
|
|
|
286 |
public function test_can_add_moduleinfo_invalid_module(): void {
|
|
|
287 |
global $DB;
|
|
|
288 |
$this->resetAfterTest(true);
|
|
|
289 |
|
|
|
290 |
$course = $this->getDataGenerator()->create_course(
|
|
|
291 |
['numsections' => 2, 'enablecompletion' => 1],
|
|
|
292 |
['createsections' => true]
|
|
|
293 |
);
|
|
|
294 |
|
|
|
295 |
$modinfo = get_fast_modinfo($course);
|
|
|
296 |
$section = $modinfo->get_section_info(2);
|
|
|
297 |
|
|
|
298 |
$this->setAdminUser();
|
|
|
299 |
|
|
|
300 |
$this->expectException(\dml_missing_record_exception::class);
|
|
|
301 |
|
|
|
302 |
can_add_moduleinfo($course, 'non-existent-module!', $section->section);
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
/**
|
|
|
306 |
* Test for can_add_moduleinfo when the user does not have addinstance capability.
|
|
|
307 |
*
|
|
|
308 |
* @covers \can_add_moduleinfo
|
|
|
309 |
*/
|
|
|
310 |
public function test_can_add_moduleinfo_deny_add_instance(): void {
|
|
|
311 |
global $DB;
|
|
|
312 |
$this->resetAfterTest(true);
|
|
|
313 |
|
|
|
314 |
$course = $this->getDataGenerator()->create_course(
|
|
|
315 |
['numsections' => 2, 'enablecompletion' => 1],
|
|
|
316 |
['createsections' => true]
|
|
|
317 |
);
|
|
|
318 |
|
|
|
319 |
$modinfo = get_fast_modinfo($course);
|
|
|
320 |
$section = $modinfo->get_section_info(2);
|
|
|
321 |
|
|
|
322 |
// The can_add_moduleinfo uses course_allowed_module to check if the module is allowed in the course.
|
|
|
323 |
// This method uses capabilities like the specific module addinstance capability.
|
|
|
324 |
$teacherrole = $DB->get_record('role', ['shortname' => 'editingteacher'], '*', MUST_EXIST);
|
|
|
325 |
role_change_permission(
|
|
|
326 |
$teacherrole->id,
|
|
|
327 |
\context_course::instance($course->id),
|
|
|
328 |
'mod/label:addinstance',
|
|
|
329 |
CAP_PROHIBIT
|
|
|
330 |
);
|
|
|
331 |
|
|
|
332 |
$user = $this->getDataGenerator()->create_user();
|
|
|
333 |
$this->getDataGenerator()->enrol_user($user->id, $course->id, 'editingteacher');
|
|
|
334 |
$this->setUser($user);
|
|
|
335 |
|
|
|
336 |
$this->expectException(\moodle_exception::class);
|
|
|
337 |
|
|
|
338 |
can_add_moduleinfo($course, 'label', $section->section);
|
|
|
339 |
}
|
|
|
340 |
|
|
|
341 |
/**
|
|
|
342 |
* Test for can_add_moduleinfo.
|
|
|
343 |
*
|
|
|
344 |
* @dataProvider provider_can_add_moduleinfo
|
|
|
345 |
* @covers \can_add_moduleinfo
|
|
|
346 |
* @param string $rolename
|
|
|
347 |
* @param bool $hascapability
|
|
|
348 |
*/
|
|
|
349 |
public function test_can_add_moduleinfo_capability(string $rolename, bool $hascapability): void {
|
|
|
350 |
global $DB;
|
|
|
351 |
$this->resetAfterTest(true);
|
|
|
352 |
|
|
|
353 |
$module = $DB->get_record('modules', ['name' => 'label'], '*', MUST_EXIST);
|
|
|
354 |
|
|
|
355 |
$course = $this->getDataGenerator()->create_course(
|
|
|
356 |
['numsections' => 2, 'enablecompletion' => 1],
|
|
|
357 |
['createsections' => true]
|
|
|
358 |
);
|
|
|
359 |
|
|
|
360 |
$modinfo = get_fast_modinfo($course);
|
|
|
361 |
$section = $modinfo->get_section_info(2);
|
|
|
362 |
|
|
|
363 |
$user = $this->getDataGenerator()->create_user();
|
|
|
364 |
$this->getDataGenerator()->enrol_user($user->id, $course->id, $rolename);
|
|
|
365 |
$this->setUser($user);
|
|
|
366 |
|
|
|
367 |
if (!$hascapability) {
|
|
|
368 |
$this->expectException(\required_capability_exception::class);
|
|
|
369 |
}
|
|
|
370 |
|
|
|
371 |
$result = can_add_moduleinfo($course, 'label', $section->section);
|
|
|
372 |
|
|
|
373 |
$this->assertEquals($module, $result[0]);
|
|
|
374 |
$this->assertEquals(
|
|
|
375 |
\context_course::instance($course->id),
|
|
|
376 |
$result[1]
|
|
|
377 |
);
|
|
|
378 |
$this->assertEquals($section->id, $result[2]->id);
|
|
|
379 |
}
|
|
|
380 |
|
|
|
381 |
/**
|
|
|
382 |
* Test for can_add_moduleinfo returns true on a delegate section.
|
|
|
383 |
*
|
|
|
384 |
* @dataProvider provider_can_add_moduleinfo
|
|
|
385 |
* @covers \can_add_moduleinfo
|
|
|
386 |
* @param string $rolename
|
|
|
387 |
* @param bool $hascapability
|
|
|
388 |
*/
|
|
|
389 |
public function test_can_add_moduleinfo_delegate_section(string $rolename, bool $hascapability): void {
|
|
|
390 |
global $DB;
|
|
|
391 |
$this->resetAfterTest(true);
|
|
|
392 |
|
|
|
393 |
$module = $DB->get_record('modules', ['name' => 'label'], '*', MUST_EXIST);
|
|
|
394 |
|
|
|
395 |
$course = $this->getDataGenerator()->create_course(
|
|
|
396 |
['numsections' => 2, 'enablecompletion' => 1],
|
|
|
397 |
['createsections' => true]
|
|
|
398 |
);
|
|
|
399 |
|
|
|
400 |
$section = formatactions::section($course)->create_delegated('mod_label', 0);
|
|
|
401 |
|
|
|
402 |
$modinfo = get_fast_modinfo($course);
|
|
|
403 |
$this->assertCount(4, $modinfo->get_section_info_all());
|
|
|
404 |
$this->assertCount(3, $modinfo->get_listed_section_info_all());
|
|
|
405 |
|
|
|
406 |
$user = $this->getDataGenerator()->create_user();
|
|
|
407 |
$this->getDataGenerator()->enrol_user($user->id, $course->id, $rolename);
|
|
|
408 |
$this->setUser($user);
|
|
|
409 |
|
|
|
410 |
if (!$hascapability) {
|
|
|
411 |
$this->expectException(\required_capability_exception::class);
|
|
|
412 |
}
|
|
|
413 |
|
|
|
414 |
$result = can_add_moduleinfo($course, 'label', $section->section);
|
|
|
415 |
|
|
|
416 |
$this->assertEquals($module, $result[0]);
|
|
|
417 |
$this->assertEquals(
|
|
|
418 |
\context_course::instance($course->id),
|
|
|
419 |
$result[1]
|
|
|
420 |
);
|
|
|
421 |
$this->assertEquals($section->id, $result[2]->id);
|
|
|
422 |
|
|
|
423 |
// Validate no section has been created.
|
|
|
424 |
$modinfo = get_fast_modinfo($course);
|
|
|
425 |
$this->assertCount(4, $modinfo->get_section_info_all());
|
|
|
426 |
$this->assertCount(3, $modinfo->get_listed_section_info_all());
|
|
|
427 |
$this->assertEquals(
|
|
|
428 |
$section->section,
|
|
|
429 |
$modinfo->get_section_info_by_id($section->id)->section
|
|
|
430 |
);
|
|
|
431 |
}
|
|
|
432 |
|
|
|
433 |
/**
|
|
|
434 |
* Data provider for test_can_add_moduleinfo.
|
|
|
435 |
* @return array
|
|
|
436 |
*/
|
|
|
437 |
public static function provider_can_add_moduleinfo(): array {
|
|
|
438 |
return [
|
|
|
439 |
'Editing teacher' => [
|
|
|
440 |
'rolename' => 'editingteacher',
|
|
|
441 |
'hascapability' => true,
|
|
|
442 |
],
|
|
|
443 |
'Manager' => [
|
|
|
444 |
'rolename' => 'manager',
|
|
|
445 |
'hascapability' => true,
|
|
|
446 |
],
|
|
|
447 |
'Course creator' => [
|
|
|
448 |
'rolename' => 'coursecreator',
|
|
|
449 |
'hascapability' => false,
|
|
|
450 |
],
|
|
|
451 |
'Non-editing teacher' => [
|
|
|
452 |
'rolename' => 'teacher',
|
|
|
453 |
'hascapability' => false,
|
|
|
454 |
],
|
|
|
455 |
'Student' => [
|
|
|
456 |
'rolename' => 'student',
|
|
|
457 |
'hascapability' => false,
|
|
|
458 |
],
|
|
|
459 |
'Guest' => [
|
|
|
460 |
'rolename' => 'guest',
|
|
|
461 |
'hascapability' => false,
|
|
|
462 |
],
|
|
|
463 |
];
|
|
|
464 |
}
|
|
|
465 |
}
|