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