2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Course related unit tests
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 require_once($CFG->dirroot
. '/course/lib.php');
30 require_once($CFG->dirroot
. '/course/tests/fixtures/course_capability_assignment.php');
31 require_once($CFG->dirroot
. '/enrol/imsenterprise/tests/imsenterprise_test.php');
33 class core_course_courselib_testcase
extends advanced_testcase
{
36 * Set forum specific test values for calling create_module().
38 * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
40 private function forum_create_set_values(&$moduleinfo) {
41 // Completion specific to forum - optional.
42 $moduleinfo->completionposts
= 3;
43 $moduleinfo->completiondiscussions
= 1;
44 $moduleinfo->completionreplies
= 2;
46 // Specific values to the Forum module.
47 $moduleinfo->forcesubscribe
= FORUM_INITIALSUBSCRIBE
;
48 $moduleinfo->type
= 'single';
49 $moduleinfo->trackingtype
= FORUM_TRACKING_FORCED
;
50 $moduleinfo->maxbytes
= 10240;
51 $moduleinfo->maxattachments
= 2;
53 // Post threshold for blocking - specific to forum.
54 $moduleinfo->blockperiod
= 60*60*24;
55 $moduleinfo->blockafter
= 10;
56 $moduleinfo->warnafter
= 5;
60 * Execute test asserts on the saved DB data by create_module($forum).
62 * @param object $moduleinfo - the specific forum values that were used to create a forum.
63 * @param object $dbmodinstance - the DB values of the created forum.
65 private function forum_create_run_asserts($moduleinfo, $dbmodinstance) {
66 // Compare values specific to forums.
67 $this->assertEquals($moduleinfo->forcesubscribe
, $dbmodinstance->forcesubscribe
);
68 $this->assertEquals($moduleinfo->type
, $dbmodinstance->type
);
69 $this->assertEquals($moduleinfo->assessed
, $dbmodinstance->assessed
);
70 $this->assertEquals($moduleinfo->completionposts
, $dbmodinstance->completionposts
);
71 $this->assertEquals($moduleinfo->completiondiscussions
, $dbmodinstance->completiondiscussions
);
72 $this->assertEquals($moduleinfo->completionreplies
, $dbmodinstance->completionreplies
);
73 $this->assertEquals($moduleinfo->scale
, $dbmodinstance->scale
);
74 $this->assertEquals($moduleinfo->assesstimestart
, $dbmodinstance->assesstimestart
);
75 $this->assertEquals($moduleinfo->assesstimefinish
, $dbmodinstance->assesstimefinish
);
76 $this->assertEquals($moduleinfo->rsstype
, $dbmodinstance->rsstype
);
77 $this->assertEquals($moduleinfo->rssarticles
, $dbmodinstance->rssarticles
);
78 $this->assertEquals($moduleinfo->trackingtype
, $dbmodinstance->trackingtype
);
79 $this->assertEquals($moduleinfo->maxbytes
, $dbmodinstance->maxbytes
);
80 $this->assertEquals($moduleinfo->maxattachments
, $dbmodinstance->maxattachments
);
81 $this->assertEquals($moduleinfo->blockperiod
, $dbmodinstance->blockperiod
);
82 $this->assertEquals($moduleinfo->blockafter
, $dbmodinstance->blockafter
);
83 $this->assertEquals($moduleinfo->warnafter
, $dbmodinstance->warnafter
);
87 * Set assign module specific test values for calling create_module().
89 * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
91 private function assign_create_set_values(&$moduleinfo) {
92 // Specific values to the Assign module.
93 $moduleinfo->alwaysshowdescription
= true;
94 $moduleinfo->submissiondrafts
= true;
95 $moduleinfo->requiresubmissionstatement
= true;
96 $moduleinfo->sendnotifications
= true;
97 $moduleinfo->sendlatenotifications
= true;
98 $moduleinfo->duedate
= time() +
(7 * 24 * 3600);
99 $moduleinfo->cutoffdate
= time() +
(7 * 24 * 3600);
100 $moduleinfo->gradingduedate
= time() +
(7 * 24 * 3600);
101 $moduleinfo->allowsubmissionsfromdate
= time();
102 $moduleinfo->teamsubmission
= true;
103 $moduleinfo->requireallteammemberssubmit
= true;
104 $moduleinfo->teamsubmissiongroupingid
= true;
105 $moduleinfo->blindmarking
= true;
106 $moduleinfo->markingworkflow
= true;
107 $moduleinfo->markingallocation
= true;
108 $moduleinfo->assignsubmission_onlinetext_enabled
= true;
109 $moduleinfo->assignsubmission_file_enabled
= true;
110 $moduleinfo->assignsubmission_file_maxfiles
= 1;
111 $moduleinfo->assignsubmission_file_maxsizebytes
= 1000000;
112 $moduleinfo->assignsubmission_comments_enabled
= true;
113 $moduleinfo->assignfeedback_comments_enabled
= true;
114 $moduleinfo->assignfeedback_offline_enabled
= true;
115 $moduleinfo->assignfeedback_file_enabled
= true;
118 $gradingmethods = grading_manager
::available_methods();
119 $moduleinfo->advancedgradingmethod_submissions
= current(array_keys($gradingmethods));
123 * Execute test asserts on the saved DB data by create_module($assign).
125 * @param object $moduleinfo - the specific assign module values that were used to create an assign module.
126 * @param object $dbmodinstance - the DB values of the created assign module.
128 private function assign_create_run_asserts($moduleinfo, $dbmodinstance) {
131 $this->assertEquals($moduleinfo->alwaysshowdescription
, $dbmodinstance->alwaysshowdescription
);
132 $this->assertEquals($moduleinfo->submissiondrafts
, $dbmodinstance->submissiondrafts
);
133 $this->assertEquals($moduleinfo->requiresubmissionstatement
, $dbmodinstance->requiresubmissionstatement
);
134 $this->assertEquals($moduleinfo->sendnotifications
, $dbmodinstance->sendnotifications
);
135 $this->assertEquals($moduleinfo->duedate
, $dbmodinstance->duedate
);
136 $this->assertEquals($moduleinfo->cutoffdate
, $dbmodinstance->cutoffdate
);
137 $this->assertEquals($moduleinfo->allowsubmissionsfromdate
, $dbmodinstance->allowsubmissionsfromdate
);
138 $this->assertEquals($moduleinfo->teamsubmission
, $dbmodinstance->teamsubmission
);
139 $this->assertEquals($moduleinfo->requireallteammemberssubmit
, $dbmodinstance->requireallteammemberssubmit
);
140 $this->assertEquals($moduleinfo->teamsubmissiongroupingid
, $dbmodinstance->teamsubmissiongroupingid
);
141 $this->assertEquals($moduleinfo->blindmarking
, $dbmodinstance->blindmarking
);
142 $this->assertEquals($moduleinfo->markingworkflow
, $dbmodinstance->markingworkflow
);
143 $this->assertEquals($moduleinfo->markingallocation
, $dbmodinstance->markingallocation
);
144 // The goal not being to fully test assign_add_instance() we'll stop here for the assign tests - to avoid too many DB queries.
147 $cm = get_coursemodule_from_instance('assign', $dbmodinstance->id
);
148 $contextmodule = context_module
::instance($cm->id
);
149 $advancedgradingmethod = $DB->get_record('grading_areas',
150 array('contextid' => $contextmodule->id
,
151 'activemethod' => $moduleinfo->advancedgradingmethod_submissions
));
152 $this->assertEquals($moduleinfo->advancedgradingmethod_submissions
, $advancedgradingmethod);
156 * Run some asserts test for a specific module for the function create_module().
158 * The function has been created (and is called) for $this->test_create_module().
159 * Note that the call to MODULE_create_set_values and MODULE_create_run_asserts are done after the common set values/run asserts.
160 * So if you want, you can overwrite the default values/asserts in the respective functions.
161 * @param string $modulename Name of the module ('forum', 'assign', 'book'...).
163 private function create_specific_module_test($modulename) {
166 $this->resetAfterTest(true);
168 $this->setAdminUser();
170 // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
171 require_once($CFG->dirroot
.'/mod/'. $modulename .'/lib.php');
173 // Enable avaibility.
174 // If not enabled all conditional fields will be ignored.
175 set_config('enableavailability', 1);
177 // Enable course completion.
178 // If not enabled all completion settings will be ignored.
179 set_config('enablecompletion', COMPLETION_ENABLED
);
181 // Enable forum RSS feeds.
182 set_config('enablerssfeeds', 1);
183 set_config('forum_enablerssfeeds', 1);
185 $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED
),
186 array('createsections'=>true));
188 $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
190 // Create assign module instance for test.
191 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
192 $params['course'] = $course->id
;
193 $instance = $generator->create_instance($params);
194 $assigncm = get_coursemodule_from_instance('assign', $instance->id
);
196 // Module test values.
197 $moduleinfo = new stdClass();
199 // Always mandatory generic values to any module.
200 $moduleinfo->modulename
= $modulename;
201 $moduleinfo->section
= 1; // This is the section number in the course. Not the section id in the database.
202 $moduleinfo->course
= $course->id
;
203 $moduleinfo->groupingid
= $grouping->id
;
204 $moduleinfo->visible
= true;
205 $moduleinfo->visibleoncoursepage
= true;
207 // Sometimes optional generic values for some modules.
208 $moduleinfo->name
= 'My test module';
209 $moduleinfo->showdescription
= 1; // standard boolean
210 require_once($CFG->libdir
. '/gradelib.php');
211 $gradecats = grade_get_categories_menu($moduleinfo->course
, false);
212 $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
213 $moduleinfo->gradecat
= $gradecatid;
214 $moduleinfo->groupmode
= VISIBLEGROUPS
;
215 $moduleinfo->cmidnumber
= 'idnumber_XXX';
217 // Completion common to all module.
218 $moduleinfo->completion
= COMPLETION_TRACKING_AUTOMATIC
;
219 $moduleinfo->completionview
= COMPLETION_VIEW_REQUIRED
;
220 $moduleinfo->completiongradeitemnumber
= 1;
221 $moduleinfo->completionexpected
= time() +
(7 * 24 * 3600);
223 // Conditional activity.
224 $moduleinfo->availability
= '{"op":"&","showc":[true,true],"c":[' .
225 '{"type":"date","d":">=","t":' . time() . '},' .
226 '{"type":"date","d":"<","t":' . (time() +
(7 * 24 * 3600)) . '}' .
228 $coursegradeitem = grade_item
::fetch_course_item($moduleinfo->course
); //the activity will become available only when the user reach some grade into the course itself.
229 $moduleinfo->conditiongradegroup
= array(array('conditiongradeitemid' => $coursegradeitem->id
, 'conditiongrademin' => 10, 'conditiongrademax' => 80));
230 $moduleinfo->conditionfieldgroup
= array(array('conditionfield' => 'email', 'conditionfieldoperator' => \availability_profile\condition
::OP_CONTAINS
, 'conditionfieldvalue' => '@'));
231 $moduleinfo->conditioncompletiongroup
= array(array('conditionsourcecmid' => $assigncm->id
, 'conditionrequiredcompletion' => COMPLETION_COMPLETE
)); // "conditionsourcecmid == 0" => none
233 // Grading and Advanced grading.
234 require_once($CFG->dirroot
. '/rating/lib.php');
235 $moduleinfo->assessed
= RATING_AGGREGATE_AVERAGE
;
236 $moduleinfo->scale
= 10; // Note: it could be minus (for specific course scale). It is a signed number.
237 $moduleinfo->assesstimestart
= time();
238 $moduleinfo->assesstimefinish
= time() +
(7 * 24 * 3600);
241 $moduleinfo->rsstype
= 2;
242 $moduleinfo->rssarticles
= 10;
244 // Optional intro editor (depends of module).
246 file_prepare_draft_area($draftid_editor, null, null, null, null);
247 $moduleinfo->introeditor
= array('text' => 'This is a module', 'format' => FORMAT_HTML
, 'itemid' => $draftid_editor);
249 // Following is the advanced grading method area called 'submissions' for the 'assign' module.
250 if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE
, false) && !plugin_supports('mod', $modulename, FEATURE_RATE
, false)) {
251 $moduleinfo->grade
= 100;
254 // Plagiarism form values.
255 // No plagiarism plugin installed by default. Use this space to make your own test.
257 // Values specific to the module.
258 $modulesetvalues = $modulename.'_create_set_values';
259 $this->$modulesetvalues($moduleinfo);
261 // Create the module.
262 $result = create_module($moduleinfo);
264 // Retrieve the module info.
265 $dbmodinstance = $DB->get_record($moduleinfo->modulename
, array('id' => $result->instance
));
266 $dbcm = get_coursemodule_from_instance($moduleinfo->modulename
, $result->instance
);
267 // We passed the course section number to create_courses but $dbcm contain the section id.
268 // We need to retrieve the db course section number.
269 $section = $DB->get_record('course_sections', array('course' => $dbcm->course
, 'id' => $dbcm->section
));
270 // Retrieve the grade item.
271 $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course
,
272 'iteminstance' => $dbmodinstance->id
, 'itemmodule' => $moduleinfo->modulename
));
274 // Compare the values common to all module instances.
275 $this->assertEquals($moduleinfo->modulename
, $dbcm->modname
);
276 $this->assertEquals($moduleinfo->section
, $section->section
);
277 $this->assertEquals($moduleinfo->course
, $dbcm->course
);
278 $this->assertEquals($moduleinfo->groupingid
, $dbcm->groupingid
);
279 $this->assertEquals($moduleinfo->visible
, $dbcm->visible
);
280 $this->assertEquals($moduleinfo->completion
, $dbcm->completion
);
281 $this->assertEquals($moduleinfo->completionview
, $dbcm->completionview
);
282 $this->assertEquals($moduleinfo->completiongradeitemnumber
, $dbcm->completiongradeitemnumber
);
283 $this->assertEquals($moduleinfo->completionexpected
, $dbcm->completionexpected
);
284 $this->assertEquals($moduleinfo->availability
, $dbcm->availability
);
285 $this->assertEquals($moduleinfo->showdescription
, $dbcm->showdescription
);
286 $this->assertEquals($moduleinfo->groupmode
, $dbcm->groupmode
);
287 $this->assertEquals($moduleinfo->cmidnumber
, $dbcm->idnumber
);
288 $this->assertEquals($moduleinfo->gradecat
, $gradeitem->categoryid
);
290 // Optional grade testing.
291 if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE
, false) && !plugin_supports('mod', $modulename, FEATURE_RATE
, false)) {
292 $this->assertEquals($moduleinfo->grade
, $dbmodinstance->grade
);
295 // Some optional (but quite common) to some module.
296 $this->assertEquals($moduleinfo->name
, $dbmodinstance->name
);
297 $this->assertEquals($moduleinfo->intro
, $dbmodinstance->intro
);
298 $this->assertEquals($moduleinfo->introformat
, $dbmodinstance->introformat
);
300 // Test specific to the module.
301 $modulerunasserts = $modulename.'_create_run_asserts';
302 $this->$modulerunasserts($moduleinfo, $dbmodinstance);
307 * Test create_module() for multiple modules defined in the $modules array (first declaration of the function).
309 public function test_create_module() {
310 // Add the module name you want to test here.
311 // Create the match MODULENAME_create_set_values() and MODULENAME_create_run_asserts().
312 $modules = array('forum', 'assign');
314 foreach ($modules as $modulename) {
315 $this->create_specific_module_test($modulename);
320 * Test update_module() for multiple modules defined in the $modules array (first declaration of the function).
322 public function test_update_module() {
323 // Add the module name you want to test here.
324 // Create the match MODULENAME_update_set_values() and MODULENAME_update_run_asserts().
325 $modules = array('forum');
327 foreach ($modules as $modulename) {
328 $this->update_specific_module_test($modulename);
333 * Set forum specific test values for calling update_module().
335 * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
337 private function forum_update_set_values(&$moduleinfo) {
338 // Completion specific to forum - optional.
339 $moduleinfo->completionposts
= 3;
340 $moduleinfo->completiondiscussions
= 1;
341 $moduleinfo->completionreplies
= 2;
343 // Specific values to the Forum module.
344 $moduleinfo->forcesubscribe
= FORUM_INITIALSUBSCRIBE
;
345 $moduleinfo->type
= 'single';
346 $moduleinfo->trackingtype
= FORUM_TRACKING_FORCED
;
347 $moduleinfo->maxbytes
= 10240;
348 $moduleinfo->maxattachments
= 2;
350 // Post threshold for blocking - specific to forum.
351 $moduleinfo->blockperiod
= 60*60*24;
352 $moduleinfo->blockafter
= 10;
353 $moduleinfo->warnafter
= 5;
357 * Execute test asserts on the saved DB data by update_module($forum).
359 * @param object $moduleinfo - the specific forum values that were used to update a forum.
360 * @param object $dbmodinstance - the DB values of the updated forum.
362 private function forum_update_run_asserts($moduleinfo, $dbmodinstance) {
363 // Compare values specific to forums.
364 $this->assertEquals($moduleinfo->forcesubscribe
, $dbmodinstance->forcesubscribe
);
365 $this->assertEquals($moduleinfo->type
, $dbmodinstance->type
);
366 $this->assertEquals($moduleinfo->assessed
, $dbmodinstance->assessed
);
367 $this->assertEquals($moduleinfo->completionposts
, $dbmodinstance->completionposts
);
368 $this->assertEquals($moduleinfo->completiondiscussions
, $dbmodinstance->completiondiscussions
);
369 $this->assertEquals($moduleinfo->completionreplies
, $dbmodinstance->completionreplies
);
370 $this->assertEquals($moduleinfo->scale
, $dbmodinstance->scale
);
371 $this->assertEquals($moduleinfo->assesstimestart
, $dbmodinstance->assesstimestart
);
372 $this->assertEquals($moduleinfo->assesstimefinish
, $dbmodinstance->assesstimefinish
);
373 $this->assertEquals($moduleinfo->rsstype
, $dbmodinstance->rsstype
);
374 $this->assertEquals($moduleinfo->rssarticles
, $dbmodinstance->rssarticles
);
375 $this->assertEquals($moduleinfo->trackingtype
, $dbmodinstance->trackingtype
);
376 $this->assertEquals($moduleinfo->maxbytes
, $dbmodinstance->maxbytes
);
377 $this->assertEquals($moduleinfo->maxattachments
, $dbmodinstance->maxattachments
);
378 $this->assertEquals($moduleinfo->blockperiod
, $dbmodinstance->blockperiod
);
379 $this->assertEquals($moduleinfo->blockafter
, $dbmodinstance->blockafter
);
380 $this->assertEquals($moduleinfo->warnafter
, $dbmodinstance->warnafter
);
386 * Test a specific type of module.
388 * @param string $modulename - the module name to test
390 private function update_specific_module_test($modulename) {
393 $this->resetAfterTest(true);
395 $this->setAdminUser();
397 // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
398 require_once($CFG->dirroot
.'/mod/'. $modulename .'/lib.php');
400 // Enable avaibility.
401 // If not enabled all conditional fields will be ignored.
402 set_config('enableavailability', 1);
404 // Enable course completion.
405 // If not enabled all completion settings will be ignored.
406 set_config('enablecompletion', COMPLETION_ENABLED
);
408 // Enable forum RSS feeds.
409 set_config('enablerssfeeds', 1);
410 set_config('forum_enablerssfeeds', 1);
412 $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED
),
413 array('createsections'=>true));
415 $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id
));
417 // Create assign module instance for testing gradeitem.
418 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
419 $params['course'] = $course->id
;
420 $instance = $generator->create_instance($params);
421 $assigncm = get_coursemodule_from_instance('assign', $instance->id
);
423 // Create the test forum to update.
424 $initvalues = new stdClass();
425 $initvalues->introformat
= FORMAT_HTML
;
426 $initvalues->course
= $course->id
;
427 $forum = self
::getDataGenerator()->create_module('forum', $initvalues);
429 // Retrieve course module.
430 $cm = get_coursemodule_from_instance('forum', $forum->id
);
432 // Module test values.
433 $moduleinfo = new stdClass();
435 // Always mandatory generic values to any module.
436 $moduleinfo->coursemodule
= $cm->id
;
437 $moduleinfo->modulename
= $modulename;
438 $moduleinfo->course
= $course->id
;
439 $moduleinfo->groupingid
= $grouping->id
;
440 $moduleinfo->visible
= true;
441 $moduleinfo->visibleoncoursepage
= true;
443 // Sometimes optional generic values for some modules.
444 $moduleinfo->name
= 'My test module';
445 $moduleinfo->showdescription
= 1; // standard boolean
446 require_once($CFG->libdir
. '/gradelib.php');
447 $gradecats = grade_get_categories_menu($moduleinfo->course
, false);
448 $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
449 $moduleinfo->gradecat
= $gradecatid;
450 $moduleinfo->groupmode
= VISIBLEGROUPS
;
451 $moduleinfo->cmidnumber
= 'idnumber_XXX';
453 // Completion common to all module.
454 $moduleinfo->completion
= COMPLETION_TRACKING_AUTOMATIC
;
455 $moduleinfo->completionview
= COMPLETION_VIEW_REQUIRED
;
456 $moduleinfo->completiongradeitemnumber
= 1;
457 $moduleinfo->completionexpected
= time() +
(7 * 24 * 3600);
458 $moduleinfo->completionunlocked
= 1;
460 // Conditional activity.
461 $coursegradeitem = grade_item
::fetch_course_item($moduleinfo->course
); //the activity will become available only when the user reach some grade into the course itself.
462 $moduleinfo->availability
= json_encode(\core_availability\tree
::get_root_json(
463 array(\availability_date\condition
::get_json('>=', time()),
464 \availability_date\condition
::get_json('<', time() +
(7 * 24 * 3600)),
465 \availability_grade\condition
::get_json($coursegradeitem->id
, 10, 80),
466 \availability_profile\condition
::get_json(false, 'email', 'contains', '@'),
467 \availability_completion\condition
::get_json($assigncm->id
, COMPLETION_COMPLETE
)), '&'));
469 // Grading and Advanced grading.
470 require_once($CFG->dirroot
. '/rating/lib.php');
471 $moduleinfo->assessed
= RATING_AGGREGATE_AVERAGE
;
472 $moduleinfo->scale
= 10; // Note: it could be minus (for specific course scale). It is a signed number.
473 $moduleinfo->assesstimestart
= time();
474 $moduleinfo->assesstimefinish
= time() +
(7 * 24 * 3600);
477 $moduleinfo->rsstype
= 2;
478 $moduleinfo->rssarticles
= 10;
480 // Optional intro editor (depends of module).
482 file_prepare_draft_area($draftid_editor, null, null, null, null);
483 $moduleinfo->introeditor
= array('text' => 'This is a module', 'format' => FORMAT_HTML
, 'itemid' => $draftid_editor);
485 // Following is the advanced grading method area called 'submissions' for the 'assign' module.
486 if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE
, false) && !plugin_supports('mod', $modulename, FEATURE_RATE
, false)) {
487 $moduleinfo->grade
= 100;
489 // Plagiarism form values.
490 // No plagiarism plugin installed by default. Use this space to make your own test.
492 // Values specific to the module.
493 $modulesetvalues = $modulename.'_update_set_values';
494 $this->$modulesetvalues($moduleinfo);
496 // Create the module.
497 $result = update_module($moduleinfo);
499 // Retrieve the module info.
500 $dbmodinstance = $DB->get_record($moduleinfo->modulename
, array('id' => $result->instance
));
501 $dbcm = get_coursemodule_from_instance($moduleinfo->modulename
, $result->instance
);
502 // Retrieve the grade item.
503 $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course
,
504 'iteminstance' => $dbmodinstance->id
, 'itemmodule' => $moduleinfo->modulename
));
506 // Compare the values common to all module instances.
507 $this->assertEquals($moduleinfo->modulename
, $dbcm->modname
);
508 $this->assertEquals($moduleinfo->course
, $dbcm->course
);
509 $this->assertEquals($moduleinfo->groupingid
, $dbcm->groupingid
);
510 $this->assertEquals($moduleinfo->visible
, $dbcm->visible
);
511 $this->assertEquals($moduleinfo->completion
, $dbcm->completion
);
512 $this->assertEquals($moduleinfo->completionview
, $dbcm->completionview
);
513 $this->assertEquals($moduleinfo->completiongradeitemnumber
, $dbcm->completiongradeitemnumber
);
514 $this->assertEquals($moduleinfo->completionexpected
, $dbcm->completionexpected
);
515 $this->assertEquals($moduleinfo->availability
, $dbcm->availability
);
516 $this->assertEquals($moduleinfo->showdescription
, $dbcm->showdescription
);
517 $this->assertEquals($moduleinfo->groupmode
, $dbcm->groupmode
);
518 $this->assertEquals($moduleinfo->cmidnumber
, $dbcm->idnumber
);
519 $this->assertEquals($moduleinfo->gradecat
, $gradeitem->categoryid
);
521 // Optional grade testing.
522 if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE
, false) && !plugin_supports('mod', $modulename, FEATURE_RATE
, false)) {
523 $this->assertEquals($moduleinfo->grade
, $dbmodinstance->grade
);
526 // Some optional (but quite common) to some module.
527 $this->assertEquals($moduleinfo->name
, $dbmodinstance->name
);
528 $this->assertEquals($moduleinfo->intro
, $dbmodinstance->intro
);
529 $this->assertEquals($moduleinfo->introformat
, $dbmodinstance->introformat
);
531 // Test specific to the module.
532 $modulerunasserts = $modulename.'_update_run_asserts';
533 $this->$modulerunasserts($moduleinfo, $dbmodinstance);
538 * Data provider for course_delete module
540 * @return array An array of arrays contain test data
542 public function provider_course_delete_module() {
545 $data['assign'] = array('assign', array('duedate' => time()));
546 $data['quiz'] = array('quiz', array('duedate' => time()));
552 * Test the create_course function
554 public function test_create_course() {
556 $this->resetAfterTest(true);
557 $defaultcategory = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
559 $course = new stdClass();
560 $course->fullname
= 'Apu loves Unit Təsts';
561 $course->shortname
= 'Spread the lÅve';
562 $course->idnumber
= '123';
563 $course->summary
= 'Awesome!';
564 $course->summaryformat
= FORMAT_PLAIN
;
565 $course->format
= 'topics';
566 $course->newsitems
= 0;
567 $course->category
= $defaultcategory;
568 $original = (array) $course;
570 $created = create_course($course);
571 $context = context_course
::instance($created->id
);
573 // Compare original and created.
574 $this->assertEquals($original, array_intersect_key((array) $created, $original));
576 // Ensure default section is created.
577 $sectioncreated = $DB->record_exists('course_sections', array('course' => $created->id
, 'section' => 0));
578 $this->assertTrue($sectioncreated);
580 // Ensure that the shortname isn't duplicated.
582 $created = create_course($course);
583 $this->fail('Exception expected');
584 } catch (moodle_exception
$e) {
585 $this->assertSame(get_string('shortnametaken', 'error', $course->shortname
), $e->getMessage());
588 // Ensure that the idnumber isn't duplicated.
589 $course->shortname
.= '1';
591 $created = create_course($course);
592 $this->fail('Exception expected');
593 } catch (moodle_exception
$e) {
594 $this->assertSame(get_string('courseidnumbertaken', 'error', $course->idnumber
), $e->getMessage());
598 public function test_create_course_with_generator() {
600 $this->resetAfterTest(true);
601 $course = $this->getDataGenerator()->create_course();
603 // Ensure default section is created.
604 $sectioncreated = $DB->record_exists('course_sections', array('course' => $course->id
, 'section' => 0));
605 $this->assertTrue($sectioncreated);
608 public function test_create_course_sections() {
610 $this->resetAfterTest(true);
613 $course = $this->getDataGenerator()->create_course(
614 array('shortname' => 'GrowingCourse',
615 'fullname' => 'Growing Course',
616 'numsections' => $numsections),
617 array('createsections' => true));
619 // Ensure all 6 (0-5) sections were created and course content cache works properly
620 $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
621 $this->assertEquals(range(0, $numsections), $sectionscreated);
623 // this will do nothing, section already exists
624 $this->assertFalse(course_create_sections_if_missing($course, $numsections));
626 // this will create new section
627 $this->assertTrue(course_create_sections_if_missing($course, $numsections +
1));
629 // Ensure all 7 (0-6) sections were created and modinfo/sectioninfo cache works properly
630 $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
631 $this->assertEquals(range(0, $numsections +
1), $sectionscreated);
634 public function test_update_course() {
637 $this->resetAfterTest();
639 $defaultcategory = $DB->get_field_select('course_categories', 'MIN(id)', 'parent = 0');
641 $course = new stdClass();
642 $course->fullname
= 'Apu loves Unit Təsts';
643 $course->shortname
= 'test1';
644 $course->idnumber
= '1';
645 $course->summary
= 'Awesome!';
646 $course->summaryformat
= FORMAT_PLAIN
;
647 $course->format
= 'topics';
648 $course->newsitems
= 0;
649 $course->numsections
= 5;
650 $course->category
= $defaultcategory;
652 $created = create_course($course);
653 // Ensure the checks only work on idnumber/shortname that are not already ours.
654 update_course($created);
656 $course->shortname
= 'test2';
657 $course->idnumber
= '2';
659 $created2 = create_course($course);
661 // Test duplicate idnumber.
662 $created2->idnumber
= '1';
664 update_course($created2);
665 $this->fail('Expected exception when trying to update a course with duplicate idnumber');
666 } catch (moodle_exception
$e) {
667 $this->assertEquals(get_string('courseidnumbertaken', 'error', $created2->idnumber
), $e->getMessage());
670 // Test duplicate shortname.
671 $created2->idnumber
= '2';
672 $created2->shortname
= 'test1';
674 update_course($created2);
675 $this->fail('Expected exception when trying to update a course with a duplicate shortname');
676 } catch (moodle_exception
$e) {
677 $this->assertEquals(get_string('shortnametaken', 'error', $created2->shortname
), $e->getMessage());
681 public function test_update_course_section_time_modified() {
684 $this->resetAfterTest();
686 // Create the course with sections.
687 $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true));
688 $sections = $DB->get_records('course_sections', array('course' => $course->id
));
690 // Get the last section's time modified value.
691 $section = array_pop($sections);
692 $oldtimemodified = $section->timemodified
;
694 // Update the section.
695 $this->waitForSecond(); // Ensuring that the section update occurs at a different timestamp.
696 course_update_section($course, $section, array());
698 // Check that the time has changed.
699 $section = $DB->get_record('course_sections', array('id' => $section->id
));
700 $newtimemodified = $section->timemodified
;
701 $this->assertGreaterThan($oldtimemodified, $newtimemodified);
704 public function test_course_add_cm_to_section() {
706 $this->resetAfterTest(true);
708 // Create course with 1 section.
709 $course = $this->getDataGenerator()->create_course(
710 array('shortname' => 'GrowingCourse',
711 'fullname' => 'Growing Course',
713 array('createsections' => true));
716 rebuild_course_cache($course->id
, true);
718 // Create some cms for testing.
720 for ($i=0; $i<4; $i++
) {
721 $cmids[$i] = $DB->insert_record('course_modules', array('course' => $course->id
));
724 // Add it to section that exists.
725 course_add_cm_to_section($course, $cmids[0], 1);
727 // Check it got added to sequence.
728 $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id
, 'section' => 1));
729 $this->assertEquals($cmids[0], $sequence);
731 // Add a second, this time using courseid variant of parameters.
732 $coursecacherev = $DB->get_field('course', 'cacherev', array('id' => $course->id
));
733 course_add_cm_to_section($course->id
, $cmids[1], 1);
734 $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id
, 'section' => 1));
735 $this->assertEquals($cmids[0] . ',' . $cmids[1], $sequence);
737 // Check that modinfo cache was reset but not rebuilt (important for performance if calling repeatedly).
738 $this->assertGreaterThan($coursecacherev, $DB->get_field('course', 'cacherev', array('id' => $course->id
)));
739 $this->assertEmpty(cache
::make('core', 'coursemodinfo')->get($course->id
));
741 // Add one to section that doesn't exist (this might rebuild modinfo).
742 course_add_cm_to_section($course, $cmids[2], 2);
743 $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id
)));
744 $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id
, 'section' => 2));
745 $this->assertEquals($cmids[2], $sequence);
747 // Add using the 'before' option.
748 course_add_cm_to_section($course, $cmids[3], 2, $cmids[2]);
749 $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id
)));
750 $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id
, 'section' => 2));
751 $this->assertEquals($cmids[3] . ',' . $cmids[2], $sequence);
754 public function test_reorder_sections() {
756 $this->resetAfterTest(true);
758 $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
759 $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
760 $oldsections = array();
762 foreach ($DB->get_records('course_sections', array('course'=>$course->id
), 'id') as $section) {
763 $oldsections[$section->section
] = $section->id
;
764 $sections[$section->id
] = $section->section
;
768 $neworder = reorder_sections($sections, 2, 4);
769 $neworder = array_keys($neworder);
770 $this->assertEquals($oldsections[0], $neworder[0]);
771 $this->assertEquals($oldsections[1], $neworder[1]);
772 $this->assertEquals($oldsections[2], $neworder[4]);
773 $this->assertEquals($oldsections[3], $neworder[2]);
774 $this->assertEquals($oldsections[4], $neworder[3]);
775 $this->assertEquals($oldsections[5], $neworder[5]);
776 $this->assertEquals($oldsections[6], $neworder[6]);
778 $neworder = reorder_sections($sections, 4, 2);
779 $neworder = array_keys($neworder);
780 $this->assertEquals($oldsections[0], $neworder[0]);
781 $this->assertEquals($oldsections[1], $neworder[1]);
782 $this->assertEquals($oldsections[2], $neworder[3]);
783 $this->assertEquals($oldsections[3], $neworder[4]);
784 $this->assertEquals($oldsections[4], $neworder[2]);
785 $this->assertEquals($oldsections[5], $neworder[5]);
786 $this->assertEquals($oldsections[6], $neworder[6]);
788 $neworder = reorder_sections(1, 2, 4);
789 $this->assertFalse($neworder);
792 public function test_move_section_down() {
794 $this->resetAfterTest(true);
796 $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
797 $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
798 $oldsections = array();
799 foreach ($DB->get_records('course_sections', array('course'=>$course->id
)) as $section) {
800 $oldsections[$section->section
] = $section->id
;
804 // Test move section down..
805 move_section_to($course, 2, 4);
807 foreach ($DB->get_records('course_sections', array('course'=>$course->id
)) as $section) {
808 $sections[$section->section
] = $section->id
;
812 $this->assertEquals($oldsections[0], $sections[0]);
813 $this->assertEquals($oldsections[1], $sections[1]);
814 $this->assertEquals($oldsections[2], $sections[4]);
815 $this->assertEquals($oldsections[3], $sections[2]);
816 $this->assertEquals($oldsections[4], $sections[3]);
817 $this->assertEquals($oldsections[5], $sections[5]);
818 $this->assertEquals($oldsections[6], $sections[6]);
821 public function test_move_section_up() {
823 $this->resetAfterTest(true);
825 $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
826 $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
827 $oldsections = array();
828 foreach ($DB->get_records('course_sections', array('course'=>$course->id
)) as $section) {
829 $oldsections[$section->section
] = $section->id
;
833 // Test move section up..
834 move_section_to($course, 6, 4);
836 foreach ($DB->get_records('course_sections', array('course'=>$course->id
)) as $section) {
837 $sections[$section->section
] = $section->id
;
841 $this->assertEquals($oldsections[0], $sections[0]);
842 $this->assertEquals($oldsections[1], $sections[1]);
843 $this->assertEquals($oldsections[2], $sections[2]);
844 $this->assertEquals($oldsections[3], $sections[3]);
845 $this->assertEquals($oldsections[4], $sections[5]);
846 $this->assertEquals($oldsections[5], $sections[6]);
847 $this->assertEquals($oldsections[6], $sections[4]);
850 public function test_move_section_marker() {
852 $this->resetAfterTest(true);
854 $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
855 $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
857 // Set course marker to the section we are going to move..
858 course_set_marker($course->id
, 2);
859 // Verify that the course marker is set correctly.
860 $course = $DB->get_record('course', array('id' => $course->id
));
861 $this->assertEquals(2, $course->marker
);
863 // Test move the marked section down..
864 move_section_to($course, 2, 4);
866 // Verify that the coruse marker has been moved along with the section..
867 $course = $DB->get_record('course', array('id' => $course->id
));
868 $this->assertEquals(4, $course->marker
);
870 // Test move the marked section up..
871 move_section_to($course, 4, 3);
873 // Verify that the course marker has been moved along with the section..
874 $course = $DB->get_record('course', array('id' => $course->id
));
875 $this->assertEquals(3, $course->marker
);
877 // Test moving a non-marked section above the marked section..
878 move_section_to($course, 4, 2);
880 // Verify that the course marker has been moved down to accomodate..
881 $course = $DB->get_record('course', array('id' => $course->id
));
882 $this->assertEquals(4, $course->marker
);
884 // Test moving a non-marked section below the marked section..
885 move_section_to($course, 3, 6);
887 // Verify that the course marker has been up to accomodate..
888 $course = $DB->get_record('course', array('id' => $course->id
));
889 $this->assertEquals(3, $course->marker
);
892 public function test_course_can_delete_section() {
894 $this->resetAfterTest(true);
896 $generator = $this->getDataGenerator();
898 $courseweeks = $generator->create_course(
899 array('numsections' => 5, 'format' => 'weeks'),
900 array('createsections' => true));
901 $assign1 = $generator->create_module('assign', array('course' => $courseweeks, 'section' => 1));
902 $assign2 = $generator->create_module('assign', array('course' => $courseweeks, 'section' => 2));
904 $coursetopics = $generator->create_course(
905 array('numsections' => 5, 'format' => 'topics'),
906 array('createsections' => true));
908 $coursesingleactivity = $generator->create_course(
909 array('format' => 'singleactivity'),
910 array('createsections' => true));
912 // Enrol student and teacher.
913 $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
914 $student = $generator->create_user();
915 $teacher = $generator->create_user();
917 $generator->enrol_user($student->id
, $courseweeks->id
, $roleids['student']);
918 $generator->enrol_user($teacher->id
, $courseweeks->id
, $roleids['editingteacher']);
920 $generator->enrol_user($student->id
, $coursetopics->id
, $roleids['student']);
921 $generator->enrol_user($teacher->id
, $coursetopics->id
, $roleids['editingteacher']);
923 $generator->enrol_user($student->id
, $coursesingleactivity->id
, $roleids['student']);
924 $generator->enrol_user($teacher->id
, $coursesingleactivity->id
, $roleids['editingteacher']);
926 // Teacher should be able to delete sections (except for 0) in topics and weeks format.
927 $this->setUser($teacher);
929 // For topics and weeks formats will return false for section 0 and true for any other section.
930 $this->assertFalse(course_can_delete_section($courseweeks, 0));
931 $this->assertTrue(course_can_delete_section($courseweeks, 1));
933 $this->assertFalse(course_can_delete_section($coursetopics, 0));
934 $this->assertTrue(course_can_delete_section($coursetopics, 1));
936 // For singleactivity course format no section can be deleted.
937 $this->assertFalse(course_can_delete_section($coursesingleactivity, 0));
938 $this->assertFalse(course_can_delete_section($coursesingleactivity, 1));
940 // Now let's revoke a capability from teacher to manage activity in section 1.
941 $modulecontext = context_module
::instance($assign1->cmid
);
942 assign_capability('moodle/course:manageactivities', CAP_PROHIBIT
, $roleids['editingteacher'],
944 $modulecontext->mark_dirty();
945 $this->assertFalse(course_can_delete_section($courseweeks, 1));
946 $this->assertTrue(course_can_delete_section($courseweeks, 2));
948 // Student does not have permissions to delete sections.
949 $this->setUser($student);
950 $this->assertFalse(course_can_delete_section($courseweeks, 1));
951 $this->assertFalse(course_can_delete_section($coursetopics, 1));
952 $this->assertFalse(course_can_delete_section($coursesingleactivity, 1));
955 public function test_course_delete_section() {
957 $this->resetAfterTest(true);
959 $generator = $this->getDataGenerator();
961 $course = $generator->create_course(array('numsections' => 6, 'format' => 'topics'),
962 array('createsections' => true));
963 $assign0 = $generator->create_module('assign', array('course' => $course, 'section' => 0));
964 $assign1 = $generator->create_module('assign', array('course' => $course, 'section' => 1));
965 $assign21 = $generator->create_module('assign', array('course' => $course, 'section' => 2));
966 $assign22 = $generator->create_module('assign', array('course' => $course, 'section' => 2));
967 $assign3 = $generator->create_module('assign', array('course' => $course, 'section' => 3));
968 $assign5 = $generator->create_module('assign', array('course' => $course, 'section' => 5));
969 $assign6 = $generator->create_module('assign', array('course' => $course, 'section' => 6));
971 $this->setAdminUser();
973 // Attempt to delete non-existing section.
974 $this->assertFalse(course_delete_section($course, 10, false));
975 $this->assertFalse(course_delete_section($course, 9, true));
977 // Attempt to delete 0-section.
978 $this->assertFalse(course_delete_section($course, 0, true));
979 $this->assertTrue($DB->record_exists('course_modules', array('id' => $assign0->cmid
)));
981 // Delete last section.
982 $this->assertTrue(course_delete_section($course, 6, true));
983 $this->assertFalse($DB->record_exists('course_modules', array('id' => $assign6->cmid
)));
984 $this->assertEquals(5, course_get_format($course)->get_last_section_number());
986 // Delete empty section.
987 $this->assertTrue(course_delete_section($course, 4, false));
988 $this->assertEquals(4, course_get_format($course)->get_last_section_number());
990 // Delete section in the middle (2).
991 $this->assertFalse(course_delete_section($course, 2, false));
992 $this->assertTrue(course_delete_section($course, 2, true));
993 $this->assertFalse($DB->record_exists('course_modules', array('id' => $assign21->cmid
)));
994 $this->assertFalse($DB->record_exists('course_modules', array('id' => $assign22->cmid
)));
995 $this->assertEquals(3, course_get_format($course)->get_last_section_number());
996 $this->assertEquals(array(0 => array($assign0->cmid
),
997 1 => array($assign1->cmid
),
998 2 => array($assign3->cmid
),
999 3 => array($assign5->cmid
)), get_fast_modinfo($course)->sections
);
1001 // Remove marked section.
1002 course_set_marker($course->id
, 1);
1003 $this->assertTrue(course_get_format($course)->is_section_current(1));
1004 $this->assertTrue(course_delete_section($course, 1, true));
1005 $this->assertFalse(course_get_format($course)->is_section_current(1));
1008 public function test_get_course_display_name_for_list() {
1010 $this->resetAfterTest(true);
1012 $course = $this->getDataGenerator()->create_course(array('shortname' => 'FROG101', 'fullname' => 'Introduction to pond life'));
1014 $CFG->courselistshortnames
= 0;
1015 $this->assertEquals('Introduction to pond life', get_course_display_name_for_list($course));
1017 $CFG->courselistshortnames
= 1;
1018 $this->assertEquals('FROG101 Introduction to pond life', get_course_display_name_for_list($course));
1021 public function test_move_module_in_course() {
1024 $this->resetAfterTest(true);
1026 $course = $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections' => true));
1027 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id
));
1029 $cms = get_fast_modinfo($course)->get_cms();
1032 $newsection = get_fast_modinfo($course)->get_section_info(3);
1033 $oldsectionid = $cm->section
;
1036 moveto_module($cm, $newsection);
1038 $cms = get_fast_modinfo($course)->get_cms();
1041 // Check that the cached modinfo contains the correct section info
1042 $modinfo = get_fast_modinfo($course);
1043 $this->assertTrue(empty($modinfo->sections
[0]));
1044 $this->assertFalse(empty($modinfo->sections
[3]));
1046 // Check that the old section's sequence no longer contains this ID
1047 $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
1048 $oldsequences = explode(',', $newsection->sequence
);
1049 $this->assertFalse(in_array($cm->id
, $oldsequences));
1051 // Check that the new section's sequence now contains this ID
1052 $newsection = $DB->get_record('course_sections', array('id' => $newsection->id
));
1053 $newsequences = explode(',', $newsection->sequence
);
1054 $this->assertTrue(in_array($cm->id
, $newsequences));
1056 // Check that the section number has been changed in the cm
1057 $this->assertEquals($newsection->id
, $cm->section
);
1060 // Perform a second move as some issues were only seen on the second move
1061 $newsection = get_fast_modinfo($course)->get_section_info(2);
1062 $oldsectionid = $cm->section
;
1063 moveto_module($cm, $newsection);
1065 $cms = get_fast_modinfo($course)->get_cms();
1068 // Check that the cached modinfo contains the correct section info
1069 $modinfo = get_fast_modinfo($course);
1070 $this->assertTrue(empty($modinfo->sections
[0]));
1071 $this->assertFalse(empty($modinfo->sections
[2]));
1073 // Check that the old section's sequence no longer contains this ID
1074 $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
1075 $oldsequences = explode(',', $newsection->sequence
);
1076 $this->assertFalse(in_array($cm->id
, $oldsequences));
1078 // Check that the new section's sequence now contains this ID
1079 $newsection = $DB->get_record('course_sections', array('id' => $newsection->id
));
1080 $newsequences = explode(',', $newsection->sequence
);
1081 $this->assertTrue(in_array($cm->id
, $newsequences));
1084 public function test_module_visibility() {
1085 $this->setAdminUser();
1086 $this->resetAfterTest(true);
1088 // Create course and modules.
1089 $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
1090 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id
));
1091 $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(), 'course' => $course->id
));
1092 $modules = compact('forum', 'assign');
1094 // Hiding the modules.
1095 foreach ($modules as $mod) {
1096 set_coursemodule_visible($mod->cmid
, 0);
1097 $this->check_module_visibility($mod, 0, 0);
1100 // Showing the modules.
1101 foreach ($modules as $mod) {
1102 set_coursemodule_visible($mod->cmid
, 1);
1103 $this->check_module_visibility($mod, 1, 1);
1107 public function test_section_visibility_events() {
1108 $this->setAdminUser();
1109 $this->resetAfterTest(true);
1111 $course = $this->getDataGenerator()->create_course(array('numsections' => 1), array('createsections' => true));
1113 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id
),
1114 array('section' => $sectionnumber));
1115 $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1116 'course' => $course->id
), array('section' => $sectionnumber));
1117 $sink = $this->redirectEvents();
1118 set_section_visible($course->id
, $sectionnumber, 0);
1119 $events = $sink->get_events();
1121 // Extract the number of events related to what we are testing, other events
1122 // such as course_section_updated could have been triggered.
1124 foreach ($events as $event) {
1125 if ($event instanceof \core\event\course_module_updated
) {
1129 $this->assertSame(2, $count);
1133 public function test_section_visibility() {
1134 $this->setAdminUser();
1135 $this->resetAfterTest(true);
1138 $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
1140 $sink = $this->redirectEvents();
1142 // Testing an empty section.
1144 set_section_visible($course->id
, $sectionnumber, 0);
1145 $section_info = get_fast_modinfo($course->id
)->get_section_info($sectionnumber);
1146 $this->assertEquals($section_info->visible
, 0);
1147 set_section_visible($course->id
, $sectionnumber, 1);
1148 $section_info = get_fast_modinfo($course->id
)->get_section_info($sectionnumber);
1149 $this->assertEquals($section_info->visible
, 1);
1151 // Checking that an event was fired.
1152 $events = $sink->get_events();
1153 $this->assertInstanceOf('\core\event\course_section_updated', $events[0]);
1156 // Testing a section with visible modules.
1158 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id
),
1159 array('section' => $sectionnumber));
1160 $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1161 'course' => $course->id
), array('section' => $sectionnumber));
1162 $modules = compact('forum', 'assign');
1163 set_section_visible($course->id
, $sectionnumber, 0);
1164 $section_info = get_fast_modinfo($course->id
)->get_section_info($sectionnumber);
1165 $this->assertEquals($section_info->visible
, 0);
1166 foreach ($modules as $mod) {
1167 $this->check_module_visibility($mod, 0, 1);
1169 set_section_visible($course->id
, $sectionnumber, 1);
1170 $section_info = get_fast_modinfo($course->id
)->get_section_info($sectionnumber);
1171 $this->assertEquals($section_info->visible
, 1);
1172 foreach ($modules as $mod) {
1173 $this->check_module_visibility($mod, 1, 1);
1176 // Testing a section with hidden modules, which should stay hidden.
1178 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id
),
1179 array('section' => $sectionnumber));
1180 $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1181 'course' => $course->id
), array('section' => $sectionnumber));
1182 $modules = compact('forum', 'assign');
1183 foreach ($modules as $mod) {
1184 set_coursemodule_visible($mod->cmid
, 0);
1185 $this->check_module_visibility($mod, 0, 0);
1187 set_section_visible($course->id
, $sectionnumber, 0);
1188 $section_info = get_fast_modinfo($course->id
)->get_section_info($sectionnumber);
1189 $this->assertEquals($section_info->visible
, 0);
1190 foreach ($modules as $mod) {
1191 $this->check_module_visibility($mod, 0, 0);
1193 set_section_visible($course->id
, $sectionnumber, 1);
1194 $section_info = get_fast_modinfo($course->id
)->get_section_info($sectionnumber);
1195 $this->assertEquals($section_info->visible
, 1);
1196 foreach ($modules as $mod) {
1197 $this->check_module_visibility($mod, 0, 0);
1202 * Helper function to assert that a module has correctly been made visible, or hidden.
1204 * @param stdClass $mod module information
1205 * @param int $visibility the current state of the module
1206 * @param int $visibleold the current state of the visibleold property
1209 public function check_module_visibility($mod, $visibility, $visibleold) {
1211 $cm = get_fast_modinfo($mod->course
)->get_cm($mod->cmid
);
1212 $this->assertEquals($visibility, $cm->visible
);
1213 $this->assertEquals($visibleold, $cm->visibleold
);
1215 // Check the module grade items.
1216 $grade_items = grade_item
::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $cm->modname
,
1217 'iteminstance' => $cm->instance
, 'courseid' => $cm->course
));
1219 foreach ($grade_items as $grade_item) {
1221 $this->assertFalse($grade_item->is_hidden(), "$cm->modname grade_item not visible");
1223 $this->assertTrue($grade_item->is_hidden(), "$cm->modname grade_item not hidden");
1228 // Check the events visibility.
1229 if ($events = $DB->get_records('event', array('instance' => $cm->instance
, 'modulename' => $cm->modname
))) {
1230 foreach ($events as $event) {
1231 $calevent = new calendar_event($event);
1232 $this->assertEquals($visibility, $calevent->visible
, "$cm->modname calendar_event visibility");
1237 public function test_course_page_type_list() {
1239 $this->resetAfterTest(true);
1241 // Create a category.
1242 $category = new stdClass();
1243 $category->name
= 'Test Category';
1245 $testcategory = $this->getDataGenerator()->create_category($category);
1248 $course = new stdClass();
1249 $course->fullname
= 'Apu loves Unit Təsts';
1250 $course->shortname
= 'Spread the lÅve';
1251 $course->idnumber
= '123';
1252 $course->summary
= 'Awesome!';
1253 $course->summaryformat
= FORMAT_PLAIN
;
1254 $course->format
= 'topics';
1255 $course->newsitems
= 0;
1256 $course->numsections
= 5;
1257 $course->category
= $testcategory->id
;
1259 $testcourse = $this->getDataGenerator()->create_course($course);
1262 $coursecontext = context_course
::instance($testcourse->id
);
1263 $parentcontext = $coursecontext->get_parent_context(); // Not actually used.
1264 $pagetype = 'page-course-x'; // Not used either.
1265 $pagetypelist = course_page_type_list($pagetype, $parentcontext, $coursecontext);
1267 // Page type lists for normal courses.
1268 $testpagetypelist1 = array();
1269 $testpagetypelist1['*'] = 'Any page';
1270 $testpagetypelist1['course-*'] = 'Any course page';
1271 $testpagetypelist1['course-view-*'] = 'Any type of course main page';
1273 $this->assertEquals($testpagetypelist1, $pagetypelist);
1275 // Get the context for the front page course.
1276 $sitecoursecontext = context_course
::instance(SITEID
);
1277 $pagetypelist = course_page_type_list($pagetype, $parentcontext, $sitecoursecontext);
1279 // Page type list for the front page course.
1280 $testpagetypelist2 = array('*' => 'Any page');
1281 $this->assertEquals($testpagetypelist2, $pagetypelist);
1283 // Make sure that providing no current context to the function doesn't result in an error.
1284 // Calls made from generate_page_type_patterns() may provide null values.
1285 $pagetypelist = course_page_type_list($pagetype, null, null);
1286 $this->assertEquals($pagetypelist, $testpagetypelist1);
1289 public function test_compare_activities_by_time_desc() {
1291 // Let's create some test data.
1292 $activitiesivities = array();
1293 $x = new stdClass();
1294 $x->timestamp
= null;
1297 $x = new stdClass();
1301 $x = new stdClass();
1305 $x = new stdClass();
1309 $x = new stdClass();
1313 $x = new stdClass();
1316 $x = new stdClass();
1321 usort($activities, 'compare_activities_by_time_desc');
1323 // Let's check the result.
1325 foreach($activities as $activity) {
1326 if (empty($activity->timestamp
)) {
1327 $activity->timestamp
= 0;
1329 $this->assertLessThanOrEqual($last, $activity->timestamp
);
1333 public function test_compare_activities_by_time_asc() {
1335 // Let's create some test data.
1336 $activities = array();
1337 $x = new stdClass();
1338 $x->timestamp
= null;
1341 $x = new stdClass();
1345 $x = new stdClass();
1349 $x = new stdClass();
1353 $x = new stdClass();
1357 $x = new stdClass();
1360 $x = new stdClass();
1365 usort($activities, 'compare_activities_by_time_asc');
1367 // Let's check the result.
1369 foreach($activities as $activity) {
1370 if (empty($activity->timestamp
)) {
1371 $activity->timestamp
= 0;
1373 $this->assertGreaterThanOrEqual($last, $activity->timestamp
);
1378 * Tests moving a module between hidden/visible sections and
1379 * verifies that the course/module visiblity seettings are
1382 public function test_moveto_module_between_hidden_sections() {
1385 $this->resetAfterTest(true);
1387 $course = $this->getDataGenerator()->create_course(array('numsections' => 4), array('createsections' => true));
1388 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id
));
1389 $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id
));
1390 $quiz= $this->getDataGenerator()->create_module('quiz', array('course' => $course->id
));
1392 // Set the page as hidden
1393 set_coursemodule_visible($page->cmid
, 0);
1395 // Set sections 3 as hidden.
1396 set_section_visible($course->id
, 3, 0);
1398 $modinfo = get_fast_modinfo($course);
1400 $hiddensection = $modinfo->get_section_info(3);
1401 // New section is definitely not visible:
1402 $this->assertEquals($hiddensection->visible
, 0);
1404 $forumcm = $modinfo->cms
[$forum->cmid
];
1405 $pagecm = $modinfo->cms
[$page->cmid
];
1407 // Move the forum and the page to a hidden section, make sure moveto_module returns 0 as new visibility state.
1408 $this->assertEquals(0, moveto_module($forumcm, $hiddensection));
1409 $this->assertEquals(0, moveto_module($pagecm, $hiddensection));
1411 $modinfo = get_fast_modinfo($course);
1413 // Verify that forum and page have been moved to the hidden section and quiz has not.
1414 $this->assertContains($forum->cmid
, $modinfo->sections
[3]);
1415 $this->assertContains($page->cmid
, $modinfo->sections
[3]);
1416 $this->assertNotContains($quiz->cmid
, $modinfo->sections
[3]);
1418 // Verify that forum has been made invisible.
1419 $forumcm = $modinfo->cms
[$forum->cmid
];
1420 $this->assertEquals($forumcm->visible
, 0);
1421 // Verify that old state has been retained.
1422 $this->assertEquals($forumcm->visibleold
, 1);
1424 // Verify that page has stayed invisible.
1425 $pagecm = $modinfo->cms
[$page->cmid
];
1426 $this->assertEquals($pagecm->visible
, 0);
1427 // Verify that old state has been retained.
1428 $this->assertEquals($pagecm->visibleold
, 0);
1430 // Verify that quiz has been unaffected.
1431 $quizcm = $modinfo->cms
[$quiz->cmid
];
1432 $this->assertEquals($quizcm->visible
, 1);
1434 // Move forum and page back to visible section.
1435 // Make sure the visibility is restored to the original value (visible for forum and hidden for page).
1436 $visiblesection = $modinfo->get_section_info(2);
1437 $this->assertEquals(1, moveto_module($forumcm, $visiblesection));
1438 $this->assertEquals(0, moveto_module($pagecm, $visiblesection));
1440 $modinfo = get_fast_modinfo($course);
1442 // Double check that forum has been made visible.
1443 $forumcm = $modinfo->cms
[$forum->cmid
];
1444 $this->assertEquals($forumcm->visible
, 1);
1446 // Double check that page has stayed invisible.
1447 $pagecm = $modinfo->cms
[$page->cmid
];
1448 $this->assertEquals($pagecm->visible
, 0);
1450 // Move the page in the same section (this is what mod duplicate does).
1451 // Visibility of page remains 0.
1452 $this->assertEquals(0, moveto_module($pagecm, $visiblesection, $forumcm));
1454 // Double check that the the page is still hidden.
1455 $modinfo = get_fast_modinfo($course);
1456 $pagecm = $modinfo->cms
[$page->cmid
];
1457 $this->assertEquals($pagecm->visible
, 0);
1461 * Tests moving a module around in the same section. moveto_module()
1462 * is called this way in modduplicate.
1464 public function test_moveto_module_in_same_section() {
1467 $this->resetAfterTest(true);
1469 $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
1470 $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id
));
1471 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id
));
1473 // Simulate inconsistent visible/visibleold values (MDL-38713).
1474 $cm = $DB->get_record('course_modules', array('id' => $page->cmid
), '*', MUST_EXIST
);
1476 $cm->visibleold
= 1;
1477 $DB->update_record('course_modules', $cm);
1479 $modinfo = get_fast_modinfo($course);
1480 $forumcm = $modinfo->cms
[$forum->cmid
];
1481 $pagecm = $modinfo->cms
[$page->cmid
];
1483 // Verify that page is hidden.
1484 $this->assertEquals($pagecm->visible
, 0);
1486 // Verify section 0 is where all mods added.
1487 $section = $modinfo->get_section_info(0);
1488 $this->assertEquals($section->id
, $forumcm->section
);
1489 $this->assertEquals($section->id
, $pagecm->section
);
1492 // Move the page inside the hidden section. Make sure it is hidden.
1493 $this->assertEquals(0, moveto_module($pagecm, $section, $forumcm));
1495 // Double check that the the page is still hidden.
1496 $modinfo = get_fast_modinfo($course);
1497 $pagecm = $modinfo->cms
[$page->cmid
];
1498 $this->assertEquals($pagecm->visible
, 0);
1502 * Tests the function that deletes a course module
1504 * @param string $type The type of module for the test
1505 * @param array $options The options for the module creation
1506 * @dataProvider provider_course_delete_module
1508 public function test_course_delete_module($type, $options) {
1511 $this->resetAfterTest(true);
1512 $this->setAdminUser();
1514 // Create course and modules.
1515 $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
1516 $options['course'] = $course->id
;
1518 // Generate an assignment with due date (will generate a course event).
1519 $module = $this->getDataGenerator()->create_module($type, $options);
1521 // Get the module context.
1522 $modcontext = context_module
::instance($module->cmid
);
1524 // Verify context exists.
1525 $this->assertInstanceOf('context_module', $modcontext);
1527 // Make module specific messes.
1530 // Add some tags to this assignment.
1531 core_tag_tag
::set_item_tags('mod_assign', 'assign', $module->id
, $modcontext, array('Tag 1', 'Tag 2', 'Tag 3'));
1532 core_tag_tag
::set_item_tags('core', 'course_modules', $module->cmid
, $modcontext, array('Tag 3', 'Tag 4', 'Tag 5'));
1534 // Confirm the tag instances were added.
1535 $criteria = array('component' => 'mod_assign', 'itemtype' => 'assign', 'contextid' => $modcontext->id
);
1536 $this->assertEquals(3, $DB->count_records('tag_instance', $criteria));
1537 $criteria = array('component' => 'core', 'itemtype' => 'course_modules', 'contextid' => $modcontext->id
);
1538 $this->assertEquals(3, $DB->count_records('tag_instance', $criteria));
1540 // Verify event assignment event has been generated.
1541 $eventcount = $DB->count_records('event', array('instance' => $module->id
, 'modulename' => $type));
1542 $this->assertEquals(1, $eventcount);
1546 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
1547 $qcat = $qgen->create_question_category(array('contextid' => $modcontext->id
));
1549 $qgen->create_question('shortanswer', null, array('category' => $qcat->id
)),
1550 $qgen->create_question('shortanswer', null, array('category' => $qcat->id
)),
1552 $this->expectOutputRegex('/'.get_string('unusedcategorydeleted', 'question').'/');
1559 course_delete_module($module->cmid
);
1561 // Verify the context has been removed.
1562 $this->assertFalse(context_module
::instance($module->cmid
, IGNORE_MISSING
));
1564 // Verify the course_module record has been deleted.
1565 $cmcount = $DB->count_records('course_modules', array('id' => $module->cmid
));
1566 $this->assertEmpty($cmcount);
1568 // Test clean up of module specific messes.
1571 // Verify event assignment events have been removed.
1572 $eventcount = $DB->count_records('event', array('instance' => $module->id
, 'modulename' => $type));
1573 $this->assertEmpty($eventcount);
1575 // Verify the tag instances were deleted.
1576 $criteria = array('component' => 'mod_assign', 'contextid' => $modcontext->id
);
1577 $this->assertEquals(0, $DB->count_records('tag_instance', $criteria));
1579 $criteria = array('component' => 'core', 'itemtype' => 'course_modules', 'contextid' => $modcontext->id
);
1580 $this->assertEquals(0, $DB->count_records('tag_instance', $criteria));
1583 // Verify category deleted.
1584 $criteria = array('contextid' => $modcontext->id
);
1585 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
1587 // Verify questions deleted.
1588 $criteria = array('category' => $qcat->id
);
1589 $this->assertEquals(0, $DB->count_records('question', $criteria));
1597 * Test that triggering a course_created event works as expected.
1599 public function test_course_created_event() {
1602 $this->resetAfterTest();
1604 // Catch the events.
1605 $sink = $this->redirectEvents();
1607 // Create the course with an id number which is used later when generating a course via the imsenterprise plugin.
1608 $data = new stdClass();
1609 $data->idnumber
= 'idnumber';
1610 $course = $this->getDataGenerator()->create_course($data);
1611 // Get course from DB for comparison.
1612 $course = $DB->get_record('course', array('id' => $course->id
));
1614 // Capture the event.
1615 $events = $sink->get_events();
1618 // Validate the event.
1619 $event = $events[0];
1620 $this->assertInstanceOf('\core\event\course_created', $event);
1621 $this->assertEquals('course', $event->objecttable
);
1622 $this->assertEquals($course->id
, $event->objectid
);
1623 $this->assertEquals(context_course
::instance($course->id
), $event->get_context());
1624 $this->assertEquals($course, $event->get_record_snapshot('course', $course->id
));
1625 $this->assertEquals('course_created', $event->get_legacy_eventname());
1626 $this->assertEventLegacyData($course, $event);
1627 $expectedlog = array(SITEID
, 'course', 'new', 'view.php?id=' . $course->id
, $course->fullname
. ' (ID ' . $course->id
. ')');
1628 $this->assertEventLegacyLogData($expectedlog, $event);
1630 // Now we want to trigger creating a course via the imsenterprise.
1631 // Delete the course we created earlier, as we want the imsenterprise plugin to create this.
1632 // We do not want print out any of the text this function generates while doing this, which is why
1633 // we are using ob_start() and ob_end_clean().
1635 delete_course($course);
1638 // Create the XML file we want to use.
1639 $course->category
= (array)$course->category
;
1640 $imstestcase = new enrol_imsenterprise_testcase();
1641 $imstestcase->imsplugin
= enrol_get_plugin('imsenterprise');
1642 $imstestcase->set_test_config();
1643 $imstestcase->set_xml_file(false, array($course));
1645 // Capture the event.
1646 $sink = $this->redirectEvents();
1647 $imstestcase->imsplugin
->cron();
1648 $events = $sink->get_events();
1651 foreach ($events as $eventinfo) {
1652 if ($eventinfo instanceof \core\event\course_created
) {
1653 $event = $eventinfo;
1658 // Validate the event triggered is \core\event\course_created. There is no need to validate the other values
1659 // as they have already been validated in the previous steps. Here we only want to make sure that when the
1660 // imsenterprise plugin creates a course an event is triggered.
1661 $this->assertInstanceOf('\core\event\course_created', $event);
1662 $this->assertEventContextNotUsed($event);
1666 * Test that triggering a course_updated event works as expected.
1668 public function test_course_updated_event() {
1671 $this->resetAfterTest();
1674 $course = $this->getDataGenerator()->create_course();
1676 // Create a category we are going to move this course to.
1677 $category = $this->getDataGenerator()->create_category();
1679 // Create a hidden category we are going to move this course to.
1680 $categoryhidden = $this->getDataGenerator()->create_category(array('visible' => 0));
1682 // Update course and catch course_updated event.
1683 $sink = $this->redirectEvents();
1684 update_course($course);
1685 $events = $sink->get_events();
1688 // Get updated course information from the DB.
1689 $updatedcourse = $DB->get_record('course', array('id' => $course->id
), '*', MUST_EXIST
);
1691 $event = array_shift($events);
1692 $this->assertInstanceOf('\core\event\course_updated', $event);
1693 $this->assertEquals('course', $event->objecttable
);
1694 $this->assertEquals($updatedcourse->id
, $event->objectid
);
1695 $this->assertEquals(context_course
::instance($course->id
), $event->get_context());
1696 $url = new moodle_url('/course/edit.php', array('id' => $event->objectid
));
1697 $this->assertEquals($url, $event->get_url());
1698 $this->assertEquals($updatedcourse, $event->get_record_snapshot('course', $event->objectid
));
1699 $this->assertEquals('course_updated', $event->get_legacy_eventname());
1700 $this->assertEventLegacyData($updatedcourse, $event);
1701 $expectedlog = array($updatedcourse->id
, 'course', 'update', 'edit.php?id=' . $course->id
, $course->id
);
1702 $this->assertEventLegacyLogData($expectedlog, $event);
1704 // Move course and catch course_updated event.
1705 $sink = $this->redirectEvents();
1706 move_courses(array($course->id
), $category->id
);
1707 $events = $sink->get_events();
1710 // Return the moved course information from the DB.
1711 $movedcourse = $DB->get_record('course', array('id' => $course->id
), '*', MUST_EXIST
);
1713 $event = array_shift($events);
1714 $this->assertInstanceOf('\core\event\course_updated', $event);
1715 $this->assertEquals('course', $event->objecttable
);
1716 $this->assertEquals($movedcourse->id
, $event->objectid
);
1717 $this->assertEquals(context_course
::instance($course->id
), $event->get_context());
1718 $this->assertEquals($movedcourse, $event->get_record_snapshot('course', $movedcourse->id
));
1719 $this->assertEquals('course_updated', $event->get_legacy_eventname());
1720 $this->assertEventLegacyData($movedcourse, $event);
1721 $expectedlog = array($movedcourse->id
, 'course', 'move', 'edit.php?id=' . $movedcourse->id
, $movedcourse->id
);
1722 $this->assertEventLegacyLogData($expectedlog, $event);
1724 // Move course to hidden category and catch course_updated event.
1725 $sink = $this->redirectEvents();
1726 move_courses(array($course->id
), $categoryhidden->id
);
1727 $events = $sink->get_events();
1730 // Return the moved course information from the DB.
1731 $movedcoursehidden = $DB->get_record('course', array('id' => $course->id
), '*', MUST_EXIST
);
1733 $event = array_shift($events);
1734 $this->assertInstanceOf('\core\event\course_updated', $event);
1735 $this->assertEquals('course', $event->objecttable
);
1736 $this->assertEquals($movedcoursehidden->id
, $event->objectid
);
1737 $this->assertEquals(context_course
::instance($course->id
), $event->get_context());
1738 $this->assertEquals($movedcoursehidden, $event->get_record_snapshot('course', $movedcoursehidden->id
));
1739 $this->assertEquals('course_updated', $event->get_legacy_eventname());
1740 $this->assertEventLegacyData($movedcoursehidden, $event);
1741 $expectedlog = array($movedcoursehidden->id
, 'course', 'move', 'edit.php?id=' . $movedcoursehidden->id
, $movedcoursehidden->id
);
1742 $this->assertEventLegacyLogData($expectedlog, $event);
1743 $this->assertEventContextNotUsed($event);
1747 * Test that triggering a course_deleted event works as expected.
1749 public function test_course_deleted_event() {
1750 $this->resetAfterTest();
1752 // Create the course.
1753 $course = $this->getDataGenerator()->create_course();
1755 // Save the course context before we delete the course.
1756 $coursecontext = context_course
::instance($course->id
);
1758 // Catch the update event.
1759 $sink = $this->redirectEvents();
1761 // Call delete_course() which will trigger the course_deleted event and the course_content_deleted
1762 // event. This function prints out data to the screen, which we do not want during a PHPUnit test,
1763 // so use ob_start and ob_end_clean to prevent this.
1765 delete_course($course);
1768 // Capture the event.
1769 $events = $sink->get_events();
1772 // Validate the event.
1773 $event = array_pop($events);
1774 $this->assertInstanceOf('\core\event\course_deleted', $event);
1775 $this->assertEquals('course', $event->objecttable
);
1776 $this->assertEquals($course->id
, $event->objectid
);
1777 $this->assertEquals($coursecontext->id
, $event->contextid
);
1778 $this->assertEquals($course, $event->get_record_snapshot('course', $course->id
));
1779 $this->assertEquals('course_deleted', $event->get_legacy_eventname());
1780 $eventdata = $event->get_data();
1781 $this->assertSame($course->idnumber
, $eventdata['other']['idnumber']);
1782 $this->assertSame($course->fullname
, $eventdata['other']['fullname']);
1783 $this->assertSame($course->shortname
, $eventdata['other']['shortname']);
1785 // The legacy data also passed the context in the course object and substitutes timemodified with the current date.
1786 $expectedlegacy = clone($course);
1787 $expectedlegacy->context
= $coursecontext;
1788 $expectedlegacy->timemodified
= $event->timecreated
;
1789 $this->assertEventLegacyData($expectedlegacy, $event);
1791 // Validate legacy log data.
1792 $expectedlog = array(SITEID
, 'course', 'delete', 'view.php?id=' . $course->id
, $course->fullname
. '(ID ' . $course->id
. ')');
1793 $this->assertEventLegacyLogData($expectedlog, $event);
1794 $this->assertEventContextNotUsed($event);
1798 * Test that triggering a course_content_deleted event works as expected.
1800 public function test_course_content_deleted_event() {
1803 $this->resetAfterTest();
1805 // Create the course.
1806 $course = $this->getDataGenerator()->create_course();
1808 // Get the course from the DB. The data generator adds some extra properties, such as
1809 // numsections, to the course object which will fail the assertions later on.
1810 $course = $DB->get_record('course', array('id' => $course->id
), '*', MUST_EXIST
);
1812 // Save the course context before we delete the course.
1813 $coursecontext = context_course
::instance($course->id
);
1815 // Catch the update event.
1816 $sink = $this->redirectEvents();
1818 remove_course_contents($course->id
, false);
1820 // Capture the event.
1821 $events = $sink->get_events();
1824 // Validate the event.
1825 $event = array_pop($events);
1826 $this->assertInstanceOf('\core\event\course_content_deleted', $event);
1827 $this->assertEquals('course', $event->objecttable
);
1828 $this->assertEquals($course->id
, $event->objectid
);
1829 $this->assertEquals($coursecontext->id
, $event->contextid
);
1830 $this->assertEquals($course, $event->get_record_snapshot('course', $course->id
));
1831 $this->assertEquals('course_content_removed', $event->get_legacy_eventname());
1832 // The legacy data also passed the context and options in the course object.
1833 $course->context
= $coursecontext;
1834 $course->options
= array();
1835 $this->assertEventLegacyData($course, $event);
1836 $this->assertEventContextNotUsed($event);
1840 * Test that triggering a course_category_deleted event works as expected.
1842 public function test_course_category_deleted_event() {
1843 $this->resetAfterTest();
1845 // Create a category.
1846 $category = $this->getDataGenerator()->create_category();
1848 // Save the context before it is deleted.
1849 $categorycontext = context_coursecat
::instance($category->id
);
1851 // Catch the update event.
1852 $sink = $this->redirectEvents();
1854 // Delete the category.
1855 $category->delete_full();
1857 // Capture the event.
1858 $events = $sink->get_events();
1861 // Validate the event.
1862 $event = $events[0];
1863 $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1864 $this->assertEquals('course_categories', $event->objecttable
);
1865 $this->assertEquals($category->id
, $event->objectid
);
1866 $this->assertEquals($categorycontext->id
, $event->contextid
);
1867 $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1868 $this->assertEquals(null, $event->get_url());
1869 $this->assertEventLegacyData($category, $event);
1870 $expectedlog = array(SITEID
, 'category', 'delete', 'index.php', $category->name
. '(ID ' . $category->id
. ')');
1871 $this->assertEventLegacyLogData($expectedlog, $event);
1873 // Create two categories.
1874 $category = $this->getDataGenerator()->create_category();
1875 $category2 = $this->getDataGenerator()->create_category();
1877 // Save the context before it is moved and then deleted.
1878 $category2context = context_coursecat
::instance($category2->id
);
1880 // Catch the update event.
1881 $sink = $this->redirectEvents();
1883 // Move the category.
1884 $category2->delete_move($category->id
);
1886 // Capture the event.
1887 $events = $sink->get_events();
1890 // Validate the event.
1891 $event = $events[0];
1892 $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1893 $this->assertEquals('course_categories', $event->objecttable
);
1894 $this->assertEquals($category2->id
, $event->objectid
);
1895 $this->assertEquals($category2context->id
, $event->contextid
);
1896 $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1897 $this->assertEventLegacyData($category2, $event);
1898 $expectedlog = array(SITEID
, 'category', 'delete', 'index.php', $category2->name
. '(ID ' . $category2->id
. ')');
1899 $this->assertEventLegacyLogData($expectedlog, $event);
1900 $this->assertEventContextNotUsed($event);
1904 * Test that triggering a course_backup_created event works as expected.
1906 public function test_course_backup_created_event() {
1909 // Get the necessary files to perform backup and restore.
1910 require_once($CFG->dirroot
. '/backup/util/includes/backup_includes.php');
1911 require_once($CFG->dirroot
. '/backup/util/includes/restore_includes.php');
1913 $this->resetAfterTest();
1915 // Set to admin user.
1916 $this->setAdminUser();
1918 // The user id is going to be 2 since we are the admin user.
1922 $course = $this->getDataGenerator()->create_course();
1924 // Create backup file and save it to the backup location.
1925 $bc = new backup_controller(backup
::TYPE_1COURSE
, $course->id
, backup
::FORMAT_MOODLE
,
1926 backup
::INTERACTIVE_NO
, backup
::MODE_GENERAL
, $userid);
1927 $sink = $this->redirectEvents();
1928 $bc->execute_plan();
1930 // Capture the event.
1931 $events = $sink->get_events();
1934 // Validate the event.
1935 $event = array_pop($events);
1936 $this->assertInstanceOf('\core\event\course_backup_created', $event);
1937 $this->assertEquals('course', $event->objecttable
);
1938 $this->assertEquals($bc->get_courseid(), $event->objectid
);
1939 $this->assertEquals(context_course
::instance($bc->get_courseid())->id
, $event->contextid
);
1941 $url = new moodle_url('/course/view.php', array('id' => $event->objectid
));
1942 $this->assertEquals($url, $event->get_url());
1943 $this->assertEventContextNotUsed($event);
1945 // Destroy the resource controller since we are done using it.
1950 * Test that triggering a course_restored event works as expected.
1952 public function test_course_restored_event() {
1955 // Get the necessary files to perform backup and restore.
1956 require_once($CFG->dirroot
. '/backup/util/includes/backup_includes.php');
1957 require_once($CFG->dirroot
. '/backup/util/includes/restore_includes.php');
1959 $this->resetAfterTest();
1961 // Set to admin user.
1962 $this->setAdminUser();
1964 // The user id is going to be 2 since we are the admin user.
1968 $course = $this->getDataGenerator()->create_course();
1970 // Create backup file and save it to the backup location.
1971 $bc = new backup_controller(backup
::TYPE_1COURSE
, $course->id
, backup
::FORMAT_MOODLE
,
1972 backup
::INTERACTIVE_NO
, backup
::MODE_GENERAL
, $userid);
1973 $bc->execute_plan();
1974 $results = $bc->get_results();
1975 $file = $results['backup_destination'];
1976 $fp = get_file_packer('application/vnd.moodle.backup');
1977 $filepath = $CFG->dataroot
. '/temp/backup/test-restore-course-event';
1978 $file->extract_to_pathname($fp, $filepath);
1981 // Now we want to catch the restore course event.
1982 $sink = $this->redirectEvents();
1984 // Now restore the course to trigger the event.
1985 $rc = new restore_controller('test-restore-course-event', $course->id
, backup
::INTERACTIVE_NO
,
1986 backup
::MODE_GENERAL
, $userid, backup
::TARGET_NEW_COURSE
);
1987 $rc->execute_precheck();
1988 $rc->execute_plan();
1990 // Capture the event.
1991 $events = $sink->get_events();
1994 // Validate the event.
1995 $event = array_pop($events);
1996 $this->assertInstanceOf('\core\event\course_restored', $event);
1997 $this->assertEquals('course', $event->objecttable
);
1998 $this->assertEquals($rc->get_courseid(), $event->objectid
);
1999 $this->assertEquals(context_course
::instance($rc->get_courseid())->id
, $event->contextid
);
2000 $this->assertEquals('course_restored', $event->get_legacy_eventname());
2001 $legacydata = (object) array(
2002 'courseid' => $rc->get_courseid(),
2003 'userid' => $rc->get_userid(),
2004 'type' => $rc->get_type(),
2005 'target' => $rc->get_target(),
2006 'mode' => $rc->get_mode(),
2007 'operation' => $rc->get_operation(),
2008 'samesite' => $rc->is_samesite()
2010 $url = new moodle_url('/course/view.php', array('id' => $event->objectid
));
2011 $this->assertEquals($url, $event->get_url());
2012 $this->assertEventLegacyData($legacydata, $event);
2013 $this->assertEventContextNotUsed($event);
2015 // Destroy the resource controller since we are done using it.
2020 * Test that triggering a course_section_updated event works as expected.
2022 public function test_course_section_updated_event() {
2025 $this->resetAfterTest();
2027 // Create the course with sections.
2028 $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true));
2029 $sections = $DB->get_records('course_sections', array('course' => $course->id
));
2031 $coursecontext = context_course
::instance($course->id
);
2033 $section = array_pop($sections);
2034 $section->name
= 'Test section';
2035 $section->summary
= 'Test section summary';
2036 $DB->update_record('course_sections', $section);
2038 // Trigger an event for course section update.
2039 $event = \core\event\course_section_updated
::create(
2041 'objectid' => $section->id
,
2042 'courseid' => $course->id
,
2043 'context' => context_course
::instance($course->id
),
2045 'sectionnum' => $section->section
2049 $event->add_record_snapshot('course_sections', $section);
2050 // Trigger and catch event.
2051 $sink = $this->redirectEvents();
2053 $events = $sink->get_events();
2056 // Validate the event.
2057 $event = $events[0];
2058 $this->assertInstanceOf('\core\event\course_section_updated', $event);
2059 $this->assertEquals('course_sections', $event->objecttable
);
2060 $this->assertEquals($section->id
, $event->objectid
);
2061 $this->assertEquals($course->id
, $event->courseid
);
2062 $this->assertEquals($coursecontext->id
, $event->contextid
);
2063 $this->assertEquals($section->section
, $event->other
['sectionnum']);
2064 $expecteddesc = "The user with id '{$event->userid}' updated section number '{$event->other['sectionnum']}' for the course with id '{$event->courseid}'";
2065 $this->assertEquals($expecteddesc, $event->get_description());
2066 $url = new moodle_url('/course/editsection.php', array('id' => $event->objectid
));
2067 $this->assertEquals($url, $event->get_url());
2068 $this->assertEquals($section, $event->get_record_snapshot('course_sections', $event->objectid
));
2070 $sectionnum = $section->section
;
2071 $expectedlegacydata = array($course->id
, "course", "editsection", 'editsection.php?id=' . $id, $sectionnum);
2072 $this->assertEventLegacyLogData($expectedlegacydata, $event);
2073 $this->assertEventContextNotUsed($event);
2077 * Test that triggering a course_section_deleted event works as expected.
2079 public function test_course_section_deleted_event() {
2081 $this->resetAfterTest();
2082 $sink = $this->redirectEvents();
2084 // Create the course with sections.
2085 $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true));
2086 $sections = $DB->get_records('course_sections', array('course' => $course->id
), 'section');
2087 $coursecontext = context_course
::instance($course->id
);
2088 $section = array_pop($sections);
2089 course_delete_section($course, $section);
2090 $events = $sink->get_events();
2091 $event = array_pop($events); // Delete section event.
2094 // Validate event data.
2095 $this->assertInstanceOf('\core\event\course_section_deleted', $event);
2096 $this->assertEquals('course_sections', $event->objecttable
);
2097 $this->assertEquals($section->id
, $event->objectid
);
2098 $this->assertEquals($course->id
, $event->courseid
);
2099 $this->assertEquals($coursecontext->id
, $event->contextid
);
2100 $this->assertEquals($section->section
, $event->other
['sectionnum']);
2101 $expecteddesc = "The user with id '{$event->userid}' deleted section number '{$event->other['sectionnum']}' " .
2102 "(section name '{$event->other['sectionname']}') for the course with id '{$event->courseid}'";
2103 $this->assertEquals($expecteddesc, $event->get_description());
2104 $this->assertEquals($section, $event->get_record_snapshot('course_sections', $event->objectid
));
2105 $this->assertNull($event->get_url());
2107 // Test legacy data.
2108 $sectionnum = $section->section
;
2109 $expectedlegacydata = array($course->id
, "course", "delete section", 'view.php?id=' . $course->id
, $sectionnum);
2110 $this->assertEventLegacyLogData($expectedlegacydata, $event);
2111 $this->assertEventContextNotUsed($event);
2114 public function test_course_integrity_check() {
2117 $this->resetAfterTest(true);
2118 $course = $this->getDataGenerator()->create_course(array('numsections' => 1),
2119 array('createsections'=>true));
2121 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id
),
2122 array('section' => 0));
2123 $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id
),
2124 array('section' => 0));
2125 $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id
),
2126 array('section' => 0));
2127 $correctseq = join(',', array($forum->cmid
, $page->cmid
, $quiz->cmid
));
2129 $section0 = $DB->get_record('course_sections', array('course' => $course->id
, 'section' => 0));
2130 $section1 = $DB->get_record('course_sections', array('course' => $course->id
, 'section' => 1));
2131 $cms = $DB->get_records('course_modules', array('course' => $course->id
), 'id', 'id,section');
2132 $this->assertEquals($correctseq, $section0->sequence
);
2133 $this->assertEmpty($section1->sequence
);
2134 $this->assertEquals($section0->id
, $cms[$forum->cmid
]->section
);
2135 $this->assertEquals($section0->id
, $cms[$page->cmid
]->section
);
2136 $this->assertEquals($section0->id
, $cms[$quiz->cmid
]->section
);
2137 $this->assertEmpty(course_integrity_check($course->id
));
2139 // Now let's make manual change in DB and let course_integrity_check() fix it:
2141 // 1. Module appears twice in one section.
2142 $DB->update_record('course_sections', array('id' => $section0->id
, 'sequence' => $section0->sequence
. ','. $page->cmid
));
2143 $this->assertEquals(
2144 array('Failed integrity check for course ['. $course->id
.
2145 ']. Sequence for course section ['. $section0->id
. '] is "'.
2146 $section0->sequence
. ','. $page->cmid
. '", must be "'.
2147 $section0->sequence
. '"'),
2148 course_integrity_check($course->id
));
2149 $section0 = $DB->get_record('course_sections', array('course' => $course->id
, 'section' => 0));
2150 $section1 = $DB->get_record('course_sections', array('course' => $course->id
, 'section' => 1));
2151 $cms = $DB->get_records('course_modules', array('course' => $course->id
), 'id', 'id,section');
2152 $this->assertEquals($correctseq, $section0->sequence
);
2153 $this->assertEmpty($section1->sequence
);
2154 $this->assertEquals($section0->id
, $cms[$forum->cmid
]->section
);
2155 $this->assertEquals($section0->id
, $cms[$page->cmid
]->section
);
2156 $this->assertEquals($section0->id
, $cms[$quiz->cmid
]->section
);
2158 // 2. Module appears in two sections (last section wins).
2159 $DB->update_record('course_sections', array('id' => $section1->id
, 'sequence' => ''. $page->cmid
));
2160 // First message about double mentioning in sequence, second message about wrong section field for $page.
2161 $this->assertEquals(array(
2162 'Failed integrity check for course ['. $course->id
. ']. Course module ['. $page->cmid
.
2163 '] must be removed from sequence of section ['. $section0->id
.
2164 '] because it is also present in sequence of section ['. $section1->id
. ']',
2165 'Failed integrity check for course ['. $course->id
. ']. Course module ['. $page->cmid
.
2166 '] points to section ['. $section0->id
. '] instead of ['. $section1->id
. ']'),
2167 course_integrity_check($course->id
));
2168 $section0 = $DB->get_record('course_sections', array('course' => $course->id
, 'section' => 0));
2169 $section1 = $DB->get_record('course_sections', array('course' => $course->id
, 'section' => 1));
2170 $cms = $DB->get_records('course_modules', array('course' => $course->id
), 'id', 'id,section');
2171 $this->assertEquals($forum->cmid
. ','. $quiz->cmid
, $section0->sequence
);
2172 $this->assertEquals(''. $page->cmid
, $section1->sequence
);
2173 $this->assertEquals($section0->id
, $cms[$forum->cmid
]->section
);
2174 $this->assertEquals($section1->id
, $cms[$page->cmid
]->section
);
2175 $this->assertEquals($section0->id
, $cms[$quiz->cmid
]->section
);
2177 // 3. Module id is not present in course_section.sequence (integrity check with $fullcheck = false).
2178 $DB->update_record('course_sections', array('id' => $section1->id
, 'sequence' => ''));
2179 $this->assertEmpty(course_integrity_check($course->id
)); // Not an error!
2180 $section0 = $DB->get_record('course_sections', array('course' => $course->id
, 'section' => 0));
2181 $section1 = $DB->get_record('course_sections', array('course' => $course->id
, 'section' => 1));
2182 $cms = $DB->get_records('course_modules', array('course' => $course->id
), 'id', 'id,section');
2183 $this->assertEquals($forum->cmid
. ','. $quiz->cmid
, $section0->sequence
);
2184 $this->assertEmpty($section1->sequence
);
2185 $this->assertEquals($section0->id
, $cms[$forum->cmid
]->section
);
2186 $this->assertEquals($section1->id
, $cms[$page->cmid
]->section
); // Not changed.
2187 $this->assertEquals($section0->id
, $cms[$quiz->cmid
]->section
);
2189 // 4. Module id is not present in course_section.sequence (integrity check with $fullcheck = true).
2190 $this->assertEquals(array('Failed integrity check for course ['. $course->id
. ']. Course module ['.
2191 $page->cmid
. '] is missing from sequence of section ['. $section1->id
. ']'),
2192 course_integrity_check($course->id
, null, null, true)); // Error!
2193 $section0 = $DB->get_record('course_sections', array('course' => $course->id
, 'section' => 0));
2194 $section1 = $DB->get_record('course_sections', array('course' => $course->id
, 'section' => 1));
2195 $cms = $DB->get_records('course_modules', array('course' => $course->id
), 'id', 'id,section');
2196 $this->assertEquals($forum->cmid
. ','. $quiz->cmid
, $section0->sequence
);
2197 $this->assertEquals(''. $page->cmid
, $section1->sequence
); // Yay, module added to section.
2198 $this->assertEquals($section0->id
, $cms[$forum->cmid
]->section
);
2199 $this->assertEquals($section1->id
, $cms[$page->cmid
]->section
); // Not changed.
2200 $this->assertEquals($section0->id
, $cms[$quiz->cmid
]->section
);
2202 // 5. Module id is not present in course_section.sequence and it's section is invalid (integrity check with $fullcheck = true).
2203 $DB->update_record('course_modules', array('id' => $page->cmid
, 'section' => 8765));
2204 $DB->update_record('course_sections', array('id' => $section1->id
, 'sequence' => ''));
2205 $this->assertEquals(array(
2206 'Failed integrity check for course ['. $course->id
. ']. Course module ['. $page->cmid
.
2207 '] is missing from sequence of section ['. $section0->id
. ']',
2208 'Failed integrity check for course ['. $course->id
. ']. Course module ['. $page->cmid
.
2209 '] points to section [8765] instead of ['. $section0->id
. ']'),
2210 course_integrity_check($course->id
, null, null, true));
2211 $section0 = $DB->get_record('course_sections', array('course' => $course->id
, 'section' => 0));
2212 $section1 = $DB->get_record('course_sections', array('course' => $course->id
, 'section' => 1));
2213 $cms = $DB->get_records('course_modules', array('course' => $course->id
), 'id', 'id,section');
2214 $this->assertEquals($forum->cmid
. ','. $quiz->cmid
. ','. $page->cmid
, $section0->sequence
); // Module added to section.
2215 $this->assertEquals($section0->id
, $cms[$forum->cmid
]->section
);
2216 $this->assertEquals($section0->id
, $cms[$page->cmid
]->section
); // Section changed to section0.
2217 $this->assertEquals($section0->id
, $cms[$quiz->cmid
]->section
);
2219 // 6. Module is deleted from course_modules but not deleted in sequence (integrity check with $fullcheck = true).
2220 $DB->delete_records('course_modules', array('id' => $page->cmid
));
2221 $this->assertEquals(array('Failed integrity check for course ['. $course->id
. ']. Course module ['.
2222 $page->cmid
. '] does not exist but is present in the sequence of section ['. $section0->id
. ']'),
2223 course_integrity_check($course->id
, null, null, true));
2224 $section0 = $DB->get_record('course_sections', array('course' => $course->id
, 'section' => 0));
2225 $section1 = $DB->get_record('course_sections', array('course' => $course->id
, 'section' => 1));
2226 $cms = $DB->get_records('course_modules', array('course' => $course->id
), 'id', 'id,section');
2227 $this->assertEquals($forum->cmid
. ','. $quiz->cmid
, $section0->sequence
);
2228 $this->assertEmpty($section1->sequence
);
2229 $this->assertEquals($section0->id
, $cms[$forum->cmid
]->section
);
2230 $this->assertEquals($section0->id
, $cms[$quiz->cmid
]->section
);
2231 $this->assertEquals(2, count($cms));
2235 * Tests for event related to course module creation.
2237 public function test_course_module_created_event() {
2239 $this->resetAfterTest();
2241 // Create an assign module.
2242 $sink = $this->redirectEvents();
2243 $modinfo = $this->create_specific_module_test('assign');
2244 $events = $sink->get_events();
2245 $event = array_pop($events);
2247 $cm = get_coursemodule_from_id('assign', $modinfo->coursemodule
, 0, false, MUST_EXIST
);
2248 $mod = $DB->get_record('assign', array('id' => $modinfo->instance
), '*', MUST_EXIST
);
2250 // Validate event data.
2251 $this->assertInstanceOf('\core\event\course_module_created', $event);
2252 $this->assertEquals($cm->id
, $event->objectid
);
2253 $this->assertEquals($USER->id
, $event->userid
);
2254 $this->assertEquals('course_modules', $event->objecttable
);
2255 $url = new moodle_url('/mod/assign/view.php', array('id' => $cm->id
));
2256 $this->assertEquals($url, $event->get_url());
2258 // Test legacy data.
2259 $this->assertSame('mod_created', $event->get_legacy_eventname());
2260 $eventdata = new stdClass();
2261 $eventdata->modulename
= 'assign';
2262 $eventdata->name
= $mod->name
;
2263 $eventdata->cmid
= $cm->id
;
2264 $eventdata->courseid
= $cm->course
;
2265 $eventdata->userid
= $USER->id
;
2266 $this->assertEventLegacyData($eventdata, $event);
2269 array($cm->course
, "course", "add mod", "../mod/assign/view.php?id=$cm->id", "assign $cm->instance"),
2270 array($cm->course
, "assign", "add", "view.php?id=$cm->id", $cm->instance
, $cm->id
)
2272 $this->assertEventLegacyLogData($arr, $event);
2273 $this->assertEventContextNotUsed($event);
2275 // Let us see if duplicating an activity results in a nice course module created event.
2277 $course = get_course($mod->course
);
2278 $newcm = duplicate_module($course, $cm);
2279 $events = $sink->get_events();
2280 $event = array_pop($events);
2283 // Validate event data.
2284 $this->assertInstanceOf('\core\event\course_module_created', $event);
2285 $this->assertEquals($newcm->id
, $event->objectid
);
2286 $this->assertEquals($USER->id
, $event->userid
);
2287 $this->assertEquals($course->id
, $event->courseid
);
2288 $url = new moodle_url('/mod/assign/view.php', array('id' => $newcm->id
));
2289 $this->assertEquals($url, $event->get_url());
2293 * Tests for event validations related to course module creation.
2295 public function test_course_module_created_event_exceptions() {
2297 $this->resetAfterTest();
2300 $modinfo = $this->create_specific_module_test('assign');
2301 $context = context_module
::instance($modinfo->coursemodule
);
2303 // Test not setting instanceid.
2305 $event = \core\event\course_module_created
::create(array(
2306 'courseid' => $modinfo->course
,
2307 'context' => $context,
2308 'objectid' => $modinfo->coursemodule
,
2310 'modulename' => 'assign',
2311 'name' => 'My assignment',
2314 $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2315 other['instanceid']");
2316 } catch (coding_exception
$e) {
2317 $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
2320 // Test not setting modulename.
2322 $event = \core\event\course_module_created
::create(array(
2323 'courseid' => $modinfo->course
,
2324 'context' => $context,
2325 'objectid' => $modinfo->coursemodule
,
2327 'instanceid' => $modinfo->instance
,
2328 'name' => 'My assignment',
2331 $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2332 other['modulename']");
2333 } catch (coding_exception
$e) {
2334 $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
2337 // Test not setting name.
2340 $event = \core\event\course_module_created
::create(array(
2341 'courseid' => $modinfo->course
,
2342 'context' => $context,
2343 'objectid' => $modinfo->coursemodule
,
2345 'modulename' => 'assign',
2346 'instanceid' => $modinfo->instance
,
2349 $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2351 } catch (coding_exception
$e) {
2352 $this->assertContains("The 'name' value must be set in other.", $e->getMessage());
2358 * Tests for event related to course module updates.
2360 public function test_course_module_updated_event() {
2362 $this->resetAfterTest();
2364 // Update a forum module.
2365 $sink = $this->redirectEvents();
2366 $modinfo = $this->update_specific_module_test('forum');
2367 $events = $sink->get_events();
2368 $event = array_pop($events);
2371 $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule
), '*', MUST_EXIST
);
2372 $mod = $DB->get_record('forum', array('id' => $cm->instance
), '*', MUST_EXIST
);
2374 // Validate event data.
2375 $this->assertInstanceOf('\core\event\course_module_updated', $event);
2376 $this->assertEquals($cm->id
, $event->objectid
);
2377 $this->assertEquals($USER->id
, $event->userid
);
2378 $this->assertEquals('course_modules', $event->objecttable
);
2379 $url = new moodle_url('/mod/forum/view.php', array('id' => $cm->id
));
2380 $this->assertEquals($url, $event->get_url());
2382 // Test legacy data.
2383 $this->assertSame('mod_updated', $event->get_legacy_eventname());
2384 $eventdata = new stdClass();
2385 $eventdata->modulename
= 'forum';
2386 $eventdata->name
= $mod->name
;
2387 $eventdata->cmid
= $cm->id
;
2388 $eventdata->courseid
= $cm->course
;
2389 $eventdata->userid
= $USER->id
;
2390 $this->assertEventLegacyData($eventdata, $event);
2393 array($cm->course
, "course", "update mod", "../mod/forum/view.php?id=$cm->id", "forum $cm->instance"),
2394 array($cm->course
, "forum", "update", "view.php?id=$cm->id", $cm->instance
, $cm->id
)
2396 $this->assertEventLegacyLogData($arr, $event);
2397 $this->assertEventContextNotUsed($event);
2401 * Tests for create_from_cm method.
2403 public function test_course_module_create_from_cm() {
2404 $this->resetAfterTest();
2405 $this->setAdminUser();
2407 // Create course and modules.
2408 $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
2410 // Generate an assignment.
2411 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id
));
2413 // Get the module context.
2414 $modcontext = context_module
::instance($assign->cmid
);
2416 // Get course module.
2417 $cm = get_coursemodule_from_id(null, $assign->cmid
, $course->id
, false, MUST_EXIST
);
2419 // Create an event from course module.
2420 $event = \core\event\course_module_updated
::create_from_cm($cm, $modcontext);
2422 // Trigger the events.
2423 $sink = $this->redirectEvents();
2425 $events = $sink->get_events();
2426 $event2 = array_pop($events);
2429 $this->assertInstanceOf('\core\event\course_module_updated', $event);
2430 $this->assertEquals($cm->id
, $event2->objectid
);
2431 $this->assertEquals($modcontext, $event2->get_context());
2432 $this->assertEquals($cm->modname
, $event2->other
['modulename']);
2433 $this->assertEquals($cm->instance
, $event2->other
['instanceid']);
2434 $this->assertEquals($cm->name
, $event2->other
['name']);
2435 $this->assertEventContextNotUsed($event2);
2436 $this->assertSame('mod_updated', $event2->get_legacy_eventname());
2438 array($cm->course
, "course", "update mod", "../mod/assign/view.php?id=$cm->id", "assign $cm->instance"),
2439 array($cm->course
, "assign", "update", "view.php?id=$cm->id", $cm->instance
, $cm->id
)
2441 $this->assertEventLegacyLogData($arr, $event);
2445 * Tests for event validations related to course module update.
2447 public function test_course_module_updated_event_exceptions() {
2449 $this->resetAfterTest();
2452 $modinfo = $this->create_specific_module_test('assign');
2453 $context = context_module
::instance($modinfo->coursemodule
);
2455 // Test not setting instanceid.
2457 $event = \core\event\course_module_updated
::create(array(
2458 'courseid' => $modinfo->course
,
2459 'context' => $context,
2460 'objectid' => $modinfo->coursemodule
,
2462 'modulename' => 'assign',
2463 'name' => 'My assignment',
2466 $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2467 other['instanceid']");
2468 } catch (coding_exception
$e) {
2469 $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
2472 // Test not setting modulename.
2474 $event = \core\event\course_module_updated
::create(array(
2475 'courseid' => $modinfo->course
,
2476 'context' => $context,
2477 'objectid' => $modinfo->coursemodule
,
2479 'instanceid' => $modinfo->instance
,
2480 'name' => 'My assignment',
2483 $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2484 other['modulename']");
2485 } catch (coding_exception
$e) {
2486 $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
2489 // Test not setting name.
2492 $event = \core\event\course_module_updated
::create(array(
2493 'courseid' => $modinfo->course
,
2494 'context' => $context,
2495 'objectid' => $modinfo->coursemodule
,
2497 'modulename' => 'assign',
2498 'instanceid' => $modinfo->instance
,
2501 $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2503 } catch (coding_exception
$e) {
2504 $this->assertContains("The 'name' value must be set in other.", $e->getMessage());
2510 * Tests for event related to course module delete.
2512 public function test_course_module_deleted_event() {
2514 $this->resetAfterTest();
2516 // Create and delete a module.
2517 $sink = $this->redirectEvents();
2518 $modinfo = $this->create_specific_module_test('forum');
2519 $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule
), '*', MUST_EXIST
);
2520 course_delete_module($modinfo->coursemodule
);
2521 $events = $sink->get_events();
2522 $event = array_pop($events); // delete module event.;
2525 // Validate event data.
2526 $this->assertInstanceOf('\core\event\course_module_deleted', $event);
2527 $this->assertEquals($cm->id
, $event->objectid
);
2528 $this->assertEquals($USER->id
, $event->userid
);
2529 $this->assertEquals('course_modules', $event->objecttable
);
2530 $this->assertEquals(null, $event->get_url());
2531 $this->assertEquals($cm, $event->get_record_snapshot('course_modules', $cm->id
));
2533 // Test legacy data.
2534 $this->assertSame('mod_deleted', $event->get_legacy_eventname());
2535 $eventdata = new stdClass();
2536 $eventdata->modulename
= 'forum';
2537 $eventdata->cmid
= $cm->id
;
2538 $eventdata->courseid
= $cm->course
;
2539 $eventdata->userid
= $USER->id
;
2540 $this->assertEventLegacyData($eventdata, $event);
2542 $arr = array($cm->course
, 'course', "delete mod", "view.php?id=$cm->course", "forum $cm->instance", $cm->id
);
2543 $this->assertEventLegacyLogData($arr, $event);
2548 * Tests for event validations related to course module deletion.
2550 public function test_course_module_deleted_event_exceptions() {
2552 $this->resetAfterTest();
2555 $modinfo = $this->create_specific_module_test('assign');
2556 $context = context_module
::instance($modinfo->coursemodule
);
2558 // Test not setting instanceid.
2560 $event = \core\event\course_module_deleted
::create(array(
2561 'courseid' => $modinfo->course
,
2562 'context' => $context,
2563 'objectid' => $modinfo->coursemodule
,
2565 'modulename' => 'assign',
2566 'name' => 'My assignment',
2569 $this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
2570 other['instanceid']");
2571 } catch (coding_exception
$e) {
2572 $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
2575 // Test not setting modulename.
2577 $event = \core\event\course_module_deleted
::create(array(
2578 'courseid' => $modinfo->course
,
2579 'context' => $context,
2580 'objectid' => $modinfo->coursemodule
,
2582 'instanceid' => $modinfo->instance
,
2583 'name' => 'My assignment',
2586 $this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
2587 other['modulename']");
2588 } catch (coding_exception
$e) {
2589 $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
2594 * Returns a user object and its assigned new role.
2596 * @param testing_data_generator $generator
2598 * @return array The user object and the role ID
2600 protected function get_user_objects(testing_data_generator
$generator, $contextid) {
2603 if (empty($USER->id
)) {
2604 $user = $generator->create_user();
2605 $this->setUser($user);
2607 $roleid = create_role('Test role', 'testrole', 'Test role description');
2608 if (!is_array($contextid)) {
2609 $contextid = array($contextid);
2611 foreach ($contextid as $cid) {
2612 $assignid = role_assign($roleid, $user->id
, $cid);
2614 return array($user, $roleid);
2618 * Test course move after course.
2620 public function test_course_change_sortorder_after_course() {
2623 $this->resetAfterTest(true);
2625 $generator = $this->getDataGenerator();
2626 $category = $generator->create_category();
2627 $course3 = $generator->create_course(array('category' => $category->id
));
2628 $course2 = $generator->create_course(array('category' => $category->id
));
2629 $course1 = $generator->create_course(array('category' => $category->id
));
2630 $context = $category->get_context();
2632 list($user, $roleid) = $this->get_user_objects($generator, $context->id
);
2633 $caps = course_capability_assignment
::allow('moodle/category:manage', $roleid, $context->id
);
2635 $courses = $category->get_courses();
2636 $this->assertInternalType('array', $courses);
2637 $this->assertEquals(array($course1->id
, $course2->id
, $course3->id
), array_keys($courses));
2638 $dbcourses = $DB->get_records('course', array('category' => $category->id
), 'sortorder', 'id');
2639 $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2641 // Test moving down.
2642 $this->assertTrue(course_change_sortorder_after_course($course1->id
, $course3->id
));
2643 $courses = $category->get_courses();
2644 $this->assertInternalType('array', $courses);
2645 $this->assertEquals(array($course2->id
, $course3->id
, $course1->id
), array_keys($courses));
2646 $dbcourses = $DB->get_records('course', array('category' => $category->id
), 'sortorder', 'id');
2647 $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2650 $this->assertTrue(course_change_sortorder_after_course($course1->id
, $course2->id
));
2651 $courses = $category->get_courses();
2652 $this->assertInternalType('array', $courses);
2653 $this->assertEquals(array($course2->id
, $course1->id
, $course3->id
), array_keys($courses));
2654 $dbcourses = $DB->get_records('course', array('category' => $category->id
), 'sortorder', 'id');
2655 $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2657 // Test moving to the top.
2658 $this->assertTrue(course_change_sortorder_after_course($course1->id
, 0));
2659 $courses = $category->get_courses();
2660 $this->assertInternalType('array', $courses);
2661 $this->assertEquals(array($course1->id
, $course2->id
, $course3->id
), array_keys($courses));
2662 $dbcourses = $DB->get_records('course', array('category' => $category->id
), 'sortorder', 'id');
2663 $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2667 * Tests changing the visibility of a course.
2669 public function test_course_change_visibility() {
2672 $this->resetAfterTest(true);
2674 $generator = $this->getDataGenerator();
2675 $category = $generator->create_category();
2676 $course = $generator->create_course(array('category' => $category->id
));
2678 $this->assertEquals('1', $course->visible
);
2679 $this->assertEquals('1', $course->visibleold
);
2681 $this->assertTrue(course_change_visibility($course->id
, false));
2682 $course = $DB->get_record('course', array('id' => $course->id
));
2683 $this->assertEquals('0', $course->visible
);
2684 $this->assertEquals('0', $course->visibleold
);
2686 $this->assertTrue(course_change_visibility($course->id
, true));
2687 $course = $DB->get_record('course', array('id' => $course->id
));
2688 $this->assertEquals('1', $course->visible
);
2689 $this->assertEquals('1', $course->visibleold
);
2693 * Tests moving the course up and down by one.
2695 public function test_course_change_sortorder_by_one() {
2698 $this->resetAfterTest(true);
2700 $generator = $this->getDataGenerator();
2701 $category = $generator->create_category();
2702 $course3 = $generator->create_course(array('category' => $category->id
));
2703 $course2 = $generator->create_course(array('category' => $category->id
));
2704 $course1 = $generator->create_course(array('category' => $category->id
));
2706 $courses = $category->get_courses();
2707 $this->assertInternalType('array', $courses);
2708 $this->assertEquals(array($course1->id
, $course2->id
, $course3->id
), array_keys($courses));
2709 $dbcourses = $DB->get_records('course', array('category' => $category->id
), 'sortorder', 'id');
2710 $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2712 // Test moving down.
2713 $course1 = get_course($course1->id
);
2714 $this->assertTrue(course_change_sortorder_by_one($course1, false));
2715 $courses = $category->get_courses();
2716 $this->assertInternalType('array', $courses);
2717 $this->assertEquals(array($course2->id
, $course1->id
, $course3->id
), array_keys($courses));
2718 $dbcourses = $DB->get_records('course', array('category' => $category->id
), 'sortorder', 'id');
2719 $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2722 $course1 = get_course($course1->id
);
2723 $this->assertTrue(course_change_sortorder_by_one($course1, true));
2724 $courses = $category->get_courses();
2725 $this->assertInternalType('array', $courses);
2726 $this->assertEquals(array($course1->id
, $course2->id
, $course3->id
), array_keys($courses));
2727 $dbcourses = $DB->get_records('course', array('category' => $category->id
), 'sortorder', 'id');
2728 $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2730 // Test moving the top course up one.
2731 $course1 = get_course($course1->id
);
2732 $this->assertFalse(course_change_sortorder_by_one($course1, true));
2733 // Check nothing changed.
2734 $courses = $category->get_courses();
2735 $this->assertInternalType('array', $courses);
2736 $this->assertEquals(array($course1->id
, $course2->id
, $course3->id
), array_keys($courses));
2737 $dbcourses = $DB->get_records('course', array('category' => $category->id
), 'sortorder', 'id');
2738 $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2740 // Test moving the bottom course up down.
2741 $course3 = get_course($course3->id
);
2742 $this->assertFalse(course_change_sortorder_by_one($course3, false));
2743 // Check nothing changed.
2744 $courses = $category->get_courses();
2745 $this->assertInternalType('array', $courses);
2746 $this->assertEquals(array($course1->id
, $course2->id
, $course3->id
), array_keys($courses));
2747 $dbcourses = $DB->get_records('course', array('category' => $category->id
), 'sortorder', 'id');
2748 $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2751 public function test_view_resources_list() {
2752 $this->resetAfterTest();
2754 $course = self
::getDataGenerator()->create_course();
2755 $coursecontext = context_course
::instance($course->id
);
2757 $event = \core\event\course_resources_list_viewed
::create(array('context' => context_course
::instance($course->id
)));
2758 $event->set_legacy_logdata(array('book', 'page', 'resource'));
2759 $sink = $this->redirectEvents();
2761 $events = $sink->get_events();
2764 // Validate the event.
2765 $event = $events[0];
2766 $this->assertInstanceOf('\core\event\course_resources_list_viewed', $event);
2767 $this->assertEquals(null, $event->objecttable
);
2768 $this->assertEquals(null, $event->objectid
);
2769 $this->assertEquals($course->id
, $event->courseid
);
2770 $this->assertEquals($coursecontext->id
, $event->contextid
);
2771 $expectedlegacydata = array(
2772 array($course->id
, "book", "view all", 'index.php?id=' . $course->id
, ''),
2773 array($course->id
, "page", "view all", 'index.php?id=' . $course->id
, ''),
2774 array($course->id
, "resource", "view all", 'index.php?id=' . $course->id
, ''),
2776 $this->assertEventLegacyLogData($expectedlegacydata, $event);
2777 $this->assertEventContextNotUsed($event);
2781 * Test duplicate_module()
2783 public function test_duplicate_module() {
2784 $this->setAdminUser();
2785 $this->resetAfterTest();
2786 $course = self
::getDataGenerator()->create_course();
2787 $res = self
::getDataGenerator()->create_module('resource', array('course' => $course));
2788 $cm = get_coursemodule_from_id('resource', $res->cmid
, 0, false, MUST_EXIST
);
2790 $newcm = duplicate_module($course, $cm);
2792 // Make sure they are the same, except obvious id changes.
2793 foreach ($cm as $prop => $value) {
2794 if ($prop == 'id' ||
$prop == 'url' ||
$prop == 'instance' ||
$prop == 'added') {
2795 // Ignore obviously different properties.
2798 $this->assertEquals($value, $newcm->$prop);
2803 * Tests that when creating or updating a module, if the availability settings
2804 * are present but set to an empty tree, availability is set to null in
2807 public function test_empty_availability_settings() {
2809 $this->setAdminUser();
2810 $this->resetAfterTest();
2812 // Enable availability.
2813 set_config('enableavailability', 1);
2816 $emptyavailability = json_encode(\core_availability\tree
::get_root_json(array()));
2817 $course = self
::getDataGenerator()->create_course();
2818 $label = self
::getDataGenerator()->create_module('label', array(
2819 'course' => $course, 'availability' => $emptyavailability));
2820 $this->assertNull($DB->get_field('course_modules', 'availability',
2821 array('id' => $label->cmid
)));
2824 $formdata = $DB->get_record('course_modules', array('id' => $label->cmid
));
2825 unset($formdata->availability
);
2826 $formdata->availabilityconditionsjson
= $emptyavailability;
2827 $formdata->modulename
= 'label';
2828 $formdata->coursemodule
= $label->cmid
;
2830 file_prepare_draft_area($draftid, context_module
::instance($label->cmid
)->id
,
2831 'mod_label', 'intro', 0);
2832 $formdata->introeditor
= array(
2833 'itemid' => $draftid,
2834 'text' => '<p>Yo</p>',
2835 'format' => FORMAT_HTML
);
2836 update_module($formdata);
2837 $this->assertNull($DB->get_field('course_modules', 'availability',
2838 array('id' => $label->cmid
)));
2842 * Test update_inplace_editable()
2844 public function test_update_module_name_inplace() {
2845 global $CFG, $DB, $PAGE;
2846 require_once($CFG->dirroot
. '/lib/external/externallib.php');
2848 $this->setUser($this->getDataGenerator()->create_user());
2850 $this->resetAfterTest(true);
2851 $course = $this->getDataGenerator()->create_course();
2852 $forum = self
::getDataGenerator()->create_module('forum', array('course' => $course->id
, 'name' => 'forum name'));
2854 // Call service for core_course component without necessary permissions.
2856 core_external
::update_inplace_editable('core_course', 'activityname', $forum->cmid
, 'New forum name');
2857 $this->fail('Exception expected');
2858 } catch (moodle_exception
$e) {
2859 $this->assertEquals('Course or activity not accessible. (Not enrolled)',
2863 // Change to admin user and make sure that cm name can be updated using web service update_inplace_editable().
2864 $this->setAdminUser();
2865 $res = core_external
::update_inplace_editable('core_course', 'activityname', $forum->cmid
, 'New forum name');
2866 $res = external_api
::clean_returnvalue(core_external
::update_inplace_editable_returns(), $res);
2867 $this->assertEquals('New forum name', $res['value']);
2868 $this->assertEquals('New forum name', $DB->get_field('forum', 'name', array('id' => $forum->id
)));
2872 * Testing function course_get_tagged_course_modules - search tagged course modules
2874 public function test_course_get_tagged_course_modules() {
2876 $this->resetAfterTest();
2877 $course3 = $this->getDataGenerator()->create_course();
2878 $course2 = $this->getDataGenerator()->create_course();
2879 $course1 = $this->getDataGenerator()->create_course();
2880 $cm11 = $this->getDataGenerator()->create_module('assign', array('course' => $course1->id
,
2881 'tags' => 'Cat, Dog'));
2882 $cm12 = $this->getDataGenerator()->create_module('page', array('course' => $course1->id
,
2883 'tags' => 'Cat, Mouse', 'visible' => 0));
2884 $cm13 = $this->getDataGenerator()->create_module('page', array('course' => $course1->id
,
2885 'tags' => 'Cat, Mouse, Dog'));
2886 $cm21 = $this->getDataGenerator()->create_module('forum', array('course' => $course2->id
,
2887 'tags' => 'Cat, Mouse'));
2888 $cm31 = $this->getDataGenerator()->create_module('forum', array('course' => $course3->id
,
2889 'tags' => 'Cat, Mouse'));
2891 // Admin is able to view everything.
2892 $this->setAdminUser();
2893 $res = course_get_tagged_course_modules(core_tag_tag
::get_by_name(0, 'Cat'),
2894 /*$exclusivemode = */false, /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$page = */0);
2895 $this->assertRegExp('/'.$cm11->name
.'/', $res->content
);
2896 $this->assertRegExp('/'.$cm12->name
.'/', $res->content
);
2897 $this->assertRegExp('/'.$cm13->name
.'/', $res->content
);
2898 $this->assertRegExp('/'.$cm21->name
.'/', $res->content
);
2899 $this->assertRegExp('/'.$cm31->name
.'/', $res->content
);
2900 // Results from course1 are returned before results from course2.
2901 $this->assertTrue(strpos($res->content
, $cm11->name
) < strpos($res->content
, $cm21->name
));
2903 // Ordinary user is not able to see anything.
2904 $user = $this->getDataGenerator()->create_user();
2905 $this->setUser($user);
2907 $res = course_get_tagged_course_modules(core_tag_tag
::get_by_name(0, 'Cat'),
2908 /*$exclusivemode = */false, /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$page = */0);
2909 $this->assertNull($res);
2911 // Enrol user as student in course1 and course2.
2912 $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
2913 $this->getDataGenerator()->enrol_user($user->id
, $course1->id
, $roleids['student']);
2914 $this->getDataGenerator()->enrol_user($user->id
, $course2->id
, $roleids['student']);
2915 core_tag_index_builder
::reset_caches();
2917 // Searching in the course context returns visible modules in this course.
2918 $context = context_course
::instance($course1->id
);
2919 $res = course_get_tagged_course_modules(core_tag_tag
::get_by_name(0, 'Cat'),
2920 /*$exclusivemode = */false, /*$fromctx = */0, /*$ctx = */$context->id
, /*$rec = */1, /*$page = */0);
2921 $this->assertRegExp('/'.$cm11->name
.'/', $res->content
);
2922 $this->assertNotRegExp('/'.$cm12->name
.'/', $res->content
);
2923 $this->assertRegExp('/'.$cm13->name
.'/', $res->content
);
2924 $this->assertNotRegExp('/'.$cm21->name
.'/', $res->content
);
2925 $this->assertNotRegExp('/'.$cm31->name
.'/', $res->content
);
2927 // Searching FROM the course context returns visible modules in all courses.
2928 $context = context_course
::instance($course2->id
);
2929 $res = course_get_tagged_course_modules(core_tag_tag
::get_by_name(0, 'Cat'),
2930 /*$exclusivemode = */false, /*$fromctx = */$context->id
, /*$ctx = */0, /*$rec = */1, /*$page = */0);
2931 $this->assertRegExp('/'.$cm11->name
.'/', $res->content
);
2932 $this->assertNotRegExp('/'.$cm12->name
.'/', $res->content
);
2933 $this->assertRegExp('/'.$cm13->name
.'/', $res->content
);
2934 $this->assertRegExp('/'.$cm21->name
.'/', $res->content
);
2935 $this->assertNotRegExp('/'.$cm31->name
.'/', $res->content
); // No access to course3.
2936 // Results from course2 are returned before results from course1.
2937 $this->assertTrue(strpos($res->content
, $cm21->name
) < strpos($res->content
, $cm11->name
));
2939 // Enrol user in course1 as a teacher - now he should be able to see hidden module.
2940 $this->getDataGenerator()->enrol_user($user->id
, $course1->id
, $roleids['editingteacher']);
2941 get_fast_modinfo(0,0,true);
2943 $context = context_course
::instance($course1->id
);
2944 $res = course_get_tagged_course_modules(core_tag_tag
::get_by_name(0, 'Cat'),
2945 /*$exclusivemode = */false, /*$fromctx = */$context->id
, /*$ctx = */0, /*$rec = */1, /*$page = */0);
2946 $this->assertRegExp('/'.$cm12->name
.'/', $res->content
);
2948 // Create more modules and try pagination.
2949 $cm14 = $this->getDataGenerator()->create_module('assign', array('course' => $course1->id
,
2950 'tags' => 'Cat, Dog'));
2951 $cm15 = $this->getDataGenerator()->create_module('page', array('course' => $course1->id
,
2952 'tags' => 'Cat, Mouse', 'visible' => 0));
2953 $cm16 = $this->getDataGenerator()->create_module('page', array('course' => $course1->id
,
2954 'tags' => 'Cat, Mouse, Dog'));
2956 $context = context_course
::instance($course1->id
);
2957 $res = course_get_tagged_course_modules(core_tag_tag
::get_by_name(0, 'Cat'),
2958 /*$exclusivemode = */false, /*$fromctx = */0, /*$ctx = */$context->id
, /*$rec = */1, /*$page = */0);
2959 $this->assertRegExp('/'.$cm11->name
.'/', $res->content
);
2960 $this->assertRegExp('/'.$cm12->name
.'/', $res->content
);
2961 $this->assertRegExp('/'.$cm13->name
.'/', $res->content
);
2962 $this->assertNotRegExp('/'.$cm21->name
.'/', $res->content
);
2963 $this->assertRegExp('/'.$cm14->name
.'/', $res->content
);
2964 $this->assertRegExp('/'.$cm15->name
.'/', $res->content
);
2965 $this->assertNotRegExp('/'.$cm16->name
.'/', $res->content
);
2966 $this->assertNotRegExp('/'.$cm31->name
.'/', $res->content
); // No access to course3.
2967 $this->assertEmpty($res->prevpageurl
);
2968 $this->assertNotEmpty($res->nextpageurl
);
2970 $res = course_get_tagged_course_modules(core_tag_tag
::get_by_name(0, 'Cat'),
2971 /*$exclusivemode = */false, /*$fromctx = */0, /*$ctx = */$context->id
, /*$rec = */1, /*$page = */1);
2972 $this->assertNotRegExp('/'.$cm11->name
.'/', $res->content
);
2973 $this->assertNotRegExp('/'.$cm12->name
.'/', $res->content
);
2974 $this->assertNotRegExp('/'.$cm13->name
.'/', $res->content
);
2975 $this->assertNotRegExp('/'.$cm21->name
.'/', $res->content
);
2976 $this->assertNotRegExp('/'.$cm14->name
.'/', $res->content
);
2977 $this->assertNotRegExp('/'.$cm15->name
.'/', $res->content
);
2978 $this->assertRegExp('/'.$cm16->name
.'/', $res->content
);
2979 $this->assertNotRegExp('/'.$cm31->name
.'/', $res->content
); // No access to course3.
2980 $this->assertNotEmpty($res->prevpageurl
);
2981 $this->assertEmpty($res->nextpageurl
);
2985 * Test course_get_user_navigation_options for frontpage.
2987 public function test_course_get_user_navigation_options_for_frontpage() {
2988 global $CFG, $SITE, $DB;
2989 $this->resetAfterTest();
2990 $context = context_system
::instance();
2991 $course = clone $SITE;
2992 $this->setAdminUser();
2994 $navoptions = course_get_user_navigation_options($context, $course);
2995 $this->assertTrue($navoptions->blogs
);
2996 $this->assertTrue($navoptions->notes
);
2997 $this->assertTrue($navoptions->participants
);
2998 $this->assertTrue($navoptions->badges
);
2999 $this->assertTrue($navoptions->tags
);
3000 $this->assertFalse($navoptions->search
);
3001 $this->assertTrue($navoptions->calendar
);
3002 $this->assertTrue($navoptions->competencies
);
3004 // Enable global search now.
3005 $CFG->enableglobalsearch
= 1;
3006 $navoptions = course_get_user_navigation_options($context, $course);
3007 $this->assertTrue($navoptions->search
);
3009 // Disable competencies.
3010 $oldcompetencies = get_config('core_competency', 'enabled');
3011 set_config('enabled', false, 'core_competency');
3012 $navoptions = course_get_user_navigation_options($context, $course);
3013 $this->assertFalse($navoptions->competencies
);
3014 set_config('enabled', $oldcompetencies, 'core_competency');
3016 // Now try with a standard user.
3017 $user = $this->getDataGenerator()->create_user();
3018 $this->setUser($user);
3019 $navoptions = course_get_user_navigation_options($context, $course);
3020 $this->assertTrue($navoptions->blogs
);
3021 $this->assertFalse($navoptions->notes
);
3022 $this->assertFalse($navoptions->participants
);
3023 $this->assertTrue($navoptions->badges
);
3024 $this->assertTrue($navoptions->tags
);
3025 $this->assertTrue($navoptions->search
);
3026 $this->assertTrue($navoptions->calendar
);
3030 * Test course_get_user_navigation_options for managers in a normal course.
3032 public function test_course_get_user_navigation_options_for_managers() {
3034 $this->resetAfterTest();
3035 $course = $this->getDataGenerator()->create_course();
3036 $context = context_course
::instance($course->id
);
3037 $this->setAdminUser();
3039 $navoptions = course_get_user_navigation_options($context);
3040 $this->assertTrue($navoptions->blogs
);
3041 $this->assertTrue($navoptions->notes
);
3042 $this->assertTrue($navoptions->participants
);
3043 $this->assertTrue($navoptions->badges
);
3047 * Test course_get_user_navigation_options for students in a normal course.
3049 public function test_course_get_user_navigation_options_for_students() {
3051 $this->resetAfterTest();
3052 $course = $this->getDataGenerator()->create_course();
3053 $context = context_course
::instance($course->id
);
3055 $user = $this->getDataGenerator()->create_user();
3056 $roleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
3057 $this->getDataGenerator()->enrol_user($user->id
, $course->id
, $roleid);
3059 $this->setUser($user);
3061 $navoptions = course_get_user_navigation_options($context);
3062 $this->assertTrue($navoptions->blogs
);
3063 $this->assertFalse($navoptions->notes
);
3064 $this->assertTrue($navoptions->participants
);
3065 $this->assertTrue($navoptions->badges
);
3067 // Disable some options.
3068 $CFG->badges_allowcoursebadges
= 0;
3069 $CFG->enableblogs
= 0;
3070 // Disable view participants capability.
3071 assign_capability('moodle/course:viewparticipants', CAP_PROHIBIT
, $roleid, $context);
3072 $context->mark_dirty();
3074 $navoptions = course_get_user_navigation_options($context);
3075 $this->assertFalse($navoptions->blogs
);
3076 $this->assertFalse($navoptions->notes
);
3077 $this->assertFalse($navoptions->participants
);
3078 $this->assertFalse($navoptions->badges
);
3082 * Test course_get_user_administration_options for frontpage.
3084 public function test_course_get_user_administration_options_for_frontpage() {
3086 $this->resetAfterTest();
3087 $course = clone $SITE;
3088 $context = context_course
::instance($course->id
);
3089 $this->setAdminUser();
3091 $adminoptions = course_get_user_administration_options($course, $context);
3092 $this->assertTrue($adminoptions->update
);
3093 $this->assertTrue($adminoptions->filters
);
3094 $this->assertTrue($adminoptions->reports
);
3095 $this->assertTrue($adminoptions->backup
);
3096 $this->assertTrue($adminoptions->restore
);
3097 $this->assertFalse($adminoptions->files
);
3098 $this->assertFalse($adminoptions->tags
);
3100 // Now try with a standard user.
3101 $user = $this->getDataGenerator()->create_user();
3102 $this->setUser($user);
3103 $adminoptions = course_get_user_administration_options($course, $context);
3104 $this->assertFalse($adminoptions->update
);
3105 $this->assertFalse($adminoptions->filters
);
3106 $this->assertFalse($adminoptions->reports
);
3107 $this->assertFalse($adminoptions->backup
);
3108 $this->assertFalse($adminoptions->restore
);
3109 $this->assertFalse($adminoptions->files
);
3110 $this->assertFalse($adminoptions->tags
);
3115 * Test course_get_user_administration_options for managers in a normal course.
3117 public function test_course_get_user_administration_options_for_managers() {
3119 $this->resetAfterTest();
3120 $course = $this->getDataGenerator()->create_course();
3121 $context = context_course
::instance($course->id
);
3122 $this->setAdminUser();
3124 $adminoptions = course_get_user_administration_options($course, $context);
3125 $this->assertTrue($adminoptions->update
);
3126 $this->assertTrue($adminoptions->filters
);
3127 $this->assertTrue($adminoptions->reports
);
3128 $this->assertTrue($adminoptions->backup
);
3129 $this->assertTrue($adminoptions->restore
);
3130 $this->assertFalse($adminoptions->files
);
3131 $this->assertTrue($adminoptions->tags
);
3132 $this->assertTrue($adminoptions->gradebook
);
3133 $this->assertFalse($adminoptions->outcomes
);
3134 $this->assertTrue($adminoptions->badges
);
3135 $this->assertTrue($adminoptions->import
);
3136 $this->assertTrue($adminoptions->publish
);
3137 $this->assertTrue($adminoptions->reset
);
3138 $this->assertTrue($adminoptions->roles
);
3142 * Test course_get_user_administration_options for students in a normal course.
3144 public function test_course_get_user_administration_options_for_students() {
3146 $this->resetAfterTest();
3147 $course = $this->getDataGenerator()->create_course();
3148 $context = context_course
::instance($course->id
);
3150 $user = $this->getDataGenerator()->create_user();
3151 $roleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
3152 $this->getDataGenerator()->enrol_user($user->id
, $course->id
, $roleid);
3154 $this->setUser($user);
3155 $adminoptions = course_get_user_administration_options($course, $context);
3157 $this->assertFalse($adminoptions->update
);
3158 $this->assertFalse($adminoptions->filters
);
3159 $this->assertFalse($adminoptions->reports
);
3160 $this->assertFalse($adminoptions->backup
);
3161 $this->assertFalse($adminoptions->restore
);
3162 $this->assertFalse($adminoptions->files
);
3163 $this->assertFalse($adminoptions->tags
);
3164 $this->assertFalse($adminoptions->gradebook
);
3165 $this->assertFalse($adminoptions->outcomes
);
3166 $this->assertTrue($adminoptions->badges
);
3167 $this->assertFalse($adminoptions->import
);
3168 $this->assertFalse($adminoptions->publish
);
3169 $this->assertFalse($adminoptions->reset
);
3170 $this->assertFalse($adminoptions->roles
);
3172 $CFG->enablebadges
= false;
3173 $adminoptions = course_get_user_administration_options($course, $context);
3174 $this->assertFalse($adminoptions->badges
);
3178 * Test test_update_course_frontpage_category.
3180 public function test_update_course_frontpage_category() {
3181 // Fetch front page course.
3182 $course = get_course(SITEID
);
3183 // Test update information on front page course.
3184 $course->category
= 99;
3185 $this->expectException('moodle_exception');
3186 $this->expectExceptionMessage(get_string('invalidcourse', 'error'));
3187 update_course($course);
3191 * test_course_enddate
3193 * @dataProvider course_enddate_provider
3194 * @param int $startdate
3195 * @param int $enddate
3196 * @param string $errorcode
3198 public function test_course_enddate($startdate, $enddate, $errorcode) {
3200 $this->resetAfterTest(true);
3202 $record = array('startdate' => $startdate, 'enddate' => $enddate);
3204 $course1 = $this->getDataGenerator()->create_course($record);
3205 if ($errorcode !== false) {
3206 $this->fail('Expected exception with "' . $errorcode . '" error code in create_create');
3208 } catch (moodle_exception
$e) {
3209 if ($errorcode === false) {
3210 $this->fail('Got "' . $errorcode . '" exception error code and no exception was expected');
3212 if ($e->errorcode
!= $errorcode) {
3213 $this->fail('Got "' . $e->errorcode
. '" exception error code and "' . $errorcode . '" was expected');
3218 $this->assertEquals($startdate, $course1->startdate
);
3219 $this->assertEquals($enddate, $course1->enddate
);
3223 * Provider for test_course_enddate.
3227 public function course_enddate_provider() {
3228 // Each provided example contains startdate, enddate and the expected exception error code if there is any.
3237 'enddatebeforestartdate'
3245 'nostartdatenoenddate'
3252 * test_course_dates_reset
3254 * @dataProvider course_dates_reset_provider
3255 * @param int $startdate
3256 * @param int $enddate
3257 * @param int $resetstartdate
3258 * @param int $resetenddate
3259 * @param int $resultingstartdate
3260 * @param int $resultingenddate
3262 public function test_course_dates_reset($startdate, $enddate, $resetstartdate, $resetenddate, $resultingstartdate, $resultingenddate) {
3265 require_once($CFG->dirroot
.'/completion/criteria/completion_criteria_date.php');
3267 $this->resetAfterTest(true);
3269 $this->setAdminUser();
3271 $CFG->enablecompletion
= true;
3273 $this->setTimezone('UTC');
3275 $record = array('startdate' => $startdate, 'enddate' => $enddate, 'enablecompletion' => 1);
3276 $originalcourse = $this->getDataGenerator()->create_course($record);
3277 $coursecriteria = new completion_criteria_date(array('course' => $originalcourse->id
, 'timeend' => $startdate + DAYSECS
));
3278 $coursecriteria->insert();
3280 $activitycompletiondate = $startdate + DAYSECS
;
3281 $data = $this->getDataGenerator()->create_module('data', array('course' => $originalcourse->id
),
3282 array('completion' => 1, 'completionexpected' => $activitycompletiondate));
3284 $resetdata = new stdClass();
3285 $resetdata->id
= $originalcourse->id
;
3286 $resetdata->reset_start_date_old
= $originalcourse->startdate
;
3287 $resetdata->reset_start_date
= $resetstartdate;
3288 $resetdata->reset_end_date
= $resetenddate;
3289 $resetdata->reset_end_date_old
= $record['enddate'];
3290 reset_course_userdata($resetdata);
3292 $course = $DB->get_record('course', array('id' => $originalcourse->id
));
3294 $this->assertEquals($resultingstartdate, $course->startdate
);
3295 $this->assertEquals($resultingenddate, $course->enddate
);
3297 $coursecompletioncriteria = completion_criteria_date
::fetch(array('course' => $originalcourse->id
));
3298 $this->assertEquals($resultingstartdate + DAYSECS
, $coursecompletioncriteria->timeend
);
3300 $this->assertEquals($resultingstartdate + DAYSECS
, $DB->get_field('course_modules', 'completionexpected',
3301 array('id' => $data->cmid
)));
3305 * Provider for test_course_dates_reset.
3309 public function course_dates_reset_provider() {
3311 // Each example contains the following:
3312 // - course startdate
3314 // - startdate to reset to (false if not reset)
3315 // - enddate to reset to (false if not reset)
3316 // - resulting startdate
3317 // - resulting enddate
3329 // End date changes to a valid value.
3334 $time + DAYSECS +
111,
3336 $time + DAYSECS +
111
3338 // Start date changes to a valid value. End date does not get updated because it does not have value.
3347 // Start date changes to a valid value. End date gets updated accordingly.
3354 $time + WEEKSECS + DAYSECS
3356 // Start date and end date change to a valid value.
3368 public function test_course_check_module_updates_since() {
3369 global $CFG, $DB, $USER;
3370 require_once($CFG->dirroot
. '/mod/glossary/lib.php');
3371 require_once($CFG->dirroot
. '/rating/lib.php');
3372 require_once($CFG->dirroot
. '/comment/lib.php');
3374 $this->resetAfterTest(true);
3376 $CFG->enablecompletion
= true;
3377 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
3378 $glossary = $this->getDataGenerator()->create_module('glossary', array(
3379 'course' => $course->id
,
3380 'completion' => COMPLETION_TRACKING_AUTOMATIC
,
3381 'completionview' => 1,
3382 'allowcomments' => 1,
3383 'assessed' => RATING_AGGREGATE_AVERAGE
,
3386 $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
3387 $context = context_module
::instance($glossary->cmid
);
3388 $modinfo = get_fast_modinfo($course);
3389 $cm = $modinfo->get_cm($glossary->cmid
);
3390 $user = $this->getDataGenerator()->create_user();
3391 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
3392 $this->getDataGenerator()->enrol_user($user->id
, $course->id
, $studentrole->id
);
3395 $teacher = $this->getDataGenerator()->create_user();
3396 $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
3397 $this->getDataGenerator()->enrol_user($teacher->id
, $course->id
, $teacherrole->id
);
3399 assign_capability('mod/glossary:viewanyrating', CAP_ALLOW
, $studentrole->id
, $context->id
, true);
3401 // Check nothing changed right now.
3402 $updates = course_check_module_updates_since($cm, $from);
3403 $this->assertFalse($updates->configuration
->updated
);
3404 $this->assertFalse($updates->completion
->updated
);
3405 $this->assertFalse($updates->gradeitems
->updated
);
3406 $this->assertFalse($updates->comments
->updated
);
3407 $this->assertFalse($updates->ratings
->updated
);
3408 $this->assertFalse($updates->introfiles
->updated
);
3409 $this->assertFalse($updates->outcomes
->updated
);
3411 $this->waitForSecond();
3414 $this->setUser($user);
3415 $entry = $glossarygenerator->create_content($glossary);
3417 $this->setUser($teacher);
3419 set_coursemodule_name($glossary->cmid
, 'New name');
3421 // Add some ratings.
3422 $rm = new rating_manager();
3423 $result = $rm->add_rating($cm, $context, 'mod_glossary', 'entry', $entry->id
, 100, 50, $user->id
, RATING_AGGREGATE_AVERAGE
);
3426 $glossary->cmidnumber
= $glossary->cmid
;
3427 glossary_update_grades($glossary, $user->id
);
3429 $this->setUser($user);
3430 // Completion status.
3431 glossary_view($glossary, $course, $cm, $context, 'letter');
3434 $args = new stdClass
;
3435 $args->context
= $context;
3436 $args->course
= $course;
3438 $args->area
= 'glossary_entry';
3439 $args->itemid
= $entry->id
;
3440 $args->client_id
= 1;
3441 $args->component
= 'mod_glossary';
3442 $manager = new comment($args);
3443 $manager->add('blah blah blah');
3445 // Check upgrade status.
3446 $updates = course_check_module_updates_since($cm, $from);
3447 $this->assertTrue($updates->configuration
->updated
);
3448 $this->assertTrue($updates->completion
->updated
);
3449 $this->assertTrue($updates->gradeitems
->updated
);
3450 $this->assertTrue($updates->comments
->updated
);
3451 $this->assertTrue($updates->ratings
->updated
);
3452 $this->assertFalse($updates->introfiles
->updated
);
3453 $this->assertFalse($updates->outcomes
->updated
);
3456 public function test_async_module_deletion_hook_implemented() {
3457 // Async module deletion depends on the 'true' being returned by at least one plugin implementing the hook,
3458 // 'course_module_adhoc_deletion_recommended'. In core, is implemented by the course recyclebin, which will only return
3459 // true if the recyclebin plugin is enabled. To make sure async deletion occurs, this test force-enables the recyclebin.
3461 $this->resetAfterTest(true);
3462 $this->setAdminUser();
3464 // Ensure recyclebin is enabled.
3465 set_config('coursebinenable', true, 'tool_recyclebin');
3467 // Create course, module and context.
3468 $course = $this->getDataGenerator()->create_course(['numsections' => 5]);
3469 $module = $this->getDataGenerator()->create_module('assign', ['course' => $course->id
]);
3470 $modcontext = context_module
::instance($module->cmid
);
3472 // Verify context exists.
3473 $this->assertInstanceOf('context_module', $modcontext);
3475 // Check events generated on the course_delete_module call.
3476 $sink = $this->redirectEvents();
3478 // Try to delete the module using the async flag.
3479 course_delete_module($module->cmid
, true); // Try to delete the module asynchronously.
3481 // Verify that no event has been generated yet.
3482 $events = $sink->get_events();
3483 $event = array_pop($events);
3485 $this->assertEmpty($event);
3487 // Grab the record, in it's final state before hard deletion, for comparison with the event snapshot.
3488 // We need to do this because the 'deletioninprogress' flag has changed from '0' to '1'.
3489 $cm = $DB->get_record('course_modules', ['id' => $module->cmid
], '*', MUST_EXIST
);
3491 // Verify the course_module is marked as 'deletioninprogress'.
3492 $this->assertNotEquals($cm, false);
3493 $this->assertEquals($cm->deletioninprogress
, '1');
3495 // Verify the context has not yet been removed.
3496 $this->assertEquals($modcontext, context_module
::instance($module->cmid
, IGNORE_MISSING
));
3498 // Set up a sink to catch the 'course_module_deleted' event.
3499 $sink = $this->redirectEvents();
3501 // Now, run the adhoc task which performs the hard deletion.
3502 phpunit_util
::run_all_adhoc_tasks();
3504 // Fetch and validate the event data.
3505 $events = $sink->get_events();
3506 $event = array_pop($events);
3508 $this->assertInstanceOf('\core\event\course_module_deleted', $event);
3509 $this->assertEquals($module->cmid
, $event->objectid
);
3510 $this->assertEquals($USER->id
, $event->userid
);
3511 $this->assertEquals('course_modules', $event->objecttable
);
3512 $this->assertEquals(null, $event->get_url());
3513 $this->assertEquals($cm, $event->get_record_snapshot('course_modules', $module->cmid
));
3515 // Verify the context has been removed.
3516 $this->assertFalse(context_module
::instance($module->cmid
, IGNORE_MISSING
));
3518 // Verify the course_module record has been deleted.
3519 $cmcount = $DB->count_records('course_modules', ['id' => $module->cmid
]);
3520 $this->assertEmpty($cmcount);
3523 public function test_async_module_deletion_hook_not_implemented() {
3524 // Only proceed if we are sure that no plugin is going to advocate async removal of a module. I.e. no plugin returns
3525 // 'true' from the 'course_module_adhoc_deletion_recommended' hook.
3526 // In the case of core, only recyclebin implements this hook, and it will only return true if enabled, so disable it.
3528 $this->resetAfterTest(true);
3529 $this->setAdminUser();
3530 set_config('coursebinenable', false, 'tool_recyclebin');
3532 // Non-core plugins might implement the 'course_module_adhoc_deletion_recommended' hook and spoil this test.
3533 // If at least one plugin still returns true, then skip this test.
3534 if ($pluginsfunction = get_plugins_with_function('course_module_background_deletion_recommended')) {
3535 foreach ($pluginsfunction as $plugintype => $plugins) {
3536 foreach ($plugins as $pluginfunction) {
3537 if ($pluginfunction()) {
3538 $this->markTestSkipped();
3544 // Create course, module and context.
3545 $course = $this->getDataGenerator()->create_course(['numsections' => 5]);
3546 $module = $this->getDataGenerator()->create_module('assign', ['course' => $course->id
]);
3547 $modcontext = context_module
::instance($module->cmid
);
3548 $cm = $DB->get_record('course_modules', ['id' => $module->cmid
], '*', MUST_EXIST
);
3550 // Verify context exists.
3551 $this->assertInstanceOf('context_module', $modcontext);
3553 // Check events generated on the course_delete_module call.
3554 $sink = $this->redirectEvents();
3556 // Try to delete the module using the async flag.
3557 course_delete_module($module->cmid
, true); // Try to delete the module asynchronously.
3559 // Fetch and validate the event data.
3560 $events = $sink->get_events();
3561 $event = array_pop($events);
3563 $this->assertInstanceOf('\core\event\course_module_deleted', $event);
3564 $this->assertEquals($module->cmid
, $event->objectid
);
3565 $this->assertEquals($USER->id
, $event->userid
);
3566 $this->assertEquals('course_modules', $event->objecttable
);
3567 $this->assertEquals(null, $event->get_url());
3568 $this->assertEquals($cm, $event->get_record_snapshot('course_modules', $module->cmid
));
3570 // Verify the context has been removed.
3571 $this->assertFalse(context_module
::instance($module->cmid
, IGNORE_MISSING
));
3573 // Verify the course_module record has been deleted.
3574 $cmcount = $DB->count_records('course_modules', ['id' => $module->cmid
]);
3575 $this->assertEmpty($cmcount);
3578 public function test_async_section_deletion_hook_implemented() {
3579 // Async section deletion (provided section contains modules), depends on the 'true' being returned by at least one plugin
3580 // implementing the 'course_module_adhoc_deletion_recommended' hook. In core, is implemented by the course recyclebin,
3581 // which will only return true if the plugin is enabled. To make sure async deletion occurs, this test enables recyclebin.
3583 $this->resetAfterTest(true);
3584 $this->setAdminUser();
3586 // Ensure recyclebin is enabled.
3587 set_config('coursebinenable', true, 'tool_recyclebin');
3589 // Create course, module and context.
3590 $generator = $this->getDataGenerator();
3591 $course = $generator->create_course(['numsections' => 4, 'format' => 'topics'], ['createsections' => true]);
3592 $assign0 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
3593 $assign1 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
3594 $assign2 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
3595 $assign3 = $generator->create_module('assign', ['course' => $course, 'section' => 0]);
3597 // Delete empty section. No difference from normal, synchronous behaviour.
3598 $this->assertTrue(course_delete_section($course, 4, false, true));
3599 $this->assertEquals(3, course_get_format($course)->get_last_section_number());
3601 // Delete a module in section 2 (using async). Need to verify this doesn't generate two tasks when we delete
3602 // the section in the next step.
3603 course_delete_module($assign2->cmid
, true);
3605 // Confirm that the module is pending deletion in its current section.
3606 $section = $DB->get_record('course_sections', ['course' => $course->id
, 'section' => '2']); // For event comparison.
3607 $this->assertEquals(true, $DB->record_exists('course_modules', ['id' => $assign2->cmid
, 'deletioninprogress' => 1,
3608 'section' => $section->id
]));
3610 // Now, delete section 2.
3611 $this->assertFalse(course_delete_section($course, 2, false, true)); // Non-empty section, no forcedelete, so no change.
3613 $sink = $this->redirectEvents(); // To capture the event.
3614 $this->assertTrue(course_delete_section($course, 2, true, true));
3616 // Now, confirm that:
3617 // a) the section's modules have been flagged for deletion and moved to section 0 and;
3618 // b) the section has been deleted and;
3619 // c) course_section_deleted event has been fired. The course_module_deleted events will only fire once they have been
3620 // removed from section 0 via the adhoc task.
3622 // Modules should have been flagged for deletion and moved to section 0.
3623 $sectionid = $DB->get_field('course_sections', 'id', ['course' => $course->id
, 'section' => 0]);
3624 $this->assertEquals(3, $DB->count_records('course_modules', ['section' => $sectionid, 'deletioninprogress' => 1]));
3626 // Confirm the section has been deleted.
3627 $this->assertEquals(2, course_get_format($course)->get_last_section_number());
3629 // Check event fired.
3630 $events = $sink->get_events();
3631 $event = array_pop($events);
3633 $this->assertInstanceOf('\core\event\course_section_deleted', $event);
3634 $this->assertEquals($section->id
, $event->objectid
);
3635 $this->assertEquals($USER->id
, $event->userid
);
3636 $this->assertEquals('course_sections', $event->objecttable
);
3637 $this->assertEquals(null, $event->get_url());
3638 $this->assertEquals($section, $event->get_record_snapshot('course_sections', $section->id
));
3640 // Now, run the adhoc task to delete the modules from section 0.
3641 $sink = $this->redirectEvents(); // To capture the events.
3642 phpunit_util
::run_all_adhoc_tasks();
3644 // Confirm the modules have been deleted.
3645 list($insql, $assignids) = $DB->get_in_or_equal([$assign0->cmid
, $assign1->cmid
, $assign2->cmid
]);
3646 $cmcount = $DB->count_records_select('course_modules', 'id ' . $insql, $assignids);
3647 $this->assertEmpty($cmcount);
3649 // Confirm other modules in section 0 still remain.
3650 $this->assertEquals(1, $DB->count_records('course_modules', ['id' => $assign3->cmid
]));
3652 // Confirm that events were generated for all 3 of the modules.
3653 $events = $sink->get_events();
3656 while (!empty($events)) {
3657 $event = array_pop($events);
3658 if ($event instanceof \core\event\course_module_deleted
&&
3659 in_array($event->objectid
, [$assign0->cmid
, $assign1->cmid
, $assign2->cmid
])) {
3663 $this->assertEquals(3, $count);
3666 public function test_async_section_deletion_hook_not_implemented() {
3667 // If no plugins advocate async removal, then normal synchronous removal will take place.
3668 // Only proceed if we are sure that no plugin is going to advocate async removal of a module. I.e. no plugin returns
3669 // 'true' from the 'course_module_adhoc_deletion_recommended' hook.
3670 // In the case of core, only recyclebin implements this hook, and it will only return true if enabled, so disable it.
3672 $this->resetAfterTest(true);
3673 $this->setAdminUser();
3674 set_config('coursebinenable', false, 'tool_recyclebin');
3676 // Non-core plugins might implement the 'course_module_adhoc_deletion_recommended' hook and spoil this test.
3677 // If at least one plugin still returns true, then skip this test.
3678 if ($pluginsfunction = get_plugins_with_function('course_module_background_deletion_recommended')) {
3679 foreach ($pluginsfunction as $plugintype => $plugins) {
3680 foreach ($plugins as $pluginfunction) {
3681 if ($pluginfunction()) {
3682 $this->markTestSkipped();
3688 // Create course, module and context.
3689 $generator = $this->getDataGenerator();
3690 $course = $generator->create_course(['numsections' => 4, 'format' => 'topics'], ['createsections' => true]);
3691 $assign0 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
3692 $assign1 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
3694 // Delete empty section. No difference from normal, synchronous behaviour.
3695 $this->assertTrue(course_delete_section($course, 4, false, true));
3696 $this->assertEquals(3, course_get_format($course)->get_last_section_number());
3698 // Delete section in the middle (2).
3699 $section = $DB->get_record('course_sections', ['course' => $course->id
, 'section' => '2']); // For event comparison.
3700 $this->assertFalse(course_delete_section($course, 2, false, true)); // Non-empty section, no forcedelete, so no change.
3702 $sink = $this->redirectEvents(); // To capture the event.
3703 $this->assertTrue(course_delete_section($course, 2, true, true));
3705 // Now, confirm that:
3706 // a) The section's modules have deleted and;
3707 // b) the section has been deleted and;
3708 // c) course_section_deleted event has been fired and;
3709 // d) course_module_deleted events have both been fired.
3711 // Confirm modules have been deleted.
3712 list($insql, $assignids) = $DB->get_in_or_equal([$assign0->cmid
, $assign1->cmid
]);
3713 $cmcount = $DB->count_records_select('course_modules', 'id ' . $insql, $assignids);
3714 $this->assertEmpty($cmcount);
3716 // Confirm the section has been deleted.
3717 $this->assertEquals(2, course_get_format($course)->get_last_section_number());
3719 // Confirm the course_section_deleted event has been generated.
3720 $events = $sink->get_events();
3721 $event = array_pop($events);
3723 $this->assertInstanceOf('\core\event\course_section_deleted', $event);
3724 $this->assertEquals($section->id
, $event->objectid
);
3725 $this->assertEquals($USER->id
, $event->userid
);
3726 $this->assertEquals('course_sections', $event->objecttable
);
3727 $this->assertEquals(null, $event->get_url());
3728 $this->assertEquals($section, $event->get_record_snapshot('course_sections', $section->id
));
3730 // Confirm that the course_module_deleted events have both been generated.
3732 while (!empty($events)) {
3733 $event = array_pop($events);
3734 if ($event instanceof \core\event\course_module_deleted
&&
3735 in_array($event->objectid
, [$assign0->cmid
, $assign1->cmid
])) {
3739 $this->assertEquals(2, $count);
3742 public function test_classify_course_for_timeline() {
3745 require_once($CFG->dirroot
.'/completion/criteria/completion_criteria_self.php');
3747 set_config('enablecompletion', COMPLETION_ENABLED
);
3749 $this->resetAfterTest(true);
3750 $this->setAdminUser();
3752 // Create courses for testing.
3753 $generator = $this->getDataGenerator();
3754 $future = time() +
3600;
3755 $past = time() - 3600;
3756 $futurecourse = $generator->create_course(['startdate' => $future]);
3757 $pastcourse = $generator->create_course(['startdate' => $past - 60, 'enddate' => $past]);
3758 $completedcourse = $generator->create_course(['enablecompletion' => COMPLETION_ENABLED
]);
3759 $inprogresscourse = $generator->create_course();
3761 // Set completion rules.
3762 $criteriadata = new stdClass();
3763 $criteriadata->id
= $completedcourse->id
;
3766 $criteriadata->criteria_self
= COMPLETION_CRITERIA_TYPE_SELF
;
3767 $class = 'completion_criteria_self';
3768 $criterion = new $class();
3769 $criterion->update_config($criteriadata);
3771 $user = $this->getDataGenerator()->create_user();
3772 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
3773 $this->getDataGenerator()->enrol_user($user->id
, $futurecourse->id
, $studentrole->id
);
3774 $this->getDataGenerator()->enrol_user($user->id
, $pastcourse->id
, $studentrole->id
);
3775 $this->getDataGenerator()->enrol_user($user->id
, $completedcourse->id
, $studentrole->id
);
3776 $this->getDataGenerator()->enrol_user($user->id
, $inprogresscourse->id
, $studentrole->id
);
3778 $this->setUser($user);
3779 core_completion_external
::mark_course_self_completed($completedcourse->id
);
3780 $ccompletion = new completion_completion(array('course' => $completedcourse->id
, 'userid' => $user->id
));
3781 $ccompletion->mark_complete();
3783 // Aggregate the completions.
3784 $this->assertEquals(COURSE_TIMELINE_PAST
, course_classify_for_timeline($pastcourse));
3785 $this->assertEquals(COURSE_TIMELINE_FUTURE
, course_classify_for_timeline($futurecourse));
3786 $this->assertEquals(COURSE_TIMELINE_PAST
, course_classify_for_timeline($completedcourse));
3787 $this->assertEquals(COURSE_TIMELINE_INPROGRESS
, course_classify_for_timeline($inprogresscourse));
3791 * Test the main function for updating all calendar events for a module.
3793 public function test_course_module_calendar_event_update_process() {
3796 $this->resetAfterTest();
3797 $this->setAdminUser();
3799 $completionexpected = time();
3802 $course = $this->getDataGenerator()->create_course(['enablecompletion' => COMPLETION_ENABLED
]);
3803 $assign = $this->getDataGenerator()->create_module('assign', [
3804 'course' => $course,
3805 'completionexpected' => $completionexpected,
3806 'duedate' => $duedate
3809 $cm = get_coursemodule_from_instance('assign', $assign->id
, $course->id
);
3810 $events = $DB->get_records('event', ['courseid' => $course->id
, 'instance' => $assign->id
]);
3811 // Check that both events are using the expected dates.
3812 foreach ($events as $event) {
3813 if ($event->eventtype
== \core_completion\api
::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED
) {
3814 $this->assertEquals($completionexpected, $event->timestart
);
3816 if ($event->eventtype
== ASSIGN_EVENT_TYPE_DUE
) {
3817 $this->assertEquals($duedate, $event->timestart
);
3821 // We have to manually update the module and the course module.
3822 $newcompletionexpected = time() + DAYSECS
* 60;
3823 $newduedate = time() + DAYSECS
* 45;
3824 $newmodulename = 'Assign - new name';
3826 $moduleobject = (object)array('id' => $assign->id
, 'duedate' => $newduedate, 'name' => $newmodulename);
3827 $DB->update_record('assign', $moduleobject);
3828 $cmobject = (object)array('id' => $cm->id
, 'completionexpected' => $newcompletionexpected);
3829 $DB->update_record('course_modules', $cmobject);
3831 $assign = $DB->get_record('assign', ['id' => $assign->id
]);
3832 $cm = get_coursemodule_from_instance('assign', $assign->id
, $course->id
);
3834 course_module_calendar_event_update_process($assign, $cm);
3836 $events = $DB->get_records('event', ['courseid' => $course->id
, 'instance' => $assign->id
]);
3837 // Now check that the details have been updated properly from the function.
3838 foreach ($events as $event) {
3839 if ($event->eventtype
== \core_completion\api
::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED
) {
3840 $this->assertEquals($newcompletionexpected, $event->timestart
);
3841 $this->assertEquals(get_string('completionexpectedfor', 'completion', (object)['instancename' => $newmodulename]),
3844 if ($event->eventtype
== ASSIGN_EVENT_TYPE_DUE
) {
3845 $this->assertEquals($newduedate, $event->timestart
);
3846 $this->assertEquals($newmodulename, $event->name
);
3852 * Test the higher level checks for updating calendar events for an instance.
3854 public function test_course_module_update_calendar_events() {
3855 $this->resetAfterTest();
3856 $this->setAdminUser();
3858 $completionexpected = time();
3861 $course = $this->getDataGenerator()->create_course(['enablecompletion' => COMPLETION_ENABLED
]);
3862 $assign = $this->getDataGenerator()->create_module('assign', [
3863 'course' => $course,
3864 'completionexpected' => $completionexpected,
3865 'duedate' => $duedate
3868 $cm = get_coursemodule_from_instance('assign', $assign->id
, $course->id
);
3870 // Both the instance and cm objects are missing.
3871 $this->assertFalse(course_module_update_calendar_events('assign'));
3872 // Just using the assign instance.
3873 $this->assertTrue(course_module_update_calendar_events('assign', $assign));
3874 // Just using the course module object.
3875 $this->assertTrue(course_module_update_calendar_events('assign', null, $cm));
3876 // Using both the assign instance and the course module object.
3877 $this->assertTrue(course_module_update_calendar_events('assign', $assign, $cm));
3881 * Test the higher level checks for updating calendar events for a module.
3883 public function test_course_module_bulk_update_calendar_events() {
3884 $this->resetAfterTest();
3885 $this->setAdminUser();
3887 $completionexpected = time();
3890 $course = $this->getDataGenerator()->create_course(['enablecompletion' => COMPLETION_ENABLED
]);
3891 $course2 = $this->getDataGenerator()->create_course(['enablecompletion' => COMPLETION_ENABLED
]);
3892 $assign = $this->getDataGenerator()->create_module('assign', [
3893 'course' => $course,
3894 'completionexpected' => $completionexpected,
3895 'duedate' => $duedate
3898 // No assign instances in this course.
3899 $this->assertFalse(course_module_bulk_update_calendar_events('assign', $course2->id
));
3900 // No book instances for the site.
3901 $this->assertFalse(course_module_bulk_update_calendar_events('book'));
3902 // Update all assign instances.
3903 $this->assertTrue(course_module_bulk_update_calendar_events('assign'));
3904 // Update the assign instances for this course.
3905 $this->assertTrue(course_module_bulk_update_calendar_events('assign', $course->id
));
3909 * Test that a student can view participants in a course they are enrolled in.
3911 public function test_course_can_view_participants_as_student() {
3912 $this->resetAfterTest();
3914 $course = $this->getDataGenerator()->create_course();
3915 $coursecontext = context_course
::instance($course->id
);
3917 $user = $this->getDataGenerator()->create_user();
3918 $this->getDataGenerator()->enrol_user($user->id
, $course->id
);
3920 $this->setUser($user);
3922 $this->assertTrue(course_can_view_participants($coursecontext));
3926 * Test that a student in a course can not view participants on the site.
3928 public function test_course_can_view_participants_as_student_on_site() {
3929 $this->resetAfterTest();
3931 $course = $this->getDataGenerator()->create_course();
3933 $user = $this->getDataGenerator()->create_user();
3934 $this->getDataGenerator()->enrol_user($user->id
, $course->id
);
3936 $this->setUser($user);
3938 $this->assertFalse(course_can_view_participants(context_system
::instance()));
3942 * Test that an admin can view participants on the site.
3944 public function test_course_can_view_participants_as_admin_on_site() {
3945 $this->resetAfterTest();
3947 $this->setAdminUser();
3949 $this->assertTrue(course_can_view_participants(context_system
::instance()));
3953 * Test teachers can view participants in a course they are enrolled in.
3955 public function test_course_can_view_participants_as_teacher() {
3958 $this->resetAfterTest();
3960 $course = $this->getDataGenerator()->create_course();
3961 $coursecontext = context_course
::instance($course->id
);
3963 $user = $this->getDataGenerator()->create_user();
3964 $roleid = $DB->get_field('role', 'id', array('shortname' => 'editingteacher'));
3965 $this->getDataGenerator()->enrol_user($user->id
, $course->id
, $roleid);
3967 $this->setUser($user);
3969 $this->assertTrue(course_can_view_participants($coursecontext));
3973 * Check the teacher can still view the participants page without the 'viewparticipants' cap.
3975 public function test_course_can_view_participants_as_teacher_without_view_participants_cap() {
3978 $this->resetAfterTest();
3980 $course = $this->getDataGenerator()->create_course();
3981 $coursecontext = context_course
::instance($course->id
);
3983 $user = $this->getDataGenerator()->create_user();
3984 $roleid = $DB->get_field('role', 'id', array('shortname' => 'editingteacher'));
3985 $this->getDataGenerator()->enrol_user($user->id
, $course->id
, $roleid);
3987 $this->setUser($user);
3989 // Disable one of the capabilties.
3990 assign_capability('moodle/course:viewparticipants', CAP_PROHIBIT
, $roleid, $coursecontext);
3992 // Should still be able to view the page as they have the 'moodle/course:enrolreview' cap.
3993 $this->assertTrue(course_can_view_participants($coursecontext));
3997 * Check the teacher can still view the participants page without the 'moodle/course:enrolreview' cap.
3999 public function test_course_can_view_participants_as_teacher_without_enrol_review_cap() {
4002 $this->resetAfterTest();
4004 $course = $this->getDataGenerator()->create_course();
4005 $coursecontext = context_course
::instance($course->id
);
4007 $user = $this->getDataGenerator()->create_user();
4008 $roleid = $DB->get_field('role', 'id', array('shortname' => 'editingteacher'));
4009 $this->getDataGenerator()->enrol_user($user->id
, $course->id
, $roleid);
4011 $this->setUser($user);
4013 // Disable one of the capabilties.
4014 assign_capability('moodle/course:enrolreview', CAP_PROHIBIT
, $roleid, $coursecontext);
4016 // Should still be able to view the page as they have the 'moodle/course:viewparticipants' cap.
4017 $this->assertTrue(course_can_view_participants($coursecontext));
4021 * Check the teacher can not view the participants page without the required caps.
4023 public function test_course_can_view_participants_as_teacher_without_required_caps() {
4026 $this->resetAfterTest();
4028 $course = $this->getDataGenerator()->create_course();
4029 $coursecontext = context_course
::instance($course->id
);
4031 $user = $this->getDataGenerator()->create_user();
4032 $roleid = $DB->get_field('role', 'id', array('shortname' => 'editingteacher'));
4033 $this->getDataGenerator()->enrol_user($user->id
, $course->id
, $roleid);
4035 $this->setUser($user);
4037 // Disable the capabilities.
4038 assign_capability('moodle/course:viewparticipants', CAP_PROHIBIT
, $roleid, $coursecontext);
4039 assign_capability('moodle/course:enrolreview', CAP_PROHIBIT
, $roleid, $coursecontext);
4041 $this->assertFalse(course_can_view_participants($coursecontext));
4045 * Check that an exception is not thrown if we can view the participants page.
4047 public function test_course_require_view_participants() {
4048 $this->resetAfterTest();
4050 $course = $this->getDataGenerator()->create_course();
4051 $coursecontext = context_course
::instance($course->id
);
4053 $user = $this->getDataGenerator()->create_user();
4054 $this->getDataGenerator()->enrol_user($user->id
, $course->id
);
4056 $this->setUser($user);
4058 course_require_view_participants($coursecontext);
4062 * Check that an exception is thrown if we can't view the participants page.
4064 public function test_course_require_view_participants_as_student_on_site() {
4065 $this->resetAfterTest();
4067 $course = $this->getDataGenerator()->create_course();
4069 $user = $this->getDataGenerator()->create_user();
4070 $this->getDataGenerator()->enrol_user($user->id
, $course->id
);
4072 $this->setUser($user);
4074 $this->expectException('required_capability_exception');
4075 course_require_view_participants(context_system
::instance());
4079 * Testing the can_download_from_backup_filearea fn.
4081 public function test_can_download_from_backup_filearea() {
4083 $this->resetAfterTest();
4084 $course = $this->getDataGenerator()->create_course();
4085 $context = context_course
::instance($course->id
);
4086 $user = $this->getDataGenerator()->create_user();
4087 $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
4088 $this->getDataGenerator()->enrol_user($user->id
, $course->id
, $teacherrole->id
);
4090 // The 'automated' backup area. Downloading from this area requires two capabilities.
4091 // If the user has only the 'backup:downloadfile' capability.
4092 unassign_capability('moodle/restore:userinfo', $teacherrole->id
, $context);
4093 assign_capability('moodle/backup:downloadfile', CAP_ALLOW
, $teacherrole->id
, $context);
4094 $this->assertFalse(can_download_from_backup_filearea('automated', $context, $user));
4096 // If the user has only the 'restore:userinfo' capability.
4097 unassign_capability('moodle/backup:downloadfile', $teacherrole->id
, $context);
4098 assign_capability('moodle/restore:userinfo', CAP_ALLOW
, $teacherrole->id
, $context);
4099 $this->assertFalse(can_download_from_backup_filearea('automated', $context, $user));
4101 // If the user has both capabilities.
4102 assign_capability('moodle/backup:downloadfile', CAP_ALLOW
, $teacherrole->id
, $context);
4103 assign_capability('moodle/restore:userinfo', CAP_ALLOW
, $teacherrole->id
, $context);
4104 $this->assertTrue(can_download_from_backup_filearea('automated', $context, $user));
4106 // Is the user has neither of the capabilities.
4107 unassign_capability('moodle/backup:downloadfile', $teacherrole->id
, $context);
4108 unassign_capability('moodle/restore:userinfo', $teacherrole->id
, $context);
4109 $this->assertFalse(can_download_from_backup_filearea('automated', $context, $user));
4111 // The 'course ' and 'backup' backup file areas. These are governed by the same download capability.
4112 // User has the capability.
4113 unassign_capability('moodle/restore:userinfo', $teacherrole->id
, $context);
4114 assign_capability('moodle/backup:downloadfile', CAP_ALLOW
, $teacherrole->id
, $context);
4115 $this->assertTrue(can_download_from_backup_filearea('course', $context, $user));
4116 $this->assertTrue(can_download_from_backup_filearea('backup', $context, $user));
4118 // User doesn't have the capability.
4119 unassign_capability('moodle/backup:downloadfile', $teacherrole->id
, $context);
4120 $this->assertFalse(can_download_from_backup_filearea('course', $context, $user));
4121 $this->assertFalse(can_download_from_backup_filearea('backup', $context, $user));
4123 // A file area that doesn't exist. No permissions, regardless of capabilities.
4124 assign_capability('moodle/backup:downloadfile', CAP_ALLOW
, $teacherrole->id
, $context);
4125 $this->assertFalse(can_download_from_backup_filearea('testing', $context, $user));