MDL-41508 move_courses() bug fix and DB query improvement
[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');
31 class core_course_courselib_testcase extends advanced_testcase {
33     /**
34      * Set forum specific test values for calling create_module().
35      *
36      * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
37      */
38     private function forum_create_set_values(&$moduleinfo) {
39         // Completion specific to forum - optional.
40         $moduleinfo->completionposts = 3;
41         $moduleinfo->completiondiscussions = 1;
42         $moduleinfo->completionreplies = 2;
44         // Specific values to the Forum module.
45         $moduleinfo->forcesubscribe = FORUM_INITIALSUBSCRIBE;
46         $moduleinfo->type = 'single';
47         $moduleinfo->trackingtype = FORUM_TRACKING_ON;
48         $moduleinfo->maxbytes = 10240;
49         $moduleinfo->maxattachments = 2;
51         // Post threshold for blocking - specific to forum.
52         $moduleinfo->blockperiod = 60*60*24;
53         $moduleinfo->blockafter = 10;
54         $moduleinfo->warnafter = 5;
55     }
57     /**
58      * Execute test asserts on the saved DB data by create_module($forum).
59      *
60      * @param object $moduleinfo - the specific forum values that were used to create a forum.
61      * @param object $dbmodinstance - the DB values of the created forum.
62      */
63     private function forum_create_run_asserts($moduleinfo, $dbmodinstance) {
64         // Compare values specific to forums.
65         $this->assertEquals($moduleinfo->forcesubscribe, $dbmodinstance->forcesubscribe);
66         $this->assertEquals($moduleinfo->type, $dbmodinstance->type);
67         $this->assertEquals($moduleinfo->assessed, $dbmodinstance->assessed);
68         $this->assertEquals($moduleinfo->completionposts, $dbmodinstance->completionposts);
69         $this->assertEquals($moduleinfo->completiondiscussions, $dbmodinstance->completiondiscussions);
70         $this->assertEquals($moduleinfo->completionreplies, $dbmodinstance->completionreplies);
71         $this->assertEquals($moduleinfo->scale, $dbmodinstance->scale);
72         $this->assertEquals($moduleinfo->assesstimestart, $dbmodinstance->assesstimestart);
73         $this->assertEquals($moduleinfo->assesstimefinish, $dbmodinstance->assesstimefinish);
74         $this->assertEquals($moduleinfo->rsstype, $dbmodinstance->rsstype);
75         $this->assertEquals($moduleinfo->rssarticles, $dbmodinstance->rssarticles);
76         $this->assertEquals($moduleinfo->trackingtype, $dbmodinstance->trackingtype);
77         $this->assertEquals($moduleinfo->maxbytes, $dbmodinstance->maxbytes);
78         $this->assertEquals($moduleinfo->maxattachments, $dbmodinstance->maxattachments);
79         $this->assertEquals($moduleinfo->blockperiod, $dbmodinstance->blockperiod);
80         $this->assertEquals($moduleinfo->blockafter, $dbmodinstance->blockafter);
81         $this->assertEquals($moduleinfo->warnafter, $dbmodinstance->warnafter);
82     }
84     /**
85      * Set assign module specific test values for calling create_module().
86      *
87      * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
88      */
89     private function assign_create_set_values(&$moduleinfo) {
90         // Specific values to the Assign module.
91         $moduleinfo->alwaysshowdescription = true;
92         $moduleinfo->submissiondrafts = true;
93         $moduleinfo->requiresubmissionstatement = true;
94         $moduleinfo->sendnotifications = true;
95         $moduleinfo->sendlatenotifications = true;
96         $moduleinfo->duedate = time() + (7 * 24 * 3600);
97         $moduleinfo->cutoffdate = time() + (7 * 24 * 3600);
98         $moduleinfo->allowsubmissionsfromdate = time();
99         $moduleinfo->teamsubmission = true;
100         $moduleinfo->requireallteammemberssubmit = true;
101         $moduleinfo->teamsubmissiongroupingid = true;
102         $moduleinfo->blindmarking = true;
103         $moduleinfo->markingworkflow = true;
104         $moduleinfo->markingallocation = true;
105         $moduleinfo->assignsubmission_onlinetext_enabled = true;
106         $moduleinfo->assignsubmission_file_enabled = true;
107         $moduleinfo->assignsubmission_file_maxfiles = 1;
108         $moduleinfo->assignsubmission_file_maxsizebytes = 1000000;
109         $moduleinfo->assignsubmission_comments_enabled = true;
110         $moduleinfo->assignfeedback_comments_enabled = true;
111         $moduleinfo->assignfeedback_offline_enabled = true;
112         $moduleinfo->assignfeedback_file_enabled = true;
114         // Advanced grading.
115         $gradingmethods = grading_manager::available_methods();
116         $moduleinfo->advancedgradingmethod_submissions = current(array_keys($gradingmethods));
117     }
119     /**
120      * Execute test asserts on the saved DB data by create_module($assign).
121      *
122      * @param object $moduleinfo - the specific assign module values that were used to create an assign module.
123      * @param object $dbmodinstance - the DB values of the created assign module.
124      */
125     private function assign_create_run_asserts($moduleinfo, $dbmodinstance) {
126         global $DB;
128         $this->assertEquals($moduleinfo->alwaysshowdescription, $dbmodinstance->alwaysshowdescription);
129         $this->assertEquals($moduleinfo->submissiondrafts, $dbmodinstance->submissiondrafts);
130         $this->assertEquals($moduleinfo->requiresubmissionstatement, $dbmodinstance->requiresubmissionstatement);
131         $this->assertEquals($moduleinfo->sendnotifications, $dbmodinstance->sendnotifications);
132         $this->assertEquals($moduleinfo->duedate, $dbmodinstance->duedate);
133         $this->assertEquals($moduleinfo->cutoffdate, $dbmodinstance->cutoffdate);
134         $this->assertEquals($moduleinfo->allowsubmissionsfromdate, $dbmodinstance->allowsubmissionsfromdate);
135         $this->assertEquals($moduleinfo->teamsubmission, $dbmodinstance->teamsubmission);
136         $this->assertEquals($moduleinfo->requireallteammemberssubmit, $dbmodinstance->requireallteammemberssubmit);
137         $this->assertEquals($moduleinfo->teamsubmissiongroupingid, $dbmodinstance->teamsubmissiongroupingid);
138         $this->assertEquals($moduleinfo->blindmarking, $dbmodinstance->blindmarking);
139         $this->assertEquals($moduleinfo->markingworkflow, $dbmodinstance->markingworkflow);
140         $this->assertEquals($moduleinfo->markingallocation, $dbmodinstance->markingallocation);
141         // The goal not being to fully test assign_add_instance() we'll stop here for the assign tests - to avoid too many DB queries.
143         // Advanced grading.
144         $contextmodule = context_module::instance($dbmodinstance->id);
145         $advancedgradingmethod = $DB->get_record('grading_areas',
146             array('contextid' => $contextmodule->id,
147                 'activemethod' => $moduleinfo->advancedgradingmethod_submissions));
148         $this->assertEquals($moduleinfo->advancedgradingmethod_submissions, $advancedgradingmethod);
149     }
151     /**
152      * Run some asserts test for a specific module for the function create_module().
153      *
154      * The function has been created (and is called) for $this->test_create_module().
155      * Note that the call to MODULE_create_set_values and MODULE_create_run_asserts are done after the common set values/run asserts.
156      * So if you want, you can overwrite the default values/asserts in the respective functions.
157      * @param string $modulename Name of the module ('forum', 'assign', 'book'...).
158      */
159     private function create_specific_module_test($modulename) {
160         global $DB, $CFG;
162         $this->resetAfterTest(true);
164         $this->setAdminUser();
166         // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
167         require_once($CFG->dirroot.'/mod/'. $modulename .'/lib.php');
169         // Enable avaibility.
170         // If not enabled all conditional fields will be ignored.
171         set_config('enableavailability', 1);
173         // Enable course completion.
174         // If not enabled all completion settings will be ignored.
175         set_config('enablecompletion', COMPLETION_ENABLED);
177         // Enable forum RSS feeds.
178         set_config('enablerssfeeds', 1);
179         set_config('forum_enablerssfeeds', 1);
181         $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED),
182            array('createsections'=>true));
184         $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
186         // Create assign module instance for test.
187         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
188         $params['course'] = $course->id;
189         $instance = $generator->create_instance($params);
190         $assigncm = get_coursemodule_from_instance('assign', $instance->id);
192         // Module test values.
193         $moduleinfo = new stdClass();
195         // Always mandatory generic values to any module.
196         $moduleinfo->modulename = $modulename;
197         $moduleinfo->section = 1; // This is the section number in the course. Not the section id in the database.
198         $moduleinfo->course = $course->id;
199         $moduleinfo->groupingid = $grouping->id;
200         $moduleinfo->groupmembersonly = 0;
201         $moduleinfo->visible = true;
203         // Sometimes optional generic values for some modules.
204         $moduleinfo->name = 'My test module';
205         $moduleinfo->showdescription = 1; // standard boolean
206         require_once($CFG->libdir . '/gradelib.php');
207         $gradecats = grade_get_categories_menu($moduleinfo->course, false);
208         $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
209         $moduleinfo->gradecat = $gradecatid;
210         $moduleinfo->groupmode = VISIBLEGROUPS;
211         $moduleinfo->cmidnumber = 'idnumber_XXX';
213         // Completion common to all module.
214         $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC;
215         $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED;
216         $moduleinfo->completiongradeitemnumber = 1;
217         $moduleinfo->completionexpected = time() + (7 * 24 * 3600);
219         // Conditional activity.
220         $moduleinfo->availablefrom = time();
221         $moduleinfo->availableuntil = time() + (7 * 24 * 3600);
222         $moduleinfo->showavailability = CONDITION_STUDENTVIEW_SHOW;
223         $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself.
224         $moduleinfo->conditiongradegroup = array(array('conditiongradeitemid' => $coursegradeitem->id, 'conditiongrademin' => 10, 'conditiongrademax' => 80));
225         $moduleinfo->conditionfieldgroup = array(array('conditionfield' => 'email', 'conditionfieldoperator' => OP_CONTAINS, 'conditionfieldvalue' => '@'));
226         $moduleinfo->conditioncompletiongroup = array(array('conditionsourcecmid' => $assigncm->id, 'conditionrequiredcompletion' => COMPLETION_COMPLETE)); // "conditionsourcecmid == 0" => none
228         // Grading and Advanced grading.
229         require_once($CFG->dirroot . '/rating/lib.php');
230         $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE;
231         $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number.
232         $moduleinfo->assesstimestart = time();
233         $moduleinfo->assesstimefinish = time() + (7 * 24 * 3600);
235         // RSS.
236         $moduleinfo->rsstype = 2;
237         $moduleinfo->rssarticles = 10;
239         // Optional intro editor (depends of module).
240         $draftid_editor = 0;
241         file_prepare_draft_area($draftid_editor, null, null, null, null);
242         $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
244         // Following is the advanced grading method area called 'submissions' for the 'assign' module.
245         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
246             $moduleinfo->grade = 100;
247         }
249         // Plagiarism form values.
250         // No plagiarism plugin installed by default. Use this space to make your own test.
252         // Values specific to the module.
253         $modulesetvalues = $modulename.'_create_set_values';
254         $this->$modulesetvalues($moduleinfo);
256         // Create the module.
257         $result = create_module($moduleinfo);
259         // Retrieve the module info.
260         $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance));
261         $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance);
262         // We passed the course section number to create_courses but $dbcm contain the section id.
263         // We need to retrieve the db course section number.
264         $section = $DB->get_record('course_sections', array('course' => $dbcm->course, 'id' => $dbcm->section));
265         // Retrieve the grade item.
266         $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course,
267             'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename));
269         // Compare the values common to all module instances.
270         $this->assertEquals($moduleinfo->modulename, $dbcm->modname);
271         $this->assertEquals($moduleinfo->section, $section->section);
272         $this->assertEquals($moduleinfo->course, $dbcm->course);
273         $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
274         $this->assertEquals($moduleinfo->groupmembersonly, $dbcm->groupmembersonly);
275         $this->assertEquals($moduleinfo->visible, $dbcm->visible);
276         $this->assertEquals($moduleinfo->completion, $dbcm->completion);
277         $this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
278         $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber);
279         $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected);
280         $this->assertEquals($moduleinfo->availablefrom, $dbcm->availablefrom);
281         $this->assertEquals($moduleinfo->availableuntil, $dbcm->availableuntil);
282         $this->assertEquals($moduleinfo->showavailability, $dbcm->showavailability);
283         $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription);
284         $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode);
285         $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber);
286         $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid);
288         // Optional grade testing.
289         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
290             $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade);
291         }
293         // Some optional (but quite common) to some module.
294         $this->assertEquals($moduleinfo->name, $dbmodinstance->name);
295         $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro);
296         $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat);
298         // Common values when conditional activity is enabled.
299         foreach ($moduleinfo->conditionfieldgroup as $fieldgroup) {
300             $isfieldgroupsaved = $DB->count_records('course_modules_avail_fields', array('coursemoduleid' => $dbcm->id,
301                 'userfield' => $fieldgroup['conditionfield'], 'operator' => $fieldgroup['conditionfieldoperator'],
302                 'value' => $fieldgroup['conditionfieldvalue']));
303             $this->assertEquals(1, $isfieldgroupsaved);
304         }
305         foreach ($moduleinfo->conditiongradegroup as $gradegroup) {
306             $isgradegroupsaved = $DB->count_records('course_modules_availability', array('coursemoduleid' => $dbcm->id,
307                 'grademin' => $gradegroup['conditiongrademin'], 'grademax' => $gradegroup['conditiongrademax'],
308                 'gradeitemid' => $gradegroup['conditiongradeitemid']));
309             $this->assertEquals(1, $isgradegroupsaved);
310         }
311         foreach ($moduleinfo->conditioncompletiongroup as $completiongroup) {
312             $iscompletiongroupsaved = $DB->count_records('course_modules_availability', array('coursemoduleid' => $dbcm->id,
313                 'sourcecmid' => $completiongroup['conditionsourcecmid'], 'requiredcompletion' => $completiongroup['conditionrequiredcompletion']));
314             $this->assertEquals(1, $iscompletiongroupsaved);
315         }
317         // Test specific to the module.
318         $modulerunasserts = $modulename.'_create_run_asserts';
319         $this->$modulerunasserts($moduleinfo, $dbmodinstance);
320     }
322     /**
323      * Test create_module() for multiple modules defined in the $modules array (first declaration of the function).
324      */
325     public function test_create_module() {
326         // Add the module name you want to test here.
327         // Create the match MODULENAME_create_set_values() and MODULENAME_create_run_asserts().
328         $modules = array('forum', 'assign');
329         // Run all tests.
330         foreach ($modules as $modulename) {
331             $this->create_specific_module_test($modulename);
332         }
333     }
335     /**
336      * Test update_module() for multiple modules defined in the $modules array (first declaration of the function).
337      */
338     public function test_update_module() {
339         // Add the module name you want to test here.
340         // Create the match MODULENAME_update_set_values() and MODULENAME_update_run_asserts().
341         $modules = array('forum');
342         // Run all tests.
343         foreach ($modules as $modulename) {
344             $this->update_specific_module_test($modulename);
345         }
346     }
348     /**
349      * Set forum specific test values for calling update_module().
350      *
351      * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
352      */
353     private function forum_update_set_values(&$moduleinfo) {
354         // Completion specific to forum - optional.
355         $moduleinfo->completionposts = 3;
356         $moduleinfo->completiondiscussions = 1;
357         $moduleinfo->completionreplies = 2;
359         // Specific values to the Forum module.
360         $moduleinfo->forcesubscribe = FORUM_INITIALSUBSCRIBE;
361         $moduleinfo->type = 'single';
362         $moduleinfo->trackingtype = FORUM_TRACKING_ON;
363         $moduleinfo->maxbytes = 10240;
364         $moduleinfo->maxattachments = 2;
366         // Post threshold for blocking - specific to forum.
367         $moduleinfo->blockperiod = 60*60*24;
368         $moduleinfo->blockafter = 10;
369         $moduleinfo->warnafter = 5;
370     }
372     /**
373      * Execute test asserts on the saved DB data by update_module($forum).
374      *
375      * @param object $moduleinfo - the specific forum values that were used to update a forum.
376      * @param object $dbmodinstance - the DB values of the updated forum.
377      */
378     private function forum_update_run_asserts($moduleinfo, $dbmodinstance) {
379         // Compare values specific to forums.
380         $this->assertEquals($moduleinfo->forcesubscribe, $dbmodinstance->forcesubscribe);
381         $this->assertEquals($moduleinfo->type, $dbmodinstance->type);
382         $this->assertEquals($moduleinfo->assessed, $dbmodinstance->assessed);
383         $this->assertEquals($moduleinfo->completionposts, $dbmodinstance->completionposts);
384         $this->assertEquals($moduleinfo->completiondiscussions, $dbmodinstance->completiondiscussions);
385         $this->assertEquals($moduleinfo->completionreplies, $dbmodinstance->completionreplies);
386         $this->assertEquals($moduleinfo->scale, $dbmodinstance->scale);
387         $this->assertEquals($moduleinfo->assesstimestart, $dbmodinstance->assesstimestart);
388         $this->assertEquals($moduleinfo->assesstimefinish, $dbmodinstance->assesstimefinish);
389         $this->assertEquals($moduleinfo->rsstype, $dbmodinstance->rsstype);
390         $this->assertEquals($moduleinfo->rssarticles, $dbmodinstance->rssarticles);
391         $this->assertEquals($moduleinfo->trackingtype, $dbmodinstance->trackingtype);
392         $this->assertEquals($moduleinfo->maxbytes, $dbmodinstance->maxbytes);
393         $this->assertEquals($moduleinfo->maxattachments, $dbmodinstance->maxattachments);
394         $this->assertEquals($moduleinfo->blockperiod, $dbmodinstance->blockperiod);
395         $this->assertEquals($moduleinfo->blockafter, $dbmodinstance->blockafter);
396         $this->assertEquals($moduleinfo->warnafter, $dbmodinstance->warnafter);
397     }
401     /**
402      * Test a specific type of module.
403      *
404      * @param string $modulename - the module name to test
405      */
406     private function update_specific_module_test($modulename) {
407         global $DB, $CFG;
409         $this->resetAfterTest(true);
411         $this->setAdminUser();
413         // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
414         require_once($CFG->dirroot.'/mod/'. $modulename .'/lib.php');
416         // Enable avaibility.
417         // If not enabled all conditional fields will be ignored.
418         set_config('enableavailability', 1);
420         // Enable course completion.
421         // If not enabled all completion settings will be ignored.
422         set_config('enablecompletion', COMPLETION_ENABLED);
424         // Enable forum RSS feeds.
425         set_config('enablerssfeeds', 1);
426         set_config('forum_enablerssfeeds', 1);
428         $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED),
429            array('createsections'=>true));
431         $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
433         // Create assign module instance for testing gradeitem.
434         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
435         $params['course'] = $course->id;
436         $instance = $generator->create_instance($params);
437         $assigncm = get_coursemodule_from_instance('assign', $instance->id);
439         // Create the test forum to update.
440         $initvalues = new stdClass();
441         $initvalues->introformat = FORMAT_HTML;
442         $initvalues->course = $course->id;
443         $forum = self::getDataGenerator()->create_module('forum', $initvalues);
445         // Retrieve course module.
446         $cm = get_coursemodule_from_instance('forum', $forum->id);
448         // Module test values.
449         $moduleinfo = new stdClass();
451         // Always mandatory generic values to any module.
452         $moduleinfo->coursemodule = $cm->id;
453         $moduleinfo->modulename = $modulename;
454         $moduleinfo->course = $course->id;
455         $moduleinfo->groupingid = $grouping->id;
456         $moduleinfo->groupmembersonly = 0;
457         $moduleinfo->visible = true;
459         // Sometimes optional generic values for some modules.
460         $moduleinfo->name = 'My test module';
461         $moduleinfo->showdescription = 1; // standard boolean
462         require_once($CFG->libdir . '/gradelib.php');
463         $gradecats = grade_get_categories_menu($moduleinfo->course, false);
464         $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
465         $moduleinfo->gradecat = $gradecatid;
466         $moduleinfo->groupmode = VISIBLEGROUPS;
467         $moduleinfo->cmidnumber = 'idnumber_XXX';
469         // Completion common to all module.
470         $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC;
471         $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED;
472         $moduleinfo->completiongradeitemnumber = 1;
473         $moduleinfo->completionexpected = time() + (7 * 24 * 3600);
474         $moduleinfo->completionunlocked = 1;
476         // Conditional activity.
477         $moduleinfo->availablefrom = time();
478         $moduleinfo->availableuntil = time() + (7 * 24 * 3600);
479         $moduleinfo->showavailability = CONDITION_STUDENTVIEW_SHOW;
480         $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself.
481         $moduleinfo->conditiongradegroup = array(array('conditiongradeitemid' => $coursegradeitem->id, 'conditiongrademin' => 10, 'conditiongrademax' => 80));
482         $moduleinfo->conditionfieldgroup = array(array('conditionfield' => 'email', 'conditionfieldoperator' => OP_CONTAINS, 'conditionfieldvalue' => '@'));
483         $moduleinfo->conditioncompletiongroup = array(array('conditionsourcecmid' => $assigncm->id, 'conditionrequiredcompletion' => COMPLETION_COMPLETE)); // "conditionsourcecmid == 0" => none
485         // Grading and Advanced grading.
486         require_once($CFG->dirroot . '/rating/lib.php');
487         $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE;
488         $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number.
489         $moduleinfo->assesstimestart = time();
490         $moduleinfo->assesstimefinish = time() + (7 * 24 * 3600);
492         // RSS.
493         $moduleinfo->rsstype = 2;
494         $moduleinfo->rssarticles = 10;
496         // Optional intro editor (depends of module).
497         $draftid_editor = 0;
498         file_prepare_draft_area($draftid_editor, null, null, null, null);
499         $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
501         // Following is the advanced grading method area called 'submissions' for the 'assign' module.
502         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
503             $moduleinfo->grade = 100;
504         }
505         // Plagiarism form values.
506         // No plagiarism plugin installed by default. Use this space to make your own test.
508         // Values specific to the module.
509         $modulesetvalues = $modulename.'_update_set_values';
510         $this->$modulesetvalues($moduleinfo);
512         // Create the module.
513         $result = update_module($moduleinfo);
515         // Retrieve the module info.
516         $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance));
517         $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance);
518         // Retrieve the grade item.
519         $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course,
520             'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename));
522         // Compare the values common to all module instances.
523         $this->assertEquals($moduleinfo->modulename, $dbcm->modname);
524         $this->assertEquals($moduleinfo->course, $dbcm->course);
525         $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
526         $this->assertEquals($moduleinfo->groupmembersonly, $dbcm->groupmembersonly);
527         $this->assertEquals($moduleinfo->visible, $dbcm->visible);
528         $this->assertEquals($moduleinfo->completion, $dbcm->completion);
529         $this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
530         $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber);
531         $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected);
532         $this->assertEquals($moduleinfo->availablefrom, $dbcm->availablefrom);
533         $this->assertEquals($moduleinfo->availableuntil, $dbcm->availableuntil);
534         $this->assertEquals($moduleinfo->showavailability, $dbcm->showavailability);
535         $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription);
536         $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode);
537         $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber);
538         $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid);
540         // Optional grade testing.
541         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
542             $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade);
543         }
545         // Some optional (but quite common) to some module.
546         $this->assertEquals($moduleinfo->name, $dbmodinstance->name);
547         $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro);
548         $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat);
550         // Common values when conditional activity is enabled.
551         foreach ($moduleinfo->conditionfieldgroup as $fieldgroup) {
552             $isfieldgroupsaved = $DB->count_records('course_modules_avail_fields', array('coursemoduleid' => $dbcm->id,
553                 'userfield' => $fieldgroup['conditionfield'], 'operator' => $fieldgroup['conditionfieldoperator'],
554                 'value' => $fieldgroup['conditionfieldvalue']));
555             $this->assertEquals(1, $isfieldgroupsaved);
556         }
557         foreach ($moduleinfo->conditiongradegroup as $gradegroup) {
558             $isgradegroupsaved = $DB->count_records('course_modules_availability', array('coursemoduleid' => $dbcm->id,
559                 'grademin' => $gradegroup['conditiongrademin'], 'grademax' => $gradegroup['conditiongrademax'],
560                 'gradeitemid' => $gradegroup['conditiongradeitemid']));
561             $this->assertEquals(1, $isgradegroupsaved);
562         }
563         foreach ($moduleinfo->conditioncompletiongroup as $completiongroup) {
564             $iscompletiongroupsaved = $DB->count_records('course_modules_availability', array('coursemoduleid' => $dbcm->id,
565                 'sourcecmid' => $completiongroup['conditionsourcecmid'], 'requiredcompletion' => $completiongroup['conditionrequiredcompletion']));
566             $this->assertEquals(1, $iscompletiongroupsaved);
567         }
569         // Test specific to the module.
570         $modulerunasserts = $modulename.'_update_run_asserts';
571         $this->$modulerunasserts($moduleinfo, $dbmodinstance);
572    }
575     public function test_create_course() {
576         global $DB;
577         $this->resetAfterTest(true);
578         $defaultcategory = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
580         $course = new stdClass();
581         $course->fullname = 'Apu loves Unit Təsts';
582         $course->shortname = 'Spread the lŭve';
583         $course->idnumber = '123';
584         $course->summary = 'Awesome!';
585         $course->summaryformat = FORMAT_PLAIN;
586         $course->format = 'topics';
587         $course->newsitems = 0;
588         $course->numsections = 5;
589         $course->category = $defaultcategory;
590         $original = (array) $course;
592         $created = create_course($course);
593         $context = context_course::instance($created->id);
595         // Compare original and created.
596         $this->assertEquals($original, array_intersect_key((array) $created, $original));
598         // Ensure default section is created.
599         $sectioncreated = $DB->record_exists('course_sections', array('course' => $created->id, 'section' => 0));
600         $this->assertTrue($sectioncreated);
602         // Ensure blocks have been associated to the course.
603         $blockcount = $DB->count_records('block_instances', array('parentcontextid' => $context->id));
604         $this->assertGreaterThan(0, $blockcount);
605     }
607     public function test_create_course_with_generator() {
608         global $DB;
609         $this->resetAfterTest(true);
610         $course = $this->getDataGenerator()->create_course();
612         // Ensure default section is created.
613         $sectioncreated = $DB->record_exists('course_sections', array('course' => $course->id, 'section' => 0));
614         $this->assertTrue($sectioncreated);
615     }
617     public function test_create_course_sections() {
618         global $DB;
619         $this->resetAfterTest(true);
621         $course = $this->getDataGenerator()->create_course(
622                 array('shortname' => 'GrowingCourse',
623                     'fullname' => 'Growing Course',
624                     'numsections' => 5),
625                 array('createsections' => true));
627         // Ensure all 6 (0-5) sections were created and modinfo/sectioninfo cache works properly
628         $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
629         $this->assertEquals(range(0, $course->numsections), $sectionscreated);
631         // this will do nothing, section already exists
632         $this->assertFalse(course_create_sections_if_missing($course, $course->numsections));
634         // this will create new section
635         $this->assertTrue(course_create_sections_if_missing($course, $course->numsections + 1));
637         // Ensure all 7 (0-6) sections were created and modinfo/sectioninfo cache works properly
638         $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
639         $this->assertEquals(range(0, $course->numsections + 1), $sectionscreated);
640     }
642     public function test_course_add_cm_to_section() {
643         global $DB;
644         $this->resetAfterTest(true);
646         // Create course with 1 section.
647         $course = $this->getDataGenerator()->create_course(
648                 array('shortname' => 'GrowingCourse',
649                     'fullname' => 'Growing Course',
650                     'numsections' => 1),
651                 array('createsections' => true));
653         // Trash modinfo.
654         rebuild_course_cache($course->id, true);
656         // Create some cms for testing.
657         $cmids = array();
658         for ($i=0; $i<4; $i++) {
659             $cmids[$i] = $DB->insert_record('course_modules', array('course' => $course->id));
660         }
662         // Add it to section that exists.
663         course_add_cm_to_section($course, $cmids[0], 1);
665         // Check it got added to sequence.
666         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 1));
667         $this->assertEquals($cmids[0], $sequence);
669         // Add a second, this time using courseid variant of parameters.
670         course_add_cm_to_section($course->id, $cmids[1], 1);
671         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 1));
672         $this->assertEquals($cmids[0] . ',' . $cmids[1], $sequence);
674         // Check modinfo was not rebuilt (important for performance if calling
675         // repeatedly).
676         $this->assertNull($DB->get_field('course', 'modinfo', array('id' => $course->id)));
678         // Add one to section that doesn't exist (this might rebuild modinfo).
679         course_add_cm_to_section($course, $cmids[2], 2);
680         $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id)));
681         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 2));
682         $this->assertEquals($cmids[2], $sequence);
684         // Add using the 'before' option.
685         course_add_cm_to_section($course, $cmids[3], 2, $cmids[2]);
686         $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id)));
687         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 2));
688         $this->assertEquals($cmids[3] . ',' . $cmids[2], $sequence);
689     }
691     public function test_reorder_sections() {
692         global $DB;
693         $this->resetAfterTest(true);
695         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
696         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
697         $oldsections = array();
698         $sections = array();
699         foreach ($DB->get_records('course_sections', array('course'=>$course->id), 'id') as $section) {
700             $oldsections[$section->section] = $section->id;
701             $sections[$section->id] = $section->section;
702         }
703         ksort($oldsections);
705         $neworder = reorder_sections($sections, 2, 4);
706         $neworder = array_keys($neworder);
707         $this->assertEquals($oldsections[0], $neworder[0]);
708         $this->assertEquals($oldsections[1], $neworder[1]);
709         $this->assertEquals($oldsections[2], $neworder[4]);
710         $this->assertEquals($oldsections[3], $neworder[2]);
711         $this->assertEquals($oldsections[4], $neworder[3]);
712         $this->assertEquals($oldsections[5], $neworder[5]);
713         $this->assertEquals($oldsections[6], $neworder[6]);
715         $neworder = reorder_sections($sections, 4, 2);
716         $neworder = array_keys($neworder);
717         $this->assertEquals($oldsections[0], $neworder[0]);
718         $this->assertEquals($oldsections[1], $neworder[1]);
719         $this->assertEquals($oldsections[2], $neworder[3]);
720         $this->assertEquals($oldsections[3], $neworder[4]);
721         $this->assertEquals($oldsections[4], $neworder[2]);
722         $this->assertEquals($oldsections[5], $neworder[5]);
723         $this->assertEquals($oldsections[6], $neworder[6]);
725         $neworder = reorder_sections(1, 2, 4);
726         $this->assertFalse($neworder);
727     }
729     public function test_move_section_down() {
730         global $DB;
731         $this->resetAfterTest(true);
733         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
734         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
735         $oldsections = array();
736         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
737             $oldsections[$section->section] = $section->id;
738         }
739         ksort($oldsections);
741         // Test move section down..
742         move_section_to($course, 2, 4);
743         $sections = array();
744         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
745             $sections[$section->section] = $section->id;
746         }
747         ksort($sections);
749         $this->assertEquals($oldsections[0], $sections[0]);
750         $this->assertEquals($oldsections[1], $sections[1]);
751         $this->assertEquals($oldsections[2], $sections[4]);
752         $this->assertEquals($oldsections[3], $sections[2]);
753         $this->assertEquals($oldsections[4], $sections[3]);
754         $this->assertEquals($oldsections[5], $sections[5]);
755         $this->assertEquals($oldsections[6], $sections[6]);
756     }
758     public function test_move_section_up() {
759         global $DB;
760         $this->resetAfterTest(true);
762         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
763         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
764         $oldsections = array();
765         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
766             $oldsections[$section->section] = $section->id;
767         }
768         ksort($oldsections);
770         // Test move section up..
771         move_section_to($course, 6, 4);
772         $sections = array();
773         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
774             $sections[$section->section] = $section->id;
775         }
776         ksort($sections);
778         $this->assertEquals($oldsections[0], $sections[0]);
779         $this->assertEquals($oldsections[1], $sections[1]);
780         $this->assertEquals($oldsections[2], $sections[2]);
781         $this->assertEquals($oldsections[3], $sections[3]);
782         $this->assertEquals($oldsections[4], $sections[5]);
783         $this->assertEquals($oldsections[5], $sections[6]);
784         $this->assertEquals($oldsections[6], $sections[4]);
785     }
787     public function test_move_section_marker() {
788         global $DB;
789         $this->resetAfterTest(true);
791         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
792         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
794         // Set course marker to the section we are going to move..
795         course_set_marker($course->id, 2);
796         // Verify that the course marker is set correctly.
797         $course = $DB->get_record('course', array('id' => $course->id));
798         $this->assertEquals(2, $course->marker);
800         // Test move the marked section down..
801         move_section_to($course, 2, 4);
803         // Verify that the coruse marker has been moved along with the section..
804         $course = $DB->get_record('course', array('id' => $course->id));
805         $this->assertEquals(4, $course->marker);
807         // Test move the marked section up..
808         move_section_to($course, 4, 3);
810         // Verify that the course marker has been moved along with the section..
811         $course = $DB->get_record('course', array('id' => $course->id));
812         $this->assertEquals(3, $course->marker);
814         // Test moving a non-marked section above the marked section..
815         move_section_to($course, 4, 2);
817         // Verify that the course marker has been moved down to accomodate..
818         $course = $DB->get_record('course', array('id' => $course->id));
819         $this->assertEquals(4, $course->marker);
821         // Test moving a non-marked section below the marked section..
822         move_section_to($course, 3, 6);
824         // Verify that the course marker has been up to accomodate..
825         $course = $DB->get_record('course', array('id' => $course->id));
826         $this->assertEquals(3, $course->marker);
827     }
829     public function test_get_course_display_name_for_list() {
830         global $CFG;
831         $this->resetAfterTest(true);
833         $course = $this->getDataGenerator()->create_course(array('shortname' => 'FROG101', 'fullname' => 'Introduction to pond life'));
835         $CFG->courselistshortnames = 0;
836         $this->assertEquals('Introduction to pond life', get_course_display_name_for_list($course));
838         $CFG->courselistshortnames = 1;
839         $this->assertEquals('FROG101 Introduction to pond life', get_course_display_name_for_list($course));
840     }
842     public function test_move_module_in_course() {
843         global $DB;
845         $this->resetAfterTest(true);
846         // Setup fixture
847         $course = $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections' => true));
848         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
850         $cms = get_fast_modinfo($course)->get_cms();
851         $cm = reset($cms);
853         $newsection = get_fast_modinfo($course)->get_section_info(3);
854         $oldsectionid = $cm->section;
856         // Perform the move
857         moveto_module($cm, $newsection);
859         $cms = get_fast_modinfo($course)->get_cms();
860         $cm = reset($cms);
862         // Check that the cached modinfo contains the correct section info
863         $modinfo = get_fast_modinfo($course);
864         $this->assertTrue(empty($modinfo->sections[0]));
865         $this->assertFalse(empty($modinfo->sections[3]));
867         // Check that the old section's sequence no longer contains this ID
868         $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
869         $oldsequences = explode(',', $newsection->sequence);
870         $this->assertFalse(in_array($cm->id, $oldsequences));
872         // Check that the new section's sequence now contains this ID
873         $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
874         $newsequences = explode(',', $newsection->sequence);
875         $this->assertTrue(in_array($cm->id, $newsequences));
877         // Check that the section number has been changed in the cm
878         $this->assertEquals($newsection->id, $cm->section);
881         // Perform a second move as some issues were only seen on the second move
882         $newsection = get_fast_modinfo($course)->get_section_info(2);
883         $oldsectionid = $cm->section;
884         moveto_module($cm, $newsection);
886         $cms = get_fast_modinfo($course)->get_cms();
887         $cm = reset($cms);
889         // Check that the cached modinfo contains the correct section info
890         $modinfo = get_fast_modinfo($course);
891         $this->assertTrue(empty($modinfo->sections[0]));
892         $this->assertFalse(empty($modinfo->sections[2]));
894         // Check that the old section's sequence no longer contains this ID
895         $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
896         $oldsequences = explode(',', $newsection->sequence);
897         $this->assertFalse(in_array($cm->id, $oldsequences));
899         // Check that the new section's sequence now contains this ID
900         $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
901         $newsequences = explode(',', $newsection->sequence);
902         $this->assertTrue(in_array($cm->id, $newsequences));
903     }
905     public function test_module_visibility() {
906         $this->setAdminUser();
907         $this->resetAfterTest(true);
909         // Create course and modules.
910         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
911         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
912         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(), 'course' => $course->id));
913         $modules = compact('forum', 'assign');
915         // Hiding the modules.
916         foreach ($modules as $mod) {
917             set_coursemodule_visible($mod->cmid, 0);
918             $this->check_module_visibility($mod, 0, 0);
919         }
921         // Showing the modules.
922         foreach ($modules as $mod) {
923             set_coursemodule_visible($mod->cmid, 1);
924             $this->check_module_visibility($mod, 1, 1);
925         }
926     }
928     public function test_section_visibility() {
929         $this->setAdminUser();
930         $this->resetAfterTest(true);
932         // Create course.
933         $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
935         // Testing an empty section.
936         $sectionnumber = 1;
937         set_section_visible($course->id, $sectionnumber, 0);
938         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
939         $this->assertEquals($section_info->visible, 0);
940         set_section_visible($course->id, $sectionnumber, 1);
941         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
942         $this->assertEquals($section_info->visible, 1);
944         // Testing a section with visible modules.
945         $sectionnumber = 2;
946         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
947                 array('section' => $sectionnumber));
948         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
949                 'course' => $course->id), array('section' => $sectionnumber));
950         $modules = compact('forum', 'assign');
951         set_section_visible($course->id, $sectionnumber, 0);
952         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
953         $this->assertEquals($section_info->visible, 0);
954         foreach ($modules as $mod) {
955             $this->check_module_visibility($mod, 0, 1);
956         }
957         set_section_visible($course->id, $sectionnumber, 1);
958         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
959         $this->assertEquals($section_info->visible, 1);
960         foreach ($modules as $mod) {
961             $this->check_module_visibility($mod, 1, 1);
962         }
964         // Testing a section with hidden modules, which should stay hidden.
965         $sectionnumber = 3;
966         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
967                 array('section' => $sectionnumber));
968         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
969                 'course' => $course->id), array('section' => $sectionnumber));
970         $modules = compact('forum', 'assign');
971         foreach ($modules as $mod) {
972             set_coursemodule_visible($mod->cmid, 0);
973             $this->check_module_visibility($mod, 0, 0);
974         }
975         set_section_visible($course->id, $sectionnumber, 0);
976         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
977         $this->assertEquals($section_info->visible, 0);
978         foreach ($modules as $mod) {
979             $this->check_module_visibility($mod, 0, 0);
980         }
981         set_section_visible($course->id, $sectionnumber, 1);
982         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
983         $this->assertEquals($section_info->visible, 1);
984         foreach ($modules as $mod) {
985             $this->check_module_visibility($mod, 0, 0);
986         }
987     }
989     /**
990      * Helper function to assert that a module has correctly been made visible, or hidden.
991      *
992      * @param stdClass $mod module information
993      * @param int $visibility the current state of the module
994      * @param int $visibleold the current state of the visibleold property
995      * @return void
996      */
997     public function check_module_visibility($mod, $visibility, $visibleold) {
998         global $DB;
999         $cm = get_fast_modinfo($mod->course)->get_cm($mod->cmid);
1000         $this->assertEquals($visibility, $cm->visible);
1001         $this->assertEquals($visibleold, $cm->visibleold);
1003         // Check the module grade items.
1004         $grade_items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $cm->modname,
1005                 'iteminstance' => $cm->instance, 'courseid' => $cm->course));
1006         if ($grade_items) {
1007             foreach ($grade_items as $grade_item) {
1008                 if ($visibility) {
1009                     $this->assertFalse($grade_item->is_hidden(), "$cm->modname grade_item not visible");
1010                 } else {
1011                     $this->assertTrue($grade_item->is_hidden(), "$cm->modname grade_item not hidden");
1012                 }
1013             }
1014         }
1016         // Check the events visibility.
1017         if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $cm->modname))) {
1018             foreach ($events as $event) {
1019                 $calevent = new calendar_event($event);
1020                 $this->assertEquals($visibility, $calevent->visible, "$cm->modname calendar_event visibility");
1021             }
1022         }
1023     }
1025     public function test_course_page_type_list() {
1026         global $DB;
1027         $this->resetAfterTest(true);
1029         // Create a category.
1030         $category = new stdClass();
1031         $category->name = 'Test Category';
1033         $testcategory = $this->getDataGenerator()->create_category($category);
1035         // Create a course.
1036         $course = new stdClass();
1037         $course->fullname = 'Apu loves Unit Təsts';
1038         $course->shortname = 'Spread the lŭve';
1039         $course->idnumber = '123';
1040         $course->summary = 'Awesome!';
1041         $course->summaryformat = FORMAT_PLAIN;
1042         $course->format = 'topics';
1043         $course->newsitems = 0;
1044         $course->numsections = 5;
1045         $course->category = $testcategory->id;
1047         $testcourse = $this->getDataGenerator()->create_course($course);
1049         // Create contexts.
1050         $coursecontext = context_course::instance($testcourse->id);
1051         $parentcontext = $coursecontext->get_parent_context(); // Not actually used.
1052         $pagetype = 'page-course-x'; // Not used either.
1053         $pagetypelist = course_page_type_list($pagetype, $parentcontext, $coursecontext);
1055         // Page type lists for normal courses.
1056         $testpagetypelist1 = array();
1057         $testpagetypelist1['*'] = 'Any page';
1058         $testpagetypelist1['course-*'] = 'Any course page';
1059         $testpagetypelist1['course-view-*'] = 'Any type of course main page';
1061         $this->assertEquals($testpagetypelist1, $pagetypelist);
1063         // Get the context for the front page course.
1064         $sitecoursecontext = context_course::instance(SITEID);
1065         $pagetypelist = course_page_type_list($pagetype, $parentcontext, $sitecoursecontext);
1067         // Page type list for the front page course.
1068         $testpagetypelist2 = array('*' => 'Any page');
1069         $this->assertEquals($testpagetypelist2, $pagetypelist);
1071         // Make sure that providing no current context to the function doesn't result in an error.
1072         // Calls made from generate_page_type_patterns() may provide null values.
1073         $pagetypelist = course_page_type_list($pagetype, null, null);
1074         $this->assertEquals($pagetypelist, $testpagetypelist1);
1075     }
1077     public function test_compare_activities_by_time_desc() {
1079         // Let's create some test data.
1080         $activitiesivities = array();
1081         $x = new stdClass();
1082         $x->timestamp = null;
1083         $activities[] = $x;
1085         $x = new stdClass();
1086         $x->timestamp = 1;
1087         $activities[] = $x;
1089         $x = new stdClass();
1090         $x->timestamp = 3;
1091         $activities[] = $x;
1093         $x = new stdClass();
1094         $x->timestamp = 0;
1095         $activities[] = $x;
1097         $x = new stdClass();
1098         $x->timestamp = 5;
1099         $activities[] = $x;
1101         $x = new stdClass();
1102         $activities[] = $x;
1104         $x = new stdClass();
1105         $x->timestamp = 5;
1106         $activities[] = $x;
1108         // Do the sorting.
1109         usort($activities, 'compare_activities_by_time_desc');
1111         // Let's check the result.
1112         $last = 10;
1113         foreach($activities as $activity) {
1114             if (empty($activity->timestamp)) {
1115                 $activity->timestamp = 0;
1116             }
1117             $this->assertLessThanOrEqual($last, $activity->timestamp);
1118         }
1119     }
1121     public function test_compare_activities_by_time_asc() {
1123         // Let's create some test data.
1124         $activities = array();
1125         $x = new stdClass();
1126         $x->timestamp = null;
1127         $activities[] = $x;
1129         $x = new stdClass();
1130         $x->timestamp = 1;
1131         $activities[] = $x;
1133         $x = new stdClass();
1134         $x->timestamp = 3;
1135         $activities[] = $x;
1137         $x = new stdClass();
1138         $x->timestamp = 0;
1139         $activities[] = $x;
1141         $x = new stdClass();
1142         $x->timestamp = 5;
1143         $activities[] = $x;
1145         $x = new stdClass();
1146         $activities[] = $x;
1148         $x = new stdClass();
1149         $x->timestamp = 5;
1150         $activities[] = $x;
1152         // Do the sorting.
1153         usort($activities, 'compare_activities_by_time_asc');
1155         // Let's check the result.
1156         $last = 0;
1157         foreach($activities as $activity) {
1158             if (empty($activity->timestamp)) {
1159                 $activity->timestamp = 0;
1160             }
1161             $this->assertGreaterThanOrEqual($last, $activity->timestamp);
1162         }
1163     }
1165     /**
1166      * Tests moving a module between hidden/visible sections and
1167      * verifies that the course/module visiblity seettings are
1168      * retained.
1169      */
1170     public function test_moveto_module_between_hidden_sections() {
1171         global $DB;
1173         $this->resetAfterTest(true);
1175         $course = $this->getDataGenerator()->create_course(array('numsections' => 4), array('createsections' => true));
1176         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1177         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
1178         $quiz= $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
1180         // Set the page as hidden
1181         set_coursemodule_visible($page->cmid, 0);
1183         // Set sections 3 as hidden.
1184         set_section_visible($course->id, 3, 0);
1186         $modinfo = get_fast_modinfo($course);
1188         $hiddensection = $modinfo->get_section_info(3);
1189         // New section is definitely not visible:
1190         $this->assertEquals($hiddensection->visible, 0);
1192         $forumcm = $modinfo->cms[$forum->cmid];
1193         $pagecm = $modinfo->cms[$page->cmid];
1195         // Move the forum and the page to a hidden section, make sure moveto_module returns 0 as new visibility state.
1196         $this->assertEquals(0, moveto_module($forumcm, $hiddensection));
1197         $this->assertEquals(0, moveto_module($pagecm, $hiddensection));
1199         $modinfo = get_fast_modinfo($course);
1201         // Verify that forum and page have been moved to the hidden section and quiz has not.
1202         $this->assertContains($forum->cmid, $modinfo->sections[3]);
1203         $this->assertContains($page->cmid, $modinfo->sections[3]);
1204         $this->assertNotContains($quiz->cmid, $modinfo->sections[3]);
1206         // Verify that forum has been made invisible.
1207         $forumcm = $modinfo->cms[$forum->cmid];
1208         $this->assertEquals($forumcm->visible, 0);
1209         // Verify that old state has been retained.
1210         $this->assertEquals($forumcm->visibleold, 1);
1212         // Verify that page has stayed invisible.
1213         $pagecm = $modinfo->cms[$page->cmid];
1214         $this->assertEquals($pagecm->visible, 0);
1215         // Verify that old state has been retained.
1216         $this->assertEquals($pagecm->visibleold, 0);
1218         // Verify that quiz has been unaffected.
1219         $quizcm = $modinfo->cms[$quiz->cmid];
1220         $this->assertEquals($quizcm->visible, 1);
1222         // Move forum and page back to visible section.
1223         // Make sure the visibility is restored to the original value (visible for forum and hidden for page).
1224         $visiblesection = $modinfo->get_section_info(2);
1225         $this->assertEquals(1, moveto_module($forumcm, $visiblesection));
1226         $this->assertEquals(0, moveto_module($pagecm, $visiblesection));
1228         $modinfo = get_fast_modinfo($course);
1230         // Double check that forum has been made visible.
1231         $forumcm = $modinfo->cms[$forum->cmid];
1232         $this->assertEquals($forumcm->visible, 1);
1234         // Double check that page has stayed invisible.
1235         $pagecm = $modinfo->cms[$page->cmid];
1236         $this->assertEquals($pagecm->visible, 0);
1238         // Move the page in the same section (this is what mod duplicate does).
1239         // Visibility of page remains 0.
1240         $this->assertEquals(0, moveto_module($pagecm, $visiblesection, $forumcm));
1242         // Double check that the the page is still hidden.
1243         $modinfo = get_fast_modinfo($course);
1244         $pagecm = $modinfo->cms[$page->cmid];
1245         $this->assertEquals($pagecm->visible, 0);
1246     }
1248     /**
1249      * Tests moving a module around in the same section. moveto_module()
1250      * is called this way in modduplicate.
1251      */
1252     public function test_moveto_module_in_same_section() {
1253         global $DB;
1255         $this->resetAfterTest(true);
1257         $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
1258         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
1259         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1261         // Simulate inconsistent visible/visibleold values (MDL-38713).
1262         $cm = $DB->get_record('course_modules', array('id' => $page->cmid), '*', MUST_EXIST);
1263         $cm->visible = 0;
1264         $cm->visibleold = 1;
1265         $DB->update_record('course_modules', $cm);
1267         $modinfo = get_fast_modinfo($course);
1268         $forumcm = $modinfo->cms[$forum->cmid];
1269         $pagecm = $modinfo->cms[$page->cmid];
1271         // Verify that page is hidden.
1272         $this->assertEquals($pagecm->visible, 0);
1274         // Verify section 0 is where all mods added.
1275         $section = $modinfo->get_section_info(0);
1276         $this->assertEquals($section->id, $forumcm->section);
1277         $this->assertEquals($section->id, $pagecm->section);
1280         // Move the page inside the hidden section. Make sure it is hidden.
1281         $this->assertEquals(0, moveto_module($pagecm, $section, $forumcm));
1283         // Double check that the the page is still hidden.
1284         $modinfo = get_fast_modinfo($course);
1285         $pagecm = $modinfo->cms[$page->cmid];
1286         $this->assertEquals($pagecm->visible, 0);
1287     }
1289     public function test_course_delete_module() {
1290         global $DB;
1291         $this->resetAfterTest(true);
1292         $this->setAdminUser();
1294         // Create course and modules.
1295         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
1297         // Generate an assignment with due date (will generate a course event).
1298         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(), 'course' => $course->id));
1300         $cm = get_coursemodule_from_instance('assign', $assign->id);
1302         // Verify context exists.
1303         $this->assertInstanceOf('context_module', context_module::instance($cm->id, IGNORE_MISSING));
1305         // Verify event assignment event has been generated.
1306         $eventcount = $DB->count_records('event', array('instance' => $assign->id, 'modulename' => 'assign'));
1307         $this->assertEquals(1, $eventcount);
1309         // Run delete..
1310         course_delete_module($cm->id);
1312         // Verify the context has been removed.
1313         $this->assertFalse(context_module::instance($cm->id, IGNORE_MISSING));
1315         // Verify the course_module record has been deleted.
1316         $cmcount = $DB->count_records('course_modules', array('id' => $cm->id));
1317         $this->assertEmpty($cmcount);
1319         // Verify event assignment events have been removed.
1320         $eventcount = $DB->count_records('event', array('instance' => $assign->id, 'modulename' => 'assign'));
1321         $this->assertEmpty($eventcount);
1322     }
1324     /**
1325      * Test that triggering a course_created event works as expected.
1326      */
1327     public function test_course_created_event() {
1328         $this->resetAfterTest();
1330         // Catch the events.
1331         $sink = $this->redirectEvents();
1333         // Create the course.
1334         $course = $this->getDataGenerator()->create_course();
1336         // Capture the event.
1337         $events = $sink->get_events();
1338         $sink->close();
1340         // Validate the event.
1341         $event = $events[0];
1342         $this->assertInstanceOf('\core\event\course_created', $event);
1343         $this->assertEquals('course', $event->objecttable);
1344         $this->assertEquals($course->id, $event->objectid);
1345         $this->assertEquals(context_course::instance($course->id)->id, $event->contextid);
1346         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1347         $this->assertEquals('course_created', $event->get_legacy_eventname());
1348         $this->assertEventLegacyData($course, $event);
1349         $expectedlog = array(SITEID, 'course', 'new', 'view.php?id=' . $course->id, $course->fullname . ' (ID ' . $course->id . ')');
1350         $this->assertEventLegacyLogData($expectedlog, $event);
1351     }
1353     /**
1354      * Test that triggering a course_updated event works as expected.
1355      */
1356     public function test_course_updated_event() {
1357         global $DB;
1359         $this->resetAfterTest();
1361         // Create a course.
1362         $course = $this->getDataGenerator()->create_course();
1364         // Create a category we are going to move this course to.
1365         $category = $this->getDataGenerator()->create_category();
1367         // Create a hidden category we are going to move this course to.
1368         $categoryhidden = $this->getDataGenerator()->create_category(array('visible' => 0));
1370         // Catch the update events.
1371         $sink = $this->redirectEvents();
1373         // Keep track of the old sortorder.
1374         $sortorder = $course->sortorder;
1376         // Call update_course which will trigger a course_updated event.
1377         update_course($course);
1379         // Return the updated course information from the DB.
1380         $updatedcourse = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1382         // Now move the course to the category, this will also trigger an event.
1383         move_courses(array($course->id), $category->id);
1385         // Return the moved course information from the DB.
1386         $movedcourse = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1388         // Now move the course to the hidden category, this will also trigger an event.
1389         move_courses(array($course->id), $categoryhidden->id);
1391         // Return the moved course information from the DB.
1392         $movedcoursehidden = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1394         // Now we want to set the sortorder back to what it was before fix_course_sortorder() was called. The reason for
1395         // this is because update_course() and move_courses() call fix_course_sortorder() which alters the sort order in
1396         // the DB, but it does not set the value of the sortorder for the course object passed to the event.
1397         $updatedcourse->sortorder = $sortorder;
1398         $movedcourse->sortorder = $category->sortorder + MAX_COURSES_IN_CATEGORY - 1;
1399         $movedcoursehidden->sortorder = $categoryhidden->sortorder + MAX_COURSES_IN_CATEGORY - 1;
1401         // Capture the events.
1402         $events = $sink->get_events();
1403         $sink->close();
1405         // Validate the events.
1406         $event = $events[0];
1407         $this->assertInstanceOf('\core\event\course_updated', $event);
1408         $this->assertEquals('course', $event->objecttable);
1409         $this->assertEquals($updatedcourse->id, $event->objectid);
1410         $this->assertEquals(context_course::instance($updatedcourse->id)->id, $event->contextid);
1411         $this->assertEquals($updatedcourse, $event->get_record_snapshot('course', $updatedcourse->id));
1412         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1413         $this->assertEventLegacyData($updatedcourse, $event);
1414         $expectedlog = array($updatedcourse->id, 'course', 'update', 'edit.php?id=' . $course->id, $course->id);
1415         $this->assertEventLegacyLogData($expectedlog, $event);
1417         $event = $events[1];
1418         $this->assertInstanceOf('\core\event\course_updated', $event);
1419         $this->assertEquals('course', $event->objecttable);
1420         $this->assertEquals($movedcourse->id, $event->objectid);
1421         $this->assertEquals(context_course::instance($movedcourse->id)->id, $event->contextid);
1422         $this->assertEquals($movedcourse, $event->get_record_snapshot('course', $movedcourse->id));
1423         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1424         $this->assertEventLegacyData($movedcourse, $event);
1425         $expectedlog = array($movedcourse->id, 'course', 'move', 'edit.php?id=' . $movedcourse->id, $movedcourse->id);
1426         $this->assertEventLegacyLogData($expectedlog, $event);
1428         $event = $events[2];
1429         $this->assertInstanceOf('\core\event\course_updated', $event);
1430         $this->assertEquals('course', $event->objecttable);
1431         $this->assertEquals($movedcoursehidden->id, $event->objectid);
1432         $this->assertEquals(context_course::instance($movedcoursehidden->id)->id, $event->contextid);
1433         $this->assertEquals($movedcoursehidden, $event->get_record_snapshot('course', $movedcoursehidden->id));
1434         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1435         $this->assertEventLegacyData($movedcoursehidden, $event);
1436         $expectedlog = array($movedcoursehidden->id, 'course', 'move', 'edit.php?id=' . $movedcoursehidden->id, $movedcoursehidden->id);
1437         $this->assertEventLegacyLogData($expectedlog, $event);
1438     }
1440     /**
1441      * Test that triggering a course_deleted event works as expected.
1442      */
1443     public function test_course_deleted_event() {
1444         $this->resetAfterTest();
1446         // Create the course.
1447         $course = $this->getDataGenerator()->create_course();
1449         // Save the course context before we delete the course.
1450         $coursecontext = context_course::instance($course->id);
1452         // Catch the update event.
1453         $sink = $this->redirectEvents();
1455         // Call delete_course() which will trigger the course_deleted event and the course_content_deleted
1456         // event. This function prints out data to the screen, which we do not want during a PHPUnit test,
1457         // so use ob_start and ob_end_clean to prevent this.
1458         ob_start();
1459         delete_course($course);
1460         ob_end_clean();
1462         // Capture the event.
1463         $events = $sink->get_events();
1464         $sink->close();
1466         // Validate the event.
1467         $event = $events[1];
1468         $this->assertInstanceOf('\core\event\course_deleted', $event);
1469         $this->assertEquals('course', $event->objecttable);
1470         $this->assertEquals($course->id, $event->objectid);
1471         $this->assertEquals($coursecontext->id, $event->contextid);
1472         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1473         $this->assertEquals('course_deleted', $event->get_legacy_eventname());
1474         // The legacy data also passed the context in the course object.
1475         $course->context = $coursecontext;
1476         $this->assertEventLegacyData($course, $event);
1477         $expectedlog = array(SITEID, 'course', 'delete', 'view.php?id=' . $course->id, $course->fullname . '(ID ' . $course->id . ')');
1478         $this->assertEventLegacyLogData($expectedlog, $event);
1479     }
1481     /**
1482      * Test that triggering a course_content_deleted event works as expected.
1483      */
1484     public function test_course_content_deleted_event() {
1485         global $DB;
1487         $this->resetAfterTest();
1489         // Create the course.
1490         $course = $this->getDataGenerator()->create_course();
1492         // Get the course from the DB. The data generator adds some extra properties, such as
1493         // numsections, to the course object which will fail the assertions later on.
1494         $course = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1496         // Save the course context before we delete the course.
1497         $coursecontext = context_course::instance($course->id);
1499         // Catch the update event.
1500         $sink = $this->redirectEvents();
1502         // Call remove_course_contents() which will trigger the course_content_deleted event.
1503         // This function prints out data to the screen, which we do not want during a PHPUnit
1504         // test, so use ob_start and ob_end_clean to prevent this.
1505         ob_start();
1506         remove_course_contents($course->id);
1507         ob_end_clean();
1509         // Capture the event.
1510         $events = $sink->get_events();
1511         $sink->close();
1513         // Validate the event.
1514         $event = $events[0];
1515         $this->assertInstanceOf('\core\event\course_content_deleted', $event);
1516         $this->assertEquals('course', $event->objecttable);
1517         $this->assertEquals($course->id, $event->objectid);
1518         $this->assertEquals($coursecontext->id, $event->contextid);
1519         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1520         $this->assertEquals('course_content_removed', $event->get_legacy_eventname());
1521         // The legacy data also passed the context and options in the course object.
1522         $course->context = $coursecontext;
1523         $course->options = array();
1524         $this->assertEventLegacyData($course, $event);
1525     }
1527     /**
1528      * Test that triggering a course_category_deleted event works as expected.
1529      */
1530     public function test_course_category_deleted_event() {
1531         $this->resetAfterTest();
1533         // Create a category.
1534         $category = $this->getDataGenerator()->create_category();
1536         // Save the context before it is deleted.
1537         $categorycontext = context_coursecat::instance($category->id);
1539         // Catch the update event.
1540         $sink = $this->redirectEvents();
1542         // Delete the category.
1543         $category->delete_full();
1545         // Capture the event.
1546         $events = $sink->get_events();
1547         $sink->close();
1549         // Validate the event.
1550         $event = $events[0];
1551         $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1552         $this->assertEquals('course_categories', $event->objecttable);
1553         $this->assertEquals($category->id, $event->objectid);
1554         $this->assertEquals($categorycontext->id, $event->contextid);
1555         $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1556         $this->assertEventLegacyData($category, $event);
1557         $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category->name . '(ID ' . $category->id . ')');
1558         $this->assertEventLegacyLogData($expectedlog, $event);
1560         // Create two categories.
1561         $category = $this->getDataGenerator()->create_category();
1562         $category2 = $this->getDataGenerator()->create_category();
1564         // Save the context before it is moved and then deleted.
1565         $category2context = context_coursecat::instance($category2->id);
1567         // Catch the update event.
1568         $sink = $this->redirectEvents();
1570         // Move the category.
1571         $category2->delete_move($category->id);
1573         // Capture the event.
1574         $events = $sink->get_events();
1575         $sink->close();
1577         // Validate the event.
1578         $event = $events[0];
1579         $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1580         $this->assertEquals('course_categories', $event->objecttable);
1581         $this->assertEquals($category2->id, $event->objectid);
1582         $this->assertEquals($category2context->id, $event->contextid);
1583         $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1584         $this->assertEventLegacyData($category2, $event);
1585         $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category2->name . '(ID ' . $category2->id . ')');
1586         $this->assertEventLegacyLogData($expectedlog, $event);
1587     }
1589     /**
1590      * Test that triggering a course_restored event works as expected.
1591      */
1592     public function test_course_restored_event() {
1593         global $CFG;
1595         // Get the necessary files to perform backup and restore.
1596         require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
1597         require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
1599         $this->resetAfterTest();
1601         // Set to admin user.
1602         $this->setAdminUser();
1604         // The user id is going to be 2 since we are the admin user.
1605         $userid = 2;
1607         // Create a course.
1608         $course = $this->getDataGenerator()->create_course();
1610         // Create backup file and save it to the backup location.
1611         $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
1612             backup::INTERACTIVE_NO, backup::MODE_GENERAL, $userid);
1613         $bc->execute_plan();
1614         $results = $bc->get_results();
1615         $file = $results['backup_destination'];
1616         $fp = get_file_packer();
1617         $filepath = $CFG->dataroot . '/temp/backup/test-restore-course-event';
1618         $file->extract_to_pathname($fp, $filepath);
1619         $bc->destroy();
1620         unset($bc);
1622         // Now we want to catch the restore course event.
1623         $sink = $this->redirectEvents();
1625         // Now restore the course to trigger the event.
1626         $rc = new restore_controller('test-restore-course-event', $course->id, backup::INTERACTIVE_NO,
1627             backup::MODE_GENERAL, $userid, backup::TARGET_NEW_COURSE);
1628         $rc->execute_precheck();
1629         $rc->execute_plan();
1631         // Capture the event.
1632         $events = $sink->get_events();
1633         $sink->close();
1635         // Validate the event.
1636         $event = $events[0];
1637         $this->assertInstanceOf('\core\event\course_restored', $event);
1638         $this->assertEquals('course', $event->objecttable);
1639         $this->assertEquals($rc->get_courseid(), $event->objectid);
1640         $this->assertEquals(context_course::instance($rc->get_courseid())->id, $event->contextid);
1641         $this->assertEquals('course_restored', $event->get_legacy_eventname());
1642         $legacydata = (object) array(
1643             'courseid' => $rc->get_courseid(),
1644             'userid' => $rc->get_userid(),
1645             'type' => $rc->get_type(),
1646             'target' => $rc->get_target(),
1647             'mode' => $rc->get_mode(),
1648             'operation' => $rc->get_operation(),
1649             'samesite' => $rc->is_samesite()
1650         );
1651         $this->assertEventLegacyData($legacydata, $event);
1653         // Destroy the resource controller since we are done using it.
1654         $rc->destroy();
1655         unset($rc);
1657         // Clear the time limit, otherwise PHPUnit complains.
1658         set_time_limit(0);
1659     }
1661     /**
1662      * Test that triggering a course_section_updated event works as expected.
1663      */
1664     public function test_course_section_updated_event() {
1665         global $DB;
1667         $this->resetAfterTest();
1669         // Create the course with sections.
1670         $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true));
1671         $sections = $DB->get_records('course_sections', array('course' => $course->id));
1673         $coursecontext = context_course::instance($course->id);
1675         $section = array_pop($sections);
1676         $section->name = 'Test section';
1677         $section->summary = 'Test section summary';
1678         $DB->update_record('course_sections', $section);
1680         // Trigger an event for course section update.
1681         $event = \core\event\course_section_updated::create(
1682                 array(
1683                     'objectid' => $section->id,
1684                     'courseid' => $course->id,
1685                     'context' => context_course::instance($course->id)
1686                 )
1687             );
1688         $event->add_record_snapshot('course_sections', $section);
1689         // Trigger and catch event.
1690         $sink = $this->redirectEvents();
1691         $event->trigger();
1692         $events = $sink->get_events();
1693         $sink->close();
1695         // Validate the event.
1696         $event = $events[0];
1697         $this->assertInstanceOf('\core\event\course_section_updated', $event);
1698         $this->assertEquals('course_sections', $event->objecttable);
1699         $this->assertEquals($section->id, $event->objectid);
1700         $this->assertEquals($course->id, $event->courseid);
1701         $this->assertEquals($coursecontext->id, $event->contextid);
1702         $expecteddesc = 'Course ' . $event->courseid . ' section ' . $event->other['sectionnum'] . ' updated by user ' . $event->userid;
1703         $this->assertEquals($expecteddesc, $event->get_description());
1704         $this->assertEquals($section, $event->get_record_snapshot('course_sections', $event->objectid));
1705         $id = $section->id;
1706         $sectionnum = $section->section;
1707         $expectedlegacydata = array($course->id, "course", "editsection", 'editsection.php?id=' . $id, $sectionnum);
1708         $this->assertEventLegacyLogData($expectedlegacydata, $event);
1709     }