MDL-52252 Tags: Add tags to modules (Resources and Activities)
[moodle.git] / course / tests / courselib_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * Course related unit tests
19  *
20  * @package    core
21  * @category   phpunit
22  * @copyright  2012 Petr Skoda {@link http://skodak.org}
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->dirroot . '/course/lib.php');
30 require_once($CFG->dirroot . '/course/tests/fixtures/course_capability_assignment.php');
31 require_once($CFG->dirroot . '/enrol/imsenterprise/tests/imsenterprise_test.php');
33 class core_course_courselib_testcase extends advanced_testcase {
35     /**
36      * Tidy up open files that may be left open.
37      */
38     protected function tearDown() {
39         gc_collect_cycles();
40     }
42     /**
43      * Set forum specific test values for calling create_module().
44      *
45      * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
46      */
47     private function forum_create_set_values(&$moduleinfo) {
48         // Completion specific to forum - optional.
49         $moduleinfo->completionposts = 3;
50         $moduleinfo->completiondiscussions = 1;
51         $moduleinfo->completionreplies = 2;
53         // Specific values to the Forum module.
54         $moduleinfo->forcesubscribe = FORUM_INITIALSUBSCRIBE;
55         $moduleinfo->type = 'single';
56         $moduleinfo->trackingtype = FORUM_TRACKING_FORCED;
57         $moduleinfo->maxbytes = 10240;
58         $moduleinfo->maxattachments = 2;
60         // Post threshold for blocking - specific to forum.
61         $moduleinfo->blockperiod = 60*60*24;
62         $moduleinfo->blockafter = 10;
63         $moduleinfo->warnafter = 5;
64     }
66     /**
67      * Execute test asserts on the saved DB data by create_module($forum).
68      *
69      * @param object $moduleinfo - the specific forum values that were used to create a forum.
70      * @param object $dbmodinstance - the DB values of the created forum.
71      */
72     private function forum_create_run_asserts($moduleinfo, $dbmodinstance) {
73         // Compare values specific to forums.
74         $this->assertEquals($moduleinfo->forcesubscribe, $dbmodinstance->forcesubscribe);
75         $this->assertEquals($moduleinfo->type, $dbmodinstance->type);
76         $this->assertEquals($moduleinfo->assessed, $dbmodinstance->assessed);
77         $this->assertEquals($moduleinfo->completionposts, $dbmodinstance->completionposts);
78         $this->assertEquals($moduleinfo->completiondiscussions, $dbmodinstance->completiondiscussions);
79         $this->assertEquals($moduleinfo->completionreplies, $dbmodinstance->completionreplies);
80         $this->assertEquals($moduleinfo->scale, $dbmodinstance->scale);
81         $this->assertEquals($moduleinfo->assesstimestart, $dbmodinstance->assesstimestart);
82         $this->assertEquals($moduleinfo->assesstimefinish, $dbmodinstance->assesstimefinish);
83         $this->assertEquals($moduleinfo->rsstype, $dbmodinstance->rsstype);
84         $this->assertEquals($moduleinfo->rssarticles, $dbmodinstance->rssarticles);
85         $this->assertEquals($moduleinfo->trackingtype, $dbmodinstance->trackingtype);
86         $this->assertEquals($moduleinfo->maxbytes, $dbmodinstance->maxbytes);
87         $this->assertEquals($moduleinfo->maxattachments, $dbmodinstance->maxattachments);
88         $this->assertEquals($moduleinfo->blockperiod, $dbmodinstance->blockperiod);
89         $this->assertEquals($moduleinfo->blockafter, $dbmodinstance->blockafter);
90         $this->assertEquals($moduleinfo->warnafter, $dbmodinstance->warnafter);
91     }
93     /**
94      * Set assign module specific test values for calling create_module().
95      *
96      * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
97      */
98     private function assign_create_set_values(&$moduleinfo) {
99         // Specific values to the Assign module.
100         $moduleinfo->alwaysshowdescription = true;
101         $moduleinfo->submissiondrafts = true;
102         $moduleinfo->requiresubmissionstatement = true;
103         $moduleinfo->sendnotifications = true;
104         $moduleinfo->sendlatenotifications = true;
105         $moduleinfo->duedate = time() + (7 * 24 * 3600);
106         $moduleinfo->cutoffdate = time() + (7 * 24 * 3600);
107         $moduleinfo->allowsubmissionsfromdate = time();
108         $moduleinfo->teamsubmission = true;
109         $moduleinfo->requireallteammemberssubmit = true;
110         $moduleinfo->teamsubmissiongroupingid = true;
111         $moduleinfo->blindmarking = true;
112         $moduleinfo->markingworkflow = true;
113         $moduleinfo->markingallocation = true;
114         $moduleinfo->assignsubmission_onlinetext_enabled = true;
115         $moduleinfo->assignsubmission_file_enabled = true;
116         $moduleinfo->assignsubmission_file_maxfiles = 1;
117         $moduleinfo->assignsubmission_file_maxsizebytes = 1000000;
118         $moduleinfo->assignsubmission_comments_enabled = true;
119         $moduleinfo->assignfeedback_comments_enabled = true;
120         $moduleinfo->assignfeedback_offline_enabled = true;
121         $moduleinfo->assignfeedback_file_enabled = true;
123         // Advanced grading.
124         $gradingmethods = grading_manager::available_methods();
125         $moduleinfo->advancedgradingmethod_submissions = current(array_keys($gradingmethods));
126     }
128     /**
129      * Execute test asserts on the saved DB data by create_module($assign).
130      *
131      * @param object $moduleinfo - the specific assign module values that were used to create an assign module.
132      * @param object $dbmodinstance - the DB values of the created assign module.
133      */
134     private function assign_create_run_asserts($moduleinfo, $dbmodinstance) {
135         global $DB;
137         $this->assertEquals($moduleinfo->alwaysshowdescription, $dbmodinstance->alwaysshowdescription);
138         $this->assertEquals($moduleinfo->submissiondrafts, $dbmodinstance->submissiondrafts);
139         $this->assertEquals($moduleinfo->requiresubmissionstatement, $dbmodinstance->requiresubmissionstatement);
140         $this->assertEquals($moduleinfo->sendnotifications, $dbmodinstance->sendnotifications);
141         $this->assertEquals($moduleinfo->duedate, $dbmodinstance->duedate);
142         $this->assertEquals($moduleinfo->cutoffdate, $dbmodinstance->cutoffdate);
143         $this->assertEquals($moduleinfo->allowsubmissionsfromdate, $dbmodinstance->allowsubmissionsfromdate);
144         $this->assertEquals($moduleinfo->teamsubmission, $dbmodinstance->teamsubmission);
145         $this->assertEquals($moduleinfo->requireallteammemberssubmit, $dbmodinstance->requireallteammemberssubmit);
146         $this->assertEquals($moduleinfo->teamsubmissiongroupingid, $dbmodinstance->teamsubmissiongroupingid);
147         $this->assertEquals($moduleinfo->blindmarking, $dbmodinstance->blindmarking);
148         $this->assertEquals($moduleinfo->markingworkflow, $dbmodinstance->markingworkflow);
149         $this->assertEquals($moduleinfo->markingallocation, $dbmodinstance->markingallocation);
150         // The goal not being to fully test assign_add_instance() we'll stop here for the assign tests - to avoid too many DB queries.
152         // Advanced grading.
153         $cm = get_coursemodule_from_instance('assign', $dbmodinstance->id);
154         $contextmodule = context_module::instance($cm->id);
155         $advancedgradingmethod = $DB->get_record('grading_areas',
156             array('contextid' => $contextmodule->id,
157                 'activemethod' => $moduleinfo->advancedgradingmethod_submissions));
158         $this->assertEquals($moduleinfo->advancedgradingmethod_submissions, $advancedgradingmethod);
159     }
161     /**
162      * Run some asserts test for a specific module for the function create_module().
163      *
164      * The function has been created (and is called) for $this->test_create_module().
165      * Note that the call to MODULE_create_set_values and MODULE_create_run_asserts are done after the common set values/run asserts.
166      * So if you want, you can overwrite the default values/asserts in the respective functions.
167      * @param string $modulename Name of the module ('forum', 'assign', 'book'...).
168      */
169     private function create_specific_module_test($modulename) {
170         global $DB, $CFG;
172         $this->resetAfterTest(true);
174         $this->setAdminUser();
176         // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
177         require_once($CFG->dirroot.'/mod/'. $modulename .'/lib.php');
179         // Enable avaibility.
180         // If not enabled all conditional fields will be ignored.
181         set_config('enableavailability', 1);
183         // Enable course completion.
184         // If not enabled all completion settings will be ignored.
185         set_config('enablecompletion', COMPLETION_ENABLED);
187         // Enable forum RSS feeds.
188         set_config('enablerssfeeds', 1);
189         set_config('forum_enablerssfeeds', 1);
191         $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED),
192            array('createsections'=>true));
194         $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
196         // Create assign module instance for test.
197         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
198         $params['course'] = $course->id;
199         $instance = $generator->create_instance($params);
200         $assigncm = get_coursemodule_from_instance('assign', $instance->id);
202         // Module test values.
203         $moduleinfo = new stdClass();
205         // Always mandatory generic values to any module.
206         $moduleinfo->modulename = $modulename;
207         $moduleinfo->section = 1; // This is the section number in the course. Not the section id in the database.
208         $moduleinfo->course = $course->id;
209         $moduleinfo->groupingid = $grouping->id;
210         $moduleinfo->visible = true;
212         // Sometimes optional generic values for some modules.
213         $moduleinfo->name = 'My test module';
214         $moduleinfo->showdescription = 1; // standard boolean
215         require_once($CFG->libdir . '/gradelib.php');
216         $gradecats = grade_get_categories_menu($moduleinfo->course, false);
217         $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
218         $moduleinfo->gradecat = $gradecatid;
219         $moduleinfo->groupmode = VISIBLEGROUPS;
220         $moduleinfo->cmidnumber = 'idnumber_XXX';
222         // Completion common to all module.
223         $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC;
224         $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED;
225         $moduleinfo->completiongradeitemnumber = 1;
226         $moduleinfo->completionexpected = time() + (7 * 24 * 3600);
228         // Conditional activity.
229         $moduleinfo->availability = '{"op":"&","showc":[true,true],"c":[' .
230                 '{"type":"date","d":">=","t":' . time() . '},' .
231                 '{"type":"date","d":"<","t":' . (time() + (7 * 24 * 3600)) . '}' .
232                 ']}';
233         $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself.
234         $moduleinfo->conditiongradegroup = array(array('conditiongradeitemid' => $coursegradeitem->id, 'conditiongrademin' => 10, 'conditiongrademax' => 80));
235         $moduleinfo->conditionfieldgroup = array(array('conditionfield' => 'email', 'conditionfieldoperator' => \availability_profile\condition::OP_CONTAINS, 'conditionfieldvalue' => '@'));
236         $moduleinfo->conditioncompletiongroup = array(array('conditionsourcecmid' => $assigncm->id, 'conditionrequiredcompletion' => COMPLETION_COMPLETE)); // "conditionsourcecmid == 0" => none
238         // Grading and Advanced grading.
239         require_once($CFG->dirroot . '/rating/lib.php');
240         $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE;
241         $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number.
242         $moduleinfo->assesstimestart = time();
243         $moduleinfo->assesstimefinish = time() + (7 * 24 * 3600);
245         // RSS.
246         $moduleinfo->rsstype = 2;
247         $moduleinfo->rssarticles = 10;
249         // Optional intro editor (depends of module).
250         $draftid_editor = 0;
251         file_prepare_draft_area($draftid_editor, null, null, null, null);
252         $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
254         // Following is the advanced grading method area called 'submissions' for the 'assign' module.
255         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
256             $moduleinfo->grade = 100;
257         }
259         // Plagiarism form values.
260         // No plagiarism plugin installed by default. Use this space to make your own test.
262         // Values specific to the module.
263         $modulesetvalues = $modulename.'_create_set_values';
264         $this->$modulesetvalues($moduleinfo);
266         // Create the module.
267         $result = create_module($moduleinfo);
269         // Retrieve the module info.
270         $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance));
271         $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance);
272         // We passed the course section number to create_courses but $dbcm contain the section id.
273         // We need to retrieve the db course section number.
274         $section = $DB->get_record('course_sections', array('course' => $dbcm->course, 'id' => $dbcm->section));
275         // Retrieve the grade item.
276         $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course,
277             'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename));
279         // Compare the values common to all module instances.
280         $this->assertEquals($moduleinfo->modulename, $dbcm->modname);
281         $this->assertEquals($moduleinfo->section, $section->section);
282         $this->assertEquals($moduleinfo->course, $dbcm->course);
283         $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
284         $this->assertEquals($moduleinfo->visible, $dbcm->visible);
285         $this->assertEquals($moduleinfo->completion, $dbcm->completion);
286         $this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
287         $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber);
288         $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected);
289         $this->assertEquals($moduleinfo->availability, $dbcm->availability);
290         $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription);
291         $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode);
292         $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber);
293         $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid);
295         // Optional grade testing.
296         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
297             $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade);
298         }
300         // Some optional (but quite common) to some module.
301         $this->assertEquals($moduleinfo->name, $dbmodinstance->name);
302         $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro);
303         $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat);
305         // Test specific to the module.
306         $modulerunasserts = $modulename.'_create_run_asserts';
307         $this->$modulerunasserts($moduleinfo, $dbmodinstance);
308         return $moduleinfo;
309     }
311     /**
312      * Test create_module() for multiple modules defined in the $modules array (first declaration of the function).
313      */
314     public function test_create_module() {
315         // Add the module name you want to test here.
316         // Create the match MODULENAME_create_set_values() and MODULENAME_create_run_asserts().
317         $modules = array('forum', 'assign');
318         // Run all tests.
319         foreach ($modules as $modulename) {
320             $this->create_specific_module_test($modulename);
321         }
322     }
324     /**
325      * Test update_module() for multiple modules defined in the $modules array (first declaration of the function).
326      */
327     public function test_update_module() {
328         // Add the module name you want to test here.
329         // Create the match MODULENAME_update_set_values() and MODULENAME_update_run_asserts().
330         $modules = array('forum');
331         // Run all tests.
332         foreach ($modules as $modulename) {
333             $this->update_specific_module_test($modulename);
334         }
335     }
337     /**
338      * Set forum specific test values for calling update_module().
339      *
340      * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
341      */
342     private function forum_update_set_values(&$moduleinfo) {
343         // Completion specific to forum - optional.
344         $moduleinfo->completionposts = 3;
345         $moduleinfo->completiondiscussions = 1;
346         $moduleinfo->completionreplies = 2;
348         // Specific values to the Forum module.
349         $moduleinfo->forcesubscribe = FORUM_INITIALSUBSCRIBE;
350         $moduleinfo->type = 'single';
351         $moduleinfo->trackingtype = FORUM_TRACKING_FORCED;
352         $moduleinfo->maxbytes = 10240;
353         $moduleinfo->maxattachments = 2;
355         // Post threshold for blocking - specific to forum.
356         $moduleinfo->blockperiod = 60*60*24;
357         $moduleinfo->blockafter = 10;
358         $moduleinfo->warnafter = 5;
359     }
361     /**
362      * Execute test asserts on the saved DB data by update_module($forum).
363      *
364      * @param object $moduleinfo - the specific forum values that were used to update a forum.
365      * @param object $dbmodinstance - the DB values of the updated forum.
366      */
367     private function forum_update_run_asserts($moduleinfo, $dbmodinstance) {
368         // Compare values specific to forums.
369         $this->assertEquals($moduleinfo->forcesubscribe, $dbmodinstance->forcesubscribe);
370         $this->assertEquals($moduleinfo->type, $dbmodinstance->type);
371         $this->assertEquals($moduleinfo->assessed, $dbmodinstance->assessed);
372         $this->assertEquals($moduleinfo->completionposts, $dbmodinstance->completionposts);
373         $this->assertEquals($moduleinfo->completiondiscussions, $dbmodinstance->completiondiscussions);
374         $this->assertEquals($moduleinfo->completionreplies, $dbmodinstance->completionreplies);
375         $this->assertEquals($moduleinfo->scale, $dbmodinstance->scale);
376         $this->assertEquals($moduleinfo->assesstimestart, $dbmodinstance->assesstimestart);
377         $this->assertEquals($moduleinfo->assesstimefinish, $dbmodinstance->assesstimefinish);
378         $this->assertEquals($moduleinfo->rsstype, $dbmodinstance->rsstype);
379         $this->assertEquals($moduleinfo->rssarticles, $dbmodinstance->rssarticles);
380         $this->assertEquals($moduleinfo->trackingtype, $dbmodinstance->trackingtype);
381         $this->assertEquals($moduleinfo->maxbytes, $dbmodinstance->maxbytes);
382         $this->assertEquals($moduleinfo->maxattachments, $dbmodinstance->maxattachments);
383         $this->assertEquals($moduleinfo->blockperiod, $dbmodinstance->blockperiod);
384         $this->assertEquals($moduleinfo->blockafter, $dbmodinstance->blockafter);
385         $this->assertEquals($moduleinfo->warnafter, $dbmodinstance->warnafter);
386     }
390     /**
391      * Test a specific type of module.
392      *
393      * @param string $modulename - the module name to test
394      */
395     private function update_specific_module_test($modulename) {
396         global $DB, $CFG;
398         $this->resetAfterTest(true);
400         $this->setAdminUser();
402         // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
403         require_once($CFG->dirroot.'/mod/'. $modulename .'/lib.php');
405         // Enable avaibility.
406         // If not enabled all conditional fields will be ignored.
407         set_config('enableavailability', 1);
409         // Enable course completion.
410         // If not enabled all completion settings will be ignored.
411         set_config('enablecompletion', COMPLETION_ENABLED);
413         // Enable forum RSS feeds.
414         set_config('enablerssfeeds', 1);
415         set_config('forum_enablerssfeeds', 1);
417         $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED),
418            array('createsections'=>true));
420         $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
422         // Create assign module instance for testing gradeitem.
423         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
424         $params['course'] = $course->id;
425         $instance = $generator->create_instance($params);
426         $assigncm = get_coursemodule_from_instance('assign', $instance->id);
428         // Create the test forum to update.
429         $initvalues = new stdClass();
430         $initvalues->introformat = FORMAT_HTML;
431         $initvalues->course = $course->id;
432         $forum = self::getDataGenerator()->create_module('forum', $initvalues);
434         // Retrieve course module.
435         $cm = get_coursemodule_from_instance('forum', $forum->id);
437         // Module test values.
438         $moduleinfo = new stdClass();
440         // Always mandatory generic values to any module.
441         $moduleinfo->coursemodule = $cm->id;
442         $moduleinfo->modulename = $modulename;
443         $moduleinfo->course = $course->id;
444         $moduleinfo->groupingid = $grouping->id;
445         $moduleinfo->visible = true;
447         // Sometimes optional generic values for some modules.
448         $moduleinfo->name = 'My test module';
449         $moduleinfo->showdescription = 1; // standard boolean
450         require_once($CFG->libdir . '/gradelib.php');
451         $gradecats = grade_get_categories_menu($moduleinfo->course, false);
452         $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
453         $moduleinfo->gradecat = $gradecatid;
454         $moduleinfo->groupmode = VISIBLEGROUPS;
455         $moduleinfo->cmidnumber = 'idnumber_XXX';
457         // Completion common to all module.
458         $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC;
459         $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED;
460         $moduleinfo->completiongradeitemnumber = 1;
461         $moduleinfo->completionexpected = time() + (7 * 24 * 3600);
462         $moduleinfo->completionunlocked = 1;
464         // Conditional activity.
465         $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself.
466         $moduleinfo->availability = json_encode(\core_availability\tree::get_root_json(
467                 array(\availability_date\condition::get_json('>=', time()),
468                 \availability_date\condition::get_json('<', time() + (7 * 24 * 3600)),
469                 \availability_grade\condition::get_json($coursegradeitem->id, 10, 80),
470                 \availability_profile\condition::get_json(false, 'email', 'contains', '@'),
471                 \availability_completion\condition::get_json($assigncm->id, COMPLETION_COMPLETE)), '&'));
473         // Grading and Advanced grading.
474         require_once($CFG->dirroot . '/rating/lib.php');
475         $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE;
476         $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number.
477         $moduleinfo->assesstimestart = time();
478         $moduleinfo->assesstimefinish = time() + (7 * 24 * 3600);
480         // RSS.
481         $moduleinfo->rsstype = 2;
482         $moduleinfo->rssarticles = 10;
484         // Optional intro editor (depends of module).
485         $draftid_editor = 0;
486         file_prepare_draft_area($draftid_editor, null, null, null, null);
487         $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
489         // Following is the advanced grading method area called 'submissions' for the 'assign' module.
490         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
491             $moduleinfo->grade = 100;
492         }
493         // Plagiarism form values.
494         // No plagiarism plugin installed by default. Use this space to make your own test.
496         // Values specific to the module.
497         $modulesetvalues = $modulename.'_update_set_values';
498         $this->$modulesetvalues($moduleinfo);
500         // Create the module.
501         $result = update_module($moduleinfo);
503         // Retrieve the module info.
504         $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance));
505         $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance);
506         // Retrieve the grade item.
507         $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course,
508             'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename));
510         // Compare the values common to all module instances.
511         $this->assertEquals($moduleinfo->modulename, $dbcm->modname);
512         $this->assertEquals($moduleinfo->course, $dbcm->course);
513         $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
514         $this->assertEquals($moduleinfo->visible, $dbcm->visible);
515         $this->assertEquals($moduleinfo->completion, $dbcm->completion);
516         $this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
517         $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber);
518         $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected);
519         $this->assertEquals($moduleinfo->availability, $dbcm->availability);
520         $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription);
521         $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode);
522         $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber);
523         $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid);
525         // Optional grade testing.
526         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
527             $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade);
528         }
530         // Some optional (but quite common) to some module.
531         $this->assertEquals($moduleinfo->name, $dbmodinstance->name);
532         $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro);
533         $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat);
535         // Test specific to the module.
536         $modulerunasserts = $modulename.'_update_run_asserts';
537         $this->$modulerunasserts($moduleinfo, $dbmodinstance);
538         return $moduleinfo;
539    }
541     /**
542      * Data provider for course_delete module
543      *
544      * @return array An array of arrays contain test data
545      */
546     public function provider_course_delete_module() {
547         $data = array();
549         $data['assign'] = array('assign', array('duedate' => time()));
550         $data['quiz'] = array('quiz', array('duedate' => time()));
552         return $data;
553     }
555     /**
556      * Test the create_course function
557      */
558     public function test_create_course() {
559         global $DB;
560         $this->resetAfterTest(true);
561         $defaultcategory = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
563         $course = new stdClass();
564         $course->fullname = 'Apu loves Unit Təsts';
565         $course->shortname = 'Spread the lŭve';
566         $course->idnumber = '123';
567         $course->summary = 'Awesome!';
568         $course->summaryformat = FORMAT_PLAIN;
569         $course->format = 'topics';
570         $course->newsitems = 0;
571         $course->numsections = 5;
572         $course->category = $defaultcategory;
573         $original = (array) $course;
575         $created = create_course($course);
576         $context = context_course::instance($created->id);
578         // Compare original and created.
579         $this->assertEquals($original, array_intersect_key((array) $created, $original));
581         // Ensure default section is created.
582         $sectioncreated = $DB->record_exists('course_sections', array('course' => $created->id, 'section' => 0));
583         $this->assertTrue($sectioncreated);
585         // Ensure blocks have been associated to the course.
586         $blockcount = $DB->count_records('block_instances', array('parentcontextid' => $context->id));
587         $this->assertGreaterThan(0, $blockcount);
589         // Ensure that the shortname isn't duplicated.
590         try {
591             $created = create_course($course);
592             $this->fail('Exception expected');
593         } catch (moodle_exception $e) {
594             $this->assertSame(get_string('shortnametaken', 'error', $course->shortname), $e->getMessage());
595         }
597         // Ensure that the idnumber isn't duplicated.
598         $course->shortname .= '1';
599         try {
600             $created = create_course($course);
601             $this->fail('Exception expected');
602         } catch (moodle_exception $e) {
603             $this->assertSame(get_string('courseidnumbertaken', 'error', $course->idnumber), $e->getMessage());
604         }
605     }
607     public function test_create_course_with_generator() {
608         global $DB;
609         $this->resetAfterTest(true);
610         $course = $this->getDataGenerator()->create_course();
612         // Ensure default section is created.
613         $sectioncreated = $DB->record_exists('course_sections', array('course' => $course->id, 'section' => 0));
614         $this->assertTrue($sectioncreated);
615     }
617     public function test_create_course_sections() {
618         global $DB;
619         $this->resetAfterTest(true);
621         $course = $this->getDataGenerator()->create_course(
622                 array('shortname' => 'GrowingCourse',
623                     'fullname' => 'Growing Course',
624                     'numsections' => 5),
625                 array('createsections' => true));
627         // Ensure all 6 (0-5) sections were created and course content cache works properly
628         $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
629         $this->assertEquals(range(0, $course->numsections), $sectionscreated);
631         // this will do nothing, section already exists
632         $this->assertFalse(course_create_sections_if_missing($course, $course->numsections));
634         // this will create new section
635         $this->assertTrue(course_create_sections_if_missing($course, $course->numsections + 1));
637         // Ensure all 7 (0-6) sections were created and modinfo/sectioninfo cache works properly
638         $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
639         $this->assertEquals(range(0, $course->numsections + 1), $sectionscreated);
640     }
642     public function test_update_course() {
643         global $DB;
645         $this->resetAfterTest();
647         $defaultcategory = $DB->get_field_select('course_categories', 'MIN(id)', 'parent = 0');
649         $course = new stdClass();
650         $course->fullname = 'Apu loves Unit Təsts';
651         $course->shortname = 'test1';
652         $course->idnumber = '1';
653         $course->summary = 'Awesome!';
654         $course->summaryformat = FORMAT_PLAIN;
655         $course->format = 'topics';
656         $course->newsitems = 0;
657         $course->numsections = 5;
658         $course->category = $defaultcategory;
660         $created = create_course($course);
661         // Ensure the checks only work on idnumber/shortname that are not already ours.
662         update_course($created);
664         $course->shortname = 'test2';
665         $course->idnumber = '2';
667         $created2 = create_course($course);
669         // Test duplicate idnumber.
670         $created2->idnumber = '1';
671         try {
672             update_course($created2);
673             $this->fail('Expected exception when trying to update a course with duplicate idnumber');
674         } catch (moodle_exception $e) {
675             $this->assertEquals(get_string('courseidnumbertaken', 'error', $created2->idnumber), $e->getMessage());
676         }
678         // Test duplicate shortname.
679         $created2->idnumber = '2';
680         $created2->shortname = 'test1';
681         try {
682             update_course($created2);
683             $this->fail('Expected exception when trying to update a course with a duplicate shortname');
684         } catch (moodle_exception $e) {
685             $this->assertEquals(get_string('shortnametaken', 'error', $created2->shortname), $e->getMessage());
686         }
687     }
689     public function test_course_add_cm_to_section() {
690         global $DB;
691         $this->resetAfterTest(true);
693         // Create course with 1 section.
694         $course = $this->getDataGenerator()->create_course(
695                 array('shortname' => 'GrowingCourse',
696                     'fullname' => 'Growing Course',
697                     'numsections' => 1),
698                 array('createsections' => true));
700         // Trash modinfo.
701         rebuild_course_cache($course->id, true);
703         // Create some cms for testing.
704         $cmids = array();
705         for ($i=0; $i<4; $i++) {
706             $cmids[$i] = $DB->insert_record('course_modules', array('course' => $course->id));
707         }
709         // Add it to section that exists.
710         course_add_cm_to_section($course, $cmids[0], 1);
712         // Check it got added to sequence.
713         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 1));
714         $this->assertEquals($cmids[0], $sequence);
716         // Add a second, this time using courseid variant of parameters.
717         $coursecacherev = $DB->get_field('course', 'cacherev', array('id' => $course->id));
718         course_add_cm_to_section($course->id, $cmids[1], 1);
719         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 1));
720         $this->assertEquals($cmids[0] . ',' . $cmids[1], $sequence);
722         // Check that modinfo cache was reset but not rebuilt (important for performance if calling repeatedly).
723         $this->assertGreaterThan($coursecacherev, $DB->get_field('course', 'cacherev', array('id' => $course->id)));
724         $this->assertEmpty(cache::make('core', 'coursemodinfo')->get($course->id));
726         // Add one to section that doesn't exist (this might rebuild modinfo).
727         course_add_cm_to_section($course, $cmids[2], 2);
728         $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id)));
729         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 2));
730         $this->assertEquals($cmids[2], $sequence);
732         // Add using the 'before' option.
733         course_add_cm_to_section($course, $cmids[3], 2, $cmids[2]);
734         $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id)));
735         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 2));
736         $this->assertEquals($cmids[3] . ',' . $cmids[2], $sequence);
737     }
739     public function test_reorder_sections() {
740         global $DB;
741         $this->resetAfterTest(true);
743         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
744         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
745         $oldsections = array();
746         $sections = array();
747         foreach ($DB->get_records('course_sections', array('course'=>$course->id), 'id') as $section) {
748             $oldsections[$section->section] = $section->id;
749             $sections[$section->id] = $section->section;
750         }
751         ksort($oldsections);
753         $neworder = reorder_sections($sections, 2, 4);
754         $neworder = array_keys($neworder);
755         $this->assertEquals($oldsections[0], $neworder[0]);
756         $this->assertEquals($oldsections[1], $neworder[1]);
757         $this->assertEquals($oldsections[2], $neworder[4]);
758         $this->assertEquals($oldsections[3], $neworder[2]);
759         $this->assertEquals($oldsections[4], $neworder[3]);
760         $this->assertEquals($oldsections[5], $neworder[5]);
761         $this->assertEquals($oldsections[6], $neworder[6]);
763         $neworder = reorder_sections($sections, 4, 2);
764         $neworder = array_keys($neworder);
765         $this->assertEquals($oldsections[0], $neworder[0]);
766         $this->assertEquals($oldsections[1], $neworder[1]);
767         $this->assertEquals($oldsections[2], $neworder[3]);
768         $this->assertEquals($oldsections[3], $neworder[4]);
769         $this->assertEquals($oldsections[4], $neworder[2]);
770         $this->assertEquals($oldsections[5], $neworder[5]);
771         $this->assertEquals($oldsections[6], $neworder[6]);
773         $neworder = reorder_sections(1, 2, 4);
774         $this->assertFalse($neworder);
775     }
777     public function test_move_section_down() {
778         global $DB;
779         $this->resetAfterTest(true);
781         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
782         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
783         $oldsections = array();
784         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
785             $oldsections[$section->section] = $section->id;
786         }
787         ksort($oldsections);
789         // Test move section down..
790         move_section_to($course, 2, 4);
791         $sections = array();
792         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
793             $sections[$section->section] = $section->id;
794         }
795         ksort($sections);
797         $this->assertEquals($oldsections[0], $sections[0]);
798         $this->assertEquals($oldsections[1], $sections[1]);
799         $this->assertEquals($oldsections[2], $sections[4]);
800         $this->assertEquals($oldsections[3], $sections[2]);
801         $this->assertEquals($oldsections[4], $sections[3]);
802         $this->assertEquals($oldsections[5], $sections[5]);
803         $this->assertEquals($oldsections[6], $sections[6]);
804     }
806     public function test_move_section_up() {
807         global $DB;
808         $this->resetAfterTest(true);
810         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
811         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
812         $oldsections = array();
813         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
814             $oldsections[$section->section] = $section->id;
815         }
816         ksort($oldsections);
818         // Test move section up..
819         move_section_to($course, 6, 4);
820         $sections = array();
821         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
822             $sections[$section->section] = $section->id;
823         }
824         ksort($sections);
826         $this->assertEquals($oldsections[0], $sections[0]);
827         $this->assertEquals($oldsections[1], $sections[1]);
828         $this->assertEquals($oldsections[2], $sections[2]);
829         $this->assertEquals($oldsections[3], $sections[3]);
830         $this->assertEquals($oldsections[4], $sections[5]);
831         $this->assertEquals($oldsections[5], $sections[6]);
832         $this->assertEquals($oldsections[6], $sections[4]);
833     }
835     public function test_move_section_marker() {
836         global $DB;
837         $this->resetAfterTest(true);
839         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
840         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
842         // Set course marker to the section we are going to move..
843         course_set_marker($course->id, 2);
844         // Verify that the course marker is set correctly.
845         $course = $DB->get_record('course', array('id' => $course->id));
846         $this->assertEquals(2, $course->marker);
848         // Test move the marked section down..
849         move_section_to($course, 2, 4);
851         // Verify that the coruse marker has been moved along with the section..
852         $course = $DB->get_record('course', array('id' => $course->id));
853         $this->assertEquals(4, $course->marker);
855         // Test move the marked section up..
856         move_section_to($course, 4, 3);
858         // Verify that the course marker has been moved along with the section..
859         $course = $DB->get_record('course', array('id' => $course->id));
860         $this->assertEquals(3, $course->marker);
862         // Test moving a non-marked section above the marked section..
863         move_section_to($course, 4, 2);
865         // Verify that the course marker has been moved down to accomodate..
866         $course = $DB->get_record('course', array('id' => $course->id));
867         $this->assertEquals(4, $course->marker);
869         // Test moving a non-marked section below the marked section..
870         move_section_to($course, 3, 6);
872         // Verify that the course marker has been up to accomodate..
873         $course = $DB->get_record('course', array('id' => $course->id));
874         $this->assertEquals(3, $course->marker);
875     }
877     public function test_course_can_delete_section() {
878         global $DB;
879         $this->resetAfterTest(true);
881         $generator = $this->getDataGenerator();
883         $courseweeks = $generator->create_course(
884             array('numsections' => 5, 'format' => 'weeks'),
885             array('createsections' => true));
886         $assign1 = $generator->create_module('assign', array('course' => $courseweeks, 'section' => 1));
887         $assign2 = $generator->create_module('assign', array('course' => $courseweeks, 'section' => 2));
889         $coursetopics = $generator->create_course(
890             array('numsections' => 5, 'format' => 'topics'),
891             array('createsections' => true));
893         $coursesingleactivity = $generator->create_course(
894             array('format' => 'singleactivity'),
895             array('createsections' => true));
897         // Enrol student and teacher.
898         $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
899         $student = $generator->create_user();
900         $teacher = $generator->create_user();
902         $generator->enrol_user($student->id, $courseweeks->id, $roleids['student']);
903         $generator->enrol_user($teacher->id, $courseweeks->id, $roleids['editingteacher']);
905         $generator->enrol_user($student->id, $coursetopics->id, $roleids['student']);
906         $generator->enrol_user($teacher->id, $coursetopics->id, $roleids['editingteacher']);
908         $generator->enrol_user($student->id, $coursesingleactivity->id, $roleids['student']);
909         $generator->enrol_user($teacher->id, $coursesingleactivity->id, $roleids['editingteacher']);
911         // Teacher should be able to delete sections (except for 0) in topics and weeks format.
912         $this->setUser($teacher);
914         // For topics and weeks formats will return false for section 0 and true for any other section.
915         $this->assertFalse(course_can_delete_section($courseweeks, 0));
916         $this->assertTrue(course_can_delete_section($courseweeks, 1));
918         $this->assertFalse(course_can_delete_section($coursetopics, 0));
919         $this->assertTrue(course_can_delete_section($coursetopics, 1));
921         // For singleactivity course format no section can be deleted.
922         $this->assertFalse(course_can_delete_section($coursesingleactivity, 0));
923         $this->assertFalse(course_can_delete_section($coursesingleactivity, 1));
925         // Now let's revoke a capability from teacher to manage activity in section 1.
926         $modulecontext = context_module::instance($assign1->cmid);
927         assign_capability('moodle/course:manageactivities', CAP_PROHIBIT, $roleids['editingteacher'],
928             $modulecontext);
929         $modulecontext->mark_dirty();
930         $this->assertFalse(course_can_delete_section($courseweeks, 1));
931         $this->assertTrue(course_can_delete_section($courseweeks, 2));
933         // Student does not have permissions to delete sections.
934         $this->setUser($student);
935         $this->assertFalse(course_can_delete_section($courseweeks, 1));
936         $this->assertFalse(course_can_delete_section($coursetopics, 1));
937         $this->assertFalse(course_can_delete_section($coursesingleactivity, 1));
938     }
940     public function test_course_delete_section() {
941         global $DB;
942         $this->resetAfterTest(true);
944         $generator = $this->getDataGenerator();
946         $course = $generator->create_course(array('numsections' => 6, 'format' => 'topics'),
947             array('createsections' => true));
948         $assign0 = $generator->create_module('assign', array('course' => $course, 'section' => 0));
949         $assign1 = $generator->create_module('assign', array('course' => $course, 'section' => 1));
950         $assign21 = $generator->create_module('assign', array('course' => $course, 'section' => 2));
951         $assign22 = $generator->create_module('assign', array('course' => $course, 'section' => 2));
952         $assign3 = $generator->create_module('assign', array('course' => $course, 'section' => 3));
953         $assign5 = $generator->create_module('assign', array('course' => $course, 'section' => 5));
954         $assign6 = $generator->create_module('assign', array('course' => $course, 'section' => 6));
956         $this->setAdminUser();
958         // Attempt to delete non-existing section.
959         $this->assertFalse(course_delete_section($course, 10, false));
960         $this->assertFalse(course_delete_section($course, 9, true));
962         // Attempt to delete 0-section.
963         $this->assertFalse(course_delete_section($course, 0, true));
964         $this->assertTrue($DB->record_exists('course_modules', array('id' => $assign0->cmid)));
966         // Delete last section.
967         $this->assertTrue(course_delete_section($course, 6, true));
968         $this->assertFalse($DB->record_exists('course_modules', array('id' => $assign6->cmid)));
969         $this->assertEquals(5, course_get_format($course)->get_course()->numsections);
971         // Delete empty section.
972         $this->assertTrue(course_delete_section($course, 4, false));
973         $this->assertEquals(4, course_get_format($course)->get_course()->numsections);
975         // Delete section in the middle (2).
976         $this->assertFalse(course_delete_section($course, 2, false));
977         $this->assertTrue(course_delete_section($course, 2, true));
978         $this->assertFalse($DB->record_exists('course_modules', array('id' => $assign21->cmid)));
979         $this->assertFalse($DB->record_exists('course_modules', array('id' => $assign22->cmid)));
980         $this->assertEquals(3, course_get_format($course)->get_course()->numsections);
981         $this->assertEquals(array(0 => array($assign0->cmid),
982             1 => array($assign1->cmid),
983             2 => array($assign3->cmid),
984             3 => array($assign5->cmid)), get_fast_modinfo($course)->sections);
986         // Make last section orphaned.
987         update_course((object)array('id' => $course->id, 'numsections' => 2));
988         $this->assertEquals(2, course_get_format($course)->get_course()->numsections);
990         // Remove orphaned section.
991         $this->assertTrue(course_delete_section($course, 3, true));
992         $this->assertEquals(2, course_get_format($course)->get_course()->numsections);
994         // Remove marked section.
995         course_set_marker($course->id, 1);
996         $this->assertTrue(course_get_format($course)->is_section_current(1));
997         $this->assertTrue(course_delete_section($course, 1, true));
998         $this->assertFalse(course_get_format($course)->is_section_current(1));
999     }
1001     public function test_get_course_display_name_for_list() {
1002         global $CFG;
1003         $this->resetAfterTest(true);
1005         $course = $this->getDataGenerator()->create_course(array('shortname' => 'FROG101', 'fullname' => 'Introduction to pond life'));
1007         $CFG->courselistshortnames = 0;
1008         $this->assertEquals('Introduction to pond life', get_course_display_name_for_list($course));
1010         $CFG->courselistshortnames = 1;
1011         $this->assertEquals('FROG101 Introduction to pond life', get_course_display_name_for_list($course));
1012     }
1014     public function test_move_module_in_course() {
1015         global $DB;
1017         $this->resetAfterTest(true);
1018         // Setup fixture
1019         $course = $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections' => true));
1020         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
1022         $cms = get_fast_modinfo($course)->get_cms();
1023         $cm = reset($cms);
1025         $newsection = get_fast_modinfo($course)->get_section_info(3);
1026         $oldsectionid = $cm->section;
1028         // Perform the move
1029         moveto_module($cm, $newsection);
1031         $cms = get_fast_modinfo($course)->get_cms();
1032         $cm = reset($cms);
1034         // Check that the cached modinfo contains the correct section info
1035         $modinfo = get_fast_modinfo($course);
1036         $this->assertTrue(empty($modinfo->sections[0]));
1037         $this->assertFalse(empty($modinfo->sections[3]));
1039         // Check that the old section's sequence no longer contains this ID
1040         $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
1041         $oldsequences = explode(',', $newsection->sequence);
1042         $this->assertFalse(in_array($cm->id, $oldsequences));
1044         // Check that the new section's sequence now contains this ID
1045         $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
1046         $newsequences = explode(',', $newsection->sequence);
1047         $this->assertTrue(in_array($cm->id, $newsequences));
1049         // Check that the section number has been changed in the cm
1050         $this->assertEquals($newsection->id, $cm->section);
1053         // Perform a second move as some issues were only seen on the second move
1054         $newsection = get_fast_modinfo($course)->get_section_info(2);
1055         $oldsectionid = $cm->section;
1056         moveto_module($cm, $newsection);
1058         $cms = get_fast_modinfo($course)->get_cms();
1059         $cm = reset($cms);
1061         // Check that the cached modinfo contains the correct section info
1062         $modinfo = get_fast_modinfo($course);
1063         $this->assertTrue(empty($modinfo->sections[0]));
1064         $this->assertFalse(empty($modinfo->sections[2]));
1066         // Check that the old section's sequence no longer contains this ID
1067         $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
1068         $oldsequences = explode(',', $newsection->sequence);
1069         $this->assertFalse(in_array($cm->id, $oldsequences));
1071         // Check that the new section's sequence now contains this ID
1072         $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
1073         $newsequences = explode(',', $newsection->sequence);
1074         $this->assertTrue(in_array($cm->id, $newsequences));
1075     }
1077     public function test_module_visibility() {
1078         $this->setAdminUser();
1079         $this->resetAfterTest(true);
1081         // Create course and modules.
1082         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
1083         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1084         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(), 'course' => $course->id));
1085         $modules = compact('forum', 'assign');
1087         // Hiding the modules.
1088         foreach ($modules as $mod) {
1089             set_coursemodule_visible($mod->cmid, 0);
1090             $this->check_module_visibility($mod, 0, 0);
1091         }
1093         // Showing the modules.
1094         foreach ($modules as $mod) {
1095             set_coursemodule_visible($mod->cmid, 1);
1096             $this->check_module_visibility($mod, 1, 1);
1097         }
1098     }
1100     public function test_section_visibility_events() {
1101         $this->setAdminUser();
1102         $this->resetAfterTest(true);
1104         $course = $this->getDataGenerator()->create_course(array('numsections' => 1), array('createsections' => true));
1105         $sectionnumber = 1;
1106         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1107             array('section' => $sectionnumber));
1108         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1109             'course' => $course->id), array('section' => $sectionnumber));
1110         $sink = $this->redirectEvents();
1111         set_section_visible($course->id, $sectionnumber, 0);
1112         $events = $sink->get_events();
1114         // Extract the number of events related to what we are testing, other events
1115         // such as course_section_updated could have been triggered.
1116         $count = 0;
1117         foreach ($events as $event) {
1118             if ($event instanceof \core\event\course_module_updated) {
1119                 $count++;
1120             }
1121         }
1122         $this->assertSame(2, $count);
1123         $sink->close();
1124     }
1126     public function test_section_visibility() {
1127         $this->setAdminUser();
1128         $this->resetAfterTest(true);
1130         // Create course.
1131         $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
1133         $sink = $this->redirectEvents();
1135         // Testing an empty section.
1136         $sectionnumber = 1;
1137         set_section_visible($course->id, $sectionnumber, 0);
1138         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1139         $this->assertEquals($section_info->visible, 0);
1140         set_section_visible($course->id, $sectionnumber, 1);
1141         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1142         $this->assertEquals($section_info->visible, 1);
1144         // Checking that an event was fired.
1145         $events = $sink->get_events();
1146         $this->assertInstanceOf('\core\event\course_section_updated', $events[0]);
1147         $sink->close();
1149         // Testing a section with visible modules.
1150         $sectionnumber = 2;
1151         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1152                 array('section' => $sectionnumber));
1153         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1154                 'course' => $course->id), array('section' => $sectionnumber));
1155         $modules = compact('forum', 'assign');
1156         set_section_visible($course->id, $sectionnumber, 0);
1157         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1158         $this->assertEquals($section_info->visible, 0);
1159         foreach ($modules as $mod) {
1160             $this->check_module_visibility($mod, 0, 1);
1161         }
1162         set_section_visible($course->id, $sectionnumber, 1);
1163         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1164         $this->assertEquals($section_info->visible, 1);
1165         foreach ($modules as $mod) {
1166             $this->check_module_visibility($mod, 1, 1);
1167         }
1169         // Testing a section with hidden modules, which should stay hidden.
1170         $sectionnumber = 3;
1171         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1172                 array('section' => $sectionnumber));
1173         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1174                 'course' => $course->id), array('section' => $sectionnumber));
1175         $modules = compact('forum', 'assign');
1176         foreach ($modules as $mod) {
1177             set_coursemodule_visible($mod->cmid, 0);
1178             $this->check_module_visibility($mod, 0, 0);
1179         }
1180         set_section_visible($course->id, $sectionnumber, 0);
1181         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1182         $this->assertEquals($section_info->visible, 0);
1183         foreach ($modules as $mod) {
1184             $this->check_module_visibility($mod, 0, 0);
1185         }
1186         set_section_visible($course->id, $sectionnumber, 1);
1187         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1188         $this->assertEquals($section_info->visible, 1);
1189         foreach ($modules as $mod) {
1190             $this->check_module_visibility($mod, 0, 0);
1191         }
1192     }
1194     /**
1195      * Helper function to assert that a module has correctly been made visible, or hidden.
1196      *
1197      * @param stdClass $mod module information
1198      * @param int $visibility the current state of the module
1199      * @param int $visibleold the current state of the visibleold property
1200      * @return void
1201      */
1202     public function check_module_visibility($mod, $visibility, $visibleold) {
1203         global $DB;
1204         $cm = get_fast_modinfo($mod->course)->get_cm($mod->cmid);
1205         $this->assertEquals($visibility, $cm->visible);
1206         $this->assertEquals($visibleold, $cm->visibleold);
1208         // Check the module grade items.
1209         $grade_items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $cm->modname,
1210                 'iteminstance' => $cm->instance, 'courseid' => $cm->course));
1211         if ($grade_items) {
1212             foreach ($grade_items as $grade_item) {
1213                 if ($visibility) {
1214                     $this->assertFalse($grade_item->is_hidden(), "$cm->modname grade_item not visible");
1215                 } else {
1216                     $this->assertTrue($grade_item->is_hidden(), "$cm->modname grade_item not hidden");
1217                 }
1218             }
1219         }
1221         // Check the events visibility.
1222         if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $cm->modname))) {
1223             foreach ($events as $event) {
1224                 $calevent = new calendar_event($event);
1225                 $this->assertEquals($visibility, $calevent->visible, "$cm->modname calendar_event visibility");
1226             }
1227         }
1228     }
1230     public function test_course_page_type_list() {
1231         global $DB;
1232         $this->resetAfterTest(true);
1234         // Create a category.
1235         $category = new stdClass();
1236         $category->name = 'Test Category';
1238         $testcategory = $this->getDataGenerator()->create_category($category);
1240         // Create a course.
1241         $course = new stdClass();
1242         $course->fullname = 'Apu loves Unit Təsts';
1243         $course->shortname = 'Spread the lŭve';
1244         $course->idnumber = '123';
1245         $course->summary = 'Awesome!';
1246         $course->summaryformat = FORMAT_PLAIN;
1247         $course->format = 'topics';
1248         $course->newsitems = 0;
1249         $course->numsections = 5;
1250         $course->category = $testcategory->id;
1252         $testcourse = $this->getDataGenerator()->create_course($course);
1254         // Create contexts.
1255         $coursecontext = context_course::instance($testcourse->id);
1256         $parentcontext = $coursecontext->get_parent_context(); // Not actually used.
1257         $pagetype = 'page-course-x'; // Not used either.
1258         $pagetypelist = course_page_type_list($pagetype, $parentcontext, $coursecontext);
1260         // Page type lists for normal courses.
1261         $testpagetypelist1 = array();
1262         $testpagetypelist1['*'] = 'Any page';
1263         $testpagetypelist1['course-*'] = 'Any course page';
1264         $testpagetypelist1['course-view-*'] = 'Any type of course main page';
1266         $this->assertEquals($testpagetypelist1, $pagetypelist);
1268         // Get the context for the front page course.
1269         $sitecoursecontext = context_course::instance(SITEID);
1270         $pagetypelist = course_page_type_list($pagetype, $parentcontext, $sitecoursecontext);
1272         // Page type list for the front page course.
1273         $testpagetypelist2 = array('*' => 'Any page');
1274         $this->assertEquals($testpagetypelist2, $pagetypelist);
1276         // Make sure that providing no current context to the function doesn't result in an error.
1277         // Calls made from generate_page_type_patterns() may provide null values.
1278         $pagetypelist = course_page_type_list($pagetype, null, null);
1279         $this->assertEquals($pagetypelist, $testpagetypelist1);
1280     }
1282     public function test_compare_activities_by_time_desc() {
1284         // Let's create some test data.
1285         $activitiesivities = array();
1286         $x = new stdClass();
1287         $x->timestamp = null;
1288         $activities[] = $x;
1290         $x = new stdClass();
1291         $x->timestamp = 1;
1292         $activities[] = $x;
1294         $x = new stdClass();
1295         $x->timestamp = 3;
1296         $activities[] = $x;
1298         $x = new stdClass();
1299         $x->timestamp = 0;
1300         $activities[] = $x;
1302         $x = new stdClass();
1303         $x->timestamp = 5;
1304         $activities[] = $x;
1306         $x = new stdClass();
1307         $activities[] = $x;
1309         $x = new stdClass();
1310         $x->timestamp = 5;
1311         $activities[] = $x;
1313         // Do the sorting.
1314         usort($activities, 'compare_activities_by_time_desc');
1316         // Let's check the result.
1317         $last = 10;
1318         foreach($activities as $activity) {
1319             if (empty($activity->timestamp)) {
1320                 $activity->timestamp = 0;
1321             }
1322             $this->assertLessThanOrEqual($last, $activity->timestamp);
1323         }
1324     }
1326     public function test_compare_activities_by_time_asc() {
1328         // Let's create some test data.
1329         $activities = array();
1330         $x = new stdClass();
1331         $x->timestamp = null;
1332         $activities[] = $x;
1334         $x = new stdClass();
1335         $x->timestamp = 1;
1336         $activities[] = $x;
1338         $x = new stdClass();
1339         $x->timestamp = 3;
1340         $activities[] = $x;
1342         $x = new stdClass();
1343         $x->timestamp = 0;
1344         $activities[] = $x;
1346         $x = new stdClass();
1347         $x->timestamp = 5;
1348         $activities[] = $x;
1350         $x = new stdClass();
1351         $activities[] = $x;
1353         $x = new stdClass();
1354         $x->timestamp = 5;
1355         $activities[] = $x;
1357         // Do the sorting.
1358         usort($activities, 'compare_activities_by_time_asc');
1360         // Let's check the result.
1361         $last = 0;
1362         foreach($activities as $activity) {
1363             if (empty($activity->timestamp)) {
1364                 $activity->timestamp = 0;
1365             }
1366             $this->assertGreaterThanOrEqual($last, $activity->timestamp);
1367         }
1368     }
1370     /**
1371      * Tests moving a module between hidden/visible sections and
1372      * verifies that the course/module visiblity seettings are
1373      * retained.
1374      */
1375     public function test_moveto_module_between_hidden_sections() {
1376         global $DB;
1378         $this->resetAfterTest(true);
1380         $course = $this->getDataGenerator()->create_course(array('numsections' => 4), array('createsections' => true));
1381         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1382         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
1383         $quiz= $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
1385         // Set the page as hidden
1386         set_coursemodule_visible($page->cmid, 0);
1388         // Set sections 3 as hidden.
1389         set_section_visible($course->id, 3, 0);
1391         $modinfo = get_fast_modinfo($course);
1393         $hiddensection = $modinfo->get_section_info(3);
1394         // New section is definitely not visible:
1395         $this->assertEquals($hiddensection->visible, 0);
1397         $forumcm = $modinfo->cms[$forum->cmid];
1398         $pagecm = $modinfo->cms[$page->cmid];
1400         // Move the forum and the page to a hidden section, make sure moveto_module returns 0 as new visibility state.
1401         $this->assertEquals(0, moveto_module($forumcm, $hiddensection));
1402         $this->assertEquals(0, moveto_module($pagecm, $hiddensection));
1404         $modinfo = get_fast_modinfo($course);
1406         // Verify that forum and page have been moved to the hidden section and quiz has not.
1407         $this->assertContains($forum->cmid, $modinfo->sections[3]);
1408         $this->assertContains($page->cmid, $modinfo->sections[3]);
1409         $this->assertNotContains($quiz->cmid, $modinfo->sections[3]);
1411         // Verify that forum has been made invisible.
1412         $forumcm = $modinfo->cms[$forum->cmid];
1413         $this->assertEquals($forumcm->visible, 0);
1414         // Verify that old state has been retained.
1415         $this->assertEquals($forumcm->visibleold, 1);
1417         // Verify that page has stayed invisible.
1418         $pagecm = $modinfo->cms[$page->cmid];
1419         $this->assertEquals($pagecm->visible, 0);
1420         // Verify that old state has been retained.
1421         $this->assertEquals($pagecm->visibleold, 0);
1423         // Verify that quiz has been unaffected.
1424         $quizcm = $modinfo->cms[$quiz->cmid];
1425         $this->assertEquals($quizcm->visible, 1);
1427         // Move forum and page back to visible section.
1428         // Make sure the visibility is restored to the original value (visible for forum and hidden for page).
1429         $visiblesection = $modinfo->get_section_info(2);
1430         $this->assertEquals(1, moveto_module($forumcm, $visiblesection));
1431         $this->assertEquals(0, moveto_module($pagecm, $visiblesection));
1433         $modinfo = get_fast_modinfo($course);
1435         // Double check that forum has been made visible.
1436         $forumcm = $modinfo->cms[$forum->cmid];
1437         $this->assertEquals($forumcm->visible, 1);
1439         // Double check that page has stayed invisible.
1440         $pagecm = $modinfo->cms[$page->cmid];
1441         $this->assertEquals($pagecm->visible, 0);
1443         // Move the page in the same section (this is what mod duplicate does).
1444         // Visibility of page remains 0.
1445         $this->assertEquals(0, moveto_module($pagecm, $visiblesection, $forumcm));
1447         // Double check that the the page is still hidden.
1448         $modinfo = get_fast_modinfo($course);
1449         $pagecm = $modinfo->cms[$page->cmid];
1450         $this->assertEquals($pagecm->visible, 0);
1451     }
1453     /**
1454      * Tests moving a module around in the same section. moveto_module()
1455      * is called this way in modduplicate.
1456      */
1457     public function test_moveto_module_in_same_section() {
1458         global $DB;
1460         $this->resetAfterTest(true);
1462         $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
1463         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
1464         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1466         // Simulate inconsistent visible/visibleold values (MDL-38713).
1467         $cm = $DB->get_record('course_modules', array('id' => $page->cmid), '*', MUST_EXIST);
1468         $cm->visible = 0;
1469         $cm->visibleold = 1;
1470         $DB->update_record('course_modules', $cm);
1472         $modinfo = get_fast_modinfo($course);
1473         $forumcm = $modinfo->cms[$forum->cmid];
1474         $pagecm = $modinfo->cms[$page->cmid];
1476         // Verify that page is hidden.
1477         $this->assertEquals($pagecm->visible, 0);
1479         // Verify section 0 is where all mods added.
1480         $section = $modinfo->get_section_info(0);
1481         $this->assertEquals($section->id, $forumcm->section);
1482         $this->assertEquals($section->id, $pagecm->section);
1485         // Move the page inside the hidden section. Make sure it is hidden.
1486         $this->assertEquals(0, moveto_module($pagecm, $section, $forumcm));
1488         // Double check that the the page is still hidden.
1489         $modinfo = get_fast_modinfo($course);
1490         $pagecm = $modinfo->cms[$page->cmid];
1491         $this->assertEquals($pagecm->visible, 0);
1492     }
1494     /**
1495      * Tests the function that deletes a course module
1496      *
1497      * @param string $type The type of module for the test
1498      * @param array $options The options for the module creation
1499      * @dataProvider provider_course_delete_module
1500      */
1501     public function test_course_delete_module($type, $options) {
1502         global $DB;
1504         $this->resetAfterTest(true);
1505         $this->setAdminUser();
1507         // Create course and modules.
1508         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
1509         $options['course'] = $course->id;
1511         // Generate an assignment with due date (will generate a course event).
1512         $module = $this->getDataGenerator()->create_module($type, $options);
1514         // Get the module context.
1515         $modcontext = context_module::instance($module->cmid);
1517         // Verify context exists.
1518         $this->assertInstanceOf('context_module', $modcontext);
1520         // Make module specific messes.
1521         switch ($type) {
1522             case 'assign':
1523                 // Add some tags to this assignment.
1524                 core_tag_tag::set_item_tags('core', 'course_modules', $module->id, $modcontext, array('Tag 1', 'Tag 2', 'Tag 3'));
1526                 // Confirm the tag instances were added.
1527                 $criteria = array('component' => 'core', 'itemtype' => 'course_modules', 'contextid' => $modcontext->id);
1528                 $this->assertEquals(3, $DB->count_records('tag_instance', $criteria));
1530                 // Verify event assignment event has been generated.
1531                 $eventcount = $DB->count_records('event', array('instance' => $module->id, 'modulename' => $type));
1532                 $this->assertEquals(1, $eventcount);
1534                 break;
1535             case 'quiz':
1536                 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
1537                 $qcat = $qgen->create_question_category(array('contextid' => $modcontext->id));
1538                 $questions = array(
1539                     $qgen->create_question('shortanswer', null, array('category' => $qcat->id)),
1540                     $qgen->create_question('shortanswer', null, array('category' => $qcat->id)),
1541                 );
1542                 $this->expectOutputRegex('/'.get_string('unusedcategorydeleted', 'question').'/');
1543                 break;
1544             default:
1545                 break;
1546         }
1548         // Run delete..
1549         course_delete_module($module->cmid);
1551         // Verify the context has been removed.
1552         $this->assertFalse(context_module::instance($module->cmid, IGNORE_MISSING));
1554         // Verify the course_module record has been deleted.
1555         $cmcount = $DB->count_records('course_modules', array('id' => $module->cmid));
1556         $this->assertEmpty($cmcount);
1558         // Test clean up of module specific messes.
1559         switch ($type) {
1560             case 'assign':
1561                 // Verify event assignment events have been removed.
1562                 $eventcount = $DB->count_records('event', array('instance' => $module->id, 'modulename' => $type));
1563                 $this->assertEmpty($eventcount);
1565                 // Verify the tag instances were deleted.
1566                 $criteria = array('component' => 'mod_assign', 'contextid' => $modcontext->id);
1567                 $this->assertEquals(0, $DB->count_records('tag_instance', $criteria));
1568                 break;
1569             case 'quiz':
1570                 // Verify category deleted.
1571                 $criteria = array('contextid' => $modcontext->id);
1572                 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
1574                 // Verify questions deleted.
1575                 $criteria = array('category' => $qcat->id);
1576                 $this->assertEquals(0, $DB->count_records('question', $criteria));
1577                 break;
1578             default:
1579                 break;
1580         }
1581     }
1583     /**
1584      * Test that triggering a course_created event works as expected.
1585      */
1586     public function test_course_created_event() {
1587         global $DB;
1589         $this->resetAfterTest();
1591         // Catch the events.
1592         $sink = $this->redirectEvents();
1594         // Create the course with an id number which is used later when generating a course via the imsenterprise plugin.
1595         $data = new stdClass();
1596         $data->idnumber = 'idnumber';
1597         $course = $this->getDataGenerator()->create_course($data);
1598         // Get course from DB for comparison.
1599         $course = $DB->get_record('course', array('id' => $course->id));
1601         // Capture the event.
1602         $events = $sink->get_events();
1603         $sink->close();
1605         // Validate the event.
1606         $event = $events[0];
1607         $this->assertInstanceOf('\core\event\course_created', $event);
1608         $this->assertEquals('course', $event->objecttable);
1609         $this->assertEquals($course->id, $event->objectid);
1610         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1611         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1612         $this->assertEquals('course_created', $event->get_legacy_eventname());
1613         $this->assertEventLegacyData($course, $event);
1614         $expectedlog = array(SITEID, 'course', 'new', 'view.php?id=' . $course->id, $course->fullname . ' (ID ' . $course->id . ')');
1615         $this->assertEventLegacyLogData($expectedlog, $event);
1617         // Now we want to trigger creating a course via the imsenterprise.
1618         // Delete the course we created earlier, as we want the imsenterprise plugin to create this.
1619         // We do not want print out any of the text this function generates while doing this, which is why
1620         // we are using ob_start() and ob_end_clean().
1621         ob_start();
1622         delete_course($course);
1623         ob_end_clean();
1625         // Create the XML file we want to use.
1626         $imstestcase = new enrol_imsenterprise_testcase();
1627         $imstestcase->imsplugin = enrol_get_plugin('imsenterprise');
1628         $imstestcase->set_test_config();
1629         $imstestcase->set_xml_file(false, array($course));
1631         // Capture the event.
1632         $sink = $this->redirectEvents();
1633         $imstestcase->imsplugin->cron();
1634         $events = $sink->get_events();
1635         $sink->close();
1636         $event = $events[0];
1638         // Validate the event triggered is \core\event\course_created. There is no need to validate the other values
1639         // as they have already been validated in the previous steps. Here we only want to make sure that when the
1640         // imsenterprise plugin creates a course an event is triggered.
1641         $this->assertInstanceOf('\core\event\course_created', $event);
1642         $this->assertEventContextNotUsed($event);
1643     }
1645     /**
1646      * Test that triggering a course_updated event works as expected.
1647      */
1648     public function test_course_updated_event() {
1649         global $DB;
1651         $this->resetAfterTest();
1653         // Create a course.
1654         $course = $this->getDataGenerator()->create_course();
1656         // Create a category we are going to move this course to.
1657         $category = $this->getDataGenerator()->create_category();
1659         // Create a hidden category we are going to move this course to.
1660         $categoryhidden = $this->getDataGenerator()->create_category(array('visible' => 0));
1662         // Update course and catch course_updated event.
1663         $sink = $this->redirectEvents();
1664         update_course($course);
1665         $events = $sink->get_events();
1666         $sink->close();
1668         // Get updated course information from the DB.
1669         $updatedcourse = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1670         // Validate event.
1671         $event = array_shift($events);
1672         $this->assertInstanceOf('\core\event\course_updated', $event);
1673         $this->assertEquals('course', $event->objecttable);
1674         $this->assertEquals($updatedcourse->id, $event->objectid);
1675         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1676         $url = new moodle_url('/course/edit.php', array('id' => $event->objectid));
1677         $this->assertEquals($url, $event->get_url());
1678         $this->assertEquals($updatedcourse, $event->get_record_snapshot('course', $event->objectid));
1679         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1680         $this->assertEventLegacyData($updatedcourse, $event);
1681         $expectedlog = array($updatedcourse->id, 'course', 'update', 'edit.php?id=' . $course->id, $course->id);
1682         $this->assertEventLegacyLogData($expectedlog, $event);
1684         // Move course and catch course_updated event.
1685         $sink = $this->redirectEvents();
1686         move_courses(array($course->id), $category->id);
1687         $events = $sink->get_events();
1688         $sink->close();
1690         // Return the moved course information from the DB.
1691         $movedcourse = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1692         // Validate event.
1693         $event = array_shift($events);
1694         $this->assertInstanceOf('\core\event\course_updated', $event);
1695         $this->assertEquals('course', $event->objecttable);
1696         $this->assertEquals($movedcourse->id, $event->objectid);
1697         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1698         $this->assertEquals($movedcourse, $event->get_record_snapshot('course', $movedcourse->id));
1699         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1700         $this->assertEventLegacyData($movedcourse, $event);
1701         $expectedlog = array($movedcourse->id, 'course', 'move', 'edit.php?id=' . $movedcourse->id, $movedcourse->id);
1702         $this->assertEventLegacyLogData($expectedlog, $event);
1704         // Move course to hidden category and catch course_updated event.
1705         $sink = $this->redirectEvents();
1706         move_courses(array($course->id), $categoryhidden->id);
1707         $events = $sink->get_events();
1708         $sink->close();
1710         // Return the moved course information from the DB.
1711         $movedcoursehidden = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1712         // Validate event.
1713         $event = array_shift($events);
1714         $this->assertInstanceOf('\core\event\course_updated', $event);
1715         $this->assertEquals('course', $event->objecttable);
1716         $this->assertEquals($movedcoursehidden->id, $event->objectid);
1717         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1718         $this->assertEquals($movedcoursehidden, $event->get_record_snapshot('course', $movedcoursehidden->id));
1719         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1720         $this->assertEventLegacyData($movedcoursehidden, $event);
1721         $expectedlog = array($movedcoursehidden->id, 'course', 'move', 'edit.php?id=' . $movedcoursehidden->id, $movedcoursehidden->id);
1722         $this->assertEventLegacyLogData($expectedlog, $event);
1723         $this->assertEventContextNotUsed($event);
1724     }
1726     /**
1727      * Test that triggering a course_deleted event works as expected.
1728      */
1729     public function test_course_deleted_event() {
1730         $this->resetAfterTest();
1732         // Create the course.
1733         $course = $this->getDataGenerator()->create_course();
1735         // Save the course context before we delete the course.
1736         $coursecontext = context_course::instance($course->id);
1738         // Catch the update event.
1739         $sink = $this->redirectEvents();
1741         // Call delete_course() which will trigger the course_deleted event and the course_content_deleted
1742         // event. This function prints out data to the screen, which we do not want during a PHPUnit test,
1743         // so use ob_start and ob_end_clean to prevent this.
1744         ob_start();
1745         delete_course($course);
1746         ob_end_clean();
1748         // Capture the event.
1749         $events = $sink->get_events();
1750         $sink->close();
1752         // Validate the event.
1753         $event = array_pop($events);
1754         $this->assertInstanceOf('\core\event\course_deleted', $event);
1755         $this->assertEquals('course', $event->objecttable);
1756         $this->assertEquals($course->id, $event->objectid);
1757         $this->assertEquals($coursecontext->id, $event->contextid);
1758         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1759         $this->assertEquals('course_deleted', $event->get_legacy_eventname());
1760         $eventdata = $event->get_data();
1761         $this->assertSame($course->idnumber, $eventdata['other']['idnumber']);
1762         $this->assertSame($course->fullname, $eventdata['other']['fullname']);
1763         $this->assertSame($course->shortname, $eventdata['other']['shortname']);
1765         // The legacy data also passed the context in the course object and substitutes timemodified with the current date.
1766         $expectedlegacy = clone($course);
1767         $expectedlegacy->context = $coursecontext;
1768         $expectedlegacy->timemodified = $event->timecreated;
1769         $this->assertEventLegacyData($expectedlegacy, $event);
1771         // Validate legacy log data.
1772         $expectedlog = array(SITEID, 'course', 'delete', 'view.php?id=' . $course->id, $course->fullname . '(ID ' . $course->id . ')');
1773         $this->assertEventLegacyLogData($expectedlog, $event);
1774         $this->assertEventContextNotUsed($event);
1775     }
1777     /**
1778      * Test that triggering a course_content_deleted event works as expected.
1779      */
1780     public function test_course_content_deleted_event() {
1781         global $DB;
1783         $this->resetAfterTest();
1785         // Create the course.
1786         $course = $this->getDataGenerator()->create_course();
1788         // Get the course from the DB. The data generator adds some extra properties, such as
1789         // numsections, to the course object which will fail the assertions later on.
1790         $course = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1792         // Save the course context before we delete the course.
1793         $coursecontext = context_course::instance($course->id);
1795         // Catch the update event.
1796         $sink = $this->redirectEvents();
1798         remove_course_contents($course->id, false);
1800         // Capture the event.
1801         $events = $sink->get_events();
1802         $sink->close();
1804         // Validate the event.
1805         $event = array_pop($events);
1806         $this->assertInstanceOf('\core\event\course_content_deleted', $event);
1807         $this->assertEquals('course', $event->objecttable);
1808         $this->assertEquals($course->id, $event->objectid);
1809         $this->assertEquals($coursecontext->id, $event->contextid);
1810         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1811         $this->assertEquals('course_content_removed', $event->get_legacy_eventname());
1812         // The legacy data also passed the context and options in the course object.
1813         $course->context = $coursecontext;
1814         $course->options = array();
1815         $this->assertEventLegacyData($course, $event);
1816         $this->assertEventContextNotUsed($event);
1817     }
1819     /**
1820      * Test that triggering a course_category_deleted event works as expected.
1821      */
1822     public function test_course_category_deleted_event() {
1823         $this->resetAfterTest();
1825         // Create a category.
1826         $category = $this->getDataGenerator()->create_category();
1828         // Save the context before it is deleted.
1829         $categorycontext = context_coursecat::instance($category->id);
1831         // Catch the update event.
1832         $sink = $this->redirectEvents();
1834         // Delete the category.
1835         $category->delete_full();
1837         // Capture the event.
1838         $events = $sink->get_events();
1839         $sink->close();
1841         // Validate the event.
1842         $event = $events[0];
1843         $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1844         $this->assertEquals('course_categories', $event->objecttable);
1845         $this->assertEquals($category->id, $event->objectid);
1846         $this->assertEquals($categorycontext->id, $event->contextid);
1847         $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1848         $this->assertEquals(null, $event->get_url());
1849         $this->assertEventLegacyData($category, $event);
1850         $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category->name . '(ID ' . $category->id . ')');
1851         $this->assertEventLegacyLogData($expectedlog, $event);
1853         // Create two categories.
1854         $category = $this->getDataGenerator()->create_category();
1855         $category2 = $this->getDataGenerator()->create_category();
1857         // Save the context before it is moved and then deleted.
1858         $category2context = context_coursecat::instance($category2->id);
1860         // Catch the update event.
1861         $sink = $this->redirectEvents();
1863         // Move the category.
1864         $category2->delete_move($category->id);
1866         // Capture the event.
1867         $events = $sink->get_events();
1868         $sink->close();
1870         // Validate the event.
1871         $event = $events[0];
1872         $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1873         $this->assertEquals('course_categories', $event->objecttable);
1874         $this->assertEquals($category2->id, $event->objectid);
1875         $this->assertEquals($category2context->id, $event->contextid);
1876         $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1877         $this->assertEventLegacyData($category2, $event);
1878         $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category2->name . '(ID ' . $category2->id . ')');
1879         $this->assertEventLegacyLogData($expectedlog, $event);
1880         $this->assertEventContextNotUsed($event);
1881     }
1883     /**
1884      * Test that triggering a course_restored event works as expected.
1885      */
1886     public function test_course_restored_event() {
1887         global $CFG;
1889         // Get the necessary files to perform backup and restore.
1890         require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
1891         require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
1893         $this->resetAfterTest();
1895         // Set to admin user.
1896         $this->setAdminUser();
1898         // The user id is going to be 2 since we are the admin user.
1899         $userid = 2;
1901         // Create a course.
1902         $course = $this->getDataGenerator()->create_course();
1904         // Create backup file and save it to the backup location.
1905         $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
1906             backup::INTERACTIVE_NO, backup::MODE_GENERAL, $userid);
1907         $bc->execute_plan();
1908         $results = $bc->get_results();
1909         $file = $results['backup_destination'];
1910         $fp = get_file_packer('application/vnd.moodle.backup');
1911         $filepath = $CFG->dataroot . '/temp/backup/test-restore-course-event';
1912         $file->extract_to_pathname($fp, $filepath);
1913         $bc->destroy();
1914         unset($bc);
1916         // Now we want to catch the restore course event.
1917         $sink = $this->redirectEvents();
1919         // Now restore the course to trigger the event.
1920         $rc = new restore_controller('test-restore-course-event', $course->id, backup::INTERACTIVE_NO,
1921             backup::MODE_GENERAL, $userid, backup::TARGET_NEW_COURSE);
1922         $rc->execute_precheck();
1923         $rc->execute_plan();
1925         // Capture the event.
1926         $events = $sink->get_events();
1927         $sink->close();
1929         // Validate the event.
1930         $event = array_pop($events);
1931         $this->assertInstanceOf('\core\event\course_restored', $event);
1932         $this->assertEquals('course', $event->objecttable);
1933         $this->assertEquals($rc->get_courseid(), $event->objectid);
1934         $this->assertEquals(context_course::instance($rc->get_courseid())->id, $event->contextid);
1935         $this->assertEquals('course_restored', $event->get_legacy_eventname());
1936         $legacydata = (object) array(
1937             'courseid' => $rc->get_courseid(),
1938             'userid' => $rc->get_userid(),
1939             'type' => $rc->get_type(),
1940             'target' => $rc->get_target(),
1941             'mode' => $rc->get_mode(),
1942             'operation' => $rc->get_operation(),
1943             'samesite' => $rc->is_samesite()
1944         );
1945         $url = new moodle_url('/course/view.php', array('id' => $event->objectid));
1946         $this->assertEquals($url, $event->get_url());
1947         $this->assertEventLegacyData($legacydata, $event);
1948         $this->assertEventContextNotUsed($event);
1950         // Destroy the resource controller since we are done using it.
1951         $rc->destroy();
1952         unset($rc);
1953     }
1955     /**
1956      * Test that triggering a course_section_updated event works as expected.
1957      */
1958     public function test_course_section_updated_event() {
1959         global $DB;
1961         $this->resetAfterTest();
1963         // Create the course with sections.
1964         $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true));
1965         $sections = $DB->get_records('course_sections', array('course' => $course->id));
1967         $coursecontext = context_course::instance($course->id);
1969         $section = array_pop($sections);
1970         $section->name = 'Test section';
1971         $section->summary = 'Test section summary';
1972         $DB->update_record('course_sections', $section);
1974         // Trigger an event for course section update.
1975         $event = \core\event\course_section_updated::create(
1976                 array(
1977                     'objectid' => $section->id,
1978                     'courseid' => $course->id,
1979                     'context' => context_course::instance($course->id),
1980                     'other' => array(
1981                         'sectionnum' => $section->section
1982                     )
1983                 )
1984             );
1985         $event->add_record_snapshot('course_sections', $section);
1986         // Trigger and catch event.
1987         $sink = $this->redirectEvents();
1988         $event->trigger();
1989         $events = $sink->get_events();
1990         $sink->close();
1992         // Validate the event.
1993         $event = $events[0];
1994         $this->assertInstanceOf('\core\event\course_section_updated', $event);
1995         $this->assertEquals('course_sections', $event->objecttable);
1996         $this->assertEquals($section->id, $event->objectid);
1997         $this->assertEquals($course->id, $event->courseid);
1998         $this->assertEquals($coursecontext->id, $event->contextid);
1999         $this->assertEquals($section->section, $event->other['sectionnum']);
2000         $expecteddesc = "The user with id '{$event->userid}' updated section number '{$event->other['sectionnum']}' for the course with id '{$event->courseid}'";
2001         $this->assertEquals($expecteddesc, $event->get_description());
2002         $url = new moodle_url('/course/editsection.php', array('id' => $event->objectid));
2003         $this->assertEquals($url, $event->get_url());
2004         $this->assertEquals($section, $event->get_record_snapshot('course_sections', $event->objectid));
2005         $id = $section->id;
2006         $sectionnum = $section->section;
2007         $expectedlegacydata = array($course->id, "course", "editsection", 'editsection.php?id=' . $id, $sectionnum);
2008         $this->assertEventLegacyLogData($expectedlegacydata, $event);
2009         $this->assertEventContextNotUsed($event);
2010     }
2012     /**
2013      * Test that triggering a course_section_deleted event works as expected.
2014      */
2015     public function test_course_section_deleted_event() {
2016         global $USER, $DB;
2017         $this->resetAfterTest();
2018         $sink = $this->redirectEvents();
2020         // Create the course with sections.
2021         $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true));
2022         $sections = $DB->get_records('course_sections', array('course' => $course->id));
2023         $coursecontext = context_course::instance($course->id);
2024         $section = array_pop($sections);
2025         course_delete_section($course, $section);
2026         $events = $sink->get_events();
2027         $event = array_pop($events); // Delete section event.
2028         $sink->close();
2030         // Validate event data.
2031         $this->assertInstanceOf('\core\event\course_section_deleted', $event);
2032         $this->assertEquals('course_sections', $event->objecttable);
2033         $this->assertEquals($section->id, $event->objectid);
2034         $this->assertEquals($course->id, $event->courseid);
2035         $this->assertEquals($coursecontext->id, $event->contextid);
2036         $this->assertEquals($section->section, $event->other['sectionnum']);
2037         $expecteddesc = "The user with id '{$event->userid}' deleted section number '{$event->other['sectionnum']}' " .
2038                 "(section name '{$event->other['sectionname']}') for the course with id '{$event->courseid}'";
2039         $this->assertEquals($expecteddesc, $event->get_description());
2040         $this->assertEquals($section, $event->get_record_snapshot('course_sections', $event->objectid));
2041         $this->assertNull($event->get_url());
2043         // Test legacy data.
2044         $sectionnum = $section->section;
2045         $expectedlegacydata = array($course->id, "course", "delete section", 'view.php?id=' . $course->id, $sectionnum);
2046         $this->assertEventLegacyLogData($expectedlegacydata, $event);
2047         $this->assertEventContextNotUsed($event);
2048     }
2050     public function test_course_integrity_check() {
2051         global $DB;
2053         $this->resetAfterTest(true);
2054         $course = $this->getDataGenerator()->create_course(array('numsections' => 1),
2055            array('createsections'=>true));
2057         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
2058                 array('section' => 0));
2059         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id),
2060                 array('section' => 0));
2061         $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id),
2062                 array('section' => 0));
2063         $correctseq = join(',', array($forum->cmid, $page->cmid, $quiz->cmid));
2065         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2066         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2067         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2068         $this->assertEquals($correctseq, $section0->sequence);
2069         $this->assertEmpty($section1->sequence);
2070         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2071         $this->assertEquals($section0->id, $cms[$page->cmid]->section);
2072         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2073         $this->assertEmpty(course_integrity_check($course->id));
2075         // Now let's make manual change in DB and let course_integrity_check() fix it:
2077         // 1. Module appears twice in one section.
2078         $DB->update_record('course_sections', array('id' => $section0->id, 'sequence' => $section0->sequence. ','. $page->cmid));
2079         $this->assertEquals(
2080                 array('Failed integrity check for course ['. $course->id.
2081                 ']. Sequence for course section ['. $section0->id. '] is "'.
2082                 $section0->sequence. ','. $page->cmid. '", must be "'.
2083                 $section0->sequence. '"'),
2084                 course_integrity_check($course->id));
2085         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2086         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2087         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2088         $this->assertEquals($correctseq, $section0->sequence);
2089         $this->assertEmpty($section1->sequence);
2090         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2091         $this->assertEquals($section0->id, $cms[$page->cmid]->section);
2092         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2094         // 2. Module appears in two sections (last section wins).
2095         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''. $page->cmid));
2096         // First message about double mentioning in sequence, second message about wrong section field for $page.
2097         $this->assertEquals(array(
2098             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
2099             '] must be removed from sequence of section ['. $section0->id.
2100             '] because it is also present in sequence of section ['. $section1->id. ']',
2101             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
2102             '] points to section ['. $section0->id. '] instead of ['. $section1->id. ']'),
2103                 course_integrity_check($course->id));
2104         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2105         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2106         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2107         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
2108         $this->assertEquals(''. $page->cmid, $section1->sequence);
2109         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2110         $this->assertEquals($section1->id, $cms[$page->cmid]->section);
2111         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2113         // 3. Module id is not present in course_section.sequence (integrity check with $fullcheck = false).
2114         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''));
2115         $this->assertEmpty(course_integrity_check($course->id)); // Not an error!
2116         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2117         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2118         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2119         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
2120         $this->assertEmpty($section1->sequence);
2121         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2122         $this->assertEquals($section1->id, $cms[$page->cmid]->section); // Not changed.
2123         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2125         // 4. Module id is not present in course_section.sequence (integrity check with $fullcheck = true).
2126         $this->assertEquals(array('Failed integrity check for course ['. $course->id. ']. Course module ['.
2127                 $page->cmid. '] is missing from sequence of section ['. $section1->id. ']'),
2128                 course_integrity_check($course->id, null, null, true)); // Error!
2129         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2130         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2131         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2132         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
2133         $this->assertEquals(''. $page->cmid, $section1->sequence);  // Yay, module added to section.
2134         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2135         $this->assertEquals($section1->id, $cms[$page->cmid]->section); // Not changed.
2136         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2138         // 5. Module id is not present in course_section.sequence and it's section is invalid (integrity check with $fullcheck = true).
2139         $DB->update_record('course_modules', array('id' => $page->cmid, 'section' => 8765));
2140         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''));
2141         $this->assertEquals(array(
2142             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
2143             '] is missing from sequence of section ['. $section0->id. ']',
2144             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
2145             '] points to section [8765] instead of ['. $section0->id. ']'),
2146                 course_integrity_check($course->id, null, null, true));
2147         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2148         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2149         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2150         $this->assertEquals($forum->cmid. ','. $quiz->cmid. ','. $page->cmid, $section0->sequence); // Module added to section.
2151         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2152         $this->assertEquals($section0->id, $cms[$page->cmid]->section); // Section changed to section0.
2153         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2155         // 6. Module is deleted from course_modules but not deleted in sequence (integrity check with $fullcheck = true).
2156         $DB->delete_records('course_modules', array('id' => $page->cmid));
2157         $this->assertEquals(array('Failed integrity check for course ['. $course->id. ']. Course module ['.
2158                 $page->cmid. '] does not exist but is present in the sequence of section ['. $section0->id. ']'),
2159                 course_integrity_check($course->id, null, null, true));
2160         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2161         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2162         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2163         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
2164         $this->assertEmpty($section1->sequence);
2165         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2166         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2167         $this->assertEquals(2, count($cms));
2168     }
2170     /**
2171      * Tests for event related to course module creation.
2172      */
2173     public function test_course_module_created_event() {
2174         global $USER, $DB;
2175         $this->resetAfterTest();
2177         // Create an assign module.
2178         $sink = $this->redirectEvents();
2179         $modinfo = $this->create_specific_module_test('assign');
2180         $events = $sink->get_events();
2181         $event = array_pop($events);
2183         $cm = get_coursemodule_from_id('assign', $modinfo->coursemodule, 0, false, MUST_EXIST);
2184         $mod = $DB->get_record('assign', array('id' => $modinfo->instance), '*', MUST_EXIST);
2186         // Validate event data.
2187         $this->assertInstanceOf('\core\event\course_module_created', $event);
2188         $this->assertEquals($cm->id, $event->objectid);
2189         $this->assertEquals($USER->id, $event->userid);
2190         $this->assertEquals('course_modules', $event->objecttable);
2191         $url = new moodle_url('/mod/assign/view.php', array('id' => $cm->id));
2192         $this->assertEquals($url, $event->get_url());
2194         // Test legacy data.
2195         $this->assertSame('mod_created', $event->get_legacy_eventname());
2196         $eventdata = new stdClass();
2197         $eventdata->modulename = 'assign';
2198         $eventdata->name       = $mod->name;
2199         $eventdata->cmid       = $cm->id;
2200         $eventdata->courseid   = $cm->course;
2201         $eventdata->userid     = $USER->id;
2202         $this->assertEventLegacyData($eventdata, $event);
2204         $arr = array(
2205             array($cm->course, "course", "add mod", "../mod/assign/view.php?id=$cm->id", "assign $cm->instance"),
2206             array($cm->course, "assign", "add", "view.php?id=$cm->id", $cm->instance, $cm->id)
2207         );
2208         $this->assertEventLegacyLogData($arr, $event);
2209         $this->assertEventContextNotUsed($event);
2211         // Let us see if duplicating an activity results in a nice course module created event.
2212         $sink->clear();
2213         $course = get_course($mod->course);
2214         $newcm = duplicate_module($course, $cm);
2215         $events = $sink->get_events();
2216         $event = array_pop($events);
2217         $sink->close();
2219         // Validate event data.
2220         $this->assertInstanceOf('\core\event\course_module_created', $event);
2221         $this->assertEquals($newcm->id, $event->objectid);
2222         $this->assertEquals($USER->id, $event->userid);
2223         $this->assertEquals($course->id, $event->courseid);
2224         $url = new moodle_url('/mod/assign/view.php', array('id' => $newcm->id));
2225         $this->assertEquals($url, $event->get_url());
2226     }
2228     /**
2229      * Tests for event validations related to course module creation.
2230      */
2231     public function test_course_module_created_event_exceptions() {
2233         $this->resetAfterTest();
2235         // Generate data.
2236         $modinfo = $this->create_specific_module_test('assign');
2237         $context = context_module::instance($modinfo->coursemodule);
2239         // Test not setting instanceid.
2240         try {
2241             $event = \core\event\course_module_created::create(array(
2242                 'courseid' => $modinfo->course,
2243                 'context'  => $context,
2244                 'objectid' => $modinfo->coursemodule,
2245                 'other'    => array(
2246                     'modulename' => 'assign',
2247                     'name'       => 'My assignment',
2248                 )
2249             ));
2250             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2251                     other['instanceid']");
2252         } catch (coding_exception $e) {
2253             $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
2254         }
2256         // Test not setting modulename.
2257         try {
2258             $event = \core\event\course_module_created::create(array(
2259                 'courseid' => $modinfo->course,
2260                 'context'  => $context,
2261                 'objectid' => $modinfo->coursemodule,
2262                 'other'    => array(
2263                     'instanceid' => $modinfo->instance,
2264                     'name'       => 'My assignment',
2265                 )
2266             ));
2267             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2268                     other['modulename']");
2269         } catch (coding_exception $e) {
2270             $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
2271         }
2273         // Test not setting name.
2275         try {
2276             $event = \core\event\course_module_created::create(array(
2277                 'courseid' => $modinfo->course,
2278                 'context'  => $context,
2279                 'objectid' => $modinfo->coursemodule,
2280                 'other'    => array(
2281                     'modulename' => 'assign',
2282                     'instanceid' => $modinfo->instance,
2283                 )
2284             ));
2285             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2286                     other['name']");
2287         } catch (coding_exception $e) {
2288             $this->assertContains("The 'name' value must be set in other.", $e->getMessage());
2289         }
2291     }
2293     /**
2294      * Tests for event related to course module updates.
2295      */
2296     public function test_course_module_updated_event() {
2297         global $USER, $DB;
2298         $this->resetAfterTest();
2300         // Update a forum module.
2301         $sink = $this->redirectEvents();
2302         $modinfo = $this->update_specific_module_test('forum');
2303         $events = $sink->get_events();
2304         $event = array_pop($events);
2305         $sink->close();
2307         $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
2308         $mod = $DB->get_record('forum', array('id' => $cm->instance), '*', MUST_EXIST);
2310         // Validate event data.
2311         $this->assertInstanceOf('\core\event\course_module_updated', $event);
2312         $this->assertEquals($cm->id, $event->objectid);
2313         $this->assertEquals($USER->id, $event->userid);
2314         $this->assertEquals('course_modules', $event->objecttable);
2315         $url = new moodle_url('/mod/forum/view.php', array('id' => $cm->id));
2316         $this->assertEquals($url, $event->get_url());
2318         // Test legacy data.
2319         $this->assertSame('mod_updated', $event->get_legacy_eventname());
2320         $eventdata = new stdClass();
2321         $eventdata->modulename = 'forum';
2322         $eventdata->name       = $mod->name;
2323         $eventdata->cmid       = $cm->id;
2324         $eventdata->courseid   = $cm->course;
2325         $eventdata->userid     = $USER->id;
2326         $this->assertEventLegacyData($eventdata, $event);
2328         $arr = array(
2329             array($cm->course, "course", "update mod", "../mod/forum/view.php?id=$cm->id", "forum $cm->instance"),
2330             array($cm->course, "forum", "update", "view.php?id=$cm->id", $cm->instance, $cm->id)
2331         );
2332         $this->assertEventLegacyLogData($arr, $event);
2333         $this->assertEventContextNotUsed($event);
2334     }
2336     /**
2337      * Tests for create_from_cm method.
2338      */
2339     public function test_course_module_create_from_cm() {
2340         $this->resetAfterTest();
2341         $this->setAdminUser();
2343         // Create course and modules.
2344         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
2346         // Generate an assignment.
2347         $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
2349         // Get the module context.
2350         $modcontext = context_module::instance($assign->cmid);
2352         // Get course module.
2353         $cm = get_coursemodule_from_id(null, $assign->cmid, $course->id, false, MUST_EXIST);
2355         // Create an event from course module.
2356         $event = \core\event\course_module_updated::create_from_cm($cm, $modcontext);
2358         // Trigger the events.
2359         $sink = $this->redirectEvents();
2360         $event->trigger();
2361         $events = $sink->get_events();
2362         $event2 = array_pop($events);
2364         // Test event data.
2365         $this->assertInstanceOf('\core\event\course_module_updated', $event);
2366         $this->assertEquals($cm->id, $event2->objectid);
2367         $this->assertEquals($modcontext, $event2->get_context());
2368         $this->assertEquals($cm->modname, $event2->other['modulename']);
2369         $this->assertEquals($cm->instance, $event2->other['instanceid']);
2370         $this->assertEquals($cm->name, $event2->other['name']);
2371         $this->assertEventContextNotUsed($event2);
2372         $this->assertSame('mod_updated', $event2->get_legacy_eventname());
2373         $arr = array(
2374             array($cm->course, "course", "update mod", "../mod/assign/view.php?id=$cm->id", "assign $cm->instance"),
2375             array($cm->course, "assign", "update", "view.php?id=$cm->id", $cm->instance, $cm->id)
2376         );
2377         $this->assertEventLegacyLogData($arr, $event);
2378     }
2380     /**
2381      * Tests for event validations related to course module update.
2382      */
2383     public function test_course_module_updated_event_exceptions() {
2385         $this->resetAfterTest();
2387         // Generate data.
2388         $modinfo = $this->create_specific_module_test('assign');
2389         $context = context_module::instance($modinfo->coursemodule);
2391         // Test not setting instanceid.
2392         try {
2393             $event = \core\event\course_module_updated::create(array(
2394                 'courseid' => $modinfo->course,
2395                 'context'  => $context,
2396                 'objectid' => $modinfo->coursemodule,
2397                 'other'    => array(
2398                     'modulename' => 'assign',
2399                     'name'       => 'My assignment',
2400                 )
2401             ));
2402             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2403                     other['instanceid']");
2404         } catch (coding_exception $e) {
2405             $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
2406         }
2408         // Test not setting modulename.
2409         try {
2410             $event = \core\event\course_module_updated::create(array(
2411                 'courseid' => $modinfo->course,
2412                 'context'  => $context,
2413                 'objectid' => $modinfo->coursemodule,
2414                 'other'    => array(
2415                     'instanceid' => $modinfo->instance,
2416                     'name'       => 'My assignment',
2417                 )
2418             ));
2419             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2420                     other['modulename']");
2421         } catch (coding_exception $e) {
2422             $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
2423         }
2425         // Test not setting name.
2427         try {
2428             $event = \core\event\course_module_updated::create(array(
2429                 'courseid' => $modinfo->course,
2430                 'context'  => $context,
2431                 'objectid' => $modinfo->coursemodule,
2432                 'other'    => array(
2433                     'modulename' => 'assign',
2434                     'instanceid' => $modinfo->instance,
2435                 )
2436             ));
2437             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2438                     other['name']");
2439         } catch (coding_exception $e) {
2440             $this->assertContains("The 'name' value must be set in other.", $e->getMessage());
2441         }
2443     }
2445     /**
2446      * Tests for event related to course module delete.
2447      */
2448     public function test_course_module_deleted_event() {
2449         global $USER, $DB;
2450         $this->resetAfterTest();
2452         // Create and delete a module.
2453         $sink = $this->redirectEvents();
2454         $modinfo = $this->create_specific_module_test('forum');
2455         $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
2456         course_delete_module($modinfo->coursemodule);
2457         $events = $sink->get_events();
2458         $event = array_pop($events); // delete module event.;
2459         $sink->close();
2461         // Validate event data.
2462         $this->assertInstanceOf('\core\event\course_module_deleted', $event);
2463         $this->assertEquals($cm->id, $event->objectid);
2464         $this->assertEquals($USER->id, $event->userid);
2465         $this->assertEquals('course_modules', $event->objecttable);
2466         $this->assertEquals(null, $event->get_url());
2467         $this->assertEquals($cm, $event->get_record_snapshot('course_modules', $cm->id));
2469         // Test legacy data.
2470         $this->assertSame('mod_deleted', $event->get_legacy_eventname());
2471         $eventdata = new stdClass();
2472         $eventdata->modulename = 'forum';
2473         $eventdata->cmid       = $cm->id;
2474         $eventdata->courseid   = $cm->course;
2475         $eventdata->userid     = $USER->id;
2476         $this->assertEventLegacyData($eventdata, $event);
2478         $arr = array($cm->course, 'course', "delete mod", "view.php?id=$cm->course", "forum $cm->instance", $cm->id);
2479         $this->assertEventLegacyLogData($arr, $event);
2481     }
2483     /**
2484      * Tests for event validations related to course module deletion.
2485      */
2486     public function test_course_module_deleted_event_exceptions() {
2488         $this->resetAfterTest();
2490         // Generate data.
2491         $modinfo = $this->create_specific_module_test('assign');
2492         $context = context_module::instance($modinfo->coursemodule);
2494         // Test not setting instanceid.
2495         try {
2496             $event = \core\event\course_module_deleted::create(array(
2497                 'courseid' => $modinfo->course,
2498                 'context'  => $context,
2499                 'objectid' => $modinfo->coursemodule,
2500                 'other'    => array(
2501                     'modulename' => 'assign',
2502                     'name'       => 'My assignment',
2503                 )
2504             ));
2505             $this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
2506                     other['instanceid']");
2507         } catch (coding_exception $e) {
2508             $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
2509         }
2511         // Test not setting modulename.
2512         try {
2513             $event = \core\event\course_module_deleted::create(array(
2514                 'courseid' => $modinfo->course,
2515                 'context'  => $context,
2516                 'objectid' => $modinfo->coursemodule,
2517                 'other'    => array(
2518                     'instanceid' => $modinfo->instance,
2519                     'name'       => 'My assignment',
2520                 )
2521             ));
2522             $this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
2523                     other['modulename']");
2524         } catch (coding_exception $e) {
2525             $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
2526         }
2527     }
2529     /**
2530      * Returns a user object and its assigned new role.
2531      *
2532      * @param testing_data_generator $generator
2533      * @param $contextid
2534      * @return array The user object and the role ID
2535      */
2536     protected function get_user_objects(testing_data_generator $generator, $contextid) {
2537         global $USER;
2539         if (empty($USER->id)) {
2540             $user  = $generator->create_user();
2541             $this->setUser($user);
2542         }
2543         $roleid = create_role('Test role', 'testrole', 'Test role description');
2544         if (!is_array($contextid)) {
2545             $contextid = array($contextid);
2546         }
2547         foreach ($contextid as $cid) {
2548             $assignid = role_assign($roleid, $user->id, $cid);
2549         }
2550         return array($user, $roleid);
2551     }
2553     /**
2554      * Test course move after course.
2555      */
2556     public function test_course_change_sortorder_after_course() {
2557         global $DB;
2559         $this->resetAfterTest(true);
2561         $generator = $this->getDataGenerator();
2562         $category = $generator->create_category();
2563         $course3 = $generator->create_course(array('category' => $category->id));
2564         $course2 = $generator->create_course(array('category' => $category->id));
2565         $course1 = $generator->create_course(array('category' => $category->id));
2566         $context = $category->get_context();
2568         list($user, $roleid) = $this->get_user_objects($generator, $context->id);
2569         $caps = course_capability_assignment::allow('moodle/category:manage', $roleid, $context->id);
2571         $courses = $category->get_courses();
2572         $this->assertInternalType('array', $courses);
2573         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2574         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2575         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2577         // Test moving down.
2578         $this->assertTrue(course_change_sortorder_after_course($course1->id, $course3->id));
2579         $courses = $category->get_courses();
2580         $this->assertInternalType('array', $courses);
2581         $this->assertEquals(array($course2->id, $course3->id, $course1->id), array_keys($courses));
2582         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2583         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2585         // Test moving up.
2586         $this->assertTrue(course_change_sortorder_after_course($course1->id, $course2->id));
2587         $courses = $category->get_courses();
2588         $this->assertInternalType('array', $courses);
2589         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
2590         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2591         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2593         // Test moving to the top.
2594         $this->assertTrue(course_change_sortorder_after_course($course1->id, 0));
2595         $courses = $category->get_courses();
2596         $this->assertInternalType('array', $courses);
2597         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2598         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2599         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2600     }
2602     /**
2603      * Tests changing the visibility of a course.
2604      */
2605     public function test_course_change_visibility() {
2606         global $DB;
2608         $this->resetAfterTest(true);
2610         $generator = $this->getDataGenerator();
2611         $category = $generator->create_category();
2612         $course = $generator->create_course(array('category' => $category->id));
2614         $this->assertEquals('1', $course->visible);
2615         $this->assertEquals('1', $course->visibleold);
2617         $this->assertTrue(course_change_visibility($course->id, false));
2618         $course = $DB->get_record('course', array('id' => $course->id));
2619         $this->assertEquals('0', $course->visible);
2620         $this->assertEquals('0', $course->visibleold);
2622         $this->assertTrue(course_change_visibility($course->id, true));
2623         $course = $DB->get_record('course', array('id' => $course->id));
2624         $this->assertEquals('1', $course->visible);
2625         $this->assertEquals('1', $course->visibleold);
2626     }
2628     /**
2629      * Tests moving the course up and down by one.
2630      */
2631     public function test_course_change_sortorder_by_one() {
2632         global $DB;
2634         $this->resetAfterTest(true);
2636         $generator = $this->getDataGenerator();
2637         $category = $generator->create_category();
2638         $course3 = $generator->create_course(array('category' => $category->id));
2639         $course2 = $generator->create_course(array('category' => $category->id));
2640         $course1 = $generator->create_course(array('category' => $category->id));
2642         $courses = $category->get_courses();
2643         $this->assertInternalType('array', $courses);
2644         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2645         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2646         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2648         // Test moving down.
2649         $course1 = get_course($course1->id);
2650         $this->assertTrue(course_change_sortorder_by_one($course1, false));
2651         $courses = $category->get_courses();
2652         $this->assertInternalType('array', $courses);
2653         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
2654         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2655         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2657         // Test moving up.
2658         $course1 = get_course($course1->id);
2659         $this->assertTrue(course_change_sortorder_by_one($course1, true));
2660         $courses = $category->get_courses();
2661         $this->assertInternalType('array', $courses);
2662         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2663         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2664         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2666         // Test moving the top course up one.
2667         $course1 = get_course($course1->id);
2668         $this->assertFalse(course_change_sortorder_by_one($course1, true));
2669         // Check nothing changed.
2670         $courses = $category->get_courses();
2671         $this->assertInternalType('array', $courses);
2672         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2673         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2674         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2676         // Test moving the bottom course up down.
2677         $course3 = get_course($course3->id);
2678         $this->assertFalse(course_change_sortorder_by_one($course3, false));
2679         // Check nothing changed.
2680         $courses = $category->get_courses();
2681         $this->assertInternalType('array', $courses);
2682         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2683         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2684         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2685     }
2687     public function test_view_resources_list() {
2688         $this->resetAfterTest();
2690         $course = self::getDataGenerator()->create_course();
2691         $coursecontext = context_course::instance($course->id);
2693         $event = \core\event\course_resources_list_viewed::create(array('context' => context_course::instance($course->id)));
2694         $event->set_legacy_logdata(array('book', 'page', 'resource'));
2695         $sink = $this->redirectEvents();
2696         $event->trigger();
2697         $events = $sink->get_events();
2698         $sink->close();
2700         // Validate the event.
2701         $event = $events[0];
2702         $this->assertInstanceOf('\core\event\course_resources_list_viewed', $event);
2703         $this->assertEquals(null, $event->objecttable);
2704         $this->assertEquals(null, $event->objectid);
2705         $this->assertEquals($course->id, $event->courseid);
2706         $this->assertEquals($coursecontext->id, $event->contextid);
2707         $expectedlegacydata = array(
2708             array($course->id, "book", "view all", 'index.php?id=' . $course->id, ''),
2709             array($course->id, "page", "view all", 'index.php?id=' . $course->id, ''),
2710             array($course->id, "resource", "view all", 'index.php?id=' . $course->id, ''),
2711         );
2712         $this->assertEventLegacyLogData($expectedlegacydata, $event);
2713         $this->assertEventContextNotUsed($event);
2714     }
2716     /**
2717      * Test duplicate_module()
2718      */
2719     public function test_duplicate_module() {
2720         $this->setAdminUser();
2721         $this->resetAfterTest();
2722         $course = self::getDataGenerator()->create_course();
2723         $res = self::getDataGenerator()->create_module('resource', array('course' => $course));
2724         $cm = get_coursemodule_from_id('resource', $res->cmid, 0, false, MUST_EXIST);
2726         $newcm = duplicate_module($course, $cm);
2728         // Make sure they are the same, except obvious id changes.
2729         foreach ($cm as $prop => $value) {
2730             if ($prop == 'id' || $prop == 'url' || $prop == 'instance' || $prop == 'added') {
2731                 // Ignore obviously different properties.
2732                 continue;
2733             }
2734             $this->assertEquals($value, $newcm->$prop);
2735         }
2736     }
2738     /**
2739      * Tests that when creating or updating a module, if the availability settings
2740      * are present but set to an empty tree, availability is set to null in
2741      * database.
2742      */
2743     public function test_empty_availability_settings() {
2744         global $DB;
2745         $this->setAdminUser();
2746         $this->resetAfterTest();
2748         // Enable availability.
2749         set_config('enableavailability', 1);
2751         // Test add.
2752         $emptyavailability = json_encode(\core_availability\tree::get_root_json(array()));
2753         $course = self::getDataGenerator()->create_course();
2754         $label = self::getDataGenerator()->create_module('label', array(
2755                 'course' => $course, 'availability' => $emptyavailability));
2756         $this->assertNull($DB->get_field('course_modules', 'availability',
2757                 array('id' => $label->cmid)));
2759         // Test update.
2760         $formdata = $DB->get_record('course_modules', array('id' => $label->cmid));
2761         unset($formdata->availability);
2762         $formdata->availabilityconditionsjson = $emptyavailability;
2763         $formdata->modulename = 'label';
2764         $formdata->coursemodule = $label->cmid;
2765         $draftid = 0;
2766         file_prepare_draft_area($draftid, context_module::instance($label->cmid)->id,
2767                 'mod_label', 'intro', 0);
2768         $formdata->introeditor = array(
2769             'itemid' => $draftid,
2770             'text' => '<p>Yo</p>',
2771             'format' => FORMAT_HTML);
2772         update_module($formdata);
2773         $this->assertNull($DB->get_field('course_modules', 'availability',
2774                 array('id' => $label->cmid)));
2775     }