MDL-42084 Unit tests: Remove unnecessary 'clear time limit change' lines
[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');
32 class core_course_courselib_testcase extends advanced_testcase {
34     /**
35      * Set forum specific test values for calling create_module().
36      *
37      * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
38      */
39     private function forum_create_set_values(&$moduleinfo) {
40         // Completion specific to forum - optional.
41         $moduleinfo->completionposts = 3;
42         $moduleinfo->completiondiscussions = 1;
43         $moduleinfo->completionreplies = 2;
45         // Specific values to the Forum module.
46         $moduleinfo->forcesubscribe = FORUM_INITIALSUBSCRIBE;
47         $moduleinfo->type = 'single';
48         $moduleinfo->trackingtype = FORUM_TRACKING_FORCED;
49         $moduleinfo->maxbytes = 10240;
50         $moduleinfo->maxattachments = 2;
52         // Post threshold for blocking - specific to forum.
53         $moduleinfo->blockperiod = 60*60*24;
54         $moduleinfo->blockafter = 10;
55         $moduleinfo->warnafter = 5;
56     }
58     /**
59      * Execute test asserts on the saved DB data by create_module($forum).
60      *
61      * @param object $moduleinfo - the specific forum values that were used to create a forum.
62      * @param object $dbmodinstance - the DB values of the created forum.
63      */
64     private function forum_create_run_asserts($moduleinfo, $dbmodinstance) {
65         // Compare values specific to forums.
66         $this->assertEquals($moduleinfo->forcesubscribe, $dbmodinstance->forcesubscribe);
67         $this->assertEquals($moduleinfo->type, $dbmodinstance->type);
68         $this->assertEquals($moduleinfo->assessed, $dbmodinstance->assessed);
69         $this->assertEquals($moduleinfo->completionposts, $dbmodinstance->completionposts);
70         $this->assertEquals($moduleinfo->completiondiscussions, $dbmodinstance->completiondiscussions);
71         $this->assertEquals($moduleinfo->completionreplies, $dbmodinstance->completionreplies);
72         $this->assertEquals($moduleinfo->scale, $dbmodinstance->scale);
73         $this->assertEquals($moduleinfo->assesstimestart, $dbmodinstance->assesstimestart);
74         $this->assertEquals($moduleinfo->assesstimefinish, $dbmodinstance->assesstimefinish);
75         $this->assertEquals($moduleinfo->rsstype, $dbmodinstance->rsstype);
76         $this->assertEquals($moduleinfo->rssarticles, $dbmodinstance->rssarticles);
77         $this->assertEquals($moduleinfo->trackingtype, $dbmodinstance->trackingtype);
78         $this->assertEquals($moduleinfo->maxbytes, $dbmodinstance->maxbytes);
79         $this->assertEquals($moduleinfo->maxattachments, $dbmodinstance->maxattachments);
80         $this->assertEquals($moduleinfo->blockperiod, $dbmodinstance->blockperiod);
81         $this->assertEquals($moduleinfo->blockafter, $dbmodinstance->blockafter);
82         $this->assertEquals($moduleinfo->warnafter, $dbmodinstance->warnafter);
83     }
85     /**
86      * Set assign module specific test values for calling create_module().
87      *
88      * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
89      */
90     private function assign_create_set_values(&$moduleinfo) {
91         // Specific values to the Assign module.
92         $moduleinfo->alwaysshowdescription = true;
93         $moduleinfo->submissiondrafts = true;
94         $moduleinfo->requiresubmissionstatement = true;
95         $moduleinfo->sendnotifications = true;
96         $moduleinfo->sendlatenotifications = true;
97         $moduleinfo->duedate = time() + (7 * 24 * 3600);
98         $moduleinfo->cutoffdate = time() + (7 * 24 * 3600);
99         $moduleinfo->allowsubmissionsfromdate = time();
100         $moduleinfo->teamsubmission = true;
101         $moduleinfo->requireallteammemberssubmit = true;
102         $moduleinfo->teamsubmissiongroupingid = true;
103         $moduleinfo->blindmarking = true;
104         $moduleinfo->markingworkflow = true;
105         $moduleinfo->markingallocation = true;
106         $moduleinfo->assignsubmission_onlinetext_enabled = true;
107         $moduleinfo->assignsubmission_file_enabled = true;
108         $moduleinfo->assignsubmission_file_maxfiles = 1;
109         $moduleinfo->assignsubmission_file_maxsizebytes = 1000000;
110         $moduleinfo->assignsubmission_comments_enabled = true;
111         $moduleinfo->assignfeedback_comments_enabled = true;
112         $moduleinfo->assignfeedback_offline_enabled = true;
113         $moduleinfo->assignfeedback_file_enabled = true;
115         // Advanced grading.
116         $gradingmethods = grading_manager::available_methods();
117         $moduleinfo->advancedgradingmethod_submissions = current(array_keys($gradingmethods));
118     }
120     /**
121      * Execute test asserts on the saved DB data by create_module($assign).
122      *
123      * @param object $moduleinfo - the specific assign module values that were used to create an assign module.
124      * @param object $dbmodinstance - the DB values of the created assign module.
125      */
126     private function assign_create_run_asserts($moduleinfo, $dbmodinstance) {
127         global $DB;
129         $this->assertEquals($moduleinfo->alwaysshowdescription, $dbmodinstance->alwaysshowdescription);
130         $this->assertEquals($moduleinfo->submissiondrafts, $dbmodinstance->submissiondrafts);
131         $this->assertEquals($moduleinfo->requiresubmissionstatement, $dbmodinstance->requiresubmissionstatement);
132         $this->assertEquals($moduleinfo->sendnotifications, $dbmodinstance->sendnotifications);
133         $this->assertEquals($moduleinfo->duedate, $dbmodinstance->duedate);
134         $this->assertEquals($moduleinfo->cutoffdate, $dbmodinstance->cutoffdate);
135         $this->assertEquals($moduleinfo->allowsubmissionsfromdate, $dbmodinstance->allowsubmissionsfromdate);
136         $this->assertEquals($moduleinfo->teamsubmission, $dbmodinstance->teamsubmission);
137         $this->assertEquals($moduleinfo->requireallteammemberssubmit, $dbmodinstance->requireallteammemberssubmit);
138         $this->assertEquals($moduleinfo->teamsubmissiongroupingid, $dbmodinstance->teamsubmissiongroupingid);
139         $this->assertEquals($moduleinfo->blindmarking, $dbmodinstance->blindmarking);
140         $this->assertEquals($moduleinfo->markingworkflow, $dbmodinstance->markingworkflow);
141         $this->assertEquals($moduleinfo->markingallocation, $dbmodinstance->markingallocation);
142         // The goal not being to fully test assign_add_instance() we'll stop here for the assign tests - to avoid too many DB queries.
144         // Advanced grading.
145         $contextmodule = context_module::instance($dbmodinstance->id);
146         $advancedgradingmethod = $DB->get_record('grading_areas',
147             array('contextid' => $contextmodule->id,
148                 'activemethod' => $moduleinfo->advancedgradingmethod_submissions));
149         $this->assertEquals($moduleinfo->advancedgradingmethod_submissions, $advancedgradingmethod);
150     }
152     /**
153      * Run some asserts test for a specific module for the function create_module().
154      *
155      * The function has been created (and is called) for $this->test_create_module().
156      * Note that the call to MODULE_create_set_values and MODULE_create_run_asserts are done after the common set values/run asserts.
157      * So if you want, you can overwrite the default values/asserts in the respective functions.
158      * @param string $modulename Name of the module ('forum', 'assign', 'book'...).
159      */
160     private function create_specific_module_test($modulename) {
161         global $DB, $CFG;
163         $this->resetAfterTest(true);
165         $this->setAdminUser();
167         // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
168         require_once($CFG->dirroot.'/mod/'. $modulename .'/lib.php');
170         // Enable avaibility.
171         // If not enabled all conditional fields will be ignored.
172         set_config('enableavailability', 1);
174         // Enable course completion.
175         // If not enabled all completion settings will be ignored.
176         set_config('enablecompletion', COMPLETION_ENABLED);
178         // Enable forum RSS feeds.
179         set_config('enablerssfeeds', 1);
180         set_config('forum_enablerssfeeds', 1);
182         $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED),
183            array('createsections'=>true));
185         $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
187         // Create assign module instance for test.
188         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
189         $params['course'] = $course->id;
190         $instance = $generator->create_instance($params);
191         $assigncm = get_coursemodule_from_instance('assign', $instance->id);
193         // Module test values.
194         $moduleinfo = new stdClass();
196         // Always mandatory generic values to any module.
197         $moduleinfo->modulename = $modulename;
198         $moduleinfo->section = 1; // This is the section number in the course. Not the section id in the database.
199         $moduleinfo->course = $course->id;
200         $moduleinfo->groupingid = $grouping->id;
201         $moduleinfo->groupmembersonly = 0;
202         $moduleinfo->visible = true;
204         // Sometimes optional generic values for some modules.
205         $moduleinfo->name = 'My test module';
206         $moduleinfo->showdescription = 1; // standard boolean
207         require_once($CFG->libdir . '/gradelib.php');
208         $gradecats = grade_get_categories_menu($moduleinfo->course, false);
209         $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
210         $moduleinfo->gradecat = $gradecatid;
211         $moduleinfo->groupmode = VISIBLEGROUPS;
212         $moduleinfo->cmidnumber = 'idnumber_XXX';
214         // Completion common to all module.
215         $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC;
216         $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED;
217         $moduleinfo->completiongradeitemnumber = 1;
218         $moduleinfo->completionexpected = time() + (7 * 24 * 3600);
220         // Conditional activity.
221         $moduleinfo->availablefrom = time();
222         $moduleinfo->availableuntil = time() + (7 * 24 * 3600);
223         $moduleinfo->showavailability = CONDITION_STUDENTVIEW_SHOW;
224         $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself.
225         $moduleinfo->conditiongradegroup = array(array('conditiongradeitemid' => $coursegradeitem->id, 'conditiongrademin' => 10, 'conditiongrademax' => 80));
226         $moduleinfo->conditionfieldgroup = array(array('conditionfield' => 'email', 'conditionfieldoperator' => OP_CONTAINS, 'conditionfieldvalue' => '@'));
227         $moduleinfo->conditioncompletiongroup = array(array('conditionsourcecmid' => $assigncm->id, 'conditionrequiredcompletion' => COMPLETION_COMPLETE)); // "conditionsourcecmid == 0" => none
229         // Grading and Advanced grading.
230         require_once($CFG->dirroot . '/rating/lib.php');
231         $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE;
232         $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number.
233         $moduleinfo->assesstimestart = time();
234         $moduleinfo->assesstimefinish = time() + (7 * 24 * 3600);
236         // RSS.
237         $moduleinfo->rsstype = 2;
238         $moduleinfo->rssarticles = 10;
240         // Optional intro editor (depends of module).
241         $draftid_editor = 0;
242         file_prepare_draft_area($draftid_editor, null, null, null, null);
243         $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
245         // Following is the advanced grading method area called 'submissions' for the 'assign' module.
246         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
247             $moduleinfo->grade = 100;
248         }
250         // Plagiarism form values.
251         // No plagiarism plugin installed by default. Use this space to make your own test.
253         // Values specific to the module.
254         $modulesetvalues = $modulename.'_create_set_values';
255         $this->$modulesetvalues($moduleinfo);
257         // Create the module.
258         $result = create_module($moduleinfo);
260         // Retrieve the module info.
261         $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance));
262         $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance);
263         // We passed the course section number to create_courses but $dbcm contain the section id.
264         // We need to retrieve the db course section number.
265         $section = $DB->get_record('course_sections', array('course' => $dbcm->course, 'id' => $dbcm->section));
266         // Retrieve the grade item.
267         $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course,
268             'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename));
270         // Compare the values common to all module instances.
271         $this->assertEquals($moduleinfo->modulename, $dbcm->modname);
272         $this->assertEquals($moduleinfo->section, $section->section);
273         $this->assertEquals($moduleinfo->course, $dbcm->course);
274         $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
275         $this->assertEquals($moduleinfo->groupmembersonly, $dbcm->groupmembersonly);
276         $this->assertEquals($moduleinfo->visible, $dbcm->visible);
277         $this->assertEquals($moduleinfo->completion, $dbcm->completion);
278         $this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
279         $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber);
280         $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected);
281         $this->assertEquals($moduleinfo->availablefrom, $dbcm->availablefrom);
282         $this->assertEquals($moduleinfo->availableuntil, $dbcm->availableuntil);
283         $this->assertEquals($moduleinfo->showavailability, $dbcm->showavailability);
284         $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription);
285         $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode);
286         $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber);
287         $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid);
289         // Optional grade testing.
290         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
291             $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade);
292         }
294         // Some optional (but quite common) to some module.
295         $this->assertEquals($moduleinfo->name, $dbmodinstance->name);
296         $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro);
297         $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat);
299         // Common values when conditional activity is enabled.
300         foreach ($moduleinfo->conditionfieldgroup as $fieldgroup) {
301             $isfieldgroupsaved = $DB->count_records('course_modules_avail_fields', array('coursemoduleid' => $dbcm->id,
302                 'userfield' => $fieldgroup['conditionfield'], 'operator' => $fieldgroup['conditionfieldoperator'],
303                 'value' => $fieldgroup['conditionfieldvalue']));
304             $this->assertEquals(1, $isfieldgroupsaved);
305         }
306         foreach ($moduleinfo->conditiongradegroup as $gradegroup) {
307             $isgradegroupsaved = $DB->count_records('course_modules_availability', array('coursemoduleid' => $dbcm->id,
308                 'grademin' => $gradegroup['conditiongrademin'], 'grademax' => $gradegroup['conditiongrademax'],
309                 'gradeitemid' => $gradegroup['conditiongradeitemid']));
310             $this->assertEquals(1, $isgradegroupsaved);
311         }
312         foreach ($moduleinfo->conditioncompletiongroup as $completiongroup) {
313             $iscompletiongroupsaved = $DB->count_records('course_modules_availability', array('coursemoduleid' => $dbcm->id,
314                 'sourcecmid' => $completiongroup['conditionsourcecmid'], 'requiredcompletion' => $completiongroup['conditionrequiredcompletion']));
315             $this->assertEquals(1, $iscompletiongroupsaved);
316         }
318         // Test specific to the module.
319         $modulerunasserts = $modulename.'_create_run_asserts';
320         $this->$modulerunasserts($moduleinfo, $dbmodinstance);
321         return $moduleinfo;
322     }
324     /**
325      * Test create_module() for multiple modules defined in the $modules array (first declaration of the function).
326      */
327     public function test_create_module() {
328         // Add the module name you want to test here.
329         // Create the match MODULENAME_create_set_values() and MODULENAME_create_run_asserts().
330         $modules = array('forum', 'assign');
331         // Run all tests.
332         foreach ($modules as $modulename) {
333             $this->create_specific_module_test($modulename);
334         }
335     }
337     /**
338      * Test update_module() for multiple modules defined in the $modules array (first declaration of the function).
339      */
340     public function test_update_module() {
341         // Add the module name you want to test here.
342         // Create the match MODULENAME_update_set_values() and MODULENAME_update_run_asserts().
343         $modules = array('forum');
344         // Run all tests.
345         foreach ($modules as $modulename) {
346             $this->update_specific_module_test($modulename);
347         }
348     }
350     /**
351      * Set forum specific test values for calling update_module().
352      *
353      * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
354      */
355     private function forum_update_set_values(&$moduleinfo) {
356         // Completion specific to forum - optional.
357         $moduleinfo->completionposts = 3;
358         $moduleinfo->completiondiscussions = 1;
359         $moduleinfo->completionreplies = 2;
361         // Specific values to the Forum module.
362         $moduleinfo->forcesubscribe = FORUM_INITIALSUBSCRIBE;
363         $moduleinfo->type = 'single';
364         $moduleinfo->trackingtype = FORUM_TRACKING_FORCED;
365         $moduleinfo->maxbytes = 10240;
366         $moduleinfo->maxattachments = 2;
368         // Post threshold for blocking - specific to forum.
369         $moduleinfo->blockperiod = 60*60*24;
370         $moduleinfo->blockafter = 10;
371         $moduleinfo->warnafter = 5;
372     }
374     /**
375      * Execute test asserts on the saved DB data by update_module($forum).
376      *
377      * @param object $moduleinfo - the specific forum values that were used to update a forum.
378      * @param object $dbmodinstance - the DB values of the updated forum.
379      */
380     private function forum_update_run_asserts($moduleinfo, $dbmodinstance) {
381         // Compare values specific to forums.
382         $this->assertEquals($moduleinfo->forcesubscribe, $dbmodinstance->forcesubscribe);
383         $this->assertEquals($moduleinfo->type, $dbmodinstance->type);
384         $this->assertEquals($moduleinfo->assessed, $dbmodinstance->assessed);
385         $this->assertEquals($moduleinfo->completionposts, $dbmodinstance->completionposts);
386         $this->assertEquals($moduleinfo->completiondiscussions, $dbmodinstance->completiondiscussions);
387         $this->assertEquals($moduleinfo->completionreplies, $dbmodinstance->completionreplies);
388         $this->assertEquals($moduleinfo->scale, $dbmodinstance->scale);
389         $this->assertEquals($moduleinfo->assesstimestart, $dbmodinstance->assesstimestart);
390         $this->assertEquals($moduleinfo->assesstimefinish, $dbmodinstance->assesstimefinish);
391         $this->assertEquals($moduleinfo->rsstype, $dbmodinstance->rsstype);
392         $this->assertEquals($moduleinfo->rssarticles, $dbmodinstance->rssarticles);
393         $this->assertEquals($moduleinfo->trackingtype, $dbmodinstance->trackingtype);
394         $this->assertEquals($moduleinfo->maxbytes, $dbmodinstance->maxbytes);
395         $this->assertEquals($moduleinfo->maxattachments, $dbmodinstance->maxattachments);
396         $this->assertEquals($moduleinfo->blockperiod, $dbmodinstance->blockperiod);
397         $this->assertEquals($moduleinfo->blockafter, $dbmodinstance->blockafter);
398         $this->assertEquals($moduleinfo->warnafter, $dbmodinstance->warnafter);
399     }
403     /**
404      * Test a specific type of module.
405      *
406      * @param string $modulename - the module name to test
407      */
408     private function update_specific_module_test($modulename) {
409         global $DB, $CFG;
411         $this->resetAfterTest(true);
413         $this->setAdminUser();
415         // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
416         require_once($CFG->dirroot.'/mod/'. $modulename .'/lib.php');
418         // Enable avaibility.
419         // If not enabled all conditional fields will be ignored.
420         set_config('enableavailability', 1);
422         // Enable course completion.
423         // If not enabled all completion settings will be ignored.
424         set_config('enablecompletion', COMPLETION_ENABLED);
426         // Enable forum RSS feeds.
427         set_config('enablerssfeeds', 1);
428         set_config('forum_enablerssfeeds', 1);
430         $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED),
431            array('createsections'=>true));
433         $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
435         // Create assign module instance for testing gradeitem.
436         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
437         $params['course'] = $course->id;
438         $instance = $generator->create_instance($params);
439         $assigncm = get_coursemodule_from_instance('assign', $instance->id);
441         // Create the test forum to update.
442         $initvalues = new stdClass();
443         $initvalues->introformat = FORMAT_HTML;
444         $initvalues->course = $course->id;
445         $forum = self::getDataGenerator()->create_module('forum', $initvalues);
447         // Retrieve course module.
448         $cm = get_coursemodule_from_instance('forum', $forum->id);
450         // Module test values.
451         $moduleinfo = new stdClass();
453         // Always mandatory generic values to any module.
454         $moduleinfo->coursemodule = $cm->id;
455         $moduleinfo->modulename = $modulename;
456         $moduleinfo->course = $course->id;
457         $moduleinfo->groupingid = $grouping->id;
458         $moduleinfo->groupmembersonly = 0;
459         $moduleinfo->visible = true;
461         // Sometimes optional generic values for some modules.
462         $moduleinfo->name = 'My test module';
463         $moduleinfo->showdescription = 1; // standard boolean
464         require_once($CFG->libdir . '/gradelib.php');
465         $gradecats = grade_get_categories_menu($moduleinfo->course, false);
466         $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
467         $moduleinfo->gradecat = $gradecatid;
468         $moduleinfo->groupmode = VISIBLEGROUPS;
469         $moduleinfo->cmidnumber = 'idnumber_XXX';
471         // Completion common to all module.
472         $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC;
473         $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED;
474         $moduleinfo->completiongradeitemnumber = 1;
475         $moduleinfo->completionexpected = time() + (7 * 24 * 3600);
476         $moduleinfo->completionunlocked = 1;
478         // Conditional activity.
479         $moduleinfo->availablefrom = time();
480         $moduleinfo->availableuntil = time() + (7 * 24 * 3600);
481         $moduleinfo->showavailability = CONDITION_STUDENTVIEW_SHOW;
482         $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself.
483         $moduleinfo->conditiongradegroup = array(array('conditiongradeitemid' => $coursegradeitem->id, 'conditiongrademin' => 10, 'conditiongrademax' => 80));
484         $moduleinfo->conditionfieldgroup = array(array('conditionfield' => 'email', 'conditionfieldoperator' => OP_CONTAINS, 'conditionfieldvalue' => '@'));
485         $moduleinfo->conditioncompletiongroup = array(array('conditionsourcecmid' => $assigncm->id, 'conditionrequiredcompletion' => COMPLETION_COMPLETE)); // "conditionsourcecmid == 0" => none
487         // Grading and Advanced grading.
488         require_once($CFG->dirroot . '/rating/lib.php');
489         $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE;
490         $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number.
491         $moduleinfo->assesstimestart = time();
492         $moduleinfo->assesstimefinish = time() + (7 * 24 * 3600);
494         // RSS.
495         $moduleinfo->rsstype = 2;
496         $moduleinfo->rssarticles = 10;
498         // Optional intro editor (depends of module).
499         $draftid_editor = 0;
500         file_prepare_draft_area($draftid_editor, null, null, null, null);
501         $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
503         // Following is the advanced grading method area called 'submissions' for the 'assign' module.
504         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
505             $moduleinfo->grade = 100;
506         }
507         // Plagiarism form values.
508         // No plagiarism plugin installed by default. Use this space to make your own test.
510         // Values specific to the module.
511         $modulesetvalues = $modulename.'_update_set_values';
512         $this->$modulesetvalues($moduleinfo);
514         // Create the module.
515         $result = update_module($moduleinfo);
517         // Retrieve the module info.
518         $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance));
519         $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance);
520         // Retrieve the grade item.
521         $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course,
522             'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename));
524         // Compare the values common to all module instances.
525         $this->assertEquals($moduleinfo->modulename, $dbcm->modname);
526         $this->assertEquals($moduleinfo->course, $dbcm->course);
527         $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
528         $this->assertEquals($moduleinfo->groupmembersonly, $dbcm->groupmembersonly);
529         $this->assertEquals($moduleinfo->visible, $dbcm->visible);
530         $this->assertEquals($moduleinfo->completion, $dbcm->completion);
531         $this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
532         $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber);
533         $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected);
534         $this->assertEquals($moduleinfo->availablefrom, $dbcm->availablefrom);
535         $this->assertEquals($moduleinfo->availableuntil, $dbcm->availableuntil);
536         $this->assertEquals($moduleinfo->showavailability, $dbcm->showavailability);
537         $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription);
538         $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode);
539         $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber);
540         $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid);
542         // Optional grade testing.
543         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
544             $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade);
545         }
547         // Some optional (but quite common) to some module.
548         $this->assertEquals($moduleinfo->name, $dbmodinstance->name);
549         $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro);
550         $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat);
552         // Common values when conditional activity is enabled.
553         foreach ($moduleinfo->conditionfieldgroup as $fieldgroup) {
554             $isfieldgroupsaved = $DB->count_records('course_modules_avail_fields', array('coursemoduleid' => $dbcm->id,
555                 'userfield' => $fieldgroup['conditionfield'], 'operator' => $fieldgroup['conditionfieldoperator'],
556                 'value' => $fieldgroup['conditionfieldvalue']));
557             $this->assertEquals(1, $isfieldgroupsaved);
558         }
559         foreach ($moduleinfo->conditiongradegroup as $gradegroup) {
560             $isgradegroupsaved = $DB->count_records('course_modules_availability', array('coursemoduleid' => $dbcm->id,
561                 'grademin' => $gradegroup['conditiongrademin'], 'grademax' => $gradegroup['conditiongrademax'],
562                 'gradeitemid' => $gradegroup['conditiongradeitemid']));
563             $this->assertEquals(1, $isgradegroupsaved);
564         }
565         foreach ($moduleinfo->conditioncompletiongroup as $completiongroup) {
566             $iscompletiongroupsaved = $DB->count_records('course_modules_availability', array('coursemoduleid' => $dbcm->id,
567                 'sourcecmid' => $completiongroup['conditionsourcecmid'], 'requiredcompletion' => $completiongroup['conditionrequiredcompletion']));
568             $this->assertEquals(1, $iscompletiongroupsaved);
569         }
571         // Test specific to the module.
572         $modulerunasserts = $modulename.'_update_run_asserts';
573         $this->$modulerunasserts($moduleinfo, $dbmodinstance);
574         return $moduleinfo;
575    }
578     public function test_create_course() {
579         global $DB;
580         $this->resetAfterTest(true);
581         $defaultcategory = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
583         $course = new stdClass();
584         $course->fullname = 'Apu loves Unit Təsts';
585         $course->shortname = 'Spread the lŭve';
586         $course->idnumber = '123';
587         $course->summary = 'Awesome!';
588         $course->summaryformat = FORMAT_PLAIN;
589         $course->format = 'topics';
590         $course->newsitems = 0;
591         $course->numsections = 5;
592         $course->category = $defaultcategory;
593         $original = (array) $course;
595         $created = create_course($course);
596         $context = context_course::instance($created->id);
598         // Compare original and created.
599         $this->assertEquals($original, array_intersect_key((array) $created, $original));
601         // Ensure default section is created.
602         $sectioncreated = $DB->record_exists('course_sections', array('course' => $created->id, 'section' => 0));
603         $this->assertTrue($sectioncreated);
605         // Ensure blocks have been associated to the course.
606         $blockcount = $DB->count_records('block_instances', array('parentcontextid' => $context->id));
607         $this->assertGreaterThan(0, $blockcount);
609         // Ensure that the shortname isn't duplicated.
610         try {
611             $created = create_course($course);
612             $this->fail('Exception expected');
613         } catch (moodle_exception $e) {
614             $this->assertSame(get_string('shortnametaken', 'error', $course->shortname), $e->getMessage());
615         }
617         // Ensure that the idnumber isn't duplicated.
618         $course->shortname .= '1';
619         try {
620             $created = create_course($course);
621             $this->fail('Exception expected');
622         } catch (moodle_exception $e) {
623             $this->assertSame(get_string('courseidnumbertaken', 'error', $course->idnumber), $e->getMessage());
624         }
625     }
627     public function test_create_course_with_generator() {
628         global $DB;
629         $this->resetAfterTest(true);
630         $course = $this->getDataGenerator()->create_course();
632         // Ensure default section is created.
633         $sectioncreated = $DB->record_exists('course_sections', array('course' => $course->id, 'section' => 0));
634         $this->assertTrue($sectioncreated);
635     }
637     public function test_create_course_sections() {
638         global $DB;
639         $this->resetAfterTest(true);
641         $course = $this->getDataGenerator()->create_course(
642                 array('shortname' => 'GrowingCourse',
643                     'fullname' => 'Growing Course',
644                     'numsections' => 5),
645                 array('createsections' => true));
647         // Ensure all 6 (0-5) sections were created and course content cache works properly
648         $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
649         $this->assertEquals(range(0, $course->numsections), $sectionscreated);
651         // this will do nothing, section already exists
652         $this->assertFalse(course_create_sections_if_missing($course, $course->numsections));
654         // this will create new section
655         $this->assertTrue(course_create_sections_if_missing($course, $course->numsections + 1));
657         // Ensure all 7 (0-6) sections were created and modinfo/sectioninfo cache works properly
658         $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
659         $this->assertEquals(range(0, $course->numsections + 1), $sectionscreated);
660     }
662     public function test_update_course() {
663         global $DB;
665         $this->resetAfterTest();
667         $defaultcategory = $DB->get_field_select('course_categories', 'MIN(id)', 'parent = 0');
669         $course = new stdClass();
670         $course->fullname = 'Apu loves Unit Təsts';
671         $course->shortname = 'test1';
672         $course->idnumber = '1';
673         $course->summary = 'Awesome!';
674         $course->summaryformat = FORMAT_PLAIN;
675         $course->format = 'topics';
676         $course->newsitems = 0;
677         $course->numsections = 5;
678         $course->category = $defaultcategory;
680         $created = create_course($course);
681         // Ensure the checks only work on idnumber/shortname that are not already ours.
682         update_course($created);
684         $course->shortname = 'test2';
685         $course->idnumber = '2';
687         $created2 = create_course($course);
689         // Test duplicate idnumber.
690         $created2->idnumber = '1';
691         try {
692             update_course($created2);
693             $this->fail('Expected exception when trying to update a course with duplicate idnumber');
694         } catch (moodle_exception $e) {
695             $this->assertEquals(get_string('courseidnumbertaken', 'error', $created2->idnumber), $e->getMessage());
696         }
698         // Test duplicate shortname.
699         $created2->idnumber = '2';
700         $created2->shortname = 'test1';
701         try {
702             update_course($created2);
703             $this->fail('Expected exception when trying to update a course with a duplicate shortname');
704         } catch (moodle_exception $e) {
705             $this->assertEquals(get_string('shortnametaken', 'error', $created2->shortname), $e->getMessage());
706         }
707     }
709     public function test_course_add_cm_to_section() {
710         global $DB;
711         $this->resetAfterTest(true);
713         // Create course with 1 section.
714         $course = $this->getDataGenerator()->create_course(
715                 array('shortname' => 'GrowingCourse',
716                     'fullname' => 'Growing Course',
717                     'numsections' => 1),
718                 array('createsections' => true));
720         // Trash modinfo.
721         rebuild_course_cache($course->id, true);
723         // Create some cms for testing.
724         $cmids = array();
725         for ($i=0; $i<4; $i++) {
726             $cmids[$i] = $DB->insert_record('course_modules', array('course' => $course->id));
727         }
729         // Add it to section that exists.
730         course_add_cm_to_section($course, $cmids[0], 1);
732         // Check it got added to sequence.
733         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 1));
734         $this->assertEquals($cmids[0], $sequence);
736         // Add a second, this time using courseid variant of parameters.
737         $coursecacherev = $DB->get_field('course', 'cacherev', array('id' => $course->id));
738         course_add_cm_to_section($course->id, $cmids[1], 1);
739         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 1));
740         $this->assertEquals($cmids[0] . ',' . $cmids[1], $sequence);
742         // Check that modinfo cache was reset but not rebuilt (important for performance if calling repeatedly).
743         $this->assertGreaterThan($coursecacherev, $DB->get_field('course', 'cacherev', array('id' => $course->id)));
744         $this->assertEmpty(cache::make('core', 'coursemodinfo')->get($course->id));
746         // Add one to section that doesn't exist (this might rebuild modinfo).
747         course_add_cm_to_section($course, $cmids[2], 2);
748         $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id)));
749         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 2));
750         $this->assertEquals($cmids[2], $sequence);
752         // Add using the 'before' option.
753         course_add_cm_to_section($course, $cmids[3], 2, $cmids[2]);
754         $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id)));
755         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 2));
756         $this->assertEquals($cmids[3] . ',' . $cmids[2], $sequence);
757     }
759     public function test_reorder_sections() {
760         global $DB;
761         $this->resetAfterTest(true);
763         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
764         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
765         $oldsections = array();
766         $sections = array();
767         foreach ($DB->get_records('course_sections', array('course'=>$course->id), 'id') as $section) {
768             $oldsections[$section->section] = $section->id;
769             $sections[$section->id] = $section->section;
770         }
771         ksort($oldsections);
773         $neworder = reorder_sections($sections, 2, 4);
774         $neworder = array_keys($neworder);
775         $this->assertEquals($oldsections[0], $neworder[0]);
776         $this->assertEquals($oldsections[1], $neworder[1]);
777         $this->assertEquals($oldsections[2], $neworder[4]);
778         $this->assertEquals($oldsections[3], $neworder[2]);
779         $this->assertEquals($oldsections[4], $neworder[3]);
780         $this->assertEquals($oldsections[5], $neworder[5]);
781         $this->assertEquals($oldsections[6], $neworder[6]);
783         $neworder = reorder_sections($sections, 4, 2);
784         $neworder = array_keys($neworder);
785         $this->assertEquals($oldsections[0], $neworder[0]);
786         $this->assertEquals($oldsections[1], $neworder[1]);
787         $this->assertEquals($oldsections[2], $neworder[3]);
788         $this->assertEquals($oldsections[3], $neworder[4]);
789         $this->assertEquals($oldsections[4], $neworder[2]);
790         $this->assertEquals($oldsections[5], $neworder[5]);
791         $this->assertEquals($oldsections[6], $neworder[6]);
793         $neworder = reorder_sections(1, 2, 4);
794         $this->assertFalse($neworder);
795     }
797     public function test_move_section_down() {
798         global $DB;
799         $this->resetAfterTest(true);
801         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
802         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
803         $oldsections = array();
804         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
805             $oldsections[$section->section] = $section->id;
806         }
807         ksort($oldsections);
809         // Test move section down..
810         move_section_to($course, 2, 4);
811         $sections = array();
812         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
813             $sections[$section->section] = $section->id;
814         }
815         ksort($sections);
817         $this->assertEquals($oldsections[0], $sections[0]);
818         $this->assertEquals($oldsections[1], $sections[1]);
819         $this->assertEquals($oldsections[2], $sections[4]);
820         $this->assertEquals($oldsections[3], $sections[2]);
821         $this->assertEquals($oldsections[4], $sections[3]);
822         $this->assertEquals($oldsections[5], $sections[5]);
823         $this->assertEquals($oldsections[6], $sections[6]);
824     }
826     public function test_move_section_up() {
827         global $DB;
828         $this->resetAfterTest(true);
830         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
831         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
832         $oldsections = array();
833         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
834             $oldsections[$section->section] = $section->id;
835         }
836         ksort($oldsections);
838         // Test move section up..
839         move_section_to($course, 6, 4);
840         $sections = array();
841         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
842             $sections[$section->section] = $section->id;
843         }
844         ksort($sections);
846         $this->assertEquals($oldsections[0], $sections[0]);
847         $this->assertEquals($oldsections[1], $sections[1]);
848         $this->assertEquals($oldsections[2], $sections[2]);
849         $this->assertEquals($oldsections[3], $sections[3]);
850         $this->assertEquals($oldsections[4], $sections[5]);
851         $this->assertEquals($oldsections[5], $sections[6]);
852         $this->assertEquals($oldsections[6], $sections[4]);
853     }
855     public function test_move_section_marker() {
856         global $DB;
857         $this->resetAfterTest(true);
859         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
860         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
862         // Set course marker to the section we are going to move..
863         course_set_marker($course->id, 2);
864         // Verify that the course marker is set correctly.
865         $course = $DB->get_record('course', array('id' => $course->id));
866         $this->assertEquals(2, $course->marker);
868         // Test move the marked section down..
869         move_section_to($course, 2, 4);
871         // Verify that the coruse marker has been moved along with the section..
872         $course = $DB->get_record('course', array('id' => $course->id));
873         $this->assertEquals(4, $course->marker);
875         // Test move the marked section up..
876         move_section_to($course, 4, 3);
878         // Verify that the course marker has been moved along with the section..
879         $course = $DB->get_record('course', array('id' => $course->id));
880         $this->assertEquals(3, $course->marker);
882         // Test moving a non-marked section above the marked section..
883         move_section_to($course, 4, 2);
885         // Verify that the course marker has been moved down to accomodate..
886         $course = $DB->get_record('course', array('id' => $course->id));
887         $this->assertEquals(4, $course->marker);
889         // Test moving a non-marked section below the marked section..
890         move_section_to($course, 3, 6);
892         // Verify that the course marker has been up to accomodate..
893         $course = $DB->get_record('course', array('id' => $course->id));
894         $this->assertEquals(3, $course->marker);
895     }
897     public function test_get_course_display_name_for_list() {
898         global $CFG;
899         $this->resetAfterTest(true);
901         $course = $this->getDataGenerator()->create_course(array('shortname' => 'FROG101', 'fullname' => 'Introduction to pond life'));
903         $CFG->courselistshortnames = 0;
904         $this->assertEquals('Introduction to pond life', get_course_display_name_for_list($course));
906         $CFG->courselistshortnames = 1;
907         $this->assertEquals('FROG101 Introduction to pond life', get_course_display_name_for_list($course));
908     }
910     public function test_move_module_in_course() {
911         global $DB;
913         $this->resetAfterTest(true);
914         // Setup fixture
915         $course = $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections' => true));
916         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
918         $cms = get_fast_modinfo($course)->get_cms();
919         $cm = reset($cms);
921         $newsection = get_fast_modinfo($course)->get_section_info(3);
922         $oldsectionid = $cm->section;
924         // Perform the move
925         moveto_module($cm, $newsection);
927         $cms = get_fast_modinfo($course)->get_cms();
928         $cm = reset($cms);
930         // Check that the cached modinfo contains the correct section info
931         $modinfo = get_fast_modinfo($course);
932         $this->assertTrue(empty($modinfo->sections[0]));
933         $this->assertFalse(empty($modinfo->sections[3]));
935         // Check that the old section's sequence no longer contains this ID
936         $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
937         $oldsequences = explode(',', $newsection->sequence);
938         $this->assertFalse(in_array($cm->id, $oldsequences));
940         // Check that the new section's sequence now contains this ID
941         $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
942         $newsequences = explode(',', $newsection->sequence);
943         $this->assertTrue(in_array($cm->id, $newsequences));
945         // Check that the section number has been changed in the cm
946         $this->assertEquals($newsection->id, $cm->section);
949         // Perform a second move as some issues were only seen on the second move
950         $newsection = get_fast_modinfo($course)->get_section_info(2);
951         $oldsectionid = $cm->section;
952         moveto_module($cm, $newsection);
954         $cms = get_fast_modinfo($course)->get_cms();
955         $cm = reset($cms);
957         // Check that the cached modinfo contains the correct section info
958         $modinfo = get_fast_modinfo($course);
959         $this->assertTrue(empty($modinfo->sections[0]));
960         $this->assertFalse(empty($modinfo->sections[2]));
962         // Check that the old section's sequence no longer contains this ID
963         $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
964         $oldsequences = explode(',', $newsection->sequence);
965         $this->assertFalse(in_array($cm->id, $oldsequences));
967         // Check that the new section's sequence now contains this ID
968         $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
969         $newsequences = explode(',', $newsection->sequence);
970         $this->assertTrue(in_array($cm->id, $newsequences));
971     }
973     public function test_module_visibility() {
974         $this->setAdminUser();
975         $this->resetAfterTest(true);
977         // Create course and modules.
978         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
979         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
980         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(), 'course' => $course->id));
981         $modules = compact('forum', 'assign');
983         // Hiding the modules.
984         foreach ($modules as $mod) {
985             set_coursemodule_visible($mod->cmid, 0);
986             $this->check_module_visibility($mod, 0, 0);
987         }
989         // Showing the modules.
990         foreach ($modules as $mod) {
991             set_coursemodule_visible($mod->cmid, 1);
992             $this->check_module_visibility($mod, 1, 1);
993         }
994     }
996     public function test_section_visibility() {
997         $this->setAdminUser();
998         $this->resetAfterTest(true);
1000         // Create course.
1001         $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
1003         // Testing an empty section.
1004         $sectionnumber = 1;
1005         set_section_visible($course->id, $sectionnumber, 0);
1006         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1007         $this->assertEquals($section_info->visible, 0);
1008         set_section_visible($course->id, $sectionnumber, 1);
1009         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1010         $this->assertEquals($section_info->visible, 1);
1012         // Testing a section with visible modules.
1013         $sectionnumber = 2;
1014         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1015                 array('section' => $sectionnumber));
1016         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1017                 'course' => $course->id), array('section' => $sectionnumber));
1018         $modules = compact('forum', 'assign');
1019         set_section_visible($course->id, $sectionnumber, 0);
1020         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1021         $this->assertEquals($section_info->visible, 0);
1022         foreach ($modules as $mod) {
1023             $this->check_module_visibility($mod, 0, 1);
1024         }
1025         set_section_visible($course->id, $sectionnumber, 1);
1026         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1027         $this->assertEquals($section_info->visible, 1);
1028         foreach ($modules as $mod) {
1029             $this->check_module_visibility($mod, 1, 1);
1030         }
1032         // Testing a section with hidden modules, which should stay hidden.
1033         $sectionnumber = 3;
1034         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1035                 array('section' => $sectionnumber));
1036         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1037                 'course' => $course->id), array('section' => $sectionnumber));
1038         $modules = compact('forum', 'assign');
1039         foreach ($modules as $mod) {
1040             set_coursemodule_visible($mod->cmid, 0);
1041             $this->check_module_visibility($mod, 0, 0);
1042         }
1043         set_section_visible($course->id, $sectionnumber, 0);
1044         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1045         $this->assertEquals($section_info->visible, 0);
1046         foreach ($modules as $mod) {
1047             $this->check_module_visibility($mod, 0, 0);
1048         }
1049         set_section_visible($course->id, $sectionnumber, 1);
1050         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1051         $this->assertEquals($section_info->visible, 1);
1052         foreach ($modules as $mod) {
1053             $this->check_module_visibility($mod, 0, 0);
1054         }
1055     }
1057     /**
1058      * Helper function to assert that a module has correctly been made visible, or hidden.
1059      *
1060      * @param stdClass $mod module information
1061      * @param int $visibility the current state of the module
1062      * @param int $visibleold the current state of the visibleold property
1063      * @return void
1064      */
1065     public function check_module_visibility($mod, $visibility, $visibleold) {
1066         global $DB;
1067         $cm = get_fast_modinfo($mod->course)->get_cm($mod->cmid);
1068         $this->assertEquals($visibility, $cm->visible);
1069         $this->assertEquals($visibleold, $cm->visibleold);
1071         // Check the module grade items.
1072         $grade_items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $cm->modname,
1073                 'iteminstance' => $cm->instance, 'courseid' => $cm->course));
1074         if ($grade_items) {
1075             foreach ($grade_items as $grade_item) {
1076                 if ($visibility) {
1077                     $this->assertFalse($grade_item->is_hidden(), "$cm->modname grade_item not visible");
1078                 } else {
1079                     $this->assertTrue($grade_item->is_hidden(), "$cm->modname grade_item not hidden");
1080                 }
1081             }
1082         }
1084         // Check the events visibility.
1085         if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $cm->modname))) {
1086             foreach ($events as $event) {
1087                 $calevent = new calendar_event($event);
1088                 $this->assertEquals($visibility, $calevent->visible, "$cm->modname calendar_event visibility");
1089             }
1090         }
1091     }
1093     public function test_course_page_type_list() {
1094         global $DB;
1095         $this->resetAfterTest(true);
1097         // Create a category.
1098         $category = new stdClass();
1099         $category->name = 'Test Category';
1101         $testcategory = $this->getDataGenerator()->create_category($category);
1103         // Create a course.
1104         $course = new stdClass();
1105         $course->fullname = 'Apu loves Unit Təsts';
1106         $course->shortname = 'Spread the lŭve';
1107         $course->idnumber = '123';
1108         $course->summary = 'Awesome!';
1109         $course->summaryformat = FORMAT_PLAIN;
1110         $course->format = 'topics';
1111         $course->newsitems = 0;
1112         $course->numsections = 5;
1113         $course->category = $testcategory->id;
1115         $testcourse = $this->getDataGenerator()->create_course($course);
1117         // Create contexts.
1118         $coursecontext = context_course::instance($testcourse->id);
1119         $parentcontext = $coursecontext->get_parent_context(); // Not actually used.
1120         $pagetype = 'page-course-x'; // Not used either.
1121         $pagetypelist = course_page_type_list($pagetype, $parentcontext, $coursecontext);
1123         // Page type lists for normal courses.
1124         $testpagetypelist1 = array();
1125         $testpagetypelist1['*'] = 'Any page';
1126         $testpagetypelist1['course-*'] = 'Any course page';
1127         $testpagetypelist1['course-view-*'] = 'Any type of course main page';
1129         $this->assertEquals($testpagetypelist1, $pagetypelist);
1131         // Get the context for the front page course.
1132         $sitecoursecontext = context_course::instance(SITEID);
1133         $pagetypelist = course_page_type_list($pagetype, $parentcontext, $sitecoursecontext);
1135         // Page type list for the front page course.
1136         $testpagetypelist2 = array('*' => 'Any page');
1137         $this->assertEquals($testpagetypelist2, $pagetypelist);
1139         // Make sure that providing no current context to the function doesn't result in an error.
1140         // Calls made from generate_page_type_patterns() may provide null values.
1141         $pagetypelist = course_page_type_list($pagetype, null, null);
1142         $this->assertEquals($pagetypelist, $testpagetypelist1);
1143     }
1145     public function test_compare_activities_by_time_desc() {
1147         // Let's create some test data.
1148         $activitiesivities = array();
1149         $x = new stdClass();
1150         $x->timestamp = null;
1151         $activities[] = $x;
1153         $x = new stdClass();
1154         $x->timestamp = 1;
1155         $activities[] = $x;
1157         $x = new stdClass();
1158         $x->timestamp = 3;
1159         $activities[] = $x;
1161         $x = new stdClass();
1162         $x->timestamp = 0;
1163         $activities[] = $x;
1165         $x = new stdClass();
1166         $x->timestamp = 5;
1167         $activities[] = $x;
1169         $x = new stdClass();
1170         $activities[] = $x;
1172         $x = new stdClass();
1173         $x->timestamp = 5;
1174         $activities[] = $x;
1176         // Do the sorting.
1177         usort($activities, 'compare_activities_by_time_desc');
1179         // Let's check the result.
1180         $last = 10;
1181         foreach($activities as $activity) {
1182             if (empty($activity->timestamp)) {
1183                 $activity->timestamp = 0;
1184             }
1185             $this->assertLessThanOrEqual($last, $activity->timestamp);
1186         }
1187     }
1189     public function test_compare_activities_by_time_asc() {
1191         // Let's create some test data.
1192         $activities = array();
1193         $x = new stdClass();
1194         $x->timestamp = null;
1195         $activities[] = $x;
1197         $x = new stdClass();
1198         $x->timestamp = 1;
1199         $activities[] = $x;
1201         $x = new stdClass();
1202         $x->timestamp = 3;
1203         $activities[] = $x;
1205         $x = new stdClass();
1206         $x->timestamp = 0;
1207         $activities[] = $x;
1209         $x = new stdClass();
1210         $x->timestamp = 5;
1211         $activities[] = $x;
1213         $x = new stdClass();
1214         $activities[] = $x;
1216         $x = new stdClass();
1217         $x->timestamp = 5;
1218         $activities[] = $x;
1220         // Do the sorting.
1221         usort($activities, 'compare_activities_by_time_asc');
1223         // Let's check the result.
1224         $last = 0;
1225         foreach($activities as $activity) {
1226             if (empty($activity->timestamp)) {
1227                 $activity->timestamp = 0;
1228             }
1229             $this->assertGreaterThanOrEqual($last, $activity->timestamp);
1230         }
1231     }
1233     /**
1234      * Tests moving a module between hidden/visible sections and
1235      * verifies that the course/module visiblity seettings are
1236      * retained.
1237      */
1238     public function test_moveto_module_between_hidden_sections() {
1239         global $DB;
1241         $this->resetAfterTest(true);
1243         $course = $this->getDataGenerator()->create_course(array('numsections' => 4), array('createsections' => true));
1244         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1245         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
1246         $quiz= $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
1248         // Set the page as hidden
1249         set_coursemodule_visible($page->cmid, 0);
1251         // Set sections 3 as hidden.
1252         set_section_visible($course->id, 3, 0);
1254         $modinfo = get_fast_modinfo($course);
1256         $hiddensection = $modinfo->get_section_info(3);
1257         // New section is definitely not visible:
1258         $this->assertEquals($hiddensection->visible, 0);
1260         $forumcm = $modinfo->cms[$forum->cmid];
1261         $pagecm = $modinfo->cms[$page->cmid];
1263         // Move the forum and the page to a hidden section, make sure moveto_module returns 0 as new visibility state.
1264         $this->assertEquals(0, moveto_module($forumcm, $hiddensection));
1265         $this->assertEquals(0, moveto_module($pagecm, $hiddensection));
1267         $modinfo = get_fast_modinfo($course);
1269         // Verify that forum and page have been moved to the hidden section and quiz has not.
1270         $this->assertContains($forum->cmid, $modinfo->sections[3]);
1271         $this->assertContains($page->cmid, $modinfo->sections[3]);
1272         $this->assertNotContains($quiz->cmid, $modinfo->sections[3]);
1274         // Verify that forum has been made invisible.
1275         $forumcm = $modinfo->cms[$forum->cmid];
1276         $this->assertEquals($forumcm->visible, 0);
1277         // Verify that old state has been retained.
1278         $this->assertEquals($forumcm->visibleold, 1);
1280         // Verify that page has stayed invisible.
1281         $pagecm = $modinfo->cms[$page->cmid];
1282         $this->assertEquals($pagecm->visible, 0);
1283         // Verify that old state has been retained.
1284         $this->assertEquals($pagecm->visibleold, 0);
1286         // Verify that quiz has been unaffected.
1287         $quizcm = $modinfo->cms[$quiz->cmid];
1288         $this->assertEquals($quizcm->visible, 1);
1290         // Move forum and page back to visible section.
1291         // Make sure the visibility is restored to the original value (visible for forum and hidden for page).
1292         $visiblesection = $modinfo->get_section_info(2);
1293         $this->assertEquals(1, moveto_module($forumcm, $visiblesection));
1294         $this->assertEquals(0, moveto_module($pagecm, $visiblesection));
1296         $modinfo = get_fast_modinfo($course);
1298         // Double check that forum has been made visible.
1299         $forumcm = $modinfo->cms[$forum->cmid];
1300         $this->assertEquals($forumcm->visible, 1);
1302         // Double check that page has stayed invisible.
1303         $pagecm = $modinfo->cms[$page->cmid];
1304         $this->assertEquals($pagecm->visible, 0);
1306         // Move the page in the same section (this is what mod duplicate does).
1307         // Visibility of page remains 0.
1308         $this->assertEquals(0, moveto_module($pagecm, $visiblesection, $forumcm));
1310         // Double check that the the page is still hidden.
1311         $modinfo = get_fast_modinfo($course);
1312         $pagecm = $modinfo->cms[$page->cmid];
1313         $this->assertEquals($pagecm->visible, 0);
1314     }
1316     /**
1317      * Tests moving a module around in the same section. moveto_module()
1318      * is called this way in modduplicate.
1319      */
1320     public function test_moveto_module_in_same_section() {
1321         global $DB;
1323         $this->resetAfterTest(true);
1325         $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
1326         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
1327         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1329         // Simulate inconsistent visible/visibleold values (MDL-38713).
1330         $cm = $DB->get_record('course_modules', array('id' => $page->cmid), '*', MUST_EXIST);
1331         $cm->visible = 0;
1332         $cm->visibleold = 1;
1333         $DB->update_record('course_modules', $cm);
1335         $modinfo = get_fast_modinfo($course);
1336         $forumcm = $modinfo->cms[$forum->cmid];
1337         $pagecm = $modinfo->cms[$page->cmid];
1339         // Verify that page is hidden.
1340         $this->assertEquals($pagecm->visible, 0);
1342         // Verify section 0 is where all mods added.
1343         $section = $modinfo->get_section_info(0);
1344         $this->assertEquals($section->id, $forumcm->section);
1345         $this->assertEquals($section->id, $pagecm->section);
1348         // Move the page inside the hidden section. Make sure it is hidden.
1349         $this->assertEquals(0, moveto_module($pagecm, $section, $forumcm));
1351         // Double check that the the page is still hidden.
1352         $modinfo = get_fast_modinfo($course);
1353         $pagecm = $modinfo->cms[$page->cmid];
1354         $this->assertEquals($pagecm->visible, 0);
1355     }
1357     public function test_course_delete_module() {
1358         global $DB;
1359         $this->resetAfterTest(true);
1360         $this->setAdminUser();
1362         // Create course and modules.
1363         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
1365         // Generate an assignment with due date (will generate a course event).
1366         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(), 'course' => $course->id));
1368         $cm = get_coursemodule_from_instance('assign', $assign->id);
1370         // Verify context exists.
1371         $this->assertInstanceOf('context_module', context_module::instance($cm->id, IGNORE_MISSING));
1373         // Verify event assignment event has been generated.
1374         $eventcount = $DB->count_records('event', array('instance' => $assign->id, 'modulename' => 'assign'));
1375         $this->assertEquals(1, $eventcount);
1377         // Run delete..
1378         course_delete_module($cm->id);
1380         // Verify the context has been removed.
1381         $this->assertFalse(context_module::instance($cm->id, IGNORE_MISSING));
1383         // Verify the course_module record has been deleted.
1384         $cmcount = $DB->count_records('course_modules', array('id' => $cm->id));
1385         $this->assertEmpty($cmcount);
1387         // Verify event assignment events have been removed.
1388         $eventcount = $DB->count_records('event', array('instance' => $assign->id, 'modulename' => 'assign'));
1389         $this->assertEmpty($eventcount);
1390     }
1392     /**
1393      * Test that triggering a course_created event works as expected.
1394      */
1395     public function test_course_created_event() {
1396         global $DB;
1398         $this->resetAfterTest();
1400         // Catch the events.
1401         $sink = $this->redirectEvents();
1403         // Create the course.
1404         $course = $this->getDataGenerator()->create_course();
1405         // Get course from DB for comparison.
1406         $course = $DB->get_record('course', array('id' => $course->id));
1408         // Capture the event.
1409         $events = $sink->get_events();
1410         $sink->close();
1412         // Validate the event.
1413         $event = $events[0];
1414         $this->assertInstanceOf('\core\event\course_created', $event);
1415         $this->assertEquals('course', $event->objecttable);
1416         $this->assertEquals($course->id, $event->objectid);
1417         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1418         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1419         $this->assertEquals('course_created', $event->get_legacy_eventname());
1420         $this->assertEventLegacyData($course, $event);
1421         $expectedlog = array(SITEID, 'course', 'new', 'view.php?id=' . $course->id, $course->fullname . ' (ID ' . $course->id . ')');
1422         $this->assertEventLegacyLogData($expectedlog, $event);
1423     }
1425     /**
1426      * Test that triggering a course_updated event works as expected.
1427      */
1428     public function test_course_updated_event() {
1429         global $DB;
1431         $this->resetAfterTest();
1433         // Create a course.
1434         $course = $this->getDataGenerator()->create_course();
1436         // Create a category we are going to move this course to.
1437         $category = $this->getDataGenerator()->create_category();
1439         // Create a hidden category we are going to move this course to.
1440         $categoryhidden = $this->getDataGenerator()->create_category(array('visible' => 0));
1442         // Update course and catch course_updated event.
1443         $sink = $this->redirectEvents();
1444         update_course($course);
1445         $events = $sink->get_events();
1446         $sink->close();
1448         // Get updated course information from the DB.
1449         $updatedcourse = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1450         // Validate event.
1451         $event = array_shift($events);
1452         $this->assertInstanceOf('\core\event\course_updated', $event);
1453         $this->assertEquals('course', $event->objecttable);
1454         $this->assertEquals($updatedcourse->id, $event->objectid);
1455         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1456         $this->assertEquals($updatedcourse, $event->get_record_snapshot('course', $event->objectid));
1457         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1458         $this->assertEventLegacyData($updatedcourse, $event);
1459         $expectedlog = array($updatedcourse->id, 'course', 'update', 'edit.php?id=' . $course->id, $course->id);
1460         $this->assertEventLegacyLogData($expectedlog, $event);
1462         // Move course and catch course_updated event.
1463         $sink = $this->redirectEvents();
1464         move_courses(array($course->id), $category->id);
1465         $events = $sink->get_events();
1466         $sink->close();
1468         // Return the moved course information from the DB.
1469         $movedcourse = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1470         // Validate event.
1471         $event = array_shift($events);
1472         $this->assertInstanceOf('\core\event\course_updated', $event);
1473         $this->assertEquals('course', $event->objecttable);
1474         $this->assertEquals($movedcourse->id, $event->objectid);
1475         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1476         $this->assertEquals($movedcourse, $event->get_record_snapshot('course', $movedcourse->id));
1477         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1478         $this->assertEventLegacyData($movedcourse, $event);
1479         $expectedlog = array($movedcourse->id, 'course', 'move', 'edit.php?id=' . $movedcourse->id, $movedcourse->id);
1480         $this->assertEventLegacyLogData($expectedlog, $event);
1482         // Move course to hidden category and catch course_updated event.
1483         $sink = $this->redirectEvents();
1484         move_courses(array($course->id), $categoryhidden->id);
1485         $events = $sink->get_events();
1486         $sink->close();
1488         // Return the moved course information from the DB.
1489         $movedcoursehidden = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1490         // Validate event.
1491         $event = array_shift($events);
1492         $this->assertInstanceOf('\core\event\course_updated', $event);
1493         $this->assertEquals('course', $event->objecttable);
1494         $this->assertEquals($movedcoursehidden->id, $event->objectid);
1495         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1496         $this->assertEquals($movedcoursehidden, $event->get_record_snapshot('course', $movedcoursehidden->id));
1497         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1498         $this->assertEventLegacyData($movedcoursehidden, $event);
1499         $expectedlog = array($movedcoursehidden->id, 'course', 'move', 'edit.php?id=' . $movedcoursehidden->id, $movedcoursehidden->id);
1500         $this->assertEventLegacyLogData($expectedlog, $event);
1501     }
1503     /**
1504      * Test that triggering a course_deleted event works as expected.
1505      */
1506     public function test_course_deleted_event() {
1507         $this->resetAfterTest();
1509         // Create the course.
1510         $course = $this->getDataGenerator()->create_course();
1512         // Save the course context before we delete the course.
1513         $coursecontext = context_course::instance($course->id);
1515         // Catch the update event.
1516         $sink = $this->redirectEvents();
1518         // Call delete_course() which will trigger the course_deleted event and the course_content_deleted
1519         // event. This function prints out data to the screen, which we do not want during a PHPUnit test,
1520         // so use ob_start and ob_end_clean to prevent this.
1521         ob_start();
1522         delete_course($course);
1523         ob_end_clean();
1525         // Capture the event.
1526         $events = $sink->get_events();
1527         $sink->close();
1529         // Validate the event.
1530         $event = $events[1];
1531         $this->assertInstanceOf('\core\event\course_deleted', $event);
1532         $this->assertEquals('course', $event->objecttable);
1533         $this->assertEquals($course->id, $event->objectid);
1534         $this->assertEquals($coursecontext->id, $event->contextid);
1535         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1536         $this->assertEquals('course_deleted', $event->get_legacy_eventname());
1537         $eventdata = $event->get_data();
1538         $this->assertSame($course->idnumber, $eventdata['other']['idnumber']);
1539         $this->assertSame($course->fullname, $eventdata['other']['fullname']);
1540         $this->assertSame($course->shortname, $eventdata['other']['shortname']);
1541         // The legacy data also passed the context in the course object.
1542         $course->context = $coursecontext;
1543         $this->assertEventLegacyData($course, $event);
1544         $expectedlog = array(SITEID, 'course', 'delete', 'view.php?id=' . $course->id, $course->fullname . '(ID ' . $course->id . ')');
1545         $this->assertEventLegacyLogData($expectedlog, $event);
1546     }
1548     /**
1549      * Test that triggering a course_content_deleted event works as expected.
1550      */
1551     public function test_course_content_deleted_event() {
1552         global $DB;
1554         $this->resetAfterTest();
1556         // Create the course.
1557         $course = $this->getDataGenerator()->create_course();
1559         // Get the course from the DB. The data generator adds some extra properties, such as
1560         // numsections, to the course object which will fail the assertions later on.
1561         $course = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1563         // Save the course context before we delete the course.
1564         $coursecontext = context_course::instance($course->id);
1566         // Catch the update event.
1567         $sink = $this->redirectEvents();
1569         // Call remove_course_contents() which will trigger the course_content_deleted event.
1570         // This function prints out data to the screen, which we do not want during a PHPUnit
1571         // test, so use ob_start and ob_end_clean to prevent this.
1572         ob_start();
1573         remove_course_contents($course->id);
1574         ob_end_clean();
1576         // Capture the event.
1577         $events = $sink->get_events();
1578         $sink->close();
1580         // Validate the event.
1581         $event = $events[0];
1582         $this->assertInstanceOf('\core\event\course_content_deleted', $event);
1583         $this->assertEquals('course', $event->objecttable);
1584         $this->assertEquals($course->id, $event->objectid);
1585         $this->assertEquals($coursecontext->id, $event->contextid);
1586         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1587         $this->assertEquals('course_content_removed', $event->get_legacy_eventname());
1588         // The legacy data also passed the context and options in the course object.
1589         $course->context = $coursecontext;
1590         $course->options = array();
1591         $this->assertEventLegacyData($course, $event);
1592     }
1594     /**
1595      * Test that triggering a course_category_deleted event works as expected.
1596      */
1597     public function test_course_category_deleted_event() {
1598         $this->resetAfterTest();
1600         // Create a category.
1601         $category = $this->getDataGenerator()->create_category();
1603         // Save the context before it is deleted.
1604         $categorycontext = context_coursecat::instance($category->id);
1606         // Catch the update event.
1607         $sink = $this->redirectEvents();
1609         // Delete the category.
1610         $category->delete_full();
1612         // Capture the event.
1613         $events = $sink->get_events();
1614         $sink->close();
1616         // Validate the event.
1617         $event = $events[0];
1618         $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1619         $this->assertEquals('course_categories', $event->objecttable);
1620         $this->assertEquals($category->id, $event->objectid);
1621         $this->assertEquals($categorycontext->id, $event->contextid);
1622         $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1623         $this->assertEventLegacyData($category, $event);
1624         $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category->name . '(ID ' . $category->id . ')');
1625         $this->assertEventLegacyLogData($expectedlog, $event);
1627         // Create two categories.
1628         $category = $this->getDataGenerator()->create_category();
1629         $category2 = $this->getDataGenerator()->create_category();
1631         // Save the context before it is moved and then deleted.
1632         $category2context = context_coursecat::instance($category2->id);
1634         // Catch the update event.
1635         $sink = $this->redirectEvents();
1637         // Move the category.
1638         $category2->delete_move($category->id);
1640         // Capture the event.
1641         $events = $sink->get_events();
1642         $sink->close();
1644         // Validate the event.
1645         $event = $events[0];
1646         $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1647         $this->assertEquals('course_categories', $event->objecttable);
1648         $this->assertEquals($category2->id, $event->objectid);
1649         $this->assertEquals($category2context->id, $event->contextid);
1650         $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1651         $this->assertEventLegacyData($category2, $event);
1652         $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category2->name . '(ID ' . $category2->id . ')');
1653         $this->assertEventLegacyLogData($expectedlog, $event);
1654     }
1656     /**
1657      * Test that triggering a course_restored event works as expected.
1658      */
1659     public function test_course_restored_event() {
1660         global $CFG;
1662         // Get the necessary files to perform backup and restore.
1663         require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
1664         require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
1666         $this->resetAfterTest();
1668         // Set to admin user.
1669         $this->setAdminUser();
1671         // The user id is going to be 2 since we are the admin user.
1672         $userid = 2;
1674         // Create a course.
1675         $course = $this->getDataGenerator()->create_course();
1677         // Create backup file and save it to the backup location.
1678         $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
1679             backup::INTERACTIVE_NO, backup::MODE_GENERAL, $userid);
1680         $bc->execute_plan();
1681         $results = $bc->get_results();
1682         $file = $results['backup_destination'];
1683         $fp = get_file_packer();
1684         $filepath = $CFG->dataroot . '/temp/backup/test-restore-course-event';
1685         $file->extract_to_pathname($fp, $filepath);
1686         $bc->destroy();
1687         unset($bc);
1689         // Now we want to catch the restore course event.
1690         $sink = $this->redirectEvents();
1692         // Now restore the course to trigger the event.
1693         $rc = new restore_controller('test-restore-course-event', $course->id, backup::INTERACTIVE_NO,
1694             backup::MODE_GENERAL, $userid, backup::TARGET_NEW_COURSE);
1695         $rc->execute_precheck();
1696         $rc->execute_plan();
1698         // Capture the event.
1699         $events = $sink->get_events();
1700         $sink->close();
1702         // Validate the event.
1703         $event = $events[0];
1704         $this->assertInstanceOf('\core\event\course_restored', $event);
1705         $this->assertEquals('course', $event->objecttable);
1706         $this->assertEquals($rc->get_courseid(), $event->objectid);
1707         $this->assertEquals(context_course::instance($rc->get_courseid())->id, $event->contextid);
1708         $this->assertEquals('course_restored', $event->get_legacy_eventname());
1709         $legacydata = (object) array(
1710             'courseid' => $rc->get_courseid(),
1711             'userid' => $rc->get_userid(),
1712             'type' => $rc->get_type(),
1713             'target' => $rc->get_target(),
1714             'mode' => $rc->get_mode(),
1715             'operation' => $rc->get_operation(),
1716             'samesite' => $rc->is_samesite()
1717         );
1718         $this->assertEventLegacyData($legacydata, $event);
1720         // Destroy the resource controller since we are done using it.
1721         $rc->destroy();
1722         unset($rc);
1723     }
1725     /**
1726      * Test that triggering a course_section_updated event works as expected.
1727      */
1728     public function test_course_section_updated_event() {
1729         global $DB;
1731         $this->resetAfterTest();
1733         // Create the course with sections.
1734         $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true));
1735         $sections = $DB->get_records('course_sections', array('course' => $course->id));
1737         $coursecontext = context_course::instance($course->id);
1739         $section = array_pop($sections);
1740         $section->name = 'Test section';
1741         $section->summary = 'Test section summary';
1742         $DB->update_record('course_sections', $section);
1744         // Trigger an event for course section update.
1745         $event = \core\event\course_section_updated::create(
1746                 array(
1747                     'objectid' => $section->id,
1748                     'courseid' => $course->id,
1749                     'context' => context_course::instance($course->id)
1750                 )
1751             );
1752         $event->add_record_snapshot('course_sections', $section);
1753         // Trigger and catch event.
1754         $sink = $this->redirectEvents();
1755         $event->trigger();
1756         $events = $sink->get_events();
1757         $sink->close();
1759         // Validate the event.
1760         $event = $events[0];
1761         $this->assertInstanceOf('\core\event\course_section_updated', $event);
1762         $this->assertEquals('course_sections', $event->objecttable);
1763         $this->assertEquals($section->id, $event->objectid);
1764         $this->assertEquals($course->id, $event->courseid);
1765         $this->assertEquals($coursecontext->id, $event->contextid);
1766         $expecteddesc = 'Course ' . $event->courseid . ' section ' . $event->other['sectionnum'] . ' updated by user ' . $event->userid;
1767         $this->assertEquals($expecteddesc, $event->get_description());
1768         $this->assertEquals($section, $event->get_record_snapshot('course_sections', $event->objectid));
1769         $id = $section->id;
1770         $sectionnum = $section->section;
1771         $expectedlegacydata = array($course->id, "course", "editsection", 'editsection.php?id=' . $id, $sectionnum);
1772         $this->assertEventLegacyLogData($expectedlegacydata, $event);
1773     }
1775     public function test_course_integrity_check() {
1776         global $DB;
1778         $this->resetAfterTest(true);
1779         $course = $this->getDataGenerator()->create_course(array('numsections' => 1),
1780            array('createsections'=>true));
1782         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1783                 array('section' => 0));
1784         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id),
1785                 array('section' => 0));
1786         $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id),
1787                 array('section' => 0));
1788         $correctseq = join(',', array($forum->cmid, $page->cmid, $quiz->cmid));
1790         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1791         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1792         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1793         $this->assertEquals($correctseq, $section0->sequence);
1794         $this->assertEmpty($section1->sequence);
1795         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1796         $this->assertEquals($section0->id, $cms[$page->cmid]->section);
1797         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1798         $this->assertEmpty(course_integrity_check($course->id));
1800         // Now let's make manual change in DB and let course_integrity_check() fix it:
1802         // 1. Module appears twice in one section.
1803         $DB->update_record('course_sections', array('id' => $section0->id, 'sequence' => $section0->sequence. ','. $page->cmid));
1804         $this->assertEquals(
1805                 array('Failed integrity check for course ['. $course->id.
1806                 ']. Sequence for course section ['. $section0->id. '] is "'.
1807                 $section0->sequence. ','. $page->cmid. '", must be "'.
1808                 $section0->sequence. '"'),
1809                 course_integrity_check($course->id));
1810         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1811         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1812         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1813         $this->assertEquals($correctseq, $section0->sequence);
1814         $this->assertEmpty($section1->sequence);
1815         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1816         $this->assertEquals($section0->id, $cms[$page->cmid]->section);
1817         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1819         // 2. Module appears in two sections (last section wins).
1820         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''. $page->cmid));
1821         // First message about double mentioning in sequence, second message about wrong section field for $page.
1822         $this->assertEquals(array(
1823             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
1824             '] must be removed from sequence of section ['. $section0->id.
1825             '] because it is also present in sequence of section ['. $section1->id. ']',
1826             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
1827             '] points to section ['. $section0->id. '] instead of ['. $section1->id. ']'),
1828                 course_integrity_check($course->id));
1829         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1830         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1831         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1832         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
1833         $this->assertEquals(''. $page->cmid, $section1->sequence);
1834         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1835         $this->assertEquals($section1->id, $cms[$page->cmid]->section);
1836         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1838         // 3. Module id is not present in course_section.sequence (integrity check with $fullcheck = false).
1839         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''));
1840         $this->assertEmpty(course_integrity_check($course->id)); // Not an error!
1841         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1842         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1843         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1844         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
1845         $this->assertEmpty($section1->sequence);
1846         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1847         $this->assertEquals($section1->id, $cms[$page->cmid]->section); // Not changed.
1848         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1850         // 4. Module id is not present in course_section.sequence (integrity check with $fullcheck = true).
1851         $this->assertEquals(array('Failed integrity check for course ['. $course->id. ']. Course module ['.
1852                 $page->cmid. '] is missing from sequence of section ['. $section1->id. ']'),
1853                 course_integrity_check($course->id, null, null, true)); // Error!
1854         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1855         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1856         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1857         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
1858         $this->assertEquals(''. $page->cmid, $section1->sequence);  // Yay, module added to section.
1859         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1860         $this->assertEquals($section1->id, $cms[$page->cmid]->section); // Not changed.
1861         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1863         // 5. Module id is not present in course_section.sequence and it's section is invalid (integrity check with $fullcheck = true).
1864         $DB->update_record('course_modules', array('id' => $page->cmid, 'section' => 8765));
1865         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''));
1866         $this->assertEquals(array(
1867             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
1868             '] is missing from sequence of section ['. $section0->id. ']',
1869             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
1870             '] points to section [8765] instead of ['. $section0->id. ']'),
1871                 course_integrity_check($course->id, null, null, true));
1872         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1873         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1874         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1875         $this->assertEquals($forum->cmid. ','. $quiz->cmid. ','. $page->cmid, $section0->sequence); // Module added to section.
1876         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1877         $this->assertEquals($section0->id, $cms[$page->cmid]->section); // Section changed to section0.
1878         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1880         // 6. Module is deleted from course_modules but not deleted in sequence (integrity check with $fullcheck = true).
1881         $DB->delete_records('course_modules', array('id' => $page->cmid));
1882         $this->assertEquals(array('Failed integrity check for course ['. $course->id. ']. Course module ['.
1883                 $page->cmid. '] does not exist but is present in the sequence of section ['. $section0->id. ']'),
1884                 course_integrity_check($course->id, null, null, true));
1885         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1886         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1887         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1888         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
1889         $this->assertEmpty($section1->sequence);
1890         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1891         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1892         $this->assertEquals(2, count($cms));
1893     }
1895     /**
1896      * Tests for event related to course module creation.
1897      */
1898     public function test_course_module_created_event() {
1899         global $USER, $DB;
1900         $this->resetAfterTest();
1902         // Create an assign module.
1903         $sink = $this->redirectEvents();
1904         $modinfo = $this->create_specific_module_test('assign');
1905         $events = $sink->get_events();
1906         $event = array_pop($events);
1907         $sink->close();
1909         $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
1910         $mod = $DB->get_record('assign', array('id' => $modinfo->instance), '*', MUST_EXIST);
1912         // Validate event data.
1913         $this->assertInstanceOf('\core\event\course_module_created', $event);
1914         $this->assertEquals($cm->id, $event->objectid);
1915         $this->assertEquals($USER->id, $event->userid);
1916         $this->assertEquals('course_modules', $event->objecttable);
1917         $url = new moodle_url('/mod/assign/view.php', array('id' => $mod->id));
1918         $this->assertEquals($url, $event->get_url());
1920         // Test legacy data.
1921         $this->assertSame('mod_created', $event->get_legacy_eventname());
1922         $eventdata = new stdClass();
1923         $eventdata->modulename = 'assign';
1924         $eventdata->name       = $mod->name;
1925         $eventdata->cmid       = $cm->id;
1926         $eventdata->courseid   = $cm->course;
1927         $eventdata->userid     = $USER->id;
1928         $this->assertEventLegacyData($eventdata, $event);
1930         $arr = array($cm->course, "course", "add mod", "../mod/assign/view.php?id=$mod->id", "assign $cm->instance");
1931         $this->assertEventLegacyLogData($arr, $event);
1933     }
1935     /**
1936      * Tests for event validations related to course module creation.
1937      */
1938     public function test_course_module_created_event_exceptions() {
1940         $this->resetAfterTest();
1942         // Generate data.
1943         $modinfo = $this->create_specific_module_test('assign');
1944         $context = context_module::instance($modinfo->coursemodule);
1946         // Test not setting instanceid.
1947         try {
1948             $event = \core\event\course_module_created::create(array(
1949                 'courseid' => $modinfo->course,
1950                 'context'  => $context,
1951                 'objectid' => $modinfo->coursemodule,
1952                 'other'    => array(
1953                     'modulename' => 'assign',
1954                     'name'       => 'My assignment',
1955                 )
1956             ));
1957             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
1958                     other['instanceid']");
1959         } catch (coding_exception $e) {
1960             $this->assertContains("Field other['instanceid'] cannot be empty", $e->getMessage());
1961         }
1963         // Test not setting modulename.
1964         try {
1965             $event = \core\event\course_module_created::create(array(
1966                 'courseid' => $modinfo->course,
1967                 'context'  => $context,
1968                 'objectid' => $modinfo->coursemodule,
1969                 'other'    => array(
1970                     'instanceid' => $modinfo->instance,
1971                     'name'       => 'My assignment',
1972                 )
1973             ));
1974             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
1975                     other['modulename']");
1976         } catch (coding_exception $e) {
1977             $this->assertContains("Field other['modulename'] cannot be empty", $e->getMessage());
1978         }
1980         // Test not setting name.
1982         try {
1983             $event = \core\event\course_module_created::create(array(
1984                 'courseid' => $modinfo->course,
1985                 'context'  => $context,
1986                 'objectid' => $modinfo->coursemodule,
1987                 'other'    => array(
1988                     'modulename' => 'assign',
1989                     'instanceid' => $modinfo->instance,
1990                 )
1991             ));
1992             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
1993                     other['name']");
1994         } catch (coding_exception $e) {
1995             $this->assertContains("Field other['name'] cannot be empty", $e->getMessage());
1996         }
1998     }
2000     /**
2001      * Tests for event related to course module updates.
2002      */
2003     public function test_course_module_updated_event() {
2004         global $USER, $DB;
2005         $this->resetAfterTest();
2007         // Update a forum module.
2008         $sink = $this->redirectEvents();
2009         $modinfo = $this->update_specific_module_test('forum');
2010         $events = $sink->get_events();
2011         $event = array_pop($events);
2012         $sink->close();
2014         $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
2015         $mod = $DB->get_record('forum', array('id' => $cm->instance), '*', MUST_EXIST);
2017         // Validate event data.
2018         $this->assertInstanceOf('\core\event\course_module_updated', $event);
2019         $this->assertEquals($cm->id, $event->objectid);
2020         $this->assertEquals($USER->id, $event->userid);
2021         $this->assertEquals('course_modules', $event->objecttable);
2022         $url = new moodle_url('/mod/forum/view.php', array('id' => $mod->id));
2023         $this->assertEquals($url, $event->get_url());
2025         // Test legacy data.
2026         $this->assertSame('mod_updated', $event->get_legacy_eventname());
2027         $eventdata = new stdClass();
2028         $eventdata->modulename = 'forum';
2029         $eventdata->name       = $mod->name;
2030         $eventdata->cmid       = $cm->id;
2031         $eventdata->courseid   = $cm->course;
2032         $eventdata->userid     = $USER->id;
2033         $this->assertEventLegacyData($eventdata, $event);
2035         $arr = array($cm->course, "course", "update mod", "../mod/forum/view.php?id=$mod->id", "forum $cm->instance");
2036         $this->assertEventLegacyLogData($arr, $event);
2038     }
2040     /**
2041      * Tests for event validations related to course module update.
2042      */
2043     public function test_course_module_updated_event_exceptions() {
2045         $this->resetAfterTest();
2047         // Generate data.
2048         $modinfo = $this->create_specific_module_test('assign');
2049         $context = context_module::instance($modinfo->coursemodule);
2051         // Test not setting instanceid.
2052         try {
2053             $event = \core\event\course_module_updated::create(array(
2054                 'courseid' => $modinfo->course,
2055                 'context'  => $context,
2056                 'objectid' => $modinfo->coursemodule,
2057                 'other'    => array(
2058                     'modulename' => 'assign',
2059                     'name'       => 'My assignment',
2060                 )
2061             ));
2062             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2063                     other['instanceid']");
2064         } catch (coding_exception $e) {
2065             $this->assertContains("Field other['instanceid'] cannot be empty", $e->getMessage());
2066         }
2068         // Test not setting modulename.
2069         try {
2070             $event = \core\event\course_module_updated::create(array(
2071                 'courseid' => $modinfo->course,
2072                 'context'  => $context,
2073                 'objectid' => $modinfo->coursemodule,
2074                 'other'    => array(
2075                     'instanceid' => $modinfo->instance,
2076                     'name'       => 'My assignment',
2077                 )
2078             ));
2079             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2080                     other['modulename']");
2081         } catch (coding_exception $e) {
2082             $this->assertContains("Field other['modulename'] cannot be empty", $e->getMessage());
2083         }
2085         // Test not setting name.
2087         try {
2088             $event = \core\event\course_module_updated::create(array(
2089                 'courseid' => $modinfo->course,
2090                 'context'  => $context,
2091                 'objectid' => $modinfo->coursemodule,
2092                 'other'    => array(
2093                     'modulename' => 'assign',
2094                     'instanceid' => $modinfo->instance,
2095                 )
2096             ));
2097             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2098                     other['name']");
2099         } catch (coding_exception $e) {
2100             $this->assertContains("Field other['name'] cannot be empty", $e->getMessage());
2101         }
2103     }
2105     /**
2106      * Tests for event related to course module delete.
2107      */
2108     public function test_course_module_deleted_event() {
2109         global $USER, $DB;
2110         $this->resetAfterTest();
2112         // Create and delete a module.
2113         $sink = $this->redirectEvents();
2114         $modinfo = $this->create_specific_module_test('forum');
2115         $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
2116         course_delete_module($modinfo->coursemodule);
2117         $events = $sink->get_events();
2118         $event = array_pop($events); // delete module event.;
2119         $sink->close();
2121         // Validate event data.
2122         $this->assertInstanceOf('\core\event\course_module_deleted', $event);
2123         $this->assertEquals($cm->id, $event->objectid);
2124         $this->assertEquals($USER->id, $event->userid);
2125         $this->assertEquals('course_modules', $event->objecttable);
2126         $this->assertEquals(null, $event->get_url());
2127         $this->assertEquals($cm, $event->get_record_snapshot('course_modules', $cm->id));
2129         // Test legacy data.
2130         $this->assertSame('mod_deleted', $event->get_legacy_eventname());
2131         $eventdata = new stdClass();
2132         $eventdata->modulename = 'forum';
2133         $eventdata->cmid       = $cm->id;
2134         $eventdata->courseid   = $cm->course;
2135         $eventdata->userid     = $USER->id;
2136         $this->assertEventLegacyData($eventdata, $event);
2138         $arr = array($cm->course, 'course', "delete mod", "view.php?id=$cm->course", "forum $cm->instance", $cm->id);
2139         $this->assertEventLegacyLogData($arr, $event);
2141     }
2143     /**
2144      * Tests for event validations related to course module deletion.
2145      */
2146     public function test_course_module_deleted_event_exceptions() {
2148         $this->resetAfterTest();
2150         // Generate data.
2151         $modinfo = $this->create_specific_module_test('assign');
2152         $context = context_module::instance($modinfo->coursemodule);
2154         // Test not setting instanceid.
2155         try {
2156             $event = \core\event\course_module_deleted::create(array(
2157                 'courseid' => $modinfo->course,
2158                 'context'  => $context,
2159                 'objectid' => $modinfo->coursemodule,
2160                 'other'    => array(
2161                     'modulename' => 'assign',
2162                     'name'       => 'My assignment',
2163                 )
2164             ));
2165             $this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
2166                     other['instanceid']");
2167         } catch (coding_exception $e) {
2168             $this->assertContains("Field other['instanceid'] cannot be empty", $e->getMessage());
2169         }
2171         // Test not setting modulename.
2172         try {
2173             $event = \core\event\course_module_deleted::create(array(
2174                 'courseid' => $modinfo->course,
2175                 'context'  => $context,
2176                 'objectid' => $modinfo->coursemodule,
2177                 'other'    => array(
2178                     'instanceid' => $modinfo->instance,
2179                     'name'       => 'My assignment',
2180                 )
2181             ));
2182             $this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
2183                     other['modulename']");
2184         } catch (coding_exception $e) {
2185             $this->assertContains("Field other['modulename'] cannot be empty", $e->getMessage());
2186         }
2187     }
2189     /**
2190      * Returns a user object and its assigned new role.
2191      *
2192      * @param testing_data_generator $generator
2193      * @param $contextid
2194      * @return array The user object and the role ID
2195      */
2196     protected function get_user_objects(testing_data_generator $generator, $contextid) {
2197         global $USER;
2199         if (empty($USER->id)) {
2200             $user  = $generator->create_user();
2201             $this->setUser($user);
2202         }
2203         $roleid = create_role('Test role', 'testrole', 'Test role description');
2204         if (!is_array($contextid)) {
2205             $contextid = array($contextid);
2206         }
2207         foreach ($contextid as $cid) {
2208             $assignid = role_assign($roleid, $user->id, $cid);
2209         }
2210         return array($user, $roleid);
2211     }
2213     /**
2214      * Test course move after course.
2215      */
2216     public function test_course_change_sortorder_after_course() {
2217         global $DB;
2219         $this->resetAfterTest(true);
2221         $generator = $this->getDataGenerator();
2222         $category = $generator->create_category();
2223         $course3 = $generator->create_course(array('category' => $category->id));
2224         $course2 = $generator->create_course(array('category' => $category->id));
2225         $course1 = $generator->create_course(array('category' => $category->id));
2226         $context = $category->get_context();
2228         list($user, $roleid) = $this->get_user_objects($generator, $context->id);
2229         $caps = course_capability_assignment::allow('moodle/category:manage', $roleid, $context->id);
2231         $courses = $category->get_courses();
2232         $this->assertInternalType('array', $courses);
2233         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2234         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2235         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2237         // Test moving down.
2238         $this->assertTrue(course_change_sortorder_after_course($course1->id, $course3->id));
2239         $courses = $category->get_courses();
2240         $this->assertInternalType('array', $courses);
2241         $this->assertEquals(array($course2->id, $course3->id, $course1->id), array_keys($courses));
2242         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2243         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2245         // Test moving up.
2246         $this->assertTrue(course_change_sortorder_after_course($course1->id, $course2->id));
2247         $courses = $category->get_courses();
2248         $this->assertInternalType('array', $courses);
2249         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
2250         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2251         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2253         // Test moving to the top.
2254         $this->assertTrue(course_change_sortorder_after_course($course1->id, 0));
2255         $courses = $category->get_courses();
2256         $this->assertInternalType('array', $courses);
2257         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2258         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2259         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2260     }
2262     /**
2263      * Tests changing the visibility of a course.
2264      */
2265     public function test_course_change_visibility() {
2266         global $DB;
2268         $this->resetAfterTest(true);
2270         $generator = $this->getDataGenerator();
2271         $category = $generator->create_category();
2272         $course = $generator->create_course(array('category' => $category->id));
2274         $this->assertEquals('1', $course->visible);
2275         $this->assertEquals('1', $course->visibleold);
2277         $this->assertTrue(course_change_visibility($course->id, false));
2278         $course = $DB->get_record('course', array('id' => $course->id));
2279         $this->assertEquals('0', $course->visible);
2280         $this->assertEquals('0', $course->visibleold);
2282         $this->assertTrue(course_change_visibility($course->id, true));
2283         $course = $DB->get_record('course', array('id' => $course->id));
2284         $this->assertEquals('1', $course->visible);
2285         $this->assertEquals('1', $course->visibleold);
2286     }
2288     /**
2289      * Tests moving the course up and down by one.
2290      */
2291     public function test_course_change_sortorder_by_one() {
2292         global $DB;
2294         $this->resetAfterTest(true);
2296         $generator = $this->getDataGenerator();
2297         $category = $generator->create_category();
2298         $course3 = $generator->create_course(array('category' => $category->id));
2299         $course2 = $generator->create_course(array('category' => $category->id));
2300         $course1 = $generator->create_course(array('category' => $category->id));
2302         $courses = $category->get_courses();
2303         $this->assertInternalType('array', $courses);
2304         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2305         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2306         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2308         // Test moving down.
2309         $course1 = get_course($course1->id);
2310         $this->assertTrue(course_change_sortorder_by_one($course1, false));
2311         $courses = $category->get_courses();
2312         $this->assertInternalType('array', $courses);
2313         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
2314         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2315         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2317         // Test moving up.
2318         $course1 = get_course($course1->id);
2319         $this->assertTrue(course_change_sortorder_by_one($course1, true));
2320         $courses = $category->get_courses();
2321         $this->assertInternalType('array', $courses);
2322         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2323         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2324         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2326         // Test moving the top course up one.
2327         $course1 = get_course($course1->id);
2328         $this->assertFalse(course_change_sortorder_by_one($course1, true));
2329         // Check nothing changed.
2330         $courses = $category->get_courses();
2331         $this->assertInternalType('array', $courses);
2332         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2333         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2334         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2336         // Test moving the bottom course up down.
2337         $course3 = get_course($course3->id);
2338         $this->assertFalse(course_change_sortorder_by_one($course3, false));
2339         // Check nothing changed.
2340         $courses = $category->get_courses();
2341         $this->assertInternalType('array', $courses);
2342         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2343         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2344         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2345     }