MDL-37476 course_create unit test fails if course completion is enabled
[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();
f31ee129 39 foreach ($DB->get_records('course_sections', array('course'=>$course->id), 'id') as $section) {
354b214c
PS
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
eb01aa2c
RT
55 $neworder = reorder_sections($sections, 4, 2);
56 $neworder = array_keys($neworder);
57 $this->assertEquals($oldsections[0], $neworder[0]);
58 $this->assertEquals($oldsections[1], $neworder[1]);
59 $this->assertEquals($oldsections[2], $neworder[3]);
60 $this->assertEquals($oldsections[3], $neworder[4]);
61 $this->assertEquals($oldsections[4], $neworder[2]);
62 $this->assertEquals($oldsections[5], $neworder[5]);
63 $this->assertEquals($oldsections[6], $neworder[6]);
64
354b214c
PS
65 $neworder = reorder_sections(1, 2, 4);
66 $this->assertFalse($neworder);
67 }
68
3d8fe482 69 public function test_move_section_down() {
354b214c
PS
70 global $DB;
71 $this->resetAfterTest(true);
72
73 $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
74 $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
75 $oldsections = array();
76 foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
77 $oldsections[$section->section] = $section->id;
78 }
79 ksort($oldsections);
80
3d8fe482 81 // Test move section down..
354b214c
PS
82 move_section_to($course, 2, 4);
83 $sections = array();
84 foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
85 $sections[$section->section] = $section->id;
86 }
87 ksort($sections);
88
89 $this->assertEquals($oldsections[0], $sections[0]);
90 $this->assertEquals($oldsections[1], $sections[1]);
91 $this->assertEquals($oldsections[2], $sections[4]);
92 $this->assertEquals($oldsections[3], $sections[2]);
93 $this->assertEquals($oldsections[4], $sections[3]);
94 $this->assertEquals($oldsections[5], $sections[5]);
95 $this->assertEquals($oldsections[6], $sections[6]);
96 }
97
3d8fe482
DP
98 public function test_move_section_up() {
99 global $DB;
100 $this->resetAfterTest(true);
101
102 $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
103 $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
104 $oldsections = array();
105 foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
106 $oldsections[$section->section] = $section->id;
107 }
108 ksort($oldsections);
109
110 // Test move section up..
111 move_section_to($course, 6, 4);
112 $sections = array();
113 foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
114 $sections[$section->section] = $section->id;
115 }
116 ksort($sections);
117
118 $this->assertEquals($oldsections[0], $sections[0]);
119 $this->assertEquals($oldsections[1], $sections[1]);
120 $this->assertEquals($oldsections[2], $sections[2]);
121 $this->assertEquals($oldsections[3], $sections[3]);
122 $this->assertEquals($oldsections[4], $sections[5]);
123 $this->assertEquals($oldsections[5], $sections[6]);
124 $this->assertEquals($oldsections[6], $sections[4]);
125 }
126
127 public function test_move_section_marker() {
128 global $DB;
129 $this->resetAfterTest(true);
130
131 $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
132 $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
133
134 // Set course marker to the section we are going to move..
135 course_set_marker($course->id, 2);
136 // Verify that the course marker is set correctly.
137 $course = $DB->get_record('course', array('id' => $course->id));
138 $this->assertEquals(2, $course->marker);
139
140 // Test move the marked section down..
141 move_section_to($course, 2, 4);
142
143 // Verify that the coruse marker has been moved along with the section..
144 $course = $DB->get_record('course', array('id' => $course->id));
145 $this->assertEquals(4, $course->marker);
146
147 // Test move the marked section up..
148 move_section_to($course, 4, 3);
149
150 // Verify that the course marker has been moved along with the section..
151 $course = $DB->get_record('course', array('id' => $course->id));
152 $this->assertEquals(3, $course->marker);
153
154 // Test moving a non-marked section above the marked section..
155 move_section_to($course, 4, 2);
156
157 // Verify that the course marker has been moved down to accomodate..
158 $course = $DB->get_record('course', array('id' => $course->id));
159 $this->assertEquals(4, $course->marker);
160
161 // Test moving a non-marked section below the marked section..
162 move_section_to($course, 3, 6);
163
164 // Verify that the course marker has been up to accomodate..
165 $course = $DB->get_record('course', array('id' => $course->id));
166 $this->assertEquals(3, $course->marker);
167 }
168
354b214c
PS
169 public function test_get_course_display_name_for_list() {
170 global $CFG;
171 $this->resetAfterTest(true);
172
173 $course = $this->getDataGenerator()->create_course(array('shortname' => 'FROG101', 'fullname' => 'Introduction to pond life'));
174
175 $CFG->courselistshortnames = 0;
176 $this->assertEquals('Introduction to pond life', get_course_display_name_for_list($course));
177
178 $CFG->courselistshortnames = 1;
179 $this->assertEquals('FROG101 Introduction to pond life', get_course_display_name_for_list($course));
180 }
b1a8aa73
ARN
181
182 public function test_create_course_category() {
183 global $CFG, $DB;
184 $this->resetAfterTest(true);
185
186 // Create the category
187 $data = new stdClass();
188 $data->name = 'aaa';
189 $data->description = 'aaa';
190 $data->idnumber = '';
191
192 $category1 = create_course_category($data);
193
194 // Initially confirm that base data was inserted correctly
195 $this->assertEquals($data->name, $category1->name);
196 $this->assertEquals($data->description, $category1->description);
197 $this->assertEquals($data->idnumber, $category1->idnumber);
198
199 // sortorder should be blank initially
200 $this->assertEmpty($category1->sortorder);
201
202 // Calling fix_course_sortorder() should provide a new sortorder
203 fix_course_sortorder();
204 $category1 = $DB->get_record('course_categories', array('id' => $category1->id));
205
206 $this->assertGreaterThanOrEqual(1, $category1->sortorder);
207
208 // Create two more categories and test the sortorder worked correctly
209 $data->name = 'ccc';
210 $category2 = create_course_category($data);
211 $this->assertEmpty($category2->sortorder);
212
213 $data->name = 'bbb';
214 $category3 = create_course_category($data);
215 $this->assertEmpty($category3->sortorder);
216
217 // Calling fix_course_sortorder() should provide a new sortorder to give category1,
218 // category2, category3. New course categories are ordered by id not name
219 fix_course_sortorder();
220
221 $category1 = $DB->get_record('course_categories', array('id' => $category1->id));
222 $category2 = $DB->get_record('course_categories', array('id' => $category2->id));
223 $category3 = $DB->get_record('course_categories', array('id' => $category3->id));
224
225 $this->assertGreaterThanOrEqual($category1->sortorder, $category2->sortorder);
226 $this->assertGreaterThanOrEqual($category2->sortorder, $category3->sortorder);
227 $this->assertGreaterThanOrEqual($category1->sortorder, $category3->sortorder);
228 }
354b214c 229}