MDL-63854 competencies, themes: misplaced dropdown arrows
[moodle.git] / course / tests / courselib_test.php
blob37ca6629042a2ba3f24100b225e7a22f0b2f4456
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.
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
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
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().
38 * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
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;
59 /**
60 * Execute test asserts on the saved DB data by create_module($forum).
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.
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);
86 /**
87 * Set assign module specific test values for calling create_module().
89 * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
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));
123 * Execute test asserts on the saved DB data by create_module($assign).
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.
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);
156 * Run some asserts test for a specific module for the function create_module().
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'...).
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;
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);
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;
307 * Create module associated blog and tags.
309 * @param object $course Course.
310 * @param object $modulecontext The context of the module.
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'
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;
351 * Test create_module() for multiple modules defined in the $modules array (first declaration of the function).
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);
364 * Test update_module() for multiple modules defined in the $modules array (first declaration of the function).
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);
377 * Set forum specific test values for calling update_module().
379 * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
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;
401 * Execute test asserts on the saved DB data by update_module($forum).
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.
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);
430 * Test a specific type of module.
432 * @param string $modulename - the module name to test
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;
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);
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;
582 * Data provider for course_delete module
584 * @return array An array of arrays contain test data
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;
596 * Test the create_course function
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());
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());
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);
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);
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());
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());
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);
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));
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);
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;
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);
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;
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;
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]);
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;
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;
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]);
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);
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));
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));
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));
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));
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);
1143 // Showing the modules.
1144 foreach ($modules as $mod) {
1145 set_coursemodule_visible($mod->cmid, 1);
1146 $this->check_module_visibility($mod, 1, 1);
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++;
1172 $this->assertSame(2, $count);
1173 $sink->close();
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);
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);
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);
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);
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);
1245 * Helper function to assert that a module has correctly been made visible, or hidden.
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
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");
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");
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);
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;
1372 $this->assertLessThanOrEqual($last, $activity->timestamp);
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;
1416 $this->assertGreaterThanOrEqual($last, $activity->timestamp);
1421 * Tests moving a module between hidden/visible sections and
1422 * verifies that the course/module visiblity seettings are
1423 * retained.
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);
1504 * Tests moving a module around in the same section. moveto_module()
1505 * is called this way in modduplicate.
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);
1545 * Tests the function that deletes a course module
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
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)),
1597 $this->expectOutputRegex('/'.get_string('unusedcategorydeleted', 'question').'/');
1598 break;
1599 default:
1600 break;
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;
1654 * Test that triggering a course_created event works as expected.
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;
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);
1723 * Test that triggering a course_updated event works as expected.
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);
1804 * Test that triggering a course_deleted event works as expected.
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);
1855 * Test that triggering a course_content_deleted event works as expected.
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);
1897 * Test that triggering a course_category_deleted event works as expected.
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);
1961 * Test that triggering a course_backup_created event works as expected.
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();
2007 * Test that triggering a course_restored event works as expected.
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()
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();
2077 * Test that triggering a course_section_updated event works as expected.
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
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);
2134 * Test that triggering a course_section_deleted event works as expected.
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);
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));
2292 * Tests for event related to course module creation.
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)
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());
2350 * Tests for event validations related to course module creation.
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',
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());
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',
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());
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,
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());
2415 * Tests for event related to course module updates.
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)
2453 $this->assertEventLegacyLogData($arr, $event);
2454 $this->assertEventContextNotUsed($event);
2458 * Tests for create_from_cm method.
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)
2498 $this->assertEventLegacyLogData($arr, $event);
2502 * Tests for event validations related to course module update.
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',
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());
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',
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());
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,
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());
2567 * Tests for event related to course module delete.
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);
2605 * Tests for event validations related to course module deletion.
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',
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());
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',
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());
2651 * Returns a user object and its assigned new role.
2653 * @param testing_data_generator $generator
2654 * @param $contextid
2655 * @return array The user object and the role ID
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);
2664 $roleid = create_role('Test role', 'testrole', 'Test role description');
2665 if (!is_array($contextid)) {
2666 $contextid = array($contextid);
2668 foreach ($contextid as $cid) {
2669 $assignid = role_assign($roleid, $user->id, $cid);
2671 return array($user, $roleid);
2675 * Test course move after course.
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));
2724 * Tests changing the visibility of a course.
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);
2750 * Tests moving the course up and down by one.
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));
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, ''),
2833 $this->assertEventLegacyLogData($expectedlegacydata, $event);
2834 $this->assertEventContextNotUsed($event);
2838 * Test duplicate_module()
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;
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);
2859 $this->assertEquals($value, $newcm->$prop);
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.
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)));
2903 * Test update_inplace_editable()
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());
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)));
2933 * Testing function course_get_tagged_course_modules - search tagged course modules
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);
3046 * Test course_get_user_navigation_options for frontpage.
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);
3091 * Test course_get_user_navigation_options for managers in a normal course.
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);
3108 * Test course_get_user_navigation_options for students in a normal course.
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);
3142 * Test course_get_user_administration_options for frontpage.
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);
3175 * Test course_get_user_administration_options for managers in a normal course.
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);
3202 * Test course_get_user_administration_options for students in a normal course.
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);
3238 * Test test_update_course_frontpage_category.
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);
3251 * test_course_enddate
3253 * @dataProvider course_enddate_provider
3254 * @param int $startdate
3255 * @param int $enddate
3256 * @param string $errorcode
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');
3268 } catch (moodle_exception $e) {
3269 if ($errorcode === false) {
3270 $this->fail('Got "' . $errorcode . '" exception error code and no exception was expected');
3272 if ($e->errorcode != $errorcode) {
3273 $this->fail('Got "' . $e->errorcode. '" exception error code and "' . $errorcode . '" was expected');
3275 return;
3278 $this->assertEquals($startdate, $course1->startdate);
3279 $this->assertEquals($enddate, $course1->enddate);
3283 * Provider for test_course_enddate.
3285 * @return array
3287 public function course_enddate_provider() {
3288 // Each provided example contains startdate, enddate and the expected exception error code if there is any.
3289 return [
3291 111,
3292 222,
3293 false
3294 ], [
3295 222,
3296 111,
3297 'enddatebeforestartdate'
3298 ], [
3299 111,
3301 false
3302 ], [
3304 222,
3305 'nostartdatenoenddate'
3312 * test_course_dates_reset
3314 * @dataProvider course_dates_reset_provider
3315 * @param int $startdate
3316 * @param int $enddate
3317 * @param int $resetstartdate
3318 * @param int $resetenddate
3319 * @param int $resultingstartdate
3320 * @param int $resultingenddate
3322 public function test_course_dates_reset($startdate, $enddate, $resetstartdate, $resetenddate, $resultingstartdate, $resultingenddate) {
3323 global $CFG, $DB;
3325 require_once($CFG->dirroot.'/completion/criteria/completion_criteria_date.php');
3327 $this->resetAfterTest(true);
3329 $this->setAdminUser();
3331 $CFG->enablecompletion = true;
3333 $this->setTimezone('UTC');
3335 $record = array('startdate' => $startdate, 'enddate' => $enddate, 'enablecompletion' => 1);
3336 $originalcourse = $this->getDataGenerator()->create_course($record);
3337 $coursecriteria = new completion_criteria_date(array('course' => $originalcourse->id, 'timeend' => $startdate + DAYSECS));
3338 $coursecriteria->insert();
3340 $activitycompletiondate = $startdate + DAYSECS;
3341 $data = $this->getDataGenerator()->create_module('data', array('course' => $originalcourse->id),
3342 array('completion' => 1, 'completionexpected' => $activitycompletiondate));
3344 $resetdata = new stdClass();
3345 $resetdata->id = $originalcourse->id;
3346 $resetdata->reset_start_date_old = $originalcourse->startdate;
3347 $resetdata->reset_start_date = $resetstartdate;
3348 $resetdata->reset_end_date = $resetenddate;
3349 $resetdata->reset_end_date_old = $record['enddate'];
3350 reset_course_userdata($resetdata);
3352 $course = $DB->get_record('course', array('id' => $originalcourse->id));
3354 $this->assertEquals($resultingstartdate, $course->startdate);
3355 $this->assertEquals($resultingenddate, $course->enddate);
3357 $coursecompletioncriteria = completion_criteria_date::fetch(array('course' => $originalcourse->id));
3358 $this->assertEquals($resultingstartdate + DAYSECS, $coursecompletioncriteria->timeend);
3360 $this->assertEquals($resultingstartdate + DAYSECS, $DB->get_field('course_modules', 'completionexpected',
3361 array('id' => $data->cmid)));
3365 * Provider for test_course_dates_reset.
3367 * @return array
3369 public function course_dates_reset_provider() {
3371 // Each example contains the following:
3372 // - course startdate
3373 // - course enddate
3374 // - startdate to reset to (false if not reset)
3375 // - enddate to reset to (false if not reset)
3376 // - resulting startdate
3377 // - resulting enddate
3378 $time = 1445644800;
3379 return [
3380 // No date changes.
3382 $time,
3383 $time + DAYSECS,
3384 false,
3385 false,
3386 $time,
3387 $time + DAYSECS
3389 // End date changes to a valid value.
3391 $time,
3392 $time + DAYSECS,
3393 false,
3394 $time + DAYSECS + 111,
3395 $time,
3396 $time + DAYSECS + 111
3398 // Start date changes to a valid value. End date does not get updated because it does not have value.
3400 $time,
3402 $time + DAYSECS,
3403 false,
3404 $time + DAYSECS,
3407 // Start date changes to a valid value. End date gets updated accordingly.
3409 $time,
3410 $time + DAYSECS,
3411 $time + WEEKSECS,
3412 false,
3413 $time + WEEKSECS,
3414 $time + WEEKSECS + DAYSECS
3416 // Start date and end date change to a valid value.
3418 $time,
3419 $time + DAYSECS,
3420 $time + WEEKSECS,
3421 $time + YEARSECS,
3422 $time + WEEKSECS,
3423 $time + YEARSECS
3429 * Test reset_course_userdata() with reset_roles_overrides enabled.
3431 public function test_course_roles_reset() {
3432 global $DB;
3434 $this->resetAfterTest(true);
3436 $generator = $this->getDataGenerator();
3438 // Create test course and user, enrol one in the other.
3439 $course = $generator->create_course();
3440 $user = $generator->create_user();
3441 $roleid = $DB->get_field('role', 'id', array('shortname' => 'student'), MUST_EXIST);
3442 $generator->enrol_user($user->id, $course->id, $roleid);
3444 // Override course so it does NOT allow students 'mod/forum:viewdiscussion'.
3445 $coursecontext = context_course::instance($course->id);
3446 assign_capability('mod/forum:viewdiscussion', CAP_PREVENT, $roleid, $coursecontext->id);
3448 // Check expected capabilities so far.
3449 $this->assertFalse(has_capability('mod/forum:viewdiscussion', $coursecontext, $user));
3451 // Oops, preventing student from viewing forums was a mistake, let's reset the course.
3452 $resetdata = new stdClass();
3453 $resetdata->id = $course->id;
3454 $resetdata->reset_roles_overrides = true;
3455 reset_course_userdata($resetdata);
3457 // Check new expected capabilities - override at the course level should be reset.
3458 $this->assertTrue(has_capability('mod/forum:viewdiscussion', $coursecontext, $user));
3461 public function test_course_check_module_updates_since() {
3462 global $CFG, $DB, $USER;
3463 require_once($CFG->dirroot . '/mod/glossary/lib.php');
3464 require_once($CFG->dirroot . '/rating/lib.php');
3465 require_once($CFG->dirroot . '/comment/lib.php');
3467 $this->resetAfterTest(true);
3469 $CFG->enablecompletion = true;
3470 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
3471 $glossary = $this->getDataGenerator()->create_module('glossary', array(
3472 'course' => $course->id,
3473 'completion' => COMPLETION_TRACKING_AUTOMATIC,
3474 'completionview' => 1,
3475 'allowcomments' => 1,
3476 'assessed' => RATING_AGGREGATE_AVERAGE,
3477 'scale' => 100
3479 $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
3480 $context = context_module::instance($glossary->cmid);
3481 $modinfo = get_fast_modinfo($course);
3482 $cm = $modinfo->get_cm($glossary->cmid);
3483 $user = $this->getDataGenerator()->create_user();
3484 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
3485 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
3486 $from = time();
3488 $teacher = $this->getDataGenerator()->create_user();
3489 $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
3490 $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
3492 assign_capability('mod/glossary:viewanyrating', CAP_ALLOW, $studentrole->id, $context->id, true);
3494 // Check nothing changed right now.
3495 $updates = course_check_module_updates_since($cm, $from);
3496 $this->assertFalse($updates->configuration->updated);
3497 $this->assertFalse($updates->completion->updated);
3498 $this->assertFalse($updates->gradeitems->updated);
3499 $this->assertFalse($updates->comments->updated);
3500 $this->assertFalse($updates->ratings->updated);
3501 $this->assertFalse($updates->introfiles->updated);
3502 $this->assertFalse($updates->outcomes->updated);
3504 $this->waitForSecond();
3506 // Do some changes.
3507 $this->setUser($user);
3508 $entry = $glossarygenerator->create_content($glossary);
3510 $this->setUser($teacher);
3511 // Name.
3512 set_coursemodule_name($glossary->cmid, 'New name');
3514 // Add some ratings.
3515 $rm = new rating_manager();
3516 $result = $rm->add_rating($cm, $context, 'mod_glossary', 'entry', $entry->id, 100, 50, $user->id, RATING_AGGREGATE_AVERAGE);
3518 // Change grades.
3519 $glossary->cmidnumber = $glossary->cmid;
3520 glossary_update_grades($glossary, $user->id);
3522 $this->setUser($user);
3523 // Completion status.
3524 glossary_view($glossary, $course, $cm, $context, 'letter');
3526 // Add one comment.
3527 $args = new stdClass;
3528 $args->context = $context;
3529 $args->course = $course;
3530 $args->cm = $cm;
3531 $args->area = 'glossary_entry';
3532 $args->itemid = $entry->id;
3533 $args->client_id = 1;
3534 $args->component = 'mod_glossary';
3535 $manager = new comment($args);
3536 $manager->add('blah blah blah');
3538 // Check upgrade status.
3539 $updates = course_check_module_updates_since($cm, $from);
3540 $this->assertTrue($updates->configuration->updated);
3541 $this->assertTrue($updates->completion->updated);
3542 $this->assertTrue($updates->gradeitems->updated);
3543 $this->assertTrue($updates->comments->updated);
3544 $this->assertTrue($updates->ratings->updated);
3545 $this->assertFalse($updates->introfiles->updated);
3546 $this->assertFalse($updates->outcomes->updated);
3549 public function test_async_module_deletion_hook_implemented() {
3550 // Async module deletion depends on the 'true' being returned by at least one plugin implementing the hook,
3551 // 'course_module_adhoc_deletion_recommended'. In core, is implemented by the course recyclebin, which will only return
3552 // true if the recyclebin plugin is enabled. To make sure async deletion occurs, this test force-enables the recyclebin.
3553 global $DB, $USER;
3554 $this->resetAfterTest(true);
3555 $this->setAdminUser();
3557 // Ensure recyclebin is enabled.
3558 set_config('coursebinenable', true, 'tool_recyclebin');
3560 // Create course, module and context.
3561 $course = $this->getDataGenerator()->create_course(['numsections' => 5]);
3562 $module = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]);
3563 $modcontext = context_module::instance($module->cmid);
3565 // Verify context exists.
3566 $this->assertInstanceOf('context_module', $modcontext);
3568 // Check events generated on the course_delete_module call.
3569 $sink = $this->redirectEvents();
3571 // Try to delete the module using the async flag.
3572 course_delete_module($module->cmid, true); // Try to delete the module asynchronously.
3574 // Verify that no event has been generated yet.
3575 $events = $sink->get_events();
3576 $event = array_pop($events);
3577 $sink->close();
3578 $this->assertEmpty($event);
3580 // Grab the record, in it's final state before hard deletion, for comparison with the event snapshot.
3581 // We need to do this because the 'deletioninprogress' flag has changed from '0' to '1'.
3582 $cm = $DB->get_record('course_modules', ['id' => $module->cmid], '*', MUST_EXIST);
3584 // Verify the course_module is marked as 'deletioninprogress'.
3585 $this->assertNotEquals($cm, false);
3586 $this->assertEquals($cm->deletioninprogress, '1');
3588 // Verify the context has not yet been removed.
3589 $this->assertEquals($modcontext, context_module::instance($module->cmid, IGNORE_MISSING));
3591 // Set up a sink to catch the 'course_module_deleted' event.
3592 $sink = $this->redirectEvents();
3594 // Now, run the adhoc task which performs the hard deletion.
3595 phpunit_util::run_all_adhoc_tasks();
3597 // Fetch and validate the event data.
3598 $events = $sink->get_events();
3599 $event = array_pop($events);
3600 $sink->close();
3601 $this->assertInstanceOf('\core\event\course_module_deleted', $event);
3602 $this->assertEquals($module->cmid, $event->objectid);
3603 $this->assertEquals($USER->id, $event->userid);
3604 $this->assertEquals('course_modules', $event->objecttable);
3605 $this->assertEquals(null, $event->get_url());
3606 $this->assertEquals($cm, $event->get_record_snapshot('course_modules', $module->cmid));
3608 // Verify the context has been removed.
3609 $this->assertFalse(context_module::instance($module->cmid, IGNORE_MISSING));
3611 // Verify the course_module record has been deleted.
3612 $cmcount = $DB->count_records('course_modules', ['id' => $module->cmid]);
3613 $this->assertEmpty($cmcount);
3616 public function test_async_module_deletion_hook_not_implemented() {
3617 // Only proceed if we are sure that no plugin is going to advocate async removal of a module. I.e. no plugin returns
3618 // 'true' from the 'course_module_adhoc_deletion_recommended' hook.
3619 // In the case of core, only recyclebin implements this hook, and it will only return true if enabled, so disable it.
3620 global $DB, $USER;
3621 $this->resetAfterTest(true);
3622 $this->setAdminUser();
3623 set_config('coursebinenable', false, 'tool_recyclebin');
3625 // Non-core plugins might implement the 'course_module_adhoc_deletion_recommended' hook and spoil this test.
3626 // If at least one plugin still returns true, then skip this test.
3627 if ($pluginsfunction = get_plugins_with_function('course_module_background_deletion_recommended')) {
3628 foreach ($pluginsfunction as $plugintype => $plugins) {
3629 foreach ($plugins as $pluginfunction) {
3630 if ($pluginfunction()) {
3631 $this->markTestSkipped();
3637 // Create course, module and context.
3638 $course = $this->getDataGenerator()->create_course(['numsections' => 5]);
3639 $module = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]);
3640 $modcontext = context_module::instance($module->cmid);
3641 $cm = $DB->get_record('course_modules', ['id' => $module->cmid], '*', MUST_EXIST);
3643 // Verify context exists.
3644 $this->assertInstanceOf('context_module', $modcontext);
3646 // Check events generated on the course_delete_module call.
3647 $sink = $this->redirectEvents();
3649 // Try to delete the module using the async flag.
3650 course_delete_module($module->cmid, true); // Try to delete the module asynchronously.
3652 // Fetch and validate the event data.
3653 $events = $sink->get_events();
3654 $event = array_pop($events);
3655 $sink->close();
3656 $this->assertInstanceOf('\core\event\course_module_deleted', $event);
3657 $this->assertEquals($module->cmid, $event->objectid);
3658 $this->assertEquals($USER->id, $event->userid);
3659 $this->assertEquals('course_modules', $event->objecttable);
3660 $this->assertEquals(null, $event->get_url());
3661 $this->assertEquals($cm, $event->get_record_snapshot('course_modules', $module->cmid));
3663 // Verify the context has been removed.
3664 $this->assertFalse(context_module::instance($module->cmid, IGNORE_MISSING));
3666 // Verify the course_module record has been deleted.
3667 $cmcount = $DB->count_records('course_modules', ['id' => $module->cmid]);
3668 $this->assertEmpty($cmcount);
3671 public function test_async_section_deletion_hook_implemented() {
3672 // Async section deletion (provided section contains modules), depends on the 'true' being returned by at least one plugin
3673 // implementing the 'course_module_adhoc_deletion_recommended' hook. In core, is implemented by the course recyclebin,
3674 // which will only return true if the plugin is enabled. To make sure async deletion occurs, this test enables recyclebin.
3675 global $DB, $USER;
3676 $this->resetAfterTest(true);
3677 $this->setAdminUser();
3679 // Ensure recyclebin is enabled.
3680 set_config('coursebinenable', true, 'tool_recyclebin');
3682 // Create course, module and context.
3683 $generator = $this->getDataGenerator();
3684 $course = $generator->create_course(['numsections' => 4, 'format' => 'topics'], ['createsections' => true]);
3685 $assign0 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
3686 $assign1 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
3687 $assign2 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
3688 $assign3 = $generator->create_module('assign', ['course' => $course, 'section' => 0]);
3690 // Delete empty section. No difference from normal, synchronous behaviour.
3691 $this->assertTrue(course_delete_section($course, 4, false, true));
3692 $this->assertEquals(3, course_get_format($course)->get_last_section_number());
3694 // Delete a module in section 2 (using async). Need to verify this doesn't generate two tasks when we delete
3695 // the section in the next step.
3696 course_delete_module($assign2->cmid, true);
3698 // Confirm that the module is pending deletion in its current section.
3699 $section = $DB->get_record('course_sections', ['course' => $course->id, 'section' => '2']); // For event comparison.
3700 $this->assertEquals(true, $DB->record_exists('course_modules', ['id' => $assign2->cmid, 'deletioninprogress' => 1,
3701 'section' => $section->id]));
3703 // Now, delete section 2.
3704 $this->assertFalse(course_delete_section($course, 2, false, true)); // Non-empty section, no forcedelete, so no change.
3706 $sink = $this->redirectEvents(); // To capture the event.
3707 $this->assertTrue(course_delete_section($course, 2, true, true));
3709 // Now, confirm that:
3710 // a) the section's modules have been flagged for deletion and moved to section 0 and;
3711 // b) the section has been deleted and;
3712 // c) course_section_deleted event has been fired. The course_module_deleted events will only fire once they have been
3713 // removed from section 0 via the adhoc task.
3715 // Modules should have been flagged for deletion and moved to section 0.
3716 $sectionid = $DB->get_field('course_sections', 'id', ['course' => $course->id, 'section' => 0]);
3717 $this->assertEquals(3, $DB->count_records('course_modules', ['section' => $sectionid, 'deletioninprogress' => 1]));
3719 // Confirm the section has been deleted.
3720 $this->assertEquals(2, course_get_format($course)->get_last_section_number());
3722 // Check event fired.
3723 $events = $sink->get_events();
3724 $event = array_pop($events);
3725 $sink->close();
3726 $this->assertInstanceOf('\core\event\course_section_deleted', $event);
3727 $this->assertEquals($section->id, $event->objectid);
3728 $this->assertEquals($USER->id, $event->userid);
3729 $this->assertEquals('course_sections', $event->objecttable);
3730 $this->assertEquals(null, $event->get_url());
3731 $this->assertEquals($section, $event->get_record_snapshot('course_sections', $section->id));
3733 // Now, run the adhoc task to delete the modules from section 0.
3734 $sink = $this->redirectEvents(); // To capture the events.
3735 phpunit_util::run_all_adhoc_tasks();
3737 // Confirm the modules have been deleted.
3738 list($insql, $assignids) = $DB->get_in_or_equal([$assign0->cmid, $assign1->cmid, $assign2->cmid]);
3739 $cmcount = $DB->count_records_select('course_modules', 'id ' . $insql, $assignids);
3740 $this->assertEmpty($cmcount);
3742 // Confirm other modules in section 0 still remain.
3743 $this->assertEquals(1, $DB->count_records('course_modules', ['id' => $assign3->cmid]));
3745 // Confirm that events were generated for all 3 of the modules.
3746 $events = $sink->get_events();
3747 $sink->close();
3748 $count = 0;
3749 while (!empty($events)) {
3750 $event = array_pop($events);
3751 if ($event instanceof \core\event\course_module_deleted &&
3752 in_array($event->objectid, [$assign0->cmid, $assign1->cmid, $assign2->cmid])) {
3753 $count++;
3756 $this->assertEquals(3, $count);
3759 public function test_async_section_deletion_hook_not_implemented() {
3760 // If no plugins advocate async removal, then normal synchronous removal will take place.
3761 // Only proceed if we are sure that no plugin is going to advocate async removal of a module. I.e. no plugin returns
3762 // 'true' from the 'course_module_adhoc_deletion_recommended' hook.
3763 // In the case of core, only recyclebin implements this hook, and it will only return true if enabled, so disable it.
3764 global $DB, $USER;
3765 $this->resetAfterTest(true);
3766 $this->setAdminUser();
3767 set_config('coursebinenable', false, 'tool_recyclebin');
3769 // Non-core plugins might implement the 'course_module_adhoc_deletion_recommended' hook and spoil this test.
3770 // If at least one plugin still returns true, then skip this test.
3771 if ($pluginsfunction = get_plugins_with_function('course_module_background_deletion_recommended')) {
3772 foreach ($pluginsfunction as $plugintype => $plugins) {
3773 foreach ($plugins as $pluginfunction) {
3774 if ($pluginfunction()) {
3775 $this->markTestSkipped();
3781 // Create course, module and context.
3782 $generator = $this->getDataGenerator();
3783 $course = $generator->create_course(['numsections' => 4, 'format' => 'topics'], ['createsections' => true]);
3784 $assign0 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
3785 $assign1 = $generator->create_module('assign', ['course' => $course, 'section' => 2]);
3787 // Delete empty section. No difference from normal, synchronous behaviour.
3788 $this->assertTrue(course_delete_section($course, 4, false, true));
3789 $this->assertEquals(3, course_get_format($course)->get_last_section_number());
3791 // Delete section in the middle (2).
3792 $section = $DB->get_record('course_sections', ['course' => $course->id, 'section' => '2']); // For event comparison.
3793 $this->assertFalse(course_delete_section($course, 2, false, true)); // Non-empty section, no forcedelete, so no change.
3795 $sink = $this->redirectEvents(); // To capture the event.
3796 $this->assertTrue(course_delete_section($course, 2, true, true));
3798 // Now, confirm that:
3799 // a) The section's modules have deleted and;
3800 // b) the section has been deleted and;
3801 // c) course_section_deleted event has been fired and;
3802 // d) course_module_deleted events have both been fired.
3804 // Confirm modules have been deleted.
3805 list($insql, $assignids) = $DB->get_in_or_equal([$assign0->cmid, $assign1->cmid]);
3806 $cmcount = $DB->count_records_select('course_modules', 'id ' . $insql, $assignids);
3807 $this->assertEmpty($cmcount);
3809 // Confirm the section has been deleted.
3810 $this->assertEquals(2, course_get_format($course)->get_last_section_number());
3812 // Confirm the course_section_deleted event has been generated.
3813 $events = $sink->get_events();
3814 $event = array_pop($events);
3815 $sink->close();
3816 $this->assertInstanceOf('\core\event\course_section_deleted', $event);
3817 $this->assertEquals($section->id, $event->objectid);
3818 $this->assertEquals($USER->id, $event->userid);
3819 $this->assertEquals('course_sections', $event->objecttable);
3820 $this->assertEquals(null, $event->get_url());
3821 $this->assertEquals($section, $event->get_record_snapshot('course_sections', $section->id));
3823 // Confirm that the course_module_deleted events have both been generated.
3824 $count = 0;
3825 while (!empty($events)) {
3826 $event = array_pop($events);
3827 if ($event instanceof \core\event\course_module_deleted &&
3828 in_array($event->objectid, [$assign0->cmid, $assign1->cmid])) {
3829 $count++;
3832 $this->assertEquals(2, $count);
3835 public function test_classify_course_for_timeline() {
3836 global $DB, $CFG;
3838 require_once($CFG->dirroot.'/completion/criteria/completion_criteria_self.php');
3840 set_config('enablecompletion', COMPLETION_ENABLED);
3841 set_config('coursegraceperiodbefore', 0);
3842 set_config('coursegraceperiodafter', 0);
3844 $this->resetAfterTest(true);
3845 $this->setAdminUser();
3847 // Create courses for testing.
3848 $generator = $this->getDataGenerator();
3849 $future = time() + 3600;
3850 $past = time() - 3600;
3851 $futurecourse = $generator->create_course(['startdate' => $future]);
3852 $pastcourse = $generator->create_course(['startdate' => $past - 60, 'enddate' => $past]);
3853 $completedcourse = $generator->create_course(['enablecompletion' => COMPLETION_ENABLED]);
3854 $inprogresscourse = $generator->create_course();
3856 // Set completion rules.
3857 $criteriadata = new stdClass();
3858 $criteriadata->id = $completedcourse->id;
3860 // Self completion.
3861 $criteriadata->criteria_self = COMPLETION_CRITERIA_TYPE_SELF;
3862 $class = 'completion_criteria_self';
3863 $criterion = new $class();
3864 $criterion->update_config($criteriadata);
3866 $user = $this->getDataGenerator()->create_user();
3867 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
3868 $this->getDataGenerator()->enrol_user($user->id, $futurecourse->id, $studentrole->id);
3869 $this->getDataGenerator()->enrol_user($user->id, $pastcourse->id, $studentrole->id);
3870 $this->getDataGenerator()->enrol_user($user->id, $completedcourse->id, $studentrole->id);
3871 $this->getDataGenerator()->enrol_user($user->id, $inprogresscourse->id, $studentrole->id);
3873 $this->setUser($user);
3874 core_completion_external::mark_course_self_completed($completedcourse->id);
3875 $ccompletion = new completion_completion(array('course' => $completedcourse->id, 'userid' => $user->id));
3876 $ccompletion->mark_complete();
3878 // Aggregate the completions.
3879 $this->assertEquals(COURSE_TIMELINE_PAST, course_classify_for_timeline($pastcourse));
3880 $this->assertEquals(COURSE_TIMELINE_FUTURE, course_classify_for_timeline($futurecourse));
3881 $this->assertEquals(COURSE_TIMELINE_PAST, course_classify_for_timeline($completedcourse));
3882 $this->assertEquals(COURSE_TIMELINE_INPROGRESS, course_classify_for_timeline($inprogresscourse));
3884 // Test grace period.
3885 set_config('coursegraceperiodafter', 1);
3886 set_config('coursegraceperiodbefore', 1);
3887 $this->assertEquals(COURSE_TIMELINE_INPROGRESS, course_classify_for_timeline($pastcourse));
3888 $this->assertEquals(COURSE_TIMELINE_INPROGRESS, course_classify_for_timeline($futurecourse));
3889 $this->assertEquals(COURSE_TIMELINE_PAST, course_classify_for_timeline($completedcourse));
3890 $this->assertEquals(COURSE_TIMELINE_INPROGRESS, course_classify_for_timeline($inprogresscourse));
3894 * Test the main function for updating all calendar events for a module.
3896 public function test_course_module_calendar_event_update_process() {
3897 global $DB;
3899 $this->resetAfterTest();
3900 $this->setAdminUser();
3902 $completionexpected = time();
3903 $duedate = time();
3905 $course = $this->getDataGenerator()->create_course(['enablecompletion' => COMPLETION_ENABLED]);
3906 $assign = $this->getDataGenerator()->create_module('assign', [
3907 'course' => $course,
3908 'completionexpected' => $completionexpected,
3909 'duedate' => $duedate
3912 $cm = get_coursemodule_from_instance('assign', $assign->id, $course->id);
3913 $events = $DB->get_records('event', ['courseid' => $course->id, 'instance' => $assign->id]);
3914 // Check that both events are using the expected dates.
3915 foreach ($events as $event) {
3916 if ($event->eventtype == \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED) {
3917 $this->assertEquals($completionexpected, $event->timestart);
3919 if ($event->eventtype == ASSIGN_EVENT_TYPE_DUE) {
3920 $this->assertEquals($duedate, $event->timestart);
3924 // We have to manually update the module and the course module.
3925 $newcompletionexpected = time() + DAYSECS * 60;
3926 $newduedate = time() + DAYSECS * 45;
3927 $newmodulename = 'Assign - new name';
3929 $moduleobject = (object)array('id' => $assign->id, 'duedate' => $newduedate, 'name' => $newmodulename);
3930 $DB->update_record('assign', $moduleobject);
3931 $cmobject = (object)array('id' => $cm->id, 'completionexpected' => $newcompletionexpected);
3932 $DB->update_record('course_modules', $cmobject);
3934 $assign = $DB->get_record('assign', ['id' => $assign->id]);
3935 $cm = get_coursemodule_from_instance('assign', $assign->id, $course->id);
3937 course_module_calendar_event_update_process($assign, $cm);
3939 $events = $DB->get_records('event', ['courseid' => $course->id, 'instance' => $assign->id]);
3940 // Now check that the details have been updated properly from the function.
3941 foreach ($events as $event) {
3942 if ($event->eventtype == \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED) {
3943 $this->assertEquals($newcompletionexpected, $event->timestart);
3944 $this->assertEquals(get_string('completionexpectedfor', 'completion', (object)['instancename' => $newmodulename]),
3945 $event->name);
3947 if ($event->eventtype == ASSIGN_EVENT_TYPE_DUE) {
3948 $this->assertEquals($newduedate, $event->timestart);
3949 $this->assertEquals(get_string('calendardue', 'assign', $newmodulename), $event->name);
3955 * Test the higher level checks for updating calendar events for an instance.
3957 public function test_course_module_update_calendar_events() {
3958 $this->resetAfterTest();
3959 $this->setAdminUser();
3961 $completionexpected = time();
3962 $duedate = time();
3964 $course = $this->getDataGenerator()->create_course(['enablecompletion' => COMPLETION_ENABLED]);
3965 $assign = $this->getDataGenerator()->create_module('assign', [
3966 'course' => $course,
3967 'completionexpected' => $completionexpected,
3968 'duedate' => $duedate
3971 $cm = get_coursemodule_from_instance('assign', $assign->id, $course->id);
3973 // Both the instance and cm objects are missing.
3974 $this->assertFalse(course_module_update_calendar_events('assign'));
3975 // Just using the assign instance.
3976 $this->assertTrue(course_module_update_calendar_events('assign', $assign));
3977 // Just using the course module object.
3978 $this->assertTrue(course_module_update_calendar_events('assign', null, $cm));
3979 // Using both the assign instance and the course module object.
3980 $this->assertTrue(course_module_update_calendar_events('assign', $assign, $cm));
3984 * Test the higher level checks for updating calendar events for a module.
3986 public function test_course_module_bulk_update_calendar_events() {
3987 $this->resetAfterTest();
3988 $this->setAdminUser();
3990 $completionexpected = time();
3991 $duedate = time();
3993 $course = $this->getDataGenerator()->create_course(['enablecompletion' => COMPLETION_ENABLED]);
3994 $course2 = $this->getDataGenerator()->create_course(['enablecompletion' => COMPLETION_ENABLED]);
3995 $assign = $this->getDataGenerator()->create_module('assign', [
3996 'course' => $course,
3997 'completionexpected' => $completionexpected,
3998 'duedate' => $duedate
4001 // No assign instances in this course.
4002 $this->assertFalse(course_module_bulk_update_calendar_events('assign', $course2->id));
4003 // No book instances for the site.
4004 $this->assertFalse(course_module_bulk_update_calendar_events('book'));
4005 // Update all assign instances.
4006 $this->assertTrue(course_module_bulk_update_calendar_events('assign'));
4007 // Update the assign instances for this course.
4008 $this->assertTrue(course_module_bulk_update_calendar_events('assign', $course->id));
4012 * Test that a student can view participants in a course they are enrolled in.
4014 public function test_course_can_view_participants_as_student() {
4015 $this->resetAfterTest();
4017 $course = $this->getDataGenerator()->create_course();
4018 $coursecontext = context_course::instance($course->id);
4020 $user = $this->getDataGenerator()->create_user();
4021 $this->getDataGenerator()->enrol_user($user->id, $course->id);
4023 $this->setUser($user);
4025 $this->assertTrue(course_can_view_participants($coursecontext));
4029 * Test that a student in a course can not view participants on the site.
4031 public function test_course_can_view_participants_as_student_on_site() {
4032 $this->resetAfterTest();
4034 $course = $this->getDataGenerator()->create_course();
4036 $user = $this->getDataGenerator()->create_user();
4037 $this->getDataGenerator()->enrol_user($user->id, $course->id);
4039 $this->setUser($user);
4041 $this->assertFalse(course_can_view_participants(context_system::instance()));
4045 * Test that an admin can view participants on the site.
4047 public function test_course_can_view_participants_as_admin_on_site() {
4048 $this->resetAfterTest();
4050 $this->setAdminUser();
4052 $this->assertTrue(course_can_view_participants(context_system::instance()));
4056 * Test teachers can view participants in a course they are enrolled in.
4058 public function test_course_can_view_participants_as_teacher() {
4059 global $DB;
4061 $this->resetAfterTest();
4063 $course = $this->getDataGenerator()->create_course();
4064 $coursecontext = context_course::instance($course->id);
4066 $user = $this->getDataGenerator()->create_user();
4067 $roleid = $DB->get_field('role', 'id', array('shortname' => 'editingteacher'));
4068 $this->getDataGenerator()->enrol_user($user->id, $course->id, $roleid);
4070 $this->setUser($user);
4072 $this->assertTrue(course_can_view_participants($coursecontext));
4076 * Check the teacher can still view the participants page without the 'viewparticipants' cap.
4078 public function test_course_can_view_participants_as_teacher_without_view_participants_cap() {
4079 global $DB;
4081 $this->resetAfterTest();
4083 $course = $this->getDataGenerator()->create_course();
4084 $coursecontext = context_course::instance($course->id);
4086 $user = $this->getDataGenerator()->create_user();
4087 $roleid = $DB->get_field('role', 'id', array('shortname' => 'editingteacher'));
4088 $this->getDataGenerator()->enrol_user($user->id, $course->id, $roleid);
4090 $this->setUser($user);
4092 // Disable one of the capabilties.
4093 assign_capability('moodle/course:viewparticipants', CAP_PROHIBIT, $roleid, $coursecontext);
4095 // Should still be able to view the page as they have the 'moodle/course:enrolreview' cap.
4096 $this->assertTrue(course_can_view_participants($coursecontext));
4100 * Check the teacher can still view the participants page without the 'moodle/course:enrolreview' cap.
4102 public function test_course_can_view_participants_as_teacher_without_enrol_review_cap() {
4103 global $DB;
4105 $this->resetAfterTest();
4107 $course = $this->getDataGenerator()->create_course();
4108 $coursecontext = context_course::instance($course->id);
4110 $user = $this->getDataGenerator()->create_user();
4111 $roleid = $DB->get_field('role', 'id', array('shortname' => 'editingteacher'));
4112 $this->getDataGenerator()->enrol_user($user->id, $course->id, $roleid);
4114 $this->setUser($user);
4116 // Disable one of the capabilties.
4117 assign_capability('moodle/course:enrolreview', CAP_PROHIBIT, $roleid, $coursecontext);
4119 // Should still be able to view the page as they have the 'moodle/course:viewparticipants' cap.
4120 $this->assertTrue(course_can_view_participants($coursecontext));
4124 * Check the teacher can not view the participants page without the required caps.
4126 public function test_course_can_view_participants_as_teacher_without_required_caps() {
4127 global $DB;
4129 $this->resetAfterTest();
4131 $course = $this->getDataGenerator()->create_course();
4132 $coursecontext = context_course::instance($course->id);
4134 $user = $this->getDataGenerator()->create_user();
4135 $roleid = $DB->get_field('role', 'id', array('shortname' => 'editingteacher'));
4136 $this->getDataGenerator()->enrol_user($user->id, $course->id, $roleid);
4138 $this->setUser($user);
4140 // Disable the capabilities.
4141 assign_capability('moodle/course:viewparticipants', CAP_PROHIBIT, $roleid, $coursecontext);
4142 assign_capability('moodle/course:enrolreview', CAP_PROHIBIT, $roleid, $coursecontext);
4144 $this->assertFalse(course_can_view_participants($coursecontext));
4148 * Check that an exception is not thrown if we can view the participants page.
4150 public function test_course_require_view_participants() {
4151 $this->resetAfterTest();
4153 $course = $this->getDataGenerator()->create_course();
4154 $coursecontext = context_course::instance($course->id);
4156 $user = $this->getDataGenerator()->create_user();
4157 $this->getDataGenerator()->enrol_user($user->id, $course->id);
4159 $this->setUser($user);
4161 course_require_view_participants($coursecontext);
4165 * Check that an exception is thrown if we can't view the participants page.
4167 public function test_course_require_view_participants_as_student_on_site() {
4168 $this->resetAfterTest();
4170 $course = $this->getDataGenerator()->create_course();
4172 $user = $this->getDataGenerator()->create_user();
4173 $this->getDataGenerator()->enrol_user($user->id, $course->id);
4175 $this->setUser($user);
4177 $this->expectException('required_capability_exception');
4178 course_require_view_participants(context_system::instance());
4182 * Testing the can_download_from_backup_filearea fn.
4184 public function test_can_download_from_backup_filearea() {
4185 global $DB;
4186 $this->resetAfterTest();
4187 $course = $this->getDataGenerator()->create_course();
4188 $context = context_course::instance($course->id);
4189 $user = $this->getDataGenerator()->create_user();
4190 $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
4191 $this->getDataGenerator()->enrol_user($user->id, $course->id, $teacherrole->id);
4193 // The 'automated' backup area. Downloading from this area requires two capabilities.
4194 // If the user has only the 'backup:downloadfile' capability.
4195 unassign_capability('moodle/restore:userinfo', $teacherrole->id, $context);
4196 assign_capability('moodle/backup:downloadfile', CAP_ALLOW, $teacherrole->id, $context);
4197 $this->assertFalse(can_download_from_backup_filearea('automated', $context, $user));
4199 // If the user has only the 'restore:userinfo' capability.
4200 unassign_capability('moodle/backup:downloadfile', $teacherrole->id, $context);
4201 assign_capability('moodle/restore:userinfo', CAP_ALLOW, $teacherrole->id, $context);
4202 $this->assertFalse(can_download_from_backup_filearea('automated', $context, $user));
4204 // If the user has both capabilities.
4205 assign_capability('moodle/backup:downloadfile', CAP_ALLOW, $teacherrole->id, $context);
4206 assign_capability('moodle/restore:userinfo', CAP_ALLOW, $teacherrole->id, $context);
4207 $this->assertTrue(can_download_from_backup_filearea('automated', $context, $user));
4209 // Is the user has neither of the capabilities.
4210 unassign_capability('moodle/backup:downloadfile', $teacherrole->id, $context);
4211 unassign_capability('moodle/restore:userinfo', $teacherrole->id, $context);
4212 $this->assertFalse(can_download_from_backup_filearea('automated', $context, $user));
4214 // The 'course ' and 'backup' backup file areas. These are governed by the same download capability.
4215 // User has the capability.
4216 unassign_capability('moodle/restore:userinfo', $teacherrole->id, $context);
4217 assign_capability('moodle/backup:downloadfile', CAP_ALLOW, $teacherrole->id, $context);
4218 $this->assertTrue(can_download_from_backup_filearea('course', $context, $user));
4219 $this->assertTrue(can_download_from_backup_filearea('backup', $context, $user));
4221 // User doesn't have the capability.
4222 unassign_capability('moodle/backup:downloadfile', $teacherrole->id, $context);
4223 $this->assertFalse(can_download_from_backup_filearea('course', $context, $user));
4224 $this->assertFalse(can_download_from_backup_filearea('backup', $context, $user));
4226 // A file area that doesn't exist. No permissions, regardless of capabilities.
4227 assign_capability('moodle/backup:downloadfile', CAP_ALLOW, $teacherrole->id, $context);
4228 $this->assertFalse(can_download_from_backup_filearea('testing', $context, $user));
4232 * Test cases for the course_classify_courses_for_timeline test.
4234 public function get_course_classify_courses_for_timeline_test_cases() {
4235 $now = time();
4236 $day = 86400;
4238 return [
4239 'no courses' => [
4240 'coursesdata' => [],
4241 'expected' => [
4242 COURSE_TIMELINE_PAST => [],
4243 COURSE_TIMELINE_FUTURE => [],
4244 COURSE_TIMELINE_INPROGRESS => []
4247 'only past' => [
4248 'coursesdata' => [
4250 'shortname' => 'past1',
4251 'startdate' => $now - ($day * 2),
4252 'enddate' => $now - $day
4255 'shortname' => 'past2',
4256 'startdate' => $now - ($day * 2),
4257 'enddate' => $now - $day
4260 'expected' => [
4261 COURSE_TIMELINE_PAST => ['past1', 'past2'],
4262 COURSE_TIMELINE_FUTURE => [],
4263 COURSE_TIMELINE_INPROGRESS => []
4266 'only in progress' => [
4267 'coursesdata' => [
4269 'shortname' => 'inprogress1',
4270 'startdate' => $now - $day,
4271 'enddate' => $now + $day
4274 'shortname' => 'inprogress2',
4275 'startdate' => $now - $day,
4276 'enddate' => $now + $day
4279 'expected' => [
4280 COURSE_TIMELINE_PAST => [],
4281 COURSE_TIMELINE_FUTURE => [],
4282 COURSE_TIMELINE_INPROGRESS => ['inprogress1', 'inprogress2']
4285 'only future' => [
4286 'coursesdata' => [
4288 'shortname' => 'future1',
4289 'startdate' => $now + $day
4292 'shortname' => 'future2',
4293 'startdate' => $now + $day
4296 'expected' => [
4297 COURSE_TIMELINE_PAST => [],
4298 COURSE_TIMELINE_FUTURE => ['future1', 'future2'],
4299 COURSE_TIMELINE_INPROGRESS => []
4302 'combination' => [
4303 'coursesdata' => [
4305 'shortname' => 'past1',
4306 'startdate' => $now - ($day * 2),
4307 'enddate' => $now - $day
4310 'shortname' => 'past2',
4311 'startdate' => $now - ($day * 2),
4312 'enddate' => $now - $day
4315 'shortname' => 'inprogress1',
4316 'startdate' => $now - $day,
4317 'enddate' => $now + $day
4320 'shortname' => 'inprogress2',
4321 'startdate' => $now - $day,
4322 'enddate' => $now + $day
4325 'shortname' => 'future1',
4326 'startdate' => $now + $day
4329 'shortname' => 'future2',
4330 'startdate' => $now + $day
4333 'expected' => [
4334 COURSE_TIMELINE_PAST => ['past1', 'past2'],
4335 COURSE_TIMELINE_FUTURE => ['future1', 'future2'],
4336 COURSE_TIMELINE_INPROGRESS => ['inprogress1', 'inprogress2']
4343 * Test the course_classify_courses_for_timeline function.
4345 * @dataProvider get_course_classify_courses_for_timeline_test_cases()
4346 * @param array $coursesdata Courses to create
4347 * @param array $expected Expected test results.
4349 public function test_course_classify_courses_for_timeline($coursesdata, $expected) {
4350 $this->resetAfterTest();
4351 $generator = $this->getDataGenerator();
4353 $courses = array_map(function($coursedata) use ($generator) {
4354 return $generator->create_course($coursedata);
4355 }, $coursesdata);
4357 sort($expected[COURSE_TIMELINE_PAST]);
4358 sort($expected[COURSE_TIMELINE_FUTURE]);
4359 sort($expected[COURSE_TIMELINE_INPROGRESS]);
4361 $results = course_classify_courses_for_timeline($courses);
4363 $actualpast = array_map(function($result) {
4364 return $result->shortname;
4365 }, $results[COURSE_TIMELINE_PAST]);
4367 $actualfuture = array_map(function($result) {
4368 return $result->shortname;
4369 }, $results[COURSE_TIMELINE_FUTURE]);
4371 $actualinprogress = array_map(function($result) {
4372 return $result->shortname;
4373 }, $results[COURSE_TIMELINE_INPROGRESS]);
4375 sort($actualpast);
4376 sort($actualfuture);
4377 sort($actualinprogress);
4379 $this->assertEquals($expected[COURSE_TIMELINE_PAST], $actualpast);
4380 $this->assertEquals($expected[COURSE_TIMELINE_FUTURE], $actualfuture);
4381 $this->assertEquals($expected[COURSE_TIMELINE_INPROGRESS], $actualinprogress);
4385 * Test cases for the course_get_enrolled_courses_for_logged_in_user tests.
4387 public function get_course_get_enrolled_courses_for_logged_in_user_test_cases() {
4388 $buildexpectedresult = function($limit, $offset) {
4389 $result = [];
4390 for ($i = $offset; $i < $offset + $limit; $i++) {
4391 $result[] = "testcourse{$i}";
4393 return $result;
4396 return [
4397 'zero records' => [
4398 'dbquerylimit' => 3,
4399 'totalcourses' => 0,
4400 'limit' => 0,
4401 'offset' => 0,
4402 'expecteddbqueries' => 1,
4403 'expectedresult' => $buildexpectedresult(0, 0)
4405 'less than query limit' => [
4406 'dbquerylimit' => 3,
4407 'totalcourses' => 2,
4408 'limit' => 0,
4409 'offset' => 0,
4410 'expecteddbqueries' => 1,
4411 'expectedresult' => $buildexpectedresult(2, 0)
4413 'more than query limit' => [
4414 'dbquerylimit' => 3,
4415 'totalcourses' => 7,
4416 'limit' => 0,
4417 'offset' => 0,
4418 'expecteddbqueries' => 3,
4419 'expectedresult' => $buildexpectedresult(7, 0)
4421 'limit less than query limit' => [
4422 'dbquerylimit' => 3,
4423 'totalcourses' => 7,
4424 'limit' => 2,
4425 'offset' => 0,
4426 'expecteddbqueries' => 1,
4427 'expectedresult' => $buildexpectedresult(2, 0)
4429 'limit less than query limit with offset' => [
4430 'dbquerylimit' => 3,
4431 'totalcourses' => 7,
4432 'limit' => 2,
4433 'offset' => 2,
4434 'expecteddbqueries' => 1,
4435 'expectedresult' => $buildexpectedresult(2, 2)
4437 'limit less than total' => [
4438 'dbquerylimit' => 3,
4439 'totalcourses' => 9,
4440 'limit' => 6,
4441 'offset' => 0,
4442 'expecteddbqueries' => 2,
4443 'expectedresult' => $buildexpectedresult(6, 0)
4445 'less results than limit' => [
4446 'dbquerylimit' => 4,
4447 'totalcourses' => 9,
4448 'limit' => 20,
4449 'offset' => 0,
4450 'expecteddbqueries' => 3,
4451 'expectedresult' => $buildexpectedresult(9, 0)
4453 'less results than limit exact divisible' => [
4454 'dbquerylimit' => 3,
4455 'totalcourses' => 9,
4456 'limit' => 20,
4457 'offset' => 0,
4458 'expecteddbqueries' => 4,
4459 'expectedresult' => $buildexpectedresult(9, 0)
4461 'less results than limit with offset' => [
4462 'dbquerylimit' => 3,
4463 'totalcourses' => 9,
4464 'limit' => 10,
4465 'offset' => 5,
4466 'expecteddbqueries' => 2,
4467 'expectedresult' => $buildexpectedresult(4, 5)
4473 * Test the course_get_enrolled_courses_for_logged_in_user function.
4475 * @dataProvider get_course_get_enrolled_courses_for_logged_in_user_test_cases()
4476 * @param int $dbquerylimit Number of records to load per DB request
4477 * @param int $totalcourses Number of courses to create
4478 * @param int $limit Maximum number of results to get.
4479 * @param int $offset Skip this number of results from the start of the result set.
4480 * @param int $expecteddbqueries The number of DB queries expected during the test.
4481 * @param array $expectedresult Expected test results.
4483 public function test_course_get_enrolled_courses_for_logged_in_user(
4484 $dbquerylimit,
4485 $totalcourses,
4486 $limit,
4487 $offset,
4488 $expecteddbqueries,
4489 $expectedresult
4491 global $DB;
4493 $this->resetAfterTest();
4494 $generator = $this->getDataGenerator();
4495 $student = $generator->create_user();
4497 for ($i = 0; $i < $totalcourses; $i++) {
4498 $shortname = "testcourse{$i}";
4499 $course = $generator->create_course(['shortname' => $shortname]);
4500 $generator->enrol_user($student->id, $course->id, 'student');
4503 $this->setUser($student);
4505 $initialquerycount = $DB->perf_get_queries();
4506 $courses = course_get_enrolled_courses_for_logged_in_user($limit, $offset, 'shortname ASC', 'shortname', $dbquerylimit);
4508 // Loop over the result set to force the lazy loading to kick in so that we can check the
4509 // number of DB queries.
4510 $actualresult = array_map(function($course) {
4511 return $course->shortname;
4512 }, iterator_to_array($courses, false));
4514 sort($expectedresult);
4516 $this->assertEquals($expectedresult, $actualresult);
4517 $this->assertEquals($expecteddbqueries, $DB->perf_get_queries() - $initialquerycount);
4521 * Test cases for the course_filter_courses_by_timeline_classification tests.
4523 public function get_course_filter_courses_by_timeline_classification_test_cases() {
4524 $now = time();
4525 $day = 86400;
4527 $coursedata = [
4529 'shortname' => 'apast',
4530 'startdate' => $now - ($day * 2),
4531 'enddate' => $now - $day
4534 'shortname' => 'bpast',
4535 'startdate' => $now - ($day * 2),
4536 'enddate' => $now - $day
4539 'shortname' => 'cpast',
4540 'startdate' => $now - ($day * 2),
4541 'enddate' => $now - $day
4544 'shortname' => 'dpast',
4545 'startdate' => $now - ($day * 2),
4546 'enddate' => $now - $day
4549 'shortname' => 'epast',
4550 'startdate' => $now - ($day * 2),
4551 'enddate' => $now - $day
4554 'shortname' => 'ainprogress',
4555 'startdate' => $now - $day,
4556 'enddate' => $now + $day
4559 'shortname' => 'binprogress',
4560 'startdate' => $now - $day,
4561 'enddate' => $now + $day
4564 'shortname' => 'cinprogress',
4565 'startdate' => $now - $day,
4566 'enddate' => $now + $day
4569 'shortname' => 'dinprogress',
4570 'startdate' => $now - $day,
4571 'enddate' => $now + $day
4574 'shortname' => 'einprogress',
4575 'startdate' => $now - $day,
4576 'enddate' => $now + $day
4579 'shortname' => 'afuture',
4580 'startdate' => $now + $day
4583 'shortname' => 'bfuture',
4584 'startdate' => $now + $day
4587 'shortname' => 'cfuture',
4588 'startdate' => $now + $day
4591 'shortname' => 'dfuture',
4592 'startdate' => $now + $day
4595 'shortname' => 'efuture',
4596 'startdate' => $now + $day
4600 // Raw enrolled courses result set should be returned in this order:
4601 // afuture, ainprogress, apast, bfuture, binprogress, bpast, cfuture, cinprogress, cpast,
4602 // dfuture, dinprogress, dpast, efuture, einprogress, epast
4604 // By classification the offset values for each record should be:
4605 // COURSE_TIMELINE_FUTURE
4606 // 0 (afuture), 3 (bfuture), 6 (cfuture), 9 (dfuture), 12 (efuture)
4607 // COURSE_TIMELINE_INPROGRESS
4608 // 1 (ainprogress), 4 (binprogress), 7 (cinprogress), 10 (dinprogress), 13 (einprogress)
4609 // COURSE_TIMELINE_PAST
4610 // 2 (apast), 5 (bpast), 8 (cpast), 11 (dpast), 14 (epast).
4611 return [
4612 'empty set' => [
4613 'coursedata' => [],
4614 'classification' => COURSE_TIMELINE_FUTURE,
4615 'limit' => 2,
4616 'offset' => 0,
4617 'expectedcourses' => [],
4618 'expectedprocessedcount' => 0
4620 // COURSE_TIMELINE_FUTURE.
4621 'future not limit no offset' => [
4622 'coursedata' => $coursedata,
4623 'classification' => COURSE_TIMELINE_FUTURE,
4624 'limit' => 0,
4625 'offset' => 0,
4626 'expectedcourses' => ['afuture', 'bfuture', 'cfuture', 'dfuture', 'efuture'],
4627 'expectedprocessedcount' => 15
4629 'future no offset' => [
4630 'coursedata' => $coursedata,
4631 'classification' => COURSE_TIMELINE_FUTURE,
4632 'limit' => 2,
4633 'offset' => 0,
4634 'expectedcourses' => ['afuture', 'bfuture'],
4635 'expectedprocessedcount' => 4
4637 'future offset' => [
4638 'coursedata' => $coursedata,
4639 'classification' => COURSE_TIMELINE_FUTURE,
4640 'limit' => 2,
4641 'offset' => 2,
4642 'expectedcourses' => ['bfuture', 'cfuture'],
4643 'expectedprocessedcount' => 5
4645 'future exact limit' => [
4646 'coursedata' => $coursedata,
4647 'classification' => COURSE_TIMELINE_FUTURE,
4648 'limit' => 5,
4649 'offset' => 0,
4650 'expectedcourses' => ['afuture', 'bfuture', 'cfuture', 'dfuture', 'efuture'],
4651 'expectedprocessedcount' => 13
4653 'future limit less results' => [
4654 'coursedata' => $coursedata,
4655 'classification' => COURSE_TIMELINE_FUTURE,
4656 'limit' => 10,
4657 'offset' => 0,
4658 'expectedcourses' => ['afuture', 'bfuture', 'cfuture', 'dfuture', 'efuture'],
4659 'expectedprocessedcount' => 15
4661 'future limit less results with offset' => [
4662 'coursedata' => $coursedata,
4663 'classification' => COURSE_TIMELINE_FUTURE,
4664 'limit' => 10,
4665 'offset' => 5,
4666 'expectedcourses' => ['cfuture', 'dfuture', 'efuture'],
4667 'expectedprocessedcount' => 10
4673 * Test the course_filter_courses_by_timeline_classification function.
4675 * @dataProvider get_course_filter_courses_by_timeline_classification_test_cases()
4676 * @param array $coursedata Course test data to create.
4677 * @param string $classification Timeline classification.
4678 * @param int $limit Maximum number of results to return.
4679 * @param int $offset Results to skip at the start of the result set.
4680 * @param string[] $expectedcourses Expected courses in results.
4681 * @param int $expectedprocessedcount Expected number of course records to be processed.
4683 public function test_course_filter_courses_by_timeline_classification(
4684 $coursedata,
4685 $classification,
4686 $limit,
4687 $offset,
4688 $expectedcourses,
4689 $expectedprocessedcount
4691 $this->resetAfterTest();
4692 $generator = $this->getDataGenerator();
4694 $courses = array_map(function($coursedata) use ($generator) {
4695 return $generator->create_course($coursedata);
4696 }, $coursedata);
4698 $student = $generator->create_user();
4700 foreach ($courses as $course) {
4701 $generator->enrol_user($student->id, $course->id, 'student');
4704 $this->setUser($student);
4706 $coursesgenerator = course_get_enrolled_courses_for_logged_in_user(0, $offset, 'shortname ASC', 'shortname');
4707 list($result, $processedcount) = course_filter_courses_by_timeline_classification(
4708 $coursesgenerator,
4709 $classification,
4710 $limit
4713 $actual = array_map(function($course) {
4714 return $course->shortname;
4715 }, $result);
4717 $this->assertEquals($expectedcourses, $actual);
4718 $this->assertEquals($expectedprocessedcount, $processedcount);
4722 * Test cases for the course_filter_courses_by_timeline_classification w/ hidden courses tests.
4724 public function get_course_filter_courses_by_timeline_classification_hidden_courses_test_cases() {
4725 $now = time();
4726 $day = 86400;
4728 $coursedata = [
4730 'shortname' => 'apast',
4731 'startdate' => $now - ($day * 2),
4732 'enddate' => $now - $day
4735 'shortname' => 'bpast',
4736 'startdate' => $now - ($day * 2),
4737 'enddate' => $now - $day
4740 'shortname' => 'cpast',
4741 'startdate' => $now - ($day * 2),
4742 'enddate' => $now - $day
4745 'shortname' => 'dpast',
4746 'startdate' => $now - ($day * 2),
4747 'enddate' => $now - $day
4750 'shortname' => 'epast',
4751 'startdate' => $now - ($day * 2),
4752 'enddate' => $now - $day
4755 'shortname' => 'ainprogress',
4756 'startdate' => $now - $day,
4757 'enddate' => $now + $day
4760 'shortname' => 'binprogress',
4761 'startdate' => $now - $day,
4762 'enddate' => $now + $day
4765 'shortname' => 'cinprogress',
4766 'startdate' => $now - $day,
4767 'enddate' => $now + $day
4770 'shortname' => 'dinprogress',
4771 'startdate' => $now - $day,
4772 'enddate' => $now + $day
4775 'shortname' => 'einprogress',
4776 'startdate' => $now - $day,
4777 'enddate' => $now + $day
4780 'shortname' => 'afuture',
4781 'startdate' => $now + $day
4784 'shortname' => 'bfuture',
4785 'startdate' => $now + $day
4788 'shortname' => 'cfuture',
4789 'startdate' => $now + $day
4792 'shortname' => 'dfuture',
4793 'startdate' => $now + $day
4796 'shortname' => 'efuture',
4797 'startdate' => $now + $day
4801 // Raw enrolled courses result set should be returned in this order:
4802 // afuture, ainprogress, apast, bfuture, binprogress, bpast, cfuture, cinprogress, cpast,
4803 // dfuture, dinprogress, dpast, efuture, einprogress, epast
4805 // By classification the offset values for each record should be:
4806 // COURSE_TIMELINE_FUTURE
4807 // 0 (afuture), 3 (bfuture), 6 (cfuture), 9 (dfuture), 12 (efuture)
4808 // COURSE_TIMELINE_INPROGRESS
4809 // 1 (ainprogress), 4 (binprogress), 7 (cinprogress), 10 (dinprogress), 13 (einprogress)
4810 // COURSE_TIMELINE_PAST
4811 // 2 (apast), 5 (bpast), 8 (cpast), 11 (dpast), 14 (epast).
4812 return [
4813 'empty set' => [
4814 'coursedata' => [],
4815 'classification' => COURSE_TIMELINE_FUTURE,
4816 'limit' => 2,
4817 'offset' => 0,
4818 'expectedcourses' => [],
4819 'expectedprocessedcount' => 0,
4820 'hiddencourse' => ''
4822 // COURSE_TIMELINE_FUTURE.
4823 'future not limit no offset' => [
4824 'coursedata' => $coursedata,
4825 'classification' => COURSE_TIMELINE_FUTURE,
4826 'limit' => 0,
4827 'offset' => 0,
4828 'expectedcourses' => ['afuture', 'cfuture', 'dfuture', 'efuture'],
4829 'expectedprocessedcount' => 15,
4830 'hiddencourse' => 'bfuture'
4832 'future no offset' => [
4833 'coursedata' => $coursedata,
4834 'classification' => COURSE_TIMELINE_FUTURE,
4835 'limit' => 2,
4836 'offset' => 0,
4837 'expectedcourses' => ['afuture', 'cfuture'],
4838 'expectedprocessedcount' => 7,
4839 'hiddencourse' => 'bfuture'
4841 'future offset' => [
4842 'coursedata' => $coursedata,
4843 'classification' => COURSE_TIMELINE_FUTURE,
4844 'limit' => 2,
4845 'offset' => 2,
4846 'expectedcourses' => ['bfuture', 'dfuture'],
4847 'expectedprocessedcount' => 8,
4848 'hiddencourse' => 'cfuture'
4850 'future exact limit' => [
4851 'coursedata' => $coursedata,
4852 'classification' => COURSE_TIMELINE_FUTURE,
4853 'limit' => 5,
4854 'offset' => 0,
4855 'expectedcourses' => ['afuture', 'cfuture', 'dfuture', 'efuture'],
4856 'expectedprocessedcount' => 15,
4857 'hiddencourse' => 'bfuture'
4859 'future limit less results' => [
4860 'coursedata' => $coursedata,
4861 'classification' => COURSE_TIMELINE_FUTURE,
4862 'limit' => 10,
4863 'offset' => 0,
4864 'expectedcourses' => ['afuture', 'cfuture', 'dfuture', 'efuture'],
4865 'expectedprocessedcount' => 15,
4866 'hiddencourse' => 'bfuture'
4868 'future limit less results with offset' => [
4869 'coursedata' => $coursedata,
4870 'classification' => COURSE_TIMELINE_FUTURE,
4871 'limit' => 10,
4872 'offset' => 5,
4873 'expectedcourses' => ['cfuture', 'efuture'],
4874 'expectedprocessedcount' => 10,
4875 'hiddencourse' => 'dfuture'
4881 * Test the course_filter_courses_by_timeline_classification function hidden courses.
4883 * @dataProvider get_course_filter_courses_by_timeline_classification_hidden_courses_test_cases()
4884 * @param array $coursedata Course test data to create.
4885 * @param string $classification Timeline classification.
4886 * @param int $limit Maximum number of results to return.
4887 * @param int $offset Results to skip at the start of the result set.
4888 * @param string[] $expectedcourses Expected courses in results.
4889 * @param int $expectedprocessedcount Expected number of course records to be processed.
4890 * @param int $hiddencourse The course to hide as part of this process
4892 public function test_course_filter_courses_by_timeline_classification_with_hidden_courses(
4893 $coursedata,
4894 $classification,
4895 $limit,
4896 $offset,
4897 $expectedcourses,
4898 $expectedprocessedcount,
4899 $hiddencourse
4901 $this->resetAfterTest();
4902 $generator = $this->getDataGenerator();
4903 $student = $generator->create_user();
4904 $this->setUser($student);
4906 $courses = array_map(function($coursedata) use ($generator, $hiddencourse) {
4907 $course = $generator->create_course($coursedata);
4908 if ($course->shortname == $hiddencourse) {
4909 set_user_preference('block_myoverview_hidden_course_' . $course->id, true);
4911 return $course;
4912 }, $coursedata);
4914 foreach ($courses as $course) {
4915 $generator->enrol_user($student->id, $course->id, 'student');
4918 $coursesgenerator = course_get_enrolled_courses_for_logged_in_user(0, $offset, 'shortname ASC', 'shortname');
4919 list($result, $processedcount) = course_filter_courses_by_timeline_classification(
4920 $coursesgenerator,
4921 $classification,
4922 $limit
4925 $actual = array_map(function($course) {
4926 return $course->shortname;
4927 }, $result);
4929 $this->assertEquals($expectedcourses, $actual);
4930 $this->assertEquals($expectedprocessedcount, $processedcount);
4935 * Testing core_course_core_calendar_get_valid_event_timestart_range when the course has no end date.
4937 public function test_core_course_core_calendar_get_valid_event_timestart_range_no_enddate() {
4938 global $CFG;
4939 require_once($CFG->dirroot . "/calendar/lib.php");
4941 $this->resetAfterTest(true);
4942 $this->setAdminUser();
4943 $generator = $this->getDataGenerator();
4944 $now = time();
4945 $course = $generator->create_course(['startdate' => $now - 86400]);
4947 // Create a course event.
4948 $event = new \calendar_event([
4949 'name' => 'Test course event',
4950 'eventtype' => 'course',
4951 'courseid' => $course->id,
4954 list ($min, $max) = core_course_core_calendar_get_valid_event_timestart_range($event, $course);
4955 $this->assertEquals($course->startdate, $min[0]);
4956 $this->assertNull($max);
4960 * Testing core_course_core_calendar_get_valid_event_timestart_range when the course has end date.
4962 public function test_core_course_core_calendar_get_valid_event_timestart_range_with_enddate() {
4963 global $CFG;
4964 require_once($CFG->dirroot . "/calendar/lib.php");
4966 $this->resetAfterTest(true);
4967 $this->setAdminUser();
4968 $generator = $this->getDataGenerator();
4969 $now = time();
4970 $course = $generator->create_course(['startdate' => $now - 86400, 'enddate' => $now + 86400]);
4972 // Create a course event.
4973 $event = new \calendar_event([
4974 'name' => 'Test course event',
4975 'eventtype' => 'course',
4976 'courseid' => $course->id,
4979 list ($min, $max) = core_course_core_calendar_get_valid_event_timestart_range($event, $course);
4980 $this->assertEquals($course->startdate, $min[0]);
4981 $this->assertNull($max);