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