7ab3925187133164172fe73a4b1aad8657306b7e
[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->allowsubmissionsfromdate = time();
101         $moduleinfo->teamsubmission = true;
102         $moduleinfo->requireallteammemberssubmit = true;
103         $moduleinfo->teamsubmissiongroupingid = true;
104         $moduleinfo->blindmarking = true;
105         $moduleinfo->markingworkflow = true;
106         $moduleinfo->markingallocation = true;
107         $moduleinfo->assignsubmission_onlinetext_enabled = true;
108         $moduleinfo->assignsubmission_file_enabled = true;
109         $moduleinfo->assignsubmission_file_maxfiles = 1;
110         $moduleinfo->assignsubmission_file_maxsizebytes = 1000000;
111         $moduleinfo->assignsubmission_comments_enabled = true;
112         $moduleinfo->assignfeedback_comments_enabled = true;
113         $moduleinfo->assignfeedback_offline_enabled = true;
114         $moduleinfo->assignfeedback_file_enabled = true;
116         // Advanced grading.
117         $gradingmethods = grading_manager::available_methods();
118         $moduleinfo->advancedgradingmethod_submissions = current(array_keys($gradingmethods));
119     }
121     /**
122      * Execute test asserts on the saved DB data by create_module($assign).
123      *
124      * @param object $moduleinfo - the specific assign module values that were used to create an assign module.
125      * @param object $dbmodinstance - the DB values of the created assign module.
126      */
127     private function assign_create_run_asserts($moduleinfo, $dbmodinstance) {
128         global $DB;
130         $this->assertEquals($moduleinfo->alwaysshowdescription, $dbmodinstance->alwaysshowdescription);
131         $this->assertEquals($moduleinfo->submissiondrafts, $dbmodinstance->submissiondrafts);
132         $this->assertEquals($moduleinfo->requiresubmissionstatement, $dbmodinstance->requiresubmissionstatement);
133         $this->assertEquals($moduleinfo->sendnotifications, $dbmodinstance->sendnotifications);
134         $this->assertEquals($moduleinfo->duedate, $dbmodinstance->duedate);
135         $this->assertEquals($moduleinfo->cutoffdate, $dbmodinstance->cutoffdate);
136         $this->assertEquals($moduleinfo->allowsubmissionsfromdate, $dbmodinstance->allowsubmissionsfromdate);
137         $this->assertEquals($moduleinfo->teamsubmission, $dbmodinstance->teamsubmission);
138         $this->assertEquals($moduleinfo->requireallteammemberssubmit, $dbmodinstance->requireallteammemberssubmit);
139         $this->assertEquals($moduleinfo->teamsubmissiongroupingid, $dbmodinstance->teamsubmissiongroupingid);
140         $this->assertEquals($moduleinfo->blindmarking, $dbmodinstance->blindmarking);
141         $this->assertEquals($moduleinfo->markingworkflow, $dbmodinstance->markingworkflow);
142         $this->assertEquals($moduleinfo->markingallocation, $dbmodinstance->markingallocation);
143         // The goal not being to fully test assign_add_instance() we'll stop here for the assign tests - to avoid too many DB queries.
145         // Advanced grading.
146         $cm = get_coursemodule_from_instance('assign', $dbmodinstance->id);
147         $contextmodule = context_module::instance($cm->id);
148         $advancedgradingmethod = $DB->get_record('grading_areas',
149             array('contextid' => $contextmodule->id,
150                 'activemethod' => $moduleinfo->advancedgradingmethod_submissions));
151         $this->assertEquals($moduleinfo->advancedgradingmethod_submissions, $advancedgradingmethod);
152     }
154     /**
155      * Run some asserts test for a specific module for the function create_module().
156      *
157      * The function has been created (and is called) for $this->test_create_module().
158      * Note that the call to MODULE_create_set_values and MODULE_create_run_asserts are done after the common set values/run asserts.
159      * So if you want, you can overwrite the default values/asserts in the respective functions.
160      * @param string $modulename Name of the module ('forum', 'assign', 'book'...).
161      */
162     private function create_specific_module_test($modulename) {
163         global $DB, $CFG;
165         $this->resetAfterTest(true);
167         $this->setAdminUser();
169         // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
170         require_once($CFG->dirroot.'/mod/'. $modulename .'/lib.php');
172         // Enable avaibility.
173         // If not enabled all conditional fields will be ignored.
174         set_config('enableavailability', 1);
176         // Enable course completion.
177         // If not enabled all completion settings will be ignored.
178         set_config('enablecompletion', COMPLETION_ENABLED);
180         // Enable forum RSS feeds.
181         set_config('enablerssfeeds', 1);
182         set_config('forum_enablerssfeeds', 1);
184         $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED),
185            array('createsections'=>true));
187         $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
189         // Create assign module instance for test.
190         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
191         $params['course'] = $course->id;
192         $instance = $generator->create_instance($params);
193         $assigncm = get_coursemodule_from_instance('assign', $instance->id);
195         // Module test values.
196         $moduleinfo = new stdClass();
198         // Always mandatory generic values to any module.
199         $moduleinfo->modulename = $modulename;
200         $moduleinfo->section = 1; // This is the section number in the course. Not the section id in the database.
201         $moduleinfo->course = $course->id;
202         $moduleinfo->groupingid = $grouping->id;
203         $moduleinfo->visible = true;
205         // Sometimes optional generic values for some modules.
206         $moduleinfo->name = 'My test module';
207         $moduleinfo->showdescription = 1; // standard boolean
208         require_once($CFG->libdir . '/gradelib.php');
209         $gradecats = grade_get_categories_menu($moduleinfo->course, false);
210         $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
211         $moduleinfo->gradecat = $gradecatid;
212         $moduleinfo->groupmode = VISIBLEGROUPS;
213         $moduleinfo->cmidnumber = 'idnumber_XXX';
215         // Completion common to all module.
216         $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC;
217         $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED;
218         $moduleinfo->completiongradeitemnumber = 1;
219         $moduleinfo->completionexpected = time() + (7 * 24 * 3600);
221         // Conditional activity.
222         $moduleinfo->availability = '{"op":"&","showc":[true,true],"c":[' .
223                 '{"type":"date","d":">=","t":' . time() . '},' .
224                 '{"type":"date","d":"<","t":' . (time() + (7 * 24 * 3600)) . '}' .
225                 ']}';
226         $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself.
227         $moduleinfo->conditiongradegroup = array(array('conditiongradeitemid' => $coursegradeitem->id, 'conditiongrademin' => 10, 'conditiongrademax' => 80));
228         $moduleinfo->conditionfieldgroup = array(array('conditionfield' => 'email', 'conditionfieldoperator' => \availability_profile\condition::OP_CONTAINS, 'conditionfieldvalue' => '@'));
229         $moduleinfo->conditioncompletiongroup = array(array('conditionsourcecmid' => $assigncm->id, 'conditionrequiredcompletion' => COMPLETION_COMPLETE)); // "conditionsourcecmid == 0" => none
231         // Grading and Advanced grading.
232         require_once($CFG->dirroot . '/rating/lib.php');
233         $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE;
234         $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number.
235         $moduleinfo->assesstimestart = time();
236         $moduleinfo->assesstimefinish = time() + (7 * 24 * 3600);
238         // RSS.
239         $moduleinfo->rsstype = 2;
240         $moduleinfo->rssarticles = 10;
242         // Optional intro editor (depends of module).
243         $draftid_editor = 0;
244         file_prepare_draft_area($draftid_editor, null, null, null, null);
245         $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
247         // Following is the advanced grading method area called 'submissions' for the 'assign' module.
248         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
249             $moduleinfo->grade = 100;
250         }
252         // Plagiarism form values.
253         // No plagiarism plugin installed by default. Use this space to make your own test.
255         // Values specific to the module.
256         $modulesetvalues = $modulename.'_create_set_values';
257         $this->$modulesetvalues($moduleinfo);
259         // Create the module.
260         $result = create_module($moduleinfo);
262         // Retrieve the module info.
263         $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance));
264         $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance);
265         // We passed the course section number to create_courses but $dbcm contain the section id.
266         // We need to retrieve the db course section number.
267         $section = $DB->get_record('course_sections', array('course' => $dbcm->course, 'id' => $dbcm->section));
268         // Retrieve the grade item.
269         $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course,
270             'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename));
272         // Compare the values common to all module instances.
273         $this->assertEquals($moduleinfo->modulename, $dbcm->modname);
274         $this->assertEquals($moduleinfo->section, $section->section);
275         $this->assertEquals($moduleinfo->course, $dbcm->course);
276         $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
277         $this->assertEquals($moduleinfo->visible, $dbcm->visible);
278         $this->assertEquals($moduleinfo->completion, $dbcm->completion);
279         $this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
280         $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber);
281         $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected);
282         $this->assertEquals($moduleinfo->availability, $dbcm->availability);
283         $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription);
284         $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode);
285         $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber);
286         $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid);
288         // Optional grade testing.
289         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
290             $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade);
291         }
293         // Some optional (but quite common) to some module.
294         $this->assertEquals($moduleinfo->name, $dbmodinstance->name);
295         $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro);
296         $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat);
298         // Test specific to the module.
299         $modulerunasserts = $modulename.'_create_run_asserts';
300         $this->$modulerunasserts($moduleinfo, $dbmodinstance);
301         return $moduleinfo;
302     }
304     /**
305      * Test create_module() for multiple modules defined in the $modules array (first declaration of the function).
306      */
307     public function test_create_module() {
308         // Add the module name you want to test here.
309         // Create the match MODULENAME_create_set_values() and MODULENAME_create_run_asserts().
310         $modules = array('forum', 'assign');
311         // Run all tests.
312         foreach ($modules as $modulename) {
313             $this->create_specific_module_test($modulename);
314         }
315     }
317     /**
318      * Test update_module() for multiple modules defined in the $modules array (first declaration of the function).
319      */
320     public function test_update_module() {
321         // Add the module name you want to test here.
322         // Create the match MODULENAME_update_set_values() and MODULENAME_update_run_asserts().
323         $modules = array('forum');
324         // Run all tests.
325         foreach ($modules as $modulename) {
326             $this->update_specific_module_test($modulename);
327         }
328     }
330     /**
331      * Set forum specific test values for calling update_module().
332      *
333      * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
334      */
335     private function forum_update_set_values(&$moduleinfo) {
336         // Completion specific to forum - optional.
337         $moduleinfo->completionposts = 3;
338         $moduleinfo->completiondiscussions = 1;
339         $moduleinfo->completionreplies = 2;
341         // Specific values to the Forum module.
342         $moduleinfo->forcesubscribe = FORUM_INITIALSUBSCRIBE;
343         $moduleinfo->type = 'single';
344         $moduleinfo->trackingtype = FORUM_TRACKING_FORCED;
345         $moduleinfo->maxbytes = 10240;
346         $moduleinfo->maxattachments = 2;
348         // Post threshold for blocking - specific to forum.
349         $moduleinfo->blockperiod = 60*60*24;
350         $moduleinfo->blockafter = 10;
351         $moduleinfo->warnafter = 5;
352     }
354     /**
355      * Execute test asserts on the saved DB data by update_module($forum).
356      *
357      * @param object $moduleinfo - the specific forum values that were used to update a forum.
358      * @param object $dbmodinstance - the DB values of the updated forum.
359      */
360     private function forum_update_run_asserts($moduleinfo, $dbmodinstance) {
361         // Compare values specific to forums.
362         $this->assertEquals($moduleinfo->forcesubscribe, $dbmodinstance->forcesubscribe);
363         $this->assertEquals($moduleinfo->type, $dbmodinstance->type);
364         $this->assertEquals($moduleinfo->assessed, $dbmodinstance->assessed);
365         $this->assertEquals($moduleinfo->completionposts, $dbmodinstance->completionposts);
366         $this->assertEquals($moduleinfo->completiondiscussions, $dbmodinstance->completiondiscussions);
367         $this->assertEquals($moduleinfo->completionreplies, $dbmodinstance->completionreplies);
368         $this->assertEquals($moduleinfo->scale, $dbmodinstance->scale);
369         $this->assertEquals($moduleinfo->assesstimestart, $dbmodinstance->assesstimestart);
370         $this->assertEquals($moduleinfo->assesstimefinish, $dbmodinstance->assesstimefinish);
371         $this->assertEquals($moduleinfo->rsstype, $dbmodinstance->rsstype);
372         $this->assertEquals($moduleinfo->rssarticles, $dbmodinstance->rssarticles);
373         $this->assertEquals($moduleinfo->trackingtype, $dbmodinstance->trackingtype);
374         $this->assertEquals($moduleinfo->maxbytes, $dbmodinstance->maxbytes);
375         $this->assertEquals($moduleinfo->maxattachments, $dbmodinstance->maxattachments);
376         $this->assertEquals($moduleinfo->blockperiod, $dbmodinstance->blockperiod);
377         $this->assertEquals($moduleinfo->blockafter, $dbmodinstance->blockafter);
378         $this->assertEquals($moduleinfo->warnafter, $dbmodinstance->warnafter);
379     }
383     /**
384      * Test a specific type of module.
385      *
386      * @param string $modulename - the module name to test
387      */
388     private function update_specific_module_test($modulename) {
389         global $DB, $CFG;
391         $this->resetAfterTest(true);
393         $this->setAdminUser();
395         // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
396         require_once($CFG->dirroot.'/mod/'. $modulename .'/lib.php');
398         // Enable avaibility.
399         // If not enabled all conditional fields will be ignored.
400         set_config('enableavailability', 1);
402         // Enable course completion.
403         // If not enabled all completion settings will be ignored.
404         set_config('enablecompletion', COMPLETION_ENABLED);
406         // Enable forum RSS feeds.
407         set_config('enablerssfeeds', 1);
408         set_config('forum_enablerssfeeds', 1);
410         $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED),
411            array('createsections'=>true));
413         $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
415         // Create assign module instance for testing gradeitem.
416         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
417         $params['course'] = $course->id;
418         $instance = $generator->create_instance($params);
419         $assigncm = get_coursemodule_from_instance('assign', $instance->id);
421         // Create the test forum to update.
422         $initvalues = new stdClass();
423         $initvalues->introformat = FORMAT_HTML;
424         $initvalues->course = $course->id;
425         $forum = self::getDataGenerator()->create_module('forum', $initvalues);
427         // Retrieve course module.
428         $cm = get_coursemodule_from_instance('forum', $forum->id);
430         // Module test values.
431         $moduleinfo = new stdClass();
433         // Always mandatory generic values to any module.
434         $moduleinfo->coursemodule = $cm->id;
435         $moduleinfo->modulename = $modulename;
436         $moduleinfo->course = $course->id;
437         $moduleinfo->groupingid = $grouping->id;
438         $moduleinfo->visible = true;
440         // Sometimes optional generic values for some modules.
441         $moduleinfo->name = 'My test module';
442         $moduleinfo->showdescription = 1; // standard boolean
443         require_once($CFG->libdir . '/gradelib.php');
444         $gradecats = grade_get_categories_menu($moduleinfo->course, false);
445         $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
446         $moduleinfo->gradecat = $gradecatid;
447         $moduleinfo->groupmode = VISIBLEGROUPS;
448         $moduleinfo->cmidnumber = 'idnumber_XXX';
450         // Completion common to all module.
451         $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC;
452         $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED;
453         $moduleinfo->completiongradeitemnumber = 1;
454         $moduleinfo->completionexpected = time() + (7 * 24 * 3600);
455         $moduleinfo->completionunlocked = 1;
457         // Conditional activity.
458         $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself.
459         $moduleinfo->availability = json_encode(\core_availability\tree::get_root_json(
460                 array(\availability_date\condition::get_json('>=', time()),
461                 \availability_date\condition::get_json('<', time() + (7 * 24 * 3600)),
462                 \availability_grade\condition::get_json($coursegradeitem->id, 10, 80),
463                 \availability_profile\condition::get_json(false, 'email', 'contains', '@'),
464                 \availability_completion\condition::get_json($assigncm->id, COMPLETION_COMPLETE)), '&'));
466         // Grading and Advanced grading.
467         require_once($CFG->dirroot . '/rating/lib.php');
468         $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE;
469         $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number.
470         $moduleinfo->assesstimestart = time();
471         $moduleinfo->assesstimefinish = time() + (7 * 24 * 3600);
473         // RSS.
474         $moduleinfo->rsstype = 2;
475         $moduleinfo->rssarticles = 10;
477         // Optional intro editor (depends of module).
478         $draftid_editor = 0;
479         file_prepare_draft_area($draftid_editor, null, null, null, null);
480         $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
482         // Following is the advanced grading method area called 'submissions' for the 'assign' module.
483         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
484             $moduleinfo->grade = 100;
485         }
486         // Plagiarism form values.
487         // No plagiarism plugin installed by default. Use this space to make your own test.
489         // Values specific to the module.
490         $modulesetvalues = $modulename.'_update_set_values';
491         $this->$modulesetvalues($moduleinfo);
493         // Create the module.
494         $result = update_module($moduleinfo);
496         // Retrieve the module info.
497         $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance));
498         $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance);
499         // Retrieve the grade item.
500         $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course,
501             'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename));
503         // Compare the values common to all module instances.
504         $this->assertEquals($moduleinfo->modulename, $dbcm->modname);
505         $this->assertEquals($moduleinfo->course, $dbcm->course);
506         $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
507         $this->assertEquals($moduleinfo->visible, $dbcm->visible);
508         $this->assertEquals($moduleinfo->completion, $dbcm->completion);
509         $this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
510         $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber);
511         $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected);
512         $this->assertEquals($moduleinfo->availability, $dbcm->availability);
513         $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription);
514         $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode);
515         $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber);
516         $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid);
518         // Optional grade testing.
519         if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
520             $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade);
521         }
523         // Some optional (but quite common) to some module.
524         $this->assertEquals($moduleinfo->name, $dbmodinstance->name);
525         $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro);
526         $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat);
528         // Test specific to the module.
529         $modulerunasserts = $modulename.'_update_run_asserts';
530         $this->$modulerunasserts($moduleinfo, $dbmodinstance);
531         return $moduleinfo;
532    }
534     /**
535      * Data provider for course_delete module
536      *
537      * @return array An array of arrays contain test data
538      */
539     public function provider_course_delete_module() {
540         $data = array();
542         $data['assign'] = array('assign', array('duedate' => time()));
543         $data['quiz'] = array('quiz', array('duedate' => time()));
545         return $data;
546     }
548     /**
549      * Test the create_course function
550      */
551     public function test_create_course() {
552         global $DB;
553         $this->resetAfterTest(true);
554         $defaultcategory = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
556         $course = new stdClass();
557         $course->fullname = 'Apu loves Unit Təsts';
558         $course->shortname = 'Spread the lŭve';
559         $course->idnumber = '123';
560         $course->summary = 'Awesome!';
561         $course->summaryformat = FORMAT_PLAIN;
562         $course->format = 'topics';
563         $course->newsitems = 0;
564         $course->numsections = 5;
565         $course->category = $defaultcategory;
566         $original = (array) $course;
568         $created = create_course($course);
569         $context = context_course::instance($created->id);
571         // Compare original and created.
572         $this->assertEquals($original, array_intersect_key((array) $created, $original));
574         // Ensure default section is created.
575         $sectioncreated = $DB->record_exists('course_sections', array('course' => $created->id, 'section' => 0));
576         $this->assertTrue($sectioncreated);
578         // Ensure that the shortname isn't duplicated.
579         try {
580             $created = create_course($course);
581             $this->fail('Exception expected');
582         } catch (moodle_exception $e) {
583             $this->assertSame(get_string('shortnametaken', 'error', $course->shortname), $e->getMessage());
584         }
586         // Ensure that the idnumber isn't duplicated.
587         $course->shortname .= '1';
588         try {
589             $created = create_course($course);
590             $this->fail('Exception expected');
591         } catch (moodle_exception $e) {
592             $this->assertSame(get_string('courseidnumbertaken', 'error', $course->idnumber), $e->getMessage());
593         }
594     }
596     public function test_create_course_with_generator() {
597         global $DB;
598         $this->resetAfterTest(true);
599         $course = $this->getDataGenerator()->create_course();
601         // Ensure default section is created.
602         $sectioncreated = $DB->record_exists('course_sections', array('course' => $course->id, 'section' => 0));
603         $this->assertTrue($sectioncreated);
604     }
606     public function test_create_course_sections() {
607         global $DB;
608         $this->resetAfterTest(true);
610         $course = $this->getDataGenerator()->create_course(
611                 array('shortname' => 'GrowingCourse',
612                     'fullname' => 'Growing Course',
613                     'numsections' => 5),
614                 array('createsections' => true));
616         // Ensure all 6 (0-5) sections were created and course content cache works properly
617         $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
618         $this->assertEquals(range(0, $course->numsections), $sectionscreated);
620         // this will do nothing, section already exists
621         $this->assertFalse(course_create_sections_if_missing($course, $course->numsections));
623         // this will create new section
624         $this->assertTrue(course_create_sections_if_missing($course, $course->numsections + 1));
626         // Ensure all 7 (0-6) sections were created and modinfo/sectioninfo cache works properly
627         $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
628         $this->assertEquals(range(0, $course->numsections + 1), $sectionscreated);
629     }
631     public function test_update_course() {
632         global $DB;
634         $this->resetAfterTest();
636         $defaultcategory = $DB->get_field_select('course_categories', 'MIN(id)', 'parent = 0');
638         $course = new stdClass();
639         $course->fullname = 'Apu loves Unit Təsts';
640         $course->shortname = 'test1';
641         $course->idnumber = '1';
642         $course->summary = 'Awesome!';
643         $course->summaryformat = FORMAT_PLAIN;
644         $course->format = 'topics';
645         $course->newsitems = 0;
646         $course->numsections = 5;
647         $course->category = $defaultcategory;
649         $created = create_course($course);
650         // Ensure the checks only work on idnumber/shortname that are not already ours.
651         update_course($created);
653         $course->shortname = 'test2';
654         $course->idnumber = '2';
656         $created2 = create_course($course);
658         // Test duplicate idnumber.
659         $created2->idnumber = '1';
660         try {
661             update_course($created2);
662             $this->fail('Expected exception when trying to update a course with duplicate idnumber');
663         } catch (moodle_exception $e) {
664             $this->assertEquals(get_string('courseidnumbertaken', 'error', $created2->idnumber), $e->getMessage());
665         }
667         // Test duplicate shortname.
668         $created2->idnumber = '2';
669         $created2->shortname = 'test1';
670         try {
671             update_course($created2);
672             $this->fail('Expected exception when trying to update a course with a duplicate shortname');
673         } catch (moodle_exception $e) {
674             $this->assertEquals(get_string('shortnametaken', 'error', $created2->shortname), $e->getMessage());
675         }
676     }
678     public function test_course_add_cm_to_section() {
679         global $DB;
680         $this->resetAfterTest(true);
682         // Create course with 1 section.
683         $course = $this->getDataGenerator()->create_course(
684                 array('shortname' => 'GrowingCourse',
685                     'fullname' => 'Growing Course',
686                     'numsections' => 1),
687                 array('createsections' => true));
689         // Trash modinfo.
690         rebuild_course_cache($course->id, true);
692         // Create some cms for testing.
693         $cmids = array();
694         for ($i=0; $i<4; $i++) {
695             $cmids[$i] = $DB->insert_record('course_modules', array('course' => $course->id));
696         }
698         // Add it to section that exists.
699         course_add_cm_to_section($course, $cmids[0], 1);
701         // Check it got added to sequence.
702         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 1));
703         $this->assertEquals($cmids[0], $sequence);
705         // Add a second, this time using courseid variant of parameters.
706         $coursecacherev = $DB->get_field('course', 'cacherev', array('id' => $course->id));
707         course_add_cm_to_section($course->id, $cmids[1], 1);
708         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 1));
709         $this->assertEquals($cmids[0] . ',' . $cmids[1], $sequence);
711         // Check that modinfo cache was reset but not rebuilt (important for performance if calling repeatedly).
712         $this->assertGreaterThan($coursecacherev, $DB->get_field('course', 'cacherev', array('id' => $course->id)));
713         $this->assertEmpty(cache::make('core', 'coursemodinfo')->get($course->id));
715         // Add one to section that doesn't exist (this might rebuild modinfo).
716         course_add_cm_to_section($course, $cmids[2], 2);
717         $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id)));
718         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 2));
719         $this->assertEquals($cmids[2], $sequence);
721         // Add using the 'before' option.
722         course_add_cm_to_section($course, $cmids[3], 2, $cmids[2]);
723         $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id)));
724         $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 2));
725         $this->assertEquals($cmids[3] . ',' . $cmids[2], $sequence);
726     }
728     public function test_reorder_sections() {
729         global $DB;
730         $this->resetAfterTest(true);
732         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
733         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
734         $oldsections = array();
735         $sections = array();
736         foreach ($DB->get_records('course_sections', array('course'=>$course->id), 'id') as $section) {
737             $oldsections[$section->section] = $section->id;
738             $sections[$section->id] = $section->section;
739         }
740         ksort($oldsections);
742         $neworder = reorder_sections($sections, 2, 4);
743         $neworder = array_keys($neworder);
744         $this->assertEquals($oldsections[0], $neworder[0]);
745         $this->assertEquals($oldsections[1], $neworder[1]);
746         $this->assertEquals($oldsections[2], $neworder[4]);
747         $this->assertEquals($oldsections[3], $neworder[2]);
748         $this->assertEquals($oldsections[4], $neworder[3]);
749         $this->assertEquals($oldsections[5], $neworder[5]);
750         $this->assertEquals($oldsections[6], $neworder[6]);
752         $neworder = reorder_sections($sections, 4, 2);
753         $neworder = array_keys($neworder);
754         $this->assertEquals($oldsections[0], $neworder[0]);
755         $this->assertEquals($oldsections[1], $neworder[1]);
756         $this->assertEquals($oldsections[2], $neworder[3]);
757         $this->assertEquals($oldsections[3], $neworder[4]);
758         $this->assertEquals($oldsections[4], $neworder[2]);
759         $this->assertEquals($oldsections[5], $neworder[5]);
760         $this->assertEquals($oldsections[6], $neworder[6]);
762         $neworder = reorder_sections(1, 2, 4);
763         $this->assertFalse($neworder);
764     }
766     public function test_move_section_down() {
767         global $DB;
768         $this->resetAfterTest(true);
770         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
771         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
772         $oldsections = array();
773         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
774             $oldsections[$section->section] = $section->id;
775         }
776         ksort($oldsections);
778         // Test move section down..
779         move_section_to($course, 2, 4);
780         $sections = array();
781         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
782             $sections[$section->section] = $section->id;
783         }
784         ksort($sections);
786         $this->assertEquals($oldsections[0], $sections[0]);
787         $this->assertEquals($oldsections[1], $sections[1]);
788         $this->assertEquals($oldsections[2], $sections[4]);
789         $this->assertEquals($oldsections[3], $sections[2]);
790         $this->assertEquals($oldsections[4], $sections[3]);
791         $this->assertEquals($oldsections[5], $sections[5]);
792         $this->assertEquals($oldsections[6], $sections[6]);
793     }
795     public function test_move_section_up() {
796         global $DB;
797         $this->resetAfterTest(true);
799         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
800         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
801         $oldsections = array();
802         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
803             $oldsections[$section->section] = $section->id;
804         }
805         ksort($oldsections);
807         // Test move section up..
808         move_section_to($course, 6, 4);
809         $sections = array();
810         foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
811             $sections[$section->section] = $section->id;
812         }
813         ksort($sections);
815         $this->assertEquals($oldsections[0], $sections[0]);
816         $this->assertEquals($oldsections[1], $sections[1]);
817         $this->assertEquals($oldsections[2], $sections[2]);
818         $this->assertEquals($oldsections[3], $sections[3]);
819         $this->assertEquals($oldsections[4], $sections[5]);
820         $this->assertEquals($oldsections[5], $sections[6]);
821         $this->assertEquals($oldsections[6], $sections[4]);
822     }
824     public function test_move_section_marker() {
825         global $DB;
826         $this->resetAfterTest(true);
828         $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
829         $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
831         // Set course marker to the section we are going to move..
832         course_set_marker($course->id, 2);
833         // Verify that the course marker is set correctly.
834         $course = $DB->get_record('course', array('id' => $course->id));
835         $this->assertEquals(2, $course->marker);
837         // Test move the marked section down..
838         move_section_to($course, 2, 4);
840         // Verify that the coruse marker has been moved along with the section..
841         $course = $DB->get_record('course', array('id' => $course->id));
842         $this->assertEquals(4, $course->marker);
844         // Test move the marked section up..
845         move_section_to($course, 4, 3);
847         // Verify that the course marker has been moved along with the section..
848         $course = $DB->get_record('course', array('id' => $course->id));
849         $this->assertEquals(3, $course->marker);
851         // Test moving a non-marked section above the marked section..
852         move_section_to($course, 4, 2);
854         // Verify that the course marker has been moved down to accomodate..
855         $course = $DB->get_record('course', array('id' => $course->id));
856         $this->assertEquals(4, $course->marker);
858         // Test moving a non-marked section below the marked section..
859         move_section_to($course, 3, 6);
861         // Verify that the course marker has been up to accomodate..
862         $course = $DB->get_record('course', array('id' => $course->id));
863         $this->assertEquals(3, $course->marker);
864     }
866     public function test_course_can_delete_section() {
867         global $DB;
868         $this->resetAfterTest(true);
870         $generator = $this->getDataGenerator();
872         $courseweeks = $generator->create_course(
873             array('numsections' => 5, 'format' => 'weeks'),
874             array('createsections' => true));
875         $assign1 = $generator->create_module('assign', array('course' => $courseweeks, 'section' => 1));
876         $assign2 = $generator->create_module('assign', array('course' => $courseweeks, 'section' => 2));
878         $coursetopics = $generator->create_course(
879             array('numsections' => 5, 'format' => 'topics'),
880             array('createsections' => true));
882         $coursesingleactivity = $generator->create_course(
883             array('format' => 'singleactivity'),
884             array('createsections' => true));
886         // Enrol student and teacher.
887         $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
888         $student = $generator->create_user();
889         $teacher = $generator->create_user();
891         $generator->enrol_user($student->id, $courseweeks->id, $roleids['student']);
892         $generator->enrol_user($teacher->id, $courseweeks->id, $roleids['editingteacher']);
894         $generator->enrol_user($student->id, $coursetopics->id, $roleids['student']);
895         $generator->enrol_user($teacher->id, $coursetopics->id, $roleids['editingteacher']);
897         $generator->enrol_user($student->id, $coursesingleactivity->id, $roleids['student']);
898         $generator->enrol_user($teacher->id, $coursesingleactivity->id, $roleids['editingteacher']);
900         // Teacher should be able to delete sections (except for 0) in topics and weeks format.
901         $this->setUser($teacher);
903         // For topics and weeks formats will return false for section 0 and true for any other section.
904         $this->assertFalse(course_can_delete_section($courseweeks, 0));
905         $this->assertTrue(course_can_delete_section($courseweeks, 1));
907         $this->assertFalse(course_can_delete_section($coursetopics, 0));
908         $this->assertTrue(course_can_delete_section($coursetopics, 1));
910         // For singleactivity course format no section can be deleted.
911         $this->assertFalse(course_can_delete_section($coursesingleactivity, 0));
912         $this->assertFalse(course_can_delete_section($coursesingleactivity, 1));
914         // Now let's revoke a capability from teacher to manage activity in section 1.
915         $modulecontext = context_module::instance($assign1->cmid);
916         assign_capability('moodle/course:manageactivities', CAP_PROHIBIT, $roleids['editingteacher'],
917             $modulecontext);
918         $modulecontext->mark_dirty();
919         $this->assertFalse(course_can_delete_section($courseweeks, 1));
920         $this->assertTrue(course_can_delete_section($courseweeks, 2));
922         // Student does not have permissions to delete sections.
923         $this->setUser($student);
924         $this->assertFalse(course_can_delete_section($courseweeks, 1));
925         $this->assertFalse(course_can_delete_section($coursetopics, 1));
926         $this->assertFalse(course_can_delete_section($coursesingleactivity, 1));
927     }
929     public function test_course_delete_section() {
930         global $DB;
931         $this->resetAfterTest(true);
933         $generator = $this->getDataGenerator();
935         $course = $generator->create_course(array('numsections' => 6, 'format' => 'topics'),
936             array('createsections' => true));
937         $assign0 = $generator->create_module('assign', array('course' => $course, 'section' => 0));
938         $assign1 = $generator->create_module('assign', array('course' => $course, 'section' => 1));
939         $assign21 = $generator->create_module('assign', array('course' => $course, 'section' => 2));
940         $assign22 = $generator->create_module('assign', array('course' => $course, 'section' => 2));
941         $assign3 = $generator->create_module('assign', array('course' => $course, 'section' => 3));
942         $assign5 = $generator->create_module('assign', array('course' => $course, 'section' => 5));
943         $assign6 = $generator->create_module('assign', array('course' => $course, 'section' => 6));
945         $this->setAdminUser();
947         // Attempt to delete non-existing section.
948         $this->assertFalse(course_delete_section($course, 10, false));
949         $this->assertFalse(course_delete_section($course, 9, true));
951         // Attempt to delete 0-section.
952         $this->assertFalse(course_delete_section($course, 0, true));
953         $this->assertTrue($DB->record_exists('course_modules', array('id' => $assign0->cmid)));
955         // Delete last section.
956         $this->assertTrue(course_delete_section($course, 6, true));
957         $this->assertFalse($DB->record_exists('course_modules', array('id' => $assign6->cmid)));
958         $this->assertEquals(5, course_get_format($course)->get_course()->numsections);
960         // Delete empty section.
961         $this->assertTrue(course_delete_section($course, 4, false));
962         $this->assertEquals(4, course_get_format($course)->get_course()->numsections);
964         // Delete section in the middle (2).
965         $this->assertFalse(course_delete_section($course, 2, false));
966         $this->assertTrue(course_delete_section($course, 2, true));
967         $this->assertFalse($DB->record_exists('course_modules', array('id' => $assign21->cmid)));
968         $this->assertFalse($DB->record_exists('course_modules', array('id' => $assign22->cmid)));
969         $this->assertEquals(3, course_get_format($course)->get_course()->numsections);
970         $this->assertEquals(array(0 => array($assign0->cmid),
971             1 => array($assign1->cmid),
972             2 => array($assign3->cmid),
973             3 => array($assign5->cmid)), get_fast_modinfo($course)->sections);
975         // Make last section orphaned.
976         update_course((object)array('id' => $course->id, 'numsections' => 2));
977         $this->assertEquals(2, course_get_format($course)->get_course()->numsections);
979         // Remove orphaned section.
980         $this->assertTrue(course_delete_section($course, 3, true));
981         $this->assertEquals(2, course_get_format($course)->get_course()->numsections);
983         // Remove marked section.
984         course_set_marker($course->id, 1);
985         $this->assertTrue(course_get_format($course)->is_section_current(1));
986         $this->assertTrue(course_delete_section($course, 1, true));
987         $this->assertFalse(course_get_format($course)->is_section_current(1));
988     }
990     public function test_get_course_display_name_for_list() {
991         global $CFG;
992         $this->resetAfterTest(true);
994         $course = $this->getDataGenerator()->create_course(array('shortname' => 'FROG101', 'fullname' => 'Introduction to pond life'));
996         $CFG->courselistshortnames = 0;
997         $this->assertEquals('Introduction to pond life', get_course_display_name_for_list($course));
999         $CFG->courselistshortnames = 1;
1000         $this->assertEquals('FROG101 Introduction to pond life', get_course_display_name_for_list($course));
1001     }
1003     public function test_move_module_in_course() {
1004         global $DB;
1006         $this->resetAfterTest(true);
1007         // Setup fixture
1008         $course = $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections' => true));
1009         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
1011         $cms = get_fast_modinfo($course)->get_cms();
1012         $cm = reset($cms);
1014         $newsection = get_fast_modinfo($course)->get_section_info(3);
1015         $oldsectionid = $cm->section;
1017         // Perform the move
1018         moveto_module($cm, $newsection);
1020         $cms = get_fast_modinfo($course)->get_cms();
1021         $cm = reset($cms);
1023         // Check that the cached modinfo contains the correct section info
1024         $modinfo = get_fast_modinfo($course);
1025         $this->assertTrue(empty($modinfo->sections[0]));
1026         $this->assertFalse(empty($modinfo->sections[3]));
1028         // Check that the old section's sequence no longer contains this ID
1029         $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
1030         $oldsequences = explode(',', $newsection->sequence);
1031         $this->assertFalse(in_array($cm->id, $oldsequences));
1033         // Check that the new section's sequence now contains this ID
1034         $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
1035         $newsequences = explode(',', $newsection->sequence);
1036         $this->assertTrue(in_array($cm->id, $newsequences));
1038         // Check that the section number has been changed in the cm
1039         $this->assertEquals($newsection->id, $cm->section);
1042         // Perform a second move as some issues were only seen on the second move
1043         $newsection = get_fast_modinfo($course)->get_section_info(2);
1044         $oldsectionid = $cm->section;
1045         moveto_module($cm, $newsection);
1047         $cms = get_fast_modinfo($course)->get_cms();
1048         $cm = reset($cms);
1050         // Check that the cached modinfo contains the correct section info
1051         $modinfo = get_fast_modinfo($course);
1052         $this->assertTrue(empty($modinfo->sections[0]));
1053         $this->assertFalse(empty($modinfo->sections[2]));
1055         // Check that the old section's sequence no longer contains this ID
1056         $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
1057         $oldsequences = explode(',', $newsection->sequence);
1058         $this->assertFalse(in_array($cm->id, $oldsequences));
1060         // Check that the new section's sequence now contains this ID
1061         $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
1062         $newsequences = explode(',', $newsection->sequence);
1063         $this->assertTrue(in_array($cm->id, $newsequences));
1064     }
1066     public function test_module_visibility() {
1067         $this->setAdminUser();
1068         $this->resetAfterTest(true);
1070         // Create course and modules.
1071         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
1072         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1073         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(), 'course' => $course->id));
1074         $modules = compact('forum', 'assign');
1076         // Hiding the modules.
1077         foreach ($modules as $mod) {
1078             set_coursemodule_visible($mod->cmid, 0);
1079             $this->check_module_visibility($mod, 0, 0);
1080         }
1082         // Showing the modules.
1083         foreach ($modules as $mod) {
1084             set_coursemodule_visible($mod->cmid, 1);
1085             $this->check_module_visibility($mod, 1, 1);
1086         }
1087     }
1089     public function test_section_visibility_events() {
1090         $this->setAdminUser();
1091         $this->resetAfterTest(true);
1093         $course = $this->getDataGenerator()->create_course(array('numsections' => 1), array('createsections' => true));
1094         $sectionnumber = 1;
1095         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1096             array('section' => $sectionnumber));
1097         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1098             'course' => $course->id), array('section' => $sectionnumber));
1099         $sink = $this->redirectEvents();
1100         set_section_visible($course->id, $sectionnumber, 0);
1101         $events = $sink->get_events();
1103         // Extract the number of events related to what we are testing, other events
1104         // such as course_section_updated could have been triggered.
1105         $count = 0;
1106         foreach ($events as $event) {
1107             if ($event instanceof \core\event\course_module_updated) {
1108                 $count++;
1109             }
1110         }
1111         $this->assertSame(2, $count);
1112         $sink->close();
1113     }
1115     public function test_section_visibility() {
1116         $this->setAdminUser();
1117         $this->resetAfterTest(true);
1119         // Create course.
1120         $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
1122         $sink = $this->redirectEvents();
1124         // Testing an empty section.
1125         $sectionnumber = 1;
1126         set_section_visible($course->id, $sectionnumber, 0);
1127         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1128         $this->assertEquals($section_info->visible, 0);
1129         set_section_visible($course->id, $sectionnumber, 1);
1130         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1131         $this->assertEquals($section_info->visible, 1);
1133         // Checking that an event was fired.
1134         $events = $sink->get_events();
1135         $this->assertInstanceOf('\core\event\course_section_updated', $events[0]);
1136         $sink->close();
1138         // Testing a section with visible modules.
1139         $sectionnumber = 2;
1140         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1141                 array('section' => $sectionnumber));
1142         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1143                 'course' => $course->id), array('section' => $sectionnumber));
1144         $modules = compact('forum', 'assign');
1145         set_section_visible($course->id, $sectionnumber, 0);
1146         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1147         $this->assertEquals($section_info->visible, 0);
1148         foreach ($modules as $mod) {
1149             $this->check_module_visibility($mod, 0, 1);
1150         }
1151         set_section_visible($course->id, $sectionnumber, 1);
1152         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1153         $this->assertEquals($section_info->visible, 1);
1154         foreach ($modules as $mod) {
1155             $this->check_module_visibility($mod, 1, 1);
1156         }
1158         // Testing a section with hidden modules, which should stay hidden.
1159         $sectionnumber = 3;
1160         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1161                 array('section' => $sectionnumber));
1162         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1163                 'course' => $course->id), array('section' => $sectionnumber));
1164         $modules = compact('forum', 'assign');
1165         foreach ($modules as $mod) {
1166             set_coursemodule_visible($mod->cmid, 0);
1167             $this->check_module_visibility($mod, 0, 0);
1168         }
1169         set_section_visible($course->id, $sectionnumber, 0);
1170         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1171         $this->assertEquals($section_info->visible, 0);
1172         foreach ($modules as $mod) {
1173             $this->check_module_visibility($mod, 0, 0);
1174         }
1175         set_section_visible($course->id, $sectionnumber, 1);
1176         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1177         $this->assertEquals($section_info->visible, 1);
1178         foreach ($modules as $mod) {
1179             $this->check_module_visibility($mod, 0, 0);
1180         }
1181     }
1183     /**
1184      * Helper function to assert that a module has correctly been made visible, or hidden.
1185      *
1186      * @param stdClass $mod module information
1187      * @param int $visibility the current state of the module
1188      * @param int $visibleold the current state of the visibleold property
1189      * @return void
1190      */
1191     public function check_module_visibility($mod, $visibility, $visibleold) {
1192         global $DB;
1193         $cm = get_fast_modinfo($mod->course)->get_cm($mod->cmid);
1194         $this->assertEquals($visibility, $cm->visible);
1195         $this->assertEquals($visibleold, $cm->visibleold);
1197         // Check the module grade items.
1198         $grade_items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $cm->modname,
1199                 'iteminstance' => $cm->instance, 'courseid' => $cm->course));
1200         if ($grade_items) {
1201             foreach ($grade_items as $grade_item) {
1202                 if ($visibility) {
1203                     $this->assertFalse($grade_item->is_hidden(), "$cm->modname grade_item not visible");
1204                 } else {
1205                     $this->assertTrue($grade_item->is_hidden(), "$cm->modname grade_item not hidden");
1206                 }
1207             }
1208         }
1210         // Check the events visibility.
1211         if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $cm->modname))) {
1212             foreach ($events as $event) {
1213                 $calevent = new calendar_event($event);
1214                 $this->assertEquals($visibility, $calevent->visible, "$cm->modname calendar_event visibility");
1215             }
1216         }
1217     }
1219     public function test_course_page_type_list() {
1220         global $DB;
1221         $this->resetAfterTest(true);
1223         // Create a category.
1224         $category = new stdClass();
1225         $category->name = 'Test Category';
1227         $testcategory = $this->getDataGenerator()->create_category($category);
1229         // Create a course.
1230         $course = new stdClass();
1231         $course->fullname = 'Apu loves Unit Təsts';
1232         $course->shortname = 'Spread the lŭve';
1233         $course->idnumber = '123';
1234         $course->summary = 'Awesome!';
1235         $course->summaryformat = FORMAT_PLAIN;
1236         $course->format = 'topics';
1237         $course->newsitems = 0;
1238         $course->numsections = 5;
1239         $course->category = $testcategory->id;
1241         $testcourse = $this->getDataGenerator()->create_course($course);
1243         // Create contexts.
1244         $coursecontext = context_course::instance($testcourse->id);
1245         $parentcontext = $coursecontext->get_parent_context(); // Not actually used.
1246         $pagetype = 'page-course-x'; // Not used either.
1247         $pagetypelist = course_page_type_list($pagetype, $parentcontext, $coursecontext);
1249         // Page type lists for normal courses.
1250         $testpagetypelist1 = array();
1251         $testpagetypelist1['*'] = 'Any page';
1252         $testpagetypelist1['course-*'] = 'Any course page';
1253         $testpagetypelist1['course-view-*'] = 'Any type of course main page';
1255         $this->assertEquals($testpagetypelist1, $pagetypelist);
1257         // Get the context for the front page course.
1258         $sitecoursecontext = context_course::instance(SITEID);
1259         $pagetypelist = course_page_type_list($pagetype, $parentcontext, $sitecoursecontext);
1261         // Page type list for the front page course.
1262         $testpagetypelist2 = array('*' => 'Any page');
1263         $this->assertEquals($testpagetypelist2, $pagetypelist);
1265         // Make sure that providing no current context to the function doesn't result in an error.
1266         // Calls made from generate_page_type_patterns() may provide null values.
1267         $pagetypelist = course_page_type_list($pagetype, null, null);
1268         $this->assertEquals($pagetypelist, $testpagetypelist1);
1269     }
1271     public function test_compare_activities_by_time_desc() {
1273         // Let's create some test data.
1274         $activitiesivities = array();
1275         $x = new stdClass();
1276         $x->timestamp = null;
1277         $activities[] = $x;
1279         $x = new stdClass();
1280         $x->timestamp = 1;
1281         $activities[] = $x;
1283         $x = new stdClass();
1284         $x->timestamp = 3;
1285         $activities[] = $x;
1287         $x = new stdClass();
1288         $x->timestamp = 0;
1289         $activities[] = $x;
1291         $x = new stdClass();
1292         $x->timestamp = 5;
1293         $activities[] = $x;
1295         $x = new stdClass();
1296         $activities[] = $x;
1298         $x = new stdClass();
1299         $x->timestamp = 5;
1300         $activities[] = $x;
1302         // Do the sorting.
1303         usort($activities, 'compare_activities_by_time_desc');
1305         // Let's check the result.
1306         $last = 10;
1307         foreach($activities as $activity) {
1308             if (empty($activity->timestamp)) {
1309                 $activity->timestamp = 0;
1310             }
1311             $this->assertLessThanOrEqual($last, $activity->timestamp);
1312         }
1313     }
1315     public function test_compare_activities_by_time_asc() {
1317         // Let's create some test data.
1318         $activities = array();
1319         $x = new stdClass();
1320         $x->timestamp = null;
1321         $activities[] = $x;
1323         $x = new stdClass();
1324         $x->timestamp = 1;
1325         $activities[] = $x;
1327         $x = new stdClass();
1328         $x->timestamp = 3;
1329         $activities[] = $x;
1331         $x = new stdClass();
1332         $x->timestamp = 0;
1333         $activities[] = $x;
1335         $x = new stdClass();
1336         $x->timestamp = 5;
1337         $activities[] = $x;
1339         $x = new stdClass();
1340         $activities[] = $x;
1342         $x = new stdClass();
1343         $x->timestamp = 5;
1344         $activities[] = $x;
1346         // Do the sorting.
1347         usort($activities, 'compare_activities_by_time_asc');
1349         // Let's check the result.
1350         $last = 0;
1351         foreach($activities as $activity) {
1352             if (empty($activity->timestamp)) {
1353                 $activity->timestamp = 0;
1354             }
1355             $this->assertGreaterThanOrEqual($last, $activity->timestamp);
1356         }
1357     }
1359     /**
1360      * Tests moving a module between hidden/visible sections and
1361      * verifies that the course/module visiblity seettings are
1362      * retained.
1363      */
1364     public function test_moveto_module_between_hidden_sections() {
1365         global $DB;
1367         $this->resetAfterTest(true);
1369         $course = $this->getDataGenerator()->create_course(array('numsections' => 4), array('createsections' => true));
1370         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1371         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
1372         $quiz= $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
1374         // Set the page as hidden
1375         set_coursemodule_visible($page->cmid, 0);
1377         // Set sections 3 as hidden.
1378         set_section_visible($course->id, 3, 0);
1380         $modinfo = get_fast_modinfo($course);
1382         $hiddensection = $modinfo->get_section_info(3);
1383         // New section is definitely not visible:
1384         $this->assertEquals($hiddensection->visible, 0);
1386         $forumcm = $modinfo->cms[$forum->cmid];
1387         $pagecm = $modinfo->cms[$page->cmid];
1389         // Move the forum and the page to a hidden section, make sure moveto_module returns 0 as new visibility state.
1390         $this->assertEquals(0, moveto_module($forumcm, $hiddensection));
1391         $this->assertEquals(0, moveto_module($pagecm, $hiddensection));
1393         $modinfo = get_fast_modinfo($course);
1395         // Verify that forum and page have been moved to the hidden section and quiz has not.
1396         $this->assertContains($forum->cmid, $modinfo->sections[3]);
1397         $this->assertContains($page->cmid, $modinfo->sections[3]);
1398         $this->assertNotContains($quiz->cmid, $modinfo->sections[3]);
1400         // Verify that forum has been made invisible.
1401         $forumcm = $modinfo->cms[$forum->cmid];
1402         $this->assertEquals($forumcm->visible, 0);
1403         // Verify that old state has been retained.
1404         $this->assertEquals($forumcm->visibleold, 1);
1406         // Verify that page has stayed invisible.
1407         $pagecm = $modinfo->cms[$page->cmid];
1408         $this->assertEquals($pagecm->visible, 0);
1409         // Verify that old state has been retained.
1410         $this->assertEquals($pagecm->visibleold, 0);
1412         // Verify that quiz has been unaffected.
1413         $quizcm = $modinfo->cms[$quiz->cmid];
1414         $this->assertEquals($quizcm->visible, 1);
1416         // Move forum and page back to visible section.
1417         // Make sure the visibility is restored to the original value (visible for forum and hidden for page).
1418         $visiblesection = $modinfo->get_section_info(2);
1419         $this->assertEquals(1, moveto_module($forumcm, $visiblesection));
1420         $this->assertEquals(0, moveto_module($pagecm, $visiblesection));
1422         $modinfo = get_fast_modinfo($course);
1424         // Double check that forum has been made visible.
1425         $forumcm = $modinfo->cms[$forum->cmid];
1426         $this->assertEquals($forumcm->visible, 1);
1428         // Double check that page has stayed invisible.
1429         $pagecm = $modinfo->cms[$page->cmid];
1430         $this->assertEquals($pagecm->visible, 0);
1432         // Move the page in the same section (this is what mod duplicate does).
1433         // Visibility of page remains 0.
1434         $this->assertEquals(0, moveto_module($pagecm, $visiblesection, $forumcm));
1436         // Double check that the the page is still hidden.
1437         $modinfo = get_fast_modinfo($course);
1438         $pagecm = $modinfo->cms[$page->cmid];
1439         $this->assertEquals($pagecm->visible, 0);
1440     }
1442     /**
1443      * Tests moving a module around in the same section. moveto_module()
1444      * is called this way in modduplicate.
1445      */
1446     public function test_moveto_module_in_same_section() {
1447         global $DB;
1449         $this->resetAfterTest(true);
1451         $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
1452         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
1453         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1455         // Simulate inconsistent visible/visibleold values (MDL-38713).
1456         $cm = $DB->get_record('course_modules', array('id' => $page->cmid), '*', MUST_EXIST);
1457         $cm->visible = 0;
1458         $cm->visibleold = 1;
1459         $DB->update_record('course_modules', $cm);
1461         $modinfo = get_fast_modinfo($course);
1462         $forumcm = $modinfo->cms[$forum->cmid];
1463         $pagecm = $modinfo->cms[$page->cmid];
1465         // Verify that page is hidden.
1466         $this->assertEquals($pagecm->visible, 0);
1468         // Verify section 0 is where all mods added.
1469         $section = $modinfo->get_section_info(0);
1470         $this->assertEquals($section->id, $forumcm->section);
1471         $this->assertEquals($section->id, $pagecm->section);
1474         // Move the page inside the hidden section. Make sure it is hidden.
1475         $this->assertEquals(0, moveto_module($pagecm, $section, $forumcm));
1477         // Double check that the the page is still hidden.
1478         $modinfo = get_fast_modinfo($course);
1479         $pagecm = $modinfo->cms[$page->cmid];
1480         $this->assertEquals($pagecm->visible, 0);
1481     }
1483     /**
1484      * Tests the function that deletes a course module
1485      *
1486      * @param string $type The type of module for the test
1487      * @param array $options The options for the module creation
1488      * @dataProvider provider_course_delete_module
1489      */
1490     public function test_course_delete_module($type, $options) {
1491         global $DB;
1493         $this->resetAfterTest(true);
1494         $this->setAdminUser();
1496         // Create course and modules.
1497         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
1498         $options['course'] = $course->id;
1500         // Generate an assignment with due date (will generate a course event).
1501         $module = $this->getDataGenerator()->create_module($type, $options);
1503         // Get the module context.
1504         $modcontext = context_module::instance($module->cmid);
1506         // Verify context exists.
1507         $this->assertInstanceOf('context_module', $modcontext);
1509         // Make module specific messes.
1510         switch ($type) {
1511             case 'assign':
1512                 // Add some tags to this assignment.
1513                 core_tag_tag::set_item_tags('mod_assign', 'assign', $module->id, $modcontext, array('Tag 1', 'Tag 2', 'Tag 3'));
1514                 core_tag_tag::set_item_tags('core', 'course_modules', $module->cmid, $modcontext, array('Tag 3', 'Tag 4', 'Tag 5'));
1516                 // Confirm the tag instances were added.
1517                 $criteria = array('component' => 'mod_assign', 'itemtype' => 'assign', 'contextid' => $modcontext->id);
1518                 $this->assertEquals(3, $DB->count_records('tag_instance', $criteria));
1519                 $criteria = array('component' => 'core', 'itemtype' => 'course_modules', 'contextid' => $modcontext->id);
1520                 $this->assertEquals(3, $DB->count_records('tag_instance', $criteria));
1522                 // Verify event assignment event has been generated.
1523                 $eventcount = $DB->count_records('event', array('instance' => $module->id, 'modulename' => $type));
1524                 $this->assertEquals(1, $eventcount);
1526                 break;
1527             case 'quiz':
1528                 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
1529                 $qcat = $qgen->create_question_category(array('contextid' => $modcontext->id));
1530                 $questions = array(
1531                     $qgen->create_question('shortanswer', null, array('category' => $qcat->id)),
1532                     $qgen->create_question('shortanswer', null, array('category' => $qcat->id)),
1533                 );
1534                 $this->expectOutputRegex('/'.get_string('unusedcategorydeleted', 'question').'/');
1535                 break;
1536             default:
1537                 break;
1538         }
1540         // Run delete..
1541         course_delete_module($module->cmid);
1543         // Verify the context has been removed.
1544         $this->assertFalse(context_module::instance($module->cmid, IGNORE_MISSING));
1546         // Verify the course_module record has been deleted.
1547         $cmcount = $DB->count_records('course_modules', array('id' => $module->cmid));
1548         $this->assertEmpty($cmcount);
1550         // Test clean up of module specific messes.
1551         switch ($type) {
1552             case 'assign':
1553                 // Verify event assignment events have been removed.
1554                 $eventcount = $DB->count_records('event', array('instance' => $module->id, 'modulename' => $type));
1555                 $this->assertEmpty($eventcount);
1557                 // Verify the tag instances were deleted.
1558                 $criteria = array('component' => 'mod_assign', 'contextid' => $modcontext->id);
1559                 $this->assertEquals(0, $DB->count_records('tag_instance', $criteria));
1561                 $criteria = array('component' => 'core', 'itemtype' => 'course_modules', 'contextid' => $modcontext->id);
1562                 $this->assertEquals(0, $DB->count_records('tag_instance', $criteria));
1563                 break;
1564             case 'quiz':
1565                 // Verify category deleted.
1566                 $criteria = array('contextid' => $modcontext->id);
1567                 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
1569                 // Verify questions deleted.
1570                 $criteria = array('category' => $qcat->id);
1571                 $this->assertEquals(0, $DB->count_records('question', $criteria));
1572                 break;
1573             default:
1574                 break;
1575         }
1576     }
1578     /**
1579      * Test that triggering a course_created event works as expected.
1580      */
1581     public function test_course_created_event() {
1582         global $DB;
1584         $this->resetAfterTest();
1586         // Catch the events.
1587         $sink = $this->redirectEvents();
1589         // Create the course with an id number which is used later when generating a course via the imsenterprise plugin.
1590         $data = new stdClass();
1591         $data->idnumber = 'idnumber';
1592         $course = $this->getDataGenerator()->create_course($data);
1593         // Get course from DB for comparison.
1594         $course = $DB->get_record('course', array('id' => $course->id));
1596         // Capture the event.
1597         $events = $sink->get_events();
1598         $sink->close();
1600         // Validate the event.
1601         $event = $events[0];
1602         $this->assertInstanceOf('\core\event\course_created', $event);
1603         $this->assertEquals('course', $event->objecttable);
1604         $this->assertEquals($course->id, $event->objectid);
1605         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1606         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1607         $this->assertEquals('course_created', $event->get_legacy_eventname());
1608         $this->assertEventLegacyData($course, $event);
1609         $expectedlog = array(SITEID, 'course', 'new', 'view.php?id=' . $course->id, $course->fullname . ' (ID ' . $course->id . ')');
1610         $this->assertEventLegacyLogData($expectedlog, $event);
1612         // Now we want to trigger creating a course via the imsenterprise.
1613         // Delete the course we created earlier, as we want the imsenterprise plugin to create this.
1614         // We do not want print out any of the text this function generates while doing this, which is why
1615         // we are using ob_start() and ob_end_clean().
1616         ob_start();
1617         delete_course($course);
1618         ob_end_clean();
1620         // Create the XML file we want to use.
1621         $course->category = (array)$course->category;
1622         $imstestcase = new enrol_imsenterprise_testcase();
1623         $imstestcase->imsplugin = enrol_get_plugin('imsenterprise');
1624         $imstestcase->set_test_config();
1625         $imstestcase->set_xml_file(false, array($course));
1627         // Capture the event.
1628         $sink = $this->redirectEvents();
1629         $imstestcase->imsplugin->cron();
1630         $events = $sink->get_events();
1631         $sink->close();
1632         $event = null;
1633         foreach ($events as $eventinfo) {
1634             if ($eventinfo instanceof \core\event\course_created ) {
1635                 $event = $eventinfo;
1636                 break;
1637             }
1638         }
1640         // Validate the event triggered is \core\event\course_created. There is no need to validate the other values
1641         // as they have already been validated in the previous steps. Here we only want to make sure that when the
1642         // imsenterprise plugin creates a course an event is triggered.
1643         $this->assertInstanceOf('\core\event\course_created', $event);
1644         $this->assertEventContextNotUsed($event);
1645     }
1647     /**
1648      * Test that triggering a course_updated event works as expected.
1649      */
1650     public function test_course_updated_event() {
1651         global $DB;
1653         $this->resetAfterTest();
1655         // Create a course.
1656         $course = $this->getDataGenerator()->create_course();
1658         // Create a category we are going to move this course to.
1659         $category = $this->getDataGenerator()->create_category();
1661         // Create a hidden category we are going to move this course to.
1662         $categoryhidden = $this->getDataGenerator()->create_category(array('visible' => 0));
1664         // Update course and catch course_updated event.
1665         $sink = $this->redirectEvents();
1666         update_course($course);
1667         $events = $sink->get_events();
1668         $sink->close();
1670         // Get updated course information from the DB.
1671         $updatedcourse = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1672         // Validate event.
1673         $event = array_shift($events);
1674         $this->assertInstanceOf('\core\event\course_updated', $event);
1675         $this->assertEquals('course', $event->objecttable);
1676         $this->assertEquals($updatedcourse->id, $event->objectid);
1677         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1678         $url = new moodle_url('/course/edit.php', array('id' => $event->objectid));
1679         $this->assertEquals($url, $event->get_url());
1680         $this->assertEquals($updatedcourse, $event->get_record_snapshot('course', $event->objectid));
1681         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1682         $this->assertEventLegacyData($updatedcourse, $event);
1683         $expectedlog = array($updatedcourse->id, 'course', 'update', 'edit.php?id=' . $course->id, $course->id);
1684         $this->assertEventLegacyLogData($expectedlog, $event);
1686         // Move course and catch course_updated event.
1687         $sink = $this->redirectEvents();
1688         move_courses(array($course->id), $category->id);
1689         $events = $sink->get_events();
1690         $sink->close();
1692         // Return the moved course information from the DB.
1693         $movedcourse = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1694         // Validate event.
1695         $event = array_shift($events);
1696         $this->assertInstanceOf('\core\event\course_updated', $event);
1697         $this->assertEquals('course', $event->objecttable);
1698         $this->assertEquals($movedcourse->id, $event->objectid);
1699         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1700         $this->assertEquals($movedcourse, $event->get_record_snapshot('course', $movedcourse->id));
1701         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1702         $this->assertEventLegacyData($movedcourse, $event);
1703         $expectedlog = array($movedcourse->id, 'course', 'move', 'edit.php?id=' . $movedcourse->id, $movedcourse->id);
1704         $this->assertEventLegacyLogData($expectedlog, $event);
1706         // Move course to hidden category and catch course_updated event.
1707         $sink = $this->redirectEvents();
1708         move_courses(array($course->id), $categoryhidden->id);
1709         $events = $sink->get_events();
1710         $sink->close();
1712         // Return the moved course information from the DB.
1713         $movedcoursehidden = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1714         // Validate event.
1715         $event = array_shift($events);
1716         $this->assertInstanceOf('\core\event\course_updated', $event);
1717         $this->assertEquals('course', $event->objecttable);
1718         $this->assertEquals($movedcoursehidden->id, $event->objectid);
1719         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1720         $this->assertEquals($movedcoursehidden, $event->get_record_snapshot('course', $movedcoursehidden->id));
1721         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1722         $this->assertEventLegacyData($movedcoursehidden, $event);
1723         $expectedlog = array($movedcoursehidden->id, 'course', 'move', 'edit.php?id=' . $movedcoursehidden->id, $movedcoursehidden->id);
1724         $this->assertEventLegacyLogData($expectedlog, $event);
1725         $this->assertEventContextNotUsed($event);
1726     }
1728     /**
1729      * Test that triggering a course_deleted event works as expected.
1730      */
1731     public function test_course_deleted_event() {
1732         $this->resetAfterTest();
1734         // Create the course.
1735         $course = $this->getDataGenerator()->create_course();
1737         // Save the course context before we delete the course.
1738         $coursecontext = context_course::instance($course->id);
1740         // Catch the update event.
1741         $sink = $this->redirectEvents();
1743         // Call delete_course() which will trigger the course_deleted event and the course_content_deleted
1744         // event. This function prints out data to the screen, which we do not want during a PHPUnit test,
1745         // so use ob_start and ob_end_clean to prevent this.
1746         ob_start();
1747         delete_course($course);
1748         ob_end_clean();
1750         // Capture the event.
1751         $events = $sink->get_events();
1752         $sink->close();
1754         // Validate the event.
1755         $event = array_pop($events);
1756         $this->assertInstanceOf('\core\event\course_deleted', $event);
1757         $this->assertEquals('course', $event->objecttable);
1758         $this->assertEquals($course->id, $event->objectid);
1759         $this->assertEquals($coursecontext->id, $event->contextid);
1760         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1761         $this->assertEquals('course_deleted', $event->get_legacy_eventname());
1762         $eventdata = $event->get_data();
1763         $this->assertSame($course->idnumber, $eventdata['other']['idnumber']);
1764         $this->assertSame($course->fullname, $eventdata['other']['fullname']);
1765         $this->assertSame($course->shortname, $eventdata['other']['shortname']);
1767         // The legacy data also passed the context in the course object and substitutes timemodified with the current date.
1768         $expectedlegacy = clone($course);
1769         $expectedlegacy->context = $coursecontext;
1770         $expectedlegacy->timemodified = $event->timecreated;
1771         $this->assertEventLegacyData($expectedlegacy, $event);
1773         // Validate legacy log data.
1774         $expectedlog = array(SITEID, 'course', 'delete', 'view.php?id=' . $course->id, $course->fullname . '(ID ' . $course->id . ')');
1775         $this->assertEventLegacyLogData($expectedlog, $event);
1776         $this->assertEventContextNotUsed($event);
1777     }
1779     /**
1780      * Test that triggering a course_content_deleted event works as expected.
1781      */
1782     public function test_course_content_deleted_event() {
1783         global $DB;
1785         $this->resetAfterTest();
1787         // Create the course.
1788         $course = $this->getDataGenerator()->create_course();
1790         // Get the course from the DB. The data generator adds some extra properties, such as
1791         // numsections, to the course object which will fail the assertions later on.
1792         $course = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1794         // Save the course context before we delete the course.
1795         $coursecontext = context_course::instance($course->id);
1797         // Catch the update event.
1798         $sink = $this->redirectEvents();
1800         remove_course_contents($course->id, false);
1802         // Capture the event.
1803         $events = $sink->get_events();
1804         $sink->close();
1806         // Validate the event.
1807         $event = array_pop($events);
1808         $this->assertInstanceOf('\core\event\course_content_deleted', $event);
1809         $this->assertEquals('course', $event->objecttable);
1810         $this->assertEquals($course->id, $event->objectid);
1811         $this->assertEquals($coursecontext->id, $event->contextid);
1812         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1813         $this->assertEquals('course_content_removed', $event->get_legacy_eventname());
1814         // The legacy data also passed the context and options in the course object.
1815         $course->context = $coursecontext;
1816         $course->options = array();
1817         $this->assertEventLegacyData($course, $event);
1818         $this->assertEventContextNotUsed($event);
1819     }
1821     /**
1822      * Test that triggering a course_category_deleted event works as expected.
1823      */
1824     public function test_course_category_deleted_event() {
1825         $this->resetAfterTest();
1827         // Create a category.
1828         $category = $this->getDataGenerator()->create_category();
1830         // Save the context before it is deleted.
1831         $categorycontext = context_coursecat::instance($category->id);
1833         // Catch the update event.
1834         $sink = $this->redirectEvents();
1836         // Delete the category.
1837         $category->delete_full();
1839         // Capture the event.
1840         $events = $sink->get_events();
1841         $sink->close();
1843         // Validate the event.
1844         $event = $events[0];
1845         $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1846         $this->assertEquals('course_categories', $event->objecttable);
1847         $this->assertEquals($category->id, $event->objectid);
1848         $this->assertEquals($categorycontext->id, $event->contextid);
1849         $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1850         $this->assertEquals(null, $event->get_url());
1851         $this->assertEventLegacyData($category, $event);
1852         $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category->name . '(ID ' . $category->id . ')');
1853         $this->assertEventLegacyLogData($expectedlog, $event);
1855         // Create two categories.
1856         $category = $this->getDataGenerator()->create_category();
1857         $category2 = $this->getDataGenerator()->create_category();
1859         // Save the context before it is moved and then deleted.
1860         $category2context = context_coursecat::instance($category2->id);
1862         // Catch the update event.
1863         $sink = $this->redirectEvents();
1865         // Move the category.
1866         $category2->delete_move($category->id);
1868         // Capture the event.
1869         $events = $sink->get_events();
1870         $sink->close();
1872         // Validate the event.
1873         $event = $events[0];
1874         $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1875         $this->assertEquals('course_categories', $event->objecttable);
1876         $this->assertEquals($category2->id, $event->objectid);
1877         $this->assertEquals($category2context->id, $event->contextid);
1878         $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1879         $this->assertEventLegacyData($category2, $event);
1880         $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category2->name . '(ID ' . $category2->id . ')');
1881         $this->assertEventLegacyLogData($expectedlog, $event);
1882         $this->assertEventContextNotUsed($event);
1883     }
1885     /**
1886      * Test that triggering a course_restored event works as expected.
1887      */
1888     public function test_course_restored_event() {
1889         global $CFG;
1891         // Get the necessary files to perform backup and restore.
1892         require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
1893         require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
1895         $this->resetAfterTest();
1897         // Set to admin user.
1898         $this->setAdminUser();
1900         // The user id is going to be 2 since we are the admin user.
1901         $userid = 2;
1903         // Create a course.
1904         $course = $this->getDataGenerator()->create_course();
1906         // Create backup file and save it to the backup location.
1907         $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
1908             backup::INTERACTIVE_NO, backup::MODE_GENERAL, $userid);
1909         $bc->execute_plan();
1910         $results = $bc->get_results();
1911         $file = $results['backup_destination'];
1912         $fp = get_file_packer('application/vnd.moodle.backup');
1913         $filepath = $CFG->dataroot . '/temp/backup/test-restore-course-event';
1914         $file->extract_to_pathname($fp, $filepath);
1915         $bc->destroy();
1917         // Now we want to catch the restore course event.
1918         $sink = $this->redirectEvents();
1920         // Now restore the course to trigger the event.
1921         $rc = new restore_controller('test-restore-course-event', $course->id, backup::INTERACTIVE_NO,
1922             backup::MODE_GENERAL, $userid, backup::TARGET_NEW_COURSE);
1923         $rc->execute_precheck();
1924         $rc->execute_plan();
1926         // Capture the event.
1927         $events = $sink->get_events();
1928         $sink->close();
1930         // Validate the event.
1931         $event = array_pop($events);
1932         $this->assertInstanceOf('\core\event\course_restored', $event);
1933         $this->assertEquals('course', $event->objecttable);
1934         $this->assertEquals($rc->get_courseid(), $event->objectid);
1935         $this->assertEquals(context_course::instance($rc->get_courseid())->id, $event->contextid);
1936         $this->assertEquals('course_restored', $event->get_legacy_eventname());
1937         $legacydata = (object) array(
1938             'courseid' => $rc->get_courseid(),
1939             'userid' => $rc->get_userid(),
1940             'type' => $rc->get_type(),
1941             'target' => $rc->get_target(),
1942             'mode' => $rc->get_mode(),
1943             'operation' => $rc->get_operation(),
1944             'samesite' => $rc->is_samesite()
1945         );
1946         $url = new moodle_url('/course/view.php', array('id' => $event->objectid));
1947         $this->assertEquals($url, $event->get_url());
1948         $this->assertEventLegacyData($legacydata, $event);
1949         $this->assertEventContextNotUsed($event);
1951         // Destroy the resource controller since we are done using it.
1952         $rc->destroy();
1953     }
1955     /**
1956      * Test that triggering a course_section_updated event works as expected.
1957      */
1958     public function test_course_section_updated_event() {
1959         global $DB;
1961         $this->resetAfterTest();
1963         // Create the course with sections.
1964         $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true));
1965         $sections = $DB->get_records('course_sections', array('course' => $course->id));
1967         $coursecontext = context_course::instance($course->id);
1969         $section = array_pop($sections);
1970         $section->name = 'Test section';
1971         $section->summary = 'Test section summary';
1972         $DB->update_record('course_sections', $section);
1974         // Trigger an event for course section update.
1975         $event = \core\event\course_section_updated::create(
1976                 array(
1977                     'objectid' => $section->id,
1978                     'courseid' => $course->id,
1979                     'context' => context_course::instance($course->id),
1980                     'other' => array(
1981                         'sectionnum' => $section->section
1982                     )
1983                 )
1984             );
1985         $event->add_record_snapshot('course_sections', $section);
1986         // Trigger and catch event.
1987         $sink = $this->redirectEvents();
1988         $event->trigger();
1989         $events = $sink->get_events();
1990         $sink->close();
1992         // Validate the event.
1993         $event = $events[0];
1994         $this->assertInstanceOf('\core\event\course_section_updated', $event);
1995         $this->assertEquals('course_sections', $event->objecttable);
1996         $this->assertEquals($section->id, $event->objectid);
1997         $this->assertEquals($course->id, $event->courseid);
1998         $this->assertEquals($coursecontext->id, $event->contextid);
1999         $this->assertEquals($section->section, $event->other['sectionnum']);
2000         $expecteddesc = "The user with id '{$event->userid}' updated section number '{$event->other['sectionnum']}' for the course with id '{$event->courseid}'";
2001         $this->assertEquals($expecteddesc, $event->get_description());
2002         $url = new moodle_url('/course/editsection.php', array('id' => $event->objectid));
2003         $this->assertEquals($url, $event->get_url());
2004         $this->assertEquals($section, $event->get_record_snapshot('course_sections', $event->objectid));
2005         $id = $section->id;
2006         $sectionnum = $section->section;
2007         $expectedlegacydata = array($course->id, "course", "editsection", 'editsection.php?id=' . $id, $sectionnum);
2008         $this->assertEventLegacyLogData($expectedlegacydata, $event);
2009         $this->assertEventContextNotUsed($event);
2010     }
2012     /**
2013      * Test that triggering a course_section_deleted event works as expected.
2014      */
2015     public function test_course_section_deleted_event() {
2016         global $USER, $DB;
2017         $this->resetAfterTest();
2018         $sink = $this->redirectEvents();
2020         // Create the course with sections.
2021         $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true));
2022         $sections = $DB->get_records('course_sections', array('course' => $course->id));
2023         $coursecontext = context_course::instance($course->id);
2024         $section = array_pop($sections);
2025         course_delete_section($course, $section);
2026         $events = $sink->get_events();
2027         $event = array_pop($events); // Delete section event.
2028         $sink->close();
2030         // Validate event data.
2031         $this->assertInstanceOf('\core\event\course_section_deleted', $event);
2032         $this->assertEquals('course_sections', $event->objecttable);
2033         $this->assertEquals($section->id, $event->objectid);
2034         $this->assertEquals($course->id, $event->courseid);
2035         $this->assertEquals($coursecontext->id, $event->contextid);
2036         $this->assertEquals($section->section, $event->other['sectionnum']);
2037         $expecteddesc = "The user with id '{$event->userid}' deleted section number '{$event->other['sectionnum']}' " .
2038                 "(section name '{$event->other['sectionname']}') for the course with id '{$event->courseid}'";
2039         $this->assertEquals($expecteddesc, $event->get_description());
2040         $this->assertEquals($section, $event->get_record_snapshot('course_sections', $event->objectid));
2041         $this->assertNull($event->get_url());
2043         // Test legacy data.
2044         $sectionnum = $section->section;
2045         $expectedlegacydata = array($course->id, "course", "delete section", 'view.php?id=' . $course->id, $sectionnum);
2046         $this->assertEventLegacyLogData($expectedlegacydata, $event);
2047         $this->assertEventContextNotUsed($event);
2048     }
2050     public function test_course_integrity_check() {
2051         global $DB;
2053         $this->resetAfterTest(true);
2054         $course = $this->getDataGenerator()->create_course(array('numsections' => 1),
2055            array('createsections'=>true));
2057         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
2058                 array('section' => 0));
2059         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id),
2060                 array('section' => 0));
2061         $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id),
2062                 array('section' => 0));
2063         $correctseq = join(',', array($forum->cmid, $page->cmid, $quiz->cmid));
2065         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2066         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2067         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2068         $this->assertEquals($correctseq, $section0->sequence);
2069         $this->assertEmpty($section1->sequence);
2070         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2071         $this->assertEquals($section0->id, $cms[$page->cmid]->section);
2072         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2073         $this->assertEmpty(course_integrity_check($course->id));
2075         // Now let's make manual change in DB and let course_integrity_check() fix it:
2077         // 1. Module appears twice in one section.
2078         $DB->update_record('course_sections', array('id' => $section0->id, 'sequence' => $section0->sequence. ','. $page->cmid));
2079         $this->assertEquals(
2080                 array('Failed integrity check for course ['. $course->id.
2081                 ']. Sequence for course section ['. $section0->id. '] is "'.
2082                 $section0->sequence. ','. $page->cmid. '", must be "'.
2083                 $section0->sequence. '"'),
2084                 course_integrity_check($course->id));
2085         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2086         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2087         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2088         $this->assertEquals($correctseq, $section0->sequence);
2089         $this->assertEmpty($section1->sequence);
2090         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2091         $this->assertEquals($section0->id, $cms[$page->cmid]->section);
2092         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2094         // 2. Module appears in two sections (last section wins).
2095         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''. $page->cmid));
2096         // First message about double mentioning in sequence, second message about wrong section field for $page.
2097         $this->assertEquals(array(
2098             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
2099             '] must be removed from sequence of section ['. $section0->id.
2100             '] because it is also present in sequence of section ['. $section1->id. ']',
2101             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
2102             '] points to section ['. $section0->id. '] instead of ['. $section1->id. ']'),
2103                 course_integrity_check($course->id));
2104         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2105         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2106         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2107         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
2108         $this->assertEquals(''. $page->cmid, $section1->sequence);
2109         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2110         $this->assertEquals($section1->id, $cms[$page->cmid]->section);
2111         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2113         // 3. Module id is not present in course_section.sequence (integrity check with $fullcheck = false).
2114         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''));
2115         $this->assertEmpty(course_integrity_check($course->id)); // Not an error!
2116         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2117         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2118         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2119         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
2120         $this->assertEmpty($section1->sequence);
2121         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2122         $this->assertEquals($section1->id, $cms[$page->cmid]->section); // Not changed.
2123         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2125         // 4. Module id is not present in course_section.sequence (integrity check with $fullcheck = true).
2126         $this->assertEquals(array('Failed integrity check for course ['. $course->id. ']. Course module ['.
2127                 $page->cmid. '] is missing from sequence of section ['. $section1->id. ']'),
2128                 course_integrity_check($course->id, null, null, true)); // Error!
2129         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2130         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2131         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2132         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
2133         $this->assertEquals(''. $page->cmid, $section1->sequence);  // Yay, module added to section.
2134         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2135         $this->assertEquals($section1->id, $cms[$page->cmid]->section); // Not changed.
2136         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2138         // 5. Module id is not present in course_section.sequence and it's section is invalid (integrity check with $fullcheck = true).
2139         $DB->update_record('course_modules', array('id' => $page->cmid, 'section' => 8765));
2140         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''));
2141         $this->assertEquals(array(
2142             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
2143             '] is missing from sequence of section ['. $section0->id. ']',
2144             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
2145             '] points to section [8765] instead of ['. $section0->id. ']'),
2146                 course_integrity_check($course->id, null, null, true));
2147         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2148         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2149         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2150         $this->assertEquals($forum->cmid. ','. $quiz->cmid. ','. $page->cmid, $section0->sequence); // Module added to section.
2151         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2152         $this->assertEquals($section0->id, $cms[$page->cmid]->section); // Section changed to section0.
2153         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2155         // 6. Module is deleted from course_modules but not deleted in sequence (integrity check with $fullcheck = true).
2156         $DB->delete_records('course_modules', array('id' => $page->cmid));
2157         $this->assertEquals(array('Failed integrity check for course ['. $course->id. ']. Course module ['.
2158                 $page->cmid. '] does not exist but is present in the sequence of section ['. $section0->id. ']'),
2159                 course_integrity_check($course->id, null, null, true));
2160         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2161         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2162         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2163         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
2164         $this->assertEmpty($section1->sequence);
2165         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2166         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2167         $this->assertEquals(2, count($cms));
2168     }
2170     /**
2171      * Tests for event related to course module creation.
2172      */
2173     public function test_course_module_created_event() {
2174         global $USER, $DB;
2175         $this->resetAfterTest();
2177         // Create an assign module.
2178         $sink = $this->redirectEvents();
2179         $modinfo = $this->create_specific_module_test('assign');
2180         $events = $sink->get_events();
2181         $event = array_pop($events);
2183         $cm = get_coursemodule_from_id('assign', $modinfo->coursemodule, 0, false, MUST_EXIST);
2184         $mod = $DB->get_record('assign', array('id' => $modinfo->instance), '*', MUST_EXIST);
2186         // Validate event data.
2187         $this->assertInstanceOf('\core\event\course_module_created', $event);
2188         $this->assertEquals($cm->id, $event->objectid);
2189         $this->assertEquals($USER->id, $event->userid);
2190         $this->assertEquals('course_modules', $event->objecttable);
2191         $url = new moodle_url('/mod/assign/view.php', array('id' => $cm->id));
2192         $this->assertEquals($url, $event->get_url());
2194         // Test legacy data.
2195         $this->assertSame('mod_created', $event->get_legacy_eventname());
2196         $eventdata = new stdClass();
2197         $eventdata->modulename = 'assign';
2198         $eventdata->name       = $mod->name;
2199         $eventdata->cmid       = $cm->id;
2200         $eventdata->courseid   = $cm->course;
2201         $eventdata->userid     = $USER->id;
2202         $this->assertEventLegacyData($eventdata, $event);
2204         $arr = array(
2205             array($cm->course, "course", "add mod", "../mod/assign/view.php?id=$cm->id", "assign $cm->instance"),
2206             array($cm->course, "assign", "add", "view.php?id=$cm->id", $cm->instance, $cm->id)
2207         );
2208         $this->assertEventLegacyLogData($arr, $event);
2209         $this->assertEventContextNotUsed($event);
2211         // Let us see if duplicating an activity results in a nice course module created event.
2212         $sink->clear();
2213         $course = get_course($mod->course);
2214         $newcm = duplicate_module($course, $cm);
2215         $events = $sink->get_events();
2216         $event = array_pop($events);
2217         $sink->close();
2219         // Validate event data.
2220         $this->assertInstanceOf('\core\event\course_module_created', $event);
2221         $this->assertEquals($newcm->id, $event->objectid);
2222         $this->assertEquals($USER->id, $event->userid);
2223         $this->assertEquals($course->id, $event->courseid);
2224         $url = new moodle_url('/mod/assign/view.php', array('id' => $newcm->id));
2225         $this->assertEquals($url, $event->get_url());
2226     }
2228     /**
2229      * Tests for event validations related to course module creation.
2230      */
2231     public function test_course_module_created_event_exceptions() {
2233         $this->resetAfterTest();
2235         // Generate data.
2236         $modinfo = $this->create_specific_module_test('assign');
2237         $context = context_module::instance($modinfo->coursemodule);
2239         // Test not setting instanceid.
2240         try {
2241             $event = \core\event\course_module_created::create(array(
2242                 'courseid' => $modinfo->course,
2243                 'context'  => $context,
2244                 'objectid' => $modinfo->coursemodule,
2245                 'other'    => array(
2246                     'modulename' => 'assign',
2247                     'name'       => 'My assignment',
2248                 )
2249             ));
2250             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2251                     other['instanceid']");
2252         } catch (coding_exception $e) {
2253             $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
2254         }
2256         // Test not setting modulename.
2257         try {
2258             $event = \core\event\course_module_created::create(array(
2259                 'courseid' => $modinfo->course,
2260                 'context'  => $context,
2261                 'objectid' => $modinfo->coursemodule,
2262                 'other'    => array(
2263                     'instanceid' => $modinfo->instance,
2264                     'name'       => 'My assignment',
2265                 )
2266             ));
2267             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2268                     other['modulename']");
2269         } catch (coding_exception $e) {
2270             $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
2271         }
2273         // Test not setting name.
2275         try {
2276             $event = \core\event\course_module_created::create(array(
2277                 'courseid' => $modinfo->course,
2278                 'context'  => $context,
2279                 'objectid' => $modinfo->coursemodule,
2280                 'other'    => array(
2281                     'modulename' => 'assign',
2282                     'instanceid' => $modinfo->instance,
2283                 )
2284             ));
2285             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2286                     other['name']");
2287         } catch (coding_exception $e) {
2288             $this->assertContains("The 'name' value must be set in other.", $e->getMessage());
2289         }
2291     }
2293     /**
2294      * Tests for event related to course module updates.
2295      */
2296     public function test_course_module_updated_event() {
2297         global $USER, $DB;
2298         $this->resetAfterTest();
2300         // Update a forum module.
2301         $sink = $this->redirectEvents();
2302         $modinfo = $this->update_specific_module_test('forum');
2303         $events = $sink->get_events();
2304         $event = array_pop($events);
2305         $sink->close();
2307         $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
2308         $mod = $DB->get_record('forum', array('id' => $cm->instance), '*', MUST_EXIST);
2310         // Validate event data.
2311         $this->assertInstanceOf('\core\event\course_module_updated', $event);
2312         $this->assertEquals($cm->id, $event->objectid);
2313         $this->assertEquals($USER->id, $event->userid);
2314         $this->assertEquals('course_modules', $event->objecttable);
2315         $url = new moodle_url('/mod/forum/view.php', array('id' => $cm->id));
2316         $this->assertEquals($url, $event->get_url());
2318         // Test legacy data.
2319         $this->assertSame('mod_updated', $event->get_legacy_eventname());
2320         $eventdata = new stdClass();
2321         $eventdata->modulename = 'forum';
2322         $eventdata->name       = $mod->name;
2323         $eventdata->cmid       = $cm->id;
2324         $eventdata->courseid   = $cm->course;
2325         $eventdata->userid     = $USER->id;
2326         $this->assertEventLegacyData($eventdata, $event);
2328         $arr = array(
2329             array($cm->course, "course", "update mod", "../mod/forum/view.php?id=$cm->id", "forum $cm->instance"),
2330             array($cm->course, "forum", "update", "view.php?id=$cm->id", $cm->instance, $cm->id)
2331         );
2332         $this->assertEventLegacyLogData($arr, $event);
2333         $this->assertEventContextNotUsed($event);
2334     }
2336     /**
2337      * Tests for create_from_cm method.
2338      */
2339     public function test_course_module_create_from_cm() {
2340         $this->resetAfterTest();
2341         $this->setAdminUser();
2343         // Create course and modules.
2344         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
2346         // Generate an assignment.
2347         $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
2349         // Get the module context.
2350         $modcontext = context_module::instance($assign->cmid);
2352         // Get course module.
2353         $cm = get_coursemodule_from_id(null, $assign->cmid, $course->id, false, MUST_EXIST);
2355         // Create an event from course module.
2356         $event = \core\event\course_module_updated::create_from_cm($cm, $modcontext);
2358         // Trigger the events.
2359         $sink = $this->redirectEvents();
2360         $event->trigger();
2361         $events = $sink->get_events();
2362         $event2 = array_pop($events);
2364         // Test event data.
2365         $this->assertInstanceOf('\core\event\course_module_updated', $event);
2366         $this->assertEquals($cm->id, $event2->objectid);
2367         $this->assertEquals($modcontext, $event2->get_context());
2368         $this->assertEquals($cm->modname, $event2->other['modulename']);
2369         $this->assertEquals($cm->instance, $event2->other['instanceid']);
2370         $this->assertEquals($cm->name, $event2->other['name']);
2371         $this->assertEventContextNotUsed($event2);
2372         $this->assertSame('mod_updated', $event2->get_legacy_eventname());
2373         $arr = array(
2374             array($cm->course, "course", "update mod", "../mod/assign/view.php?id=$cm->id", "assign $cm->instance"),
2375             array($cm->course, "assign", "update", "view.php?id=$cm->id", $cm->instance, $cm->id)
2376         );
2377         $this->assertEventLegacyLogData($arr, $event);
2378     }
2380     /**
2381      * Tests for event validations related to course module update.
2382      */
2383     public function test_course_module_updated_event_exceptions() {
2385         $this->resetAfterTest();
2387         // Generate data.
2388         $modinfo = $this->create_specific_module_test('assign');
2389         $context = context_module::instance($modinfo->coursemodule);
2391         // Test not setting instanceid.
2392         try {
2393             $event = \core\event\course_module_updated::create(array(
2394                 'courseid' => $modinfo->course,
2395                 'context'  => $context,
2396                 'objectid' => $modinfo->coursemodule,
2397                 'other'    => array(
2398                     'modulename' => 'assign',
2399                     'name'       => 'My assignment',
2400                 )
2401             ));
2402             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2403                     other['instanceid']");
2404         } catch (coding_exception $e) {
2405             $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
2406         }
2408         // Test not setting modulename.
2409         try {
2410             $event = \core\event\course_module_updated::create(array(
2411                 'courseid' => $modinfo->course,
2412                 'context'  => $context,
2413                 'objectid' => $modinfo->coursemodule,
2414                 'other'    => array(
2415                     'instanceid' => $modinfo->instance,
2416                     'name'       => 'My assignment',
2417                 )
2418             ));
2419             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2420                     other['modulename']");
2421         } catch (coding_exception $e) {
2422             $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
2423         }
2425         // Test not setting name.
2427         try {
2428             $event = \core\event\course_module_updated::create(array(
2429                 'courseid' => $modinfo->course,
2430                 'context'  => $context,
2431                 'objectid' => $modinfo->coursemodule,
2432                 'other'    => array(
2433                     'modulename' => 'assign',
2434                     'instanceid' => $modinfo->instance,
2435                 )
2436             ));
2437             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2438                     other['name']");
2439         } catch (coding_exception $e) {
2440             $this->assertContains("The 'name' value must be set in other.", $e->getMessage());
2441         }
2443     }
2445     /**
2446      * Tests for event related to course module delete.
2447      */
2448     public function test_course_module_deleted_event() {
2449         global $USER, $DB;
2450         $this->resetAfterTest();
2452         // Create and delete a module.
2453         $sink = $this->redirectEvents();
2454         $modinfo = $this->create_specific_module_test('forum');
2455         $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
2456         course_delete_module($modinfo->coursemodule);
2457         $events = $sink->get_events();
2458         $event = array_pop($events); // delete module event.;
2459         $sink->close();
2461         // Validate event data.
2462         $this->assertInstanceOf('\core\event\course_module_deleted', $event);
2463         $this->assertEquals($cm->id, $event->objectid);
2464         $this->assertEquals($USER->id, $event->userid);
2465         $this->assertEquals('course_modules', $event->objecttable);
2466         $this->assertEquals(null, $event->get_url());
2467         $this->assertEquals($cm, $event->get_record_snapshot('course_modules', $cm->id));
2469         // Test legacy data.
2470         $this->assertSame('mod_deleted', $event->get_legacy_eventname());
2471         $eventdata = new stdClass();
2472         $eventdata->modulename = 'forum';
2473         $eventdata->cmid       = $cm->id;
2474         $eventdata->courseid   = $cm->course;
2475         $eventdata->userid     = $USER->id;
2476         $this->assertEventLegacyData($eventdata, $event);
2478         $arr = array($cm->course, 'course', "delete mod", "view.php?id=$cm->course", "forum $cm->instance", $cm->id);
2479         $this->assertEventLegacyLogData($arr, $event);
2481     }
2483     /**
2484      * Tests for event validations related to course module deletion.
2485      */
2486     public function test_course_module_deleted_event_exceptions() {
2488         $this->resetAfterTest();
2490         // Generate data.
2491         $modinfo = $this->create_specific_module_test('assign');
2492         $context = context_module::instance($modinfo->coursemodule);
2494         // Test not setting instanceid.
2495         try {
2496             $event = \core\event\course_module_deleted::create(array(
2497                 'courseid' => $modinfo->course,
2498                 'context'  => $context,
2499                 'objectid' => $modinfo->coursemodule,
2500                 'other'    => array(
2501                     'modulename' => 'assign',
2502                     'name'       => 'My assignment',
2503                 )
2504             ));
2505             $this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
2506                     other['instanceid']");
2507         } catch (coding_exception $e) {
2508             $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
2509         }
2511         // Test not setting modulename.
2512         try {
2513             $event = \core\event\course_module_deleted::create(array(
2514                 'courseid' => $modinfo->course,
2515                 'context'  => $context,
2516                 'objectid' => $modinfo->coursemodule,
2517                 'other'    => array(
2518                     'instanceid' => $modinfo->instance,
2519                     'name'       => 'My assignment',
2520                 )
2521             ));
2522             $this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
2523                     other['modulename']");
2524         } catch (coding_exception $e) {
2525             $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
2526         }
2527     }
2529     /**
2530      * Returns a user object and its assigned new role.
2531      *
2532      * @param testing_data_generator $generator
2533      * @param $contextid
2534      * @return array The user object and the role ID
2535      */
2536     protected function get_user_objects(testing_data_generator $generator, $contextid) {
2537         global $USER;
2539         if (empty($USER->id)) {
2540             $user  = $generator->create_user();
2541             $this->setUser($user);
2542         }
2543         $roleid = create_role('Test role', 'testrole', 'Test role description');
2544         if (!is_array($contextid)) {
2545             $contextid = array($contextid);
2546         }
2547         foreach ($contextid as $cid) {
2548             $assignid = role_assign($roleid, $user->id, $cid);
2549         }
2550         return array($user, $roleid);
2551     }
2553     /**
2554      * Test course move after course.
2555      */
2556     public function test_course_change_sortorder_after_course() {
2557         global $DB;
2559         $this->resetAfterTest(true);
2561         $generator = $this->getDataGenerator();
2562         $category = $generator->create_category();
2563         $course3 = $generator->create_course(array('category' => $category->id));
2564         $course2 = $generator->create_course(array('category' => $category->id));
2565         $course1 = $generator->create_course(array('category' => $category->id));
2566         $context = $category->get_context();
2568         list($user, $roleid) = $this->get_user_objects($generator, $context->id);
2569         $caps = course_capability_assignment::allow('moodle/category:manage', $roleid, $context->id);
2571         $courses = $category->get_courses();
2572         $this->assertInternalType('array', $courses);
2573         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2574         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2575         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2577         // Test moving down.
2578         $this->assertTrue(course_change_sortorder_after_course($course1->id, $course3->id));
2579         $courses = $category->get_courses();
2580         $this->assertInternalType('array', $courses);
2581         $this->assertEquals(array($course2->id, $course3->id, $course1->id), array_keys($courses));
2582         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2583         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2585         // Test moving up.
2586         $this->assertTrue(course_change_sortorder_after_course($course1->id, $course2->id));
2587         $courses = $category->get_courses();
2588         $this->assertInternalType('array', $courses);
2589         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
2590         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2591         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2593         // Test moving to the top.
2594         $this->assertTrue(course_change_sortorder_after_course($course1->id, 0));
2595         $courses = $category->get_courses();
2596         $this->assertInternalType('array', $courses);
2597         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2598         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2599         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2600     }
2602     /**
2603      * Tests changing the visibility of a course.
2604      */
2605     public function test_course_change_visibility() {
2606         global $DB;
2608         $this->resetAfterTest(true);
2610         $generator = $this->getDataGenerator();
2611         $category = $generator->create_category();
2612         $course = $generator->create_course(array('category' => $category->id));
2614         $this->assertEquals('1', $course->visible);
2615         $this->assertEquals('1', $course->visibleold);
2617         $this->assertTrue(course_change_visibility($course->id, false));
2618         $course = $DB->get_record('course', array('id' => $course->id));
2619         $this->assertEquals('0', $course->visible);
2620         $this->assertEquals('0', $course->visibleold);
2622         $this->assertTrue(course_change_visibility($course->id, true));
2623         $course = $DB->get_record('course', array('id' => $course->id));
2624         $this->assertEquals('1', $course->visible);
2625         $this->assertEquals('1', $course->visibleold);
2626     }
2628     /**
2629      * Tests moving the course up and down by one.
2630      */
2631     public function test_course_change_sortorder_by_one() {
2632         global $DB;
2634         $this->resetAfterTest(true);
2636         $generator = $this->getDataGenerator();
2637         $category = $generator->create_category();
2638         $course3 = $generator->create_course(array('category' => $category->id));
2639         $course2 = $generator->create_course(array('category' => $category->id));
2640         $course1 = $generator->create_course(array('category' => $category->id));
2642         $courses = $category->get_courses();
2643         $this->assertInternalType('array', $courses);
2644         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2645         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2646         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2648         // Test moving down.
2649         $course1 = get_course($course1->id);
2650         $this->assertTrue(course_change_sortorder_by_one($course1, false));
2651         $courses = $category->get_courses();
2652         $this->assertInternalType('array', $courses);
2653         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
2654         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2655         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2657         // Test moving up.
2658         $course1 = get_course($course1->id);
2659         $this->assertTrue(course_change_sortorder_by_one($course1, true));
2660         $courses = $category->get_courses();
2661         $this->assertInternalType('array', $courses);
2662         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2663         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2664         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2666         // Test moving the top course up one.
2667         $course1 = get_course($course1->id);
2668         $this->assertFalse(course_change_sortorder_by_one($course1, true));
2669         // Check nothing changed.
2670         $courses = $category->get_courses();
2671         $this->assertInternalType('array', $courses);
2672         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2673         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2674         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2676         // Test moving the bottom course up down.
2677         $course3 = get_course($course3->id);
2678         $this->assertFalse(course_change_sortorder_by_one($course3, false));
2679         // Check nothing changed.
2680         $courses = $category->get_courses();
2681         $this->assertInternalType('array', $courses);
2682         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2683         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2684         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2685     }
2687     public function test_view_resources_list() {
2688         $this->resetAfterTest();
2690         $course = self::getDataGenerator()->create_course();
2691         $coursecontext = context_course::instance($course->id);
2693         $event = \core\event\course_resources_list_viewed::create(array('context' => context_course::instance($course->id)));
2694         $event->set_legacy_logdata(array('book', 'page', 'resource'));
2695         $sink = $this->redirectEvents();
2696         $event->trigger();
2697         $events = $sink->get_events();
2698         $sink->close();
2700         // Validate the event.
2701         $event = $events[0];
2702         $this->assertInstanceOf('\core\event\course_resources_list_viewed', $event);
2703         $this->assertEquals(null, $event->objecttable);
2704         $this->assertEquals(null, $event->objectid);
2705         $this->assertEquals($course->id, $event->courseid);
2706         $this->assertEquals($coursecontext->id, $event->contextid);
2707         $expectedlegacydata = array(
2708             array($course->id, "book", "view all", 'index.php?id=' . $course->id, ''),
2709             array($course->id, "page", "view all", 'index.php?id=' . $course->id, ''),
2710             array($course->id, "resource", "view all", 'index.php?id=' . $course->id, ''),
2711         );
2712         $this->assertEventLegacyLogData($expectedlegacydata, $event);
2713         $this->assertEventContextNotUsed($event);
2714     }
2716     /**
2717      * Test duplicate_module()
2718      */
2719     public function test_duplicate_module() {
2720         $this->setAdminUser();
2721         $this->resetAfterTest();
2722         $course = self::getDataGenerator()->create_course();
2723         $res = self::getDataGenerator()->create_module('resource', array('course' => $course));
2724         $cm = get_coursemodule_from_id('resource', $res->cmid, 0, false, MUST_EXIST);
2726         $newcm = duplicate_module($course, $cm);
2728         // Make sure they are the same, except obvious id changes.
2729         foreach ($cm as $prop => $value) {
2730             if ($prop == 'id' || $prop == 'url' || $prop == 'instance' || $prop == 'added') {
2731                 // Ignore obviously different properties.
2732                 continue;
2733             }
2734             $this->assertEquals($value, $newcm->$prop);
2735         }
2736     }
2738     /**
2739      * Tests that when creating or updating a module, if the availability settings
2740      * are present but set to an empty tree, availability is set to null in
2741      * database.
2742      */
2743     public function test_empty_availability_settings() {
2744         global $DB;
2745         $this->setAdminUser();
2746         $this->resetAfterTest();
2748         // Enable availability.
2749         set_config('enableavailability', 1);
2751         // Test add.
2752         $emptyavailability = json_encode(\core_availability\tree::get_root_json(array()));
2753         $course = self::getDataGenerator()->create_course();
2754         $label = self::getDataGenerator()->create_module('label', array(
2755                 'course' => $course, 'availability' => $emptyavailability));
2756         $this->assertNull($DB->get_field('course_modules', 'availability',
2757                 array('id' => $label->cmid)));
2759         // Test update.
2760         $formdata = $DB->get_record('course_modules', array('id' => $label->cmid));
2761         unset($formdata->availability);
2762         $formdata->availabilityconditionsjson = $emptyavailability;
2763         $formdata->modulename = 'label';
2764         $formdata->coursemodule = $label->cmid;
2765         $draftid = 0;
2766         file_prepare_draft_area($draftid, context_module::instance($label->cmid)->id,
2767                 'mod_label', 'intro', 0);
2768         $formdata->introeditor = array(
2769             'itemid' => $draftid,
2770             'text' => '<p>Yo</p>',
2771             'format' => FORMAT_HTML);
2772         update_module($formdata);
2773         $this->assertNull($DB->get_field('course_modules', 'availability',
2774                 array('id' => $label->cmid)));
2775     }
2777     /**
2778      * Test update_inplace_editable()
2779      */
2780     public function test_update_module_name_inplace() {
2781         global $CFG, $DB, $PAGE;
2782         require_once($CFG->dirroot . '/lib/external/externallib.php');
2784         $this->setUser($this->getDataGenerator()->create_user());
2786         $this->resetAfterTest(true);
2787         $course = $this->getDataGenerator()->create_course();
2788         $forum = self::getDataGenerator()->create_module('forum', array('course' => $course->id, 'name' => 'forum name'));
2790         // Call service for core_course component without necessary permissions.
2791         try {
2792             core_external::update_inplace_editable('core_course', 'activityname', $forum->cmid, 'New forum name');
2793             $this->fail('Exception expected');
2794         } catch (moodle_exception $e) {
2795             $this->assertEquals('Course or activity not accessible. (Not enrolled)',
2796                 $e->getMessage());
2797         }
2799         // Change to admin user and make sure that cm name can be updated using web service update_inplace_editable().
2800         $this->setAdminUser();
2801         $res = core_external::update_inplace_editable('core_course', 'activityname', $forum->cmid, 'New forum name');
2802         $res = external_api::clean_returnvalue(core_external::update_inplace_editable_returns(), $res);
2803         $this->assertEquals('New forum name', $res['value']);
2804         $this->assertEquals('New forum name', $DB->get_field('forum', 'name', array('id' => $forum->id)));
2805     }
2807     /**
2808      * Testing function course_get_tagged_course_modules - search tagged course modules
2809      */
2810     public function test_course_get_tagged_course_modules() {
2811         global $DB;
2812         $this->resetAfterTest();
2813         $course3 = $this->getDataGenerator()->create_course();
2814         $course2 = $this->getDataGenerator()->create_course();
2815         $course1 = $this->getDataGenerator()->create_course();
2816         $cm11 = $this->getDataGenerator()->create_module('assign', array('course' => $course1->id,
2817             'tags' => 'Cat, Dog'));
2818         $cm12 = $this->getDataGenerator()->create_module('page', array('course' => $course1->id,
2819             'tags' => 'Cat, Mouse', 'visible' => 0));
2820         $cm13 = $this->getDataGenerator()->create_module('page', array('course' => $course1->id,
2821             'tags' => 'Cat, Mouse, Dog'));
2822         $cm21 = $this->getDataGenerator()->create_module('forum', array('course' => $course2->id,
2823             'tags' => 'Cat, Mouse'));
2824         $cm31 = $this->getDataGenerator()->create_module('forum', array('course' => $course3->id,
2825             'tags' => 'Cat, Mouse'));
2827         // Admin is able to view everything.
2828         $this->setAdminUser();
2829         $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'),
2830                 /*$exclusivemode = */false, /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$page = */0);
2831         $this->assertRegExp('/'.$cm11->name.'/', $res->content);
2832         $this->assertRegExp('/'.$cm12->name.'/', $res->content);
2833         $this->assertRegExp('/'.$cm13->name.'/', $res->content);
2834         $this->assertRegExp('/'.$cm21->name.'/', $res->content);
2835         $this->assertRegExp('/'.$cm31->name.'/', $res->content);
2836         // Results from course1 are returned before results from course2.
2837         $this->assertTrue(strpos($res->content, $cm11->name) < strpos($res->content, $cm21->name));
2839         // Ordinary user is not able to see anything.
2840         $user = $this->getDataGenerator()->create_user();
2841         $this->setUser($user);
2843         $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'),
2844                 /*$exclusivemode = */false, /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$page = */0);
2845         $this->assertNull($res);
2847         // Enrol user as student in course1 and course2.
2848         $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
2849         $this->getDataGenerator()->enrol_user($user->id, $course1->id, $roleids['student']);
2850         $this->getDataGenerator()->enrol_user($user->id, $course2->id, $roleids['student']);
2851         core_tag_index_builder::reset_caches();
2853         // Searching in the course context returns visible modules in this course.
2854         $context = context_course::instance($course1->id);
2855         $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'),
2856                 /*$exclusivemode = */false, /*$fromctx = */0, /*$ctx = */$context->id, /*$rec = */1, /*$page = */0);
2857         $this->assertRegExp('/'.$cm11->name.'/', $res->content);
2858         $this->assertNotRegExp('/'.$cm12->name.'/', $res->content);
2859         $this->assertRegExp('/'.$cm13->name.'/', $res->content);
2860         $this->assertNotRegExp('/'.$cm21->name.'/', $res->content);
2861         $this->assertNotRegExp('/'.$cm31->name.'/', $res->content);
2863         // Searching FROM the course context returns visible modules in all courses.
2864         $context = context_course::instance($course2->id);
2865         $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'),
2866                 /*$exclusivemode = */false, /*$fromctx = */$context->id, /*$ctx = */0, /*$rec = */1, /*$page = */0);
2867         $this->assertRegExp('/'.$cm11->name.'/', $res->content);
2868         $this->assertNotRegExp('/'.$cm12->name.'/', $res->content);
2869         $this->assertRegExp('/'.$cm13->name.'/', $res->content);
2870         $this->assertRegExp('/'.$cm21->name.'/', $res->content);
2871         $this->assertNotRegExp('/'.$cm31->name.'/', $res->content); // No access to course3.
2872         // Results from course2 are returned before results from course1.
2873         $this->assertTrue(strpos($res->content, $cm21->name) < strpos($res->content, $cm11->name));
2875         // Enrol user in course1 as a teacher - now he should be able to see hidden module.
2876         $this->getDataGenerator()->enrol_user($user->id, $course1->id, $roleids['editingteacher']);
2877         get_fast_modinfo(0,0,true);
2879         $context = context_course::instance($course1->id);
2880         $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'),
2881                 /*$exclusivemode = */false, /*$fromctx = */$context->id, /*$ctx = */0, /*$rec = */1, /*$page = */0);
2882         $this->assertRegExp('/'.$cm12->name.'/', $res->content);
2884         // Create more modules and try pagination.
2885         $cm14 = $this->getDataGenerator()->create_module('assign', array('course' => $course1->id,
2886             'tags' => 'Cat, Dog'));
2887         $cm15 = $this->getDataGenerator()->create_module('page', array('course' => $course1->id,
2888             'tags' => 'Cat, Mouse', 'visible' => 0));
2889         $cm16 = $this->getDataGenerator()->create_module('page', array('course' => $course1->id,
2890             'tags' => 'Cat, Mouse, Dog'));
2892         $context = context_course::instance($course1->id);
2893         $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'),
2894                 /*$exclusivemode = */false, /*$fromctx = */0, /*$ctx = */$context->id, /*$rec = */1, /*$page = */0);
2895         $this->assertRegExp('/'.$cm11->name.'/', $res->content);
2896         $this->assertRegExp('/'.$cm12->name.'/', $res->content);
2897         $this->assertRegExp('/'.$cm13->name.'/', $res->content);
2898         $this->assertNotRegExp('/'.$cm21->name.'/', $res->content);
2899         $this->assertRegExp('/'.$cm14->name.'/', $res->content);
2900         $this->assertRegExp('/'.$cm15->name.'/', $res->content);
2901         $this->assertNotRegExp('/'.$cm16->name.'/', $res->content);
2902         $this->assertNotRegExp('/'.$cm31->name.'/', $res->content); // No access to course3.
2903         $this->assertEmpty($res->prevpageurl);
2904         $this->assertNotEmpty($res->nextpageurl);
2906         $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'),
2907                 /*$exclusivemode = */false, /*$fromctx = */0, /*$ctx = */$context->id, /*$rec = */1, /*$page = */1);
2908         $this->assertNotRegExp('/'.$cm11->name.'/', $res->content);
2909         $this->assertNotRegExp('/'.$cm12->name.'/', $res->content);
2910         $this->assertNotRegExp('/'.$cm13->name.'/', $res->content);
2911         $this->assertNotRegExp('/'.$cm21->name.'/', $res->content);
2912         $this->assertNotRegExp('/'.$cm14->name.'/', $res->content);
2913         $this->assertNotRegExp('/'.$cm15->name.'/', $res->content);
2914         $this->assertRegExp('/'.$cm16->name.'/', $res->content);
2915         $this->assertNotRegExp('/'.$cm31->name.'/', $res->content); // No access to course3.
2916         $this->assertNotEmpty($res->prevpageurl);
2917         $this->assertEmpty($res->nextpageurl);
2918     }
2920     /**
2921      * Test course_get_user_navigation_options for frontpage.
2922      */
2923     public function test_course_get_user_navigation_options_for_frontpage() {
2924         global $CFG, $SITE, $DB;
2925         $this->resetAfterTest();
2926         $context = context_system::instance();
2927         $course = clone $SITE;
2928         $this->setAdminUser();
2930         $navoptions = course_get_user_navigation_options($context, $course);
2931         $this->assertTrue($navoptions->blogs);
2932         $this->assertTrue($navoptions->notes);
2933         $this->assertTrue($navoptions->participants);
2934         $this->assertTrue($navoptions->badges);
2935         $this->assertTrue($navoptions->tags);
2936         $this->assertFalse($navoptions->search);
2937         $this->assertTrue($navoptions->calendar);
2939         // Enable global search now.
2940         $CFG->enableglobalsearch = 1;
2941         $navoptions = course_get_user_navigation_options($context, $course);
2942         $this->assertTrue($navoptions->search);
2944         // Now try with a standard user.
2945         $user = $this->getDataGenerator()->create_user();
2946         $this->setUser($user);
2947         $navoptions = course_get_user_navigation_options($context, $course);
2948         $this->assertTrue($navoptions->blogs);
2949         $this->assertFalse($navoptions->notes);
2950         $this->assertFalse($navoptions->participants);
2951         $this->assertTrue($navoptions->badges);
2952         $this->assertTrue($navoptions->tags);
2953         $this->assertTrue($navoptions->search);
2954         $this->assertTrue($navoptions->calendar);
2956         // Standar using viewing frontpage settings from a course where is enrolled.
2957         $course = self::getDataGenerator()->create_course();
2958         // Create a viewer user.
2959         $viewer = self::getDataGenerator()->create_user();
2960         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
2961         $this->getDataGenerator()->enrol_user($viewer->id, $course->id, $studentrole->id);
2962         $this->setUser($viewer);
2964         $navoptions = course_get_user_navigation_options($context, $course);
2965         $this->assertTrue($navoptions->blogs);
2966         $this->assertFalse($navoptions->notes);
2967         $this->assertTrue($navoptions->participants);
2968         $this->assertTrue($navoptions->badges);
2969         $this->assertTrue($navoptions->tags);
2970         $this->assertTrue($navoptions->search);
2971         $this->assertTrue($navoptions->calendar);
2972     }
2974     /**
2975      * Test course_get_user_navigation_options for managers in a normal course.
2976      */
2977     public function test_course_get_user_navigation_options_for_managers() {
2978         global $CFG;
2979         $this->resetAfterTest();
2980         $course = $this->getDataGenerator()->create_course();
2981         $context = context_course::instance($course->id);
2982         $this->setAdminUser();
2984         $navoptions = course_get_user_navigation_options($context);
2985         $this->assertTrue($navoptions->blogs);
2986         $this->assertTrue($navoptions->notes);
2987         $this->assertTrue($navoptions->participants);
2988         $this->assertTrue($navoptions->badges);
2989     }
2991     /**
2992      * Test course_get_user_navigation_options for students in a normal course.
2993      */
2994     public function test_course_get_user_navigation_options_for_students() {
2995         global $DB, $CFG;
2996         $this->resetAfterTest();
2997         $course = $this->getDataGenerator()->create_course();
2998         $context = context_course::instance($course->id);
3000         $user = $this->getDataGenerator()->create_user();
3001         $roleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
3002         $this->getDataGenerator()->enrol_user($user->id, $course->id, $roleid);
3004         $this->setUser($user);
3006         $navoptions = course_get_user_navigation_options($context);
3007         $this->assertTrue($navoptions->blogs);
3008         $this->assertFalse($navoptions->notes);
3009         $this->assertTrue($navoptions->participants);
3010         $this->assertTrue($navoptions->badges);
3012         // Disable some options.
3013         $CFG->badges_allowcoursebadges = 0;
3014         $CFG->enableblogs = 0;
3015         // Disable view participants capability.
3016         assign_capability('moodle/course:viewparticipants', CAP_PROHIBIT, $roleid, $context);
3017         $context->mark_dirty();
3019         $navoptions = course_get_user_navigation_options($context);
3020         $this->assertFalse($navoptions->blogs);
3021         $this->assertFalse($navoptions->notes);
3022         $this->assertFalse($navoptions->participants);
3023         $this->assertFalse($navoptions->badges);
3024     }
3026     /**
3027      * Test course_get_user_administration_options for frontpage.
3028      */
3029     public function test_course_get_user_administration_options_for_frontpage() {
3030         global $CFG, $SITE;
3031         $this->resetAfterTest();
3032         $course = clone $SITE;
3033         $context = context_course::instance($course->id);
3034         $this->setAdminUser();
3036         $adminoptions = course_get_user_administration_options($course, $context);
3037         $this->assertTrue($adminoptions->update);
3038         $this->assertTrue($adminoptions->filters);
3039         $this->assertTrue($adminoptions->reports);
3040         $this->assertTrue($adminoptions->backup);
3041         $this->assertTrue($adminoptions->restore);
3042         $this->assertFalse($adminoptions->files);
3043         $this->assertFalse($adminoptions->tags);
3045         // Now try with a standard user.
3046         $user = $this->getDataGenerator()->create_user();
3047         $this->setUser($user);
3048         $adminoptions = course_get_user_administration_options($course, $context);
3049         $this->assertFalse($adminoptions->update);
3050         $this->assertFalse($adminoptions->filters);
3051         $this->assertFalse($adminoptions->reports);
3052         $this->assertFalse($adminoptions->backup);
3053         $this->assertFalse($adminoptions->restore);
3054         $this->assertFalse($adminoptions->files);
3055         $this->assertFalse($adminoptions->tags);
3057     }
3059     /**
3060      * Test course_get_user_administration_options for managers in a normal course.
3061      */
3062     public function test_course_get_user_administration_options_for_managers() {
3063         global $CFG;
3064         $this->resetAfterTest();
3065         $course = $this->getDataGenerator()->create_course();
3066         $context = context_course::instance($course->id);
3067         $this->setAdminUser();
3069         $adminoptions = course_get_user_administration_options($course, $context);
3070         $this->assertTrue($adminoptions->update);
3071         $this->assertTrue($adminoptions->filters);
3072         $this->assertTrue($adminoptions->reports);
3073         $this->assertTrue($adminoptions->backup);
3074         $this->assertTrue($adminoptions->restore);
3075         $this->assertFalse($adminoptions->files);
3076         $this->assertTrue($adminoptions->tags);
3077         $this->assertTrue($adminoptions->gradebook);
3078         $this->assertFalse($adminoptions->outcomes);
3079         $this->assertTrue($adminoptions->badges);
3080         $this->assertTrue($adminoptions->import);
3081         $this->assertTrue($adminoptions->publish);
3082         $this->assertTrue($adminoptions->reset);
3083         $this->assertTrue($adminoptions->roles);
3084     }
3086     /**
3087      * Test course_get_user_administration_options for students in a normal course.
3088      */
3089     public function test_course_get_user_administration_options_for_students() {
3090         global $DB, $CFG;
3091         $this->resetAfterTest();
3092         $course = $this->getDataGenerator()->create_course();
3093         $context = context_course::instance($course->id);
3095         $user = $this->getDataGenerator()->create_user();
3096         $roleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
3097         $this->getDataGenerator()->enrol_user($user->id, $course->id, $roleid);
3099         $this->setUser($user);
3100         $adminoptions = course_get_user_administration_options($course, $context);
3102         $this->assertFalse($adminoptions->update);
3103         $this->assertFalse($adminoptions->filters);
3104         $this->assertFalse($adminoptions->reports);
3105         $this->assertFalse($adminoptions->backup);
3106         $this->assertFalse($adminoptions->restore);
3107         $this->assertFalse($adminoptions->files);
3108         $this->assertFalse($adminoptions->tags);
3109         $this->assertFalse($adminoptions->gradebook);
3110         $this->assertFalse($adminoptions->outcomes);
3111         $this->assertTrue($adminoptions->badges);
3112         $this->assertFalse($adminoptions->import);
3113         $this->assertFalse($adminoptions->publish);
3114         $this->assertFalse($adminoptions->reset);
3115         $this->assertFalse($adminoptions->roles);
3117         $CFG->enablebadges = false;
3118         $adminoptions = course_get_user_administration_options($course, $context);
3119         $this->assertFalse($adminoptions->badges);
3120     }
3122     /**
3123      * Test test_update_course_frontpage_category.
3124      */
3125     public function test_update_course_frontpage_category() {
3126         // Fetch front page course.
3127         $course = get_course(SITEID);
3128         // Test update information on front page course.
3129         $course->category = 99;
3130         $this->expectException('moodle_exception');
3131         $this->expectExceptionMessage(get_string('invalidcourse', 'error'));
3132         update_course($course);
3133     }
3135     /**
3136      * test_course_enddate
3137      *
3138      * @dataProvider course_enddate_provider
3139      * @param int $startdate
3140      * @param int $enddate
3141      * @param string $errorcode
3142      */
3143     public function test_course_enddate($startdate, $enddate, $errorcode) {
3145         $this->resetAfterTest(true);
3147         $record = array('startdate' => $startdate, 'enddate' => $enddate);
3148         try {
3149             $course1 = $this->getDataGenerator()->create_course($record);
3150             if ($errorcode !== false) {
3151                 $this->fail('Expected exception with "' . $errorcode . '" error code in create_create');
3152             }
3153         } catch (moodle_exception $e) {
3154             if ($errorcode === false) {
3155                 $this->fail('Got "' . $errorcode . '" exception error code and no exception was expected');
3156             }
3157             if ($e->errorcode != $errorcode) {
3158                 $this->fail('Got "' . $e->errorcode. '" exception error code and "' . $errorcode . '" was expected');
3159             }
3160             return;
3161         }
3163         $this->assertEquals($startdate, $course1->startdate);
3164         $this->assertEquals($enddate, $course1->enddate);
3165     }
3167     /**
3168      * Provider for test_course_enddate.
3169      *
3170      * @return array
3171      */
3172     public function course_enddate_provider() {
3173         // Each provided example contains startdate, enddate and the expected exception error code if there is any.
3174         return [
3175             [
3176                 111,
3177                 222,
3178                 false
3179             ], [
3180                 222,
3181                 111,
3182                 'enddatebeforestartdate'
3183             ], [
3184                 111,
3185                 0,
3186                 false
3187             ], [
3188                 0,
3189                 222,
3190                 'nostartdatenoenddate'
3191             ]
3192         ];
3193     }
3196     /**
3197      * test_course_dates_reset
3198      *
3199      * @dataProvider course_dates_reset_provider
3200      * @param int $startdate
3201      * @param int $enddate
3202      * @param int $resetstartdate
3203      * @param int $resetenddate
3204      * @param int $resultingstartdate
3205      * @param int $resultingenddate
3206      */
3207     public function test_course_dates_reset($startdate, $enddate, $resetstartdate, $resetenddate, $resultingstartdate, $resultingenddate) {
3208         global $DB;
3210         $this->resetAfterTest(true);
3212         $this->setTimezone('UTC');
3214         $record = array('startdate' => $startdate, 'enddate' => $enddate);
3215         $originalcourse = $this->getDataGenerator()->create_course($record);
3217         $resetdata = new stdClass();
3218         $resetdata->id = $originalcourse->id;
3219         $resetdata->reset_start_date_old = $originalcourse->startdate;
3220         $resetdata->reset_start_date = $resetstartdate;
3221         $resetdata->reset_end_date = $resetenddate;
3222         $resetdata->reset_end_date_old = $record['enddate'];
3223         reset_course_userdata($resetdata);
3225         $course = $DB->get_record('course', array('id' => $originalcourse->id));
3227         $this->assertEquals($resultingstartdate, $course->startdate);
3228         $this->assertEquals($resultingenddate, $course->enddate);
3229     }
3231     /**
3232      * Provider for test_course_dates_reset.
3233      *
3234      * @return array
3235      */
3236     public function course_dates_reset_provider() {
3238         // Each example contains the following:
3239         // - course startdate
3240         // - course enddate
3241         // - startdate to reset to (false if not reset)
3242         // - enddate to reset to (false if not reset)
3243         // - resulting startdate
3244         // - resulting enddate
3245         $time = 1445644800;
3246         return [
3247             // No date changes.
3248             [
3249                 $time,
3250                 $time + DAYSECS,
3251                 false,
3252                 false,
3253                 $time,
3254                 $time + DAYSECS
3255             ],
3256             // End date changes to a valid value.
3257             [
3258                 $time,
3259                 $time + DAYSECS,
3260                 false,
3261                 $time + DAYSECS + 111,
3262                 $time,
3263                 $time + DAYSECS + 111
3264             ],
3265             // Start date changes to a valid value. End date does not get updated because it does not have value.
3266             [
3267                 $time,
3268                 0,
3269                 $time + DAYSECS,
3270                 false,
3271                 $time + DAYSECS,
3272                 0
3273             ],
3274             // Start date changes to a valid value. End date gets updated accordingly.
3275             [
3276                 $time,
3277                 $time + DAYSECS,
3278                 $time + WEEKSECS,
3279                 false,
3280                 $time + WEEKSECS,
3281                 $time + WEEKSECS + DAYSECS
3282             ],
3283             // Start date and end date change to a valid value.
3284             [
3285                 $time,
3286                 $time + DAYSECS,
3287                 $time + WEEKSECS,
3288                 $time + YEARSECS,
3289                 $time + WEEKSECS,
3290                 $time + YEARSECS
3291             ]
3292         ];
3293     }
3295     public function test_course_check_module_updates_since() {
3296         global $CFG, $DB, $USER;
3297         require_once($CFG->dirroot . '/mod/glossary/lib.php');
3298         require_once($CFG->dirroot . '/rating/lib.php');
3299         require_once($CFG->dirroot . '/comment/lib.php');
3301         $this->resetAfterTest(true);
3303         $CFG->enablecompletion = true;
3304         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
3305         $glossary = $this->getDataGenerator()->create_module('glossary', array(
3306             'course' => $course->id,
3307             'completion' => COMPLETION_TRACKING_AUTOMATIC,
3308             'completionview' => 1,
3309             'allowcomments' => 1,
3310             'assessed' => RATING_AGGREGATE_AVERAGE,
3311             'scale' => 100
3312         ));
3313         $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
3314         $context = context_module::instance($glossary->cmid);
3315         $modinfo = get_fast_modinfo($course);
3316         $cm = $modinfo->get_cm($glossary->cmid);
3317         $user = $this->getDataGenerator()->create_user();
3318         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
3319         $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
3320         $from = time();
3322         $teacher = $this->getDataGenerator()->create_user();
3323         $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
3324         $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
3326         assign_capability('mod/glossary:viewanyrating', CAP_ALLOW, $studentrole->id, $context->id, true);
3328         // Check nothing changed right now.
3329         $updates = course_check_module_updates_since($cm, $from);
3330         $this->assertFalse($updates->configuration->updated);
3331         $this->assertFalse($updates->completion->updated);
3332         $this->assertFalse($updates->gradeitems->updated);
3333         $this->assertFalse($updates->comments->updated);
3334         $this->assertFalse($updates->ratings->updated);
3335         $this->assertFalse($updates->introfiles->updated);
3336         $this->assertFalse($updates->outcomes->updated);
3338         $this->waitForSecond();
3340         // Do some changes.
3341         $this->setUser($user);
3342         $entry = $glossarygenerator->create_content($glossary);
3344         $this->setUser($teacher);
3345         // Name.
3346         set_coursemodule_name($glossary->cmid, 'New name');
3348         // Add some ratings.
3349         $rm = new rating_manager();
3350         $result = $rm->add_rating($cm, $context, 'mod_glossary', 'entry', $entry->id, 100, 50, $user->id, RATING_AGGREGATE_AVERAGE);
3352         // Change grades.
3353         $glossary->cmidnumber = $glossary->cmid;
3354         glossary_update_grades($glossary, $user->id);
3356         $this->setUser($user);
3357         // Completion status.
3358         glossary_view($glossary, $course, $cm, $context, 'letter');
3360         // Add one comment.
3361         $args = new stdClass;
3362         $args->context   = $context;
3363         $args->course    = $course;
3364         $args->cm        = $cm;
3365         $args->area      = 'glossary_entry';
3366         $args->itemid    = $entry->id;
3367         $args->client_id = 1;
3368         $args->component = 'mod_glossary';
3369         $manager = new comment($args);
3370         $manager->add('blah blah blah');
3372         // Check upgrade status.
3373         $updates = course_check_module_updates_since($cm, $from);
3374         $this->assertTrue($updates->configuration->updated);
3375         $this->assertTrue($updates->completion->updated);
3376         $this->assertTrue($updates->gradeitems->updated);
3377         $this->assertTrue($updates->comments->updated);
3378         $this->assertTrue($updates->ratings->updated);
3379         $this->assertFalse($updates->introfiles->updated);
3380         $this->assertFalse($updates->outcomes->updated);
3381     }
3383     public function test_async_module_deletion_hook_implemented() {
3384         // Async module deletion depends on the 'true' being returned by at least one plugin implementing the hook,
3385         // 'course_module_adhoc_deletion_recommended'. In core, is implemented by the course recyclebin, which will only return
3386         // true if the recyclebin plugin is enabled. To make sure async deletion occurs, this test force-enables the recyclebin.
3387         global $DB, $USER;
3388         $this->resetAfterTest(true);
3389         $this->setAdminUser();
3391         // Ensure recyclebin is enabled.
3392         set_config('coursebinenable', true, 'tool_recyclebin');
3394         // Create course, module and context.
3395         $course = $this->getDataGenerator()->create_course(['numsections' => 5]);
3396         $module = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]);
3397         $modcontext = context_module::instance($module->cmid);
3399         // Verify context exists.
3400         $this->assertInstanceOf('context_module', $modcontext);
3402         // Check events generated on the course_delete_module call.
3403         $sink = $this->redirectEvents();
3405         // Try to delete the module using the async flag.
3406         course_delete_module($module->cmid, true); // Try to delete the module asynchronously.
3408         // Verify that no event has been generated yet.
3409         $events = $sink->get_events();
3410         $event = array_pop($events);
3411         $sink->close();
3412         $this->assertEmpty($event);
3414         // Grab the record, in it's final state before hard deletion, for comparison with the event snapshot.
3415         // We need to do this because the 'deletioninprogress' flag has changed from '0' to '1'.
3416         $cm = $DB->get_record('course_modules', ['id' => $module->cmid], '*', MUST_EXIST);
3418         // Verify the course_module is marked as 'deletioninprogress'.
3419         $this->assertNotEquals($cm, false);
3420         $this->assertEquals($cm->deletioninprogress, '1');
3422         // Verify the context has not yet been removed.
3423         $this->assertEquals($modcontext, context_module::instance($module->cmid, IGNORE_MISSING));
3425         // Set up a sink to catch the 'course_module_deleted' event.
3426         $sink = $this->redirectEvents();
3428         // Now, run the adhoc task which performs the hard deletion.
3429         phpunit_util::run_all_adhoc_tasks();
3431         // Fetch and validate the event data.
3432         $events = $sink->get_events();
3433         $event = array_pop($events);
3434         $sink->close();
3435         $this->assertInstanceOf('\core\event\course_module_deleted', $event);
3436         $this->assertEquals($module->cmid, $event->objectid);
3437         $this->assertEquals($USER->id, $event->userid);
3438         $this->assertEquals('course_modules', $event->objecttable);
3439         $this->assertEquals(null, $event->get_url());
3440         $this->assertEquals($cm, $event->get_record_snapshot('course_modules', $module->cmid));
3442         // Verify the context has been removed.
3443         $this->assertFalse(context_module::instance($module->cmid, IGNORE_MISSING));
3445         // Verify the course_module record has been deleted.
3446         $cmcount = $DB->count_records('course_modules', ['id' => $module->cmid]);
3447         $this->assertEmpty($cmcount);
3448     }
3450     public function test_async_module_deletion_hook_not_implemented() {
3451         // Only proceed if we are sure that no plugin is going to advocate async removal of a module. I.e. no plugin returns
3452         // 'true' from the 'course_module_adhoc_deletion_recommended' hook.
3453         // In the case of core, only recyclebin implements this hook, and it will only return true if enabled, so disable it.
3454         global $DB, $USER;
3455         $this->resetAfterTest(true);
3456         $this->setAdminUser();
3457         set_config('coursebinenable', false, 'tool_recyclebin');
3459         // Non-core plugins might implement the 'course_module_adhoc_deletion_recommended' hook and spoil this test.
3460         // If at least one plugin still returns true, then skip this test.
3461         if ($pluginsfunction = get_plugins_with_function('course_module_background_deletion_recommended')) {
3462             foreach ($pluginsfunction as $plugintype => $plugins) {
3463                 foreach ($plugins as $pluginfunction) {
3464                     if ($pluginfunction()) {
3465                         $this->markTestSkipped();
3466                     }
3467                 }
3468             }
3469         }
3471         // Create course, module and context.
3472         $course = $this->getDataGenerator()->create_course(['numsections' => 5]);
3473         $module = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]);
3474         $modcontext = context_module::instance($module->cmid);
3475         $cm = $DB->get_record('course_modules', ['id' => $module->cmid], '*', MUST_EXIST);
3477         // Verify context exists.
3478         $this->assertInstanceOf('context_module', $modcontext);
3480         // Check events generated on the course_delete_module call.
3481         $sink = $this->redirectEvents();
3483         // Try to delete the module using the async flag.
3484         course_delete_module($module->cmid, true); // Try to delete the module asynchronously.
3486         // Fetch and validate the event data.
3487         $events = $sink->get_events();
3488         $event = array_pop($events);
3489         $sink->close();
3490         $this->assertInstanceOf('\core\event\course_module_deleted', $event);
3491         $this->assertEquals($module->cmid, $event->objectid);
3492         $this->assertEquals($USER->id, $event->userid);
3493         $this->assertEquals('course_modules', $event->objecttable);
3494         $this->assertEquals(null, $event->get_url());
3495         $this->assertEquals($cm, $event->get_record_snapshot('course_modules', $module->cmid));
3497         // Verify the context has been removed.
3498         $this->assertFalse(context_module::instance($module->cmid, IGNORE_MISSING));
3500         // Verify the course_module record has been deleted.
3501         $cmcount = $DB->count_records('course_modules', ['id' => $module->cmid]);
3502         $this->assertEmpty($cmcount);
3503     }
3505     public function test_async_section_deletion_hook_implemented() {
3506         // Async section deletion (provided section contains modules), depends on the 'true' being returned by at least one plugin
3507         // implementing the 'course_module_adhoc_deletion_recommended' hook. In core, is implemented by the course recyclebin,
3508         // which will only return true if the plugin is enabled. To make sure async deletion occurs, this test enables recyclebin.
3509         global $DB, $USER;
3510         $this->resetAfterTest(true);
3511         $this->setAdminUser();
3513         // Ensure recyclebin is enabled.
3514         set_config('coursebinenable', true, 'tool_recyclebin');
3516         // Create course, module and context.
3517         $generator = $this->getDataGenerator();
3518         $course = $generator->create_course(['numsections' => 4, 'format' => 'topics'], ['createsections' => true]);
3519         $assign0 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
3520         $assign1 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
3521         $assign2 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
3522         $assign3 = $generator->create_module('assign', ['course' => $course, 'section' => 0]);
3524         // Delete empty section. No difference from normal, synchronous behaviour.
3525         $this->assertTrue(course_delete_section($course, 4, false, true));
3526         $this->assertEquals(3, course_get_format($course)->get_course()->numsections);
3528         // Delete a module in section 2 (using async). Need to verify this doesn't generate two tasks when we delete
3529         // the section in the next step.
3530         course_delete_module($assign2->cmid, true);
3532         // Confirm that the module is pending deletion in its current section.
3533         $section = $DB->get_record('course_sections', ['course' => $course->id, 'section' => '2']); // For event comparison.
3534         $this->assertEquals(true, $DB->record_exists('course_modules', ['id' => $assign2->cmid, 'deletioninprogress' => 1,
3535                                                      'section' => $section->id]));
3537         // Now, delete section 2.
3538         $this->assertFalse(course_delete_section($course, 2, false, true)); // Non-empty section, no forcedelete, so no change.
3540         $sink = $this->redirectEvents(); // To capture the event.
3541         $this->assertTrue(course_delete_section($course, 2, true, true));
3543         // Now, confirm that:
3544         // a) the section's modules have been flagged for deletion and moved to section 0 and;
3545         // b) the section has been deleted and;
3546         // c) course_section_deleted event has been fired. The course_module_deleted events will only fire once they have been
3547         // removed from section 0 via the adhoc task.
3549         // Modules should have been flagged for deletion and moved to section 0.
3550         $sectionid = $DB->get_field('course_sections', 'id', ['course' => $course->id, 'section' => 0]);
3551         $this->assertEquals(3, $DB->count_records('course_modules', ['section' => $sectionid, 'deletioninprogress' => 1]));
3553         // Confirm the section has been deleted.
3554         $this->assertEquals(2, course_get_format($course)->get_course()->numsections);
3556         // Check event fired.
3557         $events = $sink->get_events();
3558         $event = array_pop($events);
3559         $sink->close();
3560         $this->assertInstanceOf('\core\event\course_section_deleted', $event);
3561         $this->assertEquals($section->id, $event->objectid);
3562         $this->assertEquals($USER->id, $event->userid);
3563         $this->assertEquals('course_sections', $event->objecttable);
3564         $this->assertEquals(null, $event->get_url());
3565         $this->assertEquals($section, $event->get_record_snapshot('course_sections', $section->id));
3567         // Now, run the adhoc task to delete the modules from section 0.
3568         $sink = $this->redirectEvents(); // To capture the events.
3569         phpunit_util::run_all_adhoc_tasks();
3571         // Confirm the modules have been deleted.
3572         list($insql, $assignids) = $DB->get_in_or_equal([$assign0->cmid, $assign1->cmid, $assign2->cmid]);
3573         $cmcount = $DB->count_records_select('course_modules', 'id ' . $insql,  $assignids);
3574         $this->assertEmpty($cmcount);
3576         // Confirm other modules in section 0 still remain.
3577         $this->assertEquals(1, $DB->count_records('course_modules', ['id' => $assign3->cmid]));
3579         // Confirm that events were generated for all 3 of the modules.
3580         $events = $sink->get_events();
3581         $sink->close();
3582         $count = 0;
3583         while (!empty($events)) {
3584             $event = array_pop($events);
3585             if ($event instanceof \core\event\course_module_deleted &&
3586                 in_array($event->objectid, [$assign0->cmid, $assign1->cmid, $assign2->cmid])) {
3587                 $count++;
3588             }
3589         }
3590         $this->assertEquals(3, $count);
3591     }
3593     public function test_async_section_deletion_hook_not_implemented() {
3594         // If no plugins advocate async removal, then normal synchronous removal will take place.
3595         // Only proceed if we are sure that no plugin is going to advocate async removal of a module. I.e. no plugin returns
3596         // 'true' from the 'course_module_adhoc_deletion_recommended' hook.
3597         // In the case of core, only recyclebin implements this hook, and it will only return true if enabled, so disable it.
3598         global $DB, $USER;
3599         $this->resetAfterTest(true);
3600         $this->setAdminUser();
3601         set_config('coursebinenable', false, 'tool_recyclebin');
3603         // Non-core plugins might implement the 'course_module_adhoc_deletion_recommended' hook and spoil this test.
3604         // If at least one plugin still returns true, then skip this test.
3605         if ($pluginsfunction = get_plugins_with_function('course_module_background_deletion_recommended')) {
3606             foreach ($pluginsfunction as $plugintype => $plugins) {
3607                 foreach ($plugins as $pluginfunction) {
3608                     if ($pluginfunction()) {
3609                         $this->markTestSkipped();
3610                     }
3611                 }
3612             }
3613         }
3615         // Create course, module and context.
3616         $generator = $this->getDataGenerator();
3617         $course = $generator->create_course(['numsections' => 4, 'format' => 'topics'], ['createsections' => true]);
3618         $assign0 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
3619         $assign1 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
3621         // Delete empty section. No difference from normal, synchronous behaviour.
3622         $this->assertTrue(course_delete_section($course, 4, false, true));
3623         $this->assertEquals(3, course_get_format($course)->get_course()->numsections);
3625         // Delete section in the middle (2).
3626         $section = $DB->get_record('course_sections', ['course' => $course->id, 'section' => '2']); // For event comparison.
3627         $this->assertFalse(course_delete_section($course, 2, false, true)); // Non-empty section, no forcedelete, so no change.
3629         $sink = $this->redirectEvents(); // To capture the event.
3630         $this->assertTrue(course_delete_section($course, 2, true, true));
3632         // Now, confirm that:
3633         // a) The section's modules have deleted and;
3634         // b) the section has been deleted and;
3635         // c) course_section_deleted event has been fired and;
3636         // d) course_module_deleted events have both been fired.
3638         // Confirm modules have been deleted.
3639         list($insql, $assignids) = $DB->get_in_or_equal([$assign0->cmid, $assign1->cmid]);
3640         $cmcount = $DB->count_records_select('course_modules', 'id ' . $insql, $assignids);
3641         $this->assertEmpty($cmcount);
3643         // Confirm the section has been deleted.
3644         $this->assertEquals(2, course_get_format($course)->get_course()->numsections);
3646         // Confirm the course_section_deleted event has been generated.
3647         $events = $sink->get_events();
3648         $event = array_pop($events);
3649         $sink->close();
3650         $this->assertInstanceOf('\core\event\course_section_deleted', $event);
3651         $this->assertEquals($section->id, $event->objectid);
3652         $this->assertEquals($USER->id, $event->userid);
3653         $this->assertEquals('course_sections', $event->objecttable);
3654         $this->assertEquals(null, $event->get_url());
3655         $this->assertEquals($section, $event->get_record_snapshot('course_sections', $section->id));
3657         // Confirm that the course_module_deleted events have both been generated.
3658         $count = 0;
3659         while (!empty($events)) {
3660             $event = array_pop($events);
3661             if ($event instanceof \core\event\course_module_deleted &&
3662                 in_array($event->objectid, [$assign0->cmid, $assign1->cmid])) {
3663                 $count++;
3664             }
3665         }
3666         $this->assertEquals(2, $count);
3667     }