MDL-37079 create/update_module()
[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
JM
33 /**
34 * Test create_module()
35 */
36 public function test_create_module() {
37 global $DB, $CFG;
38
39 $this->resetAfterTest(true);
40
41 $this->setAdminUser();
42
43 require_once($CFG->dirroot.'/mod/forum/lib.php');
44
45 $course = $this->getDataGenerator()->create_course(array('numsections'=>1),
46 array('createsections'=>true));
47
48 $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
49
50 // FORUM MODULE
51
52 $moduleinfo = new stdClass();
53
54 // Always mandatory generic values to any module
55 $moduleinfo->modulename = 'forum';
56 $moduleinfo->section= 1;
57 $moduleinfo->course= $course->id;
58 $moduleinfo->groupingid= $grouping->id;
59 $moduleinfo->groupmembersonly= '';
60 $moduleinfo->visible= true;
61
62 // Sometimes optional generic values for some modules
63 $moduleinfo->name= 'My test forum';
64 $moduleinfo->completion= '';
65 $moduleinfo->completionview= '';
66 $moduleinfo->completiongradeitemonly= '';
67 $moduleinfo->completionexpected= false;
68 $moduleinfo->availablefrom= 0;
69 $moduleinfo->availableuntil= 0;
70 $moduleinfo->showavailability= false;
71 $moduleinfo->showdescription= true;
72 $moduleinfo->gradecat= '';
73 $moduleinfo->groupmod = VISIBLEGROUPS;
74 $moduleinfo->cmidnumber= 'idnumber_XXX';
75
76 // Optional intro editor (depends of module)
77 $draftid_editor = 0;
78 file_prepare_draft_area($draftid_editor, null, null, null, null);
79 $moduleinfo->introeditor = array('text'=>'This is a forum', 'format'=>FORMAT_HTML, 'itemid'=>$draftid_editor);
80
81 // Specific values to the Forum module
82 $moduleinfo->forcesubscribe = FORUM_INITIALSUBSCRIBE;
83 $moduleinfo->type = 'single';
84
85 // create the module
86 $forum = create_module($moduleinfo);
87
88 // Retriev the module
89
90 // Compare the module values
91
92 // error_log(print_r($forum, true));
93
94 // ASSIGN MODULE
95
96 $moduleinfo = new stdClass();
97
98 // Always mandatory generic values to any module
99 $moduleinfo->modulename = 'assign';
100 $moduleinfo->section= 1;
101 $moduleinfo->course= $course->id;
102 $moduleinfo->groupingid= $grouping->id;
103 $moduleinfo->groupmembersonly= '';
104 $moduleinfo->visible= true;
105
106 // Sometimes optional generic values for some modules
107 $moduleinfo->name= 'My test assignment';
108 $moduleinfo->completion= '';
109 $moduleinfo->completionview= '';
110 $moduleinfo->completiongradeitemonly= '';
111 $moduleinfo->completionexpected= false;
112 $moduleinfo->availablefrom= 0;
113 $moduleinfo->availableuntil= 0;
114 $moduleinfo->showavailability= false;
115 $moduleinfo->showdescription= true;
116 $moduleinfo->gradecat= 1;
117 $moduleinfo->groupmod = VISIBLEGROUPS;
118 $moduleinfo->cmidnumber= 'idnumber_XXXY';
119
120 // Optional intro editor (depends of module)
121 // TODO: way to support file - see Damyon assignment solution
122 $draftid_editor = 0;
123 file_prepare_draft_area($draftid_editor, null, null, null, null);
124 $moduleinfo->introeditor = array('text'=>'This is a assignmenet', 'format'=>FORMAT_HTML, 'itemid'=>$draftid_editor);
125
126 // Specific values to the Assign module
127 $moduleinfo->alwaysshowdescription = true;
128 $moduleinfo->submissiondrafts = true;
129 $moduleinfo->requiresubmissionstatement = true;
130 $moduleinfo->sendnotifications = true;
131 $moduleinfo->sendlatenotifications = true;
132 $moduleinfo->duedate = time()+7*24*3600;
133 $moduleinfo->cutoffdate = time()+7*24*3600;
134 $moduleinfo->allowsubmissionsfromdate = '';
135 $moduleinfo->teamsubmission = true;
136 $moduleinfo->requireallteammemberssubmit = true;
137 $moduleinfo->teamsubmissiongroupingid = true;
138 $moduleinfo->blindmarking = true;
139 $moduleinfo->assignsubmission_onlinetext_enabled = true;
140 $moduleinfo->assignsubmission_file_enabled = true;
141 $moduleinfo->assignsubmission_file_maxfiles = 1;
142 $moduleinfo->assignsubmission_file_maxsizebytes = 1000000;
143 $moduleinfo->assignsubmission_comments_enabled = true;
144 $moduleinfo->assignfeedback_comments_enabled = true;
145 $moduleinfo->assignfeedback_offline_enabled = true;
146 $moduleinfo->assignfeedback_file_enabled = true;
147
148 // Following is the advanced grading method area called 'submissions' for the 'assign' module :
149 $moduleinfo->grade = 100;
150 $moduleinfo->advancedgradingmethod_submissions = '';
151
152 // Plagiarism form values
153 // No plagiarism plugin installed by default. Use this space to make your own test.
154
155 // create the module
156 $assign = create_module($moduleinfo);
157
158 // Retrieve the module
159
160 // Compare the module values
161
162 // error_log(print_r($assign, true));
163
164 }
165
166 /**
167 * Test update_module()
168 */
169 public function test_update_module() {
170 global $DB, $CFG;
171
172 $this->resetAfterTest(true);
173
174 $this->setAdminUser();
175
176 require_once($CFG->dirroot.'/mod/forum/lib.php');
177
178 $course = $this->getDataGenerator()->create_course(array('numsections'=>2),
179 array('createsections'=>true));
180
181 $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
182
183
184 // UPDATE ASSIGN MODULE
185 $assigninfo = array('course' => $course->id);
186 $sectionnumber = 1;
187 $assign = $this->getDataGenerator()->create_module('assign', $assigninfo, array('section' => $sectionnumber));
188
189 // Retrieve course modulei
190 $cm = get_coursemodule_from_instance('assign', $assign->id);
191
192 $moduleinfo = new stdClass();
193
194 // Always mandatory generic values to any module
195 $moduleinfo->coursemodule = $cm->id;
196 $moduleinfo->modulename = 'assign';
197 $moduleinfo->section= 1;
198 $moduleinfo->course= $course->id;
199 $moduleinfo->groupingid= $grouping->id;
200 $moduleinfo->groupmembersonly= '';
201 $moduleinfo->visible= true;
202
203 // Sometimes optional generic values for some modules
204 $moduleinfo->name= 'My test assignment';
205 $moduleinfo->completion= '';
206 $moduleinfo->completionview= '';
207 $moduleinfo->completiongradeitemonly= '';
208 $moduleinfo->completionexpected= false;
209 $moduleinfo->availablefrom= 0;
210 $moduleinfo->availableuntil= 0;
211 $moduleinfo->showavailability= false;
212 $moduleinfo->showdescription= true;
213 $moduleinfo->gradecat= 1;
214 $moduleinfo->groupmod = VISIBLEGROUPS;
215 $moduleinfo->cmidnumber= 'idnumber_XXXY';
216
217 // Optional intro editor (depends of module)
218 // TODO: way to support file - see Damyon assignment solution
219 $draftid_editor = 0;
220 file_prepare_draft_area($draftid_editor, null, null, null, null);
221 $moduleinfo->introeditor = array('text'=>'This is a assignmenet', 'format'=>FORMAT_HTML, 'itemid'=>$draftid_editor);
222
223 // Specific values to the Assign module
224 $moduleinfo->alwaysshowdescription = true;
225 $moduleinfo->submissiondrafts = true;
226 $moduleinfo->requiresubmissionstatement = true;
227 $moduleinfo->sendnotifications = true;
228 $moduleinfo->sendlatenotifications = true;
229 $moduleinfo->duedate = time()+7*24*3600;
230 $moduleinfo->cutoffdate = time()+7*24*3600;
231 $moduleinfo->allowsubmissionsfromdate = '';
232 $moduleinfo->teamsubmission = true;
233 $moduleinfo->requireallteammemberssubmit = true;
234 $moduleinfo->teamsubmissiongroupingid = true;
235 $moduleinfo->blindmarking = true;
236 $moduleinfo->assignsubmission_onlinetext_enabled = true;
237 $moduleinfo->assignsubmission_file_enabled = true;
238 $moduleinfo->assignsubmission_file_maxfiles = 1;
239 $moduleinfo->assignsubmission_file_maxsizebytes = 1000000;
240 $moduleinfo->assignsubmission_comments_enabled = true;
241 $moduleinfo->assignfeedback_comments_enabled = true;
242 $moduleinfo->assignfeedback_offline_enabled = true;
243 $moduleinfo->assignfeedback_file_enabled = true;
244
245 // Following is the advanced grading method area called 'submissions' for the 'assign' module :
246 $moduleinfo->grade = 100;
247 $moduleinfo->advancedgradingmethod_submissions = '';
248
249 // Plagiarism form values
250 // No plagiarism plugin installed by default. Use this space to make your own test.
251
252 // update the module
253 $assign = update_module($moduleinfo);
254
255 // Retrieve the module
256
257 // Compare the module values
258
259 // error_log(print_r($assign, true));
260 }
261
262
f70bfb84
FM
263 public function test_create_course() {
264 global $DB;
265 $this->resetAfterTest(true);
266 $defaultcategory = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
267
268 $course = new stdClass();
269 $course->fullname = 'Apu loves Unit Təsts';
270 $course->shortname = 'Spread the lŭve';
271 $course->idnumber = '123';
272 $course->summary = 'Awesome!';
273 $course->summaryformat = FORMAT_PLAIN;
274 $course->format = 'topics';
275 $course->newsitems = 0;
276 $course->numsections = 5;
277 $course->category = $defaultcategory;
278
279 $created = create_course($course);
280 $context = context_course::instance($created->id);
281
282 // Compare original and created.
283 $original = (array) $course;
284 $this->assertEquals($original, array_intersect_key((array) $created, $original));
285
286 // Ensure default section is created.
287 $sectioncreated = $DB->record_exists('course_sections', array('course' => $created->id, 'section' => 0));
288 $this->assertTrue($sectioncreated);
289
290 // Ensure blocks have been associated to the course.
291 $blockcount = $DB->count_records('block_instances', array('parentcontextid' => $context->id));
292 $this->assertGreaterThan(0, $blockcount);
293 }
294
384c3510
MG
295 public function test_create_course_with_generator() {
296 global $DB;
297 $this->resetAfterTest(true);
298 $course = $this->getDataGenerator()->create_course();
299
300 // Ensure default section is created.
301 $sectioncreated = $DB->record_exists('course_sections', array('course' => $course->id, 'section' => 0));
302 $this->assertTrue($sectioncreated);
303 }
304
305 public function test_create_course_sections() {
306 global $DB;
307 $this->resetAfterTest(true);
308
309 $course = $this->getDataGenerator()->create_course(
310 array('shortname' => 'GrowingCourse',
311 'fullname' => 'Growing Course',
312 'numsections' => 5),
313 array('createsections' => true));
314
315 // Ensure all 6 (0-5) sections were created and modinfo/sectioninfo cache works properly
316 $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
317 $this->assertEquals(range(0, $course->numsections), $sectionscreated);
318
319 // this will do nothing, section already exists
320 $this->assertFalse(course_create_sections_if_missing($course, $course->numsections));
321
322 // this will create new section
323 $this->assertTrue(course_create_sections_if_missing($course, $course->numsections + 1));
324
325 // Ensure all 7 (0-6) sections were created and modinfo/sectioninfo cache works properly
326 $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
327 $this->assertEquals(range(0, $course->numsections + 1), $sectionscreated);
328 }
329
354b214c
PS
330 public function test_reorder_sections() {
331 global $DB;
332 $this->resetAfterTest(true);
333
334 $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
335 $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
336 $oldsections = array();
337 $sections = array();
3a222db2 338 foreach ($DB->get_records('course_sections', array('course'=>$course->id), 'id') as $section) {
354b214c
PS
339 $oldsections[$section->section] = $section->id;
340 $sections[$section->id] = $section->section;
341 }
342 ksort($oldsections);
343
344 $neworder = reorder_sections($sections, 2, 4);
345 $neworder = array_keys($neworder);
346 $this->assertEquals($oldsections[0], $neworder[0]);
347 $this->assertEquals($oldsections[1], $neworder[1]);
348 $this->assertEquals($oldsections[2], $neworder[4]);
349 $this->assertEquals($oldsections[3], $neworder[2]);
350 $this->assertEquals($oldsections[4], $neworder[3]);
351 $this->assertEquals($oldsections[5], $neworder[5]);
352 $this->assertEquals($oldsections[6], $neworder[6]);
353
eb01aa2c
RT
354 $neworder = reorder_sections($sections, 4, 2);
355 $neworder = array_keys($neworder);
356 $this->assertEquals($oldsections[0], $neworder[0]);
357 $this->assertEquals($oldsections[1], $neworder[1]);
358 $this->assertEquals($oldsections[2], $neworder[3]);
359 $this->assertEquals($oldsections[3], $neworder[4]);
360 $this->assertEquals($oldsections[4], $neworder[2]);
361 $this->assertEquals($oldsections[5], $neworder[5]);
362 $this->assertEquals($oldsections[6], $neworder[6]);
363
354b214c
PS
364 $neworder = reorder_sections(1, 2, 4);
365 $this->assertFalse($neworder);
366 }
367
3d8fe482 368 public function test_move_section_down() {
354b214c
PS
369 global $DB;
370 $this->resetAfterTest(true);
371
372 $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
373 $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
374 $oldsections = array();
375 foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
376 $oldsections[$section->section] = $section->id;
377 }
378 ksort($oldsections);
379
3d8fe482 380 // Test move section down..
354b214c
PS
381 move_section_to($course, 2, 4);
382 $sections = array();
383 foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
384 $sections[$section->section] = $section->id;
385 }
386 ksort($sections);
387
388 $this->assertEquals($oldsections[0], $sections[0]);
389 $this->assertEquals($oldsections[1], $sections[1]);
390 $this->assertEquals($oldsections[2], $sections[4]);
391 $this->assertEquals($oldsections[3], $sections[2]);
392 $this->assertEquals($oldsections[4], $sections[3]);
393 $this->assertEquals($oldsections[5], $sections[5]);
394 $this->assertEquals($oldsections[6], $sections[6]);
395 }
396
3d8fe482
DP
397 public function test_move_section_up() {
398 global $DB;
399 $this->resetAfterTest(true);
400
401 $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
402 $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
403 $oldsections = array();
404 foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
405 $oldsections[$section->section] = $section->id;
406 }
407 ksort($oldsections);
408
409 // Test move section up..
410 move_section_to($course, 6, 4);
411 $sections = array();
412 foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
413 $sections[$section->section] = $section->id;
414 }
415 ksort($sections);
416
417 $this->assertEquals($oldsections[0], $sections[0]);
418 $this->assertEquals($oldsections[1], $sections[1]);
419 $this->assertEquals($oldsections[2], $sections[2]);
420 $this->assertEquals($oldsections[3], $sections[3]);
421 $this->assertEquals($oldsections[4], $sections[5]);
422 $this->assertEquals($oldsections[5], $sections[6]);
423 $this->assertEquals($oldsections[6], $sections[4]);
424 }
425
426 public function test_move_section_marker() {
427 global $DB;
428 $this->resetAfterTest(true);
429
430 $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
431 $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
432
433 // Set course marker to the section we are going to move..
434 course_set_marker($course->id, 2);
435 // Verify that the course marker is set correctly.
436 $course = $DB->get_record('course', array('id' => $course->id));
437 $this->assertEquals(2, $course->marker);
438
439 // Test move the marked section down..
440 move_section_to($course, 2, 4);
441
442 // Verify that the coruse marker has been moved along with the section..
443 $course = $DB->get_record('course', array('id' => $course->id));
444 $this->assertEquals(4, $course->marker);
445
446 // Test move the marked section up..
447 move_section_to($course, 4, 3);
448
449 // Verify that the course marker has been moved along with the section..
450 $course = $DB->get_record('course', array('id' => $course->id));
451 $this->assertEquals(3, $course->marker);
452
453 // Test moving a non-marked section above the marked section..
454 move_section_to($course, 4, 2);
455
456 // Verify that the course marker has been moved down to accomodate..
457 $course = $DB->get_record('course', array('id' => $course->id));
458 $this->assertEquals(4, $course->marker);
459
460 // Test moving a non-marked section below the marked section..
461 move_section_to($course, 3, 6);
462
463 // Verify that the course marker has been up to accomodate..
464 $course = $DB->get_record('course', array('id' => $course->id));
465 $this->assertEquals(3, $course->marker);
466 }
467
354b214c
PS
468 public function test_get_course_display_name_for_list() {
469 global $CFG;
470 $this->resetAfterTest(true);
471
472 $course = $this->getDataGenerator()->create_course(array('shortname' => 'FROG101', 'fullname' => 'Introduction to pond life'));
473
474 $CFG->courselistshortnames = 0;
475 $this->assertEquals('Introduction to pond life', get_course_display_name_for_list($course));
476
477 $CFG->courselistshortnames = 1;
478 $this->assertEquals('FROG101 Introduction to pond life', get_course_display_name_for_list($course));
479 }
b1a8aa73
ARN
480
481 public function test_create_course_category() {
482 global $CFG, $DB;
483 $this->resetAfterTest(true);
484
485 // Create the category
486 $data = new stdClass();
487 $data->name = 'aaa';
488 $data->description = 'aaa';
489 $data->idnumber = '';
490
491 $category1 = create_course_category($data);
492
493 // Initially confirm that base data was inserted correctly
494 $this->assertEquals($data->name, $category1->name);
495 $this->assertEquals($data->description, $category1->description);
496 $this->assertEquals($data->idnumber, $category1->idnumber);
497
498 // sortorder should be blank initially
499 $this->assertEmpty($category1->sortorder);
500
501 // Calling fix_course_sortorder() should provide a new sortorder
502 fix_course_sortorder();
503 $category1 = $DB->get_record('course_categories', array('id' => $category1->id));
504
505 $this->assertGreaterThanOrEqual(1, $category1->sortorder);
506
507 // Create two more categories and test the sortorder worked correctly
508 $data->name = 'ccc';
509 $category2 = create_course_category($data);
510 $this->assertEmpty($category2->sortorder);
511
512 $data->name = 'bbb';
513 $category3 = create_course_category($data);
514 $this->assertEmpty($category3->sortorder);
515
516 // Calling fix_course_sortorder() should provide a new sortorder to give category1,
517 // category2, category3. New course categories are ordered by id not name
518 fix_course_sortorder();
519
520 $category1 = $DB->get_record('course_categories', array('id' => $category1->id));
521 $category2 = $DB->get_record('course_categories', array('id' => $category2->id));
522 $category3 = $DB->get_record('course_categories', array('id' => $category3->id));
523
524 $this->assertGreaterThanOrEqual($category1->sortorder, $category2->sortorder);
525 $this->assertGreaterThanOrEqual($category2->sortorder, $category3->sortorder);
526 $this->assertGreaterThanOrEqual($category1->sortorder, $category3->sortorder);
527 }
384c3510
MG
528
529 public function test_move_module_in_course() {
3f61e4cb
ARN
530 global $DB;
531
384c3510
MG
532 $this->resetAfterTest(true);
533 // Setup fixture
3f61e4cb 534 $course = $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections' => true));
384c3510
MG
535 $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
536
384c3510
MG
537 $cms = get_fast_modinfo($course)->get_cms();
538 $cm = reset($cms);
539
3f61e4cb
ARN
540 $newsection = get_fast_modinfo($course)->get_section_info(3);
541 $oldsectionid = $cm->section;
542
543 // Perform the move
544 moveto_module($cm, $newsection);
384c3510 545
3f61e4cb
ARN
546 // reset of get_fast_modinfo is usually called the code calling moveto_module so call it here
547 get_fast_modinfo(0, 0, true);
548 $cms = get_fast_modinfo($course)->get_cms();
549 $cm = reset($cms);
384c3510 550
3f61e4cb 551 // Check that the cached modinfo contains the correct section info
384c3510
MG
552 $modinfo = get_fast_modinfo($course);
553 $this->assertTrue(empty($modinfo->sections[0]));
554 $this->assertFalse(empty($modinfo->sections[3]));
3f61e4cb
ARN
555
556 // Check that the old section's sequence no longer contains this ID
557 $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
558 $oldsequences = explode(',', $newsection->sequence);
559 $this->assertFalse(in_array($cm->id, $oldsequences));
560
561 // Check that the new section's sequence now contains this ID
562 $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
563 $newsequences = explode(',', $newsection->sequence);
564 $this->assertTrue(in_array($cm->id, $newsequences));
565
566 // Check that the section number has been changed in the cm
567 $this->assertEquals($newsection->id, $cm->section);
568
569
570 // Perform a second move as some issues were only seen on the second move
571 $newsection = get_fast_modinfo($course)->get_section_info(2);
572 $oldsectionid = $cm->section;
573 $result = moveto_module($cm, $newsection);
574 $this->assertTrue($result);
575
576 // reset of get_fast_modinfo is usually called the code calling moveto_module so call it here
577 get_fast_modinfo(0, 0, true);
578 $cms = get_fast_modinfo($course)->get_cms();
579 $cm = reset($cms);
580
581 // Check that the cached modinfo contains the correct section info
582 $modinfo = get_fast_modinfo($course);
583 $this->assertTrue(empty($modinfo->sections[0]));
584 $this->assertFalse(empty($modinfo->sections[2]));
585
586 // Check that the old section's sequence no longer contains this ID
587 $oldsection = $DB->get_record('course_sections', array('id' => $oldsectionid));
588 $oldsequences = explode(',', $newsection->sequence);
589 $this->assertFalse(in_array($cm->id, $oldsequences));
590
591 // Check that the new section's sequence now contains this ID
592 $newsection = $DB->get_record('course_sections', array('id' => $newsection->id));
593 $newsequences = explode(',', $newsection->sequence);
594 $this->assertTrue(in_array($cm->id, $newsequences));
384c3510 595 }
f7d6e650
FM
596
597 public function test_module_visibility() {
598 $this->setAdminUser();
599 $this->resetAfterTest(true);
600
601 // Create course and modules.
602 $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
603 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
604 $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(), 'course' => $course->id));
605 $modules = compact('forum', 'assign');
606
607 // Hiding the modules.
608 foreach ($modules as $mod) {
609 set_coursemodule_visible($mod->cmid, 0);
610 $this->check_module_visibility($mod, 0, 0);
611 }
612
613 // Showing the modules.
614 foreach ($modules as $mod) {
615 set_coursemodule_visible($mod->cmid, 1);
616 $this->check_module_visibility($mod, 1, 1);
617 }
618 }
619
620 public function test_section_visibility() {
621 $this->setAdminUser();
622 $this->resetAfterTest(true);
623
624 // Create course.
625 $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
626
627 // Testing an empty section.
628 $sectionnumber = 1;
629 set_section_visible($course->id, $sectionnumber, 0);
630 $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
631 $this->assertEquals($section_info->visible, 0);
632 set_section_visible($course->id, $sectionnumber, 1);
633 $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
634 $this->assertEquals($section_info->visible, 1);
635
636 // Testing a section with visible modules.
637 $sectionnumber = 2;
638 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
639 array('section' => $sectionnumber));
640 $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
641 'course' => $course->id), array('section' => $sectionnumber));
642 $modules = compact('forum', 'assign');
643 set_section_visible($course->id, $sectionnumber, 0);
644 $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
645 $this->assertEquals($section_info->visible, 0);
646 foreach ($modules as $mod) {
647 $this->check_module_visibility($mod, 0, 1);
648 }
649 set_section_visible($course->id, $sectionnumber, 1);
650 $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
651 $this->assertEquals($section_info->visible, 1);
652 foreach ($modules as $mod) {
653 $this->check_module_visibility($mod, 1, 1);
654 }
655
656 // Testing a section with hidden modules, which should stay hidden.
657 $sectionnumber = 3;
658 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
659 array('section' => $sectionnumber));
660 $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
661 'course' => $course->id), array('section' => $sectionnumber));
662 $modules = compact('forum', 'assign');
663 foreach ($modules as $mod) {
664 set_coursemodule_visible($mod->cmid, 0);
665 $this->check_module_visibility($mod, 0, 0);
666 }
667 set_section_visible($course->id, $sectionnumber, 0);
668 $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
669 $this->assertEquals($section_info->visible, 0);
670 foreach ($modules as $mod) {
671 $this->check_module_visibility($mod, 0, 0);
672 }
673 set_section_visible($course->id, $sectionnumber, 1);
674 $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
675 $this->assertEquals($section_info->visible, 1);
676 foreach ($modules as $mod) {
677 $this->check_module_visibility($mod, 0, 0);
678 }
679 }
680
681 /**
682 * Helper function to assert that a module has correctly been made visible, or hidden.
683 *
684 * @param stdClass $mod module information
685 * @param int $visibility the current state of the module
686 * @param int $visibleold the current state of the visibleold property
687 * @return void
688 */
689 public function check_module_visibility($mod, $visibility, $visibleold) {
690 global $DB;
691 $cm = get_fast_modinfo($mod->course)->get_cm($mod->cmid);
692 $this->assertEquals($visibility, $cm->visible);
693 $this->assertEquals($visibleold, $cm->visibleold);
694
695 // Check the module grade items.
696 $grade_items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $cm->modname,
697 'iteminstance' => $cm->instance, 'courseid' => $cm->course));
698 if ($grade_items) {
699 foreach ($grade_items as $grade_item) {
700 if ($visibility) {
701 $this->assertFalse($grade_item->is_hidden(), "$cm->modname grade_item not visible");
702 } else {
703 $this->assertTrue($grade_item->is_hidden(), "$cm->modname grade_item not hidden");
704 }
705 }
706 }
707
708 // Check the events visibility.
709 if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $cm->modname))) {
710 foreach ($events as $event) {
711 $calevent = new calendar_event($event);
712 $this->assertEquals($visibility, $calevent->visible, "$cm->modname calendar_event visibility");
713 }
714 }
715 }
716
b7d3bb0f
AG
717 public function test_course_page_type_list() {
718 global $DB;
719 $this->resetAfterTest(true);
720
721 // Create a category.
722 $category = new stdClass();
723 $category->name = 'Test Category';
724
725 $testcategory = $this->getDataGenerator()->create_category($category);
726
727 // Create a course.
728 $course = new stdClass();
729 $course->fullname = 'Apu loves Unit Təsts';
730 $course->shortname = 'Spread the lŭve';
731 $course->idnumber = '123';
732 $course->summary = 'Awesome!';
733 $course->summaryformat = FORMAT_PLAIN;
734 $course->format = 'topics';
735 $course->newsitems = 0;
736 $course->numsections = 5;
737 $course->category = $testcategory->id;
738
739 $testcourse = $this->getDataGenerator()->create_course($course);
740
741 // Create contexts.
742 $coursecontext = context_course::instance($testcourse->id);
743 $parentcontext = $coursecontext->get_parent_context(); // Not actually used.
744 $pagetype = 'page-course-x'; // Not used either.
745 $pagetypelist = course_page_type_list($pagetype, $parentcontext, $coursecontext);
746
747 // Page type lists for normal courses.
748 $testpagetypelist1 = array();
749 $testpagetypelist1['*'] = 'Any page';
750 $testpagetypelist1['course-*'] = 'Any course page';
751 $testpagetypelist1['course-view-*'] = 'Any type of course main page';
752
753 $this->assertEquals($testpagetypelist1, $pagetypelist);
754
755 // Get the context for the front page course.
756 $sitecoursecontext = context_course::instance(SITEID);
757 $pagetypelist = course_page_type_list($pagetype, $parentcontext, $sitecoursecontext);
758
759 // Page type list for the front page course.
760 $testpagetypelist2 = array('*' => 'Any page');
761 $this->assertEquals($testpagetypelist2, $pagetypelist);
762
763 // Make sure that providing no current context to the function doesn't result in an error.
764 // Calls made from generate_page_type_patterns() may provide null values.
765 $pagetypelist = course_page_type_list($pagetype, null, null);
766 $this->assertEquals($pagetypelist, $testpagetypelist1);
767 }
354b214c 768}