Commit | Line | Data |
---|---|---|
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 | ||
26 | defined('MOODLE_INTERNAL') || die(); | |
27 | ||
f70bfb84 FM |
28 | global $CFG; |
29 | require_once($CFG->dirroot.'/course/lib.php'); | |
354b214c PS |
30 | |
31 | class courselib_testcase extends advanced_testcase { | |
32 | ||
f70bfb84 FM |
33 | public function test_create_course() { |
34 | global $DB; | |
35 | $this->resetAfterTest(true); | |
36 | $defaultcategory = $DB->get_field_select('course_categories', "MIN(id)", "parent=0"); | |
37 | ||
38 | $course = new stdClass(); | |
39 | $course->fullname = 'Apu loves Unit Təsts'; | |
40 | $course->shortname = 'Spread the lŭve'; | |
41 | $course->idnumber = '123'; | |
42 | $course->summary = 'Awesome!'; | |
43 | $course->summaryformat = FORMAT_PLAIN; | |
44 | $course->format = 'topics'; | |
45 | $course->newsitems = 0; | |
46 | $course->numsections = 5; | |
47 | $course->category = $defaultcategory; | |
48 | ||
49 | $created = create_course($course); | |
50 | $context = context_course::instance($created->id); | |
51 | ||
52 | // Compare original and created. | |
53 | $original = (array) $course; | |
54 | $this->assertEquals($original, array_intersect_key((array) $created, $original)); | |
55 | ||
56 | // Ensure default section is created. | |
57 | $sectioncreated = $DB->record_exists('course_sections', array('course' => $created->id, 'section' => 0)); | |
58 | $this->assertTrue($sectioncreated); | |
59 | ||
60 | // Ensure blocks have been associated to the course. | |
61 | $blockcount = $DB->count_records('block_instances', array('parentcontextid' => $context->id)); | |
62 | $this->assertGreaterThan(0, $blockcount); | |
63 | } | |
64 | ||
384c3510 MG |
65 | public function test_create_course_with_generator() { |
66 | global $DB; | |
67 | $this->resetAfterTest(true); | |
68 | $course = $this->getDataGenerator()->create_course(); | |
69 | ||
70 | // Ensure default section is created. | |
71 | $sectioncreated = $DB->record_exists('course_sections', array('course' => $course->id, 'section' => 0)); | |
72 | $this->assertTrue($sectioncreated); | |
73 | } | |
74 | ||
75 | public function test_create_course_sections() { | |
76 | global $DB; | |
77 | $this->resetAfterTest(true); | |
78 | ||
79 | $course = $this->getDataGenerator()->create_course( | |
80 | array('shortname' => 'GrowingCourse', | |
81 | 'fullname' => 'Growing Course', | |
82 | 'numsections' => 5), | |
83 | array('createsections' => true)); | |
84 | ||
85 | // Ensure all 6 (0-5) sections were created and modinfo/sectioninfo cache works properly | |
86 | $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all()); | |
87 | $this->assertEquals(range(0, $course->numsections), $sectionscreated); | |
88 | ||
89 | // this will do nothing, section already exists | |
90 | $this->assertFalse(course_create_sections_if_missing($course, $course->numsections)); | |
91 | ||
92 | // this will create new section | |
93 | $this->assertTrue(course_create_sections_if_missing($course, $course->numsections + 1)); | |
94 | ||
95 | // Ensure all 7 (0-6) sections were created and modinfo/sectioninfo cache works properly | |
96 | $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all()); | |
97 | $this->assertEquals(range(0, $course->numsections + 1), $sectionscreated); | |
98 | } | |
99 | ||
354b214c PS |
100 | public function test_reorder_sections() { |
101 | global $DB; | |
102 | $this->resetAfterTest(true); | |
103 | ||
104 | $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true)); | |
105 | $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true)); | |
106 | $oldsections = array(); | |
107 | $sections = array(); | |
3a222db2 | 108 | foreach ($DB->get_records('course_sections', array('course'=>$course->id), 'id') as $section) { |
354b214c PS |
109 | $oldsections[$section->section] = $section->id; |
110 | $sections[$section->id] = $section->section; | |
111 | } | |
112 | ksort($oldsections); | |
113 | ||
114 | $neworder = reorder_sections($sections, 2, 4); | |
115 | $neworder = array_keys($neworder); | |
116 | $this->assertEquals($oldsections[0], $neworder[0]); | |
117 | $this->assertEquals($oldsections[1], $neworder[1]); | |
118 | $this->assertEquals($oldsections[2], $neworder[4]); | |
119 | $this->assertEquals($oldsections[3], $neworder[2]); | |
120 | $this->assertEquals($oldsections[4], $neworder[3]); | |
121 | $this->assertEquals($oldsections[5], $neworder[5]); | |
122 | $this->assertEquals($oldsections[6], $neworder[6]); | |
123 | ||
eb01aa2c RT |
124 | $neworder = reorder_sections($sections, 4, 2); |
125 | $neworder = array_keys($neworder); | |
126 | $this->assertEquals($oldsections[0], $neworder[0]); | |
127 | $this->assertEquals($oldsections[1], $neworder[1]); | |
128 | $this->assertEquals($oldsections[2], $neworder[3]); | |
129 | $this->assertEquals($oldsections[3], $neworder[4]); | |
130 | $this->assertEquals($oldsections[4], $neworder[2]); | |
131 | $this->assertEquals($oldsections[5], $neworder[5]); | |
132 | $this->assertEquals($oldsections[6], $neworder[6]); | |
133 | ||
354b214c PS |
134 | $neworder = reorder_sections(1, 2, 4); |
135 | $this->assertFalse($neworder); | |
136 | } | |
137 | ||
3d8fe482 | 138 | public function test_move_section_down() { |
354b214c PS |
139 | global $DB; |
140 | $this->resetAfterTest(true); | |
141 | ||
142 | $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true)); | |
143 | $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true)); | |
144 | $oldsections = array(); | |
145 | foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) { | |
146 | $oldsections[$section->section] = $section->id; | |
147 | } | |
148 | ksort($oldsections); | |
149 | ||
3d8fe482 | 150 | // Test move section down.. |
354b214c PS |
151 | move_section_to($course, 2, 4); |
152 | $sections = array(); | |
153 | foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) { | |
154 | $sections[$section->section] = $section->id; | |
155 | } | |
156 | ksort($sections); | |
157 | ||
158 | $this->assertEquals($oldsections[0], $sections[0]); | |
159 | $this->assertEquals($oldsections[1], $sections[1]); | |
160 | $this->assertEquals($oldsections[2], $sections[4]); | |
161 | $this->assertEquals($oldsections[3], $sections[2]); | |
162 | $this->assertEquals($oldsections[4], $sections[3]); | |
163 | $this->assertEquals($oldsections[5], $sections[5]); | |
164 | $this->assertEquals($oldsections[6], $sections[6]); | |
165 | } | |
166 | ||
3d8fe482 DP |
167 | public function test_move_section_up() { |
168 | global $DB; | |
169 | $this->resetAfterTest(true); | |
170 | ||
171 | $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true)); | |
172 | $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true)); | |
173 | $oldsections = array(); | |
174 | foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) { | |
175 | $oldsections[$section->section] = $section->id; | |
176 | } | |
177 | ksort($oldsections); | |
178 | ||
179 | // Test move section up.. | |
180 | move_section_to($course, 6, 4); | |
181 | $sections = array(); | |
182 | foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) { | |
183 | $sections[$section->section] = $section->id; | |
184 | } | |
185 | ksort($sections); | |
186 | ||
187 | $this->assertEquals($oldsections[0], $sections[0]); | |
188 | $this->assertEquals($oldsections[1], $sections[1]); | |
189 | $this->assertEquals($oldsections[2], $sections[2]); | |
190 | $this->assertEquals($oldsections[3], $sections[3]); | |
191 | $this->assertEquals($oldsections[4], $sections[5]); | |
192 | $this->assertEquals($oldsections[5], $sections[6]); | |
193 | $this->assertEquals($oldsections[6], $sections[4]); | |
194 | } | |
195 | ||
196 | public function test_move_section_marker() { | |
197 | global $DB; | |
198 | $this->resetAfterTest(true); | |
199 | ||
200 | $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true)); | |
201 | $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true)); | |
202 | ||
203 | // Set course marker to the section we are going to move.. | |
204 | course_set_marker($course->id, 2); | |
205 | // Verify that the course marker is set correctly. | |
206 | $course = $DB->get_record('course', array('id' => $course->id)); | |
207 | $this->assertEquals(2, $course->marker); | |
208 | ||
209 | // Test move the marked section down.. | |
210 | move_section_to($course, 2, 4); | |
211 | ||
212 | // Verify that the coruse marker has been moved along with the section.. | |
213 | $course = $DB->get_record('course', array('id' => $course->id)); | |
214 | $this->assertEquals(4, $course->marker); | |
215 | ||
216 | // Test move the marked section up.. | |
217 | move_section_to($course, 4, 3); | |
218 | ||
219 | // Verify that the course marker has been moved along with the section.. | |
220 | $course = $DB->get_record('course', array('id' => $course->id)); | |
221 | $this->assertEquals(3, $course->marker); | |
222 | ||
223 | // Test moving a non-marked section above the marked section.. | |
224 | move_section_to($course, 4, 2); | |
225 | ||
226 | // Verify that the course marker has been moved down to accomodate.. | |
227 | $course = $DB->get_record('course', array('id' => $course->id)); | |
228 | $this->assertEquals(4, $course->marker); | |
229 | ||
230 | // Test moving a non-marked section below the marked section.. | |
231 | move_section_to($course, 3, 6); | |
232 | ||
233 | // Verify that the course marker has been up to accomodate.. | |
234 | $course = $DB->get_record('course', array('id' => $course->id)); | |
235 | $this->assertEquals(3, $course->marker); | |
236 | } | |
237 | ||
354b214c PS |
238 | public function test_get_course_display_name_for_list() { |
239 | global $CFG; | |
240 | $this->resetAfterTest(true); | |
241 | ||
242 | $course = $this->getDataGenerator()->create_course(array('shortname' => 'FROG101', 'fullname' => 'Introduction to pond life')); | |
243 | ||
244 | $CFG->courselistshortnames = 0; | |
245 | $this->assertEquals('Introduction to pond life', get_course_display_name_for_list($course)); | |
246 | ||
247 | $CFG->courselistshortnames = 1; | |
248 | $this->assertEquals('FROG101 Introduction to pond life', get_course_display_name_for_list($course)); | |
249 | } | |
b1a8aa73 ARN |
250 | |
251 | public function test_create_course_category() { | |
252 | global $CFG, $DB; | |
253 | $this->resetAfterTest(true); | |
254 | ||
255 | // Create the category | |
256 | $data = new stdClass(); | |
257 | $data->name = 'aaa'; | |
258 | $data->description = 'aaa'; | |
259 | $data->idnumber = ''; | |
260 | ||
261 | $category1 = create_course_category($data); | |
262 | ||
263 | // Initially confirm that base data was inserted correctly | |
264 | $this->assertEquals($data->name, $category1->name); | |
265 | $this->assertEquals($data->description, $category1->description); | |
266 | $this->assertEquals($data->idnumber, $category1->idnumber); | |
267 | ||
268 | // sortorder should be blank initially | |
269 | $this->assertEmpty($category1->sortorder); | |
270 | ||
271 | // Calling fix_course_sortorder() should provide a new sortorder | |
272 | fix_course_sortorder(); | |
273 | $category1 = $DB->get_record('course_categories', array('id' => $category1->id)); | |
274 | ||
275 | $this->assertGreaterThanOrEqual(1, $category1->sortorder); | |
276 | ||
277 | // Create two more categories and test the sortorder worked correctly | |
278 | $data->name = 'ccc'; | |
279 | $category2 = create_course_category($data); | |
280 | $this->assertEmpty($category2->sortorder); | |
281 | ||
282 | $data->name = 'bbb'; | |
283 | $category3 = create_course_category($data); | |
284 | $this->assertEmpty($category3->sortorder); | |
285 | ||
286 | // Calling fix_course_sortorder() should provide a new sortorder to give category1, | |
287 | // category2, category3. New course categories are ordered by id not name | |
288 | fix_course_sortorder(); | |
289 | ||
290 | $category1 = $DB->get_record('course_categories', array('id' => $category1->id)); | |
291 | $category2 = $DB->get_record('course_categories', array('id' => $category2->id)); | |
292 | $category3 = $DB->get_record('course_categories', array('id' => $category3->id)); | |
293 | ||
294 | $this->assertGreaterThanOrEqual($category1->sortorder, $category2->sortorder); | |
295 | $this->assertGreaterThanOrEqual($category2->sortorder, $category3->sortorder); | |
296 | $this->assertGreaterThanOrEqual($category1->sortorder, $category3->sortorder); | |
297 | } | |
384c3510 MG |
298 | |
299 | public function test_move_module_in_course() { | |
300 | $this->resetAfterTest(true); | |
301 | // Setup fixture | |
302 | $course = $this->getDataGenerator()->create_course(array('numsections'=>5)); | |
303 | $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id)); | |
304 | ||
384c3510 MG |
305 | $cms = get_fast_modinfo($course)->get_cms(); |
306 | $cm = reset($cms); | |
307 | ||
308 | course_create_sections_if_missing($course, 3); | |
309 | $section3 = get_fast_modinfo($course)->get_section_info(3); | |
310 | ||
311 | moveto_module($cm, $section3); | |
312 | ||
384c3510 MG |
313 | $modinfo = get_fast_modinfo($course); |
314 | $this->assertTrue(empty($modinfo->sections[0])); | |
315 | $this->assertFalse(empty($modinfo->sections[3])); | |
316 | } | |
354b214c | 317 | } |