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