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