MDL-33630 backup: invalidate ids cache on temptable drop.
[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
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
69 public function test_move_section() {
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
81 move_section_to($course, 2, 4);
82 $sections = array();
83 foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
84 $sections[$section->section] = $section->id;
85 }
86 ksort($sections);
87
88 $this->assertEquals($oldsections[0], $sections[0]);
89 $this->assertEquals($oldsections[1], $sections[1]);
90 $this->assertEquals($oldsections[2], $sections[4]);
91 $this->assertEquals($oldsections[3], $sections[2]);
92 $this->assertEquals($oldsections[4], $sections[3]);
93 $this->assertEquals($oldsections[5], $sections[5]);
94 $this->assertEquals($oldsections[6], $sections[6]);
95 }
96
97 public function test_get_course_display_name_for_list() {
98 global $CFG;
99 $this->resetAfterTest(true);
100
101 $course = $this->getDataGenerator()->create_course(array('shortname' => 'FROG101', 'fullname' => 'Introduction to pond life'));
102
103 $CFG->courselistshortnames = 0;
104 $this->assertEquals('Introduction to pond life', get_course_display_name_for_list($course));
105
106 $CFG->courselistshortnames = 1;
107 $this->assertEquals('FROG101 Introduction to pond life', get_course_display_name_for_list($course));
108 }
b1a8aa73
ARN
109
110 public function test_create_course_category() {
111 global $CFG, $DB;
112 $this->resetAfterTest(true);
113
114 // Create the category
115 $data = new stdClass();
116 $data->name = 'aaa';
117 $data->description = 'aaa';
118 $data->idnumber = '';
119
120 $category1 = create_course_category($data);
121
122 // Initially confirm that base data was inserted correctly
123 $this->assertEquals($data->name, $category1->name);
124 $this->assertEquals($data->description, $category1->description);
125 $this->assertEquals($data->idnumber, $category1->idnumber);
126
127 // sortorder should be blank initially
128 $this->assertEmpty($category1->sortorder);
129
130 // Calling fix_course_sortorder() should provide a new sortorder
131 fix_course_sortorder();
132 $category1 = $DB->get_record('course_categories', array('id' => $category1->id));
133
134 $this->assertGreaterThanOrEqual(1, $category1->sortorder);
135
136 // Create two more categories and test the sortorder worked correctly
137 $data->name = 'ccc';
138 $category2 = create_course_category($data);
139 $this->assertEmpty($category2->sortorder);
140
141 $data->name = 'bbb';
142 $category3 = create_course_category($data);
143 $this->assertEmpty($category3->sortorder);
144
145 // Calling fix_course_sortorder() should provide a new sortorder to give category1,
146 // category2, category3. New course categories are ordered by id not name
147 fix_course_sortorder();
148
149 $category1 = $DB->get_record('course_categories', array('id' => $category1->id));
150 $category2 = $DB->get_record('course_categories', array('id' => $category2->id));
151 $category3 = $DB->get_record('course_categories', array('id' => $category3->id));
152
153 $this->assertGreaterThanOrEqual($category1->sortorder, $category2->sortorder);
154 $this->assertGreaterThanOrEqual($category2->sortorder, $category3->sortorder);
155 $this->assertGreaterThanOrEqual($category1->sortorder, $category3->sortorder);
156 }
354b214c 157}