MDL-41266 Logging: Added choose option for external database store
[moodle.git] / course / tests / courselib_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * Course related unit tests
19  *
20  * @package    core
21  * @category   phpunit
22  * @copyright  2012 Petr Skoda {@link http://skodak.org}
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->dirroot . '/course/lib.php');
30 require_once($CFG->dirroot . '/course/tests/fixtures/course_capability_assignment.php');
31 require_once($CFG->dirroot . '/enrol/imsenterprise/tests/imsenterprise_test.php');
33 class core_course_courselib_testcase extends advanced_testcase {
35     /**
36      * Set forum specific test values for calling create_module().
37      *
38      * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
39      */
40     private function forum_create_set_values(&$moduleinfo) {
41         // Completion specific to forum - optional.
42         $moduleinfo->completionposts = 3;
43         $moduleinfo->completiondiscussions = 1;
44         $moduleinfo->completionreplies = 2;
46         // Specific values to the Forum module.
47         $moduleinfo->forcesubscribe = FORUM_INITIALSUBSCRIBE;
48         $moduleinfo->type = 'single';
49         $moduleinfo->trackingtype = FORUM_TRACKING_FORCED;
50         $moduleinfo->maxbytes = 10240;
51         $moduleinfo->maxattachments = 2;
53         // Post threshold for blocking - specific to forum.
54         $moduleinfo->blockperiod = 60*60*24;
55         $moduleinfo->blockafter = 10;
56         $moduleinfo->warnafter = 5;
57     }
59     /**
60      * Execute test asserts on the saved DB data by create_module($forum).
61      *
62      * @param object $moduleinfo - the specific forum values that were used to create a forum.
63      * @param object $dbmodinstance - the DB values of the created forum.
64      */
65     private function forum_create_run_asserts($moduleinfo, $dbmodinstance) {
66         // Compare values specific to forums.
67         $this->assertEquals($moduleinfo->forcesubscribe, $dbmodinstance->forcesubscribe);
68         $this->assertEquals($moduleinfo->type, $dbmodinstance->type);
69         $this->assertEquals($moduleinfo->assessed, $dbmodinstance->assessed);
70         $this->assertEquals($moduleinfo->completionposts, $dbmodinstance->completionposts);
71         $this->assertEquals($moduleinfo->completiondiscussions, $dbmodinstance->completiondiscussions);
72         $this->assertEquals($moduleinfo->completionreplies, $dbmodinstance->completionreplies);
73         $this->assertEquals($moduleinfo->scale, $dbmodinstance->scale);
74         $this->assertEquals($moduleinfo->assesstimestart, $dbmodinstance->assesstimestart);
75         $this->assertEquals($moduleinfo->assesstimefinish, $dbmodinstance->assesstimefinish);
76         $this->assertEquals($moduleinfo->rsstype, $dbmodinstance->rsstype);
77         $this->assertEquals($moduleinfo->rssarticles, $dbmodinstance->rssarticles);
78         $this->assertEquals($moduleinfo->trackingtype, $dbmodinstance->trackingtype);
79         $this->assertEquals($moduleinfo->maxbytes, $dbmodinstance->maxbytes);
80         $this->assertEquals($moduleinfo->maxattachments, $dbmodinstance->maxattachments);
81         $this->assertEquals($moduleinfo->blockperiod, $dbmodinstance->blockperiod);
82         $this->assertEquals($moduleinfo->blockafter, $dbmodinstance->blockafter);
83         $this->assertEquals($moduleinfo->warnafter, $dbmodinstance->warnafter);
84     }
86     /**
87      * Set assign module specific test values for calling create_module().
88      *
89      * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
90      */
91     private function assign_create_set_values(&$moduleinfo) {
92         // Specific values to the Assign module.
93         $moduleinfo->alwaysshowdescription = true;
94         $moduleinfo->submissiondrafts = true;
95         $moduleinfo->requiresubmissionstatement = true;
96         $moduleinfo->sendnotifications = true;
97         $moduleinfo->sendlatenotifications = true;
98         $moduleinfo->duedate = time() + (7 * 24 * 3600);
99         $moduleinfo->cutoffdate = time() + (7 * 24 * 3600);
100         $moduleinfo->allowsubmissionsfromdate = time();
101         $moduleinfo->teamsubmission = true;
102         $moduleinfo->requireallteammemberssubmit = true;
103         $moduleinfo->teamsubmissiongroupingid = true;
104         $moduleinfo->blindmarking = true;
105         $moduleinfo->markingworkflow = true;
106         $moduleinfo->markingallocation = true;
107         $moduleinfo->assignsubmission_onlinetext_enabled = true;
108         $moduleinfo->assignsubmission_file_enabled = true;
109         $moduleinfo->assignsubmission_file_maxfiles = 1;
110         $moduleinfo->assignsubmission_file_maxsizebytes = 1000000;
111         $moduleinfo->assignsubmission_comments_enabled = true;
112         $moduleinfo->assignfeedback_comments_enabled = true;
113         $moduleinfo->assignfeedback_offline_enabled = true;
114         $moduleinfo->assignfeedback_file_enabled = true;
116         // Advanced grading.
117         $gradingmethods = grading_manager::available_methods();
118         $moduleinfo->advancedgradingmethod_submissions = current(array_keys($gradingmethods));
119     }
121     /**
122      * Execute test asserts on the saved DB data by create_module($assign).
123      *
124      * @param object $moduleinfo - the specific assign module values that were used to create an assign module.
125      * @param object $dbmodinstance - the DB values of the created assign module.
126      */
127     private function assign_create_run_asserts($moduleinfo, $dbmodinstance) {
128         global $DB;
130         $this->assertEquals($moduleinfo->alwaysshowdescription, $dbmodinstance->alwaysshowdescription);
131         $this->assertEquals($moduleinfo->submissiondrafts, $dbmodinstance->submissiondrafts);
132         $this->assertEquals($moduleinfo->requiresubmissionstatement, $dbmodinstance->requiresubmissionstatement);
133         $this->assertEquals($moduleinfo->sendnotifications, $dbmodinstance->sendnotifications);
134         $this->assertEquals($moduleinfo->duedate, $dbmodinstance->duedate);
135         $this->assertEquals($moduleinfo->cutoffdate, $dbmodinstance->cutoffdate);
136         $this->assertEquals($moduleinfo->allowsubmissionsfromdate, $dbmodinstance->allowsubmissionsfromdate);
137         $this->assertEquals($moduleinfo->teamsubmission, $dbmodinstance->teamsubmission);
138         $this->assertEquals($moduleinfo->requireallteammemberssubmit, $dbmodinstance->requireallteammemberssubmit);
139         $this->assertEquals($moduleinfo->teamsubmissiongroupingid, $dbmodinstance->teamsubmissiongroupingid);
140         $this->assertEquals($moduleinfo->blindmarking, $dbmodinstance->blindmarking);
141         $this->assertEquals($moduleinfo->markingworkflow, $dbmodinstance->markingworkflow);
142         $this->assertEquals($moduleinfo->markingallocation, $dbmodinstance->markingallocation);
143         // The goal not being to fully test assign_add_instance() we'll stop here for the assign tests - to avoid too many DB queries.
145         // Advanced grading.
146         $contextmodule = context_module::instance($dbmodinstance->id);
147         $advancedgradingmethod = $DB->get_record('grading_areas',
148             array('contextid' => $contextmodule->id,
149                 'activemethod' => $moduleinfo->advancedgradingmethod_submissions));
150         $this->assertEquals($moduleinfo->advancedgradingmethod_submissions, $advancedgradingmethod);
151     }
153     /**
154      * Run some asserts test for a specific module for the function create_module().
155      *
156      * The function has been created (and is called) for $this->test_create_module().
157      * Note that the call to MODULE_create_set_values and MODULE_create_run_asserts are done after the common set values/run asserts.
158      * So if you want, you can overwrite the default values/asserts in the respective functions.
159      * @param string $modulename Name of the module ('forum', 'assign', 'book'...).
160      */
161     private function create_specific_module_test($modulename) {
162         global $DB, $CFG;
164         $this->resetAfterTest(true);
166         $this->setAdminUser();
168         // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
169         require_once($CFG->dirroot.'/mod/'. $modulename .'/lib.php');
171         // Enable avaibility.
172         // If not enabled all conditional fields will be ignored.
173         set_config('enableavailability', 1);
175         // Enable course completion.
176         // If not enabled all completion settings will be ignored.
177         set_config('enablecompletion', COMPLETION_ENABLED);
179         // Enable forum RSS feeds.
180         set_config('enablerssfeeds', 1);
181         set_config('forum_enablerssfeeds', 1);
183         $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED),
184            array('createsections'=>true));
186         $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
188         // Create assign module instance for test.
189         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
190         $params['course'] = $course->id;
191         $instance = $generator->create_instance($params);
192         $assigncm = get_coursemodule_from_instance('assign', $instance->id);
194         // Module test values.
195         $moduleinfo = new stdClass();
197         // Always mandatory generic values to any module.
198         $moduleinfo->modulename = $modulename;
199         $moduleinfo->section = 1; // This is the section number in the course. Not the section id in the database.
200         $moduleinfo->course = $course->id;
201         $moduleinfo->groupingid = $grouping->id;
202         $moduleinfo->groupmembersonly = 0;
203         $moduleinfo->visible = true;
205         // Sometimes optional generic values for some modules.
206         $moduleinfo->name = 'My test module';
207         $moduleinfo->showdescription = 1; // standard boolean
208         require_once($CFG->libdir . '/gradelib.php');
209         $gradecats = grade_get_categories_menu($moduleinfo->course, false);
210         $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
211         $moduleinfo->gradecat = $gradecatid;
212         $moduleinfo->groupmode = VISIBLEGROUPS;
213         $moduleinfo->cmidnumber = 'idnumber_XXX';
215         // Completion common to all module.
216         $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC;
217         $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED;
218         $moduleinfo->completiongradeitemnumber = 1;
219         $moduleinfo->completionexpected = time() + (7 * 24 * 3600);
221         // Conditional activity.
222         $moduleinfo->availablefrom = time();
223         $moduleinfo->availableuntil = time() + (7 * 24 * 3600);
224         $moduleinfo->showavailability = CONDITION_STUDENTVIEW_SHOW;
225         $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself.
226         $moduleinfo->conditiongradegroup = array(array('conditiongradeitemid' => $coursegradeitem->id, 'conditiongrademin' => 10, 'conditiongrademax' => 80));
227         $moduleinfo->conditionfieldgroup = array(array('conditionfield' => 'email', 'conditionfieldoperator' => OP_CONTAINS, 'conditionfieldvalue' => '@'));
228         $moduleinfo->conditioncompletiongroup = array(array('conditionsourcecmid' => $assigncm->id, 'conditionrequiredcompletion' => COMPLETION_COMPLETE)); // "conditionsourcecmid == 0" => none
230         // Grading and Advanced grading.
231         require_once($CFG->dirroot . '/rating/lib.php');
232         $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE;
233         $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number.
234         $moduleinfo->assesstimestart = time();
235         $moduleinfo->assesstimefinish = time() + (7 * 24 * 3600);
237         // RSS.
238         $moduleinfo->rsstype = 2;
239         $moduleinfo->rssarticles = 10;
241         // Optional intro editor (depends of module).
242         $draftid_editor = 0;
243         file_prepare_draft_area($draftid_editor, null, null, null, null);
244         $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
246         // Following is the advanced grading method area called 'submissions' for the 'assign' module.
247         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
248             $moduleinfo->grade = 100;
249         }
251         // Plagiarism form values.
252         // No plagiarism plugin installed by default. Use this space to make your own test.
254         // Values specific to the module.
255         $modulesetvalues = $modulename.'_create_set_values';
256         $this->$modulesetvalues($moduleinfo);
258         // Create the module.
259         $result = create_module($moduleinfo);
261         // Retrieve the module info.
262         $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance));
263         $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance);
264         // We passed the course section number to create_courses but $dbcm contain the section id.
265         // We need to retrieve the db course section number.
266         $section = $DB->get_record('course_sections', array('course' => $dbcm->course, 'id' => $dbcm->section));
267         // Retrieve the grade item.
268         $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course,
269             'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename));
271         // Compare the values common to all module instances.
272         $this->assertEquals($moduleinfo->modulename, $dbcm->modname);
273         $this->assertEquals($moduleinfo->section, $section->section);
274         $this->assertEquals($moduleinfo->course, $dbcm->course);
275         $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
276         $this->assertEquals($moduleinfo->groupmembersonly, $dbcm->groupmembersonly);
277         $this->assertEquals($moduleinfo->visible, $dbcm->visible);
278         $this->assertEquals($moduleinfo->completion, $dbcm->completion);
279         $this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
280         $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber);
281         $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected);
282         $this->assertEquals($moduleinfo->availablefrom, $dbcm->availablefrom);
283         $this->assertEquals($moduleinfo->availableuntil, $dbcm->availableuntil);
284         $this->assertEquals($moduleinfo->showavailability, $dbcm->showavailability);
285         $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription);
286         $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode);
287         $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber);
288         $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid);
290         // Optional grade testing.
291         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
292             $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade);
293         }
295         // Some optional (but quite common) to some module.
296         $this->assertEquals($moduleinfo->name, $dbmodinstance->name);
297         $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro);
298         $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat);
300         // Common values when conditional activity is enabled.
301         foreach ($moduleinfo->conditionfieldgroup as $fieldgroup) {
302             $isfieldgroupsaved = $DB->count_records('course_modules_avail_fields', array('coursemoduleid' => $dbcm->id,
303                 'userfield' => $fieldgroup['conditionfield'], 'operator' => $fieldgroup['conditionfieldoperator'],
304                 'value' => $fieldgroup['conditionfieldvalue']));
305             $this->assertEquals(1, $isfieldgroupsaved);
306         }
307         foreach ($moduleinfo->conditiongradegroup as $gradegroup) {
308             $isgradegroupsaved = $DB->count_records('course_modules_availability', array('coursemoduleid' => $dbcm->id,
309                 'grademin' => $gradegroup['conditiongrademin'], 'grademax' => $gradegroup['conditiongrademax'],
310                 'gradeitemid' => $gradegroup['conditiongradeitemid']));
311             $this->assertEquals(1, $isgradegroupsaved);
312         }
313         foreach ($moduleinfo->conditioncompletiongroup as $completiongroup) {
314             $iscompletiongroupsaved = $DB->count_records('course_modules_availability', array('coursemoduleid' => $dbcm->id,
315                 'sourcecmid' => $completiongroup['conditionsourcecmid'], 'requiredcompletion' => $completiongroup['conditionrequiredcompletion']));
316             $this->assertEquals(1, $iscompletiongroupsaved);
317         }
319         // Test specific to the module.
320         $modulerunasserts = $modulename.'_create_run_asserts';
321         $this->$modulerunasserts($moduleinfo, $dbmodinstance);
322         return $moduleinfo;
323     }
325     /**
326      * Test create_module() for multiple modules defined in the $modules array (first declaration of the function).
327      */
328     public function test_create_module() {
329         // Add the module name you want to test here.
330         // Create the match MODULENAME_create_set_values() and MODULENAME_create_run_asserts().
331         $modules = array('forum', 'assign');
332         // Run all tests.
333         foreach ($modules as $modulename) {
334             $this->create_specific_module_test($modulename);
335         }
336     }
338     /**
339      * Test update_module() for multiple modules defined in the $modules array (first declaration of the function).
340      */
341     public function test_update_module() {
342         // Add the module name you want to test here.
343         // Create the match MODULENAME_update_set_values() and MODULENAME_update_run_asserts().
344         $modules = array('forum');
345         // Run all tests.
346         foreach ($modules as $modulename) {
347             $this->update_specific_module_test($modulename);
348         }
349     }
351     /**
352      * Set forum specific test values for calling update_module().
353      *
354      * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
355      */
356     private function forum_update_set_values(&$moduleinfo) {
357         // Completion specific to forum - optional.
358         $moduleinfo->completionposts = 3;
359         $moduleinfo->completiondiscussions = 1;
360         $moduleinfo->completionreplies = 2;
362         // Specific values to the Forum module.
363         $moduleinfo->forcesubscribe = FORUM_INITIALSUBSCRIBE;
364         $moduleinfo->type = 'single';
365         $moduleinfo->trackingtype = FORUM_TRACKING_FORCED;
366         $moduleinfo->maxbytes = 10240;
367         $moduleinfo->maxattachments = 2;
369         // Post threshold for blocking - specific to forum.
370         $moduleinfo->blockperiod = 60*60*24;
371         $moduleinfo->blockafter = 10;
372         $moduleinfo->warnafter = 5;
373     }
375     /**
376      * Execute test asserts on the saved DB data by update_module($forum).
377      *
378      * @param object $moduleinfo - the specific forum values that were used to update a forum.
379      * @param object $dbmodinstance - the DB values of the updated forum.
380      */
381     private function forum_update_run_asserts($moduleinfo, $dbmodinstance) {
382         // Compare values specific to forums.
383         $this->assertEquals($moduleinfo->forcesubscribe, $dbmodinstance->forcesubscribe);
384         $this->assertEquals($moduleinfo->type, $dbmodinstance->type);
385         $this->assertEquals($moduleinfo->assessed, $dbmodinstance->assessed);
386         $this->assertEquals($moduleinfo->completionposts, $dbmodinstance->completionposts);
387         $this->assertEquals($moduleinfo->completiondiscussions, $dbmodinstance->completiondiscussions);
388         $this->assertEquals($moduleinfo->completionreplies, $dbmodinstance->completionreplies);
389         $this->assertEquals($moduleinfo->scale, $dbmodinstance->scale);
390         $this->assertEquals($moduleinfo->assesstimestart, $dbmodinstance->assesstimestart);
391         $this->assertEquals($moduleinfo->assesstimefinish, $dbmodinstance->assesstimefinish);
392         $this->assertEquals($moduleinfo->rsstype, $dbmodinstance->rsstype);
393         $this->assertEquals($moduleinfo->rssarticles, $dbmodinstance->rssarticles);
394         $this->assertEquals($moduleinfo->trackingtype, $dbmodinstance->trackingtype);
395         $this->assertEquals($moduleinfo->maxbytes, $dbmodinstance->maxbytes);
396         $this->assertEquals($moduleinfo->maxattachments, $dbmodinstance->maxattachments);
397         $this->assertEquals($moduleinfo->blockperiod, $dbmodinstance->blockperiod);
398         $this->assertEquals($moduleinfo->blockafter, $dbmodinstance->blockafter);
399         $this->assertEquals($moduleinfo->warnafter, $dbmodinstance->warnafter);
400     }
404     /**
405      * Test a specific type of module.
406      *
407      * @param string $modulename - the module name to test
408      */
409     private function update_specific_module_test($modulename) {
410         global $DB, $CFG;
412         $this->resetAfterTest(true);
414         $this->setAdminUser();
416         // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
417         require_once($CFG->dirroot.'/mod/'. $modulename .'/lib.php');
419         // Enable avaibility.
420         // If not enabled all conditional fields will be ignored.
421         set_config('enableavailability', 1);
423         // Enable course completion.
424         // If not enabled all completion settings will be ignored.
425         set_config('enablecompletion', COMPLETION_ENABLED);
427         // Enable forum RSS feeds.
428         set_config('enablerssfeeds', 1);
429         set_config('forum_enablerssfeeds', 1);
431         $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED),
432            array('createsections'=>true));
434         $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
436         // Create assign module instance for testing gradeitem.
437         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
438         $params['course'] = $course->id;
439         $instance = $generator->create_instance($params);
440         $assigncm = get_coursemodule_from_instance('assign', $instance->id);
442         // Create the test forum to update.
443         $initvalues = new stdClass();
444         $initvalues->introformat = FORMAT_HTML;
445         $initvalues->course = $course->id;
446         $forum = self::getDataGenerator()->create_module('forum', $initvalues);
448         // Retrieve course module.
449         $cm = get_coursemodule_from_instance('forum', $forum->id);
451         // Module test values.
452         $moduleinfo = new stdClass();
454         // Always mandatory generic values to any module.
455         $moduleinfo->coursemodule = $cm->id;
456         $moduleinfo->modulename = $modulename;
457         $moduleinfo->course = $course->id;
458         $moduleinfo->groupingid = $grouping->id;
459         $moduleinfo->groupmembersonly = 0;
460         $moduleinfo->visible = true;
462         // Sometimes optional generic values for some modules.
463         $moduleinfo->name = 'My test module';
464         $moduleinfo->showdescription = 1; // standard boolean
465         require_once($CFG->libdir . '/gradelib.php');
466         $gradecats = grade_get_categories_menu($moduleinfo->course, false);
467         $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
468         $moduleinfo->gradecat = $gradecatid;
469         $moduleinfo->groupmode = VISIBLEGROUPS;
470         $moduleinfo->cmidnumber = 'idnumber_XXX';
472         // Completion common to all module.
473         $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC;
474         $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED;
475         $moduleinfo->completiongradeitemnumber = 1;
476         $moduleinfo->completionexpected = time() + (7 * 24 * 3600);
477         $moduleinfo->completionunlocked = 1;
479         // Conditional activity.
480         $moduleinfo->availablefrom = time();
481         $moduleinfo->availableuntil = time() + (7 * 24 * 3600);
482         $moduleinfo->showavailability = CONDITION_STUDENTVIEW_SHOW;
483         $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself.
484         $moduleinfo->conditiongradegroup = array(array('conditiongradeitemid' => $coursegradeitem->id, 'conditiongrademin' => 10, 'conditiongrademax' => 80));
485         $moduleinfo->conditionfieldgroup = array(array('conditionfield' => 'email', 'conditionfieldoperator' => OP_CONTAINS, 'conditionfieldvalue' => '@'));
486         $moduleinfo->conditioncompletiongroup = array(array('conditionsourcecmid' => $assigncm->id, 'conditionrequiredcompletion' => COMPLETION_COMPLETE)); // "conditionsourcecmid == 0" => none
488         // Grading and Advanced grading.
489         require_once($CFG->dirroot . '/rating/lib.php');
490         $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE;
491         $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number.
492         $moduleinfo->assesstimestart = time();
493         $moduleinfo->assesstimefinish = time() + (7 * 24 * 3600);
495         // RSS.
496         $moduleinfo->rsstype = 2;
497         $moduleinfo->rssarticles = 10;
499         // Optional intro editor (depends of module).
500         $draftid_editor = 0;
501         file_prepare_draft_area($draftid_editor, null, null, null, null);
502         $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
504         // Following is the advanced grading method area called 'submissions' for the 'assign' module.
505         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
506             $moduleinfo->grade = 100;
507         }
508         // Plagiarism form values.
509         // No plagiarism plugin installed by default. Use this space to make your own test.
511         // Values specific to the module.
512         $modulesetvalues = $modulename.'_update_set_values';
513         $this->$modulesetvalues($moduleinfo);
515         // Create the module.
516         $result = update_module($moduleinfo);
518         // Retrieve the module info.
519         $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance));
520         $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance);
521         // Retrieve the grade item.
522         $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course,
523             'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename));
525         // Compare the values common to all module instances.
526         $this->assertEquals($moduleinfo->modulename, $dbcm->modname);
527         $this->assertEquals($moduleinfo->course, $dbcm->course);
528         $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
529         $this->assertEquals($moduleinfo->groupmembersonly, $dbcm->groupmembersonly);
530         $this->assertEquals($moduleinfo->visible, $dbcm->visible);
531         $this->assertEquals($moduleinfo->completion, $dbcm->completion);
532         $this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
533         $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber);
534         $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected);
535         $this->assertEquals($moduleinfo->availablefrom, $dbcm->availablefrom);
536         $this->assertEquals($moduleinfo->availableuntil, $dbcm->availableuntil);
537         $this->assertEquals($moduleinfo->showavailability, $dbcm->showavailability);
538         $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription);
539         $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode);
540         $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber);
541         $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid);
543         // Optional grade testing.
544         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
545             $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade);
546         }
548         // Some optional (but quite common) to some module.
549         $this->assertEquals($moduleinfo->name, $dbmodinstance->name);
550         $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro);
551         $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat);
553         // Common values when conditional activity is enabled.
554         foreach ($moduleinfo->conditionfieldgroup as $fieldgroup) {
555             $isfieldgroupsaved = $DB->count_records('course_modules_avail_fields', array('coursemoduleid' => $dbcm->id,
556                 'userfield' => $fieldgroup['conditionfield'], 'operator' => $fieldgroup['conditionfieldoperator'],
557                 'value' => $fieldgroup['conditionfieldvalue']));
558             $this->assertEquals(1, $isfieldgroupsaved);
559         }
560         foreach ($moduleinfo->conditiongradegroup as $gradegroup) {
561             $isgradegroupsaved = $DB->count_records('course_modules_availability', array('coursemoduleid' => $dbcm->id,
562                 'grademin' => $gradegroup['conditiongrademin'], 'grademax' => $gradegroup['conditiongrademax'],
563                 'gradeitemid' => $gradegroup['conditiongradeitemid']));
564             $this->assertEquals(1, $isgradegroupsaved);
565         }
566         foreach ($moduleinfo->conditioncompletiongroup as $completiongroup) {
567             $iscompletiongroupsaved = $DB->count_records('course_modules_availability', array('coursemoduleid' => $dbcm->id,
568                 'sourcecmid' => $completiongroup['conditionsourcecmid'], 'requiredcompletion' => $completiongroup['conditionrequiredcompletion']));
569             $this->assertEquals(1, $iscompletiongroupsaved);
570         }
572         // Test specific to the module.
573         $modulerunasserts = $modulename.'_update_run_asserts';
574         $this->$modulerunasserts($moduleinfo, $dbmodinstance);
575         return $moduleinfo;
576    }
579     public function test_create_course() {
580         global $DB;
581         $this->resetAfterTest(true);
582         $defaultcategory = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
584         $course = new stdClass();
585         $course->fullname = 'Apu loves Unit TÉ™sts';
586         $course->shortname = 'Spread the lÅ­ve';
587         $course->idnumber = '123';
588         $course->summary = 'Awesome!';
589         $course->summaryformat = FORMAT_PLAIN;
590         $course->format = 'topics';
591         $course->newsitems = 0;
592         $course->numsections = 5;
593         $course->category = $defaultcategory;
594         $original = (array) $course;
596         $created = create_course($course);
597         $context = context_course::instance($created->id);
599         // Compare original and created.
600         $this->assertEquals($original, array_intersect_key((array) $created, $original));
602         // Ensure default section is created.
603         $sectioncreated = $DB->record_exists('course_sections', array('course' => $created->id, 'section' => 0));
604         $this->assertTrue($sectioncreated);
606         // Ensure blocks have been associated to the course.
607         $blockcount = $DB->count_records('block_instances', array('parentcontextid' => $context->id));
608         $this->assertGreaterThan(0, $blockcount);
610         // Ensure that the shortname isn't duplicated.
611         try {
612             $created = create_course($course);
613             $this->fail('Exception expected');
614         } catch (moodle_exception $e) {
615             $this->assertSame(get_string('shortnametaken', 'error', $course->shortname), $e->getMessage());
616         }
618         // Ensure that the idnumber isn't duplicated.
619         $course->shortname .= '1';
620         try {
621             $created = create_course($course);
622             $this->fail('Exception expected');
623         } catch (moodle_exception $e) {
624             $this->assertSame(get_string('courseidnumbertaken', 'error', $course->idnumber), $e->getMessage());
625         }
626     }
628     public function test_create_course_with_generator() {
629         global $DB;
630         $this->resetAfterTest(true);
631         $course = $this->getDataGenerator()->create_course();
633         // Ensure default section is created.
634         $sectioncreated = $DB->record_exists('course_sections', array('course' => $course->id, 'section' => 0));
635         $this->assertTrue($sectioncreated);
636     }
638     public function test_create_course_sections() {
639         global $DB;
640         $this->resetAfterTest(true);
642         $course = $this->getDataGenerator()->create_course(
643                 array('shortname' => 'GrowingCourse',
644                     'fullname' => 'Growing Course',
645                     'numsections' => 5),
646                 array('createsections' => true));
648         // Ensure all 6 (0-5) sections were created and course content cache works properly
649         $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
650         $this->assertEquals(range(0, $course->numsections), $sectionscreated);
652         // this will do nothing, section already exists
653         $this->assertFalse(course_create_sections_if_missing($course, $course->numsections));
655         // this will create new section
656         $this->assertTrue(course_create_sections_if_missing($course, $course->numsections + 1));
658         // Ensure all 7 (0-6) sections were created and modinfo/sectioninfo cache works properly
659         $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
660         $this->assertEquals(range(0, $course->numsections + 1), $sectionscreated);
661     }
663     public function test_update_course() {
664         global $DB;
666         $this->resetAfterTest();
668         $defaultcategory = $DB->get_field_select('course_categories', 'MIN(id)', 'parent = 0');
670         $course = new stdClass();
671         $course->fullname = 'Apu loves Unit TÉ™sts';
672         $course->shortname = 'test1';
673         $course->idnumber = '1';
674         $course->summary = 'Awesome!';
675         $course->summaryformat = FORMAT_PLAIN;
676         $course->format = 'topics';
677         $course->newsitems = 0;
678         $course->numsections = 5;
679         $course->category = $defaultcategory;
681         $created = create_course($course);
682         // Ensure the checks only work on idnumber/shortname that are not already ours.
683         update_course($created);
685         $course->shortname = 'test2';
686         $course->idnumber = '2';
688         $created2 = create_course($course);
690         // Test duplicate idnumber.
691         $created2->idnumber = '1';
692         try {
693             update_course($created2);
694             $this->fail('Expected exception when trying to update a course with duplicate idnumber');
695         } catch (moodle_exception $e) {
696             $this->assertEquals(get_string('courseidnumbertaken', 'error', $created2->idnumber), $e->getMessage());
697         }
699         // Test duplicate shortname.
700         $created2->idnumber = '2';
701         $created2->shortname = 'test1';
702         try {
703             update_course($created2);
704             $this->fail('Expected exception when trying to update a course with a duplicate shortname');
705         } catch (moodle_exception $e) {
706             $this->assertEquals(get_string('shortnametaken', 'error', $created2->shortname), $e->getMessage());
707         }
708     }
710     public function test_course_add_cm_to_section() {
711         global $DB;
712         $this->resetAfterTest(true);
714         // Create course with 1 section.
715         $course = $this->getDataGenerator()->create_course(
716                 array('shortname' => 'GrowingCourse',
717                     'fullname' => 'Growing Course',
718                     'numsections' => 1),
719                 array('createsections' => true));
721         // Trash modinfo.
722         rebuild_course_cache($course->id, true);
724         // Create some cms for testing.
725         $cmids = array();
726         for ($i=0; $i<4; $i++) {
727             $cmids[$i] = $DB->insert_record('course_modules', array('course' => $course->id));
728         }
730         // Add it to section that exists.
731         course_add_cm_to_section($course, $cmids[0], 1);
733         // Check it got added to sequence.
734         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 1));
735         $this->assertEquals($cmids[0], $sequence);
737         // Add a second, this time using courseid variant of parameters.
738         $coursecacherev = $DB->get_field('course', 'cacherev', array('id' => $course->id));
739         course_add_cm_to_section($course->id, $cmids[1], 1);
740         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 1));
741         $this->assertEquals($cmids[0] . ',' . $cmids[1], $sequence);
743         // Check that modinfo cache was reset but not rebuilt (important for performance if calling repeatedly).
744         $this->assertGreaterThan($coursecacherev, $DB->get_field('course', 'cacherev', array('id' => $course->id)));
745         $this->assertEmpty(cache::make('core', 'coursemodinfo')->get($course->id));
747         // Add one to section that doesn't exist (this might rebuild modinfo).
748         course_add_cm_to_section($course, $cmids[2], 2);
749         $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id)));
750         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 2));
751         $this->assertEquals($cmids[2], $sequence);
753         // Add using the 'before' option.
754         course_add_cm_to_section($course, $cmids[3], 2, $cmids[2]);
755         $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id)));
756         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 2));
757         $this->assertEquals($cmids[3] . ',' . $cmids[2], $sequence);
758     }
760     public function test_reorder_sections() {
761         global $DB;
762         $this->resetAfterTest(true);
764         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
765         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
766         $oldsections = array();
767         $sections = array();
768         foreach ($DB->get_records('course_sections', array('course'=>$course->id), 'id') as $section) {
769             $oldsections[$section->section] = $section->id;
770             $sections[$section->id] = $section->section;
771         }
772         ksort($oldsections);
774         $neworder = reorder_sections($sections, 2, 4);
775         $neworder = array_keys($neworder);
776         $this->assertEquals($oldsections[0], $neworder[0]);
777         $this->assertEquals($oldsections[1], $neworder[1]);
778         $this->assertEquals($oldsections[2], $neworder[4]);
779         $this->assertEquals($oldsections[3], $neworder[2]);
780         $this->assertEquals($oldsections[4], $neworder[3]);
781         $this->assertEquals($oldsections[5], $neworder[5]);
782         $this->assertEquals($oldsections[6], $neworder[6]);
784         $neworder = reorder_sections($sections, 4, 2);
785         $neworder = array_keys($neworder);
786         $this->assertEquals($oldsections[0], $neworder[0]);
787         $this->assertEquals($oldsections[1], $neworder[1]);
788         $this->assertEquals($oldsections[2], $neworder[3]);
789         $this->assertEquals($oldsections[3], $neworder[4]);
790         $this->assertEquals($oldsections[4], $neworder[2]);
791         $this->assertEquals($oldsections[5], $neworder[5]);
792         $this->assertEquals($oldsections[6], $neworder[6]);
794         $neworder = reorder_sections(1, 2, 4);
795         $this->assertFalse($neworder);
796     }
798     public function test_move_section_down() {
799         global $DB;
800         $this->resetAfterTest(true);
802         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
803         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
804         $oldsections = array();
805         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
806             $oldsections[$section->section] = $section->id;
807         }
808         ksort($oldsections);
810         // Test move section down..
811         move_section_to($course, 2, 4);
812         $sections = array();
813         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
814             $sections[$section->section] = $section->id;
815         }
816         ksort($sections);
818         $this->assertEquals($oldsections[0], $sections[0]);
819         $this->assertEquals($oldsections[1], $sections[1]);
820         $this->assertEquals($oldsections[2], $sections[4]);
821         $this->assertEquals($oldsections[3], $sections[2]);
822         $this->assertEquals($oldsections[4], $sections[3]);
823         $this->assertEquals($oldsections[5], $sections[5]);
824         $this->assertEquals($oldsections[6], $sections[6]);
825     }
827     public function test_move_section_up() {
828         global $DB;
829         $this->resetAfterTest(true);
831         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
832         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
833         $oldsections = array();
834         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
835             $oldsections[$section->section] = $section->id;
836         }
837         ksort($oldsections);
839         // Test move section up..
840         move_section_to($course, 6, 4);
841         $sections = array();
842         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
843             $sections[$section->section] = $section->id;
844         }
845         ksort($sections);
847         $this->assertEquals($oldsections[0], $sections[0]);
848         $this->assertEquals($oldsections[1], $sections[1]);
849         $this->assertEquals($oldsections[2], $sections[2]);
850         $this->assertEquals($oldsections[3], $sections[3]);
851         $this->assertEquals($oldsections[4], $sections[5]);
852         $this->assertEquals($oldsections[5], $sections[6]);
853         $this->assertEquals($oldsections[6], $sections[4]);
854     }
856     public function test_move_section_marker() {
857         global $DB;
858         $this->resetAfterTest(true);
860         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
861         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
863         // Set course marker to the section we are going to move..
864         course_set_marker($course->id, 2);
865         // Verify that the course marker is set correctly.
866         $course = $DB->get_record('course', array('id' => $course->id));
867         $this->assertEquals(2, $course->marker);
869         // Test move the marked section down..
870         move_section_to($course, 2, 4);
872         // Verify that the coruse marker has been moved along with the section..
873         $course = $DB->get_record('course', array('id' => $course->id));
874         $this->assertEquals(4, $course->marker);
876         // Test move the marked section up..
877         move_section_to($course, 4, 3);
879         // Verify that the course marker has been moved along with the section..
880         $course = $DB->get_record('course', array('id' => $course->id));
881         $this->assertEquals(3, $course->marker);
883         // Test moving a non-marked section above the marked section..
884         move_section_to($course, 4, 2);
886         // Verify that the course marker has been moved down to accomodate..
887         $course = $DB->get_record('course', array('id' => $course->id));
888         $this->assertEquals(4, $course->marker);
890         // Test moving a non-marked section below the marked section..
891         move_section_to($course, 3, 6);
893         // Verify that the course marker has been up to accomodate..
894         $course = $DB->get_record('course', array('id' => $course->id));
895         $this->assertEquals(3, $course->marker);
896     }
898     public function test_get_course_display_name_for_list() {
899         global $CFG;
900         $this->resetAfterTest(true);
902         $course = $this->getDataGenerator()->create_course(array('shortname' => 'FROG101', 'fullname' => 'Introduction to pond life'));
904         $CFG->courselistshortnames = 0;
905         $this->assertEquals('Introduction to pond life', get_course_display_name_for_list($course));
907         $CFG->courselistshortnames = 1;
908         $this->assertEquals('FROG101 Introduction to pond life', get_course_display_name_for_list($course));
909     }
911     public function test_move_module_in_course() {
912         global $DB;
914         $this->resetAfterTest(true);
915         // Setup fixture
916         $course = $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections' => true));
917         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
919         $cms = get_fast_modinfo($course)->get_cms();
920         $cm = reset($cms);
922         $newsection = get_fast_modinfo($course)->get_section_info(3);
923         $oldsectionid = $cm->section;
925         // Perform the move
926         moveto_module($cm, $newsection);
928         $cms = get_fast_modinfo($course)->get_cms();
929         $cm = reset($cms);
931         // Check that the cached modinfo contains the correct section info
932         $modinfo = get_fast_modinfo($course);
933         $this->assertTrue(empty($modinfo->sections[0]));
934         $this->assertFalse(empty($modinfo->sections[3]));
936         // Check that the old section's sequence no longer contains this ID
937         $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
938         $oldsequences = explode(',', $newsection->sequence);
939         $this->assertFalse(in_array($cm->id, $oldsequences));
941         // Check that the new section's sequence now contains this ID
942         $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
943         $newsequences = explode(',', $newsection->sequence);
944         $this->assertTrue(in_array($cm->id, $newsequences));
946         // Check that the section number has been changed in the cm
947         $this->assertEquals($newsection->id, $cm->section);
950         // Perform a second move as some issues were only seen on the second move
951         $newsection = get_fast_modinfo($course)->get_section_info(2);
952         $oldsectionid = $cm->section;
953         moveto_module($cm, $newsection);
955         $cms = get_fast_modinfo($course)->get_cms();
956         $cm = reset($cms);
958         // Check that the cached modinfo contains the correct section info
959         $modinfo = get_fast_modinfo($course);
960         $this->assertTrue(empty($modinfo->sections[0]));
961         $this->assertFalse(empty($modinfo->sections[2]));
963         // Check that the old section's sequence no longer contains this ID
964         $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
965         $oldsequences = explode(',', $newsection->sequence);
966         $this->assertFalse(in_array($cm->id, $oldsequences));
968         // Check that the new section's sequence now contains this ID
969         $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
970         $newsequences = explode(',', $newsection->sequence);
971         $this->assertTrue(in_array($cm->id, $newsequences));
972     }
974     public function test_module_visibility() {
975         $this->setAdminUser();
976         $this->resetAfterTest(true);
978         // Create course and modules.
979         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
980         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
981         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(), 'course' => $course->id));
982         $modules = compact('forum', 'assign');
984         // Hiding the modules.
985         foreach ($modules as $mod) {
986             set_coursemodule_visible($mod->cmid, 0);
987             $this->check_module_visibility($mod, 0, 0);
988         }
990         // Showing the modules.
991         foreach ($modules as $mod) {
992             set_coursemodule_visible($mod->cmid, 1);
993             $this->check_module_visibility($mod, 1, 1);
994         }
995     }
997     public function test_section_visibility() {
998         $this->setAdminUser();
999         $this->resetAfterTest(true);
1001         // Create course.
1002         $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
1004         // Testing an empty section.
1005         $sectionnumber = 1;
1006         set_section_visible($course->id, $sectionnumber, 0);
1007         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1008         $this->assertEquals($section_info->visible, 0);
1009         set_section_visible($course->id, $sectionnumber, 1);
1010         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1011         $this->assertEquals($section_info->visible, 1);
1013         // Testing a section with visible modules.
1014         $sectionnumber = 2;
1015         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1016                 array('section' => $sectionnumber));
1017         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1018                 'course' => $course->id), array('section' => $sectionnumber));
1019         $modules = compact('forum', 'assign');
1020         set_section_visible($course->id, $sectionnumber, 0);
1021         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1022         $this->assertEquals($section_info->visible, 0);
1023         foreach ($modules as $mod) {
1024             $this->check_module_visibility($mod, 0, 1);
1025         }
1026         set_section_visible($course->id, $sectionnumber, 1);
1027         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1028         $this->assertEquals($section_info->visible, 1);
1029         foreach ($modules as $mod) {
1030             $this->check_module_visibility($mod, 1, 1);
1031         }
1033         // Testing a section with hidden modules, which should stay hidden.
1034         $sectionnumber = 3;
1035         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1036                 array('section' => $sectionnumber));
1037         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1038                 'course' => $course->id), array('section' => $sectionnumber));
1039         $modules = compact('forum', 'assign');
1040         foreach ($modules as $mod) {
1041             set_coursemodule_visible($mod->cmid, 0);
1042             $this->check_module_visibility($mod, 0, 0);
1043         }
1044         set_section_visible($course->id, $sectionnumber, 0);
1045         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1046         $this->assertEquals($section_info->visible, 0);
1047         foreach ($modules as $mod) {
1048             $this->check_module_visibility($mod, 0, 0);
1049         }
1050         set_section_visible($course->id, $sectionnumber, 1);
1051         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1052         $this->assertEquals($section_info->visible, 1);
1053         foreach ($modules as $mod) {
1054             $this->check_module_visibility($mod, 0, 0);
1055         }
1056     }
1058     /**
1059      * Helper function to assert that a module has correctly been made visible, or hidden.
1060      *
1061      * @param stdClass $mod module information
1062      * @param int $visibility the current state of the module
1063      * @param int $visibleold the current state of the visibleold property
1064      * @return void
1065      */
1066     public function check_module_visibility($mod, $visibility, $visibleold) {
1067         global $DB;
1068         $cm = get_fast_modinfo($mod->course)->get_cm($mod->cmid);
1069         $this->assertEquals($visibility, $cm->visible);
1070         $this->assertEquals($visibleold, $cm->visibleold);
1072         // Check the module grade items.
1073         $grade_items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $cm->modname,
1074                 'iteminstance' => $cm->instance, 'courseid' => $cm->course));
1075         if ($grade_items) {
1076             foreach ($grade_items as $grade_item) {
1077                 if ($visibility) {
1078                     $this->assertFalse($grade_item->is_hidden(), "$cm->modname grade_item not visible");
1079                 } else {
1080                     $this->assertTrue($grade_item->is_hidden(), "$cm->modname grade_item not hidden");
1081                 }
1082             }
1083         }
1085         // Check the events visibility.
1086         if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $cm->modname))) {
1087             foreach ($events as $event) {
1088                 $calevent = new calendar_event($event);
1089                 $this->assertEquals($visibility, $calevent->visible, "$cm->modname calendar_event visibility");
1090             }
1091         }
1092     }
1094     public function test_course_page_type_list() {
1095         global $DB;
1096         $this->resetAfterTest(true);
1098         // Create a category.
1099         $category = new stdClass();
1100         $category->name = 'Test Category';
1102         $testcategory = $this->getDataGenerator()->create_category($category);
1104         // Create a course.
1105         $course = new stdClass();
1106         $course->fullname = 'Apu loves Unit TÉ™sts';
1107         $course->shortname = 'Spread the lÅ­ve';
1108         $course->idnumber = '123';
1109         $course->summary = 'Awesome!';
1110         $course->summaryformat = FORMAT_PLAIN;
1111         $course->format = 'topics';
1112         $course->newsitems = 0;
1113         $course->numsections = 5;
1114         $course->category = $testcategory->id;
1116         $testcourse = $this->getDataGenerator()->create_course($course);
1118         // Create contexts.
1119         $coursecontext = context_course::instance($testcourse->id);
1120         $parentcontext = $coursecontext->get_parent_context(); // Not actually used.
1121         $pagetype = 'page-course-x'; // Not used either.
1122         $pagetypelist = course_page_type_list($pagetype, $parentcontext, $coursecontext);
1124         // Page type lists for normal courses.
1125         $testpagetypelist1 = array();
1126         $testpagetypelist1['*'] = 'Any page';
1127         $testpagetypelist1['course-*'] = 'Any course page';
1128         $testpagetypelist1['course-view-*'] = 'Any type of course main page';
1130         $this->assertEquals($testpagetypelist1, $pagetypelist);
1132         // Get the context for the front page course.
1133         $sitecoursecontext = context_course::instance(SITEID);
1134         $pagetypelist = course_page_type_list($pagetype, $parentcontext, $sitecoursecontext);
1136         // Page type list for the front page course.
1137         $testpagetypelist2 = array('*' => 'Any page');
1138         $this->assertEquals($testpagetypelist2, $pagetypelist);
1140         // Make sure that providing no current context to the function doesn't result in an error.
1141         // Calls made from generate_page_type_patterns() may provide null values.
1142         $pagetypelist = course_page_type_list($pagetype, null, null);
1143         $this->assertEquals($pagetypelist, $testpagetypelist1);
1144     }
1146     public function test_compare_activities_by_time_desc() {
1148         // Let's create some test data.
1149         $activitiesivities = array();
1150         $x = new stdClass();
1151         $x->timestamp = null;
1152         $activities[] = $x;
1154         $x = new stdClass();
1155         $x->timestamp = 1;
1156         $activities[] = $x;
1158         $x = new stdClass();
1159         $x->timestamp = 3;
1160         $activities[] = $x;
1162         $x = new stdClass();
1163         $x->timestamp = 0;
1164         $activities[] = $x;
1166         $x = new stdClass();
1167         $x->timestamp = 5;
1168         $activities[] = $x;
1170         $x = new stdClass();
1171         $activities[] = $x;
1173         $x = new stdClass();
1174         $x->timestamp = 5;
1175         $activities[] = $x;
1177         // Do the sorting.
1178         usort($activities, 'compare_activities_by_time_desc');
1180         // Let's check the result.
1181         $last = 10;
1182         foreach($activities as $activity) {
1183             if (empty($activity->timestamp)) {
1184                 $activity->timestamp = 0;
1185             }
1186             $this->assertLessThanOrEqual($last, $activity->timestamp);
1187         }
1188     }
1190     public function test_compare_activities_by_time_asc() {
1192         // Let's create some test data.
1193         $activities = array();
1194         $x = new stdClass();
1195         $x->timestamp = null;
1196         $activities[] = $x;
1198         $x = new stdClass();
1199         $x->timestamp = 1;
1200         $activities[] = $x;
1202         $x = new stdClass();
1203         $x->timestamp = 3;
1204         $activities[] = $x;
1206         $x = new stdClass();
1207         $x->timestamp = 0;
1208         $activities[] = $x;
1210         $x = new stdClass();
1211         $x->timestamp = 5;
1212         $activities[] = $x;
1214         $x = new stdClass();
1215         $activities[] = $x;
1217         $x = new stdClass();
1218         $x->timestamp = 5;
1219         $activities[] = $x;
1221         // Do the sorting.
1222         usort($activities, 'compare_activities_by_time_asc');
1224         // Let's check the result.
1225         $last = 0;
1226         foreach($activities as $activity) {
1227             if (empty($activity->timestamp)) {
1228                 $activity->timestamp = 0;
1229             }
1230             $this->assertGreaterThanOrEqual($last, $activity->timestamp);
1231         }
1232     }
1234     /**
1235      * Tests moving a module between hidden/visible sections and
1236      * verifies that the course/module visiblity seettings are
1237      * retained.
1238      */
1239     public function test_moveto_module_between_hidden_sections() {
1240         global $DB;
1242         $this->resetAfterTest(true);
1244         $course = $this->getDataGenerator()->create_course(array('numsections' => 4), array('createsections' => true));
1245         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1246         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
1247         $quiz= $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
1249         // Set the page as hidden
1250         set_coursemodule_visible($page->cmid, 0);
1252         // Set sections 3 as hidden.
1253         set_section_visible($course->id, 3, 0);
1255         $modinfo = get_fast_modinfo($course);
1257         $hiddensection = $modinfo->get_section_info(3);
1258         // New section is definitely not visible:
1259         $this->assertEquals($hiddensection->visible, 0);
1261         $forumcm = $modinfo->cms[$forum->cmid];
1262         $pagecm = $modinfo->cms[$page->cmid];
1264         // Move the forum and the page to a hidden section, make sure moveto_module returns 0 as new visibility state.
1265         $this->assertEquals(0, moveto_module($forumcm, $hiddensection));
1266         $this->assertEquals(0, moveto_module($pagecm, $hiddensection));
1268         $modinfo = get_fast_modinfo($course);
1270         // Verify that forum and page have been moved to the hidden section and quiz has not.
1271         $this->assertContains($forum->cmid, $modinfo->sections[3]);
1272         $this->assertContains($page->cmid, $modinfo->sections[3]);
1273         $this->assertNotContains($quiz->cmid, $modinfo->sections[3]);
1275         // Verify that forum has been made invisible.
1276         $forumcm = $modinfo->cms[$forum->cmid];
1277         $this->assertEquals($forumcm->visible, 0);
1278         // Verify that old state has been retained.
1279         $this->assertEquals($forumcm->visibleold, 1);
1281         // Verify that page has stayed invisible.
1282         $pagecm = $modinfo->cms[$page->cmid];
1283         $this->assertEquals($pagecm->visible, 0);
1284         // Verify that old state has been retained.
1285         $this->assertEquals($pagecm->visibleold, 0);
1287         // Verify that quiz has been unaffected.
1288         $quizcm = $modinfo->cms[$quiz->cmid];
1289         $this->assertEquals($quizcm->visible, 1);
1291         // Move forum and page back to visible section.
1292         // Make sure the visibility is restored to the original value (visible for forum and hidden for page).
1293         $visiblesection = $modinfo->get_section_info(2);
1294         $this->assertEquals(1, moveto_module($forumcm, $visiblesection));
1295         $this->assertEquals(0, moveto_module($pagecm, $visiblesection));
1297         $modinfo = get_fast_modinfo($course);
1299         // Double check that forum has been made visible.
1300         $forumcm = $modinfo->cms[$forum->cmid];
1301         $this->assertEquals($forumcm->visible, 1);
1303         // Double check that page has stayed invisible.
1304         $pagecm = $modinfo->cms[$page->cmid];
1305         $this->assertEquals($pagecm->visible, 0);
1307         // Move the page in the same section (this is what mod duplicate does).
1308         // Visibility of page remains 0.
1309         $this->assertEquals(0, moveto_module($pagecm, $visiblesection, $forumcm));
1311         // Double check that the the page is still hidden.
1312         $modinfo = get_fast_modinfo($course);
1313         $pagecm = $modinfo->cms[$page->cmid];
1314         $this->assertEquals($pagecm->visible, 0);
1315     }
1317     /**
1318      * Tests moving a module around in the same section. moveto_module()
1319      * is called this way in modduplicate.
1320      */
1321     public function test_moveto_module_in_same_section() {
1322         global $DB;
1324         $this->resetAfterTest(true);
1326         $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
1327         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
1328         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1330         // Simulate inconsistent visible/visibleold values (MDL-38713).
1331         $cm = $DB->get_record('course_modules', array('id' => $page->cmid), '*', MUST_EXIST);
1332         $cm->visible = 0;
1333         $cm->visibleold = 1;
1334         $DB->update_record('course_modules', $cm);
1336         $modinfo = get_fast_modinfo($course);
1337         $forumcm = $modinfo->cms[$forum->cmid];
1338         $pagecm = $modinfo->cms[$page->cmid];
1340         // Verify that page is hidden.
1341         $this->assertEquals($pagecm->visible, 0);
1343         // Verify section 0 is where all mods added.
1344         $section = $modinfo->get_section_info(0);
1345         $this->assertEquals($section->id, $forumcm->section);
1346         $this->assertEquals($section->id, $pagecm->section);
1349         // Move the page inside the hidden section. Make sure it is hidden.
1350         $this->assertEquals(0, moveto_module($pagecm, $section, $forumcm));
1352         // Double check that the the page is still hidden.
1353         $modinfo = get_fast_modinfo($course);
1354         $pagecm = $modinfo->cms[$page->cmid];
1355         $this->assertEquals($pagecm->visible, 0);
1356     }
1358     public function test_course_delete_module() {
1359         global $DB;
1360         $this->resetAfterTest(true);
1361         $this->setAdminUser();
1363         // Create course and modules.
1364         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
1366         // Generate an assignment with due date (will generate a course event).
1367         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(), 'course' => $course->id));
1369         $cm = get_coursemodule_from_instance('assign', $assign->id);
1371         // Verify context exists.
1372         $this->assertInstanceOf('context_module', context_module::instance($cm->id, IGNORE_MISSING));
1374         // Verify event assignment event has been generated.
1375         $eventcount = $DB->count_records('event', array('instance' => $assign->id, 'modulename' => 'assign'));
1376         $this->assertEquals(1, $eventcount);
1378         // Run delete..
1379         course_delete_module($cm->id);
1381         // Verify the context has been removed.
1382         $this->assertFalse(context_module::instance($cm->id, IGNORE_MISSING));
1384         // Verify the course_module record has been deleted.
1385         $cmcount = $DB->count_records('course_modules', array('id' => $cm->id));
1386         $this->assertEmpty($cmcount);
1388         // Verify event assignment events have been removed.
1389         $eventcount = $DB->count_records('event', array('instance' => $assign->id, 'modulename' => 'assign'));
1390         $this->assertEmpty($eventcount);
1391     }
1393     /**
1394      * Test that triggering a course_created event works as expected.
1395      */
1396     public function test_course_created_event() {
1397         global $DB;
1399         $this->resetAfterTest();
1401         // Catch the events.
1402         $sink = $this->redirectEvents();
1404         // Create the course with an id number which is used later when generating a course via the imsenterprise plugin.
1405         $data = new stdClass();
1406         $data->idnumber = 'idnumber';
1407         $course = $this->getDataGenerator()->create_course($data);
1408         // Get course from DB for comparison.
1409         $course = $DB->get_record('course', array('id' => $course->id));
1411         // Capture the event.
1412         $events = $sink->get_events();
1413         $sink->close();
1415         // Validate the event.
1416         $event = $events[0];
1417         $this->assertInstanceOf('\core\event\course_created', $event);
1418         $this->assertEquals('course', $event->objecttable);
1419         $this->assertEquals($course->id, $event->objectid);
1420         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1421         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1422         $this->assertEquals('course_created', $event->get_legacy_eventname());
1423         $this->assertEventLegacyData($course, $event);
1424         $expectedlog = array(SITEID, 'course', 'new', 'view.php?id=' . $course->id, $course->fullname . ' (ID ' . $course->id . ')');
1425         $this->assertEventLegacyLogData($expectedlog, $event);
1427         // Now we want to trigger creating a course via the imsenterprise.
1428         // Delete the course we created earlier, as we want the imsenterprise plugin to create this.
1429         // We do not want print out any of the text this function generates while doing this, which is why
1430         // we are using ob_start() and ob_end_clean().
1431         ob_start();
1432         delete_course($course);
1433         ob_end_clean();
1435         // Create the XML file we want to use.
1436         $imstestcase = new enrol_imsenterprise_testcase();
1437         $imstestcase->imsplugin = enrol_get_plugin('imsenterprise');
1438         $imstestcase->set_test_config();
1439         $imstestcase->set_xml_file(false, array($course));
1441         // Capture the event.
1442         $sink = $this->redirectEvents();
1443         $imstestcase->imsplugin->cron();
1444         $events = $sink->get_events();
1445         $sink->close();
1446         $event = $events[0];
1448         // Validate the event triggered is \core\event\course_created. There is no need to validate the other values
1449         // as they have already been validated in the previous steps. Here we only want to make sure that when the
1450         // imsenterprise plugin creates a course an event is triggered.
1451         $this->assertInstanceOf('\core\event\course_created', $event);
1452         $this->assertEventContextNotUsed($event);
1453     }
1455     /**
1456      * Test that triggering a course_updated event works as expected.
1457      */
1458     public function test_course_updated_event() {
1459         global $DB;
1461         $this->resetAfterTest();
1463         // Create a course.
1464         $course = $this->getDataGenerator()->create_course();
1466         // Create a category we are going to move this course to.
1467         $category = $this->getDataGenerator()->create_category();
1469         // Create a hidden category we are going to move this course to.
1470         $categoryhidden = $this->getDataGenerator()->create_category(array('visible' => 0));
1472         // Update course and catch course_updated event.
1473         $sink = $this->redirectEvents();
1474         update_course($course);
1475         $events = $sink->get_events();
1476         $sink->close();
1478         // Get updated course information from the DB.
1479         $updatedcourse = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1480         // Validate event.
1481         $event = array_shift($events);
1482         $this->assertInstanceOf('\core\event\course_updated', $event);
1483         $this->assertEquals('course', $event->objecttable);
1484         $this->assertEquals($updatedcourse->id, $event->objectid);
1485         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1486         $this->assertEquals($updatedcourse, $event->get_record_snapshot('course', $event->objectid));
1487         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1488         $this->assertEventLegacyData($updatedcourse, $event);
1489         $expectedlog = array($updatedcourse->id, 'course', 'update', 'edit.php?id=' . $course->id, $course->id);
1490         $this->assertEventLegacyLogData($expectedlog, $event);
1492         // Move course and catch course_updated event.
1493         $sink = $this->redirectEvents();
1494         move_courses(array($course->id), $category->id);
1495         $events = $sink->get_events();
1496         $sink->close();
1498         // Return the moved course information from the DB.
1499         $movedcourse = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1500         // Validate event.
1501         $event = array_shift($events);
1502         $this->assertInstanceOf('\core\event\course_updated', $event);
1503         $this->assertEquals('course', $event->objecttable);
1504         $this->assertEquals($movedcourse->id, $event->objectid);
1505         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1506         $this->assertEquals($movedcourse, $event->get_record_snapshot('course', $movedcourse->id));
1507         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1508         $this->assertEventLegacyData($movedcourse, $event);
1509         $expectedlog = array($movedcourse->id, 'course', 'move', 'edit.php?id=' . $movedcourse->id, $movedcourse->id);
1510         $this->assertEventLegacyLogData($expectedlog, $event);
1512         // Move course to hidden category and catch course_updated event.
1513         $sink = $this->redirectEvents();
1514         move_courses(array($course->id), $categoryhidden->id);
1515         $events = $sink->get_events();
1516         $sink->close();
1518         // Return the moved course information from the DB.
1519         $movedcoursehidden = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1520         // Validate event.
1521         $event = array_shift($events);
1522         $this->assertInstanceOf('\core\event\course_updated', $event);
1523         $this->assertEquals('course', $event->objecttable);
1524         $this->assertEquals($movedcoursehidden->id, $event->objectid);
1525         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1526         $this->assertEquals($movedcoursehidden, $event->get_record_snapshot('course', $movedcoursehidden->id));
1527         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1528         $this->assertEventLegacyData($movedcoursehidden, $event);
1529         $expectedlog = array($movedcoursehidden->id, 'course', 'move', 'edit.php?id=' . $movedcoursehidden->id, $movedcoursehidden->id);
1530         $this->assertEventLegacyLogData($expectedlog, $event);
1531         $this->assertEventContextNotUsed($event);
1532     }
1534     /**
1535      * Test that triggering a course_deleted event works as expected.
1536      */
1537     public function test_course_deleted_event() {
1538         $this->resetAfterTest();
1540         // Create the course.
1541         $course = $this->getDataGenerator()->create_course();
1543         // Save the course context before we delete the course.
1544         $coursecontext = context_course::instance($course->id);
1546         // Catch the update event.
1547         $sink = $this->redirectEvents();
1549         // Call delete_course() which will trigger the course_deleted event and the course_content_deleted
1550         // event. This function prints out data to the screen, which we do not want during a PHPUnit test,
1551         // so use ob_start and ob_end_clean to prevent this.
1552         ob_start();
1553         delete_course($course);
1554         ob_end_clean();
1556         // Capture the event.
1557         $events = $sink->get_events();
1558         $sink->close();
1560         // Validate the event.
1561         $event = $events[1];
1562         $this->assertInstanceOf('\core\event\course_deleted', $event);
1563         $this->assertEquals('course', $event->objecttable);
1564         $this->assertEquals($course->id, $event->objectid);
1565         $this->assertEquals($coursecontext->id, $event->contextid);
1566         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1567         $this->assertEquals('course_deleted', $event->get_legacy_eventname());
1568         $eventdata = $event->get_data();
1569         $this->assertSame($course->idnumber, $eventdata['other']['idnumber']);
1570         $this->assertSame($course->fullname, $eventdata['other']['fullname']);
1571         $this->assertSame($course->shortname, $eventdata['other']['shortname']);
1572         // The legacy data also passed the context in the course object.
1573         $course->context = $coursecontext;
1574         $this->assertEventLegacyData($course, $event);
1575         $expectedlog = array(SITEID, 'course', 'delete', 'view.php?id=' . $course->id, $course->fullname . '(ID ' . $course->id . ')');
1576         $this->assertEventLegacyLogData($expectedlog, $event);
1577         $this->assertEventContextNotUsed($event);
1578     }
1580     /**
1581      * Test that triggering a course_content_deleted event works as expected.
1582      */
1583     public function test_course_content_deleted_event() {
1584         global $DB;
1586         $this->resetAfterTest();
1588         // Create the course.
1589         $course = $this->getDataGenerator()->create_course();
1591         // Get the course from the DB. The data generator adds some extra properties, such as
1592         // numsections, to the course object which will fail the assertions later on.
1593         $course = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1595         // Save the course context before we delete the course.
1596         $coursecontext = context_course::instance($course->id);
1598         // Catch the update event.
1599         $sink = $this->redirectEvents();
1601         remove_course_contents($course->id, false);
1603         // Capture the event.
1604         $events = $sink->get_events();
1605         $sink->close();
1607         // Validate the event.
1608         $event = $events[0];
1609         $this->assertInstanceOf('\core\event\course_content_deleted', $event);
1610         $this->assertEquals('course', $event->objecttable);
1611         $this->assertEquals($course->id, $event->objectid);
1612         $this->assertEquals($coursecontext->id, $event->contextid);
1613         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1614         $this->assertEquals('course_content_removed', $event->get_legacy_eventname());
1615         // The legacy data also passed the context and options in the course object.
1616         $course->context = $coursecontext;
1617         $course->options = array();
1618         $this->assertEventLegacyData($course, $event);
1619         $this->assertEventContextNotUsed($event);
1620     }
1622     /**
1623      * Test that triggering a course_category_deleted event works as expected.
1624      */
1625     public function test_course_category_deleted_event() {
1626         $this->resetAfterTest();
1628         // Create a category.
1629         $category = $this->getDataGenerator()->create_category();
1631         // Save the context before it is deleted.
1632         $categorycontext = context_coursecat::instance($category->id);
1634         // Catch the update event.
1635         $sink = $this->redirectEvents();
1637         // Delete the category.
1638         $category->delete_full();
1640         // Capture the event.
1641         $events = $sink->get_events();
1642         $sink->close();
1644         // Validate the event.
1645         $event = $events[0];
1646         $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1647         $this->assertEquals('course_categories', $event->objecttable);
1648         $this->assertEquals($category->id, $event->objectid);
1649         $this->assertEquals($categorycontext->id, $event->contextid);
1650         $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1651         $this->assertEventLegacyData($category, $event);
1652         $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category->name . '(ID ' . $category->id . ')');
1653         $this->assertEventLegacyLogData($expectedlog, $event);
1655         // Create two categories.
1656         $category = $this->getDataGenerator()->create_category();
1657         $category2 = $this->getDataGenerator()->create_category();
1659         // Save the context before it is moved and then deleted.
1660         $category2context = context_coursecat::instance($category2->id);
1662         // Catch the update event.
1663         $sink = $this->redirectEvents();
1665         // Move the category.
1666         $category2->delete_move($category->id);
1668         // Capture the event.
1669         $events = $sink->get_events();
1670         $sink->close();
1672         // Validate the event.
1673         $event = $events[0];
1674         $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1675         $this->assertEquals('course_categories', $event->objecttable);
1676         $this->assertEquals($category2->id, $event->objectid);
1677         $this->assertEquals($category2context->id, $event->contextid);
1678         $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1679         $this->assertEventLegacyData($category2, $event);
1680         $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category2->name . '(ID ' . $category2->id . ')');
1681         $this->assertEventLegacyLogData($expectedlog, $event);
1682         $this->assertEventContextNotUsed($event);
1683     }
1685     /**
1686      * Test that triggering a course_restored event works as expected.
1687      */
1688     public function test_course_restored_event() {
1689         global $CFG;
1691         // Get the necessary files to perform backup and restore.
1692         require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
1693         require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
1695         $this->resetAfterTest();
1697         // Set to admin user.
1698         $this->setAdminUser();
1700         // The user id is going to be 2 since we are the admin user.
1701         $userid = 2;
1703         // Create a course.
1704         $course = $this->getDataGenerator()->create_course();
1706         // Create backup file and save it to the backup location.
1707         $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
1708             backup::INTERACTIVE_NO, backup::MODE_GENERAL, $userid);
1709         $bc->execute_plan();
1710         $results = $bc->get_results();
1711         $file = $results['backup_destination'];
1712         $fp = get_file_packer();
1713         $filepath = $CFG->dataroot . '/temp/backup/test-restore-course-event';
1714         $file->extract_to_pathname($fp, $filepath);
1715         $bc->destroy();
1716         unset($bc);
1718         // Now we want to catch the restore course event.
1719         $sink = $this->redirectEvents();
1721         // Now restore the course to trigger the event.
1722         $rc = new restore_controller('test-restore-course-event', $course->id, backup::INTERACTIVE_NO,
1723             backup::MODE_GENERAL, $userid, backup::TARGET_NEW_COURSE);
1724         $rc->execute_precheck();
1725         $rc->execute_plan();
1727         // Capture the event.
1728         $events = $sink->get_events();
1729         $sink->close();
1731         // Validate the event.
1732         $event = $events[0];
1733         $this->assertInstanceOf('\core\event\course_restored', $event);
1734         $this->assertEquals('course', $event->objecttable);
1735         $this->assertEquals($rc->get_courseid(), $event->objectid);
1736         $this->assertEquals(context_course::instance($rc->get_courseid())->id, $event->contextid);
1737         $this->assertEquals('course_restored', $event->get_legacy_eventname());
1738         $legacydata = (object) array(
1739             'courseid' => $rc->get_courseid(),
1740             'userid' => $rc->get_userid(),
1741             'type' => $rc->get_type(),
1742             'target' => $rc->get_target(),
1743             'mode' => $rc->get_mode(),
1744             'operation' => $rc->get_operation(),
1745             'samesite' => $rc->is_samesite()
1746         );
1747         $this->assertEventLegacyData($legacydata, $event);
1748         $this->assertEventContextNotUsed($event);
1750         // Destroy the resource controller since we are done using it.
1751         $rc->destroy();
1752         unset($rc);
1753     }
1755     /**
1756      * Test that triggering a course_section_updated event works as expected.
1757      */
1758     public function test_course_section_updated_event() {
1759         global $DB;
1761         $this->resetAfterTest();
1763         // Create the course with sections.
1764         $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true));
1765         $sections = $DB->get_records('course_sections', array('course' => $course->id));
1767         $coursecontext = context_course::instance($course->id);
1769         $section = array_pop($sections);
1770         $section->name = 'Test section';
1771         $section->summary = 'Test section summary';
1772         $DB->update_record('course_sections', $section);
1774         // Trigger an event for course section update.
1775         $event = \core\event\course_section_updated::create(
1776                 array(
1777                     'objectid' => $section->id,
1778                     'courseid' => $course->id,
1779                     'context' => context_course::instance($course->id)
1780                 )
1781             );
1782         $event->add_record_snapshot('course_sections', $section);
1783         // Trigger and catch event.
1784         $sink = $this->redirectEvents();
1785         $event->trigger();
1786         $events = $sink->get_events();
1787         $sink->close();
1789         // Validate the event.
1790         $event = $events[0];
1791         $this->assertInstanceOf('\core\event\course_section_updated', $event);
1792         $this->assertEquals('course_sections', $event->objecttable);
1793         $this->assertEquals($section->id, $event->objectid);
1794         $this->assertEquals($course->id, $event->courseid);
1795         $this->assertEquals($coursecontext->id, $event->contextid);
1796         $expecteddesc = 'Course ' . $event->courseid . ' section ' . $event->other['sectionnum'] . ' updated by user ' . $event->userid;
1797         $this->assertEquals($expecteddesc, $event->get_description());
1798         $this->assertEquals($section, $event->get_record_snapshot('course_sections', $event->objectid));
1799         $id = $section->id;
1800         $sectionnum = $section->section;
1801         $expectedlegacydata = array($course->id, "course", "editsection", 'editsection.php?id=' . $id, $sectionnum);
1802         $this->assertEventLegacyLogData($expectedlegacydata, $event);
1803         $this->assertEventContextNotUsed($event);
1804     }
1806     public function test_course_integrity_check() {
1807         global $DB;
1809         $this->resetAfterTest(true);
1810         $course = $this->getDataGenerator()->create_course(array('numsections' => 1),
1811            array('createsections'=>true));
1813         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1814                 array('section' => 0));
1815         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id),
1816                 array('section' => 0));
1817         $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id),
1818                 array('section' => 0));
1819         $correctseq = join(',', array($forum->cmid, $page->cmid, $quiz->cmid));
1821         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1822         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1823         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1824         $this->assertEquals($correctseq, $section0->sequence);
1825         $this->assertEmpty($section1->sequence);
1826         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1827         $this->assertEquals($section0->id, $cms[$page->cmid]->section);
1828         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1829         $this->assertEmpty(course_integrity_check($course->id));
1831         // Now let's make manual change in DB and let course_integrity_check() fix it:
1833         // 1. Module appears twice in one section.
1834         $DB->update_record('course_sections', array('id' => $section0->id, 'sequence' => $section0->sequence. ','. $page->cmid));
1835         $this->assertEquals(
1836                 array('Failed integrity check for course ['. $course->id.
1837                 ']. Sequence for course section ['. $section0->id. '] is "'.
1838                 $section0->sequence. ','. $page->cmid. '", must be "'.
1839                 $section0->sequence. '"'),
1840                 course_integrity_check($course->id));
1841         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1842         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1843         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1844         $this->assertEquals($correctseq, $section0->sequence);
1845         $this->assertEmpty($section1->sequence);
1846         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1847         $this->assertEquals($section0->id, $cms[$page->cmid]->section);
1848         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1850         // 2. Module appears in two sections (last section wins).
1851         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''. $page->cmid));
1852         // First message about double mentioning in sequence, second message about wrong section field for $page.
1853         $this->assertEquals(array(
1854             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
1855             '] must be removed from sequence of section ['. $section0->id.
1856             '] because it is also present in sequence of section ['. $section1->id. ']',
1857             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
1858             '] points to section ['. $section0->id. '] instead of ['. $section1->id. ']'),
1859                 course_integrity_check($course->id));
1860         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1861         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1862         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1863         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
1864         $this->assertEquals(''. $page->cmid, $section1->sequence);
1865         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1866         $this->assertEquals($section1->id, $cms[$page->cmid]->section);
1867         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1869         // 3. Module id is not present in course_section.sequence (integrity check with $fullcheck = false).
1870         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''));
1871         $this->assertEmpty(course_integrity_check($course->id)); // Not an error!
1872         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1873         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1874         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1875         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
1876         $this->assertEmpty($section1->sequence);
1877         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1878         $this->assertEquals($section1->id, $cms[$page->cmid]->section); // Not changed.
1879         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1881         // 4. Module id is not present in course_section.sequence (integrity check with $fullcheck = true).
1882         $this->assertEquals(array('Failed integrity check for course ['. $course->id. ']. Course module ['.
1883                 $page->cmid. '] is missing from sequence of section ['. $section1->id. ']'),
1884                 course_integrity_check($course->id, null, null, true)); // Error!
1885         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1886         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1887         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1888         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
1889         $this->assertEquals(''. $page->cmid, $section1->sequence);  // Yay, module added to section.
1890         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1891         $this->assertEquals($section1->id, $cms[$page->cmid]->section); // Not changed.
1892         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1894         // 5. Module id is not present in course_section.sequence and it's section is invalid (integrity check with $fullcheck = true).
1895         $DB->update_record('course_modules', array('id' => $page->cmid, 'section' => 8765));
1896         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''));
1897         $this->assertEquals(array(
1898             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
1899             '] is missing from sequence of section ['. $section0->id. ']',
1900             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
1901             '] points to section [8765] instead of ['. $section0->id. ']'),
1902                 course_integrity_check($course->id, null, null, true));
1903         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1904         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1905         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1906         $this->assertEquals($forum->cmid. ','. $quiz->cmid. ','. $page->cmid, $section0->sequence); // Module added to section.
1907         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1908         $this->assertEquals($section0->id, $cms[$page->cmid]->section); // Section changed to section0.
1909         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1911         // 6. Module is deleted from course_modules but not deleted in sequence (integrity check with $fullcheck = true).
1912         $DB->delete_records('course_modules', array('id' => $page->cmid));
1913         $this->assertEquals(array('Failed integrity check for course ['. $course->id. ']. Course module ['.
1914                 $page->cmid. '] does not exist but is present in the sequence of section ['. $section0->id. ']'),
1915                 course_integrity_check($course->id, null, null, true));
1916         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1917         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1918         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1919         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
1920         $this->assertEmpty($section1->sequence);
1921         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1922         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1923         $this->assertEquals(2, count($cms));
1924     }
1926     /**
1927      * Tests for event related to course module creation.
1928      */
1929     public function test_course_module_created_event() {
1930         global $USER, $DB;
1931         $this->resetAfterTest();
1933         // Create an assign module.
1934         $sink = $this->redirectEvents();
1935         $modinfo = $this->create_specific_module_test('assign');
1936         $events = $sink->get_events();
1937         $event = array_pop($events);
1938         $sink->close();
1940         $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
1941         $mod = $DB->get_record('assign', array('id' => $modinfo->instance), '*', MUST_EXIST);
1943         // Validate event data.
1944         $this->assertInstanceOf('\core\event\course_module_created', $event);
1945         $this->assertEquals($cm->id, $event->objectid);
1946         $this->assertEquals($USER->id, $event->userid);
1947         $this->assertEquals('course_modules', $event->objecttable);
1948         $url = new moodle_url('/mod/assign/view.php', array('id' => $mod->id));
1949         $this->assertEquals($url, $event->get_url());
1951         // Test legacy data.
1952         $this->assertSame('mod_created', $event->get_legacy_eventname());
1953         $eventdata = new stdClass();
1954         $eventdata->modulename = 'assign';
1955         $eventdata->name       = $mod->name;
1956         $eventdata->cmid       = $cm->id;
1957         $eventdata->courseid   = $cm->course;
1958         $eventdata->userid     = $USER->id;
1959         $this->assertEventLegacyData($eventdata, $event);
1961         $arr = array($cm->course, "course", "add mod", "../mod/assign/view.php?id=$mod->id", "assign $cm->instance");
1962         $this->assertEventLegacyLogData($arr, $event);
1963         $this->assertEventContextNotUsed($event);
1965     }
1967     /**
1968      * Tests for event validations related to course module creation.
1969      */
1970     public function test_course_module_created_event_exceptions() {
1972         $this->resetAfterTest();
1974         // Generate data.
1975         $modinfo = $this->create_specific_module_test('assign');
1976         $context = context_module::instance($modinfo->coursemodule);
1978         // Test not setting instanceid.
1979         try {
1980             $event = \core\event\course_module_created::create(array(
1981                 'courseid' => $modinfo->course,
1982                 'context'  => $context,
1983                 'objectid' => $modinfo->coursemodule,
1984                 'other'    => array(
1985                     'modulename' => 'assign',
1986                     'name'       => 'My assignment',
1987                 )
1988             ));
1989             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
1990                     other['instanceid']");
1991         } catch (coding_exception $e) {
1992             $this->assertContains("Field other['instanceid'] cannot be empty", $e->getMessage());
1993         }
1995         // Test not setting modulename.
1996         try {
1997             $event = \core\event\course_module_created::create(array(
1998                 'courseid' => $modinfo->course,
1999                 'context'  => $context,
2000                 'objectid' => $modinfo->coursemodule,
2001                 'other'    => array(
2002                     'instanceid' => $modinfo->instance,
2003                     'name'       => 'My assignment',
2004                 )
2005             ));
2006             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2007                     other['modulename']");
2008         } catch (coding_exception $e) {
2009             $this->assertContains("Field other['modulename'] cannot be empty", $e->getMessage());
2010         }
2012         // Test not setting name.
2014         try {
2015             $event = \core\event\course_module_created::create(array(
2016                 'courseid' => $modinfo->course,
2017                 'context'  => $context,
2018                 'objectid' => $modinfo->coursemodule,
2019                 'other'    => array(
2020                     'modulename' => 'assign',
2021                     'instanceid' => $modinfo->instance,
2022                 )
2023             ));
2024             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2025                     other['name']");
2026         } catch (coding_exception $e) {
2027             $this->assertContains("Field other['name'] cannot be empty", $e->getMessage());
2028         }
2030     }
2032     /**
2033      * Tests for event related to course module updates.
2034      */
2035     public function test_course_module_updated_event() {
2036         global $USER, $DB;
2037         $this->resetAfterTest();
2039         // Update a forum module.
2040         $sink = $this->redirectEvents();
2041         $modinfo = $this->update_specific_module_test('forum');
2042         $events = $sink->get_events();
2043         $event = array_pop($events);
2044         $sink->close();
2046         $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
2047         $mod = $DB->get_record('forum', array('id' => $cm->instance), '*', MUST_EXIST);
2049         // Validate event data.
2050         $this->assertInstanceOf('\core\event\course_module_updated', $event);
2051         $this->assertEquals($cm->id, $event->objectid);
2052         $this->assertEquals($USER->id, $event->userid);
2053         $this->assertEquals('course_modules', $event->objecttable);
2054         $url = new moodle_url('/mod/forum/view.php', array('id' => $mod->id));
2055         $this->assertEquals($url, $event->get_url());
2057         // Test legacy data.
2058         $this->assertSame('mod_updated', $event->get_legacy_eventname());
2059         $eventdata = new stdClass();
2060         $eventdata->modulename = 'forum';
2061         $eventdata->name       = $mod->name;
2062         $eventdata->cmid       = $cm->id;
2063         $eventdata->courseid   = $cm->course;
2064         $eventdata->userid     = $USER->id;
2065         $this->assertEventLegacyData($eventdata, $event);
2067         $arr = array($cm->course, "course", "update mod", "../mod/forum/view.php?id=$mod->id", "forum $cm->instance");
2068         $this->assertEventLegacyLogData($arr, $event);
2069         $this->assertEventContextNotUsed($event);
2070     }
2072     /**
2073      * Tests for event validations related to course module update.
2074      */
2075     public function test_course_module_updated_event_exceptions() {
2077         $this->resetAfterTest();
2079         // Generate data.
2080         $modinfo = $this->create_specific_module_test('assign');
2081         $context = context_module::instance($modinfo->coursemodule);
2083         // Test not setting instanceid.
2084         try {
2085             $event = \core\event\course_module_updated::create(array(
2086                 'courseid' => $modinfo->course,
2087                 'context'  => $context,
2088                 'objectid' => $modinfo->coursemodule,
2089                 'other'    => array(
2090                     'modulename' => 'assign',
2091                     'name'       => 'My assignment',
2092                 )
2093             ));
2094             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2095                     other['instanceid']");
2096         } catch (coding_exception $e) {
2097             $this->assertContains("Field other['instanceid'] cannot be empty", $e->getMessage());
2098         }
2100         // Test not setting modulename.
2101         try {
2102             $event = \core\event\course_module_updated::create(array(
2103                 'courseid' => $modinfo->course,
2104                 'context'  => $context,
2105                 'objectid' => $modinfo->coursemodule,
2106                 'other'    => array(
2107                     'instanceid' => $modinfo->instance,
2108                     'name'       => 'My assignment',
2109                 )
2110             ));
2111             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2112                     other['modulename']");
2113         } catch (coding_exception $e) {
2114             $this->assertContains("Field other['modulename'] cannot be empty", $e->getMessage());
2115         }
2117         // Test not setting name.
2119         try {
2120             $event = \core\event\course_module_updated::create(array(
2121                 'courseid' => $modinfo->course,
2122                 'context'  => $context,
2123                 'objectid' => $modinfo->coursemodule,
2124                 'other'    => array(
2125                     'modulename' => 'assign',
2126                     'instanceid' => $modinfo->instance,
2127                 )
2128             ));
2129             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2130                     other['name']");
2131         } catch (coding_exception $e) {
2132             $this->assertContains("Field other['name'] cannot be empty", $e->getMessage());
2133         }
2135     }
2137     /**
2138      * Tests for event related to course module delete.
2139      */
2140     public function test_course_module_deleted_event() {
2141         global $USER, $DB;
2142         $this->resetAfterTest();
2144         // Create and delete a module.
2145         $sink = $this->redirectEvents();
2146         $modinfo = $this->create_specific_module_test('forum');
2147         $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
2148         course_delete_module($modinfo->coursemodule);
2149         $events = $sink->get_events();
2150         $event = array_pop($events); // delete module event.;
2151         $sink->close();
2153         // Validate event data.
2154         $this->assertInstanceOf('\core\event\course_module_deleted', $event);
2155         $this->assertEquals($cm->id, $event->objectid);
2156         $this->assertEquals($USER->id, $event->userid);
2157         $this->assertEquals('course_modules', $event->objecttable);
2158         $this->assertEquals(null, $event->get_url());
2159         $this->assertEquals($cm, $event->get_record_snapshot('course_modules', $cm->id));
2161         // Test legacy data.
2162         $this->assertSame('mod_deleted', $event->get_legacy_eventname());
2163         $eventdata = new stdClass();
2164         $eventdata->modulename = 'forum';
2165         $eventdata->cmid       = $cm->id;
2166         $eventdata->courseid   = $cm->course;
2167         $eventdata->userid     = $USER->id;
2168         $this->assertEventLegacyData($eventdata, $event);
2170         $arr = array($cm->course, 'course', "delete mod", "view.php?id=$cm->course", "forum $cm->instance", $cm->id);
2171         $this->assertEventLegacyLogData($arr, $event);
2173     }
2175     /**
2176      * Tests for event validations related to course module deletion.
2177      */
2178     public function test_course_module_deleted_event_exceptions() {
2180         $this->resetAfterTest();
2182         // Generate data.
2183         $modinfo = $this->create_specific_module_test('assign');
2184         $context = context_module::instance($modinfo->coursemodule);
2186         // Test not setting instanceid.
2187         try {
2188             $event = \core\event\course_module_deleted::create(array(
2189                 'courseid' => $modinfo->course,
2190                 'context'  => $context,
2191                 'objectid' => $modinfo->coursemodule,
2192                 'other'    => array(
2193                     'modulename' => 'assign',
2194                     'name'       => 'My assignment',
2195                 )
2196             ));
2197             $this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
2198                     other['instanceid']");
2199         } catch (coding_exception $e) {
2200             $this->assertContains("Field other['instanceid'] cannot be empty", $e->getMessage());
2201         }
2203         // Test not setting modulename.
2204         try {
2205             $event = \core\event\course_module_deleted::create(array(
2206                 'courseid' => $modinfo->course,
2207                 'context'  => $context,
2208                 'objectid' => $modinfo->coursemodule,
2209                 'other'    => array(
2210                     'instanceid' => $modinfo->instance,
2211                     'name'       => 'My assignment',
2212                 )
2213             ));
2214             $this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
2215                     other['modulename']");
2216         } catch (coding_exception $e) {
2217             $this->assertContains("Field other['modulename'] cannot be empty", $e->getMessage());
2218         }
2219     }
2221     /**
2222      * Returns a user object and its assigned new role.
2223      *
2224      * @param testing_data_generator $generator
2225      * @param $contextid
2226      * @return array The user object and the role ID
2227      */
2228     protected function get_user_objects(testing_data_generator $generator, $contextid) {
2229         global $USER;
2231         if (empty($USER->id)) {
2232             $user  = $generator->create_user();
2233             $this->setUser($user);
2234         }
2235         $roleid = create_role('Test role', 'testrole', 'Test role description');
2236         if (!is_array($contextid)) {
2237             $contextid = array($contextid);
2238         }
2239         foreach ($contextid as $cid) {
2240             $assignid = role_assign($roleid, $user->id, $cid);
2241         }
2242         return array($user, $roleid);
2243     }
2245     /**
2246      * Test course move after course.
2247      */
2248     public function test_course_change_sortorder_after_course() {
2249         global $DB;
2251         $this->resetAfterTest(true);
2253         $generator = $this->getDataGenerator();
2254         $category = $generator->create_category();
2255         $course3 = $generator->create_course(array('category' => $category->id));
2256         $course2 = $generator->create_course(array('category' => $category->id));
2257         $course1 = $generator->create_course(array('category' => $category->id));
2258         $context = $category->get_context();
2260         list($user, $roleid) = $this->get_user_objects($generator, $context->id);
2261         $caps = course_capability_assignment::allow('moodle/category:manage', $roleid, $context->id);
2263         $courses = $category->get_courses();
2264         $this->assertInternalType('array', $courses);
2265         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2266         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2267         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2269         // Test moving down.
2270         $this->assertTrue(course_change_sortorder_after_course($course1->id, $course3->id));
2271         $courses = $category->get_courses();
2272         $this->assertInternalType('array', $courses);
2273         $this->assertEquals(array($course2->id, $course3->id, $course1->id), array_keys($courses));
2274         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2275         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2277         // Test moving up.
2278         $this->assertTrue(course_change_sortorder_after_course($course1->id, $course2->id));
2279         $courses = $category->get_courses();
2280         $this->assertInternalType('array', $courses);
2281         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
2282         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2283         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2285         // Test moving to the top.
2286         $this->assertTrue(course_change_sortorder_after_course($course1->id, 0));
2287         $courses = $category->get_courses();
2288         $this->assertInternalType('array', $courses);
2289         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2290         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2291         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2292     }
2294     /**
2295      * Tests changing the visibility of a course.
2296      */
2297     public function test_course_change_visibility() {
2298         global $DB;
2300         $this->resetAfterTest(true);
2302         $generator = $this->getDataGenerator();
2303         $category = $generator->create_category();
2304         $course = $generator->create_course(array('category' => $category->id));
2306         $this->assertEquals('1', $course->visible);
2307         $this->assertEquals('1', $course->visibleold);
2309         $this->assertTrue(course_change_visibility($course->id, false));
2310         $course = $DB->get_record('course', array('id' => $course->id));
2311         $this->assertEquals('0', $course->visible);
2312         $this->assertEquals('0', $course->visibleold);
2314         $this->assertTrue(course_change_visibility($course->id, true));
2315         $course = $DB->get_record('course', array('id' => $course->id));
2316         $this->assertEquals('1', $course->visible);
2317         $this->assertEquals('1', $course->visibleold);
2318     }
2320     /**
2321      * Tests moving the course up and down by one.
2322      */
2323     public function test_course_change_sortorder_by_one() {
2324         global $DB;
2326         $this->resetAfterTest(true);
2328         $generator = $this->getDataGenerator();
2329         $category = $generator->create_category();
2330         $course3 = $generator->create_course(array('category' => $category->id));
2331         $course2 = $generator->create_course(array('category' => $category->id));
2332         $course1 = $generator->create_course(array('category' => $category->id));
2334         $courses = $category->get_courses();
2335         $this->assertInternalType('array', $courses);
2336         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2337         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2338         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2340         // Test moving down.
2341         $course1 = get_course($course1->id);
2342         $this->assertTrue(course_change_sortorder_by_one($course1, false));
2343         $courses = $category->get_courses();
2344         $this->assertInternalType('array', $courses);
2345         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
2346         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2347         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2349         // Test moving up.
2350         $course1 = get_course($course1->id);
2351         $this->assertTrue(course_change_sortorder_by_one($course1, true));
2352         $courses = $category->get_courses();
2353         $this->assertInternalType('array', $courses);
2354         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2355         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2356         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2358         // Test moving the top course up one.
2359         $course1 = get_course($course1->id);
2360         $this->assertFalse(course_change_sortorder_by_one($course1, true));
2361         // Check nothing changed.
2362         $courses = $category->get_courses();
2363         $this->assertInternalType('array', $courses);
2364         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2365         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2366         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2368         // Test moving the bottom course up down.
2369         $course3 = get_course($course3->id);
2370         $this->assertFalse(course_change_sortorder_by_one($course3, false));
2371         // Check nothing changed.
2372         $courses = $category->get_courses();
2373         $this->assertInternalType('array', $courses);
2374         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2375         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2376         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2377     }