MDL-59287 core_completion: Update to unit tests.
[moodle.git] / course / tests / courselib_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * Course related unit tests
19  *
20  * @package    core
21  * @category   phpunit
22  * @copyright  2012 Petr Skoda {@link http://skodak.org}
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->dirroot . '/course/lib.php');
30 require_once($CFG->dirroot . '/course/tests/fixtures/course_capability_assignment.php');
31 require_once($CFG->dirroot . '/enrol/imsenterprise/tests/imsenterprise_test.php');
33 class core_course_courselib_testcase extends advanced_testcase {
35     /**
36      * Set forum specific test values for calling create_module().
37      *
38      * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
39      */
40     private function forum_create_set_values(&$moduleinfo) {
41         // Completion specific to forum - optional.
42         $moduleinfo->completionposts = 3;
43         $moduleinfo->completiondiscussions = 1;
44         $moduleinfo->completionreplies = 2;
46         // Specific values to the Forum module.
47         $moduleinfo->forcesubscribe = FORUM_INITIALSUBSCRIBE;
48         $moduleinfo->type = 'single';
49         $moduleinfo->trackingtype = FORUM_TRACKING_FORCED;
50         $moduleinfo->maxbytes = 10240;
51         $moduleinfo->maxattachments = 2;
53         // Post threshold for blocking - specific to forum.
54         $moduleinfo->blockperiod = 60*60*24;
55         $moduleinfo->blockafter = 10;
56         $moduleinfo->warnafter = 5;
57     }
59     /**
60      * Execute test asserts on the saved DB data by create_module($forum).
61      *
62      * @param object $moduleinfo - the specific forum values that were used to create a forum.
63      * @param object $dbmodinstance - the DB values of the created forum.
64      */
65     private function forum_create_run_asserts($moduleinfo, $dbmodinstance) {
66         // Compare values specific to forums.
67         $this->assertEquals($moduleinfo->forcesubscribe, $dbmodinstance->forcesubscribe);
68         $this->assertEquals($moduleinfo->type, $dbmodinstance->type);
69         $this->assertEquals($moduleinfo->assessed, $dbmodinstance->assessed);
70         $this->assertEquals($moduleinfo->completionposts, $dbmodinstance->completionposts);
71         $this->assertEquals($moduleinfo->completiondiscussions, $dbmodinstance->completiondiscussions);
72         $this->assertEquals($moduleinfo->completionreplies, $dbmodinstance->completionreplies);
73         $this->assertEquals($moduleinfo->scale, $dbmodinstance->scale);
74         $this->assertEquals($moduleinfo->assesstimestart, $dbmodinstance->assesstimestart);
75         $this->assertEquals($moduleinfo->assesstimefinish, $dbmodinstance->assesstimefinish);
76         $this->assertEquals($moduleinfo->rsstype, $dbmodinstance->rsstype);
77         $this->assertEquals($moduleinfo->rssarticles, $dbmodinstance->rssarticles);
78         $this->assertEquals($moduleinfo->trackingtype, $dbmodinstance->trackingtype);
79         $this->assertEquals($moduleinfo->maxbytes, $dbmodinstance->maxbytes);
80         $this->assertEquals($moduleinfo->maxattachments, $dbmodinstance->maxattachments);
81         $this->assertEquals($moduleinfo->blockperiod, $dbmodinstance->blockperiod);
82         $this->assertEquals($moduleinfo->blockafter, $dbmodinstance->blockafter);
83         $this->assertEquals($moduleinfo->warnafter, $dbmodinstance->warnafter);
84     }
86     /**
87      * Set assign module specific test values for calling create_module().
88      *
89      * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
90      */
91     private function assign_create_set_values(&$moduleinfo) {
92         // Specific values to the Assign module.
93         $moduleinfo->alwaysshowdescription = true;
94         $moduleinfo->submissiondrafts = true;
95         $moduleinfo->requiresubmissionstatement = true;
96         $moduleinfo->sendnotifications = true;
97         $moduleinfo->sendlatenotifications = true;
98         $moduleinfo->duedate = time() + (7 * 24 * 3600);
99         $moduleinfo->cutoffdate = time() + (7 * 24 * 3600);
100         $moduleinfo->gradingduedate = time() + (7 * 24 * 3600);
101         $moduleinfo->allowsubmissionsfromdate = time();
102         $moduleinfo->teamsubmission = true;
103         $moduleinfo->requireallteammemberssubmit = true;
104         $moduleinfo->teamsubmissiongroupingid = true;
105         $moduleinfo->blindmarking = true;
106         $moduleinfo->markingworkflow = true;
107         $moduleinfo->markingallocation = true;
108         $moduleinfo->assignsubmission_onlinetext_enabled = true;
109         $moduleinfo->assignsubmission_file_enabled = true;
110         $moduleinfo->assignsubmission_file_maxfiles = 1;
111         $moduleinfo->assignsubmission_file_maxsizebytes = 1000000;
112         $moduleinfo->assignsubmission_comments_enabled = true;
113         $moduleinfo->assignfeedback_comments_enabled = true;
114         $moduleinfo->assignfeedback_offline_enabled = true;
115         $moduleinfo->assignfeedback_file_enabled = true;
117         // Advanced grading.
118         $gradingmethods = grading_manager::available_methods();
119         $moduleinfo->advancedgradingmethod_submissions = current(array_keys($gradingmethods));
120     }
122     /**
123      * Execute test asserts on the saved DB data by create_module($assign).
124      *
125      * @param object $moduleinfo - the specific assign module values that were used to create an assign module.
126      * @param object $dbmodinstance - the DB values of the created assign module.
127      */
128     private function assign_create_run_asserts($moduleinfo, $dbmodinstance) {
129         global $DB;
131         $this->assertEquals($moduleinfo->alwaysshowdescription, $dbmodinstance->alwaysshowdescription);
132         $this->assertEquals($moduleinfo->submissiondrafts, $dbmodinstance->submissiondrafts);
133         $this->assertEquals($moduleinfo->requiresubmissionstatement, $dbmodinstance->requiresubmissionstatement);
134         $this->assertEquals($moduleinfo->sendnotifications, $dbmodinstance->sendnotifications);
135         $this->assertEquals($moduleinfo->duedate, $dbmodinstance->duedate);
136         $this->assertEquals($moduleinfo->cutoffdate, $dbmodinstance->cutoffdate);
137         $this->assertEquals($moduleinfo->allowsubmissionsfromdate, $dbmodinstance->allowsubmissionsfromdate);
138         $this->assertEquals($moduleinfo->teamsubmission, $dbmodinstance->teamsubmission);
139         $this->assertEquals($moduleinfo->requireallteammemberssubmit, $dbmodinstance->requireallteammemberssubmit);
140         $this->assertEquals($moduleinfo->teamsubmissiongroupingid, $dbmodinstance->teamsubmissiongroupingid);
141         $this->assertEquals($moduleinfo->blindmarking, $dbmodinstance->blindmarking);
142         $this->assertEquals($moduleinfo->markingworkflow, $dbmodinstance->markingworkflow);
143         $this->assertEquals($moduleinfo->markingallocation, $dbmodinstance->markingallocation);
144         // The goal not being to fully test assign_add_instance() we'll stop here for the assign tests - to avoid too many DB queries.
146         // Advanced grading.
147         $cm = get_coursemodule_from_instance('assign', $dbmodinstance->id);
148         $contextmodule = context_module::instance($cm->id);
149         $advancedgradingmethod = $DB->get_record('grading_areas',
150             array('contextid' => $contextmodule->id,
151                 'activemethod' => $moduleinfo->advancedgradingmethod_submissions));
152         $this->assertEquals($moduleinfo->advancedgradingmethod_submissions, $advancedgradingmethod);
153     }
155     /**
156      * Run some asserts test for a specific module for the function create_module().
157      *
158      * The function has been created (and is called) for $this->test_create_module().
159      * Note that the call to MODULE_create_set_values and MODULE_create_run_asserts are done after the common set values/run asserts.
160      * So if you want, you can overwrite the default values/asserts in the respective functions.
161      * @param string $modulename Name of the module ('forum', 'assign', 'book'...).
162      */
163     private function create_specific_module_test($modulename) {
164         global $DB, $CFG;
166         $this->resetAfterTest(true);
168         $this->setAdminUser();
170         // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
171         require_once($CFG->dirroot.'/mod/'. $modulename .'/lib.php');
173         // Enable avaibility.
174         // If not enabled all conditional fields will be ignored.
175         set_config('enableavailability', 1);
177         // Enable course completion.
178         // If not enabled all completion settings will be ignored.
179         set_config('enablecompletion', COMPLETION_ENABLED);
181         // Enable forum RSS feeds.
182         set_config('enablerssfeeds', 1);
183         set_config('forum_enablerssfeeds', 1);
185         $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED),
186            array('createsections'=>true));
188         $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
190         // Create assign module instance for test.
191         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
192         $params['course'] = $course->id;
193         $instance = $generator->create_instance($params);
194         $assigncm = get_coursemodule_from_instance('assign', $instance->id);
196         // Module test values.
197         $moduleinfo = new stdClass();
199         // Always mandatory generic values to any module.
200         $moduleinfo->modulename = $modulename;
201         $moduleinfo->section = 1; // This is the section number in the course. Not the section id in the database.
202         $moduleinfo->course = $course->id;
203         $moduleinfo->groupingid = $grouping->id;
204         $moduleinfo->visible = true;
205         $moduleinfo->visibleoncoursepage = true;
207         // Sometimes optional generic values for some modules.
208         $moduleinfo->name = 'My test module';
209         $moduleinfo->showdescription = 1; // standard boolean
210         require_once($CFG->libdir . '/gradelib.php');
211         $gradecats = grade_get_categories_menu($moduleinfo->course, false);
212         $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
213         $moduleinfo->gradecat = $gradecatid;
214         $moduleinfo->groupmode = VISIBLEGROUPS;
215         $moduleinfo->cmidnumber = 'idnumber_XXX';
217         // Completion common to all module.
218         $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC;
219         $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED;
220         $moduleinfo->completiongradeitemnumber = 1;
221         $moduleinfo->completionexpected = time() + (7 * 24 * 3600);
223         // Conditional activity.
224         $moduleinfo->availability = '{"op":"&","showc":[true,true],"c":[' .
225                 '{"type":"date","d":">=","t":' . time() . '},' .
226                 '{"type":"date","d":"<","t":' . (time() + (7 * 24 * 3600)) . '}' .
227                 ']}';
228         $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself.
229         $moduleinfo->conditiongradegroup = array(array('conditiongradeitemid' => $coursegradeitem->id, 'conditiongrademin' => 10, 'conditiongrademax' => 80));
230         $moduleinfo->conditionfieldgroup = array(array('conditionfield' => 'email', 'conditionfieldoperator' => \availability_profile\condition::OP_CONTAINS, 'conditionfieldvalue' => '@'));
231         $moduleinfo->conditioncompletiongroup = array(array('conditionsourcecmid' => $assigncm->id, 'conditionrequiredcompletion' => COMPLETION_COMPLETE)); // "conditionsourcecmid == 0" => none
233         // Grading and Advanced grading.
234         require_once($CFG->dirroot . '/rating/lib.php');
235         $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE;
236         $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number.
237         $moduleinfo->assesstimestart = time();
238         $moduleinfo->assesstimefinish = time() + (7 * 24 * 3600);
240         // RSS.
241         $moduleinfo->rsstype = 2;
242         $moduleinfo->rssarticles = 10;
244         // Optional intro editor (depends of module).
245         $draftid_editor = 0;
246         file_prepare_draft_area($draftid_editor, null, null, null, null);
247         $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
249         // Following is the advanced grading method area called 'submissions' for the 'assign' module.
250         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
251             $moduleinfo->grade = 100;
252         }
254         // Plagiarism form values.
255         // No plagiarism plugin installed by default. Use this space to make your own test.
257         // Values specific to the module.
258         $modulesetvalues = $modulename.'_create_set_values';
259         $this->$modulesetvalues($moduleinfo);
261         // Create the module.
262         $result = create_module($moduleinfo);
264         // Retrieve the module info.
265         $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance));
266         $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance);
267         // We passed the course section number to create_courses but $dbcm contain the section id.
268         // We need to retrieve the db course section number.
269         $section = $DB->get_record('course_sections', array('course' => $dbcm->course, 'id' => $dbcm->section));
270         // Retrieve the grade item.
271         $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course,
272             'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename));
274         // Compare the values common to all module instances.
275         $this->assertEquals($moduleinfo->modulename, $dbcm->modname);
276         $this->assertEquals($moduleinfo->section, $section->section);
277         $this->assertEquals($moduleinfo->course, $dbcm->course);
278         $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
279         $this->assertEquals($moduleinfo->visible, $dbcm->visible);
280         $this->assertEquals($moduleinfo->completion, $dbcm->completion);
281         $this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
282         $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber);
283         $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected);
284         $this->assertEquals($moduleinfo->availability, $dbcm->availability);
285         $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription);
286         $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode);
287         $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber);
288         $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid);
290         // Optional grade testing.
291         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
292             $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade);
293         }
295         // Some optional (but quite common) to some module.
296         $this->assertEquals($moduleinfo->name, $dbmodinstance->name);
297         $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro);
298         $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat);
300         // Test specific to the module.
301         $modulerunasserts = $modulename.'_create_run_asserts';
302         $this->$modulerunasserts($moduleinfo, $dbmodinstance);
303         return $moduleinfo;
304     }
306     /**
307      * Test create_module() for multiple modules defined in the $modules array (first declaration of the function).
308      */
309     public function test_create_module() {
310         // Add the module name you want to test here.
311         // Create the match MODULENAME_create_set_values() and MODULENAME_create_run_asserts().
312         $modules = array('forum', 'assign');
313         // Run all tests.
314         foreach ($modules as $modulename) {
315             $this->create_specific_module_test($modulename);
316         }
317     }
319     /**
320      * Test update_module() for multiple modules defined in the $modules array (first declaration of the function).
321      */
322     public function test_update_module() {
323         // Add the module name you want to test here.
324         // Create the match MODULENAME_update_set_values() and MODULENAME_update_run_asserts().
325         $modules = array('forum');
326         // Run all tests.
327         foreach ($modules as $modulename) {
328             $this->update_specific_module_test($modulename);
329         }
330     }
332     /**
333      * Set forum specific test values for calling update_module().
334      *
335      * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
336      */
337     private function forum_update_set_values(&$moduleinfo) {
338         // Completion specific to forum - optional.
339         $moduleinfo->completionposts = 3;
340         $moduleinfo->completiondiscussions = 1;
341         $moduleinfo->completionreplies = 2;
343         // Specific values to the Forum module.
344         $moduleinfo->forcesubscribe = FORUM_INITIALSUBSCRIBE;
345         $moduleinfo->type = 'single';
346         $moduleinfo->trackingtype = FORUM_TRACKING_FORCED;
347         $moduleinfo->maxbytes = 10240;
348         $moduleinfo->maxattachments = 2;
350         // Post threshold for blocking - specific to forum.
351         $moduleinfo->blockperiod = 60*60*24;
352         $moduleinfo->blockafter = 10;
353         $moduleinfo->warnafter = 5;
354     }
356     /**
357      * Execute test asserts on the saved DB data by update_module($forum).
358      *
359      * @param object $moduleinfo - the specific forum values that were used to update a forum.
360      * @param object $dbmodinstance - the DB values of the updated forum.
361      */
362     private function forum_update_run_asserts($moduleinfo, $dbmodinstance) {
363         // Compare values specific to forums.
364         $this->assertEquals($moduleinfo->forcesubscribe, $dbmodinstance->forcesubscribe);
365         $this->assertEquals($moduleinfo->type, $dbmodinstance->type);
366         $this->assertEquals($moduleinfo->assessed, $dbmodinstance->assessed);
367         $this->assertEquals($moduleinfo->completionposts, $dbmodinstance->completionposts);
368         $this->assertEquals($moduleinfo->completiondiscussions, $dbmodinstance->completiondiscussions);
369         $this->assertEquals($moduleinfo->completionreplies, $dbmodinstance->completionreplies);
370         $this->assertEquals($moduleinfo->scale, $dbmodinstance->scale);
371         $this->assertEquals($moduleinfo->assesstimestart, $dbmodinstance->assesstimestart);
372         $this->assertEquals($moduleinfo->assesstimefinish, $dbmodinstance->assesstimefinish);
373         $this->assertEquals($moduleinfo->rsstype, $dbmodinstance->rsstype);
374         $this->assertEquals($moduleinfo->rssarticles, $dbmodinstance->rssarticles);
375         $this->assertEquals($moduleinfo->trackingtype, $dbmodinstance->trackingtype);
376         $this->assertEquals($moduleinfo->maxbytes, $dbmodinstance->maxbytes);
377         $this->assertEquals($moduleinfo->maxattachments, $dbmodinstance->maxattachments);
378         $this->assertEquals($moduleinfo->blockperiod, $dbmodinstance->blockperiod);
379         $this->assertEquals($moduleinfo->blockafter, $dbmodinstance->blockafter);
380         $this->assertEquals($moduleinfo->warnafter, $dbmodinstance->warnafter);
381     }
385     /**
386      * Test a specific type of module.
387      *
388      * @param string $modulename - the module name to test
389      */
390     private function update_specific_module_test($modulename) {
391         global $DB, $CFG;
393         $this->resetAfterTest(true);
395         $this->setAdminUser();
397         // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
398         require_once($CFG->dirroot.'/mod/'. $modulename .'/lib.php');
400         // Enable avaibility.
401         // If not enabled all conditional fields will be ignored.
402         set_config('enableavailability', 1);
404         // Enable course completion.
405         // If not enabled all completion settings will be ignored.
406         set_config('enablecompletion', COMPLETION_ENABLED);
408         // Enable forum RSS feeds.
409         set_config('enablerssfeeds', 1);
410         set_config('forum_enablerssfeeds', 1);
412         $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED),
413            array('createsections'=>true));
415         $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
417         // Create assign module instance for testing gradeitem.
418         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
419         $params['course'] = $course->id;
420         $instance = $generator->create_instance($params);
421         $assigncm = get_coursemodule_from_instance('assign', $instance->id);
423         // Create the test forum to update.
424         $initvalues = new stdClass();
425         $initvalues->introformat = FORMAT_HTML;
426         $initvalues->course = $course->id;
427         $forum = self::getDataGenerator()->create_module('forum', $initvalues);
429         // Retrieve course module.
430         $cm = get_coursemodule_from_instance('forum', $forum->id);
432         // Module test values.
433         $moduleinfo = new stdClass();
435         // Always mandatory generic values to any module.
436         $moduleinfo->coursemodule = $cm->id;
437         $moduleinfo->modulename = $modulename;
438         $moduleinfo->course = $course->id;
439         $moduleinfo->groupingid = $grouping->id;
440         $moduleinfo->visible = true;
441         $moduleinfo->visibleoncoursepage = true;
443         // Sometimes optional generic values for some modules.
444         $moduleinfo->name = 'My test module';
445         $moduleinfo->showdescription = 1; // standard boolean
446         require_once($CFG->libdir . '/gradelib.php');
447         $gradecats = grade_get_categories_menu($moduleinfo->course, false);
448         $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
449         $moduleinfo->gradecat = $gradecatid;
450         $moduleinfo->groupmode = VISIBLEGROUPS;
451         $moduleinfo->cmidnumber = 'idnumber_XXX';
453         // Completion common to all module.
454         $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC;
455         $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED;
456         $moduleinfo->completiongradeitemnumber = 1;
457         $moduleinfo->completionexpected = time() + (7 * 24 * 3600);
458         $moduleinfo->completionunlocked = 1;
460         // Conditional activity.
461         $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself.
462         $moduleinfo->availability = json_encode(\core_availability\tree::get_root_json(
463                 array(\availability_date\condition::get_json('>=', time()),
464                 \availability_date\condition::get_json('<', time() + (7 * 24 * 3600)),
465                 \availability_grade\condition::get_json($coursegradeitem->id, 10, 80),
466                 \availability_profile\condition::get_json(false, 'email', 'contains', '@'),
467                 \availability_completion\condition::get_json($assigncm->id, COMPLETION_COMPLETE)), '&'));
469         // Grading and Advanced grading.
470         require_once($CFG->dirroot . '/rating/lib.php');
471         $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE;
472         $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number.
473         $moduleinfo->assesstimestart = time();
474         $moduleinfo->assesstimefinish = time() + (7 * 24 * 3600);
476         // RSS.
477         $moduleinfo->rsstype = 2;
478         $moduleinfo->rssarticles = 10;
480         // Optional intro editor (depends of module).
481         $draftid_editor = 0;
482         file_prepare_draft_area($draftid_editor, null, null, null, null);
483         $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
485         // Following is the advanced grading method area called 'submissions' for the 'assign' module.
486         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
487             $moduleinfo->grade = 100;
488         }
489         // Plagiarism form values.
490         // No plagiarism plugin installed by default. Use this space to make your own test.
492         // Values specific to the module.
493         $modulesetvalues = $modulename.'_update_set_values';
494         $this->$modulesetvalues($moduleinfo);
496         // Create the module.
497         $result = update_module($moduleinfo);
499         // Retrieve the module info.
500         $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance));
501         $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance);
502         // Retrieve the grade item.
503         $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course,
504             'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename));
506         // Compare the values common to all module instances.
507         $this->assertEquals($moduleinfo->modulename, $dbcm->modname);
508         $this->assertEquals($moduleinfo->course, $dbcm->course);
509         $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
510         $this->assertEquals($moduleinfo->visible, $dbcm->visible);
511         $this->assertEquals($moduleinfo->completion, $dbcm->completion);
512         $this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
513         $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber);
514         $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected);
515         $this->assertEquals($moduleinfo->availability, $dbcm->availability);
516         $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription);
517         $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode);
518         $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber);
519         $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid);
521         // Optional grade testing.
522         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
523             $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade);
524         }
526         // Some optional (but quite common) to some module.
527         $this->assertEquals($moduleinfo->name, $dbmodinstance->name);
528         $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro);
529         $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat);
531         // Test specific to the module.
532         $modulerunasserts = $modulename.'_update_run_asserts';
533         $this->$modulerunasserts($moduleinfo, $dbmodinstance);
534         return $moduleinfo;
535    }
537     /**
538      * Data provider for course_delete module
539      *
540      * @return array An array of arrays contain test data
541      */
542     public function provider_course_delete_module() {
543         $data = array();
545         $data['assign'] = array('assign', array('duedate' => time()));
546         $data['quiz'] = array('quiz', array('duedate' => time()));
548         return $data;
549     }
551     /**
552      * Test the create_course function
553      */
554     public function test_create_course() {
555         global $DB;
556         $this->resetAfterTest(true);
557         $defaultcategory = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
559         $course = new stdClass();
560         $course->fullname = 'Apu loves Unit Təsts';
561         $course->shortname = 'Spread the lŭve';
562         $course->idnumber = '123';
563         $course->summary = 'Awesome!';
564         $course->summaryformat = FORMAT_PLAIN;
565         $course->format = 'topics';
566         $course->newsitems = 0;
567         $course->category = $defaultcategory;
568         $original = (array) $course;
570         $created = create_course($course);
571         $context = context_course::instance($created->id);
573         // Compare original and created.
574         $this->assertEquals($original, array_intersect_key((array) $created, $original));
576         // Ensure default section is created.
577         $sectioncreated = $DB->record_exists('course_sections', array('course' => $created->id, 'section' => 0));
578         $this->assertTrue($sectioncreated);
580         // Ensure that the shortname isn't duplicated.
581         try {
582             $created = create_course($course);
583             $this->fail('Exception expected');
584         } catch (moodle_exception $e) {
585             $this->assertSame(get_string('shortnametaken', 'error', $course->shortname), $e->getMessage());
586         }
588         // Ensure that the idnumber isn't duplicated.
589         $course->shortname .= '1';
590         try {
591             $created = create_course($course);
592             $this->fail('Exception expected');
593         } catch (moodle_exception $e) {
594             $this->assertSame(get_string('courseidnumbertaken', 'error', $course->idnumber), $e->getMessage());
595         }
596     }
598     public function test_create_course_with_generator() {
599         global $DB;
600         $this->resetAfterTest(true);
601         $course = $this->getDataGenerator()->create_course();
603         // Ensure default section is created.
604         $sectioncreated = $DB->record_exists('course_sections', array('course' => $course->id, 'section' => 0));
605         $this->assertTrue($sectioncreated);
606     }
608     public function test_create_course_sections() {
609         global $DB;
610         $this->resetAfterTest(true);
612         $numsections = 5;
613         $course = $this->getDataGenerator()->create_course(
614                 array('shortname' => 'GrowingCourse',
615                     'fullname' => 'Growing Course',
616                     'numsections' => $numsections),
617                 array('createsections' => true));
619         // Ensure all 6 (0-5) sections were created and course content cache works properly
620         $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
621         $this->assertEquals(range(0, $numsections), $sectionscreated);
623         // this will do nothing, section already exists
624         $this->assertFalse(course_create_sections_if_missing($course, $numsections));
626         // this will create new section
627         $this->assertTrue(course_create_sections_if_missing($course, $numsections + 1));
629         // Ensure all 7 (0-6) sections were created and modinfo/sectioninfo cache works properly
630         $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
631         $this->assertEquals(range(0, $numsections + 1), $sectionscreated);
632     }
634     public function test_update_course() {
635         global $DB;
637         $this->resetAfterTest();
639         $defaultcategory = $DB->get_field_select('course_categories', 'MIN(id)', 'parent = 0');
641         $course = new stdClass();
642         $course->fullname = 'Apu loves Unit Təsts';
643         $course->shortname = 'test1';
644         $course->idnumber = '1';
645         $course->summary = 'Awesome!';
646         $course->summaryformat = FORMAT_PLAIN;
647         $course->format = 'topics';
648         $course->newsitems = 0;
649         $course->numsections = 5;
650         $course->category = $defaultcategory;
652         $created = create_course($course);
653         // Ensure the checks only work on idnumber/shortname that are not already ours.
654         update_course($created);
656         $course->shortname = 'test2';
657         $course->idnumber = '2';
659         $created2 = create_course($course);
661         // Test duplicate idnumber.
662         $created2->idnumber = '1';
663         try {
664             update_course($created2);
665             $this->fail('Expected exception when trying to update a course with duplicate idnumber');
666         } catch (moodle_exception $e) {
667             $this->assertEquals(get_string('courseidnumbertaken', 'error', $created2->idnumber), $e->getMessage());
668         }
670         // Test duplicate shortname.
671         $created2->idnumber = '2';
672         $created2->shortname = 'test1';
673         try {
674             update_course($created2);
675             $this->fail('Expected exception when trying to update a course with a duplicate shortname');
676         } catch (moodle_exception $e) {
677             $this->assertEquals(get_string('shortnametaken', 'error', $created2->shortname), $e->getMessage());
678         }
679     }
681     public function test_update_course_section_time_modified() {
682         global $DB;
684         $this->resetAfterTest();
686         // Create the course with sections.
687         $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true));
688         $sections = $DB->get_records('course_sections', array('course' => $course->id));
690         // Get the last section's time modified value.
691         $section = array_pop($sections);
692         $oldtimemodified = $section->timemodified;
694         // Update the section.
695         $this->waitForSecond(); // Ensuring that the section update occurs at a different timestamp.
696         course_update_section($course, $section, array());
698         // Check that the time has changed.
699         $section = $DB->get_record('course_sections', array('id' => $section->id));
700         $newtimemodified = $section->timemodified;
701         $this->assertGreaterThan($oldtimemodified, $newtimemodified);
702     }
704     public function test_course_add_cm_to_section() {
705         global $DB;
706         $this->resetAfterTest(true);
708         // Create course with 1 section.
709         $course = $this->getDataGenerator()->create_course(
710                 array('shortname' => 'GrowingCourse',
711                     'fullname' => 'Growing Course',
712                     'numsections' => 1),
713                 array('createsections' => true));
715         // Trash modinfo.
716         rebuild_course_cache($course->id, true);
718         // Create some cms for testing.
719         $cmids = array();
720         for ($i=0; $i<4; $i++) {
721             $cmids[$i] = $DB->insert_record('course_modules', array('course' => $course->id));
722         }
724         // Add it to section that exists.
725         course_add_cm_to_section($course, $cmids[0], 1);
727         // Check it got added to sequence.
728         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 1));
729         $this->assertEquals($cmids[0], $sequence);
731         // Add a second, this time using courseid variant of parameters.
732         $coursecacherev = $DB->get_field('course', 'cacherev', array('id' => $course->id));
733         course_add_cm_to_section($course->id, $cmids[1], 1);
734         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 1));
735         $this->assertEquals($cmids[0] . ',' . $cmids[1], $sequence);
737         // Check that modinfo cache was reset but not rebuilt (important for performance if calling repeatedly).
738         $this->assertGreaterThan($coursecacherev, $DB->get_field('course', 'cacherev', array('id' => $course->id)));
739         $this->assertEmpty(cache::make('core', 'coursemodinfo')->get($course->id));
741         // Add one to section that doesn't exist (this might rebuild modinfo).
742         course_add_cm_to_section($course, $cmids[2], 2);
743         $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id)));
744         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 2));
745         $this->assertEquals($cmids[2], $sequence);
747         // Add using the 'before' option.
748         course_add_cm_to_section($course, $cmids[3], 2, $cmids[2]);
749         $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id)));
750         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 2));
751         $this->assertEquals($cmids[3] . ',' . $cmids[2], $sequence);
752     }
754     public function test_reorder_sections() {
755         global $DB;
756         $this->resetAfterTest(true);
758         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
759         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
760         $oldsections = array();
761         $sections = array();
762         foreach ($DB->get_records('course_sections', array('course'=>$course->id), 'id') as $section) {
763             $oldsections[$section->section] = $section->id;
764             $sections[$section->id] = $section->section;
765         }
766         ksort($oldsections);
768         $neworder = reorder_sections($sections, 2, 4);
769         $neworder = array_keys($neworder);
770         $this->assertEquals($oldsections[0], $neworder[0]);
771         $this->assertEquals($oldsections[1], $neworder[1]);
772         $this->assertEquals($oldsections[2], $neworder[4]);
773         $this->assertEquals($oldsections[3], $neworder[2]);
774         $this->assertEquals($oldsections[4], $neworder[3]);
775         $this->assertEquals($oldsections[5], $neworder[5]);
776         $this->assertEquals($oldsections[6], $neworder[6]);
778         $neworder = reorder_sections($sections, 4, 2);
779         $neworder = array_keys($neworder);
780         $this->assertEquals($oldsections[0], $neworder[0]);
781         $this->assertEquals($oldsections[1], $neworder[1]);
782         $this->assertEquals($oldsections[2], $neworder[3]);
783         $this->assertEquals($oldsections[3], $neworder[4]);
784         $this->assertEquals($oldsections[4], $neworder[2]);
785         $this->assertEquals($oldsections[5], $neworder[5]);
786         $this->assertEquals($oldsections[6], $neworder[6]);
788         $neworder = reorder_sections(1, 2, 4);
789         $this->assertFalse($neworder);
790     }
792     public function test_move_section_down() {
793         global $DB;
794         $this->resetAfterTest(true);
796         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
797         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
798         $oldsections = array();
799         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
800             $oldsections[$section->section] = $section->id;
801         }
802         ksort($oldsections);
804         // Test move section down..
805         move_section_to($course, 2, 4);
806         $sections = array();
807         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
808             $sections[$section->section] = $section->id;
809         }
810         ksort($sections);
812         $this->assertEquals($oldsections[0], $sections[0]);
813         $this->assertEquals($oldsections[1], $sections[1]);
814         $this->assertEquals($oldsections[2], $sections[4]);
815         $this->assertEquals($oldsections[3], $sections[2]);
816         $this->assertEquals($oldsections[4], $sections[3]);
817         $this->assertEquals($oldsections[5], $sections[5]);
818         $this->assertEquals($oldsections[6], $sections[6]);
819     }
821     public function test_move_section_up() {
822         global $DB;
823         $this->resetAfterTest(true);
825         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
826         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
827         $oldsections = array();
828         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
829             $oldsections[$section->section] = $section->id;
830         }
831         ksort($oldsections);
833         // Test move section up..
834         move_section_to($course, 6, 4);
835         $sections = array();
836         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
837             $sections[$section->section] = $section->id;
838         }
839         ksort($sections);
841         $this->assertEquals($oldsections[0], $sections[0]);
842         $this->assertEquals($oldsections[1], $sections[1]);
843         $this->assertEquals($oldsections[2], $sections[2]);
844         $this->assertEquals($oldsections[3], $sections[3]);
845         $this->assertEquals($oldsections[4], $sections[5]);
846         $this->assertEquals($oldsections[5], $sections[6]);
847         $this->assertEquals($oldsections[6], $sections[4]);
848     }
850     public function test_move_section_marker() {
851         global $DB;
852         $this->resetAfterTest(true);
854         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
855         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
857         // Set course marker to the section we are going to move..
858         course_set_marker($course->id, 2);
859         // Verify that the course marker is set correctly.
860         $course = $DB->get_record('course', array('id' => $course->id));
861         $this->assertEquals(2, $course->marker);
863         // Test move the marked section down..
864         move_section_to($course, 2, 4);
866         // Verify that the coruse marker has been moved along with the section..
867         $course = $DB->get_record('course', array('id' => $course->id));
868         $this->assertEquals(4, $course->marker);
870         // Test move the marked section up..
871         move_section_to($course, 4, 3);
873         // Verify that the course marker has been moved along with the section..
874         $course = $DB->get_record('course', array('id' => $course->id));
875         $this->assertEquals(3, $course->marker);
877         // Test moving a non-marked section above the marked section..
878         move_section_to($course, 4, 2);
880         // Verify that the course marker has been moved down to accomodate..
881         $course = $DB->get_record('course', array('id' => $course->id));
882         $this->assertEquals(4, $course->marker);
884         // Test moving a non-marked section below the marked section..
885         move_section_to($course, 3, 6);
887         // Verify that the course marker has been up to accomodate..
888         $course = $DB->get_record('course', array('id' => $course->id));
889         $this->assertEquals(3, $course->marker);
890     }
892     public function test_course_can_delete_section() {
893         global $DB;
894         $this->resetAfterTest(true);
896         $generator = $this->getDataGenerator();
898         $courseweeks = $generator->create_course(
899             array('numsections' => 5, 'format' => 'weeks'),
900             array('createsections' => true));
901         $assign1 = $generator->create_module('assign', array('course' => $courseweeks, 'section' => 1));
902         $assign2 = $generator->create_module('assign', array('course' => $courseweeks, 'section' => 2));
904         $coursetopics = $generator->create_course(
905             array('numsections' => 5, 'format' => 'topics'),
906             array('createsections' => true));
908         $coursesingleactivity = $generator->create_course(
909             array('format' => 'singleactivity'),
910             array('createsections' => true));
912         // Enrol student and teacher.
913         $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
914         $student = $generator->create_user();
915         $teacher = $generator->create_user();
917         $generator->enrol_user($student->id, $courseweeks->id, $roleids['student']);
918         $generator->enrol_user($teacher->id, $courseweeks->id, $roleids['editingteacher']);
920         $generator->enrol_user($student->id, $coursetopics->id, $roleids['student']);
921         $generator->enrol_user($teacher->id, $coursetopics->id, $roleids['editingteacher']);
923         $generator->enrol_user($student->id, $coursesingleactivity->id, $roleids['student']);
924         $generator->enrol_user($teacher->id, $coursesingleactivity->id, $roleids['editingteacher']);
926         // Teacher should be able to delete sections (except for 0) in topics and weeks format.
927         $this->setUser($teacher);
929         // For topics and weeks formats will return false for section 0 and true for any other section.
930         $this->assertFalse(course_can_delete_section($courseweeks, 0));
931         $this->assertTrue(course_can_delete_section($courseweeks, 1));
933         $this->assertFalse(course_can_delete_section($coursetopics, 0));
934         $this->assertTrue(course_can_delete_section($coursetopics, 1));
936         // For singleactivity course format no section can be deleted.
937         $this->assertFalse(course_can_delete_section($coursesingleactivity, 0));
938         $this->assertFalse(course_can_delete_section($coursesingleactivity, 1));
940         // Now let's revoke a capability from teacher to manage activity in section 1.
941         $modulecontext = context_module::instance($assign1->cmid);
942         assign_capability('moodle/course:manageactivities', CAP_PROHIBIT, $roleids['editingteacher'],
943             $modulecontext);
944         $modulecontext->mark_dirty();
945         $this->assertFalse(course_can_delete_section($courseweeks, 1));
946         $this->assertTrue(course_can_delete_section($courseweeks, 2));
948         // Student does not have permissions to delete sections.
949         $this->setUser($student);
950         $this->assertFalse(course_can_delete_section($courseweeks, 1));
951         $this->assertFalse(course_can_delete_section($coursetopics, 1));
952         $this->assertFalse(course_can_delete_section($coursesingleactivity, 1));
953     }
955     public function test_course_delete_section() {
956         global $DB;
957         $this->resetAfterTest(true);
959         $generator = $this->getDataGenerator();
961         $course = $generator->create_course(array('numsections' => 6, 'format' => 'topics'),
962             array('createsections' => true));
963         $assign0 = $generator->create_module('assign', array('course' => $course, 'section' => 0));
964         $assign1 = $generator->create_module('assign', array('course' => $course, 'section' => 1));
965         $assign21 = $generator->create_module('assign', array('course' => $course, 'section' => 2));
966         $assign22 = $generator->create_module('assign', array('course' => $course, 'section' => 2));
967         $assign3 = $generator->create_module('assign', array('course' => $course, 'section' => 3));
968         $assign5 = $generator->create_module('assign', array('course' => $course, 'section' => 5));
969         $assign6 = $generator->create_module('assign', array('course' => $course, 'section' => 6));
971         $this->setAdminUser();
973         // Attempt to delete non-existing section.
974         $this->assertFalse(course_delete_section($course, 10, false));
975         $this->assertFalse(course_delete_section($course, 9, true));
977         // Attempt to delete 0-section.
978         $this->assertFalse(course_delete_section($course, 0, true));
979         $this->assertTrue($DB->record_exists('course_modules', array('id' => $assign0->cmid)));
981         // Delete last section.
982         $this->assertTrue(course_delete_section($course, 6, true));
983         $this->assertFalse($DB->record_exists('course_modules', array('id' => $assign6->cmid)));
984         $this->assertEquals(5, course_get_format($course)->get_last_section_number());
986         // Delete empty section.
987         $this->assertTrue(course_delete_section($course, 4, false));
988         $this->assertEquals(4, course_get_format($course)->get_last_section_number());
990         // Delete section in the middle (2).
991         $this->assertFalse(course_delete_section($course, 2, false));
992         $this->assertTrue(course_delete_section($course, 2, true));
993         $this->assertFalse($DB->record_exists('course_modules', array('id' => $assign21->cmid)));
994         $this->assertFalse($DB->record_exists('course_modules', array('id' => $assign22->cmid)));
995         $this->assertEquals(3, course_get_format($course)->get_last_section_number());
996         $this->assertEquals(array(0 => array($assign0->cmid),
997             1 => array($assign1->cmid),
998             2 => array($assign3->cmid),
999             3 => array($assign5->cmid)), get_fast_modinfo($course)->sections);
1001         // Remove marked section.
1002         course_set_marker($course->id, 1);
1003         $this->assertTrue(course_get_format($course)->is_section_current(1));
1004         $this->assertTrue(course_delete_section($course, 1, true));
1005         $this->assertFalse(course_get_format($course)->is_section_current(1));
1006     }
1008     public function test_get_course_display_name_for_list() {
1009         global $CFG;
1010         $this->resetAfterTest(true);
1012         $course = $this->getDataGenerator()->create_course(array('shortname' => 'FROG101', 'fullname' => 'Introduction to pond life'));
1014         $CFG->courselistshortnames = 0;
1015         $this->assertEquals('Introduction to pond life', get_course_display_name_for_list($course));
1017         $CFG->courselistshortnames = 1;
1018         $this->assertEquals('FROG101 Introduction to pond life', get_course_display_name_for_list($course));
1019     }
1021     public function test_move_module_in_course() {
1022         global $DB;
1024         $this->resetAfterTest(true);
1025         // Setup fixture
1026         $course = $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections' => true));
1027         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
1029         $cms = get_fast_modinfo($course)->get_cms();
1030         $cm = reset($cms);
1032         $newsection = get_fast_modinfo($course)->get_section_info(3);
1033         $oldsectionid = $cm->section;
1035         // Perform the move
1036         moveto_module($cm, $newsection);
1038         $cms = get_fast_modinfo($course)->get_cms();
1039         $cm = reset($cms);
1041         // Check that the cached modinfo contains the correct section info
1042         $modinfo = get_fast_modinfo($course);
1043         $this->assertTrue(empty($modinfo->sections[0]));
1044         $this->assertFalse(empty($modinfo->sections[3]));
1046         // Check that the old section's sequence no longer contains this ID
1047         $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
1048         $oldsequences = explode(',', $newsection->sequence);
1049         $this->assertFalse(in_array($cm->id, $oldsequences));
1051         // Check that the new section's sequence now contains this ID
1052         $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
1053         $newsequences = explode(',', $newsection->sequence);
1054         $this->assertTrue(in_array($cm->id, $newsequences));
1056         // Check that the section number has been changed in the cm
1057         $this->assertEquals($newsection->id, $cm->section);
1060         // Perform a second move as some issues were only seen on the second move
1061         $newsection = get_fast_modinfo($course)->get_section_info(2);
1062         $oldsectionid = $cm->section;
1063         moveto_module($cm, $newsection);
1065         $cms = get_fast_modinfo($course)->get_cms();
1066         $cm = reset($cms);
1068         // Check that the cached modinfo contains the correct section info
1069         $modinfo = get_fast_modinfo($course);
1070         $this->assertTrue(empty($modinfo->sections[0]));
1071         $this->assertFalse(empty($modinfo->sections[2]));
1073         // Check that the old section's sequence no longer contains this ID
1074         $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
1075         $oldsequences = explode(',', $newsection->sequence);
1076         $this->assertFalse(in_array($cm->id, $oldsequences));
1078         // Check that the new section's sequence now contains this ID
1079         $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
1080         $newsequences = explode(',', $newsection->sequence);
1081         $this->assertTrue(in_array($cm->id, $newsequences));
1082     }
1084     public function test_module_visibility() {
1085         $this->setAdminUser();
1086         $this->resetAfterTest(true);
1088         // Create course and modules.
1089         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
1090         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1091         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(), 'course' => $course->id));
1092         $modules = compact('forum', 'assign');
1094         // Hiding the modules.
1095         foreach ($modules as $mod) {
1096             set_coursemodule_visible($mod->cmid, 0);
1097             $this->check_module_visibility($mod, 0, 0);
1098         }
1100         // Showing the modules.
1101         foreach ($modules as $mod) {
1102             set_coursemodule_visible($mod->cmid, 1);
1103             $this->check_module_visibility($mod, 1, 1);
1104         }
1105     }
1107     public function test_section_visibility_events() {
1108         $this->setAdminUser();
1109         $this->resetAfterTest(true);
1111         $course = $this->getDataGenerator()->create_course(array('numsections' => 1), array('createsections' => true));
1112         $sectionnumber = 1;
1113         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1114             array('section' => $sectionnumber));
1115         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1116             'course' => $course->id), array('section' => $sectionnumber));
1117         $sink = $this->redirectEvents();
1118         set_section_visible($course->id, $sectionnumber, 0);
1119         $events = $sink->get_events();
1121         // Extract the number of events related to what we are testing, other events
1122         // such as course_section_updated could have been triggered.
1123         $count = 0;
1124         foreach ($events as $event) {
1125             if ($event instanceof \core\event\course_module_updated) {
1126                 $count++;
1127             }
1128         }
1129         $this->assertSame(2, $count);
1130         $sink->close();
1131     }
1133     public function test_section_visibility() {
1134         $this->setAdminUser();
1135         $this->resetAfterTest(true);
1137         // Create course.
1138         $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
1140         $sink = $this->redirectEvents();
1142         // Testing an empty section.
1143         $sectionnumber = 1;
1144         set_section_visible($course->id, $sectionnumber, 0);
1145         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1146         $this->assertEquals($section_info->visible, 0);
1147         set_section_visible($course->id, $sectionnumber, 1);
1148         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1149         $this->assertEquals($section_info->visible, 1);
1151         // Checking that an event was fired.
1152         $events = $sink->get_events();
1153         $this->assertInstanceOf('\core\event\course_section_updated', $events[0]);
1154         $sink->close();
1156         // Testing a section with visible modules.
1157         $sectionnumber = 2;
1158         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1159                 array('section' => $sectionnumber));
1160         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1161                 'course' => $course->id), array('section' => $sectionnumber));
1162         $modules = compact('forum', 'assign');
1163         set_section_visible($course->id, $sectionnumber, 0);
1164         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1165         $this->assertEquals($section_info->visible, 0);
1166         foreach ($modules as $mod) {
1167             $this->check_module_visibility($mod, 0, 1);
1168         }
1169         set_section_visible($course->id, $sectionnumber, 1);
1170         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1171         $this->assertEquals($section_info->visible, 1);
1172         foreach ($modules as $mod) {
1173             $this->check_module_visibility($mod, 1, 1);
1174         }
1176         // Testing a section with hidden modules, which should stay hidden.
1177         $sectionnumber = 3;
1178         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1179                 array('section' => $sectionnumber));
1180         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1181                 'course' => $course->id), array('section' => $sectionnumber));
1182         $modules = compact('forum', 'assign');
1183         foreach ($modules as $mod) {
1184             set_coursemodule_visible($mod->cmid, 0);
1185             $this->check_module_visibility($mod, 0, 0);
1186         }
1187         set_section_visible($course->id, $sectionnumber, 0);
1188         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1189         $this->assertEquals($section_info->visible, 0);
1190         foreach ($modules as $mod) {
1191             $this->check_module_visibility($mod, 0, 0);
1192         }
1193         set_section_visible($course->id, $sectionnumber, 1);
1194         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1195         $this->assertEquals($section_info->visible, 1);
1196         foreach ($modules as $mod) {
1197             $this->check_module_visibility($mod, 0, 0);
1198         }
1199     }
1201     /**
1202      * Helper function to assert that a module has correctly been made visible, or hidden.
1203      *
1204      * @param stdClass $mod module information
1205      * @param int $visibility the current state of the module
1206      * @param int $visibleold the current state of the visibleold property
1207      * @return void
1208      */
1209     public function check_module_visibility($mod, $visibility, $visibleold) {
1210         global $DB;
1211         $cm = get_fast_modinfo($mod->course)->get_cm($mod->cmid);
1212         $this->assertEquals($visibility, $cm->visible);
1213         $this->assertEquals($visibleold, $cm->visibleold);
1215         // Check the module grade items.
1216         $grade_items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $cm->modname,
1217                 'iteminstance' => $cm->instance, 'courseid' => $cm->course));
1218         if ($grade_items) {
1219             foreach ($grade_items as $grade_item) {
1220                 if ($visibility) {
1221                     $this->assertFalse($grade_item->is_hidden(), "$cm->modname grade_item not visible");
1222                 } else {
1223                     $this->assertTrue($grade_item->is_hidden(), "$cm->modname grade_item not hidden");
1224                 }
1225             }
1226         }
1228         // Check the events visibility.
1229         if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $cm->modname))) {
1230             foreach ($events as $event) {
1231                 $calevent = new calendar_event($event);
1232                 $this->assertEquals($visibility, $calevent->visible, "$cm->modname calendar_event visibility");
1233             }
1234         }
1235     }
1237     public function test_course_page_type_list() {
1238         global $DB;
1239         $this->resetAfterTest(true);
1241         // Create a category.
1242         $category = new stdClass();
1243         $category->name = 'Test Category';
1245         $testcategory = $this->getDataGenerator()->create_category($category);
1247         // Create a course.
1248         $course = new stdClass();
1249         $course->fullname = 'Apu loves Unit Təsts';
1250         $course->shortname = 'Spread the lŭve';
1251         $course->idnumber = '123';
1252         $course->summary = 'Awesome!';
1253         $course->summaryformat = FORMAT_PLAIN;
1254         $course->format = 'topics';
1255         $course->newsitems = 0;
1256         $course->numsections = 5;
1257         $course->category = $testcategory->id;
1259         $testcourse = $this->getDataGenerator()->create_course($course);
1261         // Create contexts.
1262         $coursecontext = context_course::instance($testcourse->id);
1263         $parentcontext = $coursecontext->get_parent_context(); // Not actually used.
1264         $pagetype = 'page-course-x'; // Not used either.
1265         $pagetypelist = course_page_type_list($pagetype, $parentcontext, $coursecontext);
1267         // Page type lists for normal courses.
1268         $testpagetypelist1 = array();
1269         $testpagetypelist1['*'] = 'Any page';
1270         $testpagetypelist1['course-*'] = 'Any course page';
1271         $testpagetypelist1['course-view-*'] = 'Any type of course main page';
1273         $this->assertEquals($testpagetypelist1, $pagetypelist);
1275         // Get the context for the front page course.
1276         $sitecoursecontext = context_course::instance(SITEID);
1277         $pagetypelist = course_page_type_list($pagetype, $parentcontext, $sitecoursecontext);
1279         // Page type list for the front page course.
1280         $testpagetypelist2 = array('*' => 'Any page');
1281         $this->assertEquals($testpagetypelist2, $pagetypelist);
1283         // Make sure that providing no current context to the function doesn't result in an error.
1284         // Calls made from generate_page_type_patterns() may provide null values.
1285         $pagetypelist = course_page_type_list($pagetype, null, null);
1286         $this->assertEquals($pagetypelist, $testpagetypelist1);
1287     }
1289     public function test_compare_activities_by_time_desc() {
1291         // Let's create some test data.
1292         $activitiesivities = array();
1293         $x = new stdClass();
1294         $x->timestamp = null;
1295         $activities[] = $x;
1297         $x = new stdClass();
1298         $x->timestamp = 1;
1299         $activities[] = $x;
1301         $x = new stdClass();
1302         $x->timestamp = 3;
1303         $activities[] = $x;
1305         $x = new stdClass();
1306         $x->timestamp = 0;
1307         $activities[] = $x;
1309         $x = new stdClass();
1310         $x->timestamp = 5;
1311         $activities[] = $x;
1313         $x = new stdClass();
1314         $activities[] = $x;
1316         $x = new stdClass();
1317         $x->timestamp = 5;
1318         $activities[] = $x;
1320         // Do the sorting.
1321         usort($activities, 'compare_activities_by_time_desc');
1323         // Let's check the result.
1324         $last = 10;
1325         foreach($activities as $activity) {
1326             if (empty($activity->timestamp)) {
1327                 $activity->timestamp = 0;
1328             }
1329             $this->assertLessThanOrEqual($last, $activity->timestamp);
1330         }
1331     }
1333     public function test_compare_activities_by_time_asc() {
1335         // Let's create some test data.
1336         $activities = array();
1337         $x = new stdClass();
1338         $x->timestamp = null;
1339         $activities[] = $x;
1341         $x = new stdClass();
1342         $x->timestamp = 1;
1343         $activities[] = $x;
1345         $x = new stdClass();
1346         $x->timestamp = 3;
1347         $activities[] = $x;
1349         $x = new stdClass();
1350         $x->timestamp = 0;
1351         $activities[] = $x;
1353         $x = new stdClass();
1354         $x->timestamp = 5;
1355         $activities[] = $x;
1357         $x = new stdClass();
1358         $activities[] = $x;
1360         $x = new stdClass();
1361         $x->timestamp = 5;
1362         $activities[] = $x;
1364         // Do the sorting.
1365         usort($activities, 'compare_activities_by_time_asc');
1367         // Let's check the result.
1368         $last = 0;
1369         foreach($activities as $activity) {
1370             if (empty($activity->timestamp)) {
1371                 $activity->timestamp = 0;
1372             }
1373             $this->assertGreaterThanOrEqual($last, $activity->timestamp);
1374         }
1375     }
1377     /**
1378      * Tests moving a module between hidden/visible sections and
1379      * verifies that the course/module visiblity seettings are
1380      * retained.
1381      */
1382     public function test_moveto_module_between_hidden_sections() {
1383         global $DB;
1385         $this->resetAfterTest(true);
1387         $course = $this->getDataGenerator()->create_course(array('numsections' => 4), array('createsections' => true));
1388         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1389         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
1390         $quiz= $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
1392         // Set the page as hidden
1393         set_coursemodule_visible($page->cmid, 0);
1395         // Set sections 3 as hidden.
1396         set_section_visible($course->id, 3, 0);
1398         $modinfo = get_fast_modinfo($course);
1400         $hiddensection = $modinfo->get_section_info(3);
1401         // New section is definitely not visible:
1402         $this->assertEquals($hiddensection->visible, 0);
1404         $forumcm = $modinfo->cms[$forum->cmid];
1405         $pagecm = $modinfo->cms[$page->cmid];
1407         // Move the forum and the page to a hidden section, make sure moveto_module returns 0 as new visibility state.
1408         $this->assertEquals(0, moveto_module($forumcm, $hiddensection));
1409         $this->assertEquals(0, moveto_module($pagecm, $hiddensection));
1411         $modinfo = get_fast_modinfo($course);
1413         // Verify that forum and page have been moved to the hidden section and quiz has not.
1414         $this->assertContains($forum->cmid, $modinfo->sections[3]);
1415         $this->assertContains($page->cmid, $modinfo->sections[3]);
1416         $this->assertNotContains($quiz->cmid, $modinfo->sections[3]);
1418         // Verify that forum has been made invisible.
1419         $forumcm = $modinfo->cms[$forum->cmid];
1420         $this->assertEquals($forumcm->visible, 0);
1421         // Verify that old state has been retained.
1422         $this->assertEquals($forumcm->visibleold, 1);
1424         // Verify that page has stayed invisible.
1425         $pagecm = $modinfo->cms[$page->cmid];
1426         $this->assertEquals($pagecm->visible, 0);
1427         // Verify that old state has been retained.
1428         $this->assertEquals($pagecm->visibleold, 0);
1430         // Verify that quiz has been unaffected.
1431         $quizcm = $modinfo->cms[$quiz->cmid];
1432         $this->assertEquals($quizcm->visible, 1);
1434         // Move forum and page back to visible section.
1435         // Make sure the visibility is restored to the original value (visible for forum and hidden for page).
1436         $visiblesection = $modinfo->get_section_info(2);
1437         $this->assertEquals(1, moveto_module($forumcm, $visiblesection));
1438         $this->assertEquals(0, moveto_module($pagecm, $visiblesection));
1440         $modinfo = get_fast_modinfo($course);
1442         // Double check that forum has been made visible.
1443         $forumcm = $modinfo->cms[$forum->cmid];
1444         $this->assertEquals($forumcm->visible, 1);
1446         // Double check that page has stayed invisible.
1447         $pagecm = $modinfo->cms[$page->cmid];
1448         $this->assertEquals($pagecm->visible, 0);
1450         // Move the page in the same section (this is what mod duplicate does).
1451         // Visibility of page remains 0.
1452         $this->assertEquals(0, moveto_module($pagecm, $visiblesection, $forumcm));
1454         // Double check that the the page is still hidden.
1455         $modinfo = get_fast_modinfo($course);
1456         $pagecm = $modinfo->cms[$page->cmid];
1457         $this->assertEquals($pagecm->visible, 0);
1458     }
1460     /**
1461      * Tests moving a module around in the same section. moveto_module()
1462      * is called this way in modduplicate.
1463      */
1464     public function test_moveto_module_in_same_section() {
1465         global $DB;
1467         $this->resetAfterTest(true);
1469         $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
1470         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
1471         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1473         // Simulate inconsistent visible/visibleold values (MDL-38713).
1474         $cm = $DB->get_record('course_modules', array('id' => $page->cmid), '*', MUST_EXIST);
1475         $cm->visible = 0;
1476         $cm->visibleold = 1;
1477         $DB->update_record('course_modules', $cm);
1479         $modinfo = get_fast_modinfo($course);
1480         $forumcm = $modinfo->cms[$forum->cmid];
1481         $pagecm = $modinfo->cms[$page->cmid];
1483         // Verify that page is hidden.
1484         $this->assertEquals($pagecm->visible, 0);
1486         // Verify section 0 is where all mods added.
1487         $section = $modinfo->get_section_info(0);
1488         $this->assertEquals($section->id, $forumcm->section);
1489         $this->assertEquals($section->id, $pagecm->section);
1492         // Move the page inside the hidden section. Make sure it is hidden.
1493         $this->assertEquals(0, moveto_module($pagecm, $section, $forumcm));
1495         // Double check that the the page is still hidden.
1496         $modinfo = get_fast_modinfo($course);
1497         $pagecm = $modinfo->cms[$page->cmid];
1498         $this->assertEquals($pagecm->visible, 0);
1499     }
1501     /**
1502      * Tests the function that deletes a course module
1503      *
1504      * @param string $type The type of module for the test
1505      * @param array $options The options for the module creation
1506      * @dataProvider provider_course_delete_module
1507      */
1508     public function test_course_delete_module($type, $options) {
1509         global $DB;
1511         $this->resetAfterTest(true);
1512         $this->setAdminUser();
1514         // Create course and modules.
1515         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
1516         $options['course'] = $course->id;
1518         // Generate an assignment with due date (will generate a course event).
1519         $module = $this->getDataGenerator()->create_module($type, $options);
1521         // Get the module context.
1522         $modcontext = context_module::instance($module->cmid);
1524         // Verify context exists.
1525         $this->assertInstanceOf('context_module', $modcontext);
1527         // Make module specific messes.
1528         switch ($type) {
1529             case 'assign':
1530                 // Add some tags to this assignment.
1531                 core_tag_tag::set_item_tags('mod_assign', 'assign', $module->id, $modcontext, array('Tag 1', 'Tag 2', 'Tag 3'));
1532                 core_tag_tag::set_item_tags('core', 'course_modules', $module->cmid, $modcontext, array('Tag 3', 'Tag 4', 'Tag 5'));
1534                 // Confirm the tag instances were added.
1535                 $criteria = array('component' => 'mod_assign', 'itemtype' => 'assign', 'contextid' => $modcontext->id);
1536                 $this->assertEquals(3, $DB->count_records('tag_instance', $criteria));
1537                 $criteria = array('component' => 'core', 'itemtype' => 'course_modules', 'contextid' => $modcontext->id);
1538                 $this->assertEquals(3, $DB->count_records('tag_instance', $criteria));
1540                 // Verify event assignment event has been generated.
1541                 $eventcount = $DB->count_records('event', array('instance' => $module->id, 'modulename' => $type));
1542                 $this->assertEquals(1, $eventcount);
1544                 break;
1545             case 'quiz':
1546                 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
1547                 $qcat = $qgen->create_question_category(array('contextid' => $modcontext->id));
1548                 $questions = array(
1549                     $qgen->create_question('shortanswer', null, array('category' => $qcat->id)),
1550                     $qgen->create_question('shortanswer', null, array('category' => $qcat->id)),
1551                 );
1552                 $this->expectOutputRegex('/'.get_string('unusedcategorydeleted', 'question').'/');
1553                 break;
1554             default:
1555                 break;
1556         }
1558         // Run delete..
1559         course_delete_module($module->cmid);
1561         // Verify the context has been removed.
1562         $this->assertFalse(context_module::instance($module->cmid, IGNORE_MISSING));
1564         // Verify the course_module record has been deleted.
1565         $cmcount = $DB->count_records('course_modules', array('id' => $module->cmid));
1566         $this->assertEmpty($cmcount);
1568         // Test clean up of module specific messes.
1569         switch ($type) {
1570             case 'assign':
1571                 // Verify event assignment events have been removed.
1572                 $eventcount = $DB->count_records('event', array('instance' => $module->id, 'modulename' => $type));
1573                 $this->assertEmpty($eventcount);
1575                 // Verify the tag instances were deleted.
1576                 $criteria = array('component' => 'mod_assign', 'contextid' => $modcontext->id);
1577                 $this->assertEquals(0, $DB->count_records('tag_instance', $criteria));
1579                 $criteria = array('component' => 'core', 'itemtype' => 'course_modules', 'contextid' => $modcontext->id);
1580                 $this->assertEquals(0, $DB->count_records('tag_instance', $criteria));
1581                 break;
1582             case 'quiz':
1583                 // Verify category deleted.
1584                 $criteria = array('contextid' => $modcontext->id);
1585                 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
1587                 // Verify questions deleted.
1588                 $criteria = array('category' => $qcat->id);
1589                 $this->assertEquals(0, $DB->count_records('question', $criteria));
1590                 break;
1591             default:
1592                 break;
1593         }
1594     }
1596     /**
1597      * Test that triggering a course_created event works as expected.
1598      */
1599     public function test_course_created_event() {
1600         global $DB;
1602         $this->resetAfterTest();
1604         // Catch the events.
1605         $sink = $this->redirectEvents();
1607         // Create the course with an id number which is used later when generating a course via the imsenterprise plugin.
1608         $data = new stdClass();
1609         $data->idnumber = 'idnumber';
1610         $course = $this->getDataGenerator()->create_course($data);
1611         // Get course from DB for comparison.
1612         $course = $DB->get_record('course', array('id' => $course->id));
1614         // Capture the event.
1615         $events = $sink->get_events();
1616         $sink->close();
1618         // Validate the event.
1619         $event = $events[0];
1620         $this->assertInstanceOf('\core\event\course_created', $event);
1621         $this->assertEquals('course', $event->objecttable);
1622         $this->assertEquals($course->id, $event->objectid);
1623         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1624         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1625         $this->assertEquals('course_created', $event->get_legacy_eventname());
1626         $this->assertEventLegacyData($course, $event);
1627         $expectedlog = array(SITEID, 'course', 'new', 'view.php?id=' . $course->id, $course->fullname . ' (ID ' . $course->id . ')');
1628         $this->assertEventLegacyLogData($expectedlog, $event);
1630         // Now we want to trigger creating a course via the imsenterprise.
1631         // Delete the course we created earlier, as we want the imsenterprise plugin to create this.
1632         // We do not want print out any of the text this function generates while doing this, which is why
1633         // we are using ob_start() and ob_end_clean().
1634         ob_start();
1635         delete_course($course);
1636         ob_end_clean();
1638         // Create the XML file we want to use.
1639         $course->category = (array)$course->category;
1640         $imstestcase = new enrol_imsenterprise_testcase();
1641         $imstestcase->imsplugin = enrol_get_plugin('imsenterprise');
1642         $imstestcase->set_test_config();
1643         $imstestcase->set_xml_file(false, array($course));
1645         // Capture the event.
1646         $sink = $this->redirectEvents();
1647         $imstestcase->imsplugin->cron();
1648         $events = $sink->get_events();
1649         $sink->close();
1650         $event = null;
1651         foreach ($events as $eventinfo) {
1652             if ($eventinfo instanceof \core\event\course_created ) {
1653                 $event = $eventinfo;
1654                 break;
1655             }
1656         }
1658         // Validate the event triggered is \core\event\course_created. There is no need to validate the other values
1659         // as they have already been validated in the previous steps. Here we only want to make sure that when the
1660         // imsenterprise plugin creates a course an event is triggered.
1661         $this->assertInstanceOf('\core\event\course_created', $event);
1662         $this->assertEventContextNotUsed($event);
1663     }
1665     /**
1666      * Test that triggering a course_updated event works as expected.
1667      */
1668     public function test_course_updated_event() {
1669         global $DB;
1671         $this->resetAfterTest();
1673         // Create a course.
1674         $course = $this->getDataGenerator()->create_course();
1676         // Create a category we are going to move this course to.
1677         $category = $this->getDataGenerator()->create_category();
1679         // Create a hidden category we are going to move this course to.
1680         $categoryhidden = $this->getDataGenerator()->create_category(array('visible' => 0));
1682         // Update course and catch course_updated event.
1683         $sink = $this->redirectEvents();
1684         update_course($course);
1685         $events = $sink->get_events();
1686         $sink->close();
1688         // Get updated course information from the DB.
1689         $updatedcourse = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1690         // Validate event.
1691         $event = array_shift($events);
1692         $this->assertInstanceOf('\core\event\course_updated', $event);
1693         $this->assertEquals('course', $event->objecttable);
1694         $this->assertEquals($updatedcourse->id, $event->objectid);
1695         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1696         $url = new moodle_url('/course/edit.php', array('id' => $event->objectid));
1697         $this->assertEquals($url, $event->get_url());
1698         $this->assertEquals($updatedcourse, $event->get_record_snapshot('course', $event->objectid));
1699         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1700         $this->assertEventLegacyData($updatedcourse, $event);
1701         $expectedlog = array($updatedcourse->id, 'course', 'update', 'edit.php?id=' . $course->id, $course->id);
1702         $this->assertEventLegacyLogData($expectedlog, $event);
1704         // Move course and catch course_updated event.
1705         $sink = $this->redirectEvents();
1706         move_courses(array($course->id), $category->id);
1707         $events = $sink->get_events();
1708         $sink->close();
1710         // Return the moved course information from the DB.
1711         $movedcourse = $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($movedcourse->id, $event->objectid);
1717         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1718         $this->assertEquals($movedcourse, $event->get_record_snapshot('course', $movedcourse->id));
1719         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1720         $this->assertEventLegacyData($movedcourse, $event);
1721         $expectedlog = array($movedcourse->id, 'course', 'move', 'edit.php?id=' . $movedcourse->id, $movedcourse->id);
1722         $this->assertEventLegacyLogData($expectedlog, $event);
1724         // Move course to hidden category and catch course_updated event.
1725         $sink = $this->redirectEvents();
1726         move_courses(array($course->id), $categoryhidden->id);
1727         $events = $sink->get_events();
1728         $sink->close();
1730         // Return the moved course information from the DB.
1731         $movedcoursehidden = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1732         // Validate event.
1733         $event = array_shift($events);
1734         $this->assertInstanceOf('\core\event\course_updated', $event);
1735         $this->assertEquals('course', $event->objecttable);
1736         $this->assertEquals($movedcoursehidden->id, $event->objectid);
1737         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1738         $this->assertEquals($movedcoursehidden, $event->get_record_snapshot('course', $movedcoursehidden->id));
1739         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1740         $this->assertEventLegacyData($movedcoursehidden, $event);
1741         $expectedlog = array($movedcoursehidden->id, 'course', 'move', 'edit.php?id=' . $movedcoursehidden->id, $movedcoursehidden->id);
1742         $this->assertEventLegacyLogData($expectedlog, $event);
1743         $this->assertEventContextNotUsed($event);
1744     }
1746     /**
1747      * Test that triggering a course_deleted event works as expected.
1748      */
1749     public function test_course_deleted_event() {
1750         $this->resetAfterTest();
1752         // Create the course.
1753         $course = $this->getDataGenerator()->create_course();
1755         // Save the course context before we delete the course.
1756         $coursecontext = context_course::instance($course->id);
1758         // Catch the update event.
1759         $sink = $this->redirectEvents();
1761         // Call delete_course() which will trigger the course_deleted event and the course_content_deleted
1762         // event. This function prints out data to the screen, which we do not want during a PHPUnit test,
1763         // so use ob_start and ob_end_clean to prevent this.
1764         ob_start();
1765         delete_course($course);
1766         ob_end_clean();
1768         // Capture the event.
1769         $events = $sink->get_events();
1770         $sink->close();
1772         // Validate the event.
1773         $event = array_pop($events);
1774         $this->assertInstanceOf('\core\event\course_deleted', $event);
1775         $this->assertEquals('course', $event->objecttable);
1776         $this->assertEquals($course->id, $event->objectid);
1777         $this->assertEquals($coursecontext->id, $event->contextid);
1778         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1779         $this->assertEquals('course_deleted', $event->get_legacy_eventname());
1780         $eventdata = $event->get_data();
1781         $this->assertSame($course->idnumber, $eventdata['other']['idnumber']);
1782         $this->assertSame($course->fullname, $eventdata['other']['fullname']);
1783         $this->assertSame($course->shortname, $eventdata['other']['shortname']);
1785         // The legacy data also passed the context in the course object and substitutes timemodified with the current date.
1786         $expectedlegacy = clone($course);
1787         $expectedlegacy->context = $coursecontext;
1788         $expectedlegacy->timemodified = $event->timecreated;
1789         $this->assertEventLegacyData($expectedlegacy, $event);
1791         // Validate legacy log data.
1792         $expectedlog = array(SITEID, 'course', 'delete', 'view.php?id=' . $course->id, $course->fullname . '(ID ' . $course->id . ')');
1793         $this->assertEventLegacyLogData($expectedlog, $event);
1794         $this->assertEventContextNotUsed($event);
1795     }
1797     /**
1798      * Test that triggering a course_content_deleted event works as expected.
1799      */
1800     public function test_course_content_deleted_event() {
1801         global $DB;
1803         $this->resetAfterTest();
1805         // Create the course.
1806         $course = $this->getDataGenerator()->create_course();
1808         // Get the course from the DB. The data generator adds some extra properties, such as
1809         // numsections, to the course object which will fail the assertions later on.
1810         $course = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1812         // Save the course context before we delete the course.
1813         $coursecontext = context_course::instance($course->id);
1815         // Catch the update event.
1816         $sink = $this->redirectEvents();
1818         remove_course_contents($course->id, false);
1820         // Capture the event.
1821         $events = $sink->get_events();
1822         $sink->close();
1824         // Validate the event.
1825         $event = array_pop($events);
1826         $this->assertInstanceOf('\core\event\course_content_deleted', $event);
1827         $this->assertEquals('course', $event->objecttable);
1828         $this->assertEquals($course->id, $event->objectid);
1829         $this->assertEquals($coursecontext->id, $event->contextid);
1830         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1831         $this->assertEquals('course_content_removed', $event->get_legacy_eventname());
1832         // The legacy data also passed the context and options in the course object.
1833         $course->context = $coursecontext;
1834         $course->options = array();
1835         $this->assertEventLegacyData($course, $event);
1836         $this->assertEventContextNotUsed($event);
1837     }
1839     /**
1840      * Test that triggering a course_category_deleted event works as expected.
1841      */
1842     public function test_course_category_deleted_event() {
1843         $this->resetAfterTest();
1845         // Create a category.
1846         $category = $this->getDataGenerator()->create_category();
1848         // Save the context before it is deleted.
1849         $categorycontext = context_coursecat::instance($category->id);
1851         // Catch the update event.
1852         $sink = $this->redirectEvents();
1854         // Delete the category.
1855         $category->delete_full();
1857         // Capture the event.
1858         $events = $sink->get_events();
1859         $sink->close();
1861         // Validate the event.
1862         $event = $events[0];
1863         $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1864         $this->assertEquals('course_categories', $event->objecttable);
1865         $this->assertEquals($category->id, $event->objectid);
1866         $this->assertEquals($categorycontext->id, $event->contextid);
1867         $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1868         $this->assertEquals(null, $event->get_url());
1869         $this->assertEventLegacyData($category, $event);
1870         $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category->name . '(ID ' . $category->id . ')');
1871         $this->assertEventLegacyLogData($expectedlog, $event);
1873         // Create two categories.
1874         $category = $this->getDataGenerator()->create_category();
1875         $category2 = $this->getDataGenerator()->create_category();
1877         // Save the context before it is moved and then deleted.
1878         $category2context = context_coursecat::instance($category2->id);
1880         // Catch the update event.
1881         $sink = $this->redirectEvents();
1883         // Move the category.
1884         $category2->delete_move($category->id);
1886         // Capture the event.
1887         $events = $sink->get_events();
1888         $sink->close();
1890         // Validate the event.
1891         $event = $events[0];
1892         $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1893         $this->assertEquals('course_categories', $event->objecttable);
1894         $this->assertEquals($category2->id, $event->objectid);
1895         $this->assertEquals($category2context->id, $event->contextid);
1896         $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1897         $this->assertEventLegacyData($category2, $event);
1898         $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category2->name . '(ID ' . $category2->id . ')');
1899         $this->assertEventLegacyLogData($expectedlog, $event);
1900         $this->assertEventContextNotUsed($event);
1901     }
1903     /**
1904      * Test that triggering a course_restored event works as expected.
1905      */
1906     public function test_course_restored_event() {
1907         global $CFG;
1909         // Get the necessary files to perform backup and restore.
1910         require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
1911         require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
1913         $this->resetAfterTest();
1915         // Set to admin user.
1916         $this->setAdminUser();
1918         // The user id is going to be 2 since we are the admin user.
1919         $userid = 2;
1921         // Create a course.
1922         $course = $this->getDataGenerator()->create_course();
1924         // Create backup file and save it to the backup location.
1925         $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
1926             backup::INTERACTIVE_NO, backup::MODE_GENERAL, $userid);
1927         $bc->execute_plan();
1928         $results = $bc->get_results();
1929         $file = $results['backup_destination'];
1930         $fp = get_file_packer('application/vnd.moodle.backup');
1931         $filepath = $CFG->dataroot . '/temp/backup/test-restore-course-event';
1932         $file->extract_to_pathname($fp, $filepath);
1933         $bc->destroy();
1935         // Now we want to catch the restore course event.
1936         $sink = $this->redirectEvents();
1938         // Now restore the course to trigger the event.
1939         $rc = new restore_controller('test-restore-course-event', $course->id, backup::INTERACTIVE_NO,
1940             backup::MODE_GENERAL, $userid, backup::TARGET_NEW_COURSE);
1941         $rc->execute_precheck();
1942         $rc->execute_plan();
1944         // Capture the event.
1945         $events = $sink->get_events();
1946         $sink->close();
1948         // Validate the event.
1949         $event = array_pop($events);
1950         $this->assertInstanceOf('\core\event\course_restored', $event);
1951         $this->assertEquals('course', $event->objecttable);
1952         $this->assertEquals($rc->get_courseid(), $event->objectid);
1953         $this->assertEquals(context_course::instance($rc->get_courseid())->id, $event->contextid);
1954         $this->assertEquals('course_restored', $event->get_legacy_eventname());
1955         $legacydata = (object) array(
1956             'courseid' => $rc->get_courseid(),
1957             'userid' => $rc->get_userid(),
1958             'type' => $rc->get_type(),
1959             'target' => $rc->get_target(),
1960             'mode' => $rc->get_mode(),
1961             'operation' => $rc->get_operation(),
1962             'samesite' => $rc->is_samesite()
1963         );
1964         $url = new moodle_url('/course/view.php', array('id' => $event->objectid));
1965         $this->assertEquals($url, $event->get_url());
1966         $this->assertEventLegacyData($legacydata, $event);
1967         $this->assertEventContextNotUsed($event);
1969         // Destroy the resource controller since we are done using it.
1970         $rc->destroy();
1971     }
1973     /**
1974      * Test that triggering a course_section_updated event works as expected.
1975      */
1976     public function test_course_section_updated_event() {
1977         global $DB;
1979         $this->resetAfterTest();
1981         // Create the course with sections.
1982         $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true));
1983         $sections = $DB->get_records('course_sections', array('course' => $course->id));
1985         $coursecontext = context_course::instance($course->id);
1987         $section = array_pop($sections);
1988         $section->name = 'Test section';
1989         $section->summary = 'Test section summary';
1990         $DB->update_record('course_sections', $section);
1992         // Trigger an event for course section update.
1993         $event = \core\event\course_section_updated::create(
1994                 array(
1995                     'objectid' => $section->id,
1996                     'courseid' => $course->id,
1997                     'context' => context_course::instance($course->id),
1998                     'other' => array(
1999                         'sectionnum' => $section->section
2000                     )
2001                 )
2002             );
2003         $event->add_record_snapshot('course_sections', $section);
2004         // Trigger and catch event.
2005         $sink = $this->redirectEvents();
2006         $event->trigger();
2007         $events = $sink->get_events();
2008         $sink->close();
2010         // Validate the event.
2011         $event = $events[0];
2012         $this->assertInstanceOf('\core\event\course_section_updated', $event);
2013         $this->assertEquals('course_sections', $event->objecttable);
2014         $this->assertEquals($section->id, $event->objectid);
2015         $this->assertEquals($course->id, $event->courseid);
2016         $this->assertEquals($coursecontext->id, $event->contextid);
2017         $this->assertEquals($section->section, $event->other['sectionnum']);
2018         $expecteddesc = "The user with id '{$event->userid}' updated section number '{$event->other['sectionnum']}' for the course with id '{$event->courseid}'";
2019         $this->assertEquals($expecteddesc, $event->get_description());
2020         $url = new moodle_url('/course/editsection.php', array('id' => $event->objectid));
2021         $this->assertEquals($url, $event->get_url());
2022         $this->assertEquals($section, $event->get_record_snapshot('course_sections', $event->objectid));
2023         $id = $section->id;
2024         $sectionnum = $section->section;
2025         $expectedlegacydata = array($course->id, "course", "editsection", 'editsection.php?id=' . $id, $sectionnum);
2026         $this->assertEventLegacyLogData($expectedlegacydata, $event);
2027         $this->assertEventContextNotUsed($event);
2028     }
2030     /**
2031      * Test that triggering a course_section_deleted event works as expected.
2032      */
2033     public function test_course_section_deleted_event() {
2034         global $USER, $DB;
2035         $this->resetAfterTest();
2036         $sink = $this->redirectEvents();
2038         // Create the course with sections.
2039         $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true));
2040         $sections = $DB->get_records('course_sections', array('course' => $course->id), 'section');
2041         $coursecontext = context_course::instance($course->id);
2042         $section = array_pop($sections);
2043         course_delete_section($course, $section);
2044         $events = $sink->get_events();
2045         $event = array_pop($events); // Delete section event.
2046         $sink->close();
2048         // Validate event data.
2049         $this->assertInstanceOf('\core\event\course_section_deleted', $event);
2050         $this->assertEquals('course_sections', $event->objecttable);
2051         $this->assertEquals($section->id, $event->objectid);
2052         $this->assertEquals($course->id, $event->courseid);
2053         $this->assertEquals($coursecontext->id, $event->contextid);
2054         $this->assertEquals($section->section, $event->other['sectionnum']);
2055         $expecteddesc = "The user with id '{$event->userid}' deleted section number '{$event->other['sectionnum']}' " .
2056                 "(section name '{$event->other['sectionname']}') for the course with id '{$event->courseid}'";
2057         $this->assertEquals($expecteddesc, $event->get_description());
2058         $this->assertEquals($section, $event->get_record_snapshot('course_sections', $event->objectid));
2059         $this->assertNull($event->get_url());
2061         // Test legacy data.
2062         $sectionnum = $section->section;
2063         $expectedlegacydata = array($course->id, "course", "delete section", 'view.php?id=' . $course->id, $sectionnum);
2064         $this->assertEventLegacyLogData($expectedlegacydata, $event);
2065         $this->assertEventContextNotUsed($event);
2066     }
2068     public function test_course_integrity_check() {
2069         global $DB;
2071         $this->resetAfterTest(true);
2072         $course = $this->getDataGenerator()->create_course(array('numsections' => 1),
2073            array('createsections'=>true));
2075         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
2076                 array('section' => 0));
2077         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id),
2078                 array('section' => 0));
2079         $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id),
2080                 array('section' => 0));
2081         $correctseq = join(',', array($forum->cmid, $page->cmid, $quiz->cmid));
2083         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2084         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2085         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2086         $this->assertEquals($correctseq, $section0->sequence);
2087         $this->assertEmpty($section1->sequence);
2088         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2089         $this->assertEquals($section0->id, $cms[$page->cmid]->section);
2090         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2091         $this->assertEmpty(course_integrity_check($course->id));
2093         // Now let's make manual change in DB and let course_integrity_check() fix it:
2095         // 1. Module appears twice in one section.
2096         $DB->update_record('course_sections', array('id' => $section0->id, 'sequence' => $section0->sequence. ','. $page->cmid));
2097         $this->assertEquals(
2098                 array('Failed integrity check for course ['. $course->id.
2099                 ']. Sequence for course section ['. $section0->id. '] is "'.
2100                 $section0->sequence. ','. $page->cmid. '", must be "'.
2101                 $section0->sequence. '"'),
2102                 course_integrity_check($course->id));
2103         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2104         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2105         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2106         $this->assertEquals($correctseq, $section0->sequence);
2107         $this->assertEmpty($section1->sequence);
2108         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2109         $this->assertEquals($section0->id, $cms[$page->cmid]->section);
2110         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2112         // 2. Module appears in two sections (last section wins).
2113         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''. $page->cmid));
2114         // First message about double mentioning in sequence, second message about wrong section field for $page.
2115         $this->assertEquals(array(
2116             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
2117             '] must be removed from sequence of section ['. $section0->id.
2118             '] because it is also present in sequence of section ['. $section1->id. ']',
2119             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
2120             '] points to section ['. $section0->id. '] instead of ['. $section1->id. ']'),
2121                 course_integrity_check($course->id));
2122         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2123         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2124         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2125         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
2126         $this->assertEquals(''. $page->cmid, $section1->sequence);
2127         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2128         $this->assertEquals($section1->id, $cms[$page->cmid]->section);
2129         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2131         // 3. Module id is not present in course_section.sequence (integrity check with $fullcheck = false).
2132         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''));
2133         $this->assertEmpty(course_integrity_check($course->id)); // Not an error!
2134         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2135         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2136         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2137         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
2138         $this->assertEmpty($section1->sequence);
2139         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2140         $this->assertEquals($section1->id, $cms[$page->cmid]->section); // Not changed.
2141         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2143         // 4. Module id is not present in course_section.sequence (integrity check with $fullcheck = true).
2144         $this->assertEquals(array('Failed integrity check for course ['. $course->id. ']. Course module ['.
2145                 $page->cmid. '] is missing from sequence of section ['. $section1->id. ']'),
2146                 course_integrity_check($course->id, null, null, true)); // Error!
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, $section0->sequence);
2151         $this->assertEquals(''. $page->cmid, $section1->sequence);  // Yay, module added to section.
2152         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2153         $this->assertEquals($section1->id, $cms[$page->cmid]->section); // Not changed.
2154         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2156         // 5. Module id is not present in course_section.sequence and it's section is invalid (integrity check with $fullcheck = true).
2157         $DB->update_record('course_modules', array('id' => $page->cmid, 'section' => 8765));
2158         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''));
2159         $this->assertEquals(array(
2160             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
2161             '] is missing from sequence of section ['. $section0->id. ']',
2162             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
2163             '] points to section [8765] instead of ['. $section0->id. ']'),
2164                 course_integrity_check($course->id, null, null, true));
2165         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2166         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2167         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2168         $this->assertEquals($forum->cmid. ','. $quiz->cmid. ','. $page->cmid, $section0->sequence); // Module added to section.
2169         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2170         $this->assertEquals($section0->id, $cms[$page->cmid]->section); // Section changed to section0.
2171         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2173         // 6. Module is deleted from course_modules but not deleted in sequence (integrity check with $fullcheck = true).
2174         $DB->delete_records('course_modules', array('id' => $page->cmid));
2175         $this->assertEquals(array('Failed integrity check for course ['. $course->id. ']. Course module ['.
2176                 $page->cmid. '] does not exist but is present in the sequence of section ['. $section0->id. ']'),
2177                 course_integrity_check($course->id, null, null, true));
2178         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2179         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2180         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2181         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
2182         $this->assertEmpty($section1->sequence);
2183         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2184         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2185         $this->assertEquals(2, count($cms));
2186     }
2188     /**
2189      * Tests for event related to course module creation.
2190      */
2191     public function test_course_module_created_event() {
2192         global $USER, $DB;
2193         $this->resetAfterTest();
2195         // Create an assign module.
2196         $sink = $this->redirectEvents();
2197         $modinfo = $this->create_specific_module_test('assign');
2198         $events = $sink->get_events();
2199         $event = array_pop($events);
2201         $cm = get_coursemodule_from_id('assign', $modinfo->coursemodule, 0, false, MUST_EXIST);
2202         $mod = $DB->get_record('assign', array('id' => $modinfo->instance), '*', MUST_EXIST);
2204         // Validate event data.
2205         $this->assertInstanceOf('\core\event\course_module_created', $event);
2206         $this->assertEquals($cm->id, $event->objectid);
2207         $this->assertEquals($USER->id, $event->userid);
2208         $this->assertEquals('course_modules', $event->objecttable);
2209         $url = new moodle_url('/mod/assign/view.php', array('id' => $cm->id));
2210         $this->assertEquals($url, $event->get_url());
2212         // Test legacy data.
2213         $this->assertSame('mod_created', $event->get_legacy_eventname());
2214         $eventdata = new stdClass();
2215         $eventdata->modulename = 'assign';
2216         $eventdata->name       = $mod->name;
2217         $eventdata->cmid       = $cm->id;
2218         $eventdata->courseid   = $cm->course;
2219         $eventdata->userid     = $USER->id;
2220         $this->assertEventLegacyData($eventdata, $event);
2222         $arr = array(
2223             array($cm->course, "course", "add mod", "../mod/assign/view.php?id=$cm->id", "assign $cm->instance"),
2224             array($cm->course, "assign", "add", "view.php?id=$cm->id", $cm->instance, $cm->id)
2225         );
2226         $this->assertEventLegacyLogData($arr, $event);
2227         $this->assertEventContextNotUsed($event);
2229         // Let us see if duplicating an activity results in a nice course module created event.
2230         $sink->clear();
2231         $course = get_course($mod->course);
2232         $newcm = duplicate_module($course, $cm);
2233         $events = $sink->get_events();
2234         $event = array_pop($events);
2235         $sink->close();
2237         // Validate event data.
2238         $this->assertInstanceOf('\core\event\course_module_created', $event);
2239         $this->assertEquals($newcm->id, $event->objectid);
2240         $this->assertEquals($USER->id, $event->userid);
2241         $this->assertEquals($course->id, $event->courseid);
2242         $url = new moodle_url('/mod/assign/view.php', array('id' => $newcm->id));
2243         $this->assertEquals($url, $event->get_url());
2244     }
2246     /**
2247      * Tests for event validations related to course module creation.
2248      */
2249     public function test_course_module_created_event_exceptions() {
2251         $this->resetAfterTest();
2253         // Generate data.
2254         $modinfo = $this->create_specific_module_test('assign');
2255         $context = context_module::instance($modinfo->coursemodule);
2257         // Test not setting instanceid.
2258         try {
2259             $event = \core\event\course_module_created::create(array(
2260                 'courseid' => $modinfo->course,
2261                 'context'  => $context,
2262                 'objectid' => $modinfo->coursemodule,
2263                 'other'    => array(
2264                     'modulename' => 'assign',
2265                     'name'       => 'My assignment',
2266                 )
2267             ));
2268             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2269                     other['instanceid']");
2270         } catch (coding_exception $e) {
2271             $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
2272         }
2274         // Test not setting modulename.
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                     'instanceid' => $modinfo->instance,
2282                     'name'       => 'My assignment',
2283                 )
2284             ));
2285             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2286                     other['modulename']");
2287         } catch (coding_exception $e) {
2288             $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
2289         }
2291         // Test not setting name.
2293         try {
2294             $event = \core\event\course_module_created::create(array(
2295                 'courseid' => $modinfo->course,
2296                 'context'  => $context,
2297                 'objectid' => $modinfo->coursemodule,
2298                 'other'    => array(
2299                     'modulename' => 'assign',
2300                     'instanceid' => $modinfo->instance,
2301                 )
2302             ));
2303             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2304                     other['name']");
2305         } catch (coding_exception $e) {
2306             $this->assertContains("The 'name' value must be set in other.", $e->getMessage());
2307         }
2309     }
2311     /**
2312      * Tests for event related to course module updates.
2313      */
2314     public function test_course_module_updated_event() {
2315         global $USER, $DB;
2316         $this->resetAfterTest();
2318         // Update a forum module.
2319         $sink = $this->redirectEvents();
2320         $modinfo = $this->update_specific_module_test('forum');
2321         $events = $sink->get_events();
2322         $event = array_pop($events);
2323         $sink->close();
2325         $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
2326         $mod = $DB->get_record('forum', array('id' => $cm->instance), '*', MUST_EXIST);
2328         // Validate event data.
2329         $this->assertInstanceOf('\core\event\course_module_updated', $event);
2330         $this->assertEquals($cm->id, $event->objectid);
2331         $this->assertEquals($USER->id, $event->userid);
2332         $this->assertEquals('course_modules', $event->objecttable);
2333         $url = new moodle_url('/mod/forum/view.php', array('id' => $cm->id));
2334         $this->assertEquals($url, $event->get_url());
2336         // Test legacy data.
2337         $this->assertSame('mod_updated', $event->get_legacy_eventname());
2338         $eventdata = new stdClass();
2339         $eventdata->modulename = 'forum';
2340         $eventdata->name       = $mod->name;
2341         $eventdata->cmid       = $cm->id;
2342         $eventdata->courseid   = $cm->course;
2343         $eventdata->userid     = $USER->id;
2344         $this->assertEventLegacyData($eventdata, $event);
2346         $arr = array(
2347             array($cm->course, "course", "update mod", "../mod/forum/view.php?id=$cm->id", "forum $cm->instance"),
2348             array($cm->course, "forum", "update", "view.php?id=$cm->id", $cm->instance, $cm->id)
2349         );
2350         $this->assertEventLegacyLogData($arr, $event);
2351         $this->assertEventContextNotUsed($event);
2352     }
2354     /**
2355      * Tests for create_from_cm method.
2356      */
2357     public function test_course_module_create_from_cm() {
2358         $this->resetAfterTest();
2359         $this->setAdminUser();
2361         // Create course and modules.
2362         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
2364         // Generate an assignment.
2365         $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
2367         // Get the module context.
2368         $modcontext = context_module::instance($assign->cmid);
2370         // Get course module.
2371         $cm = get_coursemodule_from_id(null, $assign->cmid, $course->id, false, MUST_EXIST);
2373         // Create an event from course module.
2374         $event = \core\event\course_module_updated::create_from_cm($cm, $modcontext);
2376         // Trigger the events.
2377         $sink = $this->redirectEvents();
2378         $event->trigger();
2379         $events = $sink->get_events();
2380         $event2 = array_pop($events);
2382         // Test event data.
2383         $this->assertInstanceOf('\core\event\course_module_updated', $event);
2384         $this->assertEquals($cm->id, $event2->objectid);
2385         $this->assertEquals($modcontext, $event2->get_context());
2386         $this->assertEquals($cm->modname, $event2->other['modulename']);
2387         $this->assertEquals($cm->instance, $event2->other['instanceid']);
2388         $this->assertEquals($cm->name, $event2->other['name']);
2389         $this->assertEventContextNotUsed($event2);
2390         $this->assertSame('mod_updated', $event2->get_legacy_eventname());
2391         $arr = array(
2392             array($cm->course, "course", "update mod", "../mod/assign/view.php?id=$cm->id", "assign $cm->instance"),
2393             array($cm->course, "assign", "update", "view.php?id=$cm->id", $cm->instance, $cm->id)
2394         );
2395         $this->assertEventLegacyLogData($arr, $event);
2396     }
2398     /**
2399      * Tests for event validations related to course module update.
2400      */
2401     public function test_course_module_updated_event_exceptions() {
2403         $this->resetAfterTest();
2405         // Generate data.
2406         $modinfo = $this->create_specific_module_test('assign');
2407         $context = context_module::instance($modinfo->coursemodule);
2409         // Test not setting instanceid.
2410         try {
2411             $event = \core\event\course_module_updated::create(array(
2412                 'courseid' => $modinfo->course,
2413                 'context'  => $context,
2414                 'objectid' => $modinfo->coursemodule,
2415                 'other'    => array(
2416                     'modulename' => 'assign',
2417                     'name'       => 'My assignment',
2418                 )
2419             ));
2420             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2421                     other['instanceid']");
2422         } catch (coding_exception $e) {
2423             $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
2424         }
2426         // Test not setting modulename.
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                     'instanceid' => $modinfo->instance,
2434                     'name'       => 'My assignment',
2435                 )
2436             ));
2437             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2438                     other['modulename']");
2439         } catch (coding_exception $e) {
2440             $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
2441         }
2443         // Test not setting name.
2445         try {
2446             $event = \core\event\course_module_updated::create(array(
2447                 'courseid' => $modinfo->course,
2448                 'context'  => $context,
2449                 'objectid' => $modinfo->coursemodule,
2450                 'other'    => array(
2451                     'modulename' => 'assign',
2452                     'instanceid' => $modinfo->instance,
2453                 )
2454             ));
2455             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2456                     other['name']");
2457         } catch (coding_exception $e) {
2458             $this->assertContains("The 'name' value must be set in other.", $e->getMessage());
2459         }
2461     }
2463     /**
2464      * Tests for event related to course module delete.
2465      */
2466     public function test_course_module_deleted_event() {
2467         global $USER, $DB;
2468         $this->resetAfterTest();
2470         // Create and delete a module.
2471         $sink = $this->redirectEvents();
2472         $modinfo = $this->create_specific_module_test('forum');
2473         $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
2474         course_delete_module($modinfo->coursemodule);
2475         $events = $sink->get_events();
2476         $event = array_pop($events); // delete module event.;
2477         $sink->close();
2479         // Validate event data.
2480         $this->assertInstanceOf('\core\event\course_module_deleted', $event);
2481         $this->assertEquals($cm->id, $event->objectid);
2482         $this->assertEquals($USER->id, $event->userid);
2483         $this->assertEquals('course_modules', $event->objecttable);
2484         $this->assertEquals(null, $event->get_url());
2485         $this->assertEquals($cm, $event->get_record_snapshot('course_modules', $cm->id));
2487         // Test legacy data.
2488         $this->assertSame('mod_deleted', $event->get_legacy_eventname());
2489         $eventdata = new stdClass();
2490         $eventdata->modulename = 'forum';
2491         $eventdata->cmid       = $cm->id;
2492         $eventdata->courseid   = $cm->course;
2493         $eventdata->userid     = $USER->id;
2494         $this->assertEventLegacyData($eventdata, $event);
2496         $arr = array($cm->course, 'course', "delete mod", "view.php?id=$cm->course", "forum $cm->instance", $cm->id);
2497         $this->assertEventLegacyLogData($arr, $event);
2499     }
2501     /**
2502      * Tests for event validations related to course module deletion.
2503      */
2504     public function test_course_module_deleted_event_exceptions() {
2506         $this->resetAfterTest();
2508         // Generate data.
2509         $modinfo = $this->create_specific_module_test('assign');
2510         $context = context_module::instance($modinfo->coursemodule);
2512         // Test not setting instanceid.
2513         try {
2514             $event = \core\event\course_module_deleted::create(array(
2515                 'courseid' => $modinfo->course,
2516                 'context'  => $context,
2517                 'objectid' => $modinfo->coursemodule,
2518                 'other'    => array(
2519                     'modulename' => 'assign',
2520                     'name'       => 'My assignment',
2521                 )
2522             ));
2523             $this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
2524                     other['instanceid']");
2525         } catch (coding_exception $e) {
2526             $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
2527         }
2529         // Test not setting modulename.
2530         try {
2531             $event = \core\event\course_module_deleted::create(array(
2532                 'courseid' => $modinfo->course,
2533                 'context'  => $context,
2534                 'objectid' => $modinfo->coursemodule,
2535                 'other'    => array(
2536                     'instanceid' => $modinfo->instance,
2537                     'name'       => 'My assignment',
2538                 )
2539             ));
2540             $this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
2541                     other['modulename']");
2542         } catch (coding_exception $e) {
2543             $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
2544         }
2545     }
2547     /**
2548      * Returns a user object and its assigned new role.
2549      *
2550      * @param testing_data_generator $generator
2551      * @param $contextid
2552      * @return array The user object and the role ID
2553      */
2554     protected function get_user_objects(testing_data_generator $generator, $contextid) {
2555         global $USER;
2557         if (empty($USER->id)) {
2558             $user  = $generator->create_user();
2559             $this->setUser($user);
2560         }
2561         $roleid = create_role('Test role', 'testrole', 'Test role description');
2562         if (!is_array($contextid)) {
2563             $contextid = array($contextid);
2564         }
2565         foreach ($contextid as $cid) {
2566             $assignid = role_assign($roleid, $user->id, $cid);
2567         }
2568         return array($user, $roleid);
2569     }
2571     /**
2572      * Test course move after course.
2573      */
2574     public function test_course_change_sortorder_after_course() {
2575         global $DB;
2577         $this->resetAfterTest(true);
2579         $generator = $this->getDataGenerator();
2580         $category = $generator->create_category();
2581         $course3 = $generator->create_course(array('category' => $category->id));
2582         $course2 = $generator->create_course(array('category' => $category->id));
2583         $course1 = $generator->create_course(array('category' => $category->id));
2584         $context = $category->get_context();
2586         list($user, $roleid) = $this->get_user_objects($generator, $context->id);
2587         $caps = course_capability_assignment::allow('moodle/category:manage', $roleid, $context->id);
2589         $courses = $category->get_courses();
2590         $this->assertInternalType('array', $courses);
2591         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2592         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2593         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2595         // Test moving down.
2596         $this->assertTrue(course_change_sortorder_after_course($course1->id, $course3->id));
2597         $courses = $category->get_courses();
2598         $this->assertInternalType('array', $courses);
2599         $this->assertEquals(array($course2->id, $course3->id, $course1->id), array_keys($courses));
2600         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2601         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2603         // Test moving up.
2604         $this->assertTrue(course_change_sortorder_after_course($course1->id, $course2->id));
2605         $courses = $category->get_courses();
2606         $this->assertInternalType('array', $courses);
2607         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
2608         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2609         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2611         // Test moving to the top.
2612         $this->assertTrue(course_change_sortorder_after_course($course1->id, 0));
2613         $courses = $category->get_courses();
2614         $this->assertInternalType('array', $courses);
2615         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2616         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2617         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2618     }
2620     /**
2621      * Tests changing the visibility of a course.
2622      */
2623     public function test_course_change_visibility() {
2624         global $DB;
2626         $this->resetAfterTest(true);
2628         $generator = $this->getDataGenerator();
2629         $category = $generator->create_category();
2630         $course = $generator->create_course(array('category' => $category->id));
2632         $this->assertEquals('1', $course->visible);
2633         $this->assertEquals('1', $course->visibleold);
2635         $this->assertTrue(course_change_visibility($course->id, false));
2636         $course = $DB->get_record('course', array('id' => $course->id));
2637         $this->assertEquals('0', $course->visible);
2638         $this->assertEquals('0', $course->visibleold);
2640         $this->assertTrue(course_change_visibility($course->id, true));
2641         $course = $DB->get_record('course', array('id' => $course->id));
2642         $this->assertEquals('1', $course->visible);
2643         $this->assertEquals('1', $course->visibleold);
2644     }
2646     /**
2647      * Tests moving the course up and down by one.
2648      */
2649     public function test_course_change_sortorder_by_one() {
2650         global $DB;
2652         $this->resetAfterTest(true);
2654         $generator = $this->getDataGenerator();
2655         $category = $generator->create_category();
2656         $course3 = $generator->create_course(array('category' => $category->id));
2657         $course2 = $generator->create_course(array('category' => $category->id));
2658         $course1 = $generator->create_course(array('category' => $category->id));
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 down.
2667         $course1 = get_course($course1->id);
2668         $this->assertTrue(course_change_sortorder_by_one($course1, false));
2669         $courses = $category->get_courses();
2670         $this->assertInternalType('array', $courses);
2671         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
2672         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2673         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2675         // Test moving up.
2676         $course1 = get_course($course1->id);
2677         $this->assertTrue(course_change_sortorder_by_one($course1, true));
2678         $courses = $category->get_courses();
2679         $this->assertInternalType('array', $courses);
2680         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2681         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2682         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2684         // Test moving the top course up one.
2685         $course1 = get_course($course1->id);
2686         $this->assertFalse(course_change_sortorder_by_one($course1, true));
2687         // Check nothing changed.
2688         $courses = $category->get_courses();
2689         $this->assertInternalType('array', $courses);
2690         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2691         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2692         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2694         // Test moving the bottom course up down.
2695         $course3 = get_course($course3->id);
2696         $this->assertFalse(course_change_sortorder_by_one($course3, false));
2697         // Check nothing changed.
2698         $courses = $category->get_courses();
2699         $this->assertInternalType('array', $courses);
2700         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2701         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2702         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2703     }
2705     public function test_view_resources_list() {
2706         $this->resetAfterTest();
2708         $course = self::getDataGenerator()->create_course();
2709         $coursecontext = context_course::instance($course->id);
2711         $event = \core\event\course_resources_list_viewed::create(array('context' => context_course::instance($course->id)));
2712         $event->set_legacy_logdata(array('book', 'page', 'resource'));
2713         $sink = $this->redirectEvents();
2714         $event->trigger();
2715         $events = $sink->get_events();
2716         $sink->close();
2718         // Validate the event.
2719         $event = $events[0];
2720         $this->assertInstanceOf('\core\event\course_resources_list_viewed', $event);
2721         $this->assertEquals(null, $event->objecttable);
2722         $this->assertEquals(null, $event->objectid);
2723         $this->assertEquals($course->id, $event->courseid);
2724         $this->assertEquals($coursecontext->id, $event->contextid);
2725         $expectedlegacydata = array(
2726             array($course->id, "book", "view all", 'index.php?id=' . $course->id, ''),
2727             array($course->id, "page", "view all", 'index.php?id=' . $course->id, ''),
2728             array($course->id, "resource", "view all", 'index.php?id=' . $course->id, ''),
2729         );
2730         $this->assertEventLegacyLogData($expectedlegacydata, $event);
2731         $this->assertEventContextNotUsed($event);
2732     }
2734     /**
2735      * Test duplicate_module()
2736      */
2737     public function test_duplicate_module() {
2738         $this->setAdminUser();
2739         $this->resetAfterTest();
2740         $course = self::getDataGenerator()->create_course();
2741         $res = self::getDataGenerator()->create_module('resource', array('course' => $course));
2742         $cm = get_coursemodule_from_id('resource', $res->cmid, 0, false, MUST_EXIST);
2744         $newcm = duplicate_module($course, $cm);
2746         // Make sure they are the same, except obvious id changes.
2747         foreach ($cm as $prop => $value) {
2748             if ($prop == 'id' || $prop == 'url' || $prop == 'instance' || $prop == 'added') {
2749                 // Ignore obviously different properties.
2750                 continue;
2751             }
2752             $this->assertEquals($value, $newcm->$prop);
2753         }
2754     }
2756     /**
2757      * Tests that when creating or updating a module, if the availability settings
2758      * are present but set to an empty tree, availability is set to null in
2759      * database.
2760      */
2761     public function test_empty_availability_settings() {
2762         global $DB;
2763         $this->setAdminUser();
2764         $this->resetAfterTest();
2766         // Enable availability.
2767         set_config('enableavailability', 1);
2769         // Test add.
2770         $emptyavailability = json_encode(\core_availability\tree::get_root_json(array()));
2771         $course = self::getDataGenerator()->create_course();
2772         $label = self::getDataGenerator()->create_module('label', array(
2773                 'course' => $course, 'availability' => $emptyavailability));
2774         $this->assertNull($DB->get_field('course_modules', 'availability',
2775                 array('id' => $label->cmid)));
2777         // Test update.
2778         $formdata = $DB->get_record('course_modules', array('id' => $label->cmid));
2779         unset($formdata->availability);
2780         $formdata->availabilityconditionsjson = $emptyavailability;
2781         $formdata->modulename = 'label';
2782         $formdata->coursemodule = $label->cmid;
2783         $draftid = 0;
2784         file_prepare_draft_area($draftid, context_module::instance($label->cmid)->id,
2785                 'mod_label', 'intro', 0);
2786         $formdata->introeditor = array(
2787             'itemid' => $draftid,
2788             'text' => '<p>Yo</p>',
2789             'format' => FORMAT_HTML);
2790         update_module($formdata);
2791         $this->assertNull($DB->get_field('course_modules', 'availability',
2792                 array('id' => $label->cmid)));
2793     }
2795     /**
2796      * Test update_inplace_editable()
2797      */
2798     public function test_update_module_name_inplace() {
2799         global $CFG, $DB, $PAGE;
2800         require_once($CFG->dirroot . '/lib/external/externallib.php');
2802         $this->setUser($this->getDataGenerator()->create_user());
2804         $this->resetAfterTest(true);
2805         $course = $this->getDataGenerator()->create_course();
2806         $forum = self::getDataGenerator()->create_module('forum', array('course' => $course->id, 'name' => 'forum name'));
2808         // Call service for core_course component without necessary permissions.
2809         try {
2810             core_external::update_inplace_editable('core_course', 'activityname', $forum->cmid, 'New forum name');
2811             $this->fail('Exception expected');
2812         } catch (moodle_exception $e) {
2813             $this->assertEquals('Course or activity not accessible. (Not enrolled)',
2814                 $e->getMessage());
2815         }
2817         // Change to admin user and make sure that cm name can be updated using web service update_inplace_editable().
2818         $this->setAdminUser();
2819         $res = core_external::update_inplace_editable('core_course', 'activityname', $forum->cmid, 'New forum name');
2820         $res = external_api::clean_returnvalue(core_external::update_inplace_editable_returns(), $res);
2821         $this->assertEquals('New forum name', $res['value']);
2822         $this->assertEquals('New forum name', $DB->get_field('forum', 'name', array('id' => $forum->id)));
2823     }
2825     /**
2826      * Testing function course_get_tagged_course_modules - search tagged course modules
2827      */
2828     public function test_course_get_tagged_course_modules() {
2829         global $DB;
2830         $this->resetAfterTest();
2831         $course3 = $this->getDataGenerator()->create_course();
2832         $course2 = $this->getDataGenerator()->create_course();
2833         $course1 = $this->getDataGenerator()->create_course();
2834         $cm11 = $this->getDataGenerator()->create_module('assign', array('course' => $course1->id,
2835             'tags' => 'Cat, Dog'));
2836         $cm12 = $this->getDataGenerator()->create_module('page', array('course' => $course1->id,
2837             'tags' => 'Cat, Mouse', 'visible' => 0));
2838         $cm13 = $this->getDataGenerator()->create_module('page', array('course' => $course1->id,
2839             'tags' => 'Cat, Mouse, Dog'));
2840         $cm21 = $this->getDataGenerator()->create_module('forum', array('course' => $course2->id,
2841             'tags' => 'Cat, Mouse'));
2842         $cm31 = $this->getDataGenerator()->create_module('forum', array('course' => $course3->id,
2843             'tags' => 'Cat, Mouse'));
2845         // Admin is able to view everything.
2846         $this->setAdminUser();
2847         $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'),
2848                 /*$exclusivemode = */false, /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$page = */0);
2849         $this->assertRegExp('/'.$cm11->name.'/', $res->content);
2850         $this->assertRegExp('/'.$cm12->name.'/', $res->content);
2851         $this->assertRegExp('/'.$cm13->name.'/', $res->content);
2852         $this->assertRegExp('/'.$cm21->name.'/', $res->content);
2853         $this->assertRegExp('/'.$cm31->name.'/', $res->content);
2854         // Results from course1 are returned before results from course2.
2855         $this->assertTrue(strpos($res->content, $cm11->name) < strpos($res->content, $cm21->name));
2857         // Ordinary user is not able to see anything.
2858         $user = $this->getDataGenerator()->create_user();
2859         $this->setUser($user);
2861         $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'),
2862                 /*$exclusivemode = */false, /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$page = */0);
2863         $this->assertNull($res);
2865         // Enrol user as student in course1 and course2.
2866         $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
2867         $this->getDataGenerator()->enrol_user($user->id, $course1->id, $roleids['student']);
2868         $this->getDataGenerator()->enrol_user($user->id, $course2->id, $roleids['student']);
2869         core_tag_index_builder::reset_caches();
2871         // Searching in the course context returns visible modules in this course.
2872         $context = context_course::instance($course1->id);
2873         $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'),
2874                 /*$exclusivemode = */false, /*$fromctx = */0, /*$ctx = */$context->id, /*$rec = */1, /*$page = */0);
2875         $this->assertRegExp('/'.$cm11->name.'/', $res->content);
2876         $this->assertNotRegExp('/'.$cm12->name.'/', $res->content);
2877         $this->assertRegExp('/'.$cm13->name.'/', $res->content);
2878         $this->assertNotRegExp('/'.$cm21->name.'/', $res->content);
2879         $this->assertNotRegExp('/'.$cm31->name.'/', $res->content);
2881         // Searching FROM the course context returns visible modules in all courses.
2882         $context = context_course::instance($course2->id);
2883         $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'),
2884                 /*$exclusivemode = */false, /*$fromctx = */$context->id, /*$ctx = */0, /*$rec = */1, /*$page = */0);
2885         $this->assertRegExp('/'.$cm11->name.'/', $res->content);
2886         $this->assertNotRegExp('/'.$cm12->name.'/', $res->content);
2887         $this->assertRegExp('/'.$cm13->name.'/', $res->content);
2888         $this->assertRegExp('/'.$cm21->name.'/', $res->content);
2889         $this->assertNotRegExp('/'.$cm31->name.'/', $res->content); // No access to course3.
2890         // Results from course2 are returned before results from course1.
2891         $this->assertTrue(strpos($res->content, $cm21->name) < strpos($res->content, $cm11->name));
2893         // Enrol user in course1 as a teacher - now he should be able to see hidden module.
2894         $this->getDataGenerator()->enrol_user($user->id, $course1->id, $roleids['editingteacher']);
2895         get_fast_modinfo(0,0,true);
2897         $context = context_course::instance($course1->id);
2898         $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'),
2899                 /*$exclusivemode = */false, /*$fromctx = */$context->id, /*$ctx = */0, /*$rec = */1, /*$page = */0);
2900         $this->assertRegExp('/'.$cm12->name.'/', $res->content);
2902         // Create more modules and try pagination.
2903         $cm14 = $this->getDataGenerator()->create_module('assign', array('course' => $course1->id,
2904             'tags' => 'Cat, Dog'));
2905         $cm15 = $this->getDataGenerator()->create_module('page', array('course' => $course1->id,
2906             'tags' => 'Cat, Mouse', 'visible' => 0));
2907         $cm16 = $this->getDataGenerator()->create_module('page', array('course' => $course1->id,
2908             'tags' => 'Cat, Mouse, Dog'));
2910         $context = context_course::instance($course1->id);
2911         $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'),
2912                 /*$exclusivemode = */false, /*$fromctx = */0, /*$ctx = */$context->id, /*$rec = */1, /*$page = */0);
2913         $this->assertRegExp('/'.$cm11->name.'/', $res->content);
2914         $this->assertRegExp('/'.$cm12->name.'/', $res->content);
2915         $this->assertRegExp('/'.$cm13->name.'/', $res->content);
2916         $this->assertNotRegExp('/'.$cm21->name.'/', $res->content);
2917         $this->assertRegExp('/'.$cm14->name.'/', $res->content);
2918         $this->assertRegExp('/'.$cm15->name.'/', $res->content);
2919         $this->assertNotRegExp('/'.$cm16->name.'/', $res->content);
2920         $this->assertNotRegExp('/'.$cm31->name.'/', $res->content); // No access to course3.
2921         $this->assertEmpty($res->prevpageurl);
2922         $this->assertNotEmpty($res->nextpageurl);
2924         $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'),
2925                 /*$exclusivemode = */false, /*$fromctx = */0, /*$ctx = */$context->id, /*$rec = */1, /*$page = */1);
2926         $this->assertNotRegExp('/'.$cm11->name.'/', $res->content);
2927         $this->assertNotRegExp('/'.$cm12->name.'/', $res->content);
2928         $this->assertNotRegExp('/'.$cm13->name.'/', $res->content);
2929         $this->assertNotRegExp('/'.$cm21->name.'/', $res->content);
2930         $this->assertNotRegExp('/'.$cm14->name.'/', $res->content);
2931         $this->assertNotRegExp('/'.$cm15->name.'/', $res->content);
2932         $this->assertRegExp('/'.$cm16->name.'/', $res->content);
2933         $this->assertNotRegExp('/'.$cm31->name.'/', $res->content); // No access to course3.
2934         $this->assertNotEmpty($res->prevpageurl);
2935         $this->assertEmpty($res->nextpageurl);
2936     }
2938     /**
2939      * Test course_get_user_navigation_options for frontpage.
2940      */
2941     public function test_course_get_user_navigation_options_for_frontpage() {
2942         global $CFG, $SITE, $DB;
2943         $this->resetAfterTest();
2944         $context = context_system::instance();
2945         $course = clone $SITE;
2946         $this->setAdminUser();
2948         $navoptions = course_get_user_navigation_options($context, $course);
2949         $this->assertTrue($navoptions->blogs);
2950         $this->assertTrue($navoptions->notes);
2951         $this->assertTrue($navoptions->participants);
2952         $this->assertTrue($navoptions->badges);
2953         $this->assertTrue($navoptions->tags);
2954         $this->assertFalse($navoptions->search);
2955         $this->assertTrue($navoptions->calendar);
2956         $this->assertTrue($navoptions->competencies);
2958         // Enable global search now.
2959         $CFG->enableglobalsearch = 1;
2960         $navoptions = course_get_user_navigation_options($context, $course);
2961         $this->assertTrue($navoptions->search);
2963         // Disable competencies.
2964         $oldcompetencies = get_config('core_competency', 'enabled');
2965         set_config('enabled', false, 'core_competency');
2966         $navoptions = course_get_user_navigation_options($context, $course);
2967         $this->assertFalse($navoptions->competencies);
2968         set_config('enabled', $oldcompetencies, 'core_competency');
2970         // Now try with a standard user.
2971         $user = $this->getDataGenerator()->create_user();
2972         $this->setUser($user);
2973         $navoptions = course_get_user_navigation_options($context, $course);
2974         $this->assertTrue($navoptions->blogs);
2975         $this->assertFalse($navoptions->notes);
2976         $this->assertFalse($navoptions->participants);
2977         $this->assertTrue($navoptions->badges);
2978         $this->assertTrue($navoptions->tags);
2979         $this->assertTrue($navoptions->search);
2980         $this->assertTrue($navoptions->calendar);
2982         // Standar using viewing frontpage settings from a course where is enrolled.
2983         $course = self::getDataGenerator()->create_course();
2984         // Create a viewer user.
2985         $viewer = self::getDataGenerator()->create_user();
2986         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
2987         $this->getDataGenerator()->enrol_user($viewer->id, $course->id, $studentrole->id);
2988         $this->setUser($viewer);
2990         $navoptions = course_get_user_navigation_options($context, $course);
2991         $this->assertTrue($navoptions->blogs);
2992         $this->assertFalse($navoptions->notes);
2993         $this->assertTrue($navoptions->participants);
2994         $this->assertTrue($navoptions->badges);
2995         $this->assertTrue($navoptions->tags);
2996         $this->assertTrue($navoptions->search);
2997         $this->assertTrue($navoptions->calendar);
2998     }
3000     /**
3001      * Test course_get_user_navigation_options for managers in a normal course.
3002      */
3003     public function test_course_get_user_navigation_options_for_managers() {
3004         global $CFG;
3005         $this->resetAfterTest();
3006         $course = $this->getDataGenerator()->create_course();
3007         $context = context_course::instance($course->id);
3008         $this->setAdminUser();
3010         $navoptions = course_get_user_navigation_options($context);
3011         $this->assertTrue($navoptions->blogs);
3012         $this->assertTrue($navoptions->notes);
3013         $this->assertTrue($navoptions->participants);
3014         $this->assertTrue($navoptions->badges);
3015     }
3017     /**
3018      * Test course_get_user_navigation_options for students in a normal course.
3019      */
3020     public function test_course_get_user_navigation_options_for_students() {
3021         global $DB, $CFG;
3022         $this->resetAfterTest();
3023         $course = $this->getDataGenerator()->create_course();
3024         $context = context_course::instance($course->id);
3026         $user = $this->getDataGenerator()->create_user();
3027         $roleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
3028         $this->getDataGenerator()->enrol_user($user->id, $course->id, $roleid);
3030         $this->setUser($user);
3032         $navoptions = course_get_user_navigation_options($context);
3033         $this->assertTrue($navoptions->blogs);
3034         $this->assertFalse($navoptions->notes);
3035         $this->assertTrue($navoptions->participants);
3036         $this->assertTrue($navoptions->badges);
3038         // Disable some options.
3039         $CFG->badges_allowcoursebadges = 0;
3040         $CFG->enableblogs = 0;
3041         // Disable view participants capability.
3042         assign_capability('moodle/course:viewparticipants', CAP_PROHIBIT, $roleid, $context);
3043         $context->mark_dirty();
3045         $navoptions = course_get_user_navigation_options($context);
3046         $this->assertFalse($navoptions->blogs);
3047         $this->assertFalse($navoptions->notes);
3048         $this->assertFalse($navoptions->participants);
3049         $this->assertFalse($navoptions->badges);
3050     }
3052     /**
3053      * Test course_get_user_administration_options for frontpage.
3054      */
3055     public function test_course_get_user_administration_options_for_frontpage() {
3056         global $CFG, $SITE;
3057         $this->resetAfterTest();
3058         $course = clone $SITE;
3059         $context = context_course::instance($course->id);
3060         $this->setAdminUser();
3062         $adminoptions = course_get_user_administration_options($course, $context);
3063         $this->assertTrue($adminoptions->update);
3064         $this->assertTrue($adminoptions->filters);
3065         $this->assertTrue($adminoptions->reports);
3066         $this->assertTrue($adminoptions->backup);
3067         $this->assertTrue($adminoptions->restore);
3068         $this->assertFalse($adminoptions->files);
3069         $this->assertFalse($adminoptions->tags);
3071         // Now try with a standard user.
3072         $user = $this->getDataGenerator()->create_user();
3073         $this->setUser($user);
3074         $adminoptions = course_get_user_administration_options($course, $context);
3075         $this->assertFalse($adminoptions->update);
3076         $this->assertFalse($adminoptions->filters);
3077         $this->assertFalse($adminoptions->reports);
3078         $this->assertFalse($adminoptions->backup);
3079         $this->assertFalse($adminoptions->restore);
3080         $this->assertFalse($adminoptions->files);
3081         $this->assertFalse($adminoptions->tags);
3083     }
3085     /**
3086      * Test course_get_user_administration_options for managers in a normal course.
3087      */
3088     public function test_course_get_user_administration_options_for_managers() {
3089         global $CFG;
3090         $this->resetAfterTest();
3091         $course = $this->getDataGenerator()->create_course();
3092         $context = context_course::instance($course->id);
3093         $this->setAdminUser();
3095         $adminoptions = course_get_user_administration_options($course, $context);
3096         $this->assertTrue($adminoptions->update);
3097         $this->assertTrue($adminoptions->filters);
3098         $this->assertTrue($adminoptions->reports);
3099         $this->assertTrue($adminoptions->backup);
3100         $this->assertTrue($adminoptions->restore);
3101         $this->assertFalse($adminoptions->files);
3102         $this->assertTrue($adminoptions->tags);
3103         $this->assertTrue($adminoptions->gradebook);
3104         $this->assertFalse($adminoptions->outcomes);
3105         $this->assertTrue($adminoptions->badges);
3106         $this->assertTrue($adminoptions->import);
3107         $this->assertTrue($adminoptions->publish);
3108         $this->assertTrue($adminoptions->reset);
3109         $this->assertTrue($adminoptions->roles);
3110     }
3112     /**
3113      * Test course_get_user_administration_options for students in a normal course.
3114      */
3115     public function test_course_get_user_administration_options_for_students() {
3116         global $DB, $CFG;
3117         $this->resetAfterTest();
3118         $course = $this->getDataGenerator()->create_course();
3119         $context = context_course::instance($course->id);
3121         $user = $this->getDataGenerator()->create_user();
3122         $roleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
3123         $this->getDataGenerator()->enrol_user($user->id, $course->id, $roleid);
3125         $this->setUser($user);
3126         $adminoptions = course_get_user_administration_options($course, $context);
3128         $this->assertFalse($adminoptions->update);
3129         $this->assertFalse($adminoptions->filters);
3130         $this->assertFalse($adminoptions->reports);
3131         $this->assertFalse($adminoptions->backup);
3132         $this->assertFalse($adminoptions->restore);
3133         $this->assertFalse($adminoptions->files);
3134         $this->assertFalse($adminoptions->tags);
3135         $this->assertFalse($adminoptions->gradebook);
3136         $this->assertFalse($adminoptions->outcomes);
3137         $this->assertTrue($adminoptions->badges);
3138         $this->assertFalse($adminoptions->import);
3139         $this->assertFalse($adminoptions->publish);
3140         $this->assertFalse($adminoptions->reset);
3141         $this->assertFalse($adminoptions->roles);
3143         $CFG->enablebadges = false;
3144         $adminoptions = course_get_user_administration_options($course, $context);
3145         $this->assertFalse($adminoptions->badges);
3146     }
3148     /**
3149      * Test test_update_course_frontpage_category.
3150      */
3151     public function test_update_course_frontpage_category() {
3152         // Fetch front page course.
3153         $course = get_course(SITEID);
3154         // Test update information on front page course.
3155         $course->category = 99;
3156         $this->expectException('moodle_exception');
3157         $this->expectExceptionMessage(get_string('invalidcourse', 'error'));
3158         update_course($course);
3159     }
3161     /**
3162      * test_course_enddate
3163      *
3164      * @dataProvider course_enddate_provider
3165      * @param int $startdate
3166      * @param int $enddate
3167      * @param string $errorcode
3168      */
3169     public function test_course_enddate($startdate, $enddate, $errorcode) {
3171         $this->resetAfterTest(true);
3173         $record = array('startdate' => $startdate, 'enddate' => $enddate);
3174         try {
3175             $course1 = $this->getDataGenerator()->create_course($record);
3176             if ($errorcode !== false) {
3177                 $this->fail('Expected exception with "' . $errorcode . '" error code in create_create');
3178             }
3179         } catch (moodle_exception $e) {
3180             if ($errorcode === false) {
3181                 $this->fail('Got "' . $errorcode . '" exception error code and no exception was expected');
3182             }
3183             if ($e->errorcode != $errorcode) {
3184                 $this->fail('Got "' . $e->errorcode. '" exception error code and "' . $errorcode . '" was expected');
3185             }
3186             return;
3187         }
3189         $this->assertEquals($startdate, $course1->startdate);
3190         $this->assertEquals($enddate, $course1->enddate);
3191     }
3193     /**
3194      * Provider for test_course_enddate.
3195      *
3196      * @return array
3197      */
3198     public function course_enddate_provider() {
3199         // Each provided example contains startdate, enddate and the expected exception error code if there is any.
3200         return [
3201             [
3202                 111,
3203                 222,
3204                 false
3205             ], [
3206                 222,
3207                 111,
3208                 'enddatebeforestartdate'
3209             ], [
3210                 111,
3211                 0,
3212                 false
3213             ], [
3214                 0,
3215                 222,
3216                 'nostartdatenoenddate'
3217             ]
3218         ];
3219     }
3222     /**
3223      * test_course_dates_reset
3224      *
3225      * @dataProvider course_dates_reset_provider
3226      * @param int $startdate
3227      * @param int $enddate
3228      * @param int $resetstartdate
3229      * @param int $resetenddate
3230      * @param int $resultingstartdate
3231      * @param int $resultingenddate
3232      */
3233     public function test_course_dates_reset($startdate, $enddate, $resetstartdate, $resetenddate, $resultingstartdate, $resultingenddate) {
3234         global $CFG, $DB;
3236         require_once($CFG->dirroot.'/completion/criteria/completion_criteria_date.php');
3238         $this->resetAfterTest(true);
3240         $this->setAdminUser();
3242         $CFG->enablecompletion = true;
3244         $this->setTimezone('UTC');
3246         $record = array('startdate' => $startdate, 'enddate' => $enddate, 'enablecompletion' => 1);
3247         $originalcourse = $this->getDataGenerator()->create_course($record);
3248         $coursecriteria = new completion_criteria_date(array('course' => $originalcourse->id, 'timeend' => $startdate + DAYSECS));
3249         $coursecriteria->insert();
3251         $activitycompletiondate = $startdate + DAYSECS;
3252         $data = $this->getDataGenerator()->create_module('data', array('course' => $originalcourse->id),
3253                         array('completion' => 1, 'completionexpected' => $activitycompletiondate));
3255         $resetdata = new stdClass();
3256         $resetdata->id = $originalcourse->id;
3257         $resetdata->reset_start_date_old = $originalcourse->startdate;
3258         $resetdata->reset_start_date = $resetstartdate;
3259         $resetdata->reset_end_date = $resetenddate;
3260         $resetdata->reset_end_date_old = $record['enddate'];
3261         reset_course_userdata($resetdata);
3263         $course = $DB->get_record('course', array('id' => $originalcourse->id));
3265         $this->assertEquals($resultingstartdate, $course->startdate);
3266         $this->assertEquals($resultingenddate, $course->enddate);
3268         $coursecompletioncriteria = completion_criteria_date::fetch(array('course' => $originalcourse->id));
3269         $this->assertEquals($resultingstartdate + DAYSECS, $coursecompletioncriteria->timeend);
3271         $this->assertEquals($resultingstartdate + DAYSECS, $DB->get_field('course_modules', 'completionexpected',
3272             array('id' => $data->cmid)));
3273     }
3275     /**