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