MDL-38359 courselib: Fix unit tests
[moodle.git] / course / tests / courselib_test.php
CommitLineData
354b214c
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Course related unit tests
19 *
20 * @package core
21 * @category phpunit
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
f70bfb84
FM
28global $CFG;
29require_once($CFG->dirroot.'/course/lib.php');
354b214c
PS
30
31class courselib_testcase extends advanced_testcase {
32
dd5d933f 33 /**
7bf4f6e9
JM
34 * Set forum specific test values for calling create_module().
35 *
36 * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
dd5d933f 37 */
7bf4f6e9
JM
38 private function forum_create_set_values(&$moduleinfo) {
39 // Completion specific to forum - optional.
40 $moduleinfo->completionposts = 3;
41 $moduleinfo->completiondiscussions = 1;
42 $moduleinfo->completionreplies = 2;
dd5d933f 43
7cb0ea2c 44 // Specific values to the Forum module.
dd5d933f
JM
45 $moduleinfo->forcesubscribe = FORUM_INITIALSUBSCRIBE;
46 $moduleinfo->type = 'single';
7bf4f6e9
JM
47 $moduleinfo->trackingtype = FORUM_TRACKING_ON;
48 $moduleinfo->maxbytes = 10240;
49 $moduleinfo->maxattachments = 2;
50
7cb0ea2c 51 // Post threshold for blocking - specific to forum.
7bf4f6e9
JM
52 $moduleinfo->blockperiod = 60*60*24;
53 $moduleinfo->blockafter = 10;
54 $moduleinfo->warnafter = 5;
55 }
dd5d933f 56
7bf4f6e9
JM
57 /**
58 * Execute test asserts on the saved DB data by create_module($forum).
59 *
60 * @param object $moduleinfo - the specific forum values that were used to create a forum.
61 * @param object $dbmodinstance - the DB values of the created forum.
62 */
63 private function forum_create_run_asserts($moduleinfo, $dbmodinstance) {
64 // Compare values specific to forums.
65 $this->assertEquals($moduleinfo->forcesubscribe, $dbmodinstance->forcesubscribe);
66 $this->assertEquals($moduleinfo->type, $dbmodinstance->type);
67 $this->assertEquals($moduleinfo->assessed, $dbmodinstance->assessed);
68 $this->assertEquals($moduleinfo->completionposts, $dbmodinstance->completionposts);
69 $this->assertEquals($moduleinfo->completiondiscussions, $dbmodinstance->completiondiscussions);
70 $this->assertEquals($moduleinfo->completionreplies, $dbmodinstance->completionreplies);
71 $this->assertEquals($moduleinfo->scale, $dbmodinstance->scale);
72 $this->assertEquals($moduleinfo->assesstimestart, $dbmodinstance->assesstimestart);
73 $this->assertEquals($moduleinfo->assesstimefinish, $dbmodinstance->assesstimefinish);
74 $this->assertEquals($moduleinfo->rsstype, $dbmodinstance->rsstype);
75 $this->assertEquals($moduleinfo->rssarticles, $dbmodinstance->rssarticles);
76 $this->assertEquals($moduleinfo->trackingtype, $dbmodinstance->trackingtype);
77 $this->assertEquals($moduleinfo->maxbytes, $dbmodinstance->maxbytes);
78 $this->assertEquals($moduleinfo->maxattachments, $dbmodinstance->maxattachments);
79 $this->assertEquals($moduleinfo->blockperiod, $dbmodinstance->blockperiod);
80 $this->assertEquals($moduleinfo->blockafter, $dbmodinstance->blockafter);
81 $this->assertEquals($moduleinfo->warnafter, $dbmodinstance->warnafter);
82 }
dd5d933f 83
7bf4f6e9
JM
84 /**
85 * Set assign module specific test values for calling create_module().
86 *
87 * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
88 */
89 private function assign_create_set_values(&$moduleinfo) {
7cb0ea2c 90 // Specific values to the Assign module.
dd5d933f
JM
91 $moduleinfo->alwaysshowdescription = true;
92 $moduleinfo->submissiondrafts = true;
93 $moduleinfo->requiresubmissionstatement = true;
94 $moduleinfo->sendnotifications = true;
95 $moduleinfo->sendlatenotifications = true;
7bf4f6e9
JM
96 $moduleinfo->duedate = time() + (7 * 24 * 3600);
97 $moduleinfo->cutoffdate = time() + (7 * 24 * 3600);
98 $moduleinfo->allowsubmissionsfromdate = time();
dd5d933f
JM
99 $moduleinfo->teamsubmission = true;
100 $moduleinfo->requireallteammemberssubmit = true;
101 $moduleinfo->teamsubmissiongroupingid = true;
102 $moduleinfo->blindmarking = true;
7f198356
DW
103 $moduleinfo->markingworkflow = true;
104 $moduleinfo->markingallocation = true;
dd5d933f
JM
105 $moduleinfo->assignsubmission_onlinetext_enabled = true;
106 $moduleinfo->assignsubmission_file_enabled = true;
107 $moduleinfo->assignsubmission_file_maxfiles = 1;
108 $moduleinfo->assignsubmission_file_maxsizebytes = 1000000;
109 $moduleinfo->assignsubmission_comments_enabled = true;
110 $moduleinfo->assignfeedback_comments_enabled = true;
111 $moduleinfo->assignfeedback_offline_enabled = true;
112 $moduleinfo->assignfeedback_file_enabled = true;
113
7cb0ea2c 114 // Advanced grading.
7bf4f6e9
JM
115 $gradingmethods = grading_manager::available_methods();
116 $moduleinfo->advancedgradingmethod_submissions = current(array_keys($gradingmethods));
117 }
dd5d933f 118
7bf4f6e9
JM
119 /**
120 * Execute test asserts on the saved DB data by create_module($assign).
121 *
122 * @param object $moduleinfo - the specific assign module values that were used to create an assign module.
123 * @param object $dbmodinstance - the DB values of the created assign module.
124 */
125 private function assign_create_run_asserts($moduleinfo, $dbmodinstance) {
126 global $DB;
127
128 $this->assertEquals($moduleinfo->alwaysshowdescription, $dbmodinstance->alwaysshowdescription);
129 $this->assertEquals($moduleinfo->submissiondrafts, $dbmodinstance->submissiondrafts);
130 $this->assertEquals($moduleinfo->requiresubmissionstatement, $dbmodinstance->requiresubmissionstatement);
131 $this->assertEquals($moduleinfo->sendnotifications, $dbmodinstance->sendnotifications);
132 $this->assertEquals($moduleinfo->duedate, $dbmodinstance->duedate);
133 $this->assertEquals($moduleinfo->cutoffdate, $dbmodinstance->cutoffdate);
134 $this->assertEquals($moduleinfo->allowsubmissionsfromdate, $dbmodinstance->allowsubmissionsfromdate);
135 $this->assertEquals($moduleinfo->teamsubmission, $dbmodinstance->teamsubmission);
136 $this->assertEquals($moduleinfo->requireallteammemberssubmit, $dbmodinstance->requireallteammemberssubmit);
137 $this->assertEquals($moduleinfo->teamsubmissiongroupingid, $dbmodinstance->teamsubmissiongroupingid);
138 $this->assertEquals($moduleinfo->blindmarking, $dbmodinstance->blindmarking);
7f198356
DW
139 $this->assertEquals($moduleinfo->markingworkflow, $dbmodinstance->markingworkflow);
140 $this->assertEquals($moduleinfo->markingallocation, $dbmodinstance->markingallocation);
7bf4f6e9
JM
141 // The goal not being to fully test assign_add_instance() we'll stop here for the assign tests - to avoid too many DB queries.
142
7cb0ea2c 143 // Advanced grading.
7bf4f6e9
JM
144 $contextmodule = context_module::instance($dbmodinstance->id);
145 $advancedgradingmethod = $DB->get_record('grading_areas',
146 array('contextid' => $contextmodule->id,
147 'activemethod' => $moduleinfo->advancedgradingmethod_submissions));
148 $this->assertEquals($moduleinfo->advancedgradingmethod_submissions, $advancedgradingmethod);
149 }
150
151 /**
152 * Run some asserts test for a specific module for the function create_module().
153 *
154 * The function has been created (and is called) for $this->test_create_module().
155 * Note that the call to MODULE_create_set_values and MODULE_create_run_asserts are done after the common set values/run asserts.
156 * So if you want, you can overwrite the default values/asserts in the respective functions.
157 * @param string $modulename Name of the module ('forum', 'assign', 'book'...).
158 */
159 private function create_specific_module_test($modulename) {
160 global $DB, $CFG;
161
162 $this->resetAfterTest(true);
163
164 $this->setAdminUser();
165
166 // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
167 require_once($CFG->dirroot.'/mod/'. $modulename .'/lib.php');
168
169 // Enable avaibility.
170 // If not enabled all conditional fields will be ignored.
171 set_config('enableavailability', 1);
172
173 // Enable course completion.
174 // If not enabled all completion settings will be ignored.
175 set_config('enablecompletion', COMPLETION_ENABLED);
176
177 // Enable forum RSS feeds.
178 set_config('enablerssfeeds', 1);
179 set_config('forum_enablerssfeeds', 1);
180
181 $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED),
182 array('createsections'=>true));
183
184 $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
dd5d933f 185
7cb0ea2c 186 // Create assign module instance for test.
7bf4f6e9
JM
187 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
188 $params['course'] = $course->id;
189 $instance = $generator->create_instance($params);
190 $assigncm = get_coursemodule_from_instance('assign', $instance->id);
dd5d933f 191
7bf4f6e9
JM
192 // Module test values.
193 $moduleinfo = new stdClass();
dd5d933f 194
7bf4f6e9
JM
195 // Always mandatory generic values to any module.
196 $moduleinfo->modulename = $modulename;
197 $moduleinfo->section = 1; // This is the section number in the course. Not the section id in the database.
198 $moduleinfo->course = $course->id;
199 $moduleinfo->groupingid = $grouping->id;
200 $moduleinfo->groupmembersonly = 0;
201 $moduleinfo->visible = true;
202
203 // Sometimes optional generic values for some modules.
204 $moduleinfo->name = 'My test module';
205 $moduleinfo->showdescription = 1; // standard boolean
206 require_once($CFG->libdir . '/gradelib.php');
207 $gradecats = grade_get_categories_menu($moduleinfo->course, false);
208 $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
209 $moduleinfo->gradecat = $gradecatid;
210 $moduleinfo->groupmode = VISIBLEGROUPS;
211 $moduleinfo->cmidnumber = 'idnumber_XXX';
212
213 // Completion common to all module.
214 $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC;
215 $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED;
7cb0ea2c 216 $moduleinfo->completiongradeitemnumber = 1;
7bf4f6e9
JM
217 $moduleinfo->completionexpected = time() + (7 * 24 * 3600);
218
219 // Conditional activity.
220 $moduleinfo->availablefrom = time();
221 $moduleinfo->availableuntil = time() + (7 * 24 * 3600);
222 $moduleinfo->showavailability = CONDITION_STUDENTVIEW_SHOW;
223 $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself.
224 $moduleinfo->conditiongradegroup = array(array('conditiongradeitemid' => $coursegradeitem->id, 'conditiongrademin' => 10, 'conditiongrademax' => 80));
225 $moduleinfo->conditionfieldgroup = array(array('conditionfield' => 'email', 'conditionfieldoperator' => OP_CONTAINS, 'conditionfieldvalue' => '@'));
226 $moduleinfo->conditioncompletiongroup = array(array('conditionsourcecmid' => $assigncm->id, 'conditionrequiredcompletion' => COMPLETION_COMPLETE)); // "conditionsourcecmid == 0" => none
227
228 // Grading and Advanced grading.
229 require_once($CFG->dirroot . '/rating/lib.php');
230 $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE;
231 $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number.
232 $moduleinfo->assesstimestart = time();
233 $moduleinfo->assesstimefinish = time() + (7 * 24 * 3600);
234
7cb0ea2c 235 // RSS.
7bf4f6e9
JM
236 $moduleinfo->rsstype = 2;
237 $moduleinfo->rssarticles = 10;
238
239 // Optional intro editor (depends of module).
7bf4f6e9
JM
240 $draftid_editor = 0;
241 file_prepare_draft_area($draftid_editor, null, null, null, null);
242 $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
dd5d933f 243
7bf4f6e9
JM
244 // Following is the advanced grading method area called 'submissions' for the 'assign' module.
245 if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
246 $moduleinfo->grade = 100;
247 }
7cb0ea2c 248
7bf4f6e9
JM
249 // Plagiarism form values.
250 // No plagiarism plugin installed by default. Use this space to make your own test.
251
252 // Values specific to the module.
253 $modulesetvalues = $modulename.'_create_set_values';
254 $this->$modulesetvalues($moduleinfo);
255
256 // Create the module.
257 $result = create_module($moduleinfo);
258
259 // Retrieve the module info.
260 $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance));
261 $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance);
262 // We passed the course section number to create_courses but $dbcm contain the section id.
263 // We need to retrieve the db course section number.
264 $section = $DB->get_record('course_sections', array('course' => $dbcm->course, 'id' => $dbcm->section));
7cb0ea2c 265 // Retrieve the grade item.
7bf4f6e9
JM
266 $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course,
267 'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename));
268
269 // Compare the values common to all module instances.
270 $this->assertEquals($moduleinfo->modulename, $dbcm->modname);
271 $this->assertEquals($moduleinfo->section, $section->section);
272 $this->assertEquals($moduleinfo->course, $dbcm->course);
273 $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
274 $this->assertEquals($moduleinfo->groupmembersonly, $dbcm->groupmembersonly);
275 $this->assertEquals($moduleinfo->visible, $dbcm->visible);
276 $this->assertEquals($moduleinfo->completion, $dbcm->completion);
277 $this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
278 $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber);
279 $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected);
280 $this->assertEquals($moduleinfo->availablefrom, $dbcm->availablefrom);
281 $this->assertEquals($moduleinfo->availableuntil, $dbcm->availableuntil);
282 $this->assertEquals($moduleinfo->showavailability, $dbcm->showavailability);
283 $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription);
284 $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode);
285 $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber);
286 $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid);
287
288 // Optional grade testing.
289 if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
290 $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade);
291 }
292
293 // Some optional (but quite common) to some module.
294 $this->assertEquals($moduleinfo->name, $dbmodinstance->name);
295 $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro);
296 $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat);
297
298 // Common values when conditional activity is enabled.
299 foreach ($moduleinfo->conditionfieldgroup as $fieldgroup) {
300 $isfieldgroupsaved = $DB->count_records('course_modules_avail_fields', array('coursemoduleid' => $dbcm->id,
301 'userfield' => $fieldgroup['conditionfield'], 'operator' => $fieldgroup['conditionfieldoperator'],
302 'value' => $fieldgroup['conditionfieldvalue']));
303 $this->assertEquals(1, $isfieldgroupsaved);
304 }
305 foreach ($moduleinfo->conditiongradegroup as $gradegroup) {
306 $isgradegroupsaved = $DB->count_records('course_modules_availability', array('coursemoduleid' => $dbcm->id,
307 'grademin' => $gradegroup['conditiongrademin'], 'grademax' => $gradegroup['conditiongrademax'],
308 'gradeitemid' => $gradegroup['conditiongradeitemid']));
309 $this->assertEquals(1, $isgradegroupsaved);
310 }
311 foreach ($moduleinfo->conditioncompletiongroup as $completiongroup) {
312 $iscompletiongroupsaved = $DB->count_records('course_modules_availability', array('coursemoduleid' => $dbcm->id,
313 'sourcecmid' => $completiongroup['conditionsourcecmid'], 'requiredcompletion' => $completiongroup['conditionrequiredcompletion']));
314 $this->assertEquals(1, $iscompletiongroupsaved);
315 }
316
7cb0ea2c 317 // Test specific to the module.
7bf4f6e9
JM
318 $modulerunasserts = $modulename.'_create_run_asserts';
319 $this->$modulerunasserts($moduleinfo, $dbmodinstance);
7bf4f6e9 320 }
dd5d933f 321
7bf4f6e9
JM
322 /**
323 * Test create_module() for multiple modules defined in the $modules array (first declaration of the function).
324 */
325 public function test_create_module() {
326 // Add the module name you want to test here.
7cb0ea2c 327 // Create the match MODULENAME_create_set_values() and MODULENAME_create_run_asserts().
7bf4f6e9
JM
328 $modules = array('forum', 'assign');
329 // Run all tests.
330 foreach ($modules as $modulename) {
331 $this->create_specific_module_test($modulename);
332 }
dd5d933f
JM
333 }
334
335 /**
7bf4f6e9 336 * Test update_module() for multiple modules defined in the $modules array (first declaration of the function).
dd5d933f
JM
337 */
338 public function test_update_module() {
7bf4f6e9 339 // Add the module name you want to test here.
7cb0ea2c 340 // Create the match MODULENAME_update_set_values() and MODULENAME_update_run_asserts().
7bf4f6e9
JM
341 $modules = array('forum');
342 // Run all tests.
343 foreach ($modules as $modulename) {
344 $this->update_specific_module_test($modulename);
345 }
346 }
347
348 /**
349 * Set forum specific test values for calling update_module().
350 *
351 * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
352 */
353 private function forum_update_set_values(&$moduleinfo) {
354 // Completion specific to forum - optional.
355 $moduleinfo->completionposts = 3;
356 $moduleinfo->completiondiscussions = 1;
357 $moduleinfo->completionreplies = 2;
358
7cb0ea2c 359 // Specific values to the Forum module.
7bf4f6e9
JM
360 $moduleinfo->forcesubscribe = FORUM_INITIALSUBSCRIBE;
361 $moduleinfo->type = 'single';
362 $moduleinfo->trackingtype = FORUM_TRACKING_ON;
363 $moduleinfo->maxbytes = 10240;
364 $moduleinfo->maxattachments = 2;
365
7cb0ea2c 366 // Post threshold for blocking - specific to forum.
7bf4f6e9
JM
367 $moduleinfo->blockperiod = 60*60*24;
368 $moduleinfo->blockafter = 10;
369 $moduleinfo->warnafter = 5;
370 }
371
372 /**
373 * Execute test asserts on the saved DB data by update_module($forum).
374 *
375 * @param object $moduleinfo - the specific forum values that were used to update a forum.
376 * @param object $dbmodinstance - the DB values of the updated forum.
377 */
378 private function forum_update_run_asserts($moduleinfo, $dbmodinstance) {
379 // Compare values specific to forums.
380 $this->assertEquals($moduleinfo->forcesubscribe, $dbmodinstance->forcesubscribe);
381 $this->assertEquals($moduleinfo->type, $dbmodinstance->type);
382 $this->assertEquals($moduleinfo->assessed, $dbmodinstance->assessed);
383 $this->assertEquals($moduleinfo->completionposts, $dbmodinstance->completionposts);
384 $this->assertEquals($moduleinfo->completiondiscussions, $dbmodinstance->completiondiscussions);
385 $this->assertEquals($moduleinfo->completionreplies, $dbmodinstance->completionreplies);
386 $this->assertEquals($moduleinfo->scale, $dbmodinstance->scale);
387 $this->assertEquals($moduleinfo->assesstimestart, $dbmodinstance->assesstimestart);
388 $this->assertEquals($moduleinfo->assesstimefinish, $dbmodinstance->assesstimefinish);
389 $this->assertEquals($moduleinfo->rsstype, $dbmodinstance->rsstype);
390 $this->assertEquals($moduleinfo->rssarticles, $dbmodinstance->rssarticles);
391 $this->assertEquals($moduleinfo->trackingtype, $dbmodinstance->trackingtype);
392 $this->assertEquals($moduleinfo->maxbytes, $dbmodinstance->maxbytes);
393 $this->assertEquals($moduleinfo->maxattachments, $dbmodinstance->maxattachments);
394 $this->assertEquals($moduleinfo->blockperiod, $dbmodinstance->blockperiod);
395 $this->assertEquals($moduleinfo->blockafter, $dbmodinstance->blockafter);
396 $this->assertEquals($moduleinfo->warnafter, $dbmodinstance->warnafter);
397 }
398
399
400
401 /**
402 * Test a specific type of module.
403 *
404 * @param string $modulename - the module name to test
405 */
406 private function update_specific_module_test($modulename) {
dd5d933f
JM
407 global $DB, $CFG;
408
409 $this->resetAfterTest(true);
410
411 $this->setAdminUser();
412
7bf4f6e9
JM
413 // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
414 require_once($CFG->dirroot.'/mod/'. $modulename .'/lib.php');
dd5d933f 415
7bf4f6e9
JM
416 // Enable avaibility.
417 // If not enabled all conditional fields will be ignored.
418 set_config('enableavailability', 1);
419
420 // Enable course completion.
421 // If not enabled all completion settings will be ignored.
422 set_config('enablecompletion', COMPLETION_ENABLED);
423
424 // Enable forum RSS feeds.
425 set_config('enablerssfeeds', 1);
426 set_config('forum_enablerssfeeds', 1);
427
428 $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED),
dd5d933f
JM
429 array('createsections'=>true));
430
431 $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
432
7cb0ea2c 433 // Create assign module instance for testing gradeitem.
7bf4f6e9
JM
434 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
435 $params['course'] = $course->id;
436 $instance = $generator->create_instance($params);
437 $assigncm = get_coursemodule_from_instance('assign', $instance->id);
dd5d933f 438
7cb0ea2c 439 // Create the test forum to update.
7bf4f6e9
JM
440 $initvalues = new stdClass();
441 $initvalues->introformat = FORMAT_HTML;
442 $initvalues->course = $course->id;
443 $forum = self::getDataGenerator()->create_module('forum', $initvalues);
dd5d933f 444
7cb0ea2c 445 // Retrieve course module.
7bf4f6e9 446 $cm = get_coursemodule_from_instance('forum', $forum->id);
dd5d933f 447
7bf4f6e9 448 // Module test values.
dd5d933f
JM
449 $moduleinfo = new stdClass();
450
7bf4f6e9 451 // Always mandatory generic values to any module.
dd5d933f 452 $moduleinfo->coursemodule = $cm->id;
7bf4f6e9
JM
453 $moduleinfo->modulename = $modulename;
454 $moduleinfo->course = $course->id;
455 $moduleinfo->groupingid = $grouping->id;
456 $moduleinfo->groupmembersonly = 0;
457 $moduleinfo->visible = true;
458
459 // Sometimes optional generic values for some modules.
460 $moduleinfo->name = 'My test module';
461 $moduleinfo->showdescription = 1; // standard boolean
462 require_once($CFG->libdir . '/gradelib.php');
463 $gradecats = grade_get_categories_menu($moduleinfo->course, false);
464 $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
465 $moduleinfo->gradecat = $gradecatid;
466 $moduleinfo->groupmode = VISIBLEGROUPS;
467 $moduleinfo->cmidnumber = 'idnumber_XXX';
468
469 // Completion common to all module.
470 $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC;
471 $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED;
7cb0ea2c 472 $moduleinfo->completiongradeitemnumber = 1;
7bf4f6e9 473 $moduleinfo->completionexpected = time() + (7 * 24 * 3600);
69a0e65c 474 $moduleinfo->completionunlocked = 1;
7bf4f6e9
JM
475
476 // Conditional activity.
477 $moduleinfo->availablefrom = time();
478 $moduleinfo->availableuntil = time() + (7 * 24 * 3600);
479 $moduleinfo->showavailability = CONDITION_STUDENTVIEW_SHOW;
480 $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself.
481 $moduleinfo->conditiongradegroup = array(array('conditiongradeitemid' => $coursegradeitem->id, 'conditiongrademin' => 10, 'conditiongrademax' => 80));
482 $moduleinfo->conditionfieldgroup = array(array('conditionfield' => 'email', 'conditionfieldoperator' => OP_CONTAINS, 'conditionfieldvalue' => '@'));
483 $moduleinfo->conditioncompletiongroup = array(array('conditionsourcecmid' => $assigncm->id, 'conditionrequiredcompletion' => COMPLETION_COMPLETE)); // "conditionsourcecmid == 0" => none
484
485 // Grading and Advanced grading.
486 require_once($CFG->dirroot . '/rating/lib.php');
487 $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE;
488 $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number.
489 $moduleinfo->assesstimestart = time();
490 $moduleinfo->assesstimefinish = time() + (7 * 24 * 3600);
491
7cb0ea2c 492 // RSS.
7bf4f6e9
JM
493 $moduleinfo->rsstype = 2;
494 $moduleinfo->rssarticles = 10;
495
496 // Optional intro editor (depends of module).
dd5d933f
JM
497 $draftid_editor = 0;
498 file_prepare_draft_area($draftid_editor, null, null, null, null);
7bf4f6e9 499 $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
dd5d933f 500
7bf4f6e9
JM
501 // Following is the advanced grading method area called 'submissions' for the 'assign' module.
502 if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
503 $moduleinfo->grade = 100;
504 }
505 // Plagiarism form values.
dd5d933f
JM
506 // No plagiarism plugin installed by default. Use this space to make your own test.
507
7bf4f6e9
JM
508 // Values specific to the module.
509 $modulesetvalues = $modulename.'_update_set_values';
510 $this->$modulesetvalues($moduleinfo);
511
512 // Create the module.
513 $result = update_module($moduleinfo);
514
515 // Retrieve the module info.
516 $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance));
517 $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance);
7cb0ea2c 518 // Retrieve the grade item.
7bf4f6e9
JM
519 $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course,
520 'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename));
521
522 // Compare the values common to all module instances.
523 $this->assertEquals($moduleinfo->modulename, $dbcm->modname);
524 $this->assertEquals($moduleinfo->course, $dbcm->course);
525 $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
526 $this->assertEquals($moduleinfo->groupmembersonly, $dbcm->groupmembersonly);
527 $this->assertEquals($moduleinfo->visible, $dbcm->visible);
528 $this->assertEquals($moduleinfo->completion, $dbcm->completion);
529 $this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
530 $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber);
531 $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected);
532 $this->assertEquals($moduleinfo->availablefrom, $dbcm->availablefrom);
533 $this->assertEquals($moduleinfo->availableuntil, $dbcm->availableuntil);
534 $this->assertEquals($moduleinfo->showavailability, $dbcm->showavailability);
535 $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription);
536 $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode);
537 $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber);
538 $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid);
539
540 // Optional grade testing.
541 if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
542 $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade);
543 }
544
545 // Some optional (but quite common) to some module.
546 $this->assertEquals($moduleinfo->name, $dbmodinstance->name);
547 $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro);
548 $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat);
549
550 // Common values when conditional activity is enabled.
551 foreach ($moduleinfo->conditionfieldgroup as $fieldgroup) {
552 $isfieldgroupsaved = $DB->count_records('course_modules_avail_fields', array('coursemoduleid' => $dbcm->id,
553 'userfield' => $fieldgroup['conditionfield'], 'operator' => $fieldgroup['conditionfieldoperator'],
554 'value' => $fieldgroup['conditionfieldvalue']));
555 $this->assertEquals(1, $isfieldgroupsaved);
556 }
557 foreach ($moduleinfo->conditiongradegroup as $gradegroup) {
558 $isgradegroupsaved = $DB->count_records('course_modules_availability', array('coursemoduleid' => $dbcm->id,
559 'grademin' => $gradegroup['conditiongrademin'], 'grademax' => $gradegroup['conditiongrademax'],
560 'gradeitemid' => $gradegroup['conditiongradeitemid']));
561 $this->assertEquals(1, $isgradegroupsaved);
562 }
563 foreach ($moduleinfo->conditioncompletiongroup as $completiongroup) {
564 $iscompletiongroupsaved = $DB->count_records('course_modules_availability', array('coursemoduleid' => $dbcm->id,
565 'sourcecmid' => $completiongroup['conditionsourcecmid'], 'requiredcompletion' => $completiongroup['conditionrequiredcompletion']));
566 $this->assertEquals(1, $iscompletiongroupsaved);
567 }
568
7cb0ea2c 569 // Test specific to the module.
7bf4f6e9
JM
570 $modulerunasserts = $modulename.'_update_run_asserts';
571 $this->$modulerunasserts($moduleinfo, $dbmodinstance);
572 }
dd5d933f
JM
573
574
f70bfb84
FM
575 public function test_create_course() {
576 global $DB;
577 $this->resetAfterTest(true);
578 $defaultcategory = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
579
580 $course = new stdClass();
581 $course->fullname = 'Apu loves Unit Təsts';
582 $course->shortname = 'Spread the lŭve';
583 $course->idnumber = '123';
584 $course->summary = 'Awesome!';
585 $course->summaryformat = FORMAT_PLAIN;
586 $course->format = 'topics';
587 $course->newsitems = 0;
588 $course->numsections = 5;
589 $course->category = $defaultcategory;
97d5e39c 590 $original = (array) $course;
f70bfb84
FM
591
592 $created = create_course($course);
593 $context = context_course::instance($created->id);
594
595 // Compare original and created.
f70bfb84
FM
596 $this->assertEquals($original, array_intersect_key((array) $created, $original));
597
598 // Ensure default section is created.
599 $sectioncreated = $DB->record_exists('course_sections', array('course' => $created->id, 'section' => 0));
600 $this->assertTrue($sectioncreated);
601
602 // Ensure blocks have been associated to the course.
603 $blockcount = $DB->count_records('block_instances', array('parentcontextid' => $context->id));
604 $this->assertGreaterThan(0, $blockcount);
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
627 // Ensure all 6 (0-5) sections were created and modinfo/sectioninfo cache works properly
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
354b214c
PS
642 public function test_reorder_sections() {
643 global $DB;
644 $this->resetAfterTest(true);
645
646 $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
647 $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
648 $oldsections = array();
649 $sections = array();
3a222db2 650 foreach ($DB->get_records('course_sections', array('course'=>$course->id), 'id') as $section) {
354b214c
PS
651 $oldsections[$section->section] = $section->id;
652 $sections[$section->id] = $section->section;
653 }
654 ksort($oldsections);
655
656 $neworder = reorder_sections($sections, 2, 4);
657 $neworder = array_keys($neworder);
658 $this->assertEquals($oldsections[0], $neworder[0]);
659 $this->assertEquals($oldsections[1], $neworder[1]);
660 $this->assertEquals($oldsections[2], $neworder[4]);
661 $this->assertEquals($oldsections[3], $neworder[2]);
662 $this->assertEquals($oldsections[4], $neworder[3]);
663 $this->assertEquals($oldsections[5], $neworder[5]);
664 $this->assertEquals($oldsections[6], $neworder[6]);
665
eb01aa2c
RT
666 $neworder = reorder_sections($sections, 4, 2);
667 $neworder = array_keys($neworder);
668 $this->assertEquals($oldsections[0], $neworder[0]);
669 $this->assertEquals($oldsections[1], $neworder[1]);
670 $this->assertEquals($oldsections[2], $neworder[3]);
671 $this->assertEquals($oldsections[3], $neworder[4]);
672 $this->assertEquals($oldsections[4], $neworder[2]);
673 $this->assertEquals($oldsections[5], $neworder[5]);
674 $this->assertEquals($oldsections[6], $neworder[6]);
675
354b214c
PS
676 $neworder = reorder_sections(1, 2, 4);
677 $this->assertFalse($neworder);
678 }
679
3d8fe482 680 public function test_move_section_down() {
354b214c
PS
681 global $DB;
682 $this->resetAfterTest(true);
683
684 $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
685 $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
686 $oldsections = array();
687 foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
688 $oldsections[$section->section] = $section->id;
689 }
690 ksort($oldsections);
691
3d8fe482 692 // Test move section down..
354b214c
PS
693 move_section_to($course, 2, 4);
694 $sections = array();
695 foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
696 $sections[$section->section] = $section->id;
697 }
698 ksort($sections);
699
700 $this->assertEquals($oldsections[0], $sections[0]);
701 $this->assertEquals($oldsections[1], $sections[1]);
702 $this->assertEquals($oldsections[2], $sections[4]);
703 $this->assertEquals($oldsections[3], $sections[2]);
704 $this->assertEquals($oldsections[4], $sections[3]);
705 $this->assertEquals($oldsections[5], $sections[5]);
706 $this->assertEquals($oldsections[6], $sections[6]);
707 }
708
3d8fe482
DP
709 public function test_move_section_up() {
710 global $DB;
711 $this->resetAfterTest(true);
712
713 $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
714 $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
715 $oldsections = array();
716 foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
717 $oldsections[$section->section] = $section->id;
718 }
719 ksort($oldsections);
720
721 // Test move section up..
722 move_section_to($course, 6, 4);
723 $sections = array();
724 foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
725 $sections[$section->section] = $section->id;
726 }
727 ksort($sections);
728
729 $this->assertEquals($oldsections[0], $sections[0]);
730 $this->assertEquals($oldsections[1], $sections[1]);
731 $this->assertEquals($oldsections[2], $sections[2]);
732 $this->assertEquals($oldsections[3], $sections[3]);
733 $this->assertEquals($oldsections[4], $sections[5]);
734 $this->assertEquals($oldsections[5], $sections[6]);
735 $this->assertEquals($oldsections[6], $sections[4]);
736 }
737
738 public function test_move_section_marker() {
739 global $DB;
740 $this->resetAfterTest(true);
741
742 $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
743 $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
744
745 // Set course marker to the section we are going to move..
746 course_set_marker($course->id, 2);
747 // Verify that the course marker is set correctly.
748 $course = $DB->get_record('course', array('id' => $course->id));
749 $this->assertEquals(2, $course->marker);
750
751 // Test move the marked section down..
752 move_section_to($course, 2, 4);
753
754 // Verify that the coruse marker has been moved along with the section..
755 $course = $DB->get_record('course', array('id' => $course->id));
756 $this->assertEquals(4, $course->marker);
757
758 // Test move the marked section up..
759 move_section_to($course, 4, 3);
760
761 // Verify that the course marker has been moved along with the section..
762 $course = $DB->get_record('course', array('id' => $course->id));
763 $this->assertEquals(3, $course->marker);
764
765 // Test moving a non-marked section above the marked section..
766 move_section_to($course, 4, 2);
767
768 // Verify that the course marker has been moved down to accomodate..
769 $course = $DB->get_record('course', array('id' => $course->id));
770 $this->assertEquals(4, $course->marker);
771
772 // Test moving a non-marked section below the marked section..
773 move_section_to($course, 3, 6);
774
775 // Verify that the course marker has been up to accomodate..
776 $course = $DB->get_record('course', array('id' => $course->id));
777 $this->assertEquals(3, $course->marker);
778 }
779
354b214c
PS
780 public function test_get_course_display_name_for_list() {
781 global $CFG;
782 $this->resetAfterTest(true);
783
784 $course = $this->getDataGenerator()->create_course(array('shortname' => 'FROG101', 'fullname' => 'Introduction to pond life'));
785
786 $CFG->courselistshortnames = 0;
787 $this->assertEquals('Introduction to pond life', get_course_display_name_for_list($course));
788
789 $CFG->courselistshortnames = 1;
790 $this->assertEquals('FROG101 Introduction to pond life', get_course_display_name_for_list($course));
791 }
b1a8aa73 792
384c3510 793 public function test_move_module_in_course() {
3f61e4cb
ARN
794 global $DB;
795
384c3510
MG
796 $this->resetAfterTest(true);
797 // Setup fixture
3f61e4cb 798 $course = $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections' => true));
384c3510
MG
799 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
800
384c3510
MG
801 $cms = get_fast_modinfo($course)->get_cms();
802 $cm = reset($cms);
803
3f61e4cb
ARN
804 $newsection = get_fast_modinfo($course)->get_section_info(3);
805 $oldsectionid = $cm->section;
806
807 // Perform the move
808 moveto_module($cm, $newsection);
384c3510 809
3f61e4cb
ARN
810 // reset of get_fast_modinfo is usually called the code calling moveto_module so call it here
811 get_fast_modinfo(0, 0, true);
812 $cms = get_fast_modinfo($course)->get_cms();
813 $cm = reset($cms);
384c3510 814
3f61e4cb 815 // Check that the cached modinfo contains the correct section info
384c3510
MG
816 $modinfo = get_fast_modinfo($course);
817 $this->assertTrue(empty($modinfo->sections[0]));
818 $this->assertFalse(empty($modinfo->sections[3]));
3f61e4cb
ARN
819
820 // Check that the old section's sequence no longer contains this ID
821 $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
822 $oldsequences = explode(',', $newsection->sequence);
823 $this->assertFalse(in_array($cm->id, $oldsequences));
824
825 // Check that the new section's sequence now contains this ID
826 $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
827 $newsequences = explode(',', $newsection->sequence);
828 $this->assertTrue(in_array($cm->id, $newsequences));
829
830 // Check that the section number has been changed in the cm
831 $this->assertEquals($newsection->id, $cm->section);
832
833
834 // Perform a second move as some issues were only seen on the second move
835 $newsection = get_fast_modinfo($course)->get_section_info(2);
836 $oldsectionid = $cm->section;
837 $result = moveto_module($cm, $newsection);
838 $this->assertTrue($result);
839
840 // reset of get_fast_modinfo is usually called the code calling moveto_module so call it here
841 get_fast_modinfo(0, 0, true);
842 $cms = get_fast_modinfo($course)->get_cms();
843 $cm = reset($cms);
844
845 // Check that the cached modinfo contains the correct section info
846 $modinfo = get_fast_modinfo($course);
847 $this->assertTrue(empty($modinfo->sections[0]));
848 $this->assertFalse(empty($modinfo->sections[2]));
849
850 // Check that the old section's sequence no longer contains this ID
851 $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
852 $oldsequences = explode(',', $newsection->sequence);
853 $this->assertFalse(in_array($cm->id, $oldsequences));
854
855 // Check that the new section's sequence now contains this ID
856 $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
857 $newsequences = explode(',', $newsection->sequence);
858 $this->assertTrue(in_array($cm->id, $newsequences));
384c3510 859 }
f7d6e650
FM
860
861 public function test_module_visibility() {
862 $this->setAdminUser();
863 $this->resetAfterTest(true);
864
865 // Create course and modules.
866 $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
867 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
868 $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(), 'course' => $course->id));
869 $modules = compact('forum', 'assign');
870
871 // Hiding the modules.
872 foreach ($modules as $mod) {
873 set_coursemodule_visible($mod->cmid, 0);
874 $this->check_module_visibility($mod, 0, 0);
875 }
876
877 // Showing the modules.
878 foreach ($modules as $mod) {
879 set_coursemodule_visible($mod->cmid, 1);
880 $this->check_module_visibility($mod, 1, 1);
881 }
882 }
883
884 public function test_section_visibility() {
885 $this->setAdminUser();
886 $this->resetAfterTest(true);
887
888 // Create course.
889 $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
890
891 // Testing an empty section.
892 $sectionnumber = 1;
893 set_section_visible($course->id, $sectionnumber, 0);
894 $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
895 $this->assertEquals($section_info->visible, 0);
896 set_section_visible($course->id, $sectionnumber, 1);
897 $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
898 $this->assertEquals($section_info->visible, 1);
899
900 // Testing a section with visible modules.
901 $sectionnumber = 2;
902 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
903 array('section' => $sectionnumber));
904 $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
905 'course' => $course->id), array('section' => $sectionnumber));
906 $modules = compact('forum', 'assign');
907 set_section_visible($course->id, $sectionnumber, 0);
908 $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
909 $this->assertEquals($section_info->visible, 0);
910 foreach ($modules as $mod) {
911 $this->check_module_visibility($mod, 0, 1);
912 }
913 set_section_visible($course->id, $sectionnumber, 1);
914 $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
915 $this->assertEquals($section_info->visible, 1);
916 foreach ($modules as $mod) {
917 $this->check_module_visibility($mod, 1, 1);
918 }
919
920 // Testing a section with hidden modules, which should stay hidden.
921 $sectionnumber = 3;
922 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
923 array('section' => $sectionnumber));
924 $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
925 'course' => $course->id), array('section' => $sectionnumber));
926 $modules = compact('forum', 'assign');
927 foreach ($modules as $mod) {
928 set_coursemodule_visible($mod->cmid, 0);
929 $this->check_module_visibility($mod, 0, 0);
930 }
931 set_section_visible($course->id, $sectionnumber, 0);
932 $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
933 $this->assertEquals($section_info->visible, 0);
934 foreach ($modules as $mod) {
935 $this->check_module_visibility($mod, 0, 0);
936 }
937 set_section_visible($course->id, $sectionnumber, 1);
938 $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
939 $this->assertEquals($section_info->visible, 1);
940 foreach ($modules as $mod) {
941 $this->check_module_visibility($mod, 0, 0);
942 }
943 }
944
945 /**
946 * Helper function to assert that a module has correctly been made visible, or hidden.
947 *
948 * @param stdClass $mod module information
949 * @param int $visibility the current state of the module
950 * @param int $visibleold the current state of the visibleold property
951 * @return void
952 */
953 public function check_module_visibility($mod, $visibility, $visibleold) {
954 global $DB;
955 $cm = get_fast_modinfo($mod->course)->get_cm($mod->cmid);
956 $this->assertEquals($visibility, $cm->visible);
957 $this->assertEquals($visibleold, $cm->visibleold);
958
959 // Check the module grade items.
960 $grade_items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $cm->modname,
961 'iteminstance' => $cm->instance, 'courseid' => $cm->course));
962 if ($grade_items) {
963 foreach ($grade_items as $grade_item) {
964 if ($visibility) {
965 $this->assertFalse($grade_item->is_hidden(), "$cm->modname grade_item not visible");
966 } else {
967 $this->assertTrue($grade_item->is_hidden(), "$cm->modname grade_item not hidden");
968 }
969 }
970 }
971
972 // Check the events visibility.
973 if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $cm->modname))) {
974 foreach ($events as $event) {
975 $calevent = new calendar_event($event);
976 $this->assertEquals($visibility, $calevent->visible, "$cm->modname calendar_event visibility");
977 }
978 }
979 }
980
b7d3bb0f
AG
981 public function test_course_page_type_list() {
982 global $DB;
983 $this->resetAfterTest(true);
984
985 // Create a category.
986 $category = new stdClass();
987 $category->name = 'Test Category';
988
989 $testcategory = $this->getDataGenerator()->create_category($category);
990
991 // Create a course.
992 $course = new stdClass();
993 $course->fullname = 'Apu loves Unit Təsts';
994 $course->shortname = 'Spread the lŭve';
995 $course->idnumber = '123';
996 $course->summary = 'Awesome!';
997 $course->summaryformat = FORMAT_PLAIN;
998 $course->format = 'topics';
999 $course->newsitems = 0;
1000 $course->numsections = 5;
1001 $course->category = $testcategory->id;
1002
1003 $testcourse = $this->getDataGenerator()->create_course($course);
1004
1005 // Create contexts.
1006 $coursecontext = context_course::instance($testcourse->id);
1007 $parentcontext = $coursecontext->get_parent_context(); // Not actually used.
1008 $pagetype = 'page-course-x'; // Not used either.
1009 $pagetypelist = course_page_type_list($pagetype, $parentcontext, $coursecontext);
1010
1011 // Page type lists for normal courses.
1012 $testpagetypelist1 = array();
1013 $testpagetypelist1['*'] = 'Any page';
1014 $testpagetypelist1['course-*'] = 'Any course page';
1015 $testpagetypelist1['course-view-*'] = 'Any type of course main page';
1016
1017 $this->assertEquals($testpagetypelist1, $pagetypelist);
1018
1019 // Get the context for the front page course.
1020 $sitecoursecontext = context_course::instance(SITEID);
1021 $pagetypelist = course_page_type_list($pagetype, $parentcontext, $sitecoursecontext);
1022
1023 // Page type list for the front page course.
1024 $testpagetypelist2 = array('*' => 'Any page');
1025 $this->assertEquals($testpagetypelist2, $pagetypelist);
1026
1027 // Make sure that providing no current context to the function doesn't result in an error.
1028 // Calls made from generate_page_type_patterns() may provide null values.
1029 $pagetypelist = course_page_type_list($pagetype, null, null);
1030 $this->assertEquals($pagetypelist, $testpagetypelist1);
1031 }
bf6b3c7a
AA
1032
1033 public function test_compare_activities_by_time_desc() {
1034
1035 // Let's create some test data.
1036 $activitiesivities = array();
1037 $x = new stdClass();
1038 $x->timestamp = null;
1039 $activities[] = $x;
1040
1041 $x = new stdClass();
1042 $x->timestamp = 1;
1043 $activities[] = $x;
1044
1045 $x = new stdClass();
1046 $x->timestamp = 3;
1047 $activities[] = $x;
1048
1049 $x = new stdClass();
1050 $x->timestamp = 0;
1051 $activities[] = $x;
1052
1053 $x = new stdClass();
1054 $x->timestamp = 5;
1055 $activities[] = $x;
1056
1057 $x = new stdClass();
1058 $activities[] = $x;
1059
1060 $x = new stdClass();
1061 $x->timestamp = 5;
1062 $activities[] = $x;
1063
1064 // Do the sorting.
1065 usort($activities, 'compare_activities_by_time_desc');
1066
1067 // Let's check the result.
1068 $last = 10;
1069 foreach($activities as $activity) {
1070 if (empty($activity->timestamp)) {
1071 $activity->timestamp = 0;
1072 }
1073 $this->assertLessThanOrEqual($last, $activity->timestamp);
1074 }
1075 }
1076
1077 public function test_compare_activities_by_time_asc() {
1078
1079 // Let's create some test data.
1080 $activities = array();
1081 $x = new stdClass();
1082 $x->timestamp = null;
1083 $activities[] = $x;
1084
1085 $x = new stdClass();
1086 $x->timestamp = 1;
1087 $activities[] = $x;
1088
1089 $x = new stdClass();
1090 $x->timestamp = 3;
1091 $activities[] = $x;
1092
1093 $x = new stdClass();
1094 $x->timestamp = 0;
1095 $activities[] = $x;
1096
1097 $x = new stdClass();
1098 $x->timestamp = 5;
1099 $activities[] = $x;
1100
1101 $x = new stdClass();
1102 $activities[] = $x;
1103
1104 $x = new stdClass();
1105 $x->timestamp = 5;
1106 $activities[] = $x;
1107
1108 // Do the sorting.
1109 usort($activities, 'compare_activities_by_time_asc');
1110
1111 // Let's check the result.
1112 $last = 0;
1113 foreach($activities as $activity) {
1114 if (empty($activity->timestamp)) {
1115 $activity->timestamp = 0;
1116 }
1117 $this->assertGreaterThanOrEqual($last, $activity->timestamp);
1118 }
1119 }
9ab0aece 1120
1fff1b8c
DP
1121 /**
1122 * Tests moving a module between hidden/visible sections and
1123 * verifies that the course/module visiblity seettings are
1124 * retained.
1125 */
1126 public function test_moveto_module_between_hidden_sections() {
1127 global $DB;
1128
1129 $this->resetAfterTest(true);
1130
1131 $course = $this->getDataGenerator()->create_course(array('numsections' => 4), array('createsections' => true));
1132 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1133 $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
1134 $quiz= $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
1135
1136 // Set the page as hidden
1137 set_coursemodule_visible($page->cmid, 0);
1138
1139 // Set sections 3 as hidden.
1140 set_section_visible($course->id, 3, 0);
1141
1142 $modinfo = get_fast_modinfo($course);
1143
1144 $hiddensection = $modinfo->get_section_info(3);
1145 // New section is definitely not visible:
1146 $this->assertEquals($hiddensection->visible, 0);
1147
1148 $forumcm = $modinfo->cms[$forum->cmid];
1149 $pagecm = $modinfo->cms[$page->cmid];
1150
1151 // Move the forum and the page to a hidden section.
1152 moveto_module($forumcm, $hiddensection);
1153 moveto_module($pagecm, $hiddensection);
1154
1155 // Reset modinfo cache.
1156 get_fast_modinfo(0, 0, true);
1157
1158 $modinfo = get_fast_modinfo($course);
1159
1160 // Verify that forum and page have been moved to the hidden section and quiz has not.
1161 $this->assertContains($forum->cmid, $modinfo->sections[3]);
1162 $this->assertContains($page->cmid, $modinfo->sections[3]);
1163 $this->assertNotContains($quiz->cmid, $modinfo->sections[3]);
1164
1165 // Verify that forum has been made invisible.
1166 $forumcm = $modinfo->cms[$forum->cmid];
1167 $this->assertEquals($forumcm->visible, 0);
1168 // Verify that old state has been retained.
1169 $this->assertEquals($forumcm->visibleold, 1);
1170
1171 // Verify that page has stayed invisible.
1172 $pagecm = $modinfo->cms[$page->cmid];
1173 $this->assertEquals($pagecm->visible, 0);
1174 // Verify that old state has been retained.
1175 $this->assertEquals($pagecm->visibleold, 0);
1176
1177 // Verify that quiz has been unaffected.
1178 $quizcm = $modinfo->cms[$quiz->cmid];
1179 $this->assertEquals($quizcm->visible, 1);
1180
1181 // Move forum and page back to visible section.
1182 $visiblesection = $modinfo->get_section_info(2);
1183 moveto_module($forumcm, $visiblesection);
1184 moveto_module($pagecm, $visiblesection);
1185
1186 // Reset modinfo cache.
1187 get_fast_modinfo(0, 0, true);
1188 $modinfo = get_fast_modinfo($course);
1189
1190 // Verify that forum has been made visible.
1191 $forumcm = $modinfo->cms[$forum->cmid];
1192 $this->assertEquals($forumcm->visible, 1);
1193
1194 // Verify that page has stayed invisible.
1195 $pagecm = $modinfo->cms[$page->cmid];
1196 $this->assertEquals($pagecm->visible, 0);
1197
1198 // Move the page in the same section (this is what mod duplicate does_
1199 moveto_module($pagecm, $visiblesection, $forumcm);
1200
1201 // Reset modinfo cache.
1202 get_fast_modinfo(0, 0, true);
1203
1204 // Verify that the the page is still hidden
1205 $modinfo = get_fast_modinfo($course);
1206 $pagecm = $modinfo->cms[$page->cmid];
1207 $this->assertEquals($pagecm->visible, 0);
1208 }
1209
1210 /**
1211 * Tests moving a module around in the same section. moveto_module()
1212 * is called this way in modduplicate.
1213 */
1214 public function test_moveto_module_in_same_section() {
1215 global $DB;
1216
1217 $this->resetAfterTest(true);
1218
1219 $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
1220 $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
1221 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1222
1223 // Simulate inconsistent visible/visibleold values (MDL-38713).
1224 $cm = $DB->get_record('course_modules', array('id' => $page->cmid), '*', MUST_EXIST);
1225 $cm->visible = 0;
1226 $cm->visibleold = 1;
1227 $DB->update_record('course_modules', $cm);
1228
1229 $modinfo = get_fast_modinfo($course);
1230 $forumcm = $modinfo->cms[$forum->cmid];
1231 $pagecm = $modinfo->cms[$page->cmid];
1232
1233 // Verify that page is hidden.
1234 $this->assertEquals($pagecm->visible, 0);
1235
1236 // Verify section 0 is where all mods added.
1237 $section = $modinfo->get_section_info(0);
1238 $this->assertEquals($section->id, $forumcm->section);
1239 $this->assertEquals($section->id, $pagecm->section);
1240
1241
1242 // Move the forum and the page to a hidden section.
1243 moveto_module($pagecm, $section, $forumcm);
1244
1245 // Reset modinfo cache.
1246 get_fast_modinfo(0, 0, true);
1247
1248 // Verify that the the page is still hidden
1249 $modinfo = get_fast_modinfo($course);
1250 $pagecm = $modinfo->cms[$page->cmid];
1251 $this->assertEquals($pagecm->visible, 0);
1252 }
354b214c 1253}