297e54b4c852cb2b899be67c8526632c525c0a54
[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         $this->assertFalse(course_can_delete_section($courseweeks, 1));
989         $this->assertTrue(course_can_delete_section($courseweeks, 2));
991         // Student does not have permissions to delete sections.
992         $this->setUser($student);
993         $this->assertFalse(course_can_delete_section($courseweeks, 1));
994         $this->assertFalse(course_can_delete_section($coursetopics, 1));
995         $this->assertFalse(course_can_delete_section($coursesingleactivity, 1));
996     }
998     public function test_course_delete_section() {
999         global $DB;
1000         $this->resetAfterTest(true);
1002         $generator = $this->getDataGenerator();
1004         $course = $generator->create_course(array('numsections' => 6, 'format' => 'topics'),
1005             array('createsections' => true));
1006         $assign0 = $generator->create_module('assign', array('course' => $course, 'section' => 0));
1007         $assign1 = $generator->create_module('assign', array('course' => $course, 'section' => 1));
1008         $assign21 = $generator->create_module('assign', array('course' => $course, 'section' => 2));
1009         $assign22 = $generator->create_module('assign', array('course' => $course, 'section' => 2));
1010         $assign3 = $generator->create_module('assign', array('course' => $course, 'section' => 3));
1011         $assign5 = $generator->create_module('assign', array('course' => $course, 'section' => 5));
1012         $assign6 = $generator->create_module('assign', array('course' => $course, 'section' => 6));
1014         $this->setAdminUser();
1016         // Attempt to delete non-existing section.
1017         $this->assertFalse(course_delete_section($course, 10, false));
1018         $this->assertFalse(course_delete_section($course, 9, true));
1020         // Attempt to delete 0-section.
1021         $this->assertFalse(course_delete_section($course, 0, true));
1022         $this->assertTrue($DB->record_exists('course_modules', array('id' => $assign0->cmid)));
1024         // Delete last section.
1025         $this->assertTrue(course_delete_section($course, 6, true));
1026         $this->assertFalse($DB->record_exists('course_modules', array('id' => $assign6->cmid)));
1027         $this->assertEquals(5, course_get_format($course)->get_last_section_number());
1029         // Delete empty section.
1030         $this->assertTrue(course_delete_section($course, 4, false));
1031         $this->assertEquals(4, course_get_format($course)->get_last_section_number());
1033         // Delete section in the middle (2).
1034         $this->assertFalse(course_delete_section($course, 2, false));
1035         $this->assertTrue(course_delete_section($course, 2, true));
1036         $this->assertFalse($DB->record_exists('course_modules', array('id' => $assign21->cmid)));
1037         $this->assertFalse($DB->record_exists('course_modules', array('id' => $assign22->cmid)));
1038         $this->assertEquals(3, course_get_format($course)->get_last_section_number());
1039         $this->assertEquals(array(0 => array($assign0->cmid),
1040             1 => array($assign1->cmid),
1041             2 => array($assign3->cmid),
1042             3 => array($assign5->cmid)), get_fast_modinfo($course)->sections);
1044         // Remove marked section.
1045         course_set_marker($course->id, 1);
1046         $this->assertTrue(course_get_format($course)->is_section_current(1));
1047         $this->assertTrue(course_delete_section($course, 1, true));
1048         $this->assertFalse(course_get_format($course)->is_section_current(1));
1049     }
1051     public function test_get_course_display_name_for_list() {
1052         global $CFG;
1053         $this->resetAfterTest(true);
1055         $course = $this->getDataGenerator()->create_course(array('shortname' => 'FROG101', 'fullname' => 'Introduction to pond life'));
1057         $CFG->courselistshortnames = 0;
1058         $this->assertEquals('Introduction to pond life', get_course_display_name_for_list($course));
1060         $CFG->courselistshortnames = 1;
1061         $this->assertEquals('FROG101 Introduction to pond life', get_course_display_name_for_list($course));
1062     }
1064     public function test_move_module_in_course() {
1065         global $DB;
1067         $this->resetAfterTest(true);
1068         // Setup fixture
1069         $course = $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections' => true));
1070         $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
1072         $cms = get_fast_modinfo($course)->get_cms();
1073         $cm = reset($cms);
1075         $newsection = get_fast_modinfo($course)->get_section_info(3);
1076         $oldsectionid = $cm->section;
1078         // Perform the move
1079         moveto_module($cm, $newsection);
1081         $cms = get_fast_modinfo($course)->get_cms();
1082         $cm = reset($cms);
1084         // Check that the cached modinfo contains the correct section info
1085         $modinfo = get_fast_modinfo($course);
1086         $this->assertTrue(empty($modinfo->sections[0]));
1087         $this->assertFalse(empty($modinfo->sections[3]));
1089         // Check that the old section's sequence no longer contains this ID
1090         $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
1091         $oldsequences = explode(',', $newsection->sequence);
1092         $this->assertFalse(in_array($cm->id, $oldsequences));
1094         // Check that the new section's sequence now contains this ID
1095         $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
1096         $newsequences = explode(',', $newsection->sequence);
1097         $this->assertTrue(in_array($cm->id, $newsequences));
1099         // Check that the section number has been changed in the cm
1100         $this->assertEquals($newsection->id, $cm->section);
1103         // Perform a second move as some issues were only seen on the second move
1104         $newsection = get_fast_modinfo($course)->get_section_info(2);
1105         $oldsectionid = $cm->section;
1106         moveto_module($cm, $newsection);
1108         $cms = get_fast_modinfo($course)->get_cms();
1109         $cm = reset($cms);
1111         // Check that the cached modinfo contains the correct section info
1112         $modinfo = get_fast_modinfo($course);
1113         $this->assertTrue(empty($modinfo->sections[0]));
1114         $this->assertFalse(empty($modinfo->sections[2]));
1116         // Check that the old section's sequence no longer contains this ID
1117         $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
1118         $oldsequences = explode(',', $newsection->sequence);
1119         $this->assertFalse(in_array($cm->id, $oldsequences));
1121         // Check that the new section's sequence now contains this ID
1122         $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
1123         $newsequences = explode(',', $newsection->sequence);
1124         $this->assertTrue(in_array($cm->id, $newsequences));
1125     }
1127     public function test_module_visibility() {
1128         $this->setAdminUser();
1129         $this->resetAfterTest(true);
1131         // Create course and modules.
1132         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
1133         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1134         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(), 'course' => $course->id));
1135         $modules = compact('forum', 'assign');
1137         // Hiding the modules.
1138         foreach ($modules as $mod) {
1139             set_coursemodule_visible($mod->cmid, 0);
1140             $this->check_module_visibility($mod, 0, 0);
1141         }
1143         // Showing the modules.
1144         foreach ($modules as $mod) {
1145             set_coursemodule_visible($mod->cmid, 1);
1146             $this->check_module_visibility($mod, 1, 1);
1147         }
1148     }
1150     public function test_section_visibility_events() {
1151         $this->setAdminUser();
1152         $this->resetAfterTest(true);
1154         $course = $this->getDataGenerator()->create_course(array('numsections' => 1), array('createsections' => true));
1155         $sectionnumber = 1;
1156         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1157             array('section' => $sectionnumber));
1158         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1159             'course' => $course->id), array('section' => $sectionnumber));
1160         $sink = $this->redirectEvents();
1161         set_section_visible($course->id, $sectionnumber, 0);
1162         $events = $sink->get_events();
1164         // Extract the number of events related to what we are testing, other events
1165         // such as course_section_updated could have been triggered.
1166         $count = 0;
1167         foreach ($events as $event) {
1168             if ($event instanceof \core\event\course_module_updated) {
1169                 $count++;
1170             }
1171         }
1172         $this->assertSame(2, $count);
1173         $sink->close();
1174     }
1176     public function test_section_visibility() {
1177         $this->setAdminUser();
1178         $this->resetAfterTest(true);
1180         // Create course.
1181         $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
1183         $sink = $this->redirectEvents();
1185         // Testing an empty section.
1186         $sectionnumber = 1;
1187         set_section_visible($course->id, $sectionnumber, 0);
1188         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1189         $this->assertEquals($section_info->visible, 0);
1190         set_section_visible($course->id, $sectionnumber, 1);
1191         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1192         $this->assertEquals($section_info->visible, 1);
1194         // Checking that an event was fired.
1195         $events = $sink->get_events();
1196         $this->assertInstanceOf('\core\event\course_section_updated', $events[0]);
1197         $sink->close();
1199         // Testing a section with visible modules.
1200         $sectionnumber = 2;
1201         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1202                 array('section' => $sectionnumber));
1203         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1204                 'course' => $course->id), array('section' => $sectionnumber));
1205         $modules = compact('forum', 'assign');
1206         set_section_visible($course->id, $sectionnumber, 0);
1207         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1208         $this->assertEquals($section_info->visible, 0);
1209         foreach ($modules as $mod) {
1210             $this->check_module_visibility($mod, 0, 1);
1211         }
1212         set_section_visible($course->id, $sectionnumber, 1);
1213         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1214         $this->assertEquals($section_info->visible, 1);
1215         foreach ($modules as $mod) {
1216             $this->check_module_visibility($mod, 1, 1);
1217         }
1219         // Testing a section with hidden modules, which should stay hidden.
1220         $sectionnumber = 3;
1221         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1222                 array('section' => $sectionnumber));
1223         $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
1224                 'course' => $course->id), array('section' => $sectionnumber));
1225         $modules = compact('forum', 'assign');
1226         foreach ($modules as $mod) {
1227             set_coursemodule_visible($mod->cmid, 0);
1228             $this->check_module_visibility($mod, 0, 0);
1229         }
1230         set_section_visible($course->id, $sectionnumber, 0);
1231         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1232         $this->assertEquals($section_info->visible, 0);
1233         foreach ($modules as $mod) {
1234             $this->check_module_visibility($mod, 0, 0);
1235         }
1236         set_section_visible($course->id, $sectionnumber, 1);
1237         $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
1238         $this->assertEquals($section_info->visible, 1);
1239         foreach ($modules as $mod) {
1240             $this->check_module_visibility($mod, 0, 0);
1241         }
1242     }
1244     /**
1245      * Helper function to assert that a module has correctly been made visible, or hidden.
1246      *
1247      * @param stdClass $mod module information
1248      * @param int $visibility the current state of the module
1249      * @param int $visibleold the current state of the visibleold property
1250      * @return void
1251      */
1252     public function check_module_visibility($mod, $visibility, $visibleold) {
1253         global $DB;
1254         $cm = get_fast_modinfo($mod->course)->get_cm($mod->cmid);
1255         $this->assertEquals($visibility, $cm->visible);
1256         $this->assertEquals($visibleold, $cm->visibleold);
1258         // Check the module grade items.
1259         $grade_items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $cm->modname,
1260                 'iteminstance' => $cm->instance, 'courseid' => $cm->course));
1261         if ($grade_items) {
1262             foreach ($grade_items as $grade_item) {
1263                 if ($visibility) {
1264                     $this->assertFalse($grade_item->is_hidden(), "$cm->modname grade_item not visible");
1265                 } else {
1266                     $this->assertTrue($grade_item->is_hidden(), "$cm->modname grade_item not hidden");
1267                 }
1268             }
1269         }
1271         // Check the events visibility.
1272         if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $cm->modname))) {
1273             foreach ($events as $event) {
1274                 $calevent = new calendar_event($event);
1275                 $this->assertEquals($visibility, $calevent->visible, "$cm->modname calendar_event visibility");
1276             }
1277         }
1278     }
1280     public function test_course_page_type_list() {
1281         global $DB;
1282         $this->resetAfterTest(true);
1284         // Create a category.
1285         $category = new stdClass();
1286         $category->name = 'Test Category';
1288         $testcategory = $this->getDataGenerator()->create_category($category);
1290         // Create a course.
1291         $course = new stdClass();
1292         $course->fullname = 'Apu loves Unit Təsts';
1293         $course->shortname = 'Spread the lŭve';
1294         $course->idnumber = '123';
1295         $course->summary = 'Awesome!';
1296         $course->summaryformat = FORMAT_PLAIN;
1297         $course->format = 'topics';
1298         $course->newsitems = 0;
1299         $course->numsections = 5;
1300         $course->category = $testcategory->id;
1302         $testcourse = $this->getDataGenerator()->create_course($course);
1304         // Create contexts.
1305         $coursecontext = context_course::instance($testcourse->id);
1306         $parentcontext = $coursecontext->get_parent_context(); // Not actually used.
1307         $pagetype = 'page-course-x'; // Not used either.
1308         $pagetypelist = course_page_type_list($pagetype, $parentcontext, $coursecontext);
1310         // Page type lists for normal courses.
1311         $testpagetypelist1 = array();
1312         $testpagetypelist1['*'] = 'Any page';
1313         $testpagetypelist1['course-*'] = 'Any course page';
1314         $testpagetypelist1['course-view-*'] = 'Any type of course main page';
1316         $this->assertEquals($testpagetypelist1, $pagetypelist);
1318         // Get the context for the front page course.
1319         $sitecoursecontext = context_course::instance(SITEID);
1320         $pagetypelist = course_page_type_list($pagetype, $parentcontext, $sitecoursecontext);
1322         // Page type list for the front page course.
1323         $testpagetypelist2 = array('*' => 'Any page');
1324         $this->assertEquals($testpagetypelist2, $pagetypelist);
1326         // Make sure that providing no current context to the function doesn't result in an error.
1327         // Calls made from generate_page_type_patterns() may provide null values.
1328         $pagetypelist = course_page_type_list($pagetype, null, null);
1329         $this->assertEquals($pagetypelist, $testpagetypelist1);
1330     }
1332     public function test_compare_activities_by_time_desc() {
1334         // Let's create some test data.
1335         $activitiesivities = array();
1336         $x = new stdClass();
1337         $x->timestamp = null;
1338         $activities[] = $x;
1340         $x = new stdClass();
1341         $x->timestamp = 1;
1342         $activities[] = $x;
1344         $x = new stdClass();
1345         $x->timestamp = 3;
1346         $activities[] = $x;
1348         $x = new stdClass();
1349         $x->timestamp = 0;
1350         $activities[] = $x;
1352         $x = new stdClass();
1353         $x->timestamp = 5;
1354         $activities[] = $x;
1356         $x = new stdClass();
1357         $activities[] = $x;
1359         $x = new stdClass();
1360         $x->timestamp = 5;
1361         $activities[] = $x;
1363         // Do the sorting.
1364         usort($activities, 'compare_activities_by_time_desc');
1366         // Let's check the result.
1367         $last = 10;
1368         foreach($activities as $activity) {
1369             if (empty($activity->timestamp)) {
1370                 $activity->timestamp = 0;
1371             }
1372             $this->assertLessThanOrEqual($last, $activity->timestamp);
1373         }
1374     }
1376     public function test_compare_activities_by_time_asc() {
1378         // Let's create some test data.
1379         $activities = array();
1380         $x = new stdClass();
1381         $x->timestamp = null;
1382         $activities[] = $x;
1384         $x = new stdClass();
1385         $x->timestamp = 1;
1386         $activities[] = $x;
1388         $x = new stdClass();
1389         $x->timestamp = 3;
1390         $activities[] = $x;
1392         $x = new stdClass();
1393         $x->timestamp = 0;
1394         $activities[] = $x;
1396         $x = new stdClass();
1397         $x->timestamp = 5;
1398         $activities[] = $x;
1400         $x = new stdClass();
1401         $activities[] = $x;
1403         $x = new stdClass();
1404         $x->timestamp = 5;
1405         $activities[] = $x;
1407         // Do the sorting.
1408         usort($activities, 'compare_activities_by_time_asc');
1410         // Let's check the result.
1411         $last = 0;
1412         foreach($activities as $activity) {
1413             if (empty($activity->timestamp)) {
1414                 $activity->timestamp = 0;
1415             }
1416             $this->assertGreaterThanOrEqual($last, $activity->timestamp);
1417         }
1418     }
1420     /**
1421      * Tests moving a module between hidden/visible sections and
1422      * verifies that the course/module visiblity seettings are
1423      * retained.
1424      */
1425     public function test_moveto_module_between_hidden_sections() {
1426         global $DB;
1428         $this->resetAfterTest(true);
1430         $course = $this->getDataGenerator()->create_course(array('numsections' => 4), array('createsections' => true));
1431         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1432         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
1433         $quiz= $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
1435         // Set the page as hidden
1436         set_coursemodule_visible($page->cmid, 0);
1438         // Set sections 3 as hidden.
1439         set_section_visible($course->id, 3, 0);
1441         $modinfo = get_fast_modinfo($course);
1443         $hiddensection = $modinfo->get_section_info(3);
1444         // New section is definitely not visible:
1445         $this->assertEquals($hiddensection->visible, 0);
1447         $forumcm = $modinfo->cms[$forum->cmid];
1448         $pagecm = $modinfo->cms[$page->cmid];
1450         // Move the forum and the page to a hidden section, make sure moveto_module returns 0 as new visibility state.
1451         $this->assertEquals(0, moveto_module($forumcm, $hiddensection));
1452         $this->assertEquals(0, moveto_module($pagecm, $hiddensection));
1454         $modinfo = get_fast_modinfo($course);
1456         // Verify that forum and page have been moved to the hidden section and quiz has not.
1457         $this->assertContains($forum->cmid, $modinfo->sections[3]);
1458         $this->assertContains($page->cmid, $modinfo->sections[3]);
1459         $this->assertNotContains($quiz->cmid, $modinfo->sections[3]);
1461         // Verify that forum has been made invisible.
1462         $forumcm = $modinfo->cms[$forum->cmid];
1463         $this->assertEquals($forumcm->visible, 0);
1464         // Verify that old state has been retained.
1465         $this->assertEquals($forumcm->visibleold, 1);
1467         // Verify that page has stayed invisible.
1468         $pagecm = $modinfo->cms[$page->cmid];
1469         $this->assertEquals($pagecm->visible, 0);
1470         // Verify that old state has been retained.
1471         $this->assertEquals($pagecm->visibleold, 0);
1473         // Verify that quiz has been unaffected.
1474         $quizcm = $modinfo->cms[$quiz->cmid];
1475         $this->assertEquals($quizcm->visible, 1);
1477         // Move forum and page back to visible section.
1478         // Make sure the visibility is restored to the original value (visible for forum and hidden for page).
1479         $visiblesection = $modinfo->get_section_info(2);
1480         $this->assertEquals(1, moveto_module($forumcm, $visiblesection));
1481         $this->assertEquals(0, moveto_module($pagecm, $visiblesection));
1483         $modinfo = get_fast_modinfo($course);
1485         // Double check that forum has been made visible.
1486         $forumcm = $modinfo->cms[$forum->cmid];
1487         $this->assertEquals($forumcm->visible, 1);
1489         // Double check that page has stayed invisible.
1490         $pagecm = $modinfo->cms[$page->cmid];
1491         $this->assertEquals($pagecm->visible, 0);
1493         // Move the page in the same section (this is what mod duplicate does).
1494         // Visibility of page remains 0.
1495         $this->assertEquals(0, moveto_module($pagecm, $visiblesection, $forumcm));
1497         // Double check that the the page is still hidden.
1498         $modinfo = get_fast_modinfo($course);
1499         $pagecm = $modinfo->cms[$page->cmid];
1500         $this->assertEquals($pagecm->visible, 0);
1501     }
1503     /**
1504      * Tests moving a module around in the same section. moveto_module()
1505      * is called this way in modduplicate.
1506      */
1507     public function test_moveto_module_in_same_section() {
1508         global $DB;
1510         $this->resetAfterTest(true);
1512         $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
1513         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
1514         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1516         // Simulate inconsistent visible/visibleold values (MDL-38713).
1517         $cm = $DB->get_record('course_modules', array('id' => $page->cmid), '*', MUST_EXIST);
1518         $cm->visible = 0;
1519         $cm->visibleold = 1;
1520         $DB->update_record('course_modules', $cm);
1522         $modinfo = get_fast_modinfo($course);
1523         $forumcm = $modinfo->cms[$forum->cmid];
1524         $pagecm = $modinfo->cms[$page->cmid];
1526         // Verify that page is hidden.
1527         $this->assertEquals($pagecm->visible, 0);
1529         // Verify section 0 is where all mods added.
1530         $section = $modinfo->get_section_info(0);
1531         $this->assertEquals($section->id, $forumcm->section);
1532         $this->assertEquals($section->id, $pagecm->section);
1535         // Move the page inside the hidden section. Make sure it is hidden.
1536         $this->assertEquals(0, moveto_module($pagecm, $section, $forumcm));
1538         // Double check that the the page is still hidden.
1539         $modinfo = get_fast_modinfo($course);
1540         $pagecm = $modinfo->cms[$page->cmid];
1541         $this->assertEquals($pagecm->visible, 0);
1542     }
1544     /**
1545      * Tests the function that deletes a course module
1546      *
1547      * @param string $type The type of module for the test
1548      * @param array $options The options for the module creation
1549      * @dataProvider provider_course_delete_module
1550      */
1551     public function test_course_delete_module($type, $options) {
1552         global $DB;
1554         $this->resetAfterTest(true);
1555         $this->setAdminUser();
1557         // Create course and modules.
1558         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
1559         $options['course'] = $course->id;
1561         // Generate an assignment with due date (will generate a course event).
1562         $module = $this->getDataGenerator()->create_module($type, $options);
1564         // Get the module context.
1565         $modcontext = context_module::instance($module->cmid);
1567         $assocblog = $this->create_module_asscociated_blog($course, $modcontext);
1569         // Verify context exists.
1570         $this->assertInstanceOf('context_module', $modcontext);
1572         // Make module specific messes.
1573         switch ($type) {
1574             case 'assign':
1575                 // Add some tags to this assignment.
1576                 core_tag_tag::set_item_tags('mod_assign', 'assign', $module->id, $modcontext, array('Tag 1', 'Tag 2', 'Tag 3'));
1577                 core_tag_tag::set_item_tags('core', 'course_modules', $module->cmid, $modcontext, array('Tag 3', 'Tag 4', 'Tag 5'));
1579                 // Confirm the tag instances were added.
1580                 $criteria = array('component' => 'mod_assign', 'itemtype' => 'assign', 'contextid' => $modcontext->id);
1581                 $this->assertEquals(3, $DB->count_records('tag_instance', $criteria));
1582                 $criteria = array('component' => 'core', 'itemtype' => 'course_modules', 'contextid' => $modcontext->id);
1583                 $this->assertEquals(3, $DB->count_records('tag_instance', $criteria));
1585                 // Verify event assignment event has been generated.
1586                 $eventcount = $DB->count_records('event', array('instance' => $module->id, 'modulename' => $type));
1587                 $this->assertEquals(1, $eventcount);
1589                 break;
1590             case 'quiz':
1591                 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
1592                 $qcat = $qgen->create_question_category(array('contextid' => $modcontext->id));
1593                 $questions = array(
1594                     $qgen->create_question('shortanswer', null, array('category' => $qcat->id)),
1595                     $qgen->create_question('shortanswer', null, array('category' => $qcat->id)),
1596                 );
1597                 $this->expectOutputRegex('/'.get_string('unusedcategorydeleted', 'question').'/');
1598                 break;
1599             default:
1600                 break;
1601         }
1603         // Run delete..
1604         course_delete_module($module->cmid);
1606         // Verify the context has been removed.
1607         $this->assertFalse(context_module::instance($module->cmid, IGNORE_MISSING));
1609         // Verify the course_module record has been deleted.
1610         $cmcount = $DB->count_records('course_modules', array('id' => $module->cmid));
1611         $this->assertEmpty($cmcount);
1613         // Verify the blog_association record has been deleted.
1614         $this->assertCount(0, $DB->get_records('blog_association',
1615                 array('contextid' => $modcontext->id)));
1617         // Verify the blog post record has been deleted.
1618         $this->assertCount(0, $DB->get_records('post',
1619                 array('id' => $assocblog->id)));
1621         // Verify the tag instance record has been deleted.
1622         $this->assertCount(0, $DB->get_records('tag_instance',
1623                 array('itemid' => $assocblog->id)));
1625         // Test clean up of module specific messes.
1626         switch ($type) {
1627             case 'assign':
1628                 // Verify event assignment events have been removed.
1629                 $eventcount = $DB->count_records('event', array('instance' => $module->id, 'modulename' => $type));
1630                 $this->assertEmpty($eventcount);
1632                 // Verify the tag instances were deleted.
1633                 $criteria = array('component' => 'mod_assign', 'contextid' => $modcontext->id);
1634                 $this->assertEquals(0, $DB->count_records('tag_instance', $criteria));
1636                 $criteria = array('component' => 'core', 'itemtype' => 'course_modules', 'contextid' => $modcontext->id);
1637                 $this->assertEquals(0, $DB->count_records('tag_instance', $criteria));
1638                 break;
1639             case 'quiz':
1640                 // Verify category deleted.
1641                 $criteria = array('contextid' => $modcontext->id);
1642                 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
1644                 // Verify questions deleted.
1645                 $criteria = array('category' => $qcat->id);
1646                 $this->assertEquals(0, $DB->count_records('question', $criteria));
1647                 break;
1648             default:
1649                 break;
1650         }
1651     }
1653     /**
1654      * Test that triggering a course_created event works as expected.
1655      */
1656     public function test_course_created_event() {
1657         global $DB;
1659         $this->resetAfterTest();
1661         // Catch the events.
1662         $sink = $this->redirectEvents();
1664         // Create the course with an id number which is used later when generating a course via the imsenterprise plugin.
1665         $data = new stdClass();
1666         $data->idnumber = 'idnumber';
1667         $course = $this->getDataGenerator()->create_course($data);
1668         // Get course from DB for comparison.
1669         $course = $DB->get_record('course', array('id' => $course->id));
1671         // Capture the event.
1672         $events = $sink->get_events();
1673         $sink->close();
1675         // Validate the event.
1676         $event = $events[0];
1677         $this->assertInstanceOf('\core\event\course_created', $event);
1678         $this->assertEquals('course', $event->objecttable);
1679         $this->assertEquals($course->id, $event->objectid);
1680         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1681         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1682         $this->assertEquals('course_created', $event->get_legacy_eventname());
1683         $this->assertEventLegacyData($course, $event);
1684         $expectedlog = array(SITEID, 'course', 'new', 'view.php?id=' . $course->id, $course->fullname . ' (ID ' . $course->id . ')');
1685         $this->assertEventLegacyLogData($expectedlog, $event);
1687         // Now we want to trigger creating a course via the imsenterprise.
1688         // Delete the course we created earlier, as we want the imsenterprise plugin to create this.
1689         // We do not want print out any of the text this function generates while doing this, which is why
1690         // we are using ob_start() and ob_end_clean().
1691         ob_start();
1692         delete_course($course);
1693         ob_end_clean();
1695         // Create the XML file we want to use.
1696         $course->category = (array)$course->category;
1697         $imstestcase = new enrol_imsenterprise_testcase();
1698         $imstestcase->imsplugin = enrol_get_plugin('imsenterprise');
1699         $imstestcase->set_test_config();
1700         $imstestcase->set_xml_file(false, array($course));
1702         // Capture the event.
1703         $sink = $this->redirectEvents();
1704         $imstestcase->imsplugin->cron();
1705         $events = $sink->get_events();
1706         $sink->close();
1707         $event = null;
1708         foreach ($events as $eventinfo) {
1709             if ($eventinfo instanceof \core\event\course_created ) {
1710                 $event = $eventinfo;
1711                 break;
1712             }
1713         }
1715         // Validate the event triggered is \core\event\course_created. There is no need to validate the other values
1716         // as they have already been validated in the previous steps. Here we only want to make sure that when the
1717         // imsenterprise plugin creates a course an event is triggered.
1718         $this->assertInstanceOf('\core\event\course_created', $event);
1719         $this->assertEventContextNotUsed($event);
1720     }
1722     /**
1723      * Test that triggering a course_updated event works as expected.
1724      */
1725     public function test_course_updated_event() {
1726         global $DB;
1728         $this->resetAfterTest();
1730         // Create a course.
1731         $course = $this->getDataGenerator()->create_course();
1733         // Create a category we are going to move this course to.
1734         $category = $this->getDataGenerator()->create_category();
1736         // Create a hidden category we are going to move this course to.
1737         $categoryhidden = $this->getDataGenerator()->create_category(array('visible' => 0));
1739         // Update course and catch course_updated event.
1740         $sink = $this->redirectEvents();
1741         update_course($course);
1742         $events = $sink->get_events();
1743         $sink->close();
1745         // Get updated course information from the DB.
1746         $updatedcourse = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1747         // Validate event.
1748         $event = array_shift($events);
1749         $this->assertInstanceOf('\core\event\course_updated', $event);
1750         $this->assertEquals('course', $event->objecttable);
1751         $this->assertEquals($updatedcourse->id, $event->objectid);
1752         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1753         $url = new moodle_url('/course/edit.php', array('id' => $event->objectid));
1754         $this->assertEquals($url, $event->get_url());
1755         $this->assertEquals($updatedcourse, $event->get_record_snapshot('course', $event->objectid));
1756         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1757         $this->assertEventLegacyData($updatedcourse, $event);
1758         $expectedlog = array($updatedcourse->id, 'course', 'update', 'edit.php?id=' . $course->id, $course->id);
1759         $this->assertEventLegacyLogData($expectedlog, $event);
1761         // Move course and catch course_updated event.
1762         $sink = $this->redirectEvents();
1763         move_courses(array($course->id), $category->id);
1764         $events = $sink->get_events();
1765         $sink->close();
1767         // Return the moved course information from the DB.
1768         $movedcourse = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1769         // Validate event.
1770         $event = array_shift($events);
1771         $this->assertInstanceOf('\core\event\course_updated', $event);
1772         $this->assertEquals('course', $event->objecttable);
1773         $this->assertEquals($movedcourse->id, $event->objectid);
1774         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1775         $this->assertEquals($movedcourse, $event->get_record_snapshot('course', $movedcourse->id));
1776         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1777         $this->assertEventLegacyData($movedcourse, $event);
1778         $expectedlog = array($movedcourse->id, 'course', 'move', 'edit.php?id=' . $movedcourse->id, $movedcourse->id);
1779         $this->assertEventLegacyLogData($expectedlog, $event);
1781         // Move course to hidden category and catch course_updated event.
1782         $sink = $this->redirectEvents();
1783         move_courses(array($course->id), $categoryhidden->id);
1784         $events = $sink->get_events();
1785         $sink->close();
1787         // Return the moved course information from the DB.
1788         $movedcoursehidden = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1789         // Validate event.
1790         $event = array_shift($events);
1791         $this->assertInstanceOf('\core\event\course_updated', $event);
1792         $this->assertEquals('course', $event->objecttable);
1793         $this->assertEquals($movedcoursehidden->id, $event->objectid);
1794         $this->assertEquals(context_course::instance($course->id), $event->get_context());
1795         $this->assertEquals($movedcoursehidden, $event->get_record_snapshot('course', $movedcoursehidden->id));
1796         $this->assertEquals('course_updated', $event->get_legacy_eventname());
1797         $this->assertEventLegacyData($movedcoursehidden, $event);
1798         $expectedlog = array($movedcoursehidden->id, 'course', 'move', 'edit.php?id=' . $movedcoursehidden->id, $movedcoursehidden->id);
1799         $this->assertEventLegacyLogData($expectedlog, $event);
1800         $this->assertEventContextNotUsed($event);
1801     }
1803     /**
1804      * Test that triggering a course_deleted event works as expected.
1805      */
1806     public function test_course_deleted_event() {
1807         $this->resetAfterTest();
1809         // Create the course.
1810         $course = $this->getDataGenerator()->create_course();
1812         // Save the course context before we delete the course.
1813         $coursecontext = context_course::instance($course->id);
1815         // Catch the update event.
1816         $sink = $this->redirectEvents();
1818         // Call delete_course() which will trigger the course_deleted event and the course_content_deleted
1819         // event. This function prints out data to the screen, which we do not want during a PHPUnit test,
1820         // so use ob_start and ob_end_clean to prevent this.
1821         ob_start();
1822         delete_course($course);
1823         ob_end_clean();
1825         // Capture the event.
1826         $events = $sink->get_events();
1827         $sink->close();
1829         // Validate the event.
1830         $event = array_pop($events);
1831         $this->assertInstanceOf('\core\event\course_deleted', $event);
1832         $this->assertEquals('course', $event->objecttable);
1833         $this->assertEquals($course->id, $event->objectid);
1834         $this->assertEquals($coursecontext->id, $event->contextid);
1835         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1836         $this->assertEquals('course_deleted', $event->get_legacy_eventname());
1837         $eventdata = $event->get_data();
1838         $this->assertSame($course->idnumber, $eventdata['other']['idnumber']);
1839         $this->assertSame($course->fullname, $eventdata['other']['fullname']);
1840         $this->assertSame($course->shortname, $eventdata['other']['shortname']);
1842         // The legacy data also passed the context in the course object and substitutes timemodified with the current date.
1843         $expectedlegacy = clone($course);
1844         $expectedlegacy->context = $coursecontext;
1845         $expectedlegacy->timemodified = $event->timecreated;
1846         $this->assertEventLegacyData($expectedlegacy, $event);
1848         // Validate legacy log data.
1849         $expectedlog = array(SITEID, 'course', 'delete', 'view.php?id=' . $course->id, $course->fullname . '(ID ' . $course->id . ')');
1850         $this->assertEventLegacyLogData($expectedlog, $event);
1851         $this->assertEventContextNotUsed($event);
1852     }
1854     /**
1855      * Test that triggering a course_content_deleted event works as expected.
1856      */
1857     public function test_course_content_deleted_event() {
1858         global $DB;
1860         $this->resetAfterTest();
1862         // Create the course.
1863         $course = $this->getDataGenerator()->create_course();
1865         // Get the course from the DB. The data generator adds some extra properties, such as
1866         // numsections, to the course object which will fail the assertions later on.
1867         $course = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1869         // Save the course context before we delete the course.
1870         $coursecontext = context_course::instance($course->id);
1872         // Catch the update event.
1873         $sink = $this->redirectEvents();
1875         remove_course_contents($course->id, false);
1877         // Capture the event.
1878         $events = $sink->get_events();
1879         $sink->close();
1881         // Validate the event.
1882         $event = array_pop($events);
1883         $this->assertInstanceOf('\core\event\course_content_deleted', $event);
1884         $this->assertEquals('course', $event->objecttable);
1885         $this->assertEquals($course->id, $event->objectid);
1886         $this->assertEquals($coursecontext->id, $event->contextid);
1887         $this->assertEquals($course, $event->get_record_snapshot('course', $course->id));
1888         $this->assertEquals('course_content_removed', $event->get_legacy_eventname());
1889         // The legacy data also passed the context and options in the course object.
1890         $course->context = $coursecontext;
1891         $course->options = array();
1892         $this->assertEventLegacyData($course, $event);
1893         $this->assertEventContextNotUsed($event);
1894     }
1896     /**
1897      * Test that triggering a course_category_deleted event works as expected.
1898      */
1899     public function test_course_category_deleted_event() {
1900         $this->resetAfterTest();
1902         // Create a category.
1903         $category = $this->getDataGenerator()->create_category();
1905         // Save the context before it is deleted.
1906         $categorycontext = context_coursecat::instance($category->id);
1908         // Catch the update event.
1909         $sink = $this->redirectEvents();
1911         // Delete the category.
1912         $category->delete_full();
1914         // Capture the event.
1915         $events = $sink->get_events();
1916         $sink->close();
1918         // Validate the event.
1919         $event = $events[0];
1920         $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1921         $this->assertEquals('course_categories', $event->objecttable);
1922         $this->assertEquals($category->id, $event->objectid);
1923         $this->assertEquals($categorycontext->id, $event->contextid);
1924         $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1925         $this->assertEquals(null, $event->get_url());
1926         $this->assertEventLegacyData($category, $event);
1927         $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category->name . '(ID ' . $category->id . ')');
1928         $this->assertEventLegacyLogData($expectedlog, $event);
1930         // Create two categories.
1931         $category = $this->getDataGenerator()->create_category();
1932         $category2 = $this->getDataGenerator()->create_category();
1934         // Save the context before it is moved and then deleted.
1935         $category2context = context_coursecat::instance($category2->id);
1937         // Catch the update event.
1938         $sink = $this->redirectEvents();
1940         // Move the category.
1941         $category2->delete_move($category->id);
1943         // Capture the event.
1944         $events = $sink->get_events();
1945         $sink->close();
1947         // Validate the event.
1948         $event = $events[0];
1949         $this->assertInstanceOf('\core\event\course_category_deleted', $event);
1950         $this->assertEquals('course_categories', $event->objecttable);
1951         $this->assertEquals($category2->id, $event->objectid);
1952         $this->assertEquals($category2context->id, $event->contextid);
1953         $this->assertEquals('course_category_deleted', $event->get_legacy_eventname());
1954         $this->assertEventLegacyData($category2, $event);
1955         $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category2->name . '(ID ' . $category2->id . ')');
1956         $this->assertEventLegacyLogData($expectedlog, $event);
1957         $this->assertEventContextNotUsed($event);
1958     }
1960     /**
1961      * Test that triggering a course_backup_created event works as expected.
1962      */
1963     public function test_course_backup_created_event() {
1964         global $CFG;
1966         // Get the necessary files to perform backup and restore.
1967         require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
1968         require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
1970         $this->resetAfterTest();
1972         // Set to admin user.
1973         $this->setAdminUser();
1975         // The user id is going to be 2 since we are the admin user.
1976         $userid = 2;
1978         // Create a course.
1979         $course = $this->getDataGenerator()->create_course();
1981         // Create backup file and save it to the backup location.
1982         $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
1983             backup::INTERACTIVE_NO, backup::MODE_GENERAL, $userid);
1984         $sink = $this->redirectEvents();
1985         $bc->execute_plan();
1987         // Capture the event.
1988         $events = $sink->get_events();
1989         $sink->close();
1991         // Validate the event.
1992         $event = array_pop($events);
1993         $this->assertInstanceOf('\core\event\course_backup_created', $event);
1994         $this->assertEquals('course', $event->objecttable);
1995         $this->assertEquals($bc->get_courseid(), $event->objectid);
1996         $this->assertEquals(context_course::instance($bc->get_courseid())->id, $event->contextid);
1998         $url = new moodle_url('/course/view.php', array('id' => $event->objectid));
1999         $this->assertEquals($url, $event->get_url());
2000         $this->assertEventContextNotUsed($event);
2002         // Destroy the resource controller since we are done using it.
2003         $bc->destroy();
2004     }
2006     /**
2007      * Test that triggering a course_restored event works as expected.
2008      */
2009     public function test_course_restored_event() {
2010         global $CFG;
2012         // Get the necessary files to perform backup and restore.
2013         require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
2014         require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
2016         $this->resetAfterTest();
2018         // Set to admin user.
2019         $this->setAdminUser();
2021         // The user id is going to be 2 since we are the admin user.
2022         $userid = 2;
2024         // Create a course.
2025         $course = $this->getDataGenerator()->create_course();
2027         // Create backup file and save it to the backup location.
2028         $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
2029             backup::INTERACTIVE_NO, backup::MODE_GENERAL, $userid);
2030         $bc->execute_plan();
2031         $results = $bc->get_results();
2032         $file = $results['backup_destination'];
2033         $fp = get_file_packer('application/vnd.moodle.backup');
2034         $filepath = $CFG->dataroot . '/temp/backup/test-restore-course-event';
2035         $file->extract_to_pathname($fp, $filepath);
2036         $bc->destroy();
2038         // Now we want to catch the restore course event.
2039         $sink = $this->redirectEvents();
2041         // Now restore the course to trigger the event.
2042         $rc = new restore_controller('test-restore-course-event', $course->id, backup::INTERACTIVE_NO,
2043             backup::MODE_GENERAL, $userid, backup::TARGET_NEW_COURSE);
2044         $rc->execute_precheck();
2045         $rc->execute_plan();
2047         // Capture the event.
2048         $events = $sink->get_events();
2049         $sink->close();
2051         // Validate the event.
2052         $event = array_pop($events);
2053         $this->assertInstanceOf('\core\event\course_restored', $event);
2054         $this->assertEquals('course', $event->objecttable);
2055         $this->assertEquals($rc->get_courseid(), $event->objectid);
2056         $this->assertEquals(context_course::instance($rc->get_courseid())->id, $event->contextid);
2057         $this->assertEquals('course_restored', $event->get_legacy_eventname());
2058         $legacydata = (object) array(
2059             'courseid' => $rc->get_courseid(),
2060             'userid' => $rc->get_userid(),
2061             'type' => $rc->get_type(),
2062             'target' => $rc->get_target(),
2063             'mode' => $rc->get_mode(),
2064             'operation' => $rc->get_operation(),
2065             'samesite' => $rc->is_samesite()
2066         );
2067         $url = new moodle_url('/course/view.php', array('id' => $event->objectid));
2068         $this->assertEquals($url, $event->get_url());
2069         $this->assertEventLegacyData($legacydata, $event);
2070         $this->assertEventContextNotUsed($event);
2072         // Destroy the resource controller since we are done using it.
2073         $rc->destroy();
2074     }
2076     /**
2077      * Test that triggering a course_section_updated event works as expected.
2078      */
2079     public function test_course_section_updated_event() {
2080         global $DB;
2082         $this->resetAfterTest();
2084         // Create the course with sections.
2085         $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true));
2086         $sections = $DB->get_records('course_sections', array('course' => $course->id));
2088         $coursecontext = context_course::instance($course->id);
2090         $section = array_pop($sections);
2091         $section->name = 'Test section';
2092         $section->summary = 'Test section summary';
2093         $DB->update_record('course_sections', $section);
2095         // Trigger an event for course section update.
2096         $event = \core\event\course_section_updated::create(
2097                 array(
2098                     'objectid' => $section->id,
2099                     'courseid' => $course->id,
2100                     'context' => context_course::instance($course->id),
2101                     'other' => array(
2102                         'sectionnum' => $section->section
2103                     )
2104                 )
2105             );
2106         $event->add_record_snapshot('course_sections', $section);
2107         // Trigger and catch event.
2108         $sink = $this->redirectEvents();
2109         $event->trigger();
2110         $events = $sink->get_events();
2111         $sink->close();
2113         // Validate the event.
2114         $event = $events[0];
2115         $this->assertInstanceOf('\core\event\course_section_updated', $event);
2116         $this->assertEquals('course_sections', $event->objecttable);
2117         $this->assertEquals($section->id, $event->objectid);
2118         $this->assertEquals($course->id, $event->courseid);
2119         $this->assertEquals($coursecontext->id, $event->contextid);
2120         $this->assertEquals($section->section, $event->other['sectionnum']);
2121         $expecteddesc = "The user with id '{$event->userid}' updated section number '{$event->other['sectionnum']}' for the course with id '{$event->courseid}'";
2122         $this->assertEquals($expecteddesc, $event->get_description());
2123         $url = new moodle_url('/course/editsection.php', array('id' => $event->objectid));
2124         $this->assertEquals($url, $event->get_url());
2125         $this->assertEquals($section, $event->get_record_snapshot('course_sections', $event->objectid));
2126         $id = $section->id;
2127         $sectionnum = $section->section;
2128         $expectedlegacydata = array($course->id, "course", "editsection", 'editsection.php?id=' . $id, $sectionnum);
2129         $this->assertEventLegacyLogData($expectedlegacydata, $event);
2130         $this->assertEventContextNotUsed($event);
2131     }
2133     /**
2134      * Test that triggering a course_section_deleted event works as expected.
2135      */
2136     public function test_course_section_deleted_event() {
2137         global $USER, $DB;
2138         $this->resetAfterTest();
2139         $sink = $this->redirectEvents();
2141         // Create the course with sections.
2142         $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true));
2143         $sections = $DB->get_records('course_sections', array('course' => $course->id), 'section');
2144         $coursecontext = context_course::instance($course->id);
2145         $section = array_pop($sections);
2146         course_delete_section($course, $section);
2147         $events = $sink->get_events();
2148         $event = array_pop($events); // Delete section event.
2149         $sink->close();
2151         // Validate event data.
2152         $this->assertInstanceOf('\core\event\course_section_deleted', $event);
2153         $this->assertEquals('course_sections', $event->objecttable);
2154         $this->assertEquals($section->id, $event->objectid);
2155         $this->assertEquals($course->id, $event->courseid);
2156         $this->assertEquals($coursecontext->id, $event->contextid);
2157         $this->assertEquals($section->section, $event->other['sectionnum']);
2158         $expecteddesc = "The user with id '{$event->userid}' deleted section number '{$event->other['sectionnum']}' " .
2159                 "(section name '{$event->other['sectionname']}') for the course with id '{$event->courseid}'";
2160         $this->assertEquals($expecteddesc, $event->get_description());
2161         $this->assertEquals($section, $event->get_record_snapshot('course_sections', $event->objectid));
2162         $this->assertNull($event->get_url());
2164         // Test legacy data.
2165         $sectionnum = $section->section;
2166         $expectedlegacydata = array($course->id, "course", "delete section", 'view.php?id=' . $course->id, $sectionnum);
2167         $this->assertEventLegacyLogData($expectedlegacydata, $event);
2168         $this->assertEventContextNotUsed($event);
2169     }
2171     public function test_course_integrity_check() {
2172         global $DB;
2174         $this->resetAfterTest(true);
2175         $course = $this->getDataGenerator()->create_course(array('numsections' => 1),
2176            array('createsections'=>true));
2178         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
2179                 array('section' => 0));
2180         $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id),
2181                 array('section' => 0));
2182         $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id),
2183                 array('section' => 0));
2184         $correctseq = join(',', array($forum->cmid, $page->cmid, $quiz->cmid));
2186         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2187         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2188         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2189         $this->assertEquals($correctseq, $section0->sequence);
2190         $this->assertEmpty($section1->sequence);
2191         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2192         $this->assertEquals($section0->id, $cms[$page->cmid]->section);
2193         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2194         $this->assertEmpty(course_integrity_check($course->id));
2196         // Now let's make manual change in DB and let course_integrity_check() fix it:
2198         // 1. Module appears twice in one section.
2199         $DB->update_record('course_sections', array('id' => $section0->id, 'sequence' => $section0->sequence. ','. $page->cmid));
2200         $this->assertEquals(
2201                 array('Failed integrity check for course ['. $course->id.
2202                 ']. Sequence for course section ['. $section0->id. '] is "'.
2203                 $section0->sequence. ','. $page->cmid. '", must be "'.
2204                 $section0->sequence. '"'),
2205                 course_integrity_check($course->id));
2206         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2207         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2208         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2209         $this->assertEquals($correctseq, $section0->sequence);
2210         $this->assertEmpty($section1->sequence);
2211         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2212         $this->assertEquals($section0->id, $cms[$page->cmid]->section);
2213         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2215         // 2. Module appears in two sections (last section wins).
2216         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''. $page->cmid));
2217         // First message about double mentioning in sequence, second message about wrong section field for $page.
2218         $this->assertEquals(array(
2219             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
2220             '] must be removed from sequence of section ['. $section0->id.
2221             '] because it is also present in sequence of section ['. $section1->id. ']',
2222             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
2223             '] points to section ['. $section0->id. '] instead of ['. $section1->id. ']'),
2224                 course_integrity_check($course->id));
2225         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2226         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2227         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2228         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
2229         $this->assertEquals(''. $page->cmid, $section1->sequence);
2230         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2231         $this->assertEquals($section1->id, $cms[$page->cmid]->section);
2232         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2234         // 3. Module id is not present in course_section.sequence (integrity check with $fullcheck = false).
2235         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''));
2236         $this->assertEmpty(course_integrity_check($course->id)); // Not an error!
2237         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2238         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2239         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2240         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
2241         $this->assertEmpty($section1->sequence);
2242         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2243         $this->assertEquals($section1->id, $cms[$page->cmid]->section); // Not changed.
2244         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2246         // 4. Module id is not present in course_section.sequence (integrity check with $fullcheck = true).
2247         $this->assertEquals(array('Failed integrity check for course ['. $course->id. ']. Course module ['.
2248                 $page->cmid. '] is missing from sequence of section ['. $section1->id. ']'),
2249                 course_integrity_check($course->id, null, null, true)); // Error!
2250         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2251         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2252         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2253         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
2254         $this->assertEquals(''. $page->cmid, $section1->sequence);  // Yay, module added to section.
2255         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2256         $this->assertEquals($section1->id, $cms[$page->cmid]->section); // Not changed.
2257         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2259         // 5. Module id is not present in course_section.sequence and it's section is invalid (integrity check with $fullcheck = true).
2260         $DB->update_record('course_modules', array('id' => $page->cmid, 'section' => 8765));
2261         $DB->update_record('course_sections', array('id' => $section1->id, 'sequence' => ''));
2262         $this->assertEquals(array(
2263             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
2264             '] is missing from sequence of section ['. $section0->id. ']',
2265             'Failed integrity check for course ['. $course->id. ']. Course module ['. $page->cmid.
2266             '] points to section [8765] instead of ['. $section0->id. ']'),
2267                 course_integrity_check($course->id, null, null, true));
2268         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2269         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2270         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2271         $this->assertEquals($forum->cmid. ','. $quiz->cmid. ','. $page->cmid, $section0->sequence); // Module added to section.
2272         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2273         $this->assertEquals($section0->id, $cms[$page->cmid]->section); // Section changed to section0.
2274         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2276         // 6. Module is deleted from course_modules but not deleted in sequence (integrity check with $fullcheck = true).
2277         $DB->delete_records('course_modules', array('id' => $page->cmid));
2278         $this->assertEquals(array('Failed integrity check for course ['. $course->id. ']. Course module ['.
2279                 $page->cmid. '] does not exist but is present in the sequence of section ['. $section0->id. ']'),
2280                 course_integrity_check($course->id, null, null, true));
2281         $section0 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 0));
2282         $section1 = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 1));
2283         $cms = $DB->get_records('course_modules', array('course' => $course->id), 'id', 'id,section');
2284         $this->assertEquals($forum->cmid. ','. $quiz->cmid, $section0->sequence);
2285         $this->assertEmpty($section1->sequence);
2286         $this->assertEquals($section0->id, $cms[$forum->cmid]->section);
2287         $this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
2288         $this->assertEquals(2, count($cms));
2289     }
2291     /**
2292      * Tests for event related to course module creation.
2293      */
2294     public function test_course_module_created_event() {
2295         global $USER, $DB;
2296         $this->resetAfterTest();
2298         // Create an assign module.
2299         $sink = $this->redirectEvents();
2300         $modinfo = $this->create_specific_module_test('assign');
2301         $events = $sink->get_events();
2302         $event = array_pop($events);
2304         $cm = get_coursemodule_from_id('assign', $modinfo->coursemodule, 0, false, MUST_EXIST);
2305         $mod = $DB->get_record('assign', array('id' => $modinfo->instance), '*', MUST_EXIST);
2307         // Validate event data.
2308         $this->assertInstanceOf('\core\event\course_module_created', $event);
2309         $this->assertEquals($cm->id, $event->objectid);
2310         $this->assertEquals($USER->id, $event->userid);
2311         $this->assertEquals('course_modules', $event->objecttable);
2312         $url = new moodle_url('/mod/assign/view.php', array('id' => $cm->id));
2313         $this->assertEquals($url, $event->get_url());
2315         // Test legacy data.
2316         $this->assertSame('mod_created', $event->get_legacy_eventname());
2317         $eventdata = new stdClass();
2318         $eventdata->modulename = 'assign';
2319         $eventdata->name       = $mod->name;
2320         $eventdata->cmid       = $cm->id;
2321         $eventdata->courseid   = $cm->course;
2322         $eventdata->userid     = $USER->id;
2323         $this->assertEventLegacyData($eventdata, $event);
2325         $arr = array(
2326             array($cm->course, "course", "add mod", "../mod/assign/view.php?id=$cm->id", "assign $cm->instance"),
2327             array($cm->course, "assign", "add", "view.php?id=$cm->id", $cm->instance, $cm->id)
2328         );
2329         $this->assertEventLegacyLogData($arr, $event);
2330         $this->assertEventContextNotUsed($event);
2332         // Let us see if duplicating an activity results in a nice course module created event.
2333         $sink->clear();
2334         $course = get_course($mod->course);
2335         $newcm = duplicate_module($course, $cm);
2336         $events = $sink->get_events();
2337         $event = array_pop($events);
2338         $sink->close();
2340         // Validate event data.
2341         $this->assertInstanceOf('\core\event\course_module_created', $event);
2342         $this->assertEquals($newcm->id, $event->objectid);
2343         $this->assertEquals($USER->id, $event->userid);
2344         $this->assertEquals($course->id, $event->courseid);
2345         $url = new moodle_url('/mod/assign/view.php', array('id' => $newcm->id));
2346         $this->assertEquals($url, $event->get_url());
2347     }
2349     /**
2350      * Tests for event validations related to course module creation.
2351      */
2352     public function test_course_module_created_event_exceptions() {
2354         $this->resetAfterTest();
2356         // Generate data.
2357         $modinfo = $this->create_specific_module_test('assign');
2358         $context = context_module::instance($modinfo->coursemodule);
2360         // Test not setting instanceid.
2361         try {
2362             $event = \core\event\course_module_created::create(array(
2363                 'courseid' => $modinfo->course,
2364                 'context'  => $context,
2365                 'objectid' => $modinfo->coursemodule,
2366                 'other'    => array(
2367                     'modulename' => 'assign',
2368                     'name'       => 'My assignment',
2369                 )
2370             ));
2371             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2372                     other['instanceid']");
2373         } catch (coding_exception $e) {
2374             $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
2375         }
2377         // Test not setting modulename.
2378         try {
2379             $event = \core\event\course_module_created::create(array(
2380                 'courseid' => $modinfo->course,
2381                 'context'  => $context,
2382                 'objectid' => $modinfo->coursemodule,
2383                 'other'    => array(
2384                     'instanceid' => $modinfo->instance,
2385                     'name'       => 'My assignment',
2386                 )
2387             ));
2388             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2389                     other['modulename']");
2390         } catch (coding_exception $e) {
2391             $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
2392         }
2394         // Test not setting name.
2396         try {
2397             $event = \core\event\course_module_created::create(array(
2398                 'courseid' => $modinfo->course,
2399                 'context'  => $context,
2400                 'objectid' => $modinfo->coursemodule,
2401                 'other'    => array(
2402                     'modulename' => 'assign',
2403                     'instanceid' => $modinfo->instance,
2404                 )
2405             ));
2406             $this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
2407                     other['name']");
2408         } catch (coding_exception $e) {
2409             $this->assertContains("The 'name' value must be set in other.", $e->getMessage());
2410         }
2412     }
2414     /**
2415      * Tests for event related to course module updates.
2416      */
2417     public function test_course_module_updated_event() {
2418         global $USER, $DB;
2419         $this->resetAfterTest();
2421         // Update a forum module.
2422         $sink = $this->redirectEvents();
2423         $modinfo = $this->update_specific_module_test('forum');
2424         $events = $sink->get_events();
2425         $event = array_pop($events);
2426         $sink->close();
2428         $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
2429         $mod = $DB->get_record('forum', array('id' => $cm->instance), '*', MUST_EXIST);
2431         // Validate event data.
2432         $this->assertInstanceOf('\core\event\course_module_updated', $event);
2433         $this->assertEquals($cm->id, $event->objectid);
2434         $this->assertEquals($USER->id, $event->userid);
2435         $this->assertEquals('course_modules', $event->objecttable);
2436         $url = new moodle_url('/mod/forum/view.php', array('id' => $cm->id));
2437         $this->assertEquals($url, $event->get_url());
2439         // Test legacy data.
2440         $this->assertSame('mod_updated', $event->get_legacy_eventname());
2441         $eventdata = new stdClass();
2442         $eventdata->modulename = 'forum';
2443         $eventdata->name       = $mod->name;
2444         $eventdata->cmid       = $cm->id;
2445         $eventdata->courseid   = $cm->course;
2446         $eventdata->userid     = $USER->id;
2447         $this->assertEventLegacyData($eventdata, $event);
2449         $arr = array(
2450             array($cm->course, "course", "update mod", "../mod/forum/view.php?id=$cm->id", "forum $cm->instance"),
2451             array($cm->course, "forum", "update", "view.php?id=$cm->id", $cm->instance, $cm->id)
2452         );
2453         $this->assertEventLegacyLogData($arr, $event);
2454         $this->assertEventContextNotUsed($event);
2455     }
2457     /**
2458      * Tests for create_from_cm method.
2459      */
2460     public function test_course_module_create_from_cm() {
2461         $this->resetAfterTest();
2462         $this->setAdminUser();
2464         // Create course and modules.
2465         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
2467         // Generate an assignment.
2468         $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
2470         // Get the module context.
2471         $modcontext = context_module::instance($assign->cmid);
2473         // Get course module.
2474         $cm = get_coursemodule_from_id(null, $assign->cmid, $course->id, false, MUST_EXIST);
2476         // Create an event from course module.
2477         $event = \core\event\course_module_updated::create_from_cm($cm, $modcontext);
2479         // Trigger the events.
2480         $sink = $this->redirectEvents();
2481         $event->trigger();
2482         $events = $sink->get_events();
2483         $event2 = array_pop($events);
2485         // Test event data.
2486         $this->assertInstanceOf('\core\event\course_module_updated', $event);
2487         $this->assertEquals($cm->id, $event2->objectid);
2488         $this->assertEquals($modcontext, $event2->get_context());
2489         $this->assertEquals($cm->modname, $event2->other['modulename']);
2490         $this->assertEquals($cm->instance, $event2->other['instanceid']);
2491         $this->assertEquals($cm->name, $event2->other['name']);
2492         $this->assertEventContextNotUsed($event2);
2493         $this->assertSame('mod_updated', $event2->get_legacy_eventname());
2494         $arr = array(
2495             array($cm->course, "course", "update mod", "../mod/assign/view.php?id=$cm->id", "assign $cm->instance"),
2496             array($cm->course, "assign", "update", "view.php?id=$cm->id", $cm->instance, $cm->id)
2497         );
2498         $this->assertEventLegacyLogData($arr, $event);
2499     }
2501     /**
2502      * Tests for event validations related to course module update.
2503      */
2504     public function test_course_module_updated_event_exceptions() {
2506         $this->resetAfterTest();
2508         // Generate data.
2509         $modinfo = $this->create_specific_module_test('assign');
2510         $context = context_module::instance($modinfo->coursemodule);
2512         // Test not setting instanceid.
2513         try {
2514             $event = \core\event\course_module_updated::create(array(
2515                 'courseid' => $modinfo->course,
2516                 'context'  => $context,
2517                 'objectid' => $modinfo->coursemodule,
2518                 'other'    => array(
2519                     'modulename' => 'assign',
2520                     'name'       => 'My assignment',
2521                 )
2522             ));
2523             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2524                     other['instanceid']");
2525         } catch (coding_exception $e) {
2526             $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
2527         }
2529         // Test not setting modulename.
2530         try {
2531             $event = \core\event\course_module_updated::create(array(
2532                 'courseid' => $modinfo->course,
2533                 'context'  => $context,
2534                 'objectid' => $modinfo->coursemodule,
2535                 'other'    => array(
2536                     'instanceid' => $modinfo->instance,
2537                     'name'       => 'My assignment',
2538                 )
2539             ));
2540             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2541                     other['modulename']");
2542         } catch (coding_exception $e) {
2543             $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
2544         }
2546         // Test not setting name.
2548         try {
2549             $event = \core\event\course_module_updated::create(array(
2550                 'courseid' => $modinfo->course,
2551                 'context'  => $context,
2552                 'objectid' => $modinfo->coursemodule,
2553                 'other'    => array(
2554                     'modulename' => 'assign',
2555                     'instanceid' => $modinfo->instance,
2556                 )
2557             ));
2558             $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
2559                     other['name']");
2560         } catch (coding_exception $e) {
2561             $this->assertContains("The 'name' value must be set in other.", $e->getMessage());
2562         }
2564     }
2566     /**
2567      * Tests for event related to course module delete.
2568      */
2569     public function test_course_module_deleted_event() {
2570         global $USER, $DB;
2571         $this->resetAfterTest();
2573         // Create and delete a module.
2574         $sink = $this->redirectEvents();
2575         $modinfo = $this->create_specific_module_test('forum');
2576         $cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
2577         course_delete_module($modinfo->coursemodule);
2578         $events = $sink->get_events();
2579         $event = array_pop($events); // delete module event.;
2580         $sink->close();
2582         // Validate event data.
2583         $this->assertInstanceOf('\core\event\course_module_deleted', $event);
2584         $this->assertEquals($cm->id, $event->objectid);
2585         $this->assertEquals($USER->id, $event->userid);
2586         $this->assertEquals('course_modules', $event->objecttable);
2587         $this->assertEquals(null, $event->get_url());
2588         $this->assertEquals($cm, $event->get_record_snapshot('course_modules', $cm->id));
2590         // Test legacy data.
2591         $this->assertSame('mod_deleted', $event->get_legacy_eventname());
2592         $eventdata = new stdClass();
2593         $eventdata->modulename = 'forum';
2594         $eventdata->cmid       = $cm->id;
2595         $eventdata->courseid   = $cm->course;
2596         $eventdata->userid     = $USER->id;
2597         $this->assertEventLegacyData($eventdata, $event);
2599         $arr = array($cm->course, 'course', "delete mod", "view.php?id=$cm->course", "forum $cm->instance", $cm->id);
2600         $this->assertEventLegacyLogData($arr, $event);
2602     }
2604     /**
2605      * Tests for event validations related to course module deletion.
2606      */
2607     public function test_course_module_deleted_event_exceptions() {
2609         $this->resetAfterTest();
2611         // Generate data.
2612         $modinfo = $this->create_specific_module_test('assign');
2613         $context = context_module::instance($modinfo->coursemodule);
2615         // Test not setting instanceid.
2616         try {
2617             $event = \core\event\course_module_deleted::create(array(
2618                 'courseid' => $modinfo->course,
2619                 'context'  => $context,
2620                 'objectid' => $modinfo->coursemodule,
2621                 'other'    => array(
2622                     'modulename' => 'assign',
2623                     'name'       => 'My assignment',
2624                 )
2625             ));
2626             $this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
2627                     other['instanceid']");
2628         } catch (coding_exception $e) {
2629             $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
2630         }
2632         // Test not setting modulename.
2633         try {
2634             $event = \core\event\course_module_deleted::create(array(
2635                 'courseid' => $modinfo->course,
2636                 'context'  => $context,
2637                 'objectid' => $modinfo->coursemodule,
2638                 'other'    => array(
2639                     'instanceid' => $modinfo->instance,
2640                     'name'       => 'My assignment',
2641                 )
2642             ));
2643             $this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
2644                     other['modulename']");
2645         } catch (coding_exception $e) {
2646             $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
2647         }
2648     }
2650     /**
2651      * Returns a user object and its assigned new role.
2652      *
2653      * @param testing_data_generator $generator
2654      * @param $contextid
2655      * @return array The user object and the role ID
2656      */
2657     protected function get_user_objects(testing_data_generator $generator, $contextid) {
2658         global $USER;
2660         if (empty($USER->id)) {
2661             $user  = $generator->create_user();
2662             $this->setUser($user);
2663         }
2664         $roleid = create_role('Test role', 'testrole', 'Test role description');
2665         if (!is_array($contextid)) {
2666             $contextid = array($contextid);
2667         }
2668         foreach ($contextid as $cid) {
2669             $assignid = role_assign($roleid, $user->id, $cid);
2670         }
2671         return array($user, $roleid);
2672     }
2674     /**
2675      * Test course move after course.
2676      */
2677     public function test_course_change_sortorder_after_course() {
2678         global $DB;
2680         $this->resetAfterTest(true);
2682         $generator = $this->getDataGenerator();
2683         $category = $generator->create_category();
2684         $course3 = $generator->create_course(array('category' => $category->id));
2685         $course2 = $generator->create_course(array('category' => $category->id));
2686         $course1 = $generator->create_course(array('category' => $category->id));
2687         $context = $category->get_context();
2689         list($user, $roleid) = $this->get_user_objects($generator, $context->id);
2690         $caps = course_capability_assignment::allow('moodle/category:manage', $roleid, $context->id);
2692         $courses = $category->get_courses();
2693         $this->assertInternalType('array', $courses);
2694         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2695         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2696         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2698         // Test moving down.
2699         $this->assertTrue(course_change_sortorder_after_course($course1->id, $course3->id));
2700         $courses = $category->get_courses();
2701         $this->assertInternalType('array', $courses);
2702         $this->assertEquals(array($course2->id, $course3->id, $course1->id), array_keys($courses));
2703         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2704         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2706         // Test moving up.
2707         $this->assertTrue(course_change_sortorder_after_course($course1->id, $course2->id));
2708         $courses = $category->get_courses();
2709         $this->assertInternalType('array', $courses);
2710         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
2711         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2712         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2714         // Test moving to the top.
2715         $this->assertTrue(course_change_sortorder_after_course($course1->id, 0));
2716         $courses = $category->get_courses();
2717         $this->assertInternalType('array', $courses);
2718         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2719         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2720         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2721     }
2723     /**
2724      * Tests changing the visibility of a course.
2725      */
2726     public function test_course_change_visibility() {
2727         global $DB;
2729         $this->resetAfterTest(true);
2731         $generator = $this->getDataGenerator();
2732         $category = $generator->create_category();
2733         $course = $generator->create_course(array('category' => $category->id));
2735         $this->assertEquals('1', $course->visible);
2736         $this->assertEquals('1', $course->visibleold);
2738         $this->assertTrue(course_change_visibility($course->id, false));
2739         $course = $DB->get_record('course', array('id' => $course->id));
2740         $this->assertEquals('0', $course->visible);
2741         $this->assertEquals('0', $course->visibleold);
2743         $this->assertTrue(course_change_visibility($course->id, true));
2744         $course = $DB->get_record('course', array('id' => $course->id));
2745         $this->assertEquals('1', $course->visible);
2746         $this->assertEquals('1', $course->visibleold);
2747     }
2749     /**
2750      * Tests moving the course up and down by one.
2751      */
2752     public function test_course_change_sortorder_by_one() {
2753         global $DB;
2755         $this->resetAfterTest(true);
2757         $generator = $this->getDataGenerator();
2758         $category = $generator->create_category();
2759         $course3 = $generator->create_course(array('category' => $category->id));
2760         $course2 = $generator->create_course(array('category' => $category->id));
2761         $course1 = $generator->create_course(array('category' => $category->id));
2763         $courses = $category->get_courses();
2764         $this->assertInternalType('array', $courses);
2765         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2766         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2767         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2769         // Test moving down.
2770         $course1 = get_course($course1->id);
2771         $this->assertTrue(course_change_sortorder_by_one($course1, false));
2772         $courses = $category->get_courses();
2773         $this->assertInternalType('array', $courses);
2774         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
2775         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2776         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2778         // Test moving up.
2779         $course1 = get_course($course1->id);
2780         $this->assertTrue(course_change_sortorder_by_one($course1, true));
2781         $courses = $category->get_courses();
2782         $this->assertInternalType('array', $courses);
2783         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2784         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2785         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2787         // Test moving the top course up one.
2788         $course1 = get_course($course1->id);
2789         $this->assertFalse(course_change_sortorder_by_one($course1, true));
2790         // Check nothing changed.
2791         $courses = $category->get_courses();
2792         $this->assertInternalType('array', $courses);
2793         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2794         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2795         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2797         // Test moving the bottom course up down.
2798         $course3 = get_course($course3->id);
2799         $this->assertFalse(course_change_sortorder_by_one($course3, false));
2800         // Check nothing changed.
2801         $courses = $category->get_courses();
2802         $this->assertInternalType('array', $courses);
2803         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
2804         $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
2805         $this->assertEquals(array_keys($dbcourses), array_keys($courses));
2806     }
2808     public function test_view_resources_list() {
2809         $this->resetAfterTest();
2811         $course = self::getDataGenerator()->create_course();
2812         $coursecontext = context_course::instance($course->id);
2814         $event = \core\event\course_resources_list_viewed::create(array('context' => context_course::instance($course->id)));
2815         $event->set_legacy_logdata(array('book', 'page', 'resource'));
2816         $sink = $this->redirectEvents();
2817         $event->trigger();
2818         $events = $sink->get_events();
2819         $sink->close();
2821         // Validate the event.
2822         $event = $events[0];
2823         $this->assertInstanceOf('\core\event\course_resources_list_viewed', $event);
2824         $this->assertEquals(null, $event->objecttable);
2825         $this->assertEquals(null, $event->objectid);
2826         $this->assertEquals($course->id, $event->courseid);
2827         $this->assertEquals($coursecontext->id, $event->contextid);
2828         $expectedlegacydata = array(
2829             array($course->id, "book", "view all", 'index.php?id=' . $course->id, ''),
2830             array($course->id, "page", "view all", 'index.php?id=' . $course->id, ''),
2831             array($course->id, "resource", "view all", 'index.php?id=' . $course->id, ''),
2832         );
2833         $this->assertEventLegacyLogData($expectedlegacydata, $event);
2834         $this->assertEventContextNotUsed($event);
2835     }
2837     /**
2838      * Test duplicate_module()
2839      */
2840     public function test_duplicate_module() {
2841         $this->setAdminUser();
2842         $this->resetAfterTest();
2843         $course = self::getDataGenerator()->create_course();
2844         $res = self::getDataGenerator()->create_module('resource', array('course' => $course));
2845         $cm = get_coursemodule_from_id('resource', $res->cmid, 0, false, MUST_EXIST);
2847         $newcm = duplicate_module($course, $cm);
2849         // Make sure they are the same, except obvious id changes.
2850         foreach ($cm as $prop => $value) {
2851             if ($prop == 'id' || $prop == 'url' || $prop == 'instance' || $prop == 'added') {
2852                 // Ignore obviously different properties.
2853                 continue;
2854             }
2855             if ($prop == 'name') {
2856                 // We expect ' (copy)' to be added to the original name since MDL-59227.
2857                 $value = get_string('duplicatedmodule', 'moodle', $value);
2858             }
2859             $this->assertEquals($value, $newcm->$prop);
2860         }
2861     }
2863     /**
2864      * Tests that when creating or updating a module, if the availability settings
2865      * are present but set to an empty tree, availability is set to null in
2866      * database.
2867      */
2868     public function test_empty_availability_settings() {
2869         global $DB;
2870         $this->setAdminUser();
2871         $this->resetAfterTest();
2873         // Enable availability.
2874         set_config('enableavailability', 1);
2876         // Test add.
2877         $emptyavailability = json_encode(\core_availability\tree::get_root_json(array()));
2878         $course = self::getDataGenerator()->create_course();
2879         $label = self::getDataGenerator()->create_module('label', array(
2880                 'course' => $course, 'availability' => $emptyavailability));
2881         $this->assertNull($DB->get_field('course_modules', 'availability',
2882                 array('id' => $label->cmid)));
2884         // Test update.
2885         $formdata = $DB->get_record('course_modules', array('id' => $label->cmid));
2886         unset($formdata->availability);
2887         $formdata->availabilityconditionsjson = $emptyavailability;
2888         $formdata->modulename = 'label';
2889         $formdata->coursemodule = $label->cmid;
2890         $draftid = 0;
2891         file_prepare_draft_area($draftid, context_module::instance($label->cmid)->id,
2892                 'mod_label', 'intro', 0);
2893         $formdata->introeditor = array(
2894             'itemid' => $draftid,
2895             'text' => '<p>Yo</p>',
2896             'format' => FORMAT_HTML);
2897         update_module($formdata);
2898         $this->assertNull($DB->get_field('course_modules', 'availability',
2899                 array('id' => $label->cmid)));
2900     }
2902     /**
2903      * Test update_inplace_editable()
2904      */
2905     public function test_update_module_name_inplace() {
2906         global $CFG, $DB, $PAGE;
2907         require_once($CFG->dirroot . '/lib/external/externallib.php');
2909         $this->setUser($this->getDataGenerator()->create_user());
2911         $this->resetAfterTest(true);
2912         $course = $this->getDataGenerator()->create_course();
2913         $forum = self::getDataGenerator()->create_module('forum', array('course' => $course->id, 'name' => 'forum name'));
2915         // Call service for core_course component without necessary permissions.
2916         try {
2917             core_external::update_inplace_editable('core_course', 'activityname', $forum->cmid, 'New forum name');
2918             $this->fail('Exception expected');
2919         } catch (moodle_exception $e) {
2920             $this->assertEquals('Course or activity not accessible. (Not enrolled)',
2921                 $e->getMessage());
2922         }
2924         // Change to admin user and make sure that cm name can be updated using web service update_inplace_editable().
2925         $this->setAdminUser();
2926         $res = core_external::update_inplace_editable('core_course', 'activityname', $forum->cmid, 'New forum name');
2927         $res = external_api::clean_returnvalue(core_external::update_inplace_editable_returns(), $res);
2928         $this->assertEquals('New forum name', $res['value']);
2929         $this->assertEquals('New forum name', $DB->get_field('forum', 'name', array('id' => $forum->id)));
2930     }
2932     /**
2933      * Testing function course_get_tagged_course_modules - search tagged course modules
2934      */
2935     public function test_course_get_tagged_course_modules() {
2936         global $DB;
2937         $this->resetAfterTest();
2938         $course3 = $this->getDataGenerator()->create_course();
2939         $course2 = $this->getDataGenerator()->create_course();
2940         $course1 = $this->getDataGenerator()->create_course();
2941         $cm11 = $this->getDataGenerator()->create_module('assign', array('course' => $course1->id,
2942             'tags' => 'Cat, Dog'));
2943         $cm12 = $this->getDataGenerator()->create_module('page', array('course' => $course1->id,
2944             'tags' => 'Cat, Mouse', 'visible' => 0));
2945         $cm13 = $this->getDataGenerator()->create_module('page', array('course' => $course1->id,
2946             'tags' => 'Cat, Mouse, Dog'));
2947         $cm21 = $this->getDataGenerator()->create_module('forum', array('course' => $course2->id,
2948             'tags' => 'Cat, Mouse'));
2949         $cm31 = $this->getDataGenerator()->create_module('forum', array('course' => $course3->id,
2950             'tags' => 'Cat, Mouse'));
2952         // Admin is able to view everything.
2953         $this->setAdminUser();
2954         $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'),
2955                 /*$exclusivemode = */false, /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$page = */0);
2956         $this->assertRegExp('/'.$cm11->name.'/', $res->content);
2957         $this->assertRegExp('/'.$cm12->name.'/', $res->content);
2958         $this->assertRegExp('/'.$cm13->name.'/', $res->content);
2959         $this->assertRegExp('/'.$cm21->name.'/', $res->content);
2960         $this->assertRegExp('/'.$cm31->name.'/', $res->content);
2961         // Results from course1 are returned before results from course2.
2962         $this->assertTrue(strpos($res->content, $cm11->name) < strpos($res->content, $cm21->name));
2964         // Ordinary user is not able to see anything.
2965         $user = $this->getDataGenerator()->create_user();
2966         $this->setUser($user);
2968         $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'),
2969                 /*$exclusivemode = */false, /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$page = */0);
2970         $this->assertNull($res);
2972         // Enrol user as student in course1 and course2.
2973         $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
2974         $this->getDataGenerator()->enrol_user($user->id, $course1->id, $roleids['student']);
2975         $this->getDataGenerator()->enrol_user($user->id, $course2->id, $roleids['student']);
2976         core_tag_index_builder::reset_caches();
2978         // Searching in the course context returns visible modules in this course.
2979         $context = context_course::instance($course1->id);
2980         $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'),
2981                 /*$exclusivemode = */false, /*$fromctx = */0, /*$ctx = */$context->id, /*$rec = */1, /*$page = */0);
2982         $this->assertRegExp('/'.$cm11->name.'/', $res->content);
2983         $this->assertNotRegExp('/'.$cm12->name.'/', $res->content);
2984         $this->assertRegExp('/'.$cm13->name.'/', $res->content);
2985         $this->assertNotRegExp('/'.$cm21->name.'/', $res->content);
2986         $this->assertNotRegExp('/'.$cm31->name.'/', $res->content);
2988         // Searching FROM the course context returns visible modules in all courses.
2989         $context = context_course::instance($course2->id);
2990         $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'),
2991                 /*$exclusivemode = */false, /*$fromctx = */$context->id, /*$ctx = */0, /*$rec = */1, /*$page = */0);
2992         $this->assertRegExp('/'.$cm11->name.'/', $res->content);
2993         $this->assertNotRegExp('/'.$cm12->name.'/', $res->content);
2994         $this->assertRegExp('/'.$cm13->name.'/', $res->content);
2995         $this->assertRegExp('/'.$cm21->name.'/', $res->content);
2996         $this->assertNotRegExp('/'.$cm31->name.'/', $res->content); // No access to course3.
2997         // Results from course2 are returned before results from course1.
2998         $this->assertTrue(strpos($res->content, $cm21->name) < strpos($res->content, $cm11->name));
3000         // Enrol user in course1 as a teacher - now he should be able to see hidden module.
3001         $this->getDataGenerator()->enrol_user($user->id, $course1->id, $roleids['editingteacher']);
3002         get_fast_modinfo(0,0,true);
3004         $context = context_course::instance($course1->id);
3005         $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'),
3006                 /*$exclusivemode = */false, /*$fromctx = */$context->id, /*$ctx = */0, /*$rec = */1, /*$page = */0);
3007         $this->assertRegExp('/'.$cm12->name.'/', $res->content);
3009         // Create more modules and try pagination.
3010         $cm14 = $this->getDataGenerator()->create_module('assign', array('course' => $course1->id,
3011             'tags' => 'Cat, Dog'));
3012         $cm15 = $this->getDataGenerator()->create_module('page', array('course' => $course1->id,
3013             'tags' => 'Cat, Mouse', 'visible' => 0));
3014         $cm16 = $this->getDataGenerator()->create_module('page', array('course' => $course1->id,
3015             'tags' => 'Cat, Mouse, Dog'));
3017         $context = context_course::instance($course1->id);
3018         $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'),
3019                 /*$exclusivemode = */false, /*$fromctx = */0, /*$ctx = */$context->id, /*$rec = */1, /*$page = */0);
3020         $this->assertRegExp('/'.$cm11->name.'/', $res->content);
3021         $this->assertRegExp('/'.$cm12->name.'/', $res->content);
3022         $this->assertRegExp('/'.$cm13->name.'/', $res->content);
3023         $this->assertNotRegExp('/'.$cm21->name.'/', $res->content);
3024         $this->assertRegExp('/'.$cm14->name.'/', $res->content);
3025         $this->assertRegExp('/'.$cm15->name.'/', $res->content);
3026         $this->assertNotRegExp('/'.$cm16->name.'/', $res->content);
3027         $this->assertNotRegExp('/'.$cm31->name.'/', $res->content); // No access to course3.
3028         $this->assertEmpty($res->prevpageurl);
3029         $this->assertNotEmpty($res->nextpageurl);
3031         $res = course_get_tagged_course_modules(core_tag_tag::get_by_name(0, 'Cat'),
3032                 /*$exclusivemode = */false, /*$fromctx = */0, /*$ctx = */$context->id, /*$rec = */1, /*$page = */1);
3033         $this->assertNotRegExp('/'.$cm11->name.'/', $res->content);
3034         $this->assertNotRegExp('/'.$cm12->name.'/', $res->content);
3035         $this->assertNotRegExp('/'.$cm13->name.'/', $res->content);
3036         $this->assertNotRegExp('/'.$cm21->name.'/', $res->content);
3037         $this->assertNotRegExp('/'.$cm14->name.'/', $res->content);
3038         $this->assertNotRegExp('/'.$cm15->name.'/', $res->content);
3039         $this->assertRegExp('/'.$cm16->name.'/', $res->content);
3040         $this->assertNotRegExp('/'.$cm31->name.'/', $res->content); // No access to course3.
3041         $this->assertNotEmpty($res->prevpageurl);
3042         $this->assertEmpty($res->nextpageurl);
3043     }
3045     /**
3046      * Test course_get_user_navigation_options for frontpage.
3047      */
3048     public function test_course_get_user_navigation_options_for_frontpage() {
3049         global $CFG, $SITE, $DB;
3050         $this->resetAfterTest();
3051         $context = context_system::instance();
3052         $course = clone $SITE;
3053         $this->setAdminUser();
3055         $navoptions = course_get_user_navigation_options($context, $course);
3056         $this->assertTrue($navoptions->blogs);
3057         $this->assertTrue($navoptions->notes);
3058         $this->assertTrue($navoptions->participants);
3059         $this->assertTrue($navoptions->badges);
3060         $this->assertTrue($navoptions->tags);
3061         $this->assertFalse($navoptions->search);
3062         $this->assertTrue($navoptions->calendar);
3063         $this->assertTrue($navoptions->competencies);
3065         // Enable global search now.
3066         $CFG->enableglobalsearch = 1;
3067         $navoptions = course_get_user_navigation_options($context, $course);
3068         $this->assertTrue($navoptions->search);
3070         // Disable competencies.
3071         $oldcompetencies = get_config('core_competency', 'enabled');
3072         set_config('enabled', false, 'core_competency');
3073         $navoptions = course_get_user_navigation_options($context, $course);
3074         $this->assertFalse($navoptions->competencies);
3075         set_config('enabled', $oldcompetencies, 'core_competency');
3077         // Now try with a standard user.
3078         $user = $this->getDataGenerator()->create_user();
3079         $this->setUser($user);
3080         $navoptions = course_get_user_navigation_options($context, $course);
3081         $this->assertTrue($navoptions->blogs);
3082         $this->assertFalse($navoptions->notes);
3083         $this->assertFalse($navoptions->participants);
3084         $this->assertTrue($navoptions->badges);
3085         $this->assertTrue($navoptions->tags);
3086         $this->assertTrue($navoptions->search);
3087         $this->assertTrue($navoptions->calendar);
3088     }
3090     /**
3091      * Test course_get_user_navigation_options for managers in a normal course.
3092      */
3093     public function test_course_get_user_navigation_options_for_managers() {
3094         global $CFG;
3095         $this->resetAfterTest();
3096         $course = $this->getDataGenerator()->create_course();
3097         $context = context_course::instance($course->id);
3098         $this->setAdminUser();
3100         $navoptions = course_get_user_navigation_options($context);
3101         $this->assertTrue($navoptions->blogs);
3102         $this->assertTrue($navoptions->notes);
3103         $this->assertTrue($navoptions->participants);
3104         $this->assertTrue($navoptions->badges);
3105     }
3107     /**
3108      * Test course_get_user_navigation_options for students in a normal course.
3109      */
3110     public function test_course_get_user_navigation_options_for_students() {
3111         global $DB, $CFG;
3112         $this->resetAfterTest();
3113         $course = $this->getDataGenerator()->create_course();
3114         $context = context_course::instance($course->id);
3116         $user = $this->getDataGenerator()->create_user();
3117         $roleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
3118         $this->getDataGenerator()->enrol_user($user->id, $course->id, $roleid);
3120         $this->setUser($user);
3122         $navoptions = course_get_user_navigation_options($context);
3123         $this->assertTrue($navoptions->blogs);
3124         $this->assertFalse($navoptions->notes);
3125         $this->assertTrue($navoptions->participants);
3126         $this->assertTrue($navoptions->badges);
3128         // Disable some options.
3129         $CFG->badges_allowcoursebadges = 0;
3130         $CFG->enableblogs = 0;
3131         // Disable view participants capability.
3132         assign_capability('moodle/course:viewparticipants', CAP_PROHIBIT, $roleid, $context);
3134         $navoptions = course_get_user_navigation_options($context);
3135         $this->assertFalse($navoptions->blogs);
3136         $this->assertFalse($navoptions->notes);
3137         $this->assertFalse($navoptions->participants);
3138         $this->assertFalse($navoptions->badges);
3139     }
3141     /**
3142      * Test course_get_user_administration_options for frontpage.
3143      */
3144     public function test_course_get_user_administration_options_for_frontpage() {
3145         global $CFG, $SITE;
3146         $this->resetAfterTest();
3147         $course = clone $SITE;
3148         $context = context_course::instance($course->id);
3149         $this->setAdminUser();
3151         $adminoptions = course_get_user_administration_options($course, $context);
3152         $this->assertTrue($adminoptions->update);
3153         $this->assertTrue($adminoptions->filters);
3154         $this->assertTrue($adminoptions->reports);
3155         $this->assertTrue($adminoptions->backup);
3156         $this->assertTrue($adminoptions->restore);
3157         $this->assertFalse($adminoptions->files);
3158         $this->assertFalse($adminoptions->tags);
3160         // Now try with a standard user.
3161         $user = $this->getDataGenerator()->create_user();
3162         $this->setUser($user);
3163         $adminoptions = course_get_user_administration_options($course, $context);
3164         $this->assertFalse($adminoptions->update);
3165         $this->assertFalse($adminoptions->filters);
3166         $this->assertFalse($adminoptions->reports);
3167         $this->assertFalse($adminoptions->backup);
3168         $this->assertFalse($adminoptions->restore);
3169         $this->assertFalse($adminoptions->files);
3170         $this->assertFalse($adminoptions->tags);
3172     }
3174     /**
3175      * Test course_get_user_administration_options for managers in a normal course.
3176      */
3177     public function test_course_get_user_administration_options_for_managers() {
3178         global $CFG;
3179         $this->resetAfterTest();
3180         $course = $this->getDataGenerator()->create_course();
3181         $context = context_course::instance($course->id);
3182         $this->setAdminUser();
3184         $adminoptions = course_get_user_administration_options($course, $context);
3185         $this->assertTrue($adminoptions->update);
3186         $this->assertTrue($adminoptions->filters);
3187         $this->assertTrue($adminoptions->reports);
3188         $this->assertTrue($adminoptions->backup);
3189         $this->assertTrue($adminoptions->restore);
3190         $this->assertFalse($adminoptions->files);
3191         $this->assertTrue($adminoptions->tags);
3192         $this->assertTrue($adminoptions->gradebook);
3193         $this->assertFalse($adminoptions->outcomes);
3194         $this->assertTrue($adminoptions->badges);
3195         $this->assertTrue($adminoptions->import);
3196         $this->assertTrue($adminoptions->publish);
3197         $this->assertTrue($adminoptions->reset);
3198         $this->assertTrue($adminoptions->roles);
3199     }
3201     /**
3202      * Test course_get_user_administration_options for students in a normal course.
3203      */
3204     public function test_course_get_user_administration_options_for_students() {
3205         global $DB, $CFG;
3206         $this->resetAfterTest();
3207         $course = $this->getDataGenerator()->create_course();
3208         $context = context_course::instance($course->id);
3210         $user = $this->getDataGenerator()->create_user();
3211         $roleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
3212         $this->getDataGenerator()->enrol_user($user->id, $course->id, $roleid);
3214         $this->setUser($user);
3215         $adminoptions = course_get_user_administration_options($course, $context);
3217         $this->assertFalse($adminoptions->update);
3218         $this->assertFalse($adminoptions->filters);
3219         $this->assertFalse($adminoptions->reports);
3220         $this->assertFalse($adminoptions->backup);
3221         $this->assertFalse($adminoptions->restore);
3222         $this->assertFalse($adminoptions->files);
3223         $this->assertFalse($adminoptions->tags);
3224         $this->assertFalse($adminoptions->gradebook);
3225         $this->assertFalse($adminoptions->outcomes);
3226         $this->assertTrue($adminoptions->badges);
3227         $this->assertFalse($adminoptions->import);
3228         $this->assertFalse($adminoptions->publish);
3229         $this->assertFalse($adminoptions->reset);
3230         $this->assertFalse($adminoptions->roles);
3232         $CFG->enablebadges = false;
3233         $adminoptions = course_get_user_administration_options($course, $context);
3234         $this->assertFalse($adminoptions->badges);
3235     }
3237     /**
3238      * Test test_update_course_frontpage_category.
3239      */
3240     public function test_update_course_frontpage_category() {
3241         // Fetch front page course.
3242         $course = get_course(SITEID);
3243         // Test update information on front page course.
3244         $course->category = 99;
3245         $this->expectException('moodle_exception');
3246         $this->expectExceptionMessage(get_string('invalidcourse', 'error'));
3247         update_course($course);
3248     }
3250     /**
3251      * test_course_enddate
3252      *
3253      * @dataProvider course_enddate_provider
3254      * @param int $startdate
3255      * @param int $enddate
3256      * @param string $errorcode
3257      */
3258     public function test_course_enddate($startdate, $enddate, $errorcode) {
3260         $this->resetAfterTest(true);
3262         $record = array('startdate' => $startdate, 'enddate' => $enddate);
3263         try {
3264             $course1 = $this->getDataGenerator()->create_course($record);
3265             if ($errorcode !== false) {
3266                 $this->fail('Expected exception with "' . $errorcode . '" error code in create_create');
3267             }
3268         } catch (moodle_exception $e) {
3269             if ($errorcode === false) {
3270                 $this->fail('Got "' . $errorcode . '" exception error code and no exception was expected');
3271             }
3272             if ($e->errorcode != $errorcode) {
3273                 $this->fail('Got "' . $e->errorcode. '" exception error code and "' . $errorcode . '" was expected');
3274             }
3275             return;
3276         }
3278         $