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');
32 require_once($CFG->dirroot . '/tag/lib.php');
34 class core_course_courselib_testcase extends advanced_testcase {
37 * Tidy up open files that may be left open.
39 protected function tearDown() {
44 * Set forum specific test values for calling create_module().
46 * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
48 private function forum_create_set_values(&$moduleinfo) {
49 // Completion specific to forum - optional.
50 $moduleinfo->completionposts = 3;
51 $moduleinfo->completiondiscussions = 1;
52 $moduleinfo->completionreplies = 2;
54 // Specific values to the Forum module.
55 $moduleinfo->forcesubscribe = FORUM_INITIALSUBSCRIBE;
56 $moduleinfo->type = 'single';
57 $moduleinfo->trackingtype = FORUM_TRACKING_FORCED;
58 $moduleinfo->maxbytes = 10240;
59 $moduleinfo->maxattachments = 2;
61 // Post threshold for blocking - specific to forum.
62 $moduleinfo->blockperiod = 60*60*24;
63 $moduleinfo->blockafter = 10;
64 $moduleinfo->warnafter = 5;
68 * Execute test asserts on the saved DB data by create_module($forum).
70 * @param object $moduleinfo - the specific forum values that were used to create a forum.
71 * @param object $dbmodinstance - the DB values of the created forum.
73 private function forum_create_run_asserts($moduleinfo, $dbmodinstance) {
74 // Compare values specific to forums.
75 $this->assertEquals($moduleinfo->forcesubscribe, $dbmodinstance->forcesubscribe);
76 $this->assertEquals($moduleinfo->type, $dbmodinstance->type);
77 $this->assertEquals($moduleinfo->assessed, $dbmodinstance->assessed);
78 $this->assertEquals($moduleinfo->completionposts, $dbmodinstance->completionposts);
79 $this->assertEquals($moduleinfo->completiondiscussions, $dbmodinstance->completiondiscussions);
80 $this->assertEquals($moduleinfo->completionreplies, $dbmodinstance->completionreplies);
81 $this->assertEquals($moduleinfo->scale, $dbmodinstance->scale);
82 $this->assertEquals($moduleinfo->assesstimestart, $dbmodinstance->assesstimestart);
83 $this->assertEquals($moduleinfo->assesstimefinish, $dbmodinstance->assesstimefinish);
84 $this->assertEquals($moduleinfo->rsstype, $dbmodinstance->rsstype);
85 $this->assertEquals($moduleinfo->rssarticles, $dbmodinstance->rssarticles);
86 $this->assertEquals($moduleinfo->trackingtype, $dbmodinstance->trackingtype);
87 $this->assertEquals($moduleinfo->maxbytes, $dbmodinstance->maxbytes);
88 $this->assertEquals($moduleinfo->maxattachments, $dbmodinstance->maxattachments);
89 $this->assertEquals($moduleinfo->blockperiod, $dbmodinstance->blockperiod);
90 $this->assertEquals($moduleinfo->blockafter, $dbmodinstance->blockafter);
91 $this->assertEquals($moduleinfo->warnafter, $dbmodinstance->warnafter);
95 * Set assign module specific test values for calling create_module().
97 * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
99 private function assign_create_set_values(&$moduleinfo) {
100 // Specific values to the Assign module.
101 $moduleinfo->alwaysshowdescription = true;
102 $moduleinfo->submissiondrafts = true;
103 $moduleinfo->requiresubmissionstatement = true;
104 $moduleinfo->sendnotifications = true;
105 $moduleinfo->sendlatenotifications = true;
106 $moduleinfo->duedate = time() + (7 * 24 * 3600);
107 $moduleinfo->cutoffdate = time() + (7 * 24 * 3600);
108 $moduleinfo->allowsubmissionsfromdate = time();
109 $moduleinfo->teamsubmission = true;
110 $moduleinfo->requireallteammemberssubmit = true;
111 $moduleinfo->teamsubmissiongroupingid = true;
112 $moduleinfo->blindmarking = true;
113 $moduleinfo->markingworkflow = true;
114 $moduleinfo->markingallocation = true;
115 $moduleinfo->assignsubmission_onlinetext_enabled = true;
116 $moduleinfo->assignsubmission_file_enabled = true;
117 $moduleinfo->assignsubmission_file_maxfiles = 1;
118 $moduleinfo->assignsubmission_file_maxsizebytes = 1000000;
119 $moduleinfo->assignsubmission_comments_enabled = true;
120 $moduleinfo->assignfeedback_comments_enabled = true;
121 $moduleinfo->assignfeedback_offline_enabled = true;
122 $moduleinfo->assignfeedback_file_enabled = true;
125 $gradingmethods = grading_manager::available_methods();
126 $moduleinfo->advancedgradingmethod_submissions = current(array_keys($gradingmethods));
130 * Execute test asserts on the saved DB data by create_module($assign).
132 * @param object $moduleinfo - the specific assign module values that were used to create an assign module.
133 * @param object $dbmodinstance - the DB values of the created assign module.
135 private function assign_create_run_asserts($moduleinfo, $dbmodinstance) {
138 $this->assertEquals($moduleinfo->alwaysshowdescription, $dbmodinstance->alwaysshowdescription);
139 $this->assertEquals($moduleinfo->submissiondrafts, $dbmodinstance->submissiondrafts);
140 $this->assertEquals($moduleinfo->requiresubmissionstatement, $dbmodinstance->requiresubmissionstatement);
141 $this->assertEquals($moduleinfo->sendnotifications, $dbmodinstance->sendnotifications);
142 $this->assertEquals($moduleinfo->duedate, $dbmodinstance->duedate);
143 $this->assertEquals($moduleinfo->cutoffdate, $dbmodinstance->cutoffdate);
144 $this->assertEquals($moduleinfo->allowsubmissionsfromdate, $dbmodinstance->allowsubmissionsfromdate);
145 $this->assertEquals($moduleinfo->teamsubmission, $dbmodinstance->teamsubmission);
146 $this->assertEquals($moduleinfo->requireallteammemberssubmit, $dbmodinstance->requireallteammemberssubmit);
147 $this->assertEquals($moduleinfo->teamsubmissiongroupingid, $dbmodinstance->teamsubmissiongroupingid);
148 $this->assertEquals($moduleinfo->blindmarking, $dbmodinstance->blindmarking);
149 $this->assertEquals($moduleinfo->markingworkflow, $dbmodinstance->markingworkflow);
150 $this->assertEquals($moduleinfo->markingallocation, $dbmodinstance->markingallocation);
151 // The goal not being to fully test assign_add_instance() we'll stop here for the assign tests - to avoid too many DB queries.
154 $cm = get_coursemodule_from_instance('assign', $dbmodinstance->id);
155 $contextmodule = context_module::instance($cm->id);
156 $advancedgradingmethod = $DB->get_record('grading_areas',
157 array('contextid' => $contextmodule->id,
158 'activemethod' => $moduleinfo->advancedgradingmethod_submissions));
159 $this->assertEquals($moduleinfo->advancedgradingmethod_submissions, $advancedgradingmethod);
163 * Run some asserts test for a specific module for the function create_module().
165 * The function has been created (and is called) for $this->test_create_module().
166 * Note that the call to MODULE_create_set_values and MODULE_create_run_asserts are done after the common set values/run asserts.
167 * So if you want, you can overwrite the default values/asserts in the respective functions.
168 * @param string $modulename Name of the module ('forum', 'assign', 'book'...).
170 private function create_specific_module_test($modulename) {
173 $this->resetAfterTest(true);
175 $this->setAdminUser();
177 // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
178 require_once($CFG->dirroot.'/mod/'. $modulename .'/lib.php');
180 // Enable avaibility.
181 // If not enabled all conditional fields will be ignored.
182 set_config('enableavailability', 1);
184 // Enable course completion.
185 // If not enabled all completion settings will be ignored.
186 set_config('enablecompletion', COMPLETION_ENABLED);
188 // Enable forum RSS feeds.
189 set_config('enablerssfeeds', 1);
190 set_config('forum_enablerssfeeds', 1);
192 $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED),
193 array('createsections'=>true));
195 $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
197 // Create assign module instance for test.
198 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
199 $params['course'] = $course->id;
200 $instance = $generator->create_instance($params);
201 $assigncm = get_coursemodule_from_instance('assign', $instance->id);
203 // Module test values.
204 $moduleinfo = new stdClass();
206 // Always mandatory generic values to any module.
207 $moduleinfo->modulename = $modulename;
208 $moduleinfo->section = 1; // This is the section number in the course. Not the section id in the database.
209 $moduleinfo->course = $course->id;
210 $moduleinfo->groupingid = $grouping->id;
211 $moduleinfo->visible = true;
213 // Sometimes optional generic values for some modules.
214 $moduleinfo->name = 'My test module';
215 $moduleinfo->showdescription = 1; // standard boolean
216 require_once($CFG->libdir . '/gradelib.php');
217 $gradecats = grade_get_categories_menu($moduleinfo->course, false);
218 $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
219 $moduleinfo->gradecat = $gradecatid;
220 $moduleinfo->groupmode = VISIBLEGROUPS;
221 $moduleinfo->cmidnumber = 'idnumber_XXX';
223 // Completion common to all module.
224 $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC;
225 $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED;
226 $moduleinfo->completiongradeitemnumber = 1;
227 $moduleinfo->completionexpected = time() + (7 * 24 * 3600);
229 // Conditional activity.
230 $moduleinfo->availability = '{"op":"&","showc":[true,true],"c":[' .
231 '{"type":"date","d":">=","t":' . time() . '},' .
232 '{"type":"date","d":"<","t":' . (time() + (7 * 24 * 3600)) . '}' .
234 $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself.
235 $moduleinfo->conditiongradegroup = array(array('conditiongradeitemid' => $coursegradeitem->id, 'conditiongrademin' => 10, 'conditiongrademax' => 80));
236 $moduleinfo->conditionfieldgroup = array(array('conditionfield' => 'email', 'conditionfieldoperator' => \availability_profile\condition::OP_CONTAINS, 'conditionfieldvalue' => '@'));
237 $moduleinfo->conditioncompletiongroup = array(array('conditionsourcecmid' => $assigncm->id, 'conditionrequiredcompletion' => COMPLETION_COMPLETE)); // "conditionsourcecmid == 0" => none
239 // Grading and Advanced grading.
240 require_once($CFG->dirroot . '/rating/lib.php');
241 $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE;
242 $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number.
243 $moduleinfo->assesstimestart = time();
244 $moduleinfo->assesstimefinish = time() + (7 * 24 * 3600);
247 $moduleinfo->rsstype = 2;
248 $moduleinfo->rssarticles = 10;
250 // Optional intro editor (depends of module).
252 file_prepare_draft_area($draftid_editor, null, null, null, null);
253 $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
255 // Following is the advanced grading method area called 'submissions' for the 'assign' module.
256 if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
257 $moduleinfo->grade = 100;
260 // Plagiarism form values.
261 // No plagiarism plugin installed by default. Use this space to make your own test.
263 // Values specific to the module.
264 $modulesetvalues = $modulename.'_create_set_values';
265 $this->$modulesetvalues($moduleinfo);
267 // Create the module.
268 $result = create_module($moduleinfo);
270 // Retrieve the module info.
271 $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance));
272 $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance);
273 // We passed the course section number to create_courses but $dbcm contain the section id.
274 // We need to retrieve the db course section number.
275 $section = $DB->get_record('course_sections', array('course' => $dbcm->course, 'id' => $dbcm->section));
276 // Retrieve the grade item.
277 $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course,
278 'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename));
280 // Compare the values common to all module instances.
281 $this->assertEquals($moduleinfo->modulename, $dbcm->modname);
282 $this->assertEquals($moduleinfo->section, $section->section);
283 $this->assertEquals($moduleinfo->course, $dbcm->course);
284 $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
285 $this->assertEquals($moduleinfo->visible, $dbcm->visible);
286 $this->assertEquals($moduleinfo->completion, $dbcm->completion);
287 $this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
288 $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber);
289 $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected);
290 $this->assertEquals($moduleinfo->availability, $dbcm->availability);
291 $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription);
292 $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode);
293 $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber);
294 $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid);
296 // Optional grade testing.
297 if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
298 $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade);
301 // Some optional (but quite common) to some module.
302 $this->assertEquals($moduleinfo->name, $dbmodinstance->name);
303 $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro);
304 $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat);
306 // Test specific to the module.
307 $modulerunasserts = $modulename.'_create_run_asserts';
308 $this->$modulerunasserts($moduleinfo, $dbmodinstance);
313 * Test create_module() for multiple modules defined in the $modules array (first declaration of the function).
315 public function test_create_module() {
316 // Add the module name you want to test here.
317 // Create the match MODULENAME_create_set_values() and MODULENAME_create_run_asserts().
318 $modules = array('forum', 'assign');
320 foreach ($modules as $modulename) {
321 $this->create_specific_module_test($modulename);
326 * Test update_module() for multiple modules defined in the $modules array (first declaration of the function).
328 public function test_update_module() {
329 // Add the module name you want to test here.
330 // Create the match MODULENAME_update_set_values() and MODULENAME_update_run_asserts().
331 $modules = array('forum');
333 foreach ($modules as $modulename) {
334 $this->update_specific_module_test($modulename);
339 * Set forum specific test values for calling update_module().
341 * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
343 private function forum_update_set_values(&$moduleinfo) {
344 // Completion specific to forum - optional.
345 $moduleinfo->completionposts = 3;
346 $moduleinfo->completiondiscussions = 1;
347 $moduleinfo->completionreplies = 2;
349 // Specific values to the Forum module.
350 $moduleinfo->forcesubscribe = FORUM_INITIALSUBSCRIBE;
351 $moduleinfo->type = 'single';
352 $moduleinfo->trackingtype = FORUM_TRACKING_FORCED;
353 $moduleinfo->maxbytes = 10240;
354 $moduleinfo->maxattachments = 2;
356 // Post threshold for blocking - specific to forum.
357 $moduleinfo->blockperiod = 60*60*24;
358 $moduleinfo->blockafter = 10;
359 $moduleinfo->warnafter = 5;
363 * Execute test asserts on the saved DB data by update_module($forum).
365 * @param object $moduleinfo - the specific forum values that were used to update a forum.
366 * @param object $dbmodinstance - the DB values of the updated forum.
368 private function forum_update_run_asserts($moduleinfo, $dbmodinstance) {
369 // Compare values specific to forums.
370 $this->assertEquals($moduleinfo->forcesubscribe, $dbmodinstance->forcesubscribe);
371 $this->assertEquals($moduleinfo->type, $dbmodinstance->type);
372 $this->assertEquals($moduleinfo->assessed, $dbmodinstance->assessed);
373 $this->assertEquals($moduleinfo->completionposts, $dbmodinstance->completionposts);
374 $this->assertEquals($moduleinfo->completiondiscussions, $dbmodinstance->completiondiscussions);
375 $this->assertEquals($moduleinfo->completionreplies, $dbmodinstance->completionreplies);
376 $this->assertEquals($moduleinfo->scale, $dbmodinstance->scale);
377 $this->assertEquals($moduleinfo->assesstimestart, $dbmodinstance->assesstimestart);
378 $this->assertEquals($moduleinfo->assesstimefinish, $dbmodinstance->assesstimefinish);
379 $this->assertEquals($moduleinfo->rsstype, $dbmodinstance->rsstype);
380 $this->assertEquals($moduleinfo->rssarticles, $dbmodinstance->rssarticles);
381 $this->assertEquals($moduleinfo->trackingtype, $dbmodinstance->trackingtype);
382 $this->assertEquals($moduleinfo->maxbytes, $dbmodinstance->maxbytes);
383 $this->assertEquals($moduleinfo->maxattachments, $dbmodinstance->maxattachments);
384 $this->assertEquals($moduleinfo->blockperiod, $dbmodinstance->blockperiod);
385 $this->assertEquals($moduleinfo->blockafter, $dbmodinstance->blockafter);
386 $this->assertEquals($moduleinfo->warnafter, $dbmodinstance->warnafter);
392 * Test a specific type of module.
394 * @param string $modulename - the module name to test
396 private function update_specific_module_test($modulename) {
399 $this->resetAfterTest(true);
401 $this->setAdminUser();
403 // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
404 require_once($CFG->dirroot.'/mod/'. $modulename .'/lib.php');
406 // Enable avaibility.
407 // If not enabled all conditional fields will be ignored.
408 set_config('enableavailability', 1);
410 // Enable course completion.
411 // If not enabled all completion settings will be ignored.
412 set_config('enablecompletion', COMPLETION_ENABLED);
414 // Enable forum RSS feeds.
415 set_config('enablerssfeeds', 1);
416 set_config('forum_enablerssfeeds', 1);
418 $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED),
419 array('createsections'=>true));
421 $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
423 // Create assign module instance for testing gradeitem.
424 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
425 $params['course'] = $course->id;
426 $instance = $generator->create_instance($params);
427 $assigncm = get_coursemodule_from_instance('assign', $instance->id);
429 // Create the test forum to update.
430 $initvalues = new stdClass();
431 $initvalues->introformat = FORMAT_HTML;
432 $initvalues->course = $course->id;
433 $forum = self::getDataGenerator()->create_module('forum', $initvalues);
435 // Retrieve course module.
436 $cm = get_coursemodule_from_instance('forum', $forum->id);
438 // Module test values.
439 $moduleinfo = new stdClass();
441 // Always mandatory generic values to any module.
442 $moduleinfo->coursemodule = $cm->id;
443 $moduleinfo->modulename = $modulename;
444 $moduleinfo->course = $course->id;
445 $moduleinfo->groupingid = $grouping->id;
446 $moduleinfo->visible = true;
448 // Sometimes optional generic values for some modules.
449 $moduleinfo->name = 'My test module';
450 $moduleinfo->showdescription = 1; // standard boolean
451 require_once($CFG->libdir . '/gradelib.php');
452 $gradecats = grade_get_categories_menu($moduleinfo->course, false);
453 $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
454 $moduleinfo->gradecat = $gradecatid;
455 $moduleinfo->groupmode = VISIBLEGROUPS;
456 $moduleinfo->cmidnumber = 'idnumber_XXX';
458 // Completion common to all module.
459 $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC;
460 $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED;
461 $moduleinfo->completiongradeitemnumber = 1;
462 $moduleinfo->completionexpected = time() + (7 * 24 * 3600);
463 $moduleinfo->completionunlocked = 1;
465 // Conditional activity.
466 $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself.
467 $moduleinfo->availability = json_encode(\core_availability\tree::get_root_json(
468 array(\availability_date\condition::get_json('>=', time()),
469 \availability_date\condition::get_json('<', time() + (7 * 24 * 3600)),
470 \availability_grade\condition::get_json($coursegradeitem->id, 10, 80),
471 \availability_profile\condition::get_json(false, 'email', 'contains', '@'),
472 \availability_completion\condition::get_json($assigncm->id, COMPLETION_COMPLETE)), '&'));
474 // Grading and Advanced grading.
475 require_once($CFG->dirroot . '/rating/lib.php');
476 $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE;
477 $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number.
478 $moduleinfo->assesstimestart = time();
479 $moduleinfo->assesstimefinish = time() + (7 * 24 * 3600);
482 $moduleinfo->rsstype = 2;
483 $moduleinfo->rssarticles = 10;
485 // Optional intro editor (depends of module).
487 file_prepare_draft_area($draftid_editor, null, null, null, null);
488 $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
490 // Following is the advanced grading method area called 'submissions' for the 'assign' module.
491 if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
492 $moduleinfo->grade = 100;
494 // Plagiarism form values.
495 // No plagiarism plugin installed by default. Use this space to make your own test.
497 // Values specific to the module.
498 $modulesetvalues = $modulename.'_update_set_values';
499 $this->$modulesetvalues($moduleinfo);
501 // Create the module.
502 $result = update_module($moduleinfo);
504 // Retrieve the module info.
505 $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance));
506 $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance);
507 // Retrieve the grade item.
508 $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course,
509 'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename));
511 // Compare the values common to all module instances.
512 $this->assertEquals($moduleinfo->modulename, $dbcm->modname);
513 $this->assertEquals($moduleinfo->course, $dbcm->course);
514 $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
515 $this->assertEquals($moduleinfo->visible, $dbcm->visible);
516 $this->assertEquals($moduleinfo->completion, $dbcm->completion);
517 $this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
518 $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber);
519 $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected);
520 $this->assertEquals($moduleinfo->availability, $dbcm->availability);
521 $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription);
522 $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode);
523 $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber);
524 $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid);
526 // Optional grade testing.
527 if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
528 $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade);
531 // Some optional (but quite common) to some module.
532 $this->assertEquals($moduleinfo->name, $dbmodinstance->name);
533 $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro);
534 $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat);
536 // Test specific to the module.
537 $modulerunasserts = $modulename.'_update_run_asserts';
538 $this->$modulerunasserts($moduleinfo, $dbmodinstance);
543 * Data provider for course_delete module
545 * @return array An array of arrays contain test data
547 public function provider_course_delete_module() {
550 $data['assign'] = array('assign', array('duedate' => time()));
551 $data['quiz'] = array('quiz', array('duedate' => time()));
557 * Test the create_course function
559 public function test_create_course() {
561 $this->resetAfterTest(true);
562 $defaultcategory = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
564 $course = new stdClass();
565 $course->fullname = 'Apu loves Unit Təsts';
566 $course->shortname = 'Spread the lÅve';
567 $course->idnumber = '123';
568 $course->summary = 'Awesome!';
569 $course->summaryformat = FORMAT_PLAIN;
570 $course->format = 'topics';
571 $course->newsitems = 0;
572 $course->numsections = 5;
573 $course->category = $defaultcategory;
574 $original = (array) $course;
576 $created = create_course($course);
577 $context = context_course::instance($created->id);
579 // Compare original and created.
580 $this->assertEquals($original, array_intersect_key((array) $created, $original));
582 // Ensure default section is created.
583 $sectioncreated = $DB->record_exists('course_sections', array('course' => $created->id, 'section' => 0));
584 $this->assertTrue($sectioncreated);
586 // Ensure blocks have been associated to the course.
587 $blockcount = $DB->count_records('block_instances', array('parentcontextid' => $context->id));
588 $this->assertGreaterThan(0, $blockcount);
590 // Ensure that the shortname isn't duplicated.
592 $created = create_course($course);
593 $this->fail('Exception expected');
594 } catch (moodle_exception $e) {
595 $this->assertSame(get_string('shortnametaken', 'error', $course->shortname), $e->getMessage());
598 // Ensure that the idnumber isn't duplicated.
599 $course->shortname .= '1';
601 $created = create_course($course);
602 $this->fail('Exception expected');
603 } catch (moodle_exception $e) {
604 $this->assertSame(get_string('courseidnumbertaken', 'error', $course->idnumber), $e->getMessage());
608 public function test_create_course_with_generator() {
610 $this->resetAfterTest(true);
611 $course = $this->getDataGenerator()->create_course();
613 // Ensure default section is created.
614 $sectioncreated = $DB->record_exists('course_sections', array('course' => $course->id, 'section' => 0));
615 $this->assertTrue($sectioncreated);
618 public function test_create_course_sections() {
620 $this->resetAfterTest(true);
622 $course = $this->getDataGenerator()->create_course(
623 array('shortname' => 'GrowingCourse',
624 'fullname' => 'Growing Course',
626 array('createsections' => true));
628 // Ensure all 6 (0-5) sections were created and course content cache works properly
629 $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
630 $this->assertEquals(range(0, $course->numsections), $sectionscreated);
632 // this will do nothing, section already exists
633 $this->assertFalse(course_create_sections_if_missing($course, $course->numsections));
635 // this will create new section
636 $this->assertTrue(course_create_sections_if_missing($course, $course->numsections + 1));
638 // Ensure all 7 (0-6) sections were created and modinfo/sectioninfo cache works properly
639 $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
640 $this->assertEquals(range(0, $course->numsections + 1), $sectionscreated);
643 public function test_update_course() {
646 $this->resetAfterTest();
648 $defaultcategory = $DB->get_field_select('course_categories', 'MIN(id)', 'parent = 0');
650 $course = new stdClass();
651 $course->fullname = 'Apu loves Unit Təsts';
652 $course->shortname = 'test1';
653 $course->idnumber = '1';
654 $course->summary = 'Awesome!';
655 $course->summaryformat = FORMAT_PLAIN;
656 $course->format = 'topics';
657 $course->newsitems = 0;
658 $course->numsections = 5;
659 $course->category = $defaultcategory;
661 $created = create_course($course);
662 // Ensure the checks only work on idnumber/shortname that are not already ours.
663 update_course($created);
665 $course->shortname = 'test2';
666 $course->idnumber = '2';
668 $created2 = create_course($course);
670 // Test duplicate idnumber.
671 $created2->idnumber = '1';
673 update_course($created2);
674 $this->fail('Expected exception when trying to update a course with duplicate idnumber');
675 } catch (moodle_exception $e) {
676 $this->assertEquals(get_string('courseidnumbertaken', 'error', $created2->idnumber), $e->getMessage());
679 // Test duplicate shortname.
680 $created2->idnumber = '2';
681 $created2->shortname = 'test1';
683 update_course($created2);
684 $this->fail('Expected exception when trying to update a course with a duplicate shortname');
685 } catch (moodle_exception $e) {
686 $this->assertEquals(get_string('shortnametaken', 'error', $created2->shortname), $e->getMessage());
690 public function test_course_add_cm_to_section() {
692 $this->resetAfterTest(true);
694 // Create course with 1 section.
695 $course = $this->getDataGenerator()->create_course(
696 array('shortname' => 'GrowingCourse',
697 'fullname' => 'Growing Course',
699 array('createsections' => true));
702 rebuild_course_cache($course->id, true);
704 // Create some cms for testing.
706 for ($i=0; $i<4; $i++) {
707 $cmids[$i] = $DB->insert_record('course_modules', array('course' => $course->id));
710 // Add it to section that exists.
711 course_add_cm_to_section($course, $cmids[0], 1);
713 // Check it got added to sequence.
714 $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 1));
715 $this->assertEquals($cmids[0], $sequence);
717 // Add a second, this time using courseid variant of parameters.
718 $coursecacherev = $DB->get_field('course', 'cacherev', array('id' => $course->id));
719 course_add_cm_to_section($course->id, $cmids[1], 1);
720 $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 1));
721 $this->assertEquals($cmids[0] . ',' . $cmids[1], $sequence);
723 // Check that modinfo cache was reset but not rebuilt (important for performance if calling repeatedly).
724 $this->assertGreaterThan($coursecacherev, $DB->get_field('course', 'cacherev', array('id' => $course->id)));
725 $this->assertEmpty(cache::make('core', 'coursemodinfo')->get($course->id));
727 // Add one to section that doesn't exist (this might rebuild modinfo).
728 course_add_cm_to_section($course, $cmids[2], 2);
729 $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id)));
730 $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 2));
731 $this->assertEquals($cmids[2], $sequence);
733 // Add using the 'before' option.
734 course_add_cm_to_section($course, $cmids[3], 2, $cmids[2]);
735 $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id)));
736 $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 2));
737 $this->assertEquals($cmids[3] . ',' . $cmids[2], $sequence);
740 public function test_reorder_sections() {
742 $this->resetAfterTest(true);
744 $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
745 $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
746 $oldsections = array();
748 foreach ($DB->get_records('course_sections', array('course'=>$course->id), 'id') as $section) {
749 $oldsections[$section->section] = $section->id;
750 $sections[$section->id] = $section->section;
754 $neworder = reorder_sections($sections, 2, 4);
755 $neworder = array_keys($neworder);
756 $this->assertEquals($oldsections[0], $neworder[0]);
757 $this->assertEquals($oldsections[1], $neworder[1]);
758 $this->assertEquals($oldsections[2], $neworder[4]);
759 $this->assertEquals($oldsections[3], $neworder[2]);
760 $this->assertEquals($oldsections[4], $neworder[3]);
761 $this->assertEquals($oldsections[5], $neworder[5]);
762 $this->assertEquals($oldsections[6], $neworder[6]);
764 $neworder = reorder_sections($sections, 4, 2);
765 $neworder = array_keys($neworder);
766 $this->assertEquals($oldsections[0], $neworder[0]);
767 $this->assertEquals($oldsections[1], $neworder[1]);
768 $this->assertEquals($oldsections[2], $neworder[3]);
769 $this->assertEquals($oldsections[3], $neworder[4]);
770 $this->assertEquals($oldsections[4], $neworder[2]);
771 $this->assertEquals($oldsections[5], $neworder[5]);
772 $this->assertEquals($oldsections[6], $neworder[6]);
774 $neworder = reorder_sections(1, 2, 4);
775 $this->assertFalse($neworder);
778 public function test_move_section_down() {
780 $this->resetAfterTest(true);
782 $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
783 $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
784 $oldsections = array();
785 foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
786 $oldsections[$section->section] = $section->id;
790 // Test move section down..
791 move_section_to($course, 2, 4);
793 foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
794 $sections[$section->section] = $section->id;
798 $this->assertEquals($oldsections[0], $sections[0]);
799 $this->assertEquals($oldsections[1], $sections[1]);
800 $this->assertEquals($oldsections[2], $sections[4]);
801 $this->assertEquals($oldsections[3], $sections[2]);
802 $this->assertEquals($oldsections[4], $sections[3]);
803 $this->assertEquals($oldsections[5], $sections[5]);
804 $this->assertEquals($oldsections[6], $sections[6]);
807 public function test_move_section_up() {
809 $this->resetAfterTest(true);
811 $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
812 $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
813 $oldsections = array();
814 foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
815 $oldsections[$section->section] = $section->id;
819 // Test move section up..
820 move_section_to($course, 6, 4);
822 foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
823 $sections[$section->section] = $section->id;
827 $this->assertEquals($oldsections[0], $sections[0]);
828 $this->assertEquals($oldsections[1], $sections[1]);
829 $this->assertEquals($oldsections[2], $sections[2]);
830 $this->assertEquals($oldsections[3], $sections[3]);
831 $this->assertEquals($oldsections[4], $sections[5]);
832 $this->assertEquals($oldsections[5], $sections[6]);
833 $this->assertEquals($oldsections[6], $sections[4]);
836 public function test_move_section_marker() {
838 $this->resetAfterTest(true);
840 $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
841 $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
843 // Set course marker to the section we are going to move..
844 course_set_marker($course->id, 2);
845 // Verify that the course marker is set correctly.
846 $course = $DB->get_record('course', array('id' => $course->id));
847 $this->assertEquals(2, $course->marker);
849 // Test move the marked section down..
850 move_section_to($course, 2, 4);
852 // Verify that the coruse marker has been moved along with the section..
853 $course = $DB->get_record('course', array('id' => $course->id));
854 $this->assertEquals(4, $course->marker);
856 // Test move the marked section up..
857 move_section_to($course, 4, 3);
859 // Verify that the course marker has been moved along with the section..
860 $course = $DB->get_record('course', array('id' => $course->id));
861 $this->assertEquals(3, $course->marker);
863 // Test moving a non-marked section above the marked section..
864 move_section_to($course, 4, 2);
866 // Verify that the course marker has been moved down to accomodate..
867 $course = $DB->get_record('course', array('id' => $course->id));
868 $this->assertEquals(4, $course->marker);
870 // Test moving a non-marked section below the marked section..
871 move_section_to($course, 3, 6);
873 // Verify that the course marker has been up to accomodate..
874 $course = $DB->get_record('course', array('id' => $course->id));
875 $this->assertEquals(3, $course->marker);
878 public function test_course_can_delete_section() {
880 $this->resetAfterTest(true);
882 $generator = $this->getDataGenerator();
884 $courseweeks = $generator->create_course(
885 array('numsections' => 5, 'format' => 'weeks'),
886 array('createsections' => true));
887 $assign1 = $generator->create_module('assign', array('course' => $courseweeks, 'section' => 1));
888 $assign2 = $generator->create_module('assign', array('course' => $courseweeks, 'section' => 2));
890 $coursetopics = $generator->create_course(
891 array('numsections' => 5, 'format' => 'topics'),
892 array('createsections' => true));
894 $coursesingleactivity = $generator->create_course(
895 array('format' => 'singleactivity'),
896 array('createsections' => true));
898 // Enrol student and teacher.
899 $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
900 $student = $generator->create_user();
901 $teacher = $generator->create_user();
903 $generator->enrol_user($student->id, $courseweeks->id, $roleids['student']);
904 $generator->enrol_user($teacher->id, $courseweeks->id, $roleids['editingteacher']);
906 $generator->enrol_user($student->id, $coursetopics->id, $roleids['student']);
907 $generator->enrol_user($teacher->id, $coursetopics->id, $roleids['editingteacher']);
909 $generator->enrol_user($student->id, $coursesingleactivity->id, $roleids['student']);
910 $generator->enrol_user($teacher->id, $coursesingleactivity->id, $roleids['editingteacher']);
912 // Teacher should be able to delete sections (except for 0) in topics and weeks format.
913 $this->setUser($teacher);
915 // For topics and weeks formats will return false for section 0 and true for any other section.
916 $this->assertFalse(course_can_delete_section($courseweeks, 0));
917 $this->assertTrue(course_can_delete_section($courseweeks, 1));
919 $this->assertFalse(course_can_delete_section($coursetopics, 0));
920 $this->assertTrue(course_can_delete_section($coursetopics, 1));
922 // For singleactivity course format no section can be deleted.
923 $this->assertFalse(course_can_delete_section($coursesingleactivity, 0));
924 $this->assertFalse(course_can_delete_section($coursesingleactivity, 1));
926 // Now let's revoke a capability from teacher to manage activity in section 1.
927 $modulecontext = context_module::instance($assign1->cmid);
928 assign_capability('moodle/course:manageactivities', CAP_PROHIBIT, $roleids['editingteacher'],
930 $modulecontext->mark_dirty();
931 $this->assertFalse(course_can_delete_section($courseweeks, 1));
932 $this->assertTrue(course_can_delete_section($courseweeks, 2));
934 // Student does not have permissions to delete sections.
935 $this->setUser($student);
936 $this->assertFalse(course_can_delete_section($courseweeks, 1));
937 $this->assertFalse(course_can_delete_section($coursetopics, 1));
938 $this->assertFalse(course_can_delete_section($coursesingleactivity, 1));
941 public function test_course_delete_section() {
943 $this->resetAfterTest(true);
945 $generator = $this->getDataGenerator();
947 $course = $generator->create_course(array('numsections' => 6, 'format' => 'topics'),
948 array('createsections' => true));
949 $assign0 = $generator->create_module('assign', array('course' => $course, 'section' => 0));
950 $assign1 = $generator->create_module('assign', array('course' => $course, 'section' => 1));
951 $assign21 = $generator->create_module('assign', array('course' => $course, 'section' => 2));
952 $assign22 = $generator->create_module('assign', array('course' => $course, 'section' => 2));
953 $assign3 = $generator->create_module('assign', array('course' => $course, 'section' => 3));
954 $assign5 = $generator->create_module('assign', array('course' => $course, 'section' => 5));
955 $assign6 = $generator->create_module('assign', array('course' => $course, 'section' => 6));
957 $this->setAdminUser();
959 // Attempt to delete non-existing section.
960 $this->assertFalse(course_delete_section($course, 10, false));
961 $this->assertFalse(course_delete_section($course, 9, true));
963 // Attempt to delete 0-section.
964 $this->assertFalse(course_delete_section($course, 0, true));
965 $this->assertTrue($DB->record_exists('course_modules', array('id' => $assign0->cmid)));
967 // Delete last section.
968 $this->assertTrue(course_delete_section($course, 6, true));
969 $this->assertFalse($DB->record_exists('course_modules', array('id' => $assign6->cmid)));
970 $this->assertEquals(5, course_get_format($course)->get_course()->numsections);
972 // Delete empty section.
973 $this->assertTrue(course_delete_section($course, 4, false));
974 $this->assertEquals(4, course_get_format($course)->get_course()->numsections);
976 // Delete section in the middle (2).
977 $this->assertFalse(course_delete_section($course, 2, false));
978 $this->assertTrue(course_delete_section($course, 2, true));
979 $this->assertFalse($DB->record_exists('course_modules', array('id' => $assign21->cmid)));
980 $this->assertFalse($DB->record_exists('course_modules', array('id' => $assign22->cmid)));
981 $this->assertEquals(3, course_get_format($course)->get_course()->numsections);
982 $this->assertEquals(array(0 => array($assign0->cmid),
983 1 => array($assign1->cmid),
984 2 => array($assign3->cmid),
985 3 => array($assign5->cmid)), get_fast_modinfo($course)->sections);
987 // Make last section orphaned.
988 update_course((object)array('id' => $course->id, 'numsections' => 2));
989 $this->assertEquals(2, course_get_format($course)->get_course()->numsections);
991 // Remove orphaned section.
992 $this->assertTrue(course_delete_section($course, 3, true));
993 $this->assertEquals(2, course_get_format($course)->get_course()->numsections);
995 // Remove marked section.
996 course_set_marker($course->id, 1);
997 $this->assertTrue(course_get_format($course)->is_section_current(1));
998 $this->assertTrue(course_delete_section($course, 1, true));
999 $this->assertFalse(course_get_format($course)->is_section_current(1));
1002 public function test_get_course_display_name_for_list() {
1004 $this->resetAfterTest(true);
1006 $course = $this->getDataGenerator()->create_course(array('shortname' => 'FROG101', 'fullname' => 'Introduction to pond life'));
1008 $CFG->courselistshortnames = 0;
1009 $this->assertEquals('Introduction to pond life', get_course_display_name_for_list($course));
1011 $CFG->courselistshortnames = 1;
1012 $this->assertEquals('FROG101 Introduction to pond life', get_course_display_name_for_list($course));
1015 public function test_move_module_in_course() {
1018 $this->resetAfterTest(true);
1020 $course = $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections' => true));
1021 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
1023 $cms = get_fast_modinfo($course)->get_cms();
1026 $newsection = get_fast_modinfo($course)->get_section_info(3);
1027 $oldsectionid = $cm->section;
1030 moveto_module($cm, $newsection);
1032 $cms = get_fast_modinfo($course)->get_cms();
1035 // Check that the cached modinfo contains the correct section info
1036 $modinfo = get_fast_modinfo($course);
1037 $this->assertTrue(empty($modinfo->sections[0]));
1038 $this->assertFalse(empty($modinfo->sections[3]));
1040 // Check that the old section's sequence no longer contains this ID
1041 $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
1042 $oldsequences = explode(',', $newsection->sequence);
1043 $this->assertFalse(in_array($cm->id, $oldsequences));
1045 // Check that the new section's sequence now contains this ID
1046 $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
1047 $newsequences = explode(',', $newsection->sequence);
1048 $this->assertTrue(in_array($cm->id, $newsequences));
1050 // Check that the section number has been changed in the cm
1051 $this->assertEquals($newsection->id, $cm->section);
1054 // Perform a second move as some issues were only seen on the second move
1055 $newsection = get_fast_modinfo($course)->get_section_info(2);
1056 $oldsectionid = $cm->section;
1057 moveto_module($cm, $newsection);
1059 $cms = get_fast_modinfo($course)->get_cms();
1062 // Check that the cached modinfo contains the correct section info
1063 $modinfo = get_fast_modinfo($course);
1064 $this->assertTrue(empty($modinfo->sections[0]));
1065 $this->assertFalse(empty($modinfo->sections[2]));
1067 // Check that the old section's sequence no longer contains this ID
1068 $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
1069 $oldsequences = explode(',', $newsection->sequence);
1070 $this->assertFalse(in_array($cm->id, $oldsequences));
1072 // Check that the new section's sequence now contains this ID
1073 $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
1074 $newsequences = explode(',', $newsection->sequence);
1075 $this->assertTrue(in_array($cm->id, $newsequences));
1078 public function test_module_visibility() {
1079 $this->setAdminUser();
1080 $this->resetAfterTest(true);
1082 // Create course and modules.
1083 $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
1084 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1085 $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(), 'course' => $course->id));
1086 $modules = compact('forum', 'assign');
1088 // Hiding the modules.
1089 foreach ($modules as $mod) {
1090 set_coursemodule_visible($mod->cmid, 0);
1091 $this->check_module_visibility($mod, 0, 0);
1094 // Showing the modules.
1095 foreach ($modules as $mod) {
1096 set_coursemodule_visible($mod->cmid, 1);
1097 $this->check_module_visibility($mod, 1, 1);
1101 public function test_section_visibility_events() {
1102 $this->setAdminUser();
1103 $this->resetAfterTest(true);
1105 $course = $this->getDataGenerator()->create_course(array('numsections' => 1), array('createsections' => true));
1107 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1108 array('section' => $sectionnumber));
1109 $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1110 'course' => $course->id), array('section' => $sectionnumber));
1111 $sink = $this->redirectEvents();
1112 set_section_visible($course->id, $sectionnumber, 0);
1113 $events = $sink->get_events();
1115 // Extract the number of events related to what we are testing, other events
1116 // such as course_section_updated could have been triggered.
1118 foreach ($events as $event) {
1119 if ($event instanceof \core\event\course_module_updated) {
1123 $this->assertSame(2, $count);
1127 public function test_section_visibility() {
1128 $this->setAdminUser();
1129 $this->resetAfterTest(true);
1132 $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
1134 $sink = $this->redirectEvents();
1136 // Testing an empty section.
1138 set_section_visible($course->id, $sectionnumber, 0);
1139 $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1140 $this->assertEquals($section_info->visible, 0);
1141 set_section_visible($course->id, $sectionnumber, 1);
1142 $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1143 $this->assertEquals($section_info->visible, 1);
1145 // Checking that an event was fired.
1146 $events = $sink->get_events();
1147 $this->assertInstanceOf('\core\event\course_section_updated', $events[0]);
1150 // Testing a section with visible modules.
1152 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1153 array('section' => $sectionnumber));
1154 $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1155 'course' => $course->id), array('section' => $sectionnumber));
1156 $modules = compact('forum', 'assign');
1157 set_section_visible($course->id, $sectionnumber, 0);
1158 $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1159 $this->assertEquals($section_info->visible, 0);
1160 foreach ($modules as $mod) {
1161 $this->check_module_visibility($mod, 0, 1);
1163 set_section_visible($course->id, $sectionnumber, 1);
1164 $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1165 $this->assertEquals($section_info->visible, 1);
1166 foreach ($modules as $mod) {
1167 $this->check_module_visibility($mod, 1, 1);
1170 // Testing a section with hidden modules, which should stay hidden.
1172 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1173 array('section' => $sectionnumber));
1174 $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1175 'course' => $course->id), array('section' => $sectionnumber));
1176 $modules = compact('forum', 'assign');
1177 foreach ($modules as $mod) {
1178 set_coursemodule_visible($mod->cmid, 0);
1179 $this->check_module_visibility($mod, 0, 0);
1181 set_section_visible($course->id, $sectionnumber, 0);
1182 $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1183 $this->assertEquals($section_info->visible, 0);
1184 foreach ($modules as $mod) {
1185 $this->check_module_visibility($mod, 0, 0);
1187 set_section_visible($course->id, $sectionnumber, 1);
1188 $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1189 $this->assertEquals($section_info->visible, 1);
1190 foreach ($modules as $mod) {
1191 $this->check_module_visibility($mod, 0, 0);
1196 * Helper function to assert that a module has correctly been made visible, or hidden.
1198 * @param stdClass $mod module information
1199 * @param int $visibility the current state of the module
1200 * @param int $visibleold the current state of the visibleold property
1203 public function check_module_visibility($mod, $visibility, $visibleold) {
1205 $cm = get_fast_modinfo($mod->course)->get_cm($mod->cmid);
1206 $this->assertEquals($visibility, $cm->visible);
1207 $this->assertEquals($visibleold, $cm->visibleold);
1209 // Check the module grade items.
1210 $grade_items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $cm->modname,
1211 'iteminstance' => $cm->instance, 'courseid' => $cm->course));
1213 foreach ($grade_items as $grade_item) {
1215 $this->assertFalse($grade_item->is_hidden(), "$cm->modname grade_item not visible");
1217 $this->assertTrue($grade_item->is_hidden(), "$cm->modname grade_item not hidden");
1222 // Check the events visibility.
1223 if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $cm->modname))) {
1224 foreach ($events as $event) {
1225 $calevent = new calendar_event($event);
1226 $this->assertEquals($visibility, $calevent->visible, "$cm->modname calendar_event visibility");
1231 public function test_course_page_type_list() {
1233 $this->resetAfterTest(true);
1235 // Create a category.
1236 $category = new stdClass();
1237 $category->name = 'Test Category';
1239 $testcategory = $this->getDataGenerator()->create_category($category);
1242 $course = new stdClass();
1243 $course->fullname = 'Apu loves Unit Təsts';
1244 $course->shortname = 'Spread the lÅve';
1245 $course->idnumber = '123';
1246 $course->summary = 'Awesome!';
1247 $course->summaryformat = FORMAT_PLAIN;
1248 $course->format = 'topics';
1249 $course->newsitems = 0;
1250 $course->numsections = 5;
1251 $course->category = $testcategory->id;
1253 $testcourse = $this->getDataGenerator()->create_course($course);
1256 $coursecontext = context_course::instance($testcourse->id);
1257 $parentcontext = $coursecontext->get_parent_context(); // Not actually used.
1258 $pagetype = 'page-course-x'; // Not used either.
1259 $pagetypelist = course_page_type_list($pagetype, $parentcontext, $coursecontext);
1261 // Page type lists for normal courses.
1262 $testpagetypelist1 = array();
1263 $testpagetypelist1['*'] = 'Any page';
1264 $testpagetypelist1['course-*'] = 'Any course page';
1265 $testpagetypelist1['course-view-*'] = 'Any type of course main page';
1267 $this->assertEquals($testpagetypelist1, $pagetypelist);
1269 // Get the context for the front page course.
1270 $sitecoursecontext = context_course::instance(SITEID);
1271 $pagetypelist = course_page_type_list($pagetype, $parentcontext, $sitecoursecontext);
1273 // Page type list for the front page course.
1274 $testpagetypelist2 = array('*' => 'Any page');
1275 $this->assertEquals($testpagetypelist2, $pagetypelist);
1277 // Make sure that providing no current context to the function doesn't result in an error.
1278 // Calls made from generate_page_type_patterns() may provide null values.
1279 $pagetypelist = course_page_type_list($pagetype, null, null);
1280 $this->assertEquals($pagetypelist, $testpagetypelist1);
1283 public function test_compare_activities_by_time_desc() {
1285 // Let's create some test data.
1286 $activitiesivities = array();
1287 $x = new stdClass();
1288 $x->timestamp = null;
1291 $x = new stdClass();
1295 $x = new stdClass();
1299 $x = new stdClass();
1303 $x = new stdClass();
1307 $x = new stdClass();
1310 $x = new stdClass();
1315 usort($activities, 'compare_activities_by_time_desc');
1317 // Let's check the result.
1319 foreach($activities as $activity) {
1320 if (empty($activity->timestamp)) {
1321 $activity->timestamp = 0;
1323 $this->assertLessThanOrEqual($last, $activity->timestamp);
1327 public function test_compare_activities_by_time_asc() {
1329 // Let's create some test data.
1330 $activities = array();
1331 $x = new stdClass();
1332 $x->timestamp = null;
1335 $x = new stdClass();
1339 $x = new stdClass();
1343 $x = new stdClass();
1347 $x = new stdClass();
1351 $x = new stdClass();
1354 $x = new stdClass();
1359 usort($activities, 'compare_activities_by_time_asc');
1361 // Let's check the result.
1363 foreach($activities as $activity) {
1364 if (empty($activity->timestamp)) {
1365 $activity->timestamp = 0;
1367 $this->assertGreaterThanOrEqual($last, $activity->timestamp);
1372 * Tests moving a module between hidden/visible sections and
1373 * verifies that the course/module visiblity seettings are
1376 public function test_moveto_module_between_hidden_sections() {
1379 $this->resetAfterTest(true);
1381 $course = $this->getDataGenerator()->create_course(array('numsections' => 4), array('createsections' => true));
1382 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1383 $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
1384 $quiz= $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
1386 // Set the page as hidden
1387 set_coursemodule_visible($page->cmid, 0);
1389 // Set sections 3 as hidden.
1390 set_section_visible($course->id, 3, 0);
1392 $modinfo = get_fast_modinfo($course);
1394 $hiddensection = $modinfo->get_section_info(3);
1395 // New section is definitely not visible:
1396 $this->assertEquals($hiddensection->visible, 0);
1398 $forumcm = $modinfo->cms[$forum->cmid];
1399 $pagecm = $modinfo->cms[$page->cmid];
1401 // Move the forum and the page to a hidden section, make sure moveto_module returns 0 as new visibility state.
1402 $this->assertEquals(0, moveto_module($forumcm, $hiddensection));
1403 $this->assertEquals(0, moveto_module($pagecm, $hiddensection));
1405 $modinfo = get_fast_modinfo($course);
1407 // Verify that forum and page have been moved to the hidden section and quiz has not.
1408 $this->assertContains($forum->cmid, $modinfo->sections[3]);
1409 $this->assertContains($page->cmid, $modinfo->sections[3]);
1410 $this->assertNotContains($quiz->cmid, $modinfo->sections[3]);
1412 // Verify that forum has been made invisible.
1413 $forumcm = $modinfo->cms[$forum->cmid];
1414 $this->assertEquals($forumcm->visible, 0);
1415 // Verify that old state has been retained.
1416 $this->assertEquals($forumcm->visibleold, 1);
1418 // Verify that page has stayed invisible.
1419 $pagecm = $modinfo->cms[$page->cmid];
1420 $this->assertEquals($pagecm->visible, 0);
1421 // Verify that old state has been retained.
1422 $this->assertEquals($pagecm->visibleold, 0);
1424 // Verify that quiz has been unaffected.
1425 $quizcm = $modinfo->cms[$quiz->cmid];
1426 $this->assertEquals($quizcm->visible, 1);
1428 // Move forum and page back to visible section.
1429 // Make sure the visibility is restored to the original value (visible for forum and hidden for page).
1430 $visiblesection = $modinfo->get_section_info(2);
1431 $this->assertEquals(1, moveto_module($forumcm, $visiblesection));
1432 $this->assertEquals(0, moveto_module($pagecm, $visiblesection));
1434 $modinfo = get_fast_modinfo($course);
1436 // Double check that forum has been made visible.
1437 $forumcm = $modinfo->cms[$forum->cmid];
1438 $this->assertEquals($forumcm->visible, 1);
1440 // Double check that page has stayed invisible.
1441 $pagecm = $modinfo->cms[$page->cmid];
1442 $this->assertEquals($pagecm->visible, 0);
1444 // Move the page in the same section (this is what mod duplicate does).
1445 // Visibility of page remains 0.
1446 $this->assertEquals(0, moveto_module($pagecm, $visiblesection, $forumcm));
1448 // Double check that the the page is still hidden.
1449 $modinfo = get_fast_modinfo($course);
1450 $pagecm = $modinfo->cms[$page->cmid];
1451 $this->assertEquals($pagecm->visible, 0);
1455 * Tests moving a module around in the same section. moveto_module()
1456 * is called this way in modduplicate.
1458 public function test_moveto_module_in_same_section() {
1461 $this->resetAfterTest(true);
1463 $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
1464 $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
1465 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1467 // Simulate inconsistent visible/visibleold values (MDL-38713).
1468 $cm = $DB->get_record('course_modules', array('id' => $page->cmid), '*', MUST_EXIST);
1470 $cm->visibleold = 1;
1471 $DB->update_record('course_modules', $cm);
1473 $modinfo = get_fast_modinfo($course);
1474 $forumcm = $modinfo->cms[$forum->cmid];
1475 $pagecm = $modinfo->cms[$page->cmid];
1477 // Verify that page is hidden.
1478 $this->assertEquals($pagecm->visible, 0);
1480 // Verify section 0 is where all mods added.
1481 $section = $modinfo->get_section_info(0);
1482 $this->assertEquals($section->id, $forumcm->section);
1483 $this->assertEquals($section->id, $pagecm->section);
1486 // Move the page inside the hidden section. Make sure it is hidden.
1487 $this->assertEquals(0, moveto_module($pagecm, $section, $forumcm));
1489 // Double check that the the page is still hidden.
1490 $modinfo = get_fast_modinfo($course);
1491 $pagecm = $modinfo->cms[$page->cmid];
1492 $this->assertEquals($pagecm->visible, 0);
1496 * Tests the function that deletes a course module
1498 * @param string $type The type of module for the test
1499 * @param array $options The options for the module creation
1500 * @dataProvider provider_course_delete_module
1502 public function test_course_delete_module($type, $options) {
1504 $this->resetAfterTest(true);
1505 $this->setAdminUser();
1507 // Create course and modules.
1508 $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
1509 $options['course'] = $course->id;
1511 // Generate an assignment with due date (will generate a course event).
1512 $module = $this->getDataGenerator()->create_module($type, $options);
1514 // Get the module context.
1515 $modcontext = context_module::instance($module->cmid);
1517 // Verify context exists.
1518 $this->assertInstanceOf('context_module', $modcontext);
1520 // Make module specific messes.
1523 // Add some tags to this assignment.
1524 tag_set('assign', $module->id, array('Tag 1', 'Tag 2', 'Tag 3'), 'mod_assign', $modcontext->id);
1526 // Confirm the tag instances were added.
1527 $criteria = array('component' => 'mod_assign', 'contextid' => $modcontext->id);
1528 $this->assertEquals(3, $DB->count_records('tag_instance', $criteria));
1530 // Verify event assignment event has been generated.
1531 $eventcount = $DB->count_records('event', array('instance' => $module->id, 'modulename' => $type));
1532 $this->assertEquals(1, $eventcount);
1536 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
1537 $qcat = $qgen->create_question_category(array('contextid' => $modcontext->id));
1539 $qgen->create_question('shortanswer', null, array('category' => $qcat->id)),
1540 $qgen->create_question('shortanswer', null, array('category' => $qcat->id)),
1542 $this->expectOutputRegex('/'.get_string('unusedcategorydeleted', 'question').'/');
1549 course_delete_module($module->cmid);
1551 // Verify the context has been removed.
1552 $this->assertFalse(context_module::instance($module->cmid, IGNORE_MISSING));
1554 // Verify the course_module record has been deleted.
1555 $cmcount = $DB->count_records('course_modules', array('id' => $module->cmid));
1556 $this->assertEmpty($cmcount);
1558 // Test clean up of module specific messes.
1561 // Verify event assignment events have been removed.
1562 $eventcount = $DB->count_records('event', array('instance' => $module->id, 'modulename' => $type));
1563 $this->assertEmpty($eventcount);
1565 // Verify the tag instances were deleted.
1566 $criteria = array('component' => 'mod_assign', 'contextid' => $modcontext->id);
1567 $this->assertEquals(0, $DB->count_records('tag_instance', $criteria));
1570 // Verify category deleted.
1571 $criteria = array('contextid' => $modcontext->id);
1572 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
1574 // Verify questions deleted.
1575 $criteria = array('category' => $qcat->id);
1576 $this->assertEquals(0, $DB->count_records('question', $criteria));
1584 * Test that triggering a course_created event works as expected.
1586 public function test_course_created_event() {
1589 $this->resetAfterTest();
1591 // Catch the events.
1592 $sink = $this->redirectEvents();
1594 // Create the course with an id number which is used later when generating a course via the imsenterprise plugin.
1595 $data = new stdClass();
1596 $data->idnumber = 'idnumber';
1597 $course = $this->getDataGenerator()->create_course($data);
1598 // Get course from DB for comparison.
1599 $course = $DB->get_record('course', array('id' => $course->id));
1601 // Capture the event.
1602 $events = $sink->get_events();
1605 // Validate the event.
1606 $event = $events[0];
1607 $this->assertInstanceOf('\core\event\course_created', $event);
1608 $this->assertEquals('course', $event->objecttable);
1609 $this->assertEquals($course->id, $event->objectid);
1610 $this->assertEquals(context_course::instance($course->id), $event->get_context());
1611 $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1612 $this->assertEquals('course_created', $event->get_legacy_eventname());
1613 $this->assertEventLegacyData($course, $event);
1614 $expectedlog = array(SITEID, 'course', 'new', 'view.php?id=' . $course->id, $course->fullname . ' (ID ' . $course->id . ')');
1615 $this->assertEventLegacyLogData($expectedlog, $event);
1617 // Now we want to trigger creating a course via the imsenterprise.
1618 // Delete the course we created earlier, as we want the imsenterprise plugin to create this.
1619 // We do not want print out any of the text this function generates while doing this, which is why
1620 // we are using ob_start() and ob_end_clean().
1622 delete_course($course);
1625 // Create the XML file we want to use.
1626 $imstestcase = new enrol_imsenterprise_testcase();
1627 $imstestcase->imsplugin = enrol_get_plugin('imsenterprise');
1628 $imstestcase->set_test_config();
1629 $imstestcase->set_xml_file(false, array($course));
1631 // Capture the event.
1632 $sink = $this->redirectEvents();
1633 $imstestcase->imsplugin->cron();
1634 $events = $sink->get_events();
1636 $event = $events[0];
1638 // Validate the event triggered is \core\event\course_created. There is no need to validate the other values
1639 // as they have already been validated in the previous steps. Here we only want to make sure that when the
1640 // imsenterprise plugin creates a course an event is triggered.
1641 $this->assertInstanceOf('\core\event\course_created', $event);
1642 $this->assertEventContextNotUsed($event);
1646 * Test that triggering a course_updated event works as expected.
1648 public function test_course_updated_event() {
1651 $this->resetAfterTest();
1654 $course = $this->getDataGenerator()->create_course();
1656 // Create a category we are going to move this course to.
1657 $category = $this->getDataGenerator()->create_category();
1659 // Create a hidden category we are going to move this course to.
1660 $categoryhidden = $this->getDataGenerator()->create_category(array('visible' => 0));
1662 // Update course and catch course_updated event.
1663 $sink = $this->redirectEvents();
1664 update_course($course);
1665 $events = $sink->get_events();
1668 // Get updated course information from the DB.
1669 $updatedcourse = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1671 $event = array_shift($events);
1672 $this->assertInstanceOf('\core\event\course_updated', $event);
1673 $this->assertEquals('course', $event->objecttable);
1674 $this->assertEquals($updatedcourse->id, $event->objectid);
1675 $this->assertEquals(context_course::instance($course->id), $event->get_context());
1676 $url = new moodle_url('/course/edit.php', array('id' => $event->objectid));
1677 $this->assertEquals($url, $event->get_url());
1678 $this->assertEquals($updatedcourse, $event->get_record_snapshot('course', $event->objectid));
1679 $this->assertEquals('course_updated', $event->get_legacy_eventname());
1680 $this->assertEventLegacyData($updatedcourse, $event);
1681 $expectedlog = array($updatedcourse->id, 'course', 'update', 'edit.php?id=' . $course->id, $course->id);
1682 $this->assertEventLegacyLogData($expectedlog, $event);
1684 // Move course and catch course_updated event.
1685 $sink = $this->redirectEvents();
1686 move_courses(array($course->id), $category->id);
1687 $events = $sink->get_events();
1690 // Return the moved course information from the DB.
1691 $movedcourse = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1693 $event = array_shift($events);
1694 $this->assertInstanceOf('\core\event\course_updated', $event);
1695 $this->assertEquals('course', $event->objecttable);
1696 $this->assertEquals($movedcourse->id, $event->objectid);
1697 $this->assertEquals(context_course::instance($course->id), $event->get_context());
1698 $this->assertEquals($movedcourse, $event->get_record_snapshot('course', $movedcourse->id));
1699 $this->assertEquals('course_updated', $event->get_legacy_eventname());
1700 $this->assertEventLegacyData($movedcourse, $event);
1701 $expectedlog = array($movedcourse->id, 'course', 'move', 'edit.php?id=' . $movedcourse->id, $movedcourse->id);
1702 $this->assertEventLegacyLogData($expectedlog, $event);
1704 // Move course to hidden category and catch course_updated event.
1705 $sink = $this->redirectEvents();
1706 move_courses(array($course->id), $categoryhidden->id);
1707 $events = $sink->get_events();
1710 // Return the moved course information from the DB.
1711 $movedcoursehidden = $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($movedcoursehidden->id, $event->objectid);
1717 $this->assertEquals(context_course::instance($course->id), $event->get_context());
1718 $this->assertEquals($movedcoursehidden, $event->get_record_snapshot('course', $movedcoursehidden->id));
1719 $this->assertEquals('course_updated', $event->get_legacy_eventname());
1720 $this->assertEventLegacyData($movedcoursehidden, $event);
1721 $expectedlog = array($movedcoursehidden->id, 'course', 'move', 'edit.php?id=' . $movedcoursehidden->id, $movedcoursehidden->id);
1722 $this->assertEventLegacyLogData($expectedlog, $event);
1723 $this->assertEventContextNotUsed($event);
1727 * Test that triggering a course_deleted event works as expected.
1729 public function test_course_deleted_event() {
1730 $this->resetAfterTest();
1732 // Create the course.
1733 $course = $this->getDataGenerator()->create_course();
1735 // Save the course context before we delete the course.
1736 $coursecontext = context_course::instance($course->id);
1738 // Catch the update event.
1739 $sink = $this->redirectEvents();
1741 // Call delete_course() which will trigger the course_deleted event and the course_content_deleted
1742 // event. This function prints out data to the screen, which we do not want during a PHPUnit test,
1743 // so use ob_start and ob_end_clean to prevent this.
1745 delete_course($course);
1748 // Capture the event.
1749 $events = $sink->get_events();
1752 // Validate the event.
1753 $event = array_pop($events);
1754 $this->assertInstanceOf('\core\event\course_deleted', $event);
1755 $this->assertEquals('course', $event->objecttable);
1756 $this->assertEquals($course->id, $event->objectid);
1757 $this->assertEquals($coursecontext->id, $event->contextid);
1758 $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1759 $this->assertEquals('course_deleted', $event->get_legacy_eventname());
1760 $eventdata = $event->get_data();
1761 $this->assertSame($course->idnumber, $eventdata['other']['idnumber']);
1762 $this->assertSame($course->fullname, $eventdata['other']['fullname']);
1763 $this->assertSame($course->shortname, $eventdata['other']['shortname']);
1765 // The legacy data also passed the context in the course object and substitutes timemodified with the current date.
1766 $expectedlegacy = clone($course);
1767 $expectedlegacy->context = $coursecontext;
1768 $expectedlegacy->timemodified = $event->timecreated;
1769 $this->assertEventLegacyData($expectedlegacy, $event);
1771 // Validate legacy log data.
1772 $expectedlog = array(SITEID, 'course', 'delete', 'view.php?id=' . $course->id, $course->fullname . '(ID ' . $course->id . ')');
1773 $this->assertEventLegacyLogData($expectedlog, $event);
1774 $this->assertEventContextNotUsed($event);
1778 * Test that triggering a course_content_deleted event works as expected.
1780 public function test_course_content_deleted_event() {
1783 $this->resetAfterTest();
1785 // Create the course.
1786 $course = $this->getDataGenerator()->create_course();
1788 // Get the course from the DB. The data generator adds some extra properties, such as
1789 // numsections, to the course object which will fail the assertions later on.
1790 $course = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1792 // Save the course context before we delete the course.
1793 $coursecontext = context_course::instance($course->id);
1795 // Catch the update event.
1796 $sink = $this->redirectEvents();
1798 remove_course_contents($course->id, false);
1800 // Capture the event.
1801 $events = $sink->get_events();
1804 // Validate the event.
1805 $event = array_pop($events);
1806 $this->assertInstanceOf('\core\event\course_content_deleted', $event);
1807 $this->assertEquals('course', $event->objecttable);
1808 $this->assertEquals($course->id, $event->objectid);
1809 $this->assertEquals($coursecontext->id, $event->contextid);
1810 $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1811 $this->assertEquals('course_content_removed', $event->get_legacy_eventname());
1812 // The legacy data also passed the context and options in the course object.
1813 $course->context = $coursecontext;
1814 $course->options = array();
1815 $this->assertEventLegacyData($course, $event);
1816 $this->assertEventContextNotUsed($event);
1820 * Test that triggering a course_category_deleted event works as expected.
1822 public function test_course_category_deleted_event() {
1823 $this->resetAfterTest();
1825 // Create a category.
1826 $category = $this->getDataGenerator()->create_category();
1828 // Save the context before it is deleted.
1829 $categorycontext = context_coursecat::instance($category->id);
1831 // Catch the update event.
1832 $sink = $this->redirectEvents();
1834 // Delete the category.
1835 $category->delete_full();
1837 // Capture the event.
1838 $events = $sink->get_events();
1841 // Validate the event.
1842 $event = $events[0];
1843 $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1844 $this->assertEquals('course_categories', $event->objecttable);
1845 $this->assertEquals($category->id, $event->objectid);
1846 $this->assertEquals($categorycontext->id, $event->contextid);
1847 $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1848 $this->assertEquals(null, $event->get_url());
1849 $this->assertEventLegacyData($category, $event);
1850 $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category->name . '(ID ' . $category->id . ')');
1851 $this->assertEventLegacyLogData($expectedlog, $event);
1853 // Create two categories.
1854 $category = $this->getDataGenerator()->create_category();
1855 $category2 = $this->getDataGenerator()->create_category();
1857 // Save the context before it is moved and then deleted.
1858 $category2context = context_coursecat::instance($category2->id);
1860 // Catch the update event.
1861 $sink = $this->redirectEvents();
1863 // Move the category.
1864 $category2->delete_move($category->id);
1866 // Capture the event.
1867 $events = $sink->get_events();
1870 // Validate the event.
1871 $event = $events[0];
1872 $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1873 $this->assertEquals('course_categories', $event->objecttable);
1874 $this->assertEquals($category2->id, $event->objectid);
1875 $this->assertEquals($category2context->id, $event->contextid);
1876 $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1877 $this->assertEventLegacyData($category2, $event);
1878 $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category2->name . '(ID ' . $category2->id . ')');
1879 $this->assertEventLegacyLogData($expectedlog, $event);
1880 $this->assertEventContextNotUsed($event);
1884 * Test that triggering a course_restored event works as expected.
1886 public function test_course_restored_event() {
1889 // Get the necessary files to perform backup and restore.
1890 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
1891 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
1893 $this->resetAfterTest();
1895 // Set to admin user.
1896 $this->setAdminUser();
1898 // The user id is going to be 2 since we are the admin user.
1902 $course = $this->getDataGenerator()->create_course();
1904 // Create backup file and save it to the backup location.
1905 $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
1906 backup::INTERACTIVE_NO, backup::MODE_GENERAL, $userid);
1907 $bc->execute_plan();
1908 $results = $bc->get_results();
1909 $file = $results['backup_destination'];
1910 $fp = get_file_packer('application/vnd.moodle.backup');
1911 $filepath = $CFG->dataroot . '/temp/backup/test-restore-course-event';
1912 $file->extract_to_pathname($fp, $filepath);
1916 // Now we want to catch the restore course event.
1917 $sink = $this->redirectEvents();
1919 // Now restore the course to trigger the event.
1920 $rc = new restore_controller('test-restore-course-event', $course->id, backup::INTERACTIVE_NO,
1921 backup::MODE_GENERAL, $userid, backup::TARGET_NEW_COURSE);
1922 $rc->execute_precheck();
1923 $rc->execute_plan();
1925 // Capture the event.
1926 $events = $sink->get_events();
1929 // Validate the event.
1930 $event = array_pop($events);
1931 $this->assertInstanceOf('\core\event\course_restored', $event);
1932 $this->assertEquals('course', $event->objecttable);
1933 $this->assertEquals($rc->get_courseid(), $event->objectid);
1934 $this->assertEquals(context_course::instance($rc->get_courseid())->id, $event->contextid);
1935 $this->assertEquals('course_restored', $event->get_legacy_eventname());
1936 $legacydata = (object) array(
1937 'courseid' => $rc->get_courseid(),
1938 'userid' => $rc->get_userid(),
1939 'type' => $rc->get_type(),
1940 'target' => $rc->get_target(),
1941 'mode' => $rc->get_mode(),
1942 'operation' => $rc->get_operation(),
1943 'samesite' => $rc->is_samesite()
1945 $url = new moodle_url('/course/view.php', array('id' => $event->objectid));
1946 $this->assertEquals($url, $event->get_url());
1947 $this->assertEventLegacyData($legacydata, $event);
1948 $this->assertEventContextNotUsed($event);
1950 // Destroy the resource controller since we are done using it.
1956 * Test that triggering a course_section_updated event works as expected.
1958 public function test_course_section_updated_event() {
1961 $this->resetAfterTest();
1963 // Create the course with sections.
1964 $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true));
1965 $sections = $DB->get_records('course_sections', array('course' => $course->id));
1967 $coursecontext = context_course::instance($course->id);
1969 $section = array_pop($sections);
1970 $section->name = 'Test section';
1971 $section->summary = 'Test section summary';
1972 $DB->update_record('course_sections', $section);
1974 // Trigger an event for course section update.
1975 $event = \core\event\course_section_updated::create(
1977 'objectid' => $section->id,
1978 'courseid' => $course->id,
1979 'context' => context_course::instance($course->id),
1981 'sectionnum' => $section->section
1985 $event->add_record_snapshot('course_sections', $section);
1986 // Trigger and catch event.
1987 $sink = $this->redirectEvents();
1989 $events = $sink->get_events();
1992 // Validate the event.
1993 $event = $events[0];
1994 $this->assertInstanceOf('\core\event\course_section_updated', $event);
1995 $this->assertEquals('course_sections', $event->objecttable);
1996 $this->assertEquals($section->id, $event->objectid);
1997 $this->assertEquals($course->id, $event->courseid);
1998 $this->assertEquals($coursecontext->id, $event->contextid);
1999 $this->assertEquals($section->section, $event->other['sectionnum']);
2000 $expecteddesc = "The user with id '{$event->userid}' updated section number '{$event->other['sectionnum']}' for the course with id '{$event->courseid}'";
2001 $this->assertEquals($expecteddesc, $event->get_description());
2002 $url = new moodle_url('/course/editsection.php', array('id' => $event->objectid));
2003 $this->assertEquals($url, $event->get_url());
2004 $this->assertEquals($section, $event->get_record_snapshot('course_sections', $event->objectid));
2006 $sectionnum = $section->section;
2007 $expectedlegacydata = array($course->id, "course", "editsection", 'editsection.php?id=' . $id, $sectionnum);
2008 $this->assertEventLegacyLogData($expectedlegacydata, $event);
2009 $this->assertEventContextNotUsed($event);
2013 * Test that triggering a course_section_deleted event works as expected.
2015 public function test_course_section_deleted_event() {
2017 $this->resetAfterTest();
2018 $sink = $this->redirectEvents();
2020 // Create the course with sections.
2021 $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true));
2022 $sections = $DB->get_records('course_sections', array('course' => $course->id));
2023 $coursecontext = context_course::instance($course->id);
2024 $section = array_pop($sections);
2025 course_delete_section($course, $section);
2026 $events = $sink->get_events();
2027 $event = array_pop($events); // Delete section event.
2030 // Validate event data.
2031 $this->assertInstanceOf('\core\event\course_section_deleted', $event);
2032 $this->assertEquals('course_sections', $event->objecttable);
2033 $this->assertEquals($section->id, $event->objectid);
2034 $this->assertEquals($course->id, $event->courseid);
2035 $this->assertEquals($coursecontext->id, $event->contextid);
2036 $this->assertEquals($section->section, $event->other['sectionnum']);
2037 $expecteddesc = "The user with id '{$event->userid}' deleted section number '{$event->other['sectionnum']}' " .
2038 "(section name '{$event->other['sectionname']}') for the course with id '{$event->courseid}'";
2039 $this->assertEquals($expecteddesc, $event->get_description());
2040 $this->assertEquals($section, $event->get_record_snapshot('course_sections', $event->objectid));
2041 $this->assertNull($event->get_url());
2043 // Test legacy data.
2044 $sectionnum = $section->section;
2045 $expectedlegacydata = array($course->id, "course", "delete section", 'view.php?id=' . $course->id, $sectionnum);
2046 $this->assertEventLegacyLogData($expectedlegacydata, $event);
2047 $this->assertEventContextNotUsed($event);
2050 public function test_course_integrity_check() {
2053 $this->resetAfterTest(true);
2054 $course = $this->getDataGenerator()->create_course(array('numsections' => 1),
2055 array('createsections'=>true));
2057 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
2058 array('section' => 0));
2059 $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id),
2060 array('section' => 0));
2061 $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id),
2062 array('section' => 0));
2063 $correctseq = join(',', array($forum->cmid, $page->cmid, $quiz->cmid));
2065 $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2066 $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2067 $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2068 $this->assertEquals($correctseq, $section0->sequence);
2069 $this->assertEmpty($section1->sequence);
2070 $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2071 $this->assertEquals($section0->id, $cms[$page->cmid]->section);
2072 $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2073 $this->assertEmpty(course_integrity_check($course->id));
2075 // Now let's make manual change in DB and let course_integrity_check() fix it:
2077 // 1. Module appears twice in one section.
2078 $DB->update_record('course_sections', array('id' => $section0->id, 'sequence' => $section0->sequence. ','. $page->cmid));
2079 $this->assertEquals(
2080 array('Failed integrity check for course ['. $course->id.
2081 ']. Sequence for course section ['. $section0->id. '] is "'.
2082 $section0->sequence. ','. $page->cmid. '", must be "'.
2083 $section0->sequence. '"'),
2084 course_integrity_check($course->id));
2085 $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2086 $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2087 $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2088 $this->assertEquals($correctseq, $section0->sequence);
2089 $this->assertEmpty($section1->sequence);
2090 $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2091 $this->assertEquals($section0->id, $cms[$page->cmid]->section);
2092 $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2094 // 2. Module appears in two sections (last section wins).
2095 $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''. $page->cmid));
2096 // First message about double mentioning in sequence, second message about wrong section field for $page.
2097 $this->assertEquals(array(
2098 'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
2099 '] must be removed from sequence of section ['. $section0->id.
2100 '] because it is also present in sequence of section ['. $section1->id. ']',
2101 'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
2102 '] points to section ['. $section0->id. '] instead of ['. $section1->id. ']'),
2103 course_integrity_check($course->id));
2104 $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2105 $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2106 $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2107 $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
2108 $this->assertEquals(''. $page->cmid, $section1->sequence);
2109 $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2110 $this->assertEquals($section1->id, $cms[$page->cmid]->section);
2111 $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2113 // 3. Module id is not present in course_section.sequence (integrity check with $fullcheck = false).
2114 $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''));
2115 $this->assertEmpty(course_integrity_check($course->id)); // Not an error!
2116 $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2117 $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2118 $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2119 $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
2120 $this->assertEmpty($section1->sequence);
2121 $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2122 $this->assertEquals($section1->id, $cms[$page->cmid]->section); // Not changed.
2123 $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2125 // 4. Module id is not present in course_section.sequence (integrity check with $fullcheck = true).
2126 $this->assertEquals(array('Failed integrity check for course ['. $course->id. ']. Course module ['.
2127 $page->cmid. '] is missing from sequence of section ['. $section1->id. ']'),
2128 course_integrity_check($course->id, null, null, true)); // Error!
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($forum->cmid. ','. $quiz->cmid, $section0->sequence);
2133 $this->assertEquals(''. $page->cmid, $section1->sequence); // Yay, module added to section.
2134 $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2135 $this->assertEquals($section1->id, $cms[$page->cmid]->section); // Not changed.
2136 $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2138 // 5. Module id is not present in course_section.sequence and it's section is invalid (integrity check with $fullcheck = true).
2139 $DB->update_record('course_modules', array('id' => $page->cmid, 'section' => 8765));
2140 $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''));
2141 $this->assertEquals(array(
2142 'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
2143 '] is missing from sequence of section ['. $section0->id. ']',
2144 'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
2145 '] points to section [8765] instead of ['. $section0->id. ']'),
2146 course_integrity_check($course->id, null, null, true));
2147 $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2148 $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2149 $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2150 $this->assertEquals($forum->cmid. ','. $quiz->cmid. ','. $page->cmid, $section0->sequence); // Module added to section.
2151 $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2152 $this->assertEquals($section0->id, $cms[$page->cmid]->section); // Section changed to section0.
2153 $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2155 // 6. Module is deleted from course_modules but not deleted in sequence (integrity check with $fullcheck = true).
2156 $DB->delete_records('course_modules', array('id' => $page->cmid));
2157 $this->assertEquals(array('Failed integrity check for course ['. $course->id. ']. Course module ['.
2158 $page->cmid. '] does not exist but is present in the sequence of section ['. $section0->id. ']'),
2159 course_integrity_check($course->id, null, null, true));
2160 $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2161 $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2162 $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2163 $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
2164 $this->assertEmpty($section1->sequence);
2165 $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2166 $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2167 $this->assertEquals(2, count($cms));
2171 * Tests for event related to course module creation.
2173 public function test_course_module_created_event() {
2175 $this->resetAfterTest();
2177 // Create an assign module.
2178 $sink = $this->redirectEvents();
2179 $modinfo = $this->create_specific_module_test('assign');
2180 $events = $sink->get_events();
2181 $event = array_pop($events);
2183 $cm = get_coursemodule_from_id('assign', $modinfo->coursemodule, 0, false, MUST_EXIST);
2184 $mod = $DB->get_record('assign', array('id' => $modinfo->instance), '*', MUST_EXIST);
2186 // Validate event data.
2187 $this->assertInstanceOf('\core\event\course_module_created', $event);
2188 $this->assertEquals($cm->id, $event->objectid);
2189 $this->assertEquals($USER->id, $event->userid);
2190 $this->assertEquals('course_modules', $event->objecttable);
2191 $url = new moodle_url('/mod/assign/view.php', array('id' => $cm->id));
2192 $this->assertEquals($url, $event->get_url());
2194 // Test legacy data.
2195 $this->assertSame('mod_created', $event->get_legacy_eventname());
2196 $eventdata = new stdClass();
2197 $eventdata->modulename = 'assign';
2198 $eventdata->name = $mod->name;
2199 $eventdata->cmid = $cm->id;
2200 $eventdata->courseid = $cm->course;
2201 $eventdata->userid = $USER->id;
2202 $this->assertEventLegacyData($eventdata, $event);
2205 array($cm->course, "course", "add mod", "../mod/assign/view.php?id=$cm->id", "assign $cm->instance"),
2206 array($cm->course, "assign", "add", "view.php?id=$cm->id", $cm->instance, $cm->id)
2208 $this->assertEventLegacyLogData($arr, $event);
2209 $this->assertEventContextNotUsed($event);
2211 // Let us see if duplicating an activity results in a nice course module created event.
2213 $course = get_course($mod->course);
2214 $newcm = duplicate_module($course, $cm);
2215 $events = $sink->get_events();
2216 $event = array_pop($events);
2219 // Validate event data.
2220 $this->assertInstanceOf('\core\event\course_module_created', $event);
2221 $this->assertEquals($newcm->id, $event->objectid);
2222 $this->assertEquals($USER->id, $event->userid);
2223 $this->assertEquals($course->id, $event->courseid);
2224 $url = new moodle_url('/mod/assign/view.php', array('id' => $newcm->id));
2225 $this->assertEquals($url, $event->get_url());
2229 * Tests for event validations related to course module creation.
2231 public function test_course_module_created_event_exceptions() {
2233 $this->resetAfterTest();
2236 $modinfo = $this->create_specific_module_test('assign');
2237 $context = context_module::instance($modinfo->coursemodule);
2239 // Test not setting instanceid.
2241 $event = \core\event\course_module_created::create(array(
2242 'courseid' => $modinfo->course,
2243 'context' => $context,
2244 'objectid' => $modinfo->coursemodule,
2246 'modulename' => 'assign',
2247 'name' => 'My assignment',
2250 $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2251 other['instanceid']");
2252 } catch (coding_exception $e) {
2253 $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
2256 // Test not setting modulename.
2258 $event = \core\event\course_module_created::create(array(
2259 'courseid' => $modinfo->course,
2260 'context' => $context,
2261 'objectid' => $modinfo->coursemodule,
2263 'instanceid' => $modinfo->instance,
2264 'name' => 'My assignment',
2267 $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2268 other['modulename']");
2269 } catch (coding_exception $e) {
2270 $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
2273 // Test not setting name.
2276 $event = \core\event\course_module_created::create(array(
2277 'courseid' => $modinfo->course,
2278 'context' => $context,
2279 'objectid' => $modinfo->coursemodule,
2281 'modulename' => 'assign',
2282 'instanceid' => $modinfo->instance,
2285 $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2287 } catch (coding_exception $e) {
2288 $this->assertContains("The 'name' value must be set in other.", $e->getMessage());
2294 * Tests for event related to course module updates.
2296 public function test_course_module_updated_event() {
2298 $this->resetAfterTest();
2300 // Update a forum module.
2301 $sink = $this->redirectEvents();
2302 $modinfo = $this->update_specific_module_test('forum');
2303 $events = $sink->get_events();
2304 $event = array_pop($events);
2307 $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
2308 $mod = $DB->get_record('forum', array('id' => $cm->instance), '*', MUST_EXIST);
2310 // Validate event data.
2311 $this->assertInstanceOf('\core\event\course_module_updated', $event);
2312 $this->assertEquals($cm->id, $event->objectid);
2313 $this->assertEquals($USER->id, $event->userid);
2314 $this->assertEquals('course_modules', $event->objecttable);
2315 $url = new moodle_url('/mod/forum/view.php', array('id' => $cm->id));
2316 $this->assertEquals($url, $event->get_url());
2318 // Test legacy data.
2319 $this->assertSame('mod_updated', $event->get_legacy_eventname());
2320 $eventdata = new stdClass();
2321 $eventdata->modulename = 'forum';
2322 $eventdata->name = $mod->name;
2323 $eventdata->cmid = $cm->id;
2324 $eventdata->courseid = $cm->course;
2325 $eventdata->userid = $USER->id;
2326 $this->assertEventLegacyData($eventdata, $event);
2329 array($cm->course, "course", "update mod", "../mod/forum/view.php?id=$cm->id", "forum $cm->instance"),
2330 array($cm->course, "forum", "update", "view.php?id=$cm->id", $cm->instance, $cm->id)
2332 $this->assertEventLegacyLogData($arr, $event);
2333 $this->assertEventContextNotUsed($event);
2337 * Tests for create_from_cm method.
2339 public function test_course_module_create_from_cm() {
2340 $this->resetAfterTest();
2341 $this->setAdminUser();
2343 // Create course and modules.
2344 $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
2346 // Generate an assignment.
2347 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
2349 // Get the module context.
2350 $modcontext = context_module::instance($assign->cmid);
2352 // Get course module.
2353 $cm = get_coursemodule_from_id(null, $assign->cmid, $course->id, false, MUST_EXIST);
2355 // Create an event from course module.
2356 $event = \core\event\course_module_updated::create_from_cm($cm, $modcontext);
2358 // Trigger the events.
2359 $sink = $this->redirectEvents();
2361 $events = $sink->get_events();
2362 $event2 = array_pop($events);
2365 $this->assertInstanceOf('\core\event\course_module_updated', $event);
2366 $this->assertEquals($cm->id, $event2->objectid);
2367 $this->assertEquals($modcontext, $event2->get_context());
2368 $this->assertEquals($cm->modname, $event2->other['modulename']);
2369 $this->assertEquals($cm->instance, $event2->other['instanceid']);
2370 $this->assertEquals($cm->name, $event2->other['name']);
2371 $this->assertEventContextNotUsed($event2);
2372 $this->assertSame('mod_updated', $event2->get_legacy_eventname());
2374 array($cm->course, "course", "update mod", "../mod/assign/view.php?id=$cm->id", "assign $cm->instance"),
2375 array($cm->course, "assign", "update", "view.php?id=$cm->id", $cm->instance, $cm->id)
2377 $this->assertEventLegacyLogData($arr, $event);
2381 * Tests for event validations related to course module update.
2383 public function test_course_module_updated_event_exceptions() {
2385 $this->resetAfterTest();
2388 $modinfo = $this->create_specific_module_test('assign');
2389 $context = context_module::instance($modinfo->coursemodule);
2391 // Test not setting instanceid.
2393 $event = \core\event\course_module_updated::create(array(
2394 'courseid' => $modinfo->course,
2395 'context' => $context,
2396 'objectid' => $modinfo->coursemodule,
2398 'modulename' => 'assign',
2399 'name' => 'My assignment',
2402 $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2403 other['instanceid']");
2404 } catch (coding_exception $e) {
2405 $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
2408 // Test not setting modulename.
2410 $event = \core\event\course_module_updated::create(array(
2411 'courseid' => $modinfo->course,
2412 'context' => $context,
2413 'objectid' => $modinfo->coursemodule,
2415 'instanceid' => $modinfo->instance,
2416 'name' => 'My assignment',
2419 $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2420 other['modulename']");
2421 } catch (coding_exception $e) {
2422 $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
2425 // Test not setting name.
2428 $event = \core\event\course_module_updated::create(array(
2429 'courseid' => $modinfo->course,
2430 'context' => $context,
2431 'objectid' => $modinfo->coursemodule,
2433 'modulename' => 'assign',
2434 'instanceid' => $modinfo->instance,
2437 $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2439 } catch (coding_exception $e) {
2440 $this->assertContains("The 'name' value must be set in other.", $e->getMessage());
2446 * Tests for event related to course module delete.
2448 public function test_course_module_deleted_event() {
2450 $this->resetAfterTest();
2452 // Create and delete a module.
2453 $sink = $this->redirectEvents();
2454 $modinfo = $this->create_specific_module_test('forum');
2455 $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
2456 course_delete_module($modinfo->coursemodule);
2457 $events = $sink->get_events();
2458 $event = array_pop($events); // delete module event.;
2461 // Validate event data.
2462 $this->assertInstanceOf('\core\event\course_module_deleted', $event);
2463 $this->assertEquals($cm->id, $event->objectid);
2464 $this->assertEquals($USER->id, $event->userid);
2465 $this->assertEquals('course_modules', $event->objecttable);
2466 $this->assertEquals(null, $event->get_url());
2467 $this->assertEquals($cm, $event->get_record_snapshot('course_modules', $cm->id));
2469 // Test legacy data.
2470 $this->assertSame('mod_deleted', $event->get_legacy_eventname());
2471 $eventdata = new stdClass();
2472 $eventdata->modulename = 'forum';
2473 $eventdata->cmid = $cm->id;
2474 $eventdata->courseid = $cm->course;
2475 $eventdata->userid = $USER->id;
2476 $this->assertEventLegacyData($eventdata, $event);
2478 $arr = array($cm->course, 'course', "delete mod", "view.php?id=$cm->course", "forum $cm->instance", $cm->id);
2479 $this->assertEventLegacyLogData($arr, $event);
2484 * Tests for event validations related to course module deletion.
2486 public function test_course_module_deleted_event_exceptions() {
2488 $this->resetAfterTest();
2491 $modinfo = $this->create_specific_module_test('assign');
2492 $context = context_module::instance($modinfo->coursemodule);
2494 // Test not setting instanceid.
2496 $event = \core\event\course_module_deleted::create(array(
2497 'courseid' => $modinfo->course,
2498 'context' => $context,
2499 'objectid' => $modinfo->coursemodule,
2501 'modulename' => 'assign',
2502 'name' => 'My assignment',
2505 $this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
2506 other['instanceid']");
2507 } catch (coding_exception $e) {
2508 $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
2511 // Test not setting modulename.
2513 $event = \core\event\course_module_deleted::create(array(
2514 'courseid' => $modinfo->course,
2515 'context' => $context,
2516 'objectid' => $modinfo->coursemodule,
2518 'instanceid' => $modinfo->instance,
2519 'name' => 'My assignment',
2522 $this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
2523 other['modulename']");
2524 } catch (coding_exception $e) {
2525 $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
2530 * Returns a user object and its assigned new role.
2532 * @param testing_data_generator $generator
2534 * @return array The user object and the role ID
2536 protected function get_user_objects(testing_data_generator $generator, $contextid) {
2539 if (empty($USER->id)) {
2540 $user = $generator->create_user();
2541 $this->setUser($user);
2543 $roleid = create_role('Test role', 'testrole', 'Test role description');
2544 if (!is_array($contextid)) {
2545 $contextid = array($contextid);
2547 foreach ($contextid as $cid) {
2548 $assignid = role_assign($roleid, $user->id, $cid);
2550 return array($user, $roleid);
2554 * Test course move after course.
2556 public function test_course_change_sortorder_after_course() {
2559 $this->resetAfterTest(true);
2561 $generator = $this->getDataGenerator();
2562 $category = $generator->create_category();
2563 $course3 = $generator->create_course(array('category' => $category->id));
2564 $course2 = $generator->create_course(array('category' => $category->id));
2565 $course1 = $generator->create_course(array('category' => $category->id));
2566 $context = $category->get_context();
2568 list($user, $roleid) = $this->get_user_objects($generator, $context->id);
2569 $caps = course_capability_assignment::allow('moodle/category:manage', $roleid, $context->id);
2571 $courses = $category->get_courses();
2572 $this->assertInternalType('array', $courses);
2573 $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2574 $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2575 $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2577 // Test moving down.
2578 $this->assertTrue(course_change_sortorder_after_course($course1->id, $course3->id));
2579 $courses = $category->get_courses();
2580 $this->assertInternalType('array', $courses);
2581 $this->assertEquals(array($course2->id, $course3->id, $course1->id), array_keys($courses));
2582 $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2583 $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2586 $this->assertTrue(course_change_sortorder_after_course($course1->id, $course2->id));
2587 $courses = $category->get_courses();
2588 $this->assertInternalType('array', $courses);
2589 $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
2590 $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2591 $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2593 // Test moving to the top.
2594 $this->assertTrue(course_change_sortorder_after_course($course1->id, 0));
2595 $courses = $category->get_courses();
2596 $this->assertInternalType('array', $courses);
2597 $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2598 $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2599 $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2603 * Tests changing the visibility of a course.
2605 public function test_course_change_visibility() {
2608 $this->resetAfterTest(true);
2610 $generator = $this->getDataGenerator();
2611 $category = $generator->create_category();
2612 $course = $generator->create_course(array('category' => $category->id));
2614 $this->assertEquals('1', $course->visible);
2615 $this->assertEquals('1', $course->visibleold);
2617 $this->assertTrue(course_change_visibility($course->id, false));
2618 $course = $DB->get_record('course', array('id' => $course->id));
2619 $this->assertEquals('0', $course->visible);
2620 $this->assertEquals('0', $course->visibleold);
2622 $this->assertTrue(course_change_visibility($course->id, true));
2623 $course = $DB->get_record('course', array('id' => $course->id));
2624 $this->assertEquals('1', $course->visible);
2625 $this->assertEquals('1', $course->visibleold);
2629 * Tests moving the course up and down by one.
2631 public function test_course_change_sortorder_by_one() {
2634 $this->resetAfterTest(true);
2636 $generator = $this->getDataGenerator();
2637 $category = $generator->create_category();
2638 $course3 = $generator->create_course(array('category' => $category->id));
2639 $course2 = $generator->create_course(array('category' => $category->id));
2640 $course1 = $generator->create_course(array('category' => $category->id));
2642 $courses = $category->get_courses();
2643 $this->assertInternalType('array', $courses);
2644 $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2645 $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2646 $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2648 // Test moving down.
2649 $course1 = get_course($course1->id);
2650 $this->assertTrue(course_change_sortorder_by_one($course1, false));
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));
2658 $course1 = get_course($course1->id);
2659 $this->assertTrue(course_change_sortorder_by_one($course1, true));
2660 $courses = $category->get_courses();
2661 $this->assertInternalType('array', $courses);
2662 $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2663 $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2664 $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2666 // Test moving the top course up one.
2667 $course1 = get_course($course1->id);
2668 $this->assertFalse(course_change_sortorder_by_one($course1, true));
2669 // Check nothing changed.
2670 $courses = $category->get_courses();
2671 $this->assertInternalType('array', $courses);
2672 $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2673 $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2674 $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2676 // Test moving the bottom course up down.
2677 $course3 = get_course($course3->id);
2678 $this->assertFalse(course_change_sortorder_by_one($course3, false));
2679 // Check nothing changed.
2680 $courses = $category->get_courses();
2681 $this->assertInternalType('array', $courses);
2682 $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2683 $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2684 $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2687 public function test_view_resources_list() {
2688 $this->resetAfterTest();
2690 $course = self::getDataGenerator()->create_course();
2691 $coursecontext = context_course::instance($course->id);
2693 $event = \core\event\course_resources_list_viewed::create(array('context' => context_course::instance($course->id)));
2694 $event->set_legacy_logdata(array('book', 'page', 'resource'));
2695 $sink = $this->redirectEvents();
2697 $events = $sink->get_events();
2700 // Validate the event.
2701 $event = $events[0];
2702 $this->assertInstanceOf('\core\event\course_resources_list_viewed', $event);
2703 $this->assertEquals(null, $event->objecttable);
2704 $this->assertEquals(null, $event->objectid);
2705 $this->assertEquals($course->id, $event->courseid);
2706 $this->assertEquals($coursecontext->id, $event->contextid);
2707 $expectedlegacydata = array(
2708 array($course->id, "book", "view all", 'index.php?id=' . $course->id, ''),
2709 array($course->id, "page", "view all", 'index.php?id=' . $course->id, ''),
2710 array($course->id, "resource", "view all", 'index.php?id=' . $course->id, ''),
2712 $this->assertEventLegacyLogData($expectedlegacydata, $event);
2713 $this->assertEventContextNotUsed($event);
2717 * Test duplicate_module()
2719 public function test_duplicate_module() {
2720 $this->setAdminUser();
2721 $this->resetAfterTest();
2722 $course = self::getDataGenerator()->create_course();
2723 $res = self::getDataGenerator()->create_module('resource', array('course' => $course));
2724 $cm = get_coursemodule_from_id('resource', $res->cmid, 0, false, MUST_EXIST);
2726 $newcm = duplicate_module($course, $cm);
2728 // Make sure they are the same, except obvious id changes.
2729 foreach ($cm as $prop => $value) {
2730 if ($prop == 'id' || $prop == 'url' || $prop == 'instance' || $prop == 'added') {
2731 // Ignore obviously different properties.
2734 $this->assertEquals($value, $newcm->$prop);
2739 * Tests that when creating or updating a module, if the availability settings
2740 * are present but set to an empty tree, availability is set to null in
2743 public function test_empty_availability_settings() {
2745 $this->setAdminUser();
2746 $this->resetAfterTest();
2748 // Enable availability.
2749 set_config('enableavailability', 1);
2752 $emptyavailability = json_encode(\core_availability\tree::get_root_json(array()));
2753 $course = self::getDataGenerator()->create_course();
2754 $label = self::getDataGenerator()->create_module('label', array(
2755 'course' => $course, 'availability' => $emptyavailability));
2756 $this->assertNull($DB->get_field('course_modules', 'availability',
2757 array('id' => $label->cmid)));
2760 $formdata = $DB->get_record('course_modules', array('id' => $label->cmid));
2761 unset($formdata->availability);
2762 $formdata->availabilityconditionsjson = $emptyavailability;
2763 $formdata->modulename = 'label';
2764 $formdata->coursemodule = $label->cmid;
2766 file_prepare_draft_area($draftid, context_module::instance($label->cmid)->id,
2767 'mod_label', 'intro', 0);
2768 $formdata->introeditor = array(
2769 'itemid' => $draftid,
2770 'text' => '<p>Yo</p>',
2771 'format' => FORMAT_HTML);
2772 update_module($formdata);
2773 $this->assertNull($DB->get_field('course_modules', 'availability',
2774 array('id' => $label->cmid)));