Merge branch 'MDL-44272-master' of git://github.com/ankitagarwal/moodle
[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']);
1573         // The legacy data also passed the context in the course object and substitutes timemodified with the current date.
1574         $expectedlegacy = clone($course);
1575         $expectedlegacy->context = $coursecontext;
1576         $expectedlegacy->timemodified = $event->timecreated;
1577         $this->assertEventLegacyData($expectedlegacy, $event);
1579         // Validate legacy log data.
1580         $expectedlog = array(SITEID, 'course', 'delete', 'view.php?id=' . $course->id, $course->fullname . '(ID ' . $course->id . ')');
1581         $this->assertEventLegacyLogData($expectedlog, $event);
1582         $this->assertEventContextNotUsed($event);
1583     }
1585     /**
1586      * Test that triggering a course_content_deleted event works as expected.
1587      */
1588     public function test_course_content_deleted_event() {
1589         global $DB;
1591         $this->resetAfterTest();
1593         // Create the course.
1594         $course = $this->getDataGenerator()->create_course();
1596         // Get the course from the DB. The data generator adds some extra properties, such as
1597         // numsections, to the course object which will fail the assertions later on.
1598         $course = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1600         // Save the course context before we delete the course.
1601         $coursecontext = context_course::instance($course->id);
1603         // Catch the update event.
1604         $sink = $this->redirectEvents();
1606         remove_course_contents($course->id, false);
1608         // Capture the event.
1609         $events = $sink->get_events();
1610         $sink->close();
1612         // Validate the event.
1613         $event = $events[0];
1614         $this->assertInstanceOf('\core\event\course_content_deleted', $event);
1615         $this->assertEquals('course', $event->objecttable);
1616         $this->assertEquals($course->id, $event->objectid);
1617         $this->assertEquals($coursecontext->id, $event->contextid);
1618         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1619         $this->assertEquals('course_content_removed', $event->get_legacy_eventname());
1620         // The legacy data also passed the context and options in the course object.
1621         $course->context = $coursecontext;
1622         $course->options = array();
1623         $this->assertEventLegacyData($course, $event);
1624         $this->assertEventContextNotUsed($event);
1625     }
1627     /**
1628      * Test that triggering a course_category_deleted event works as expected.
1629      */
1630     public function test_course_category_deleted_event() {
1631         $this->resetAfterTest();
1633         // Create a category.
1634         $category = $this->getDataGenerator()->create_category();
1636         // Save the context before it is deleted.
1637         $categorycontext = context_coursecat::instance($category->id);
1639         // Catch the update event.
1640         $sink = $this->redirectEvents();
1642         // Delete the category.
1643         $category->delete_full();
1645         // Capture the event.
1646         $events = $sink->get_events();
1647         $sink->close();
1649         // Validate the event.
1650         $event = $events[0];
1651         $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1652         $this->assertEquals('course_categories', $event->objecttable);
1653         $this->assertEquals($category->id, $event->objectid);
1654         $this->assertEquals($categorycontext->id, $event->contextid);
1655         $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1656         $this->assertEventLegacyData($category, $event);
1657         $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category->name . '(ID ' . $category->id . ')');
1658         $this->assertEventLegacyLogData($expectedlog, $event);
1660         // Create two categories.
1661         $category = $this->getDataGenerator()->create_category();
1662         $category2 = $this->getDataGenerator()->create_category();
1664         // Save the context before it is moved and then deleted.
1665         $category2context = context_coursecat::instance($category2->id);
1667         // Catch the update event.
1668         $sink = $this->redirectEvents();
1670         // Move the category.
1671         $category2->delete_move($category->id);
1673         // Capture the event.
1674         $events = $sink->get_events();
1675         $sink->close();
1677         // Validate the event.
1678         $event = $events[0];
1679         $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1680         $this->assertEquals('course_categories', $event->objecttable);
1681         $this->assertEquals($category2->id, $event->objectid);
1682         $this->assertEquals($category2context->id, $event->contextid);
1683         $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1684         $this->assertEventLegacyData($category2, $event);
1685         $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category2->name . '(ID ' . $category2->id . ')');
1686         $this->assertEventLegacyLogData($expectedlog, $event);
1687         $this->assertEventContextNotUsed($event);
1688     }
1690     /**
1691      * Test that triggering a course_restored event works as expected.
1692      */
1693     public function test_course_restored_event() {
1694         global $CFG;
1696         // Get the necessary files to perform backup and restore.
1697         require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
1698         require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
1700         $this->resetAfterTest();
1702         // Set to admin user.
1703         $this->setAdminUser();
1705         // The user id is going to be 2 since we are the admin user.
1706         $userid = 2;
1708         // Create a course.
1709         $course = $this->getDataGenerator()->create_course();
1711         // Create backup file and save it to the backup location.
1712         $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
1713             backup::INTERACTIVE_NO, backup::MODE_GENERAL, $userid);
1714         $bc->execute_plan();
1715         $results = $bc->get_results();
1716         $file = $results['backup_destination'];
1717         $fp = get_file_packer();
1718         $filepath = $CFG->dataroot . '/temp/backup/test-restore-course-event';
1719         $file->extract_to_pathname($fp, $filepath);
1720         $bc->destroy();
1721         unset($bc);
1723         // Now we want to catch the restore course event.
1724         $sink = $this->redirectEvents();
1726         // Now restore the course to trigger the event.
1727         $rc = new restore_controller('test-restore-course-event', $course->id, backup::INTERACTIVE_NO,
1728             backup::MODE_GENERAL, $userid, backup::TARGET_NEW_COURSE);
1729         $rc->execute_precheck();
1730         $rc->execute_plan();
1732         // Capture the event.
1733         $events = $sink->get_events();
1734         $sink->close();
1736         // Validate the event.
1737         $event = $events[0];
1738         $this->assertInstanceOf('\core\event\course_restored', $event);
1739         $this->assertEquals('course', $event->objecttable);
1740         $this->assertEquals($rc->get_courseid(), $event->objectid);
1741         $this->assertEquals(context_course::instance($rc->get_courseid())->id, $event->contextid);
1742         $this->assertEquals('course_restored', $event->get_legacy_eventname());
1743         $legacydata = (object) array(
1744             'courseid' => $rc->get_courseid(),
1745             'userid' => $rc->get_userid(),
1746             'type' => $rc->get_type(),
1747             'target' => $rc->get_target(),
1748             'mode' => $rc->get_mode(),
1749             'operation' => $rc->get_operation(),
1750             'samesite' => $rc->is_samesite()
1751         );
1752         $this->assertEventLegacyData($legacydata, $event);
1753         $this->assertEventContextNotUsed($event);
1755         // Destroy the resource controller since we are done using it.
1756         $rc->destroy();
1757         unset($rc);
1758     }
1760     /**
1761      * Test that triggering a course_section_updated event works as expected.
1762      */
1763     public function test_course_section_updated_event() {
1764         global $DB;
1766         $this->resetAfterTest();
1768         // Create the course with sections.
1769         $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true));
1770         $sections = $DB->get_records('course_sections', array('course' => $course->id));
1772         $coursecontext = context_course::instance($course->id);
1774         $section = array_pop($sections);
1775         $section->name = 'Test section';
1776         $section->summary = 'Test section summary';
1777         $DB->update_record('course_sections', $section);
1779         // Trigger an event for course section update.
1780         $event = \core\event\course_section_updated::create(
1781                 array(
1782                     'objectid' => $section->id,
1783                     'courseid' => $course->id,
1784                     'context' => context_course::instance($course->id)
1785                 )
1786             );
1787         $event->add_record_snapshot('course_sections', $section);
1788         // Trigger and catch event.
1789         $sink = $this->redirectEvents();
1790         $event->trigger();
1791         $events = $sink->get_events();
1792         $sink->close();
1794         // Validate the event.
1795         $event = $events[0];
1796         $this->assertInstanceOf('\core\event\course_section_updated', $event);
1797         $this->assertEquals('course_sections', $event->objecttable);
1798         $this->assertEquals($section->id, $event->objectid);
1799         $this->assertEquals($course->id, $event->courseid);
1800         $this->assertEquals($coursecontext->id, $event->contextid);
1801         $expecteddesc = 'Course ' . $event->courseid . ' section ' . $event->other['sectionnum'] . ' updated by user ' . $event->userid;
1802         $this->assertEquals($expecteddesc, $event->get_description());
1803         $this->assertEquals($section, $event->get_record_snapshot('course_sections', $event->objectid));
1804         $id = $section->id;
1805         $sectionnum = $section->section;
1806         $expectedlegacydata = array($course->id, "course", "editsection", 'editsection.php?id=' . $id, $sectionnum);
1807         $this->assertEventLegacyLogData($expectedlegacydata, $event);
1808         $this->assertEventContextNotUsed($event);
1809     }
1811     public function test_course_integrity_check() {
1812         global $DB;
1814         $this->resetAfterTest(true);
1815         $course = $this->getDataGenerator()->create_course(array('numsections' => 1),
1816            array('createsections'=>true));
1818         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1819                 array('section' => 0));
1820         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id),
1821                 array('section' => 0));
1822         $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id),
1823                 array('section' => 0));
1824         $correctseq = join(',', array($forum->cmid, $page->cmid, $quiz->cmid));
1826         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1827         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1828         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1829         $this->assertEquals($correctseq, $section0->sequence);
1830         $this->assertEmpty($section1->sequence);
1831         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1832         $this->assertEquals($section0->id, $cms[$page->cmid]->section);
1833         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1834         $this->assertEmpty(course_integrity_check($course->id));
1836         // Now let's make manual change in DB and let course_integrity_check() fix it:
1838         // 1. Module appears twice in one section.
1839         $DB->update_record('course_sections', array('id' => $section0->id, 'sequence' => $section0->sequence. ','. $page->cmid));
1840         $this->assertEquals(
1841                 array('Failed integrity check for course ['. $course->id.
1842                 ']. Sequence for course section ['. $section0->id. '] is "'.
1843                 $section0->sequence. ','. $page->cmid. '", must be "'.
1844                 $section0->sequence. '"'),
1845                 course_integrity_check($course->id));
1846         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1847         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1848         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1849         $this->assertEquals($correctseq, $section0->sequence);
1850         $this->assertEmpty($section1->sequence);
1851         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1852         $this->assertEquals($section0->id, $cms[$page->cmid]->section);
1853         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1855         // 2. Module appears in two sections (last section wins).
1856         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''. $page->cmid));
1857         // First message about double mentioning in sequence, second message about wrong section field for $page.
1858         $this->assertEquals(array(
1859             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
1860             '] must be removed from sequence of section ['. $section0->id.
1861             '] because it is also present in sequence of section ['. $section1->id. ']',
1862             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
1863             '] points to section ['. $section0->id. '] instead of ['. $section1->id. ']'),
1864                 course_integrity_check($course->id));
1865         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1866         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1867         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1868         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
1869         $this->assertEquals(''. $page->cmid, $section1->sequence);
1870         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1871         $this->assertEquals($section1->id, $cms[$page->cmid]->section);
1872         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1874         // 3. Module id is not present in course_section.sequence (integrity check with $fullcheck = false).
1875         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''));
1876         $this->assertEmpty(course_integrity_check($course->id)); // Not an error!
1877         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1878         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1879         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1880         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
1881         $this->assertEmpty($section1->sequence);
1882         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1883         $this->assertEquals($section1->id, $cms[$page->cmid]->section); // Not changed.
1884         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1886         // 4. Module id is not present in course_section.sequence (integrity check with $fullcheck = true).
1887         $this->assertEquals(array('Failed integrity check for course ['. $course->id. ']. Course module ['.
1888                 $page->cmid. '] is missing from sequence of section ['. $section1->id. ']'),
1889                 course_integrity_check($course->id, null, null, true)); // Error!
1890         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1891         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1892         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1893         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
1894         $this->assertEquals(''. $page->cmid, $section1->sequence);  // Yay, module added to section.
1895         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1896         $this->assertEquals($section1->id, $cms[$page->cmid]->section); // Not changed.
1897         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1899         // 5. Module id is not present in course_section.sequence and it's section is invalid (integrity check with $fullcheck = true).
1900         $DB->update_record('course_modules', array('id' => $page->cmid, 'section' => 8765));
1901         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''));
1902         $this->assertEquals(array(
1903             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
1904             '] is missing from sequence of section ['. $section0->id. ']',
1905             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
1906             '] points to section [8765] instead of ['. $section0->id. ']'),
1907                 course_integrity_check($course->id, null, null, true));
1908         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1909         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1910         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1911         $this->assertEquals($forum->cmid. ','. $quiz->cmid. ','. $page->cmid, $section0->sequence); // Module added to section.
1912         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1913         $this->assertEquals($section0->id, $cms[$page->cmid]->section); // Section changed to section0.
1914         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1916         // 6. Module is deleted from course_modules but not deleted in sequence (integrity check with $fullcheck = true).
1917         $DB->delete_records('course_modules', array('id' => $page->cmid));
1918         $this->assertEquals(array('Failed integrity check for course ['. $course->id. ']. Course module ['.
1919                 $page->cmid. '] does not exist but is present in the sequence of section ['. $section0->id. ']'),
1920                 course_integrity_check($course->id, null, null, true));
1921         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1922         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1923         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1924         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
1925         $this->assertEmpty($section1->sequence);
1926         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1927         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1928         $this->assertEquals(2, count($cms));
1929     }
1931     /**
1932      * Tests for event related to course module creation.
1933      */
1934     public function test_course_module_created_event() {
1935         global $USER, $DB;
1936         $this->resetAfterTest();
1938         // Create an assign module.
1939         $sink = $this->redirectEvents();
1940         $modinfo = $this->create_specific_module_test('assign');
1941         $events = $sink->get_events();
1942         $event = array_pop($events);
1943         $sink->close();
1945         $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
1946         $mod = $DB->get_record('assign', array('id' => $modinfo->instance), '*', MUST_EXIST);
1948         // Validate event data.
1949         $this->assertInstanceOf('\core\event\course_module_created', $event);
1950         $this->assertEquals($cm->id, $event->objectid);
1951         $this->assertEquals($USER->id, $event->userid);
1952         $this->assertEquals('course_modules', $event->objecttable);
1953         $url = new moodle_url('/mod/assign/view.php', array('id' => $cm->id));
1954         $this->assertEquals($url, $event->get_url());
1956         // Test legacy data.
1957         $this->assertSame('mod_created', $event->get_legacy_eventname());
1958         $eventdata = new stdClass();
1959         $eventdata->modulename = 'assign';
1960         $eventdata->name       = $mod->name;
1961         $eventdata->cmid       = $cm->id;
1962         $eventdata->courseid   = $cm->course;
1963         $eventdata->userid     = $USER->id;
1964         $this->assertEventLegacyData($eventdata, $event);
1966         $arr = array($cm->course, "course", "add mod", "../mod/assign/view.php?id=$cm->id", "assign $cm->instance");
1967         $this->assertEventLegacyLogData($arr, $event);
1968         $this->assertEventContextNotUsed($event);
1970     }
1972     /**
1973      * Tests for event validations related to course module creation.
1974      */
1975     public function test_course_module_created_event_exceptions() {
1977         $this->resetAfterTest();
1979         // Generate data.
1980         $modinfo = $this->create_specific_module_test('assign');
1981         $context = context_module::instance($modinfo->coursemodule);
1983         // Test not setting instanceid.
1984         try {
1985             $event = \core\event\course_module_created::create(array(
1986                 'courseid' => $modinfo->course,
1987                 'context'  => $context,
1988                 'objectid' => $modinfo->coursemodule,
1989                 'other'    => array(
1990                     'modulename' => 'assign',
1991                     'name'       => 'My assignment',
1992                 )
1993             ));
1994             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
1995                     other['instanceid']");
1996         } catch (coding_exception $e) {
1997             $this->assertContains("Field other['instanceid'] cannot be empty", $e->getMessage());
1998         }
2000         // Test not setting modulename.
2001         try {
2002             $event = \core\event\course_module_created::create(array(
2003                 'courseid' => $modinfo->course,
2004                 'context'  => $context,
2005                 'objectid' => $modinfo->coursemodule,
2006                 'other'    => array(
2007                     'instanceid' => $modinfo->instance,
2008                     'name'       => 'My assignment',
2009                 )
2010             ));
2011             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2012                     other['modulename']");
2013         } catch (coding_exception $e) {
2014             $this->assertContains("Field other['modulename'] cannot be empty", $e->getMessage());
2015         }
2017         // Test not setting name.
2019         try {
2020             $event = \core\event\course_module_created::create(array(
2021                 'courseid' => $modinfo->course,
2022                 'context'  => $context,
2023                 'objectid' => $modinfo->coursemodule,
2024                 'other'    => array(
2025                     'modulename' => 'assign',
2026                     'instanceid' => $modinfo->instance,
2027                 )
2028             ));
2029             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2030                     other['name']");
2031         } catch (coding_exception $e) {
2032             $this->assertContains("Field other['name'] cannot be empty", $e->getMessage());
2033         }
2035     }
2037     /**
2038      * Tests for event related to course module updates.
2039      */
2040     public function test_course_module_updated_event() {
2041         global $USER, $DB;
2042         $this->resetAfterTest();
2044         // Update a forum module.
2045         $sink = $this->redirectEvents();
2046         $modinfo = $this->update_specific_module_test('forum');
2047         $events = $sink->get_events();
2048         $event = array_pop($events);
2049         $sink->close();
2051         $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
2052         $mod = $DB->get_record('forum', array('id' => $cm->instance), '*', MUST_EXIST);
2054         // Validate event data.
2055         $this->assertInstanceOf('\core\event\course_module_updated', $event);
2056         $this->assertEquals($cm->id, $event->objectid);
2057         $this->assertEquals($USER->id, $event->userid);
2058         $this->assertEquals('course_modules', $event->objecttable);
2059         $url = new moodle_url('/mod/forum/view.php', array('id' => $cm->id));
2060         $this->assertEquals($url, $event->get_url());
2062         // Test legacy data.
2063         $this->assertSame('mod_updated', $event->get_legacy_eventname());
2064         $eventdata = new stdClass();
2065         $eventdata->modulename = 'forum';
2066         $eventdata->name       = $mod->name;
2067         $eventdata->cmid       = $cm->id;
2068         $eventdata->courseid   = $cm->course;
2069         $eventdata->userid     = $USER->id;
2070         $this->assertEventLegacyData($eventdata, $event);
2072         $arr = array($cm->course, "course", "update mod", "../mod/forum/view.php?id=$cm->id", "forum $cm->instance");
2073         $this->assertEventLegacyLogData($arr, $event);
2074         $this->assertEventContextNotUsed($event);
2075     }
2077     /**
2078      * Tests for event validations related to course module update.
2079      */
2080     public function test_course_module_updated_event_exceptions() {
2082         $this->resetAfterTest();
2084         // Generate data.
2085         $modinfo = $this->create_specific_module_test('assign');
2086         $context = context_module::instance($modinfo->coursemodule);
2088         // Test not setting instanceid.
2089         try {
2090             $event = \core\event\course_module_updated::create(array(
2091                 'courseid' => $modinfo->course,
2092                 'context'  => $context,
2093                 'objectid' => $modinfo->coursemodule,
2094                 'other'    => array(
2095                     'modulename' => 'assign',
2096                     'name'       => 'My assignment',
2097                 )
2098             ));
2099             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2100                     other['instanceid']");
2101         } catch (coding_exception $e) {
2102             $this->assertContains("Field other['instanceid'] cannot be empty", $e->getMessage());
2103         }
2105         // Test not setting modulename.
2106         try {
2107             $event = \core\event\course_module_updated::create(array(
2108                 'courseid' => $modinfo->course,
2109                 'context'  => $context,
2110                 'objectid' => $modinfo->coursemodule,
2111                 'other'    => array(
2112                     'instanceid' => $modinfo->instance,
2113                     'name'       => 'My assignment',
2114                 )
2115             ));
2116             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2117                     other['modulename']");
2118         } catch (coding_exception $e) {
2119             $this->assertContains("Field other['modulename'] cannot be empty", $e->getMessage());
2120         }
2122         // Test not setting name.
2124         try {
2125             $event = \core\event\course_module_updated::create(array(
2126                 'courseid' => $modinfo->course,
2127                 'context'  => $context,
2128                 'objectid' => $modinfo->coursemodule,
2129                 'other'    => array(
2130                     'modulename' => 'assign',
2131                     'instanceid' => $modinfo->instance,
2132                 )
2133             ));
2134             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2135                     other['name']");
2136         } catch (coding_exception $e) {
2137             $this->assertContains("Field other['name'] cannot be empty", $e->getMessage());
2138         }
2140     }
2142     /**
2143      * Tests for event related to course module delete.
2144      */
2145     public function test_course_module_deleted_event() {
2146         global $USER, $DB;
2147         $this->resetAfterTest();
2149         // Create and delete a module.
2150         $sink = $this->redirectEvents();
2151         $modinfo = $this->create_specific_module_test('forum');
2152         $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
2153         course_delete_module($modinfo->coursemodule);
2154         $events = $sink->get_events();
2155         $event = array_pop($events); // delete module event.;
2156         $sink->close();
2158         // Validate event data.
2159         $this->assertInstanceOf('\core\event\course_module_deleted', $event);
2160         $this->assertEquals($cm->id, $event->objectid);
2161         $this->assertEquals($USER->id, $event->userid);
2162         $this->assertEquals('course_modules', $event->objecttable);
2163         $this->assertEquals(null, $event->get_url());
2164         $this->assertEquals($cm, $event->get_record_snapshot('course_modules', $cm->id));
2166         // Test legacy data.
2167         $this->assertSame('mod_deleted', $event->get_legacy_eventname());
2168         $eventdata = new stdClass();
2169         $eventdata->modulename = 'forum';
2170         $eventdata->cmid       = $cm->id;
2171         $eventdata->courseid   = $cm->course;
2172         $eventdata->userid     = $USER->id;
2173         $this->assertEventLegacyData($eventdata, $event);
2175         $arr = array($cm->course, 'course', "delete mod", "view.php?id=$cm->course", "forum $cm->instance", $cm->id);
2176         $this->assertEventLegacyLogData($arr, $event);
2178     }
2180     /**
2181      * Tests for event validations related to course module deletion.
2182      */
2183     public function test_course_module_deleted_event_exceptions() {
2185         $this->resetAfterTest();
2187         // Generate data.
2188         $modinfo = $this->create_specific_module_test('assign');
2189         $context = context_module::instance($modinfo->coursemodule);
2191         // Test not setting instanceid.
2192         try {
2193             $event = \core\event\course_module_deleted::create(array(
2194                 'courseid' => $modinfo->course,
2195                 'context'  => $context,
2196                 'objectid' => $modinfo->coursemodule,
2197                 'other'    => array(
2198                     'modulename' => 'assign',
2199                     'name'       => 'My assignment',
2200                 )
2201             ));
2202             $this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
2203                     other['instanceid']");
2204         } catch (coding_exception $e) {
2205             $this->assertContains("Field other['instanceid'] cannot be empty", $e->getMessage());
2206         }
2208         // Test not setting modulename.
2209         try {
2210             $event = \core\event\course_module_deleted::create(array(
2211                 'courseid' => $modinfo->course,
2212                 'context'  => $context,
2213                 'objectid' => $modinfo->coursemodule,
2214                 'other'    => array(
2215                     'instanceid' => $modinfo->instance,
2216                     'name'       => 'My assignment',
2217                 )
2218             ));
2219             $this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
2220                     other['modulename']");
2221         } catch (coding_exception $e) {
2222             $this->assertContains("Field other['modulename'] cannot be empty", $e->getMessage());
2223         }
2224     }
2226     /**
2227      * Returns a user object and its assigned new role.
2228      *
2229      * @param testing_data_generator $generator
2230      * @param $contextid
2231      * @return array The user object and the role ID
2232      */
2233     protected function get_user_objects(testing_data_generator $generator, $contextid) {
2234         global $USER;
2236         if (empty($USER->id)) {
2237             $user  = $generator->create_user();
2238             $this->setUser($user);
2239         }
2240         $roleid = create_role('Test role', 'testrole', 'Test role description');
2241         if (!is_array($contextid)) {
2242             $contextid = array($contextid);
2243         }
2244         foreach ($contextid as $cid) {
2245             $assignid = role_assign($roleid, $user->id, $cid);
2246         }
2247         return array($user, $roleid);
2248     }
2250     /**
2251      * Test course move after course.
2252      */
2253     public function test_course_change_sortorder_after_course() {
2254         global $DB;
2256         $this->resetAfterTest(true);
2258         $generator = $this->getDataGenerator();
2259         $category = $generator->create_category();
2260         $course3 = $generator->create_course(array('category' => $category->id));
2261         $course2 = $generator->create_course(array('category' => $category->id));
2262         $course1 = $generator->create_course(array('category' => $category->id));
2263         $context = $category->get_context();
2265         list($user, $roleid) = $this->get_user_objects($generator, $context->id);
2266         $caps = course_capability_assignment::allow('moodle/category:manage', $roleid, $context->id);
2268         $courses = $category->get_courses();
2269         $this->assertInternalType('array', $courses);
2270         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2271         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2272         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2274         // Test moving down.
2275         $this->assertTrue(course_change_sortorder_after_course($course1->id, $course3->id));
2276         $courses = $category->get_courses();
2277         $this->assertInternalType('array', $courses);
2278         $this->assertEquals(array($course2->id, $course3->id, $course1->id), array_keys($courses));
2279         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2280         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2282         // Test moving up.
2283         $this->assertTrue(course_change_sortorder_after_course($course1->id, $course2->id));
2284         $courses = $category->get_courses();
2285         $this->assertInternalType('array', $courses);
2286         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
2287         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2288         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2290         // Test moving to the top.
2291         $this->assertTrue(course_change_sortorder_after_course($course1->id, 0));
2292         $courses = $category->get_courses();
2293         $this->assertInternalType('array', $courses);
2294         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2295         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2296         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2297     }
2299     /**
2300      * Tests changing the visibility of a course.
2301      */
2302     public function test_course_change_visibility() {
2303         global $DB;
2305         $this->resetAfterTest(true);
2307         $generator = $this->getDataGenerator();
2308         $category = $generator->create_category();
2309         $course = $generator->create_course(array('category' => $category->id));
2311         $this->assertEquals('1', $course->visible);
2312         $this->assertEquals('1', $course->visibleold);
2314         $this->assertTrue(course_change_visibility($course->id, false));
2315         $course = $DB->get_record('course', array('id' => $course->id));
2316         $this->assertEquals('0', $course->visible);
2317         $this->assertEquals('0', $course->visibleold);
2319         $this->assertTrue(course_change_visibility($course->id, true));
2320         $course = $DB->get_record('course', array('id' => $course->id));
2321         $this->assertEquals('1', $course->visible);
2322         $this->assertEquals('1', $course->visibleold);
2323     }
2325     /**
2326      * Tests moving the course up and down by one.
2327      */
2328     public function test_course_change_sortorder_by_one() {
2329         global $DB;
2331         $this->resetAfterTest(true);
2333         $generator = $this->getDataGenerator();
2334         $category = $generator->create_category();
2335         $course3 = $generator->create_course(array('category' => $category->id));
2336         $course2 = $generator->create_course(array('category' => $category->id));
2337         $course1 = $generator->create_course(array('category' => $category->id));
2339         $courses = $category->get_courses();
2340         $this->assertInternalType('array', $courses);
2341         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2342         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2343         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2345         // Test moving down.
2346         $course1 = get_course($course1->id);
2347         $this->assertTrue(course_change_sortorder_by_one($course1, false));
2348         $courses = $category->get_courses();
2349         $this->assertInternalType('array', $courses);
2350         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
2351         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2352         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2354         // Test moving up.
2355         $course1 = get_course($course1->id);
2356         $this->assertTrue(course_change_sortorder_by_one($course1, true));
2357         $courses = $category->get_courses();
2358         $this->assertInternalType('array', $courses);
2359         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2360         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2361         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2363         // Test moving the top course up one.
2364         $course1 = get_course($course1->id);
2365         $this->assertFalse(course_change_sortorder_by_one($course1, true));
2366         // Check nothing changed.
2367         $courses = $category->get_courses();
2368         $this->assertInternalType('array', $courses);
2369         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2370         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2371         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2373         // Test moving the bottom course up down.
2374         $course3 = get_course($course3->id);
2375         $this->assertFalse(course_change_sortorder_by_one($course3, false));
2376         // Check nothing changed.
2377         $courses = $category->get_courses();
2378         $this->assertInternalType('array', $courses);
2379         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2380         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2381         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2382     }