on-demand release 2.3dev
[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 }
b1a8aa73
ARN
99
100 public function test_create_course_category() {
101 global $CFG, $DB;
102 $this->resetAfterTest(true);
103
104 // Create the category
105 $data = new stdClass();
106 $data->name = 'aaa';
107 $data->description = 'aaa';
108 $data->idnumber = '';
109
110 $category1 = create_course_category($data);
111
112 // Initially confirm that base data was inserted correctly
113 $this->assertEquals($data->name, $category1->name);
114 $this->assertEquals($data->description, $category1->description);
115 $this->assertEquals($data->idnumber, $category1->idnumber);
116
117 // sortorder should be blank initially
118 $this->assertEmpty($category1->sortorder);
119
120 // Calling fix_course_sortorder() should provide a new sortorder
121 fix_course_sortorder();
122 $category1 = $DB->get_record('course_categories', array('id' => $category1->id));
123
124 $this->assertGreaterThanOrEqual(1, $category1->sortorder);
125
126 // Create two more categories and test the sortorder worked correctly
127 $data->name = 'ccc';
128 $category2 = create_course_category($data);
129 $this->assertEmpty($category2->sortorder);
130
131 $data->name = 'bbb';
132 $category3 = create_course_category($data);
133 $this->assertEmpty($category3->sortorder);
134
135 // Calling fix_course_sortorder() should provide a new sortorder to give category1,
136 // category2, category3. New course categories are ordered by id not name
137 fix_course_sortorder();
138
139 $category1 = $DB->get_record('course_categories', array('id' => $category1->id));
140 $category2 = $DB->get_record('course_categories', array('id' => $category2->id));
141 $category3 = $DB->get_record('course_categories', array('id' => $category3->id));
142
143 $this->assertGreaterThanOrEqual($category1->sortorder, $category2->sortorder);
144 $this->assertGreaterThanOrEqual($category2->sortorder, $category3->sortorder);
145 $this->assertGreaterThanOrEqual($category1->sortorder, $category3->sortorder);
146 }
354b214c 147}