MDL-32149 courselib tests and support for generation of course sections
[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
28
29class courselib_testcase extends advanced_testcase {
30
31 public function test_reorder_sections() {
32 global $DB;
33 $this->resetAfterTest(true);
34
35 $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
36 $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
37 $oldsections = array();
38 $sections = array();
39 foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
40 $oldsections[$section->section] = $section->id;
41 $sections[$section->id] = $section->section;
42 }
43 ksort($oldsections);
44
45 $neworder = reorder_sections($sections, 2, 4);
46 $neworder = array_keys($neworder);
47 $this->assertEquals($oldsections[0], $neworder[0]);
48 $this->assertEquals($oldsections[1], $neworder[1]);
49 $this->assertEquals($oldsections[2], $neworder[4]);
50 $this->assertEquals($oldsections[3], $neworder[2]);
51 $this->assertEquals($oldsections[4], $neworder[3]);
52 $this->assertEquals($oldsections[5], $neworder[5]);
53 $this->assertEquals($oldsections[6], $neworder[6]);
54
55 $neworder = reorder_sections(1, 2, 4);
56 $this->assertFalse($neworder);
57 }
58
59 public function test_move_section() {
60 global $DB;
61 $this->resetAfterTest(true);
62
63 $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
64 $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
65 $oldsections = array();
66 foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
67 $oldsections[$section->section] = $section->id;
68 }
69 ksort($oldsections);
70
71 move_section_to($course, 2, 4);
72 $sections = array();
73 foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
74 $sections[$section->section] = $section->id;
75 }
76 ksort($sections);
77
78 $this->assertEquals($oldsections[0], $sections[0]);
79 $this->assertEquals($oldsections[1], $sections[1]);
80 $this->assertEquals($oldsections[2], $sections[4]);
81 $this->assertEquals($oldsections[3], $sections[2]);
82 $this->assertEquals($oldsections[4], $sections[3]);
83 $this->assertEquals($oldsections[5], $sections[5]);
84 $this->assertEquals($oldsections[6], $sections[6]);
85 }
86
87 public function test_get_course_display_name_for_list() {
88 global $CFG;
89 $this->resetAfterTest(true);
90
91 $course = $this->getDataGenerator()->create_course(array('shortname' => 'FROG101', 'fullname' => 'Introduction to pond life'));
92
93 $CFG->courselistshortnames = 0;
94 $this->assertEquals('Introduction to pond life', get_course_display_name_for_list($course));
95
96 $CFG->courselistshortnames = 1;
97 $this->assertEquals('FROG101 Introduction to pond life', get_course_display_name_for_list($course));
98 }
99}