216db322cb4cb165778165de999a36db0b5feb79
[moodle.git] / course / tests / courselib_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * Course related unit tests
19  *
20  * @package    core
21  * @category   phpunit
22  * @copyright  2012 Petr Skoda {@link http://skodak.org}
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->dirroot . '/course/lib.php');
30 require_once($CFG->dirroot . '/course/tests/fixtures/course_capability_assignment.php');
31 require_once($CFG->dirroot . '/enrol/imsenterprise/tests/imsenterprise_test.php');
32 require_once($CFG->dirroot . '/tag/lib.php');
34 class core_course_courselib_testcase extends advanced_testcase {
36     /**
37      * Set forum specific test values for calling create_module().
38      *
39      * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
40      */
41     private function forum_create_set_values(&$moduleinfo) {
42         // Completion specific to forum - optional.
43         $moduleinfo->completionposts = 3;
44         $moduleinfo->completiondiscussions = 1;
45         $moduleinfo->completionreplies = 2;
47         // Specific values to the Forum module.
48         $moduleinfo->forcesubscribe = FORUM_INITIALSUBSCRIBE;
49         $moduleinfo->type = 'single';
50         $moduleinfo->trackingtype = FORUM_TRACKING_FORCED;
51         $moduleinfo->maxbytes = 10240;
52         $moduleinfo->maxattachments = 2;
54         // Post threshold for blocking - specific to forum.
55         $moduleinfo->blockperiod = 60*60*24;
56         $moduleinfo->blockafter = 10;
57         $moduleinfo->warnafter = 5;
58     }
60     /**
61      * Execute test asserts on the saved DB data by create_module($forum).
62      *
63      * @param object $moduleinfo - the specific forum values that were used to create a forum.
64      * @param object $dbmodinstance - the DB values of the created forum.
65      */
66     private function forum_create_run_asserts($moduleinfo, $dbmodinstance) {
67         // Compare values specific to forums.
68         $this->assertEquals($moduleinfo->forcesubscribe, $dbmodinstance->forcesubscribe);
69         $this->assertEquals($moduleinfo->type, $dbmodinstance->type);
70         $this->assertEquals($moduleinfo->assessed, $dbmodinstance->assessed);
71         $this->assertEquals($moduleinfo->completionposts, $dbmodinstance->completionposts);
72         $this->assertEquals($moduleinfo->completiondiscussions, $dbmodinstance->completiondiscussions);
73         $this->assertEquals($moduleinfo->completionreplies, $dbmodinstance->completionreplies);
74         $this->assertEquals($moduleinfo->scale, $dbmodinstance->scale);
75         $this->assertEquals($moduleinfo->assesstimestart, $dbmodinstance->assesstimestart);
76         $this->assertEquals($moduleinfo->assesstimefinish, $dbmodinstance->assesstimefinish);
77         $this->assertEquals($moduleinfo->rsstype, $dbmodinstance->rsstype);
78         $this->assertEquals($moduleinfo->rssarticles, $dbmodinstance->rssarticles);
79         $this->assertEquals($moduleinfo->trackingtype, $dbmodinstance->trackingtype);
80         $this->assertEquals($moduleinfo->maxbytes, $dbmodinstance->maxbytes);
81         $this->assertEquals($moduleinfo->maxattachments, $dbmodinstance->maxattachments);
82         $this->assertEquals($moduleinfo->blockperiod, $dbmodinstance->blockperiod);
83         $this->assertEquals($moduleinfo->blockafter, $dbmodinstance->blockafter);
84         $this->assertEquals($moduleinfo->warnafter, $dbmodinstance->warnafter);
85     }
87     /**
88      * Set assign module specific test values for calling create_module().
89      *
90      * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
91      */
92     private function assign_create_set_values(&$moduleinfo) {
93         // Specific values to the Assign module.
94         $moduleinfo->alwaysshowdescription = true;
95         $moduleinfo->submissiondrafts = true;
96         $moduleinfo->requiresubmissionstatement = true;
97         $moduleinfo->sendnotifications = true;
98         $moduleinfo->sendlatenotifications = true;
99         $moduleinfo->duedate = time() + (7 * 24 * 3600);
100         $moduleinfo->cutoffdate = time() + (7 * 24 * 3600);
101         $moduleinfo->allowsubmissionsfromdate = time();
102         $moduleinfo->teamsubmission = true;
103         $moduleinfo->requireallteammemberssubmit = true;
104         $moduleinfo->teamsubmissiongroupingid = true;
105         $moduleinfo->blindmarking = true;
106         $moduleinfo->markingworkflow = true;
107         $moduleinfo->markingallocation = true;
108         $moduleinfo->assignsubmission_onlinetext_enabled = true;
109         $moduleinfo->assignsubmission_file_enabled = true;
110         $moduleinfo->assignsubmission_file_maxfiles = 1;
111         $moduleinfo->assignsubmission_file_maxsizebytes = 1000000;
112         $moduleinfo->assignsubmission_comments_enabled = true;
113         $moduleinfo->assignfeedback_comments_enabled = true;
114         $moduleinfo->assignfeedback_offline_enabled = true;
115         $moduleinfo->assignfeedback_file_enabled = true;
117         // Advanced grading.
118         $gradingmethods = grading_manager::available_methods();
119         $moduleinfo->advancedgradingmethod_submissions = current(array_keys($gradingmethods));
120     }
122     /**
123      * Execute test asserts on the saved DB data by create_module($assign).
124      *
125      * @param object $moduleinfo - the specific assign module values that were used to create an assign module.
126      * @param object $dbmodinstance - the DB values of the created assign module.
127      */
128     private function assign_create_run_asserts($moduleinfo, $dbmodinstance) {
129         global $DB;
131         $this->assertEquals($moduleinfo->alwaysshowdescription, $dbmodinstance->alwaysshowdescription);
132         $this->assertEquals($moduleinfo->submissiondrafts, $dbmodinstance->submissiondrafts);
133         $this->assertEquals($moduleinfo->requiresubmissionstatement, $dbmodinstance->requiresubmissionstatement);
134         $this->assertEquals($moduleinfo->sendnotifications, $dbmodinstance->sendnotifications);
135         $this->assertEquals($moduleinfo->duedate, $dbmodinstance->duedate);
136         $this->assertEquals($moduleinfo->cutoffdate, $dbmodinstance->cutoffdate);
137         $this->assertEquals($moduleinfo->allowsubmissionsfromdate, $dbmodinstance->allowsubmissionsfromdate);
138         $this->assertEquals($moduleinfo->teamsubmission, $dbmodinstance->teamsubmission);
139         $this->assertEquals($moduleinfo->requireallteammemberssubmit, $dbmodinstance->requireallteammemberssubmit);
140         $this->assertEquals($moduleinfo->teamsubmissiongroupingid, $dbmodinstance->teamsubmissiongroupingid);
141         $this->assertEquals($moduleinfo->blindmarking, $dbmodinstance->blindmarking);
142         $this->assertEquals($moduleinfo->markingworkflow, $dbmodinstance->markingworkflow);
143         $this->assertEquals($moduleinfo->markingallocation, $dbmodinstance->markingallocation);
144         // The goal not being to fully test assign_add_instance() we'll stop here for the assign tests - to avoid too many DB queries.
146         // Advanced grading.
147         $cm = get_coursemodule_from_instance('assign', $dbmodinstance->id);
148         $contextmodule = context_module::instance($cm->id);
149         $advancedgradingmethod = $DB->get_record('grading_areas',
150             array('contextid' => $contextmodule->id,
151                 'activemethod' => $moduleinfo->advancedgradingmethod_submissions));
152         $this->assertEquals($moduleinfo->advancedgradingmethod_submissions, $advancedgradingmethod);
153     }
155     /**
156      * Run some asserts test for a specific module for the function create_module().
157      *
158      * The function has been created (and is called) for $this->test_create_module().
159      * Note that the call to MODULE_create_set_values and MODULE_create_run_asserts are done after the common set values/run asserts.
160      * So if you want, you can overwrite the default values/asserts in the respective functions.
161      * @param string $modulename Name of the module ('forum', 'assign', 'book'...).
162      */
163     private function create_specific_module_test($modulename) {
164         global $DB, $CFG;
166         $this->resetAfterTest(true);
168         $this->setAdminUser();
170         // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
171         require_once($CFG->dirroot.'/mod/'. $modulename .'/lib.php');
173         // Enable avaibility.
174         // If not enabled all conditional fields will be ignored.
175         set_config('enableavailability', 1);
177         // Enable course completion.
178         // If not enabled all completion settings will be ignored.
179         set_config('enablecompletion', COMPLETION_ENABLED);
181         // Enable forum RSS feeds.
182         set_config('enablerssfeeds', 1);
183         set_config('forum_enablerssfeeds', 1);
185         $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED),
186            array('createsections'=>true));
188         $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
190         // Create assign module instance for test.
191         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
192         $params['course'] = $course->id;
193         $instance = $generator->create_instance($params);
194         $assigncm = get_coursemodule_from_instance('assign', $instance->id);
196         // Module test values.
197         $moduleinfo = new stdClass();
199         // Always mandatory generic values to any module.
200         $moduleinfo->modulename = $modulename;
201         $moduleinfo->section = 1; // This is the section number in the course. Not the section id in the database.
202         $moduleinfo->course = $course->id;
203         $moduleinfo->groupingid = $grouping->id;
204         $moduleinfo->groupmembersonly = 0;
205         $moduleinfo->visible = true;
207         // Sometimes optional generic values for some modules.
208         $moduleinfo->name = 'My test module';
209         $moduleinfo->showdescription = 1; // standard boolean
210         require_once($CFG->libdir . '/gradelib.php');
211         $gradecats = grade_get_categories_menu($moduleinfo->course, false);
212         $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
213         $moduleinfo->gradecat = $gradecatid;
214         $moduleinfo->groupmode = VISIBLEGROUPS;
215         $moduleinfo->cmidnumber = 'idnumber_XXX';
217         // Completion common to all module.
218         $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC;
219         $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED;
220         $moduleinfo->completiongradeitemnumber = 1;
221         $moduleinfo->completionexpected = time() + (7 * 24 * 3600);
223         // Conditional activity.
224         $moduleinfo->availability = '{"op":"&","showc":[true,true],"c":[' .
225                 '{"type":"date","d":">=","t":' . time() . '},' .
226                 '{"type":"date","d":"<","t":' . (time() + (7 * 24 * 3600)) . '}' .
227                 ']}';
228         $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself.
229         $moduleinfo->conditiongradegroup = array(array('conditiongradeitemid' => $coursegradeitem->id, 'conditiongrademin' => 10, 'conditiongrademax' => 80));
230         $moduleinfo->conditionfieldgroup = array(array('conditionfield' => 'email', 'conditionfieldoperator' => OP_CONTAINS, 'conditionfieldvalue' => '@'));
231         $moduleinfo->conditioncompletiongroup = array(array('conditionsourcecmid' => $assigncm->id, 'conditionrequiredcompletion' => COMPLETION_COMPLETE)); // "conditionsourcecmid == 0" => none
233         // Grading and Advanced grading.
234         require_once($CFG->dirroot . '/rating/lib.php');
235         $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE;
236         $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number.
237         $moduleinfo->assesstimestart = time();
238         $moduleinfo->assesstimefinish = time() + (7 * 24 * 3600);
240         // RSS.
241         $moduleinfo->rsstype = 2;
242         $moduleinfo->rssarticles = 10;
244         // Optional intro editor (depends of module).
245         $draftid_editor = 0;
246         file_prepare_draft_area($draftid_editor, null, null, null, null);
247         $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
249         // Following is the advanced grading method area called 'submissions' for the 'assign' module.
250         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
251             $moduleinfo->grade = 100;
252         }
254         // Plagiarism form values.
255         // No plagiarism plugin installed by default. Use this space to make your own test.
257         // Values specific to the module.
258         $modulesetvalues = $modulename.'_create_set_values';
259         $this->$modulesetvalues($moduleinfo);
261         // Create the module.
262         $result = create_module($moduleinfo);
264         // Retrieve the module info.
265         $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance));
266         $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance);
267         // We passed the course section number to create_courses but $dbcm contain the section id.
268         // We need to retrieve the db course section number.
269         $section = $DB->get_record('course_sections', array('course' => $dbcm->course, 'id' => $dbcm->section));
270         // Retrieve the grade item.
271         $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course,
272             'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename));
274         // Compare the values common to all module instances.
275         $this->assertEquals($moduleinfo->modulename, $dbcm->modname);
276         $this->assertEquals($moduleinfo->section, $section->section);
277         $this->assertEquals($moduleinfo->course, $dbcm->course);
278         $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
279         $this->assertEquals($moduleinfo->groupmembersonly, $dbcm->groupmembersonly);
280         $this->assertEquals($moduleinfo->visible, $dbcm->visible);
281         $this->assertEquals($moduleinfo->completion, $dbcm->completion);
282         $this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
283         $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber);
284         $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected);
285         $this->assertEquals($moduleinfo->availability, $dbcm->availability);
286         $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription);
287         $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode);
288         $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber);
289         $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid);
291         // Optional grade testing.
292         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
293             $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade);
294         }
296         // Some optional (but quite common) to some module.
297         $this->assertEquals($moduleinfo->name, $dbmodinstance->name);
298         $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro);
299         $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat);
301         // Test specific to the module.
302         $modulerunasserts = $modulename.'_create_run_asserts';
303         $this->$modulerunasserts($moduleinfo, $dbmodinstance);
304         return $moduleinfo;
305     }
307     /**
308      * Test create_module() for multiple modules defined in the $modules array (first declaration of the function).
309      */
310     public function test_create_module() {
311         // Add the module name you want to test here.
312         // Create the match MODULENAME_create_set_values() and MODULENAME_create_run_asserts().
313         $modules = array('forum', 'assign');
314         // Run all tests.
315         foreach ($modules as $modulename) {
316             $this->create_specific_module_test($modulename);
317         }
318     }
320     /**
321      * Test update_module() for multiple modules defined in the $modules array (first declaration of the function).
322      */
323     public function test_update_module() {
324         // Add the module name you want to test here.
325         // Create the match MODULENAME_update_set_values() and MODULENAME_update_run_asserts().
326         $modules = array('forum');
327         // Run all tests.
328         foreach ($modules as $modulename) {
329             $this->update_specific_module_test($modulename);
330         }
331     }
333     /**
334      * Set forum specific test values for calling update_module().
335      *
336      * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
337      */
338     private function forum_update_set_values(&$moduleinfo) {
339         // Completion specific to forum - optional.
340         $moduleinfo->completionposts = 3;
341         $moduleinfo->completiondiscussions = 1;
342         $moduleinfo->completionreplies = 2;
344         // Specific values to the Forum module.
345         $moduleinfo->forcesubscribe = FORUM_INITIALSUBSCRIBE;
346         $moduleinfo->type = 'single';
347         $moduleinfo->trackingtype = FORUM_TRACKING_FORCED;
348         $moduleinfo->maxbytes = 10240;
349         $moduleinfo->maxattachments = 2;
351         // Post threshold for blocking - specific to forum.
352         $moduleinfo->blockperiod = 60*60*24;
353         $moduleinfo->blockafter = 10;
354         $moduleinfo->warnafter = 5;
355     }
357     /**
358      * Execute test asserts on the saved DB data by update_module($forum).
359      *
360      * @param object $moduleinfo - the specific forum values that were used to update a forum.
361      * @param object $dbmodinstance - the DB values of the updated forum.
362      */
363     private function forum_update_run_asserts($moduleinfo, $dbmodinstance) {
364         // Compare values specific to forums.
365         $this->assertEquals($moduleinfo->forcesubscribe, $dbmodinstance->forcesubscribe);
366         $this->assertEquals($moduleinfo->type, $dbmodinstance->type);
367         $this->assertEquals($moduleinfo->assessed, $dbmodinstance->assessed);
368         $this->assertEquals($moduleinfo->completionposts, $dbmodinstance->completionposts);
369         $this->assertEquals($moduleinfo->completiondiscussions, $dbmodinstance->completiondiscussions);
370         $this->assertEquals($moduleinfo->completionreplies, $dbmodinstance->completionreplies);
371         $this->assertEquals($moduleinfo->scale, $dbmodinstance->scale);
372         $this->assertEquals($moduleinfo->assesstimestart, $dbmodinstance->assesstimestart);
373         $this->assertEquals($moduleinfo->assesstimefinish, $dbmodinstance->assesstimefinish);
374         $this->assertEquals($moduleinfo->rsstype, $dbmodinstance->rsstype);
375         $this->assertEquals($moduleinfo->rssarticles, $dbmodinstance->rssarticles);
376         $this->assertEquals($moduleinfo->trackingtype, $dbmodinstance->trackingtype);
377         $this->assertEquals($moduleinfo->maxbytes, $dbmodinstance->maxbytes);
378         $this->assertEquals($moduleinfo->maxattachments, $dbmodinstance->maxattachments);
379         $this->assertEquals($moduleinfo->blockperiod, $dbmodinstance->blockperiod);
380         $this->assertEquals($moduleinfo->blockafter, $dbmodinstance->blockafter);
381         $this->assertEquals($moduleinfo->warnafter, $dbmodinstance->warnafter);
382     }
386     /**
387      * Test a specific type of module.
388      *
389      * @param string $modulename - the module name to test
390      */
391     private function update_specific_module_test($modulename) {
392         global $DB, $CFG;
394         $this->resetAfterTest(true);
396         $this->setAdminUser();
398         // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
399         require_once($CFG->dirroot.'/mod/'. $modulename .'/lib.php');
401         // Enable avaibility.
402         // If not enabled all conditional fields will be ignored.
403         set_config('enableavailability', 1);
405         // Enable course completion.
406         // If not enabled all completion settings will be ignored.
407         set_config('enablecompletion', COMPLETION_ENABLED);
409         // Enable forum RSS feeds.
410         set_config('enablerssfeeds', 1);
411         set_config('forum_enablerssfeeds', 1);
413         $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED),
414            array('createsections'=>true));
416         $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
418         // Create assign module instance for testing gradeitem.
419         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
420         $params['course'] = $course->id;
421         $instance = $generator->create_instance($params);
422         $assigncm = get_coursemodule_from_instance('assign', $instance->id);
424         // Create the test forum to update.
425         $initvalues = new stdClass();
426         $initvalues->introformat = FORMAT_HTML;
427         $initvalues->course = $course->id;
428         $forum = self::getDataGenerator()->create_module('forum', $initvalues);
430         // Retrieve course module.
431         $cm = get_coursemodule_from_instance('forum', $forum->id);
433         // Module test values.
434         $moduleinfo = new stdClass();
436         // Always mandatory generic values to any module.
437         $moduleinfo->coursemodule = $cm->id;
438         $moduleinfo->modulename = $modulename;
439         $moduleinfo->course = $course->id;
440         $moduleinfo->groupingid = $grouping->id;
441         $moduleinfo->groupmembersonly = 0;
442         $moduleinfo->visible = true;
444         // Sometimes optional generic values for some modules.
445         $moduleinfo->name = 'My test module';
446         $moduleinfo->showdescription = 1; // standard boolean
447         require_once($CFG->libdir . '/gradelib.php');
448         $gradecats = grade_get_categories_menu($moduleinfo->course, false);
449         $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
450         $moduleinfo->gradecat = $gradecatid;
451         $moduleinfo->groupmode = VISIBLEGROUPS;
452         $moduleinfo->cmidnumber = 'idnumber_XXX';
454         // Completion common to all module.
455         $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC;
456         $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED;
457         $moduleinfo->completiongradeitemnumber = 1;
458         $moduleinfo->completionexpected = time() + (7 * 24 * 3600);
459         $moduleinfo->completionunlocked = 1;
461         // Conditional activity.
462         $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself.
463         $moduleinfo->availability = '{"op":"&","showc":[true,true],"c":[' .
464                 '{"type":"date","d":">=","t":' . time() . '},' .
465                 '{"type":"date","d":"<","t":' . (time() + (7 * 24 * 3600)) . '}' .
466                 '{"type":"grade","id":' . $coursegradeitem->id . ',"min":10,"max":80},' .
467                 '{"type":"profile","sf":"email","op":"contains","v":"@"},'.
468                 '{"type":"completion","id":'. $assigncm->id . ',"e":' . COMPLETION_COMPLETE . '}' .
469                 ']}';
471         // Grading and Advanced grading.
472         require_once($CFG->dirroot . '/rating/lib.php');
473         $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE;
474         $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number.
475         $moduleinfo->assesstimestart = time();
476         $moduleinfo->assesstimefinish = time() + (7 * 24 * 3600);
478         // RSS.
479         $moduleinfo->rsstype = 2;
480         $moduleinfo->rssarticles = 10;
482         // Optional intro editor (depends of module).
483         $draftid_editor = 0;
484         file_prepare_draft_area($draftid_editor, null, null, null, null);
485         $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
487         // Following is the advanced grading method area called 'submissions' for the 'assign' module.
488         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
489             $moduleinfo->grade = 100;
490         }
491         // Plagiarism form values.
492         // No plagiarism plugin installed by default. Use this space to make your own test.
494         // Values specific to the module.
495         $modulesetvalues = $modulename.'_update_set_values';
496         $this->$modulesetvalues($moduleinfo);
498         // Create the module.
499         $result = update_module($moduleinfo);
501         // Retrieve the module info.
502         $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance));
503         $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance);
504         // Retrieve the grade item.
505         $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course,
506             'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename));
508         // Compare the values common to all module instances.
509         $this->assertEquals($moduleinfo->modulename, $dbcm->modname);
510         $this->assertEquals($moduleinfo->course, $dbcm->course);
511         $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
512         $this->assertEquals($moduleinfo->groupmembersonly, $dbcm->groupmembersonly);
513         $this->assertEquals($moduleinfo->visible, $dbcm->visible);
514         $this->assertEquals($moduleinfo->completion, $dbcm->completion);
515         $this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
516         $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber);
517         $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected);
518         $this->assertEquals($moduleinfo->availability, $dbcm->availability);
519         $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription);
520         $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode);
521         $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber);
522         $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid);
524         // Optional grade testing.
525         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
526             $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade);
527         }
529         // Some optional (but quite common) to some module.
530         $this->assertEquals($moduleinfo->name, $dbmodinstance->name);
531         $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro);
532         $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat);
534         // Test specific to the module.
535         $modulerunasserts = $modulename.'_update_run_asserts';
536         $this->$modulerunasserts($moduleinfo, $dbmodinstance);
537         return $moduleinfo;
538    }
541     public function test_create_course() {
542         global $DB;
543         $this->resetAfterTest(true);
544         $defaultcategory = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
546         $course = new stdClass();
547         $course->fullname = 'Apu loves Unit Təsts';
548         $course->shortname = 'Spread the lŭve';
549         $course->idnumber = '123';
550         $course->summary = 'Awesome!';
551         $course->summaryformat = FORMAT_PLAIN;
552         $course->format = 'topics';
553         $course->newsitems = 0;
554         $course->numsections = 5;
555         $course->category = $defaultcategory;
556         $original = (array) $course;
558         $created = create_course($course);
559         $context = context_course::instance($created->id);
561         // Compare original and created.
562         $this->assertEquals($original, array_intersect_key((array) $created, $original));
564         // Ensure default section is created.
565         $sectioncreated = $DB->record_exists('course_sections', array('course' => $created->id, 'section' => 0));
566         $this->assertTrue($sectioncreated);
568         // Ensure blocks have been associated to the course.
569         $blockcount = $DB->count_records('block_instances', array('parentcontextid' => $context->id));
570         $this->assertGreaterThan(0, $blockcount);
572         // Ensure that the shortname isn't duplicated.
573         try {
574             $created = create_course($course);
575             $this->fail('Exception expected');
576         } catch (moodle_exception $e) {
577             $this->assertSame(get_string('shortnametaken', 'error', $course->shortname), $e->getMessage());
578         }
580         // Ensure that the idnumber isn't duplicated.
581         $course->shortname .= '1';
582         try {
583             $created = create_course($course);
584             $this->fail('Exception expected');
585         } catch (moodle_exception $e) {
586             $this->assertSame(get_string('courseidnumbertaken', 'error', $course->idnumber), $e->getMessage());
587         }
588     }
590     public function test_create_course_with_generator() {
591         global $DB;
592         $this->resetAfterTest(true);
593         $course = $this->getDataGenerator()->create_course();
595         // Ensure default section is created.
596         $sectioncreated = $DB->record_exists('course_sections', array('course' => $course->id, 'section' => 0));
597         $this->assertTrue($sectioncreated);
598     }
600     public function test_create_course_sections() {
601         global $DB;
602         $this->resetAfterTest(true);
604         $course = $this->getDataGenerator()->create_course(
605                 array('shortname' => 'GrowingCourse',
606                     'fullname' => 'Growing Course',
607                     'numsections' => 5),
608                 array('createsections' => true));
610         // Ensure all 6 (0-5) sections were created and course content cache works properly
611         $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
612         $this->assertEquals(range(0, $course->numsections), $sectionscreated);
614         // this will do nothing, section already exists
615         $this->assertFalse(course_create_sections_if_missing($course, $course->numsections));
617         // this will create new section
618         $this->assertTrue(course_create_sections_if_missing($course, $course->numsections + 1));
620         // Ensure all 7 (0-6) sections were created and modinfo/sectioninfo cache works properly
621         $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
622         $this->assertEquals(range(0, $course->numsections + 1), $sectionscreated);
623     }
625     public function test_update_course() {
626         global $DB;
628         $this->resetAfterTest();
630         $defaultcategory = $DB->get_field_select('course_categories', 'MIN(id)', 'parent = 0');
632         $course = new stdClass();
633         $course->fullname = 'Apu loves Unit Təsts';
634         $course->shortname = 'test1';
635         $course->idnumber = '1';
636         $course->summary = 'Awesome!';
637         $course->summaryformat = FORMAT_PLAIN;
638         $course->format = 'topics';
639         $course->newsitems = 0;
640         $course->numsections = 5;
641         $course->category = $defaultcategory;
643         $created = create_course($course);
644         // Ensure the checks only work on idnumber/shortname that are not already ours.
645         update_course($created);
647         $course->shortname = 'test2';
648         $course->idnumber = '2';
650         $created2 = create_course($course);
652         // Test duplicate idnumber.
653         $created2->idnumber = '1';
654         try {
655             update_course($created2);
656             $this->fail('Expected exception when trying to update a course with duplicate idnumber');
657         } catch (moodle_exception $e) {
658             $this->assertEquals(get_string('courseidnumbertaken', 'error', $created2->idnumber), $e->getMessage());
659         }
661         // Test duplicate shortname.
662         $created2->idnumber = '2';
663         $created2->shortname = 'test1';
664         try {
665             update_course($created2);
666             $this->fail('Expected exception when trying to update a course with a duplicate shortname');
667         } catch (moodle_exception $e) {
668             $this->assertEquals(get_string('shortnametaken', 'error', $created2->shortname), $e->getMessage());
669         }
670     }
672     public function test_course_add_cm_to_section() {
673         global $DB;
674         $this->resetAfterTest(true);
676         // Create course with 1 section.
677         $course = $this->getDataGenerator()->create_course(
678                 array('shortname' => 'GrowingCourse',
679                     'fullname' => 'Growing Course',
680                     'numsections' => 1),
681                 array('createsections' => true));
683         // Trash modinfo.
684         rebuild_course_cache($course->id, true);
686         // Create some cms for testing.
687         $cmids = array();
688         for ($i=0; $i<4; $i++) {
689             $cmids[$i] = $DB->insert_record('course_modules', array('course' => $course->id));
690         }
692         // Add it to section that exists.
693         course_add_cm_to_section($course, $cmids[0], 1);
695         // Check it got added to sequence.
696         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 1));
697         $this->assertEquals($cmids[0], $sequence);
699         // Add a second, this time using courseid variant of parameters.
700         $coursecacherev = $DB->get_field('course', 'cacherev', array('id' => $course->id));
701         course_add_cm_to_section($course->id, $cmids[1], 1);
702         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 1));
703         $this->assertEquals($cmids[0] . ',' . $cmids[1], $sequence);
705         // Check that modinfo cache was reset but not rebuilt (important for performance if calling repeatedly).
706         $this->assertGreaterThan($coursecacherev, $DB->get_field('course', 'cacherev', array('id' => $course->id)));
707         $this->assertEmpty(cache::make('core', 'coursemodinfo')->get($course->id));
709         // Add one to section that doesn't exist (this might rebuild modinfo).
710         course_add_cm_to_section($course, $cmids[2], 2);
711         $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id)));
712         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 2));
713         $this->assertEquals($cmids[2], $sequence);
715         // Add using the 'before' option.
716         course_add_cm_to_section($course, $cmids[3], 2, $cmids[2]);
717         $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id)));
718         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 2));
719         $this->assertEquals($cmids[3] . ',' . $cmids[2], $sequence);
720     }
722     public function test_reorder_sections() {
723         global $DB;
724         $this->resetAfterTest(true);
726         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
727         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
728         $oldsections = array();
729         $sections = array();
730         foreach ($DB->get_records('course_sections', array('course'=>$course->id), 'id') as $section) {
731             $oldsections[$section->section] = $section->id;
732             $sections[$section->id] = $section->section;
733         }
734         ksort($oldsections);
736         $neworder = reorder_sections($sections, 2, 4);
737         $neworder = array_keys($neworder);
738         $this->assertEquals($oldsections[0], $neworder[0]);
739         $this->assertEquals($oldsections[1], $neworder[1]);
740         $this->assertEquals($oldsections[2], $neworder[4]);
741         $this->assertEquals($oldsections[3], $neworder[2]);
742         $this->assertEquals($oldsections[4], $neworder[3]);
743         $this->assertEquals($oldsections[5], $neworder[5]);
744         $this->assertEquals($oldsections[6], $neworder[6]);
746         $neworder = reorder_sections($sections, 4, 2);
747         $neworder = array_keys($neworder);
748         $this->assertEquals($oldsections[0], $neworder[0]);
749         $this->assertEquals($oldsections[1], $neworder[1]);
750         $this->assertEquals($oldsections[2], $neworder[3]);
751         $this->assertEquals($oldsections[3], $neworder[4]);
752         $this->assertEquals($oldsections[4], $neworder[2]);
753         $this->assertEquals($oldsections[5], $neworder[5]);
754         $this->assertEquals($oldsections[6], $neworder[6]);
756         $neworder = reorder_sections(1, 2, 4);
757         $this->assertFalse($neworder);
758     }
760     public function test_move_section_down() {
761         global $DB;
762         $this->resetAfterTest(true);
764         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
765         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
766         $oldsections = array();
767         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
768             $oldsections[$section->section] = $section->id;
769         }
770         ksort($oldsections);
772         // Test move section down..
773         move_section_to($course, 2, 4);
774         $sections = array();
775         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
776             $sections[$section->section] = $section->id;
777         }
778         ksort($sections);
780         $this->assertEquals($oldsections[0], $sections[0]);
781         $this->assertEquals($oldsections[1], $sections[1]);
782         $this->assertEquals($oldsections[2], $sections[4]);
783         $this->assertEquals($oldsections[3], $sections[2]);
784         $this->assertEquals($oldsections[4], $sections[3]);
785         $this->assertEquals($oldsections[5], $sections[5]);
786         $this->assertEquals($oldsections[6], $sections[6]);
787     }
789     public function test_move_section_up() {
790         global $DB;
791         $this->resetAfterTest(true);
793         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
794         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
795         $oldsections = array();
796         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
797             $oldsections[$section->section] = $section->id;
798         }
799         ksort($oldsections);
801         // Test move section up..
802         move_section_to($course, 6, 4);
803         $sections = array();
804         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
805             $sections[$section->section] = $section->id;
806         }
807         ksort($sections);
809         $this->assertEquals($oldsections[0], $sections[0]);
810         $this->assertEquals($oldsections[1], $sections[1]);
811         $this->assertEquals($oldsections[2], $sections[2]);
812         $this->assertEquals($oldsections[3], $sections[3]);
813         $this->assertEquals($oldsections[4], $sections[5]);
814         $this->assertEquals($oldsections[5], $sections[6]);
815         $this->assertEquals($oldsections[6], $sections[4]);
816     }
818     public function test_move_section_marker() {
819         global $DB;
820         $this->resetAfterTest(true);
822         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
823         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
825         // Set course marker to the section we are going to move..
826         course_set_marker($course->id, 2);
827         // Verify that the course marker is set correctly.
828         $course = $DB->get_record('course', array('id' => $course->id));
829         $this->assertEquals(2, $course->marker);
831         // Test move the marked section down..
832         move_section_to($course, 2, 4);
834         // Verify that the coruse marker has been moved along with the section..
835         $course = $DB->get_record('course', array('id' => $course->id));
836         $this->assertEquals(4, $course->marker);
838         // Test move the marked section up..
839         move_section_to($course, 4, 3);
841         // Verify that the course marker has been moved along with the section..
842         $course = $DB->get_record('course', array('id' => $course->id));
843         $this->assertEquals(3, $course->marker);
845         // Test moving a non-marked section above the marked section..
846         move_section_to($course, 4, 2);
848         // Verify that the course marker has been moved down to accomodate..
849         $course = $DB->get_record('course', array('id' => $course->id));
850         $this->assertEquals(4, $course->marker);
852         // Test moving a non-marked section below the marked section..
853         move_section_to($course, 3, 6);
855         // Verify that the course marker has been up to accomodate..
856         $course = $DB->get_record('course', array('id' => $course->id));
857         $this->assertEquals(3, $course->marker);
858     }
860     public function test_get_course_display_name_for_list() {
861         global $CFG;
862         $this->resetAfterTest(true);
864         $course = $this->getDataGenerator()->create_course(array('shortname' => 'FROG101', 'fullname' => 'Introduction to pond life'));
866         $CFG->courselistshortnames = 0;
867         $this->assertEquals('Introduction to pond life', get_course_display_name_for_list($course));
869         $CFG->courselistshortnames = 1;
870         $this->assertEquals('FROG101 Introduction to pond life', get_course_display_name_for_list($course));
871     }
873     public function test_move_module_in_course() {
874         global $DB;
876         $this->resetAfterTest(true);
877         // Setup fixture
878         $course = $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections' => true));
879         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
881         $cms = get_fast_modinfo($course)->get_cms();
882         $cm = reset($cms);
884         $newsection = get_fast_modinfo($course)->get_section_info(3);
885         $oldsectionid = $cm->section;
887         // Perform the move
888         moveto_module($cm, $newsection);
890         $cms = get_fast_modinfo($course)->get_cms();
891         $cm = reset($cms);
893         // Check that the cached modinfo contains the correct section info
894         $modinfo = get_fast_modinfo($course);
895         $this->assertTrue(empty($modinfo->sections[0]));
896         $this->assertFalse(empty($modinfo->sections[3]));
898         // Check that the old section's sequence no longer contains this ID
899         $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
900         $oldsequences = explode(',', $newsection->sequence);
901         $this->assertFalse(in_array($cm->id, $oldsequences));
903         // Check that the new section's sequence now contains this ID
904         $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
905         $newsequences = explode(',', $newsection->sequence);
906         $this->assertTrue(in_array($cm->id, $newsequences));
908         // Check that the section number has been changed in the cm
909         $this->assertEquals($newsection->id, $cm->section);
912         // Perform a second move as some issues were only seen on the second move
913         $newsection = get_fast_modinfo($course)->get_section_info(2);
914         $oldsectionid = $cm->section;
915         moveto_module($cm, $newsection);
917         $cms = get_fast_modinfo($course)->get_cms();
918         $cm = reset($cms);
920         // Check that the cached modinfo contains the correct section info
921         $modinfo = get_fast_modinfo($course);
922         $this->assertTrue(empty($modinfo->sections[0]));
923         $this->assertFalse(empty($modinfo->sections[2]));
925         // Check that the old section's sequence no longer contains this ID
926         $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
927         $oldsequences = explode(',', $newsection->sequence);
928         $this->assertFalse(in_array($cm->id, $oldsequences));
930         // Check that the new section's sequence now contains this ID
931         $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
932         $newsequences = explode(',', $newsection->sequence);
933         $this->assertTrue(in_array($cm->id, $newsequences));
934     }
936     public function test_module_visibility() {
937         $this->setAdminUser();
938         $this->resetAfterTest(true);
940         // Create course and modules.
941         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
942         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
943         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(), 'course' => $course->id));
944         $modules = compact('forum', 'assign');
946         // Hiding the modules.
947         foreach ($modules as $mod) {
948             set_coursemodule_visible($mod->cmid, 0);
949             $this->check_module_visibility($mod, 0, 0);
950         }
952         // Showing the modules.
953         foreach ($modules as $mod) {
954             set_coursemodule_visible($mod->cmid, 1);
955             $this->check_module_visibility($mod, 1, 1);
956         }
957     }
959     public function test_section_visibility_events() {
960         $this->setAdminUser();
961         $this->resetAfterTest(true);
963         $course = $this->getDataGenerator()->create_course(array('numsections' => 1), array('createsections' => true));
964         $sectionnumber = 1;
965         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
966             array('section' => $sectionnumber));
967         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
968             'course' => $course->id), array('section' => $sectionnumber));
969         $sink = $this->redirectEvents();
970         set_section_visible($course->id, $sectionnumber, 0);
971         $events = $sink->get_events();
973         // Extract the number of events related to what we are testing, other events
974         // such as course_section_updated could have been triggered.
975         $count = 0;
976         foreach ($events as $event) {
977             if ($event instanceof \core\event\course_module_updated) {
978                 $count++;
979             }
980         }
981         $this->assertSame(2, $count);
982         $sink->close();
983     }
985     public function test_section_visibility() {
986         $this->setAdminUser();
987         $this->resetAfterTest(true);
989         // Create course.
990         $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
992         $sink = $this->redirectEvents();
994         // Testing an empty section.
995         $sectionnumber = 1;
996         set_section_visible($course->id, $sectionnumber, 0);
997         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
998         $this->assertEquals($section_info->visible, 0);
999         set_section_visible($course->id, $sectionnumber, 1);
1000         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1001         $this->assertEquals($section_info->visible, 1);
1003         // Checking that an event was fired.
1004         $events = $sink->get_events();
1005         $this->assertInstanceOf('\core\event\course_section_updated', $events[0]);
1006         $sink->close();
1008         // Testing a section with visible modules.
1009         $sectionnumber = 2;
1010         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1011                 array('section' => $sectionnumber));
1012         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1013                 'course' => $course->id), array('section' => $sectionnumber));
1014         $modules = compact('forum', 'assign');
1015         set_section_visible($course->id, $sectionnumber, 0);
1016         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1017         $this->assertEquals($section_info->visible, 0);
1018         foreach ($modules as $mod) {
1019             $this->check_module_visibility($mod, 0, 1);
1020         }
1021         set_section_visible($course->id, $sectionnumber, 1);
1022         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1023         $this->assertEquals($section_info->visible, 1);
1024         foreach ($modules as $mod) {
1025             $this->check_module_visibility($mod, 1, 1);
1026         }
1028         // Testing a section with hidden modules, which should stay hidden.
1029         $sectionnumber = 3;
1030         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1031                 array('section' => $sectionnumber));
1032         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1033                 'course' => $course->id), array('section' => $sectionnumber));
1034         $modules = compact('forum', 'assign');
1035         foreach ($modules as $mod) {
1036             set_coursemodule_visible($mod->cmid, 0);
1037             $this->check_module_visibility($mod, 0, 0);
1038         }
1039         set_section_visible($course->id, $sectionnumber, 0);
1040         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1041         $this->assertEquals($section_info->visible, 0);
1042         foreach ($modules as $mod) {
1043             $this->check_module_visibility($mod, 0, 0);
1044         }
1045         set_section_visible($course->id, $sectionnumber, 1);
1046         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1047         $this->assertEquals($section_info->visible, 1);
1048         foreach ($modules as $mod) {
1049             $this->check_module_visibility($mod, 0, 0);
1050         }
1051     }
1053     /**
1054      * Helper function to assert that a module has correctly been made visible, or hidden.
1055      *
1056      * @param stdClass $mod module information
1057      * @param int $visibility the current state of the module
1058      * @param int $visibleold the current state of the visibleold property
1059      * @return void
1060      */
1061     public function check_module_visibility($mod, $visibility, $visibleold) {
1062         global $DB;
1063         $cm = get_fast_modinfo($mod->course)->get_cm($mod->cmid);
1064         $this->assertEquals($visibility, $cm->visible);
1065         $this->assertEquals($visibleold, $cm->visibleold);
1067         // Check the module grade items.
1068         $grade_items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $cm->modname,
1069                 'iteminstance' => $cm->instance, 'courseid' => $cm->course));
1070         if ($grade_items) {
1071             foreach ($grade_items as $grade_item) {
1072                 if ($visibility) {
1073                     $this->assertFalse($grade_item->is_hidden(), "$cm->modname grade_item not visible");
1074                 } else {
1075                     $this->assertTrue($grade_item->is_hidden(), "$cm->modname grade_item not hidden");
1076                 }
1077             }
1078         }
1080         // Check the events visibility.
1081         if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $cm->modname))) {
1082             foreach ($events as $event) {
1083                 $calevent = new calendar_event($event);
1084                 $this->assertEquals($visibility, $calevent->visible, "$cm->modname calendar_event visibility");
1085             }
1086         }
1087     }
1089     public function test_course_page_type_list() {
1090         global $DB;
1091         $this->resetAfterTest(true);
1093         // Create a category.
1094         $category = new stdClass();
1095         $category->name = 'Test Category';
1097         $testcategory = $this->getDataGenerator()->create_category($category);
1099         // Create a course.
1100         $course = new stdClass();
1101         $course->fullname = 'Apu loves Unit Təsts';
1102         $course->shortname = 'Spread the lŭve';
1103         $course->idnumber = '123';
1104         $course->summary = 'Awesome!';
1105         $course->summaryformat = FORMAT_PLAIN;
1106         $course->format = 'topics';
1107         $course->newsitems = 0;
1108         $course->numsections = 5;
1109         $course->category = $testcategory->id;
1111         $testcourse = $this->getDataGenerator()->create_course($course);
1113         // Create contexts.
1114         $coursecontext = context_course::instance($testcourse->id);
1115         $parentcontext = $coursecontext->get_parent_context(); // Not actually used.
1116         $pagetype = 'page-course-x'; // Not used either.
1117         $pagetypelist = course_page_type_list($pagetype, $parentcontext, $coursecontext);
1119         // Page type lists for normal courses.
1120         $testpagetypelist1 = array();
1121         $testpagetypelist1['*'] = 'Any page';
1122         $testpagetypelist1['course-*'] = 'Any course page';
1123         $testpagetypelist1['course-view-*'] = 'Any type of course main page';
1125         $this->assertEquals($testpagetypelist1, $pagetypelist);
1127         // Get the context for the front page course.
1128         $sitecoursecontext = context_course::instance(SITEID);
1129         $pagetypelist = course_page_type_list($pagetype, $parentcontext, $sitecoursecontext);
1131         // Page type list for the front page course.
1132         $testpagetypelist2 = array('*' => 'Any page');
1133         $this->assertEquals($testpagetypelist2, $pagetypelist);
1135         // Make sure that providing no current context to the function doesn't result in an error.
1136         // Calls made from generate_page_type_patterns() may provide null values.
1137         $pagetypelist = course_page_type_list($pagetype, null, null);
1138         $this->assertEquals($pagetypelist, $testpagetypelist1);
1139     }
1141     public function test_compare_activities_by_time_desc() {
1143         // Let's create some test data.
1144         $activitiesivities = array();
1145         $x = new stdClass();
1146         $x->timestamp = null;
1147         $activities[] = $x;
1149         $x = new stdClass();
1150         $x->timestamp = 1;
1151         $activities[] = $x;
1153         $x = new stdClass();
1154         $x->timestamp = 3;
1155         $activities[] = $x;
1157         $x = new stdClass();
1158         $x->timestamp = 0;
1159         $activities[] = $x;
1161         $x = new stdClass();
1162         $x->timestamp = 5;
1163         $activities[] = $x;
1165         $x = new stdClass();
1166         $activities[] = $x;
1168         $x = new stdClass();
1169         $x->timestamp = 5;
1170         $activities[] = $x;
1172         // Do the sorting.
1173         usort($activities, 'compare_activities_by_time_desc');
1175         // Let's check the result.
1176         $last = 10;
1177         foreach($activities as $activity) {
1178             if (empty($activity->timestamp)) {
1179                 $activity->timestamp = 0;
1180             }
1181             $this->assertLessThanOrEqual($last, $activity->timestamp);
1182         }
1183     }
1185     public function test_compare_activities_by_time_asc() {
1187         // Let's create some test data.
1188         $activities = array();
1189         $x = new stdClass();
1190         $x->timestamp = null;
1191         $activities[] = $x;
1193         $x = new stdClass();
1194         $x->timestamp = 1;
1195         $activities[] = $x;
1197         $x = new stdClass();
1198         $x->timestamp = 3;
1199         $activities[] = $x;
1201         $x = new stdClass();
1202         $x->timestamp = 0;
1203         $activities[] = $x;
1205         $x = new stdClass();
1206         $x->timestamp = 5;
1207         $activities[] = $x;
1209         $x = new stdClass();
1210         $activities[] = $x;
1212         $x = new stdClass();
1213         $x->timestamp = 5;
1214         $activities[] = $x;
1216         // Do the sorting.
1217         usort($activities, 'compare_activities_by_time_asc');
1219         // Let's check the result.
1220         $last = 0;
1221         foreach($activities as $activity) {
1222             if (empty($activity->timestamp)) {
1223                 $activity->timestamp = 0;
1224             }
1225             $this->assertGreaterThanOrEqual($last, $activity->timestamp);
1226         }
1227     }
1229     /**
1230      * Tests moving a module between hidden/visible sections and
1231      * verifies that the course/module visiblity seettings are
1232      * retained.
1233      */
1234     public function test_moveto_module_between_hidden_sections() {
1235         global $DB;
1237         $this->resetAfterTest(true);
1239         $course = $this->getDataGenerator()->create_course(array('numsections' => 4), array('createsections' => true));
1240         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1241         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
1242         $quiz= $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
1244         // Set the page as hidden
1245         set_coursemodule_visible($page->cmid, 0);
1247         // Set sections 3 as hidden.
1248         set_section_visible($course->id, 3, 0);
1250         $modinfo = get_fast_modinfo($course);
1252         $hiddensection = $modinfo->get_section_info(3);
1253         // New section is definitely not visible:
1254         $this->assertEquals($hiddensection->visible, 0);
1256         $forumcm = $modinfo->cms[$forum->cmid];
1257         $pagecm = $modinfo->cms[$page->cmid];
1259         // Move the forum and the page to a hidden section, make sure moveto_module returns 0 as new visibility state.
1260         $this->assertEquals(0, moveto_module($forumcm, $hiddensection));
1261         $this->assertEquals(0, moveto_module($pagecm, $hiddensection));
1263         $modinfo = get_fast_modinfo($course);
1265         // Verify that forum and page have been moved to the hidden section and quiz has not.
1266         $this->assertContains($forum->cmid, $modinfo->sections[3]);
1267         $this->assertContains($page->cmid, $modinfo->sections[3]);
1268         $this->assertNotContains($quiz->cmid, $modinfo->sections[3]);
1270         // Verify that forum has been made invisible.
1271         $forumcm = $modinfo->cms[$forum->cmid];
1272         $this->assertEquals($forumcm->visible, 0);
1273         // Verify that old state has been retained.
1274         $this->assertEquals($forumcm->visibleold, 1);
1276         // Verify that page has stayed invisible.
1277         $pagecm = $modinfo->cms[$page->cmid];
1278         $this->assertEquals($pagecm->visible, 0);
1279         // Verify that old state has been retained.
1280         $this->assertEquals($pagecm->visibleold, 0);
1282         // Verify that quiz has been unaffected.
1283         $quizcm = $modinfo->cms[$quiz->cmid];
1284         $this->assertEquals($quizcm->visible, 1);
1286         // Move forum and page back to visible section.
1287         // Make sure the visibility is restored to the original value (visible for forum and hidden for page).
1288         $visiblesection = $modinfo->get_section_info(2);
1289         $this->assertEquals(1, moveto_module($forumcm, $visiblesection));
1290         $this->assertEquals(0, moveto_module($pagecm, $visiblesection));
1292         $modinfo = get_fast_modinfo($course);
1294         // Double check that forum has been made visible.
1295         $forumcm = $modinfo->cms[$forum->cmid];
1296         $this->assertEquals($forumcm->visible, 1);
1298         // Double check that page has stayed invisible.
1299         $pagecm = $modinfo->cms[$page->cmid];
1300         $this->assertEquals($pagecm->visible, 0);
1302         // Move the page in the same section (this is what mod duplicate does).
1303         // Visibility of page remains 0.
1304         $this->assertEquals(0, moveto_module($pagecm, $visiblesection, $forumcm));
1306         // Double check that the the page is still hidden.
1307         $modinfo = get_fast_modinfo($course);
1308         $pagecm = $modinfo->cms[$page->cmid];
1309         $this->assertEquals($pagecm->visible, 0);
1310     }
1312     /**
1313      * Tests moving a module around in the same section. moveto_module()
1314      * is called this way in modduplicate.
1315      */
1316     public function test_moveto_module_in_same_section() {
1317         global $DB;
1319         $this->resetAfterTest(true);
1321         $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
1322         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
1323         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1325         // Simulate inconsistent visible/visibleold values (MDL-38713).
1326         $cm = $DB->get_record('course_modules', array('id' => $page->cmid), '*', MUST_EXIST);
1327         $cm->visible = 0;
1328         $cm->visibleold = 1;
1329         $DB->update_record('course_modules', $cm);
1331         $modinfo = get_fast_modinfo($course);
1332         $forumcm = $modinfo->cms[$forum->cmid];
1333         $pagecm = $modinfo->cms[$page->cmid];
1335         // Verify that page is hidden.
1336         $this->assertEquals($pagecm->visible, 0);
1338         // Verify section 0 is where all mods added.
1339         $section = $modinfo->get_section_info(0);
1340         $this->assertEquals($section->id, $forumcm->section);
1341         $this->assertEquals($section->id, $pagecm->section);
1344         // Move the page inside the hidden section. Make sure it is hidden.
1345         $this->assertEquals(0, moveto_module($pagecm, $section, $forumcm));
1347         // Double check that the the page is still hidden.
1348         $modinfo = get_fast_modinfo($course);
1349         $pagecm = $modinfo->cms[$page->cmid];
1350         $this->assertEquals($pagecm->visible, 0);
1351     }
1353     public function test_course_delete_module() {
1354         global $DB;
1355         $this->resetAfterTest(true);
1356         $this->setAdminUser();
1358         // Create course and modules.
1359         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
1361         // Generate an assignment with due date (will generate a course event).
1362         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(), 'course' => $course->id));
1364         // Get the module context.
1365         $modcontext = context_module::instance($assign->cmid);
1367         // Verify context exists.
1368         $this->assertInstanceOf('context_module', $modcontext);
1370         // Add some tags to this assignment.
1371         tag_set('assign', $assign->id, array('Tag 1', 'Tag 2', 'Tag 3'), 'mod_assign', $modcontext->id);
1373         // Confirm the tag instances were added.
1374         $this->assertEquals(3, $DB->count_records('tag_instance', array('component' => 'mod_assign', 'contextid' =>
1375             $modcontext->id)));
1377         // Verify event assignment event has been generated.
1378         $eventcount = $DB->count_records('event', array('instance' => $assign->id, 'modulename' => 'assign'));
1379         $this->assertEquals(1, $eventcount);
1381         // Run delete..
1382         course_delete_module($assign->cmid);
1384         // Verify the context has been removed.
1385         $this->assertFalse(context_module::instance($assign->cmid, IGNORE_MISSING));
1387         // Verify the course_module record has been deleted.
1388         $cmcount = $DB->count_records('course_modules', array('id' => $assign->cmid));
1389         $this->assertEmpty($cmcount);
1391         // Verify event assignment events have been removed.
1392         $eventcount = $DB->count_records('event', array('instance' => $assign->id, 'modulename' => 'assign'));
1393         $this->assertEmpty($eventcount);
1395         // Verify the tag instances were deleted.
1396         $this->assertEquals(0, $DB->count_records('tag_instance', array('component' => 'mod_assign', 'contextid' =>
1397             $modcontext->id)));
1398     }
1400     /**
1401      * Test that triggering a course_created event works as expected.
1402      */
1403     public function test_course_created_event() {
1404         global $DB;
1406         $this->resetAfterTest();
1408         // Catch the events.
1409         $sink = $this->redirectEvents();
1411         // Create the course with an id number which is used later when generating a course via the imsenterprise plugin.
1412         $data = new stdClass();
1413         $data->idnumber = 'idnumber';
1414         $course = $this->getDataGenerator()->create_course($data);
1415         // Get course from DB for comparison.
1416         $course = $DB->get_record('course', array('id' => $course->id));
1418         // Capture the event.
1419         $events = $sink->get_events();
1420         $sink->close();
1422         // Validate the event.
1423         $event = $events[0];
1424         $this->assertInstanceOf('\core\event\course_created', $event);
1425         $this->assertEquals('course', $event->objecttable);
1426         $this->assertEquals($course->id, $event->objectid);
1427         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1428         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1429         $this->assertEquals('course_created', $event->get_legacy_eventname());
1430         $this->assertEventLegacyData($course, $event);
1431         $expectedlog = array(SITEID, 'course', 'new', 'view.php?id=' . $course->id, $course->fullname . ' (ID ' . $course->id . ')');
1432         $this->assertEventLegacyLogData($expectedlog, $event);
1434         // Now we want to trigger creating a course via the imsenterprise.
1435         // Delete the course we created earlier, as we want the imsenterprise plugin to create this.
1436         // We do not want print out any of the text this function generates while doing this, which is why
1437         // we are using ob_start() and ob_end_clean().
1438         ob_start();
1439         delete_course($course);
1440         ob_end_clean();
1442         // Create the XML file we want to use.
1443         $imstestcase = new enrol_imsenterprise_testcase();
1444         $imstestcase->imsplugin = enrol_get_plugin('imsenterprise');
1445         $imstestcase->set_test_config();
1446         $imstestcase->set_xml_file(false, array($course));
1448         // Capture the event.
1449         $sink = $this->redirectEvents();
1450         $imstestcase->imsplugin->cron();
1451         $events = $sink->get_events();
1452         $sink->close();
1453         $event = $events[0];
1455         // Validate the event triggered is \core\event\course_created. There is no need to validate the other values
1456         // as they have already been validated in the previous steps. Here we only want to make sure that when the
1457         // imsenterprise plugin creates a course an event is triggered.
1458         $this->assertInstanceOf('\core\event\course_created', $event);
1459         $this->assertEventContextNotUsed($event);
1460     }
1462     /**
1463      * Test that triggering a course_updated event works as expected.
1464      */
1465     public function test_course_updated_event() {
1466         global $DB;
1468         $this->resetAfterTest();
1470         // Create a course.
1471         $course = $this->getDataGenerator()->create_course();
1473         // Create a category we are going to move this course to.
1474         $category = $this->getDataGenerator()->create_category();
1476         // Create a hidden category we are going to move this course to.
1477         $categoryhidden = $this->getDataGenerator()->create_category(array('visible' => 0));
1479         // Update course and catch course_updated event.
1480         $sink = $this->redirectEvents();
1481         update_course($course);
1482         $events = $sink->get_events();
1483         $sink->close();
1485         // Get updated course information from the DB.
1486         $updatedcourse = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1487         // Validate event.
1488         $event = array_shift($events);
1489         $this->assertInstanceOf('\core\event\course_updated', $event);
1490         $this->assertEquals('course', $event->objecttable);
1491         $this->assertEquals($updatedcourse->id, $event->objectid);
1492         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1493         $url = new moodle_url('/course/edit.php', array('id' => $event->objectid));
1494         $this->assertEquals($url, $event->get_url());
1495         $this->assertEquals($updatedcourse, $event->get_record_snapshot('course', $event->objectid));
1496         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1497         $this->assertEventLegacyData($updatedcourse, $event);
1498         $expectedlog = array($updatedcourse->id, 'course', 'update', 'edit.php?id=' . $course->id, $course->id);
1499         $this->assertEventLegacyLogData($expectedlog, $event);
1501         // Move course and catch course_updated event.
1502         $sink = $this->redirectEvents();
1503         move_courses(array($course->id), $category->id);
1504         $events = $sink->get_events();
1505         $sink->close();
1507         // Return the moved course information from the DB.
1508         $movedcourse = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1509         // Validate event.
1510         $event = array_shift($events);
1511         $this->assertInstanceOf('\core\event\course_updated', $event);
1512         $this->assertEquals('course', $event->objecttable);
1513         $this->assertEquals($movedcourse->id, $event->objectid);
1514         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1515         $this->assertEquals($movedcourse, $event->get_record_snapshot('course', $movedcourse->id));
1516         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1517         $this->assertEventLegacyData($movedcourse, $event);
1518         $expectedlog = array($movedcourse->id, 'course', 'move', 'edit.php?id=' . $movedcourse->id, $movedcourse->id);
1519         $this->assertEventLegacyLogData($expectedlog, $event);
1521         // Move course to hidden category and catch course_updated event.
1522         $sink = $this->redirectEvents();
1523         move_courses(array($course->id), $categoryhidden->id);
1524         $events = $sink->get_events();
1525         $sink->close();
1527         // Return the moved course information from the DB.
1528         $movedcoursehidden = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1529         // Validate event.
1530         $event = array_shift($events);
1531         $this->assertInstanceOf('\core\event\course_updated', $event);
1532         $this->assertEquals('course', $event->objecttable);
1533         $this->assertEquals($movedcoursehidden->id, $event->objectid);
1534         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1535         $this->assertEquals($movedcoursehidden, $event->get_record_snapshot('course', $movedcoursehidden->id));
1536         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1537         $this->assertEventLegacyData($movedcoursehidden, $event);
1538         $expectedlog = array($movedcoursehidden->id, 'course', 'move', 'edit.php?id=' . $movedcoursehidden->id, $movedcoursehidden->id);
1539         $this->assertEventLegacyLogData($expectedlog, $event);
1540         $this->assertEventContextNotUsed($event);
1541     }
1543     /**
1544      * Test that triggering a course_deleted event works as expected.
1545      */
1546     public function test_course_deleted_event() {
1547         $this->resetAfterTest();
1549         // Create the course.
1550         $course = $this->getDataGenerator()->create_course();
1552         // Save the course context before we delete the course.
1553         $coursecontext = context_course::instance($course->id);
1555         // Catch the update event.
1556         $sink = $this->redirectEvents();
1558         // Call delete_course() which will trigger the course_deleted event and the course_content_deleted
1559         // event. This function prints out data to the screen, which we do not want during a PHPUnit test,
1560         // so use ob_start and ob_end_clean to prevent this.
1561         ob_start();
1562         delete_course($course);
1563         ob_end_clean();
1565         // Capture the event.
1566         $events = $sink->get_events();
1567         $sink->close();
1569         // Validate the event.
1570         $event = $events[1];
1571         $this->assertInstanceOf('\core\event\course_deleted', $event);
1572         $this->assertEquals('course', $event->objecttable);
1573         $this->assertEquals($course->id, $event->objectid);
1574         $this->assertEquals($coursecontext->id, $event->contextid);
1575         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1576         $this->assertEquals('course_deleted', $event->get_legacy_eventname());
1577         $eventdata = $event->get_data();
1578         $this->assertSame($course->idnumber, $eventdata['other']['idnumber']);
1579         $this->assertSame($course->fullname, $eventdata['other']['fullname']);
1580         $this->assertSame($course->shortname, $eventdata['other']['shortname']);
1582         // The legacy data also passed the context in the course object and substitutes timemodified with the current date.
1583         $expectedlegacy = clone($course);
1584         $expectedlegacy->context = $coursecontext;
1585         $expectedlegacy->timemodified = $event->timecreated;
1586         $this->assertEventLegacyData($expectedlegacy, $event);
1588         // Validate legacy log data.
1589         $expectedlog = array(SITEID, 'course', 'delete', 'view.php?id=' . $course->id, $course->fullname . '(ID ' . $course->id . ')');
1590         $this->assertEventLegacyLogData($expectedlog, $event);
1591         $this->assertEventContextNotUsed($event);
1592     }
1594     /**
1595      * Test that triggering a course_content_deleted event works as expected.
1596      */
1597     public function test_course_content_deleted_event() {
1598         global $DB;
1600         $this->resetAfterTest();
1602         // Create the course.
1603         $course = $this->getDataGenerator()->create_course();
1605         // Get the course from the DB. The data generator adds some extra properties, such as
1606         // numsections, to the course object which will fail the assertions later on.
1607         $course = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1609         // Save the course context before we delete the course.
1610         $coursecontext = context_course::instance($course->id);
1612         // Catch the update event.
1613         $sink = $this->redirectEvents();
1615         remove_course_contents($course->id, false);
1617         // Capture the event.
1618         $events = $sink->get_events();
1619         $sink->close();
1621         // Validate the event.
1622         $event = $events[0];
1623         $this->assertInstanceOf('\core\event\course_content_deleted', $event);
1624         $this->assertEquals('course', $event->objecttable);
1625         $this->assertEquals($course->id, $event->objectid);
1626         $this->assertEquals($coursecontext->id, $event->contextid);
1627         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1628         $this->assertEquals('course_content_removed', $event->get_legacy_eventname());
1629         // The legacy data also passed the context and options in the course object.
1630         $course->context = $coursecontext;
1631         $course->options = array();
1632         $this->assertEventLegacyData($course, $event);
1633         $this->assertEventContextNotUsed($event);
1634     }
1636     /**
1637      * Test that triggering a course_category_deleted event works as expected.
1638      */
1639     public function test_course_category_deleted_event() {
1640         $this->resetAfterTest();
1642         // Create a category.
1643         $category = $this->getDataGenerator()->create_category();
1645         // Save the context before it is deleted.
1646         $categorycontext = context_coursecat::instance($category->id);
1648         // Catch the update event.
1649         $sink = $this->redirectEvents();
1651         // Delete the category.
1652         $category->delete_full();
1654         // Capture the event.
1655         $events = $sink->get_events();
1656         $sink->close();
1658         // Validate the event.
1659         $event = $events[0];
1660         $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1661         $this->assertEquals('course_categories', $event->objecttable);
1662         $this->assertEquals($category->id, $event->objectid);
1663         $this->assertEquals($categorycontext->id, $event->contextid);
1664         $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1665         $this->assertEquals(null, $event->get_url());
1666         $this->assertEventLegacyData($category, $event);
1667         $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category->name . '(ID ' . $category->id . ')');
1668         $this->assertEventLegacyLogData($expectedlog, $event);
1670         // Create two categories.
1671         $category = $this->getDataGenerator()->create_category();
1672         $category2 = $this->getDataGenerator()->create_category();
1674         // Save the context before it is moved and then deleted.
1675         $category2context = context_coursecat::instance($category2->id);
1677         // Catch the update event.
1678         $sink = $this->redirectEvents();
1680         // Move the category.
1681         $category2->delete_move($category->id);
1683         // Capture the event.
1684         $events = $sink->get_events();
1685         $sink->close();
1687         // Validate the event.
1688         $event = $events[0];
1689         $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1690         $this->assertEquals('course_categories', $event->objecttable);
1691         $this->assertEquals($category2->id, $event->objectid);
1692         $this->assertEquals($category2context->id, $event->contextid);
1693         $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1694         $this->assertEventLegacyData($category2, $event);
1695         $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category2->name . '(ID ' . $category2->id . ')');
1696         $this->assertEventLegacyLogData($expectedlog, $event);
1697         $this->assertEventContextNotUsed($event);
1698     }
1700     /**
1701      * Test that triggering a course_restored event works as expected.
1702      */
1703     public function test_course_restored_event() {
1704         global $CFG;
1706         // Get the necessary files to perform backup and restore.
1707         require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
1708         require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
1710         $this->resetAfterTest();
1712         // Set to admin user.
1713         $this->setAdminUser();
1715         // The user id is going to be 2 since we are the admin user.
1716         $userid = 2;
1718         // Create a course.
1719         $course = $this->getDataGenerator()->create_course();
1721         // Create backup file and save it to the backup location.
1722         $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
1723             backup::INTERACTIVE_NO, backup::MODE_GENERAL, $userid);
1724         $bc->execute_plan();
1725         $results = $bc->get_results();
1726         $file = $results['backup_destination'];
1727         $fp = get_file_packer();
1728         $filepath = $CFG->dataroot . '/temp/backup/test-restore-course-event';
1729         $file->extract_to_pathname($fp, $filepath);
1730         $bc->destroy();
1731         unset($bc);
1733         // Now we want to catch the restore course event.
1734         $sink = $this->redirectEvents();
1736         // Now restore the course to trigger the event.
1737         $rc = new restore_controller('test-restore-course-event', $course->id, backup::INTERACTIVE_NO,
1738             backup::MODE_GENERAL, $userid, backup::TARGET_NEW_COURSE);
1739         $rc->execute_precheck();
1740         $rc->execute_plan();
1742         // Capture the event.
1743         $events = $sink->get_events();
1744         $sink->close();
1746         // Validate the event.
1747         $event = $events[0];
1748         $this->assertInstanceOf('\core\event\course_restored', $event);
1749         $this->assertEquals('course', $event->objecttable);
1750         $this->assertEquals($rc->get_courseid(), $event->objectid);
1751         $this->assertEquals(context_course::instance($rc->get_courseid())->id, $event->contextid);
1752         $this->assertEquals('course_restored', $event->get_legacy_eventname());
1753         $legacydata = (object) array(
1754             'courseid' => $rc->get_courseid(),
1755             'userid' => $rc->get_userid(),
1756             'type' => $rc->get_type(),
1757             'target' => $rc->get_target(),
1758             'mode' => $rc->get_mode(),
1759             'operation' => $rc->get_operation(),
1760             'samesite' => $rc->is_samesite()
1761         );
1762         $url = new moodle_url('/course/view.php', array('id' => $event->objectid));
1763         $this->assertEquals($url, $event->get_url());
1764         $this->assertEventLegacyData($legacydata, $event);
1765         $this->assertEventContextNotUsed($event);
1767         // Destroy the resource controller since we are done using it.
1768         $rc->destroy();
1769         unset($rc);
1770     }
1772     /**
1773      * Test that triggering a course_section_updated event works as expected.
1774      */
1775     public function test_course_section_updated_event() {
1776         global $DB;
1778         $this->resetAfterTest();
1780         // Create the course with sections.
1781         $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true));
1782         $sections = $DB->get_records('course_sections', array('course' => $course->id));
1784         $coursecontext = context_course::instance($course->id);
1786         $section = array_pop($sections);
1787         $section->name = 'Test section';
1788         $section->summary = 'Test section summary';
1789         $DB->update_record('course_sections', $section);
1791         // Trigger an event for course section update.
1792         $event = \core\event\course_section_updated::create(
1793                 array(
1794                     'objectid' => $section->id,
1795                     'courseid' => $course->id,
1796                     'context' => context_course::instance($course->id),
1797                     'other' => array(
1798                         'sectionnum' => $section->section
1799                     )
1800                 )
1801             );
1802         $event->add_record_snapshot('course_sections', $section);
1803         // Trigger and catch event.
1804         $sink = $this->redirectEvents();
1805         $event->trigger();
1806         $events = $sink->get_events();
1807         $sink->close();
1809         // Validate the event.
1810         $event = $events[0];
1811         $this->assertInstanceOf('\core\event\course_section_updated', $event);
1812         $this->assertEquals('course_sections', $event->objecttable);
1813         $this->assertEquals($section->id, $event->objectid);
1814         $this->assertEquals($course->id, $event->courseid);
1815         $this->assertEquals($coursecontext->id, $event->contextid);
1816         $this->assertEquals($section->section, $event->other['sectionnum']);
1817         $expecteddesc = "The user with id '{$event->userid}' updated section number '{$event->other['sectionnum']}' for the course with id '{$event->courseid}'";
1818         $this->assertEquals($expecteddesc, $event->get_description());
1819         $url = new moodle_url('/course/editsection.php', array('id' => $event->objectid));
1820         $this->assertEquals($url, $event->get_url());
1821         $this->assertEquals($section, $event->get_record_snapshot('course_sections', $event->objectid));
1822         $id = $section->id;
1823         $sectionnum = $section->section;
1824         $expectedlegacydata = array($course->id, "course", "editsection", 'editsection.php?id=' . $id, $sectionnum);
1825         $this->assertEventLegacyLogData($expectedlegacydata, $event);
1826         $this->assertEventContextNotUsed($event);
1827     }
1829     public function test_course_integrity_check() {
1830         global $DB;
1832         $this->resetAfterTest(true);
1833         $course = $this->getDataGenerator()->create_course(array('numsections' => 1),
1834            array('createsections'=>true));
1836         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1837                 array('section' => 0));
1838         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id),
1839                 array('section' => 0));
1840         $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id),
1841                 array('section' => 0));
1842         $correctseq = join(',', array($forum->cmid, $page->cmid, $quiz->cmid));
1844         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1845         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1846         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1847         $this->assertEquals($correctseq, $section0->sequence);
1848         $this->assertEmpty($section1->sequence);
1849         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1850         $this->assertEquals($section0->id, $cms[$page->cmid]->section);
1851         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1852         $this->assertEmpty(course_integrity_check($course->id));
1854         // Now let's make manual change in DB and let course_integrity_check() fix it:
1856         // 1. Module appears twice in one section.
1857         $DB->update_record('course_sections', array('id' => $section0->id, 'sequence' => $section0->sequence. ','. $page->cmid));
1858         $this->assertEquals(
1859                 array('Failed integrity check for course ['. $course->id.
1860                 ']. Sequence for course section ['. $section0->id. '] is "'.
1861                 $section0->sequence. ','. $page->cmid. '", must be "'.
1862                 $section0->sequence. '"'),
1863                 course_integrity_check($course->id));
1864         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1865         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1866         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1867         $this->assertEquals($correctseq, $section0->sequence);
1868         $this->assertEmpty($section1->sequence);
1869         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1870         $this->assertEquals($section0->id, $cms[$page->cmid]->section);
1871         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1873         // 2. Module appears in two sections (last section wins).
1874         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''. $page->cmid));
1875         // First message about double mentioning in sequence, second message about wrong section field for $page.
1876         $this->assertEquals(array(
1877             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
1878             '] must be removed from sequence of section ['. $section0->id.
1879             '] because it is also present in sequence of section ['. $section1->id. ']',
1880             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
1881             '] points to section ['. $section0->id. '] instead of ['. $section1->id. ']'),
1882                 course_integrity_check($course->id));
1883         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1884         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1885         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1886         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
1887         $this->assertEquals(''. $page->cmid, $section1->sequence);
1888         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1889         $this->assertEquals($section1->id, $cms[$page->cmid]->section);
1890         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1892         // 3. Module id is not present in course_section.sequence (integrity check with $fullcheck = false).
1893         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''));
1894         $this->assertEmpty(course_integrity_check($course->id)); // Not an error!
1895         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1896         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1897         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1898         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
1899         $this->assertEmpty($section1->sequence);
1900         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1901         $this->assertEquals($section1->id, $cms[$page->cmid]->section); // Not changed.
1902         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1904         // 4. Module id is not present in course_section.sequence (integrity check with $fullcheck = true).
1905         $this->assertEquals(array('Failed integrity check for course ['. $course->id. ']. Course module ['.
1906                 $page->cmid. '] is missing from sequence of section ['. $section1->id. ']'),
1907                 course_integrity_check($course->id, null, null, true)); // Error!
1908         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1909         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1910         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1911         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
1912         $this->assertEquals(''. $page->cmid, $section1->sequence);  // Yay, module added to section.
1913         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1914         $this->assertEquals($section1->id, $cms[$page->cmid]->section); // Not changed.
1915         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1917         // 5. Module id is not present in course_section.sequence and it's section is invalid (integrity check with $fullcheck = true).
1918         $DB->update_record('course_modules', array('id' => $page->cmid, 'section' => 8765));
1919         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''));
1920         $this->assertEquals(array(
1921             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
1922             '] is missing from sequence of section ['. $section0->id. ']',
1923             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
1924             '] points to section [8765] instead of ['. $section0->id. ']'),
1925                 course_integrity_check($course->id, null, null, true));
1926         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1927         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1928         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1929         $this->assertEquals($forum->cmid. ','. $quiz->cmid. ','. $page->cmid, $section0->sequence); // Module added to section.
1930         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1931         $this->assertEquals($section0->id, $cms[$page->cmid]->section); // Section changed to section0.
1932         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1934         // 6. Module is deleted from course_modules but not deleted in sequence (integrity check with $fullcheck = true).
1935         $DB->delete_records('course_modules', array('id' => $page->cmid));
1936         $this->assertEquals(array('Failed integrity check for course ['. $course->id. ']. Course module ['.
1937                 $page->cmid. '] does not exist but is present in the sequence of section ['. $section0->id. ']'),
1938                 course_integrity_check($course->id, null, null, true));
1939         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
1940         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
1941         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
1942         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
1943         $this->assertEmpty($section1->sequence);
1944         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
1945         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
1946         $this->assertEquals(2, count($cms));
1947     }
1949     /**
1950      * Tests for event related to course module creation.
1951      */
1952     public function test_course_module_created_event() {
1953         global $USER, $DB;
1954         $this->resetAfterTest();
1956         // Create an assign module.
1957         $sink = $this->redirectEvents();
1958         $modinfo = $this->create_specific_module_test('assign');
1959         $events = $sink->get_events();
1960         $event = array_pop($events);
1961         $sink->close();
1963         $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
1964         $mod = $DB->get_record('assign', array('id' => $modinfo->instance), '*', MUST_EXIST);
1966         // Validate event data.
1967         $this->assertInstanceOf('\core\event\course_module_created', $event);
1968         $this->assertEquals($cm->id, $event->objectid);
1969         $this->assertEquals($USER->id, $event->userid);
1970         $this->assertEquals('course_modules', $event->objecttable);
1971         $url = new moodle_url('/mod/assign/view.php', array('id' => $cm->id));
1972         $this->assertEquals($url, $event->get_url());
1974         // Test legacy data.
1975         $this->assertSame('mod_created', $event->get_legacy_eventname());
1976         $eventdata = new stdClass();
1977         $eventdata->modulename = 'assign';
1978         $eventdata->name       = $mod->name;
1979         $eventdata->cmid       = $cm->id;
1980         $eventdata->courseid   = $cm->course;
1981         $eventdata->userid     = $USER->id;
1982         $this->assertEventLegacyData($eventdata, $event);
1984         $arr = array(
1985             array($cm->course, "course", "add mod", "../mod/assign/view.php?id=$cm->id", "assign $cm->instance"),
1986             array($cm->course, "assign", "add", "view.php?id=$cm->id", $cm->instance, $cm->id)
1987         );
1988         $this->assertEventLegacyLogData($arr, $event);
1989         $this->assertEventContextNotUsed($event);
1991     }
1993     /**
1994      * Tests for event validations related to course module creation.
1995      */
1996     public function test_course_module_created_event_exceptions() {
1998         $this->resetAfterTest();
2000         // Generate data.
2001         $modinfo = $this->create_specific_module_test('assign');
2002         $context = context_module::instance($modinfo->coursemodule);
2004         // Test not setting instanceid.
2005         try {
2006             $event = \core\event\course_module_created::create(array(
2007                 'courseid' => $modinfo->course,
2008                 'context'  => $context,
2009                 'objectid' => $modinfo->coursemodule,
2010                 'other'    => array(
2011                     'modulename' => 'assign',
2012                     'name'       => 'My assignment',
2013                 )
2014             ));
2015             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2016                     other['instanceid']");
2017         } catch (coding_exception $e) {
2018             $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
2019         }
2021         // Test not setting modulename.
2022         try {
2023             $event = \core\event\course_module_created::create(array(
2024                 'courseid' => $modinfo->course,
2025                 'context'  => $context,
2026                 'objectid' => $modinfo->coursemodule,
2027                 'other'    => array(
2028                     'instanceid' => $modinfo->instance,
2029                     'name'       => 'My assignment',
2030                 )
2031             ));
2032             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2033                     other['modulename']");
2034         } catch (coding_exception $e) {
2035             $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
2036         }
2038         // Test not setting name.
2040         try {
2041             $event = \core\event\course_module_created::create(array(
2042                 'courseid' => $modinfo->course,
2043                 'context'  => $context,
2044                 'objectid' => $modinfo->coursemodule,
2045                 'other'    => array(
2046                     'modulename' => 'assign',
2047                     'instanceid' => $modinfo->instance,
2048                 )
2049             ));
2050             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2051                     other['name']");
2052         } catch (coding_exception $e) {
2053             $this->assertContains("The 'name' value must be set in other.", $e->getMessage());
2054         }
2056     }
2058     /**
2059      * Tests for event related to course module updates.
2060      */
2061     public function test_course_module_updated_event() {
2062         global $USER, $DB;
2063         $this->resetAfterTest();
2065         // Update a forum module.
2066         $sink = $this->redirectEvents();
2067         $modinfo = $this->update_specific_module_test('forum');
2068         $events = $sink->get_events();
2069         $event = array_pop($events);
2070         $sink->close();
2072         $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
2073         $mod = $DB->get_record('forum', array('id' => $cm->instance), '*', MUST_EXIST);
2075         // Validate event data.
2076         $this->assertInstanceOf('\core\event\course_module_updated', $event);
2077         $this->assertEquals($cm->id, $event->objectid);
2078         $this->assertEquals($USER->id, $event->userid);
2079         $this->assertEquals('course_modules', $event->objecttable);
2080         $url = new moodle_url('/mod/forum/view.php', array('id' => $cm->id));
2081         $this->assertEquals($url, $event->get_url());
2083         // Test legacy data.
2084         $this->assertSame('mod_updated', $event->get_legacy_eventname());
2085         $eventdata = new stdClass();
2086         $eventdata->modulename = 'forum';
2087         $eventdata->name       = $mod->name;
2088         $eventdata->cmid       = $cm->id;
2089         $eventdata->courseid   = $cm->course;
2090         $eventdata->userid     = $USER->id;
2091         $this->assertEventLegacyData($eventdata, $event);
2093         $arr = array(
2094             array($cm->course, "course", "update mod", "../mod/forum/view.php?id=$cm->id", "forum $cm->instance"),
2095             array($cm->course, "forum", "update", "view.php?id=$cm->id", $cm->instance, $cm->id)
2096         );
2097         $this->assertEventLegacyLogData($arr, $event);
2098         $this->assertEventContextNotUsed($event);
2099     }
2101     /**
2102      * Tests for create_from_cm method.
2103      */
2104     public function test_course_module_create_from_cm() {
2105         $this->resetAfterTest();
2106         $this->setAdminUser();
2108         // Create course and modules.
2109         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
2111         // Generate an assignment.
2112         $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
2114         // Get the module context.
2115         $modcontext = context_module::instance($assign->cmid);
2117         // Get course module.
2118         $cm = get_coursemodule_from_id(null, $assign->cmid, $course->id, false, MUST_EXIST);
2120         // Create an event from course module.
2121         $event = \core\event\course_module_updated::create_from_cm($cm, $modcontext);
2123         // Trigger the events.
2124         $sink = $this->redirectEvents();
2125         $event->trigger();
2126         $events = $sink->get_events();
2127         $event2 = array_pop($events);
2129         // Test event data.
2130         $this->assertInstanceOf('\core\event\course_module_updated', $event);
2131         $this->assertEquals($cm->id, $event2->objectid);
2132         $this->assertEquals($modcontext, $event2->get_context());
2133         $this->assertEquals($cm->modname, $event2->other['modulename']);
2134         $this->assertEquals($cm->instance, $event2->other['instanceid']);
2135         $this->assertEquals($cm->name, $event2->other['name']);
2136         $this->assertEventContextNotUsed($event2);
2137         $this->assertSame('mod_updated', $event2->get_legacy_eventname());
2138         $arr = array(
2139             array($cm->course, "course", "update mod", "../mod/assign/view.php?id=$cm->id", "assign $cm->instance"),
2140             array($cm->course, "assign", "update", "view.php?id=$cm->id", $cm->instance, $cm->id)
2141         );
2142         $this->assertEventLegacyLogData($arr, $event);
2143     }
2145     /**
2146      * Tests for event validations related to course module update.
2147      */
2148     public function test_course_module_updated_event_exceptions() {
2150         $this->resetAfterTest();
2152         // Generate data.
2153         $modinfo = $this->create_specific_module_test('assign');
2154         $context = context_module::instance($modinfo->coursemodule);
2156         // Test not setting instanceid.
2157         try {
2158             $event = \core\event\course_module_updated::create(array(
2159                 'courseid' => $modinfo->course,
2160                 'context'  => $context,
2161                 'objectid' => $modinfo->coursemodule,
2162                 'other'    => array(
2163                     'modulename' => 'assign',
2164                     'name'       => 'My assignment',
2165                 )
2166             ));
2167             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2168                     other['instanceid']");
2169         } catch (coding_exception $e) {
2170             $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
2171         }
2173         // Test not setting modulename.
2174         try {
2175             $event = \core\event\course_module_updated::create(array(
2176                 'courseid' => $modinfo->course,
2177                 'context'  => $context,
2178                 'objectid' => $modinfo->coursemodule,
2179                 'other'    => array(
2180                     'instanceid' => $modinfo->instance,
2181                     'name'       => 'My assignment',
2182                 )
2183             ));
2184             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2185                     other['modulename']");
2186         } catch (coding_exception $e) {
2187             $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
2188         }
2190         // Test not setting name.
2192         try {
2193             $event = \core\event\course_module_updated::create(array(
2194                 'courseid' => $modinfo->course,
2195                 'context'  => $context,
2196                 'objectid' => $modinfo->coursemodule,
2197                 'other'    => array(
2198                     'modulename' => 'assign',
2199                     'instanceid' => $modinfo->instance,
2200                 )
2201             ));
2202             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2203                     other['name']");
2204         } catch (coding_exception $e) {
2205             $this->assertContains("The 'name' value must be set in other.", $e->getMessage());
2206         }
2208     }
2210     /**
2211      * Tests for event related to course module delete.
2212      */
2213     public function test_course_module_deleted_event() {
2214         global $USER, $DB;
2215         $this->resetAfterTest();
2217         // Create and delete a module.
2218         $sink = $this->redirectEvents();
2219         $modinfo = $this->create_specific_module_test('forum');
2220         $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
2221         course_delete_module($modinfo->coursemodule);
2222         $events = $sink->get_events();
2223         $event = array_pop($events); // delete module event.;
2224         $sink->close();
2226         // Validate event data.
2227         $this->assertInstanceOf('\core\event\course_module_deleted', $event);
2228         $this->assertEquals($cm->id, $event->objectid);
2229         $this->assertEquals($USER->id, $event->userid);
2230         $this->assertEquals('course_modules', $event->objecttable);
2231         $this->assertEquals(null, $event->get_url());
2232         $this->assertEquals($cm, $event->get_record_snapshot('course_modules', $cm->id));
2234         // Test legacy data.
2235         $this->assertSame('mod_deleted', $event->get_legacy_eventname());
2236         $eventdata = new stdClass();
2237         $eventdata->modulename = 'forum';
2238         $eventdata->cmid       = $cm->id;
2239         $eventdata->courseid   = $cm->course;
2240         $eventdata->userid     = $USER->id;
2241         $this->assertEventLegacyData($eventdata, $event);
2243         $arr = array($cm->course, 'course', "delete mod", "view.php?id=$cm->course", "forum $cm->instance", $cm->id);
2244         $this->assertEventLegacyLogData($arr, $event);
2246     }
2248     /**
2249      * Tests for event validations related to course module deletion.
2250      */
2251     public function test_course_module_deleted_event_exceptions() {
2253         $this->resetAfterTest();
2255         // Generate data.
2256         $modinfo = $this->create_specific_module_test('assign');
2257         $context = context_module::instance($modinfo->coursemodule);
2259         // Test not setting instanceid.
2260         try {
2261             $event = \core\event\course_module_deleted::create(array(
2262                 'courseid' => $modinfo->course,
2263                 'context'  => $context,
2264                 'objectid' => $modinfo->coursemodule,
2265                 'other'    => array(
2266                     'modulename' => 'assign',
2267                     'name'       => 'My assignment',
2268                 )
2269             ));
2270             $this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
2271                     other['instanceid']");
2272         } catch (coding_exception $e) {
2273             $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
2274         }
2276         // Test not setting modulename.
2277         try {
2278             $event = \core\event\course_module_deleted::create(array(
2279                 'courseid' => $modinfo->course,
2280                 'context'  => $context,
2281                 'objectid' => $modinfo->coursemodule,
2282                 'other'    => array(
2283                     'instanceid' => $modinfo->instance,
2284                     'name'       => 'My assignment',
2285                 )
2286             ));
2287             $this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
2288                     other['modulename']");
2289         } catch (coding_exception $e) {
2290             $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
2291         }
2292     }
2294     /**
2295      * Returns a user object and its assigned new role.
2296      *
2297      * @param testing_data_generator $generator
2298      * @param $contextid
2299      * @return array The user object and the role ID
2300      */
2301     protected function get_user_objects(testing_data_generator $generator, $contextid) {
2302         global $USER;
2304         if (empty($USER->id)) {
2305             $user  = $generator->create_user();
2306             $this->setUser($user);
2307         }
2308         $roleid = create_role('Test role', 'testrole', 'Test role description');
2309         if (!is_array($contextid)) {
2310             $contextid = array($contextid);
2311         }
2312         foreach ($contextid as $cid) {
2313             $assignid = role_assign($roleid, $user->id, $cid);
2314         }
2315         return array($user, $roleid);
2316     }
2318     /**
2319      * Test course move after course.
2320      */
2321     public function test_course_change_sortorder_after_course() {
2322         global $DB;
2324         $this->resetAfterTest(true);
2326         $generator = $this->getDataGenerator();
2327         $category = $generator->create_category();
2328         $course3 = $generator->create_course(array('category' => $category->id));
2329         $course2 = $generator->create_course(array('category' => $category->id));
2330         $course1 = $generator->create_course(array('category' => $category->id));
2331         $context = $category->get_context();
2333         list($user, $roleid) = $this->get_user_objects($generator, $context->id);
2334         $caps = course_capability_assignment::allow('moodle/category:manage', $roleid, $context->id);
2336         $courses = $category->get_courses();
2337         $this->assertInternalType('array', $courses);
2338         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2339         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2340         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2342         // Test moving down.
2343         $this->assertTrue(course_change_sortorder_after_course($course1->id, $course3->id));
2344         $courses = $category->get_courses();
2345         $this->assertInternalType('array', $courses);
2346         $this->assertEquals(array($course2->id, $course3->id, $course1->id), array_keys($courses));
2347         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2348         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2350         // Test moving up.
2351         $this->assertTrue(course_change_sortorder_after_course($course1->id, $course2->id));
2352         $courses = $category->get_courses();
2353         $this->assertInternalType('array', $courses);
2354         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
2355         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2356         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2358         // Test moving to the top.
2359         $this->assertTrue(course_change_sortorder_after_course($course1->id, 0));
2360         $courses = $category->get_courses();
2361         $this->assertInternalType('array', $courses);
2362         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2363         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2364         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2365     }
2367     /**
2368      * Tests changing the visibility of a course.
2369      */
2370     public function test_course_change_visibility() {
2371         global $DB;
2373         $this->resetAfterTest(true);
2375         $generator = $this->getDataGenerator();
2376         $category = $generator->create_category();
2377         $course = $generator->create_course(array('category' => $category->id));
2379         $this->assertEquals('1', $course->visible);
2380         $this->assertEquals('1', $course->visibleold);
2382         $this->assertTrue(course_change_visibility($course->id, false));
2383         $course = $DB->get_record('course', array('id' => $course->id));
2384         $this->assertEquals('0', $course->visible);
2385         $this->assertEquals('0', $course->visibleold);
2387         $this->assertTrue(course_change_visibility($course->id, true));
2388         $course = $DB->get_record('course', array('id' => $course->id));
2389         $this->assertEquals('1', $course->visible);
2390         $this->assertEquals('1', $course->visibleold);
2391     }
2393     /**
2394      * Tests moving the course up and down by one.
2395      */
2396     public function test_course_change_sortorder_by_one() {
2397         global $DB;
2399         $this->resetAfterTest(true);
2401         $generator = $this->getDataGenerator();
2402         $category = $generator->create_category();
2403         $course3 = $generator->create_course(array('category' => $category->id));
2404         $course2 = $generator->create_course(array('category' => $category->id));
2405         $course1 = $generator->create_course(array('category' => $category->id));
2407         $courses = $category->get_courses();
2408         $this->assertInternalType('array', $courses);
2409         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2410         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2411         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2413         // Test moving down.
2414         $course1 = get_course($course1->id);
2415         $this->assertTrue(course_change_sortorder_by_one($course1, false));
2416         $courses = $category->get_courses();
2417         $this->assertInternalType('array', $courses);
2418         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
2419         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2420         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2422         // Test moving up.
2423         $course1 = get_course($course1->id);
2424         $this->assertTrue(course_change_sortorder_by_one($course1, true));
2425         $courses = $category->get_courses();
2426         $this->assertInternalType('array', $courses);
2427         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2428         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2429         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2431         // Test moving the top course up one.
2432         $course1 = get_course($course1->id);
2433         $this->assertFalse(course_change_sortorder_by_one($course1, true));
2434         // Check nothing changed.
2435         $courses = $category->get_courses();
2436         $this->assertInternalType('array', $courses);
2437         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2438         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2439         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2441         // Test moving the bottom course up down.
2442         $course3 = get_course($course3->id);
2443         $this->assertFalse(course_change_sortorder_by_one($course3, false));
2444         // Check nothing changed.
2445         $courses = $category->get_courses();
2446         $this->assertInternalType('array', $courses);
2447         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2448         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2449         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2450     }
2452     public function test_view_resources_list() {
2453         $this->resetAfterTest();
2455         $course = self::getDataGenerator()->create_course();
2456         $coursecontext = context_course::instance($course->id);
2458         $event = \core\event\course_resources_list_viewed::create(array('context' => context_course::instance($course->id)));
2459         $event->set_legacy_logdata(array('book', 'page', 'resource'));
2460         $sink = $this->redirectEvents();
2461         $event->trigger();
2462         $events = $sink->get_events();
2463         $sink->close();
2465         // Validate the event.
2466         $event = $events[0];
2467         $this->assertInstanceOf('\core\event\course_resources_list_viewed', $event);
2468         $this->assertEquals(null, $event->objecttable);
2469         $this->assertEquals(null, $event->objectid);
2470         $this->assertEquals($course->id, $event->courseid);
2471         $this->assertEquals($coursecontext->id, $event->contextid);
2472         $expectedlegacydata = array(
2473             array($course->id, "book", "view all", 'index.php?id=' . $course->id, ''),
2474             array($course->id, "page", "view all", 'index.php?id=' . $course->id, ''),
2475             array($course->id, "resource", "view all", 'index.php?id=' . $course->id, ''),
2476         );
2477         $this->assertEventLegacyLogData($expectedlegacydata, $event);
2478         $this->assertEventContextNotUsed($event);
2479     }
2481     /**
2482      * Test duplicate_module()
2483      */
2484     public function test_duplicate_module() {
2485         $this->setAdminUser();
2486         $this->resetAfterTest();
2487         $course = self::getDataGenerator()->create_course();
2488         $res = self::getDataGenerator()->create_module('resource', array('course' => $course));
2489         $cm = get_coursemodule_from_id('resource', $res->cmid, 0, false, MUST_EXIST);
2491         $newcm = duplicate_module($course, $cm);
2493         // Make sure they are the same, except obvious id changes.
2494         foreach ($cm as $prop => $value) {
2495             if ($prop == 'id' || $prop == 'url' || $prop == 'instance' || $prop == 'added') {
2496                 // Ignore obviously different properties.
2497                 continue;
2498             }
2499             $this->assertEquals($value, $newcm->$prop);
2500         }
2501     }