MDL-41062 gradebook: remove sortorder duplicates
[moodle.git] / lib / tests / upgradelib_test.php
CommitLineData
6b7df0b5
EL
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/**
f5700877 18 * Unit tests for the lib/upgradelib.php library.
6b7df0b5
EL
19 *
20 * @package core
21 * @category phpunit
22 * @copyright 2013 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->libdir.'/upgradelib.php');
30
31
32/**
f5700877 33 * Tests various classes and functions in upgradelib.php library.
6b7df0b5 34 */
f5700877 35class core_upgradelib_testcase extends advanced_testcase {
6b7df0b5
EL
36
37 /**
38 * Test the {@link upgrade_stale_php_files_present() function
39 */
40 public function test_upgrade_stale_php_files_present() {
41 // Just call the function, must return bool false always
42 // if there aren't any old files in the codebase.
43 $this->assertFalse(upgrade_stale_php_files_present());
44 }
2a9d7a42
DP
45
46 /**
47 * Test the {@link upgrade_grade_item_fix_sortorder() function with
48 * faked duplicate sortorder data.
49 */
50 public function test_upgrade_grade_item_fix_sortorder() {
51 global $DB;
52
53 $this->resetAfterTest(true);
54
55 // Create fake items in course1, with sortorder duplicates.
56 $course1 = $this->getDataGenerator()->create_course();
57 $course1item = array();
58 $course1item[0] = $this->insert_fake_grade_item_sortorder($course1->id, 1);
59
60 $course1item[1] = $this->insert_fake_grade_item_sortorder($course1->id, 2);
61 $course1item[2] = $this->insert_fake_grade_item_sortorder($course1->id, 2);
62
63 $course1item[3] = $this->insert_fake_grade_item_sortorder($course1->id, 3);
64 $course1item[4] = $this->insert_fake_grade_item_sortorder($course1->id, 3);
65
66 $course1item[5] = $this->insert_fake_grade_item_sortorder($course1->id, 4);
67 $course1item[6] = $this->insert_fake_grade_item_sortorder($course1->id, 5);
68
69 // Create fake items in course2 which need no action.
70 $course2 = $this->getDataGenerator()->create_course();
71 $course2item = array();
72 $course2item[0] = $this->insert_fake_grade_item_sortorder($course2->id, 1);
73 $course2item[1] = $this->insert_fake_grade_item_sortorder($course2->id, 2);
74 $course2item[2] = $this->insert_fake_grade_item_sortorder($course2->id, 3);
75
76 // Create a new course which only has sortorder duplicates.
77 $course3 = $this->getDataGenerator()->create_course();
78 $course3item = array();
79 $course3item[0] = $this->insert_fake_grade_item_sortorder($course3->id, 1);
80 $course3item[1] = $this->insert_fake_grade_item_sortorder($course3->id, 1);
81
82 // A course with non-sequential sortorders and duplicates.
83 $course4 = $this->getDataGenerator()->create_course();
84 $course4item = array();
85 $course4item[0] = $this->insert_fake_grade_item_sortorder($course4->id, 3);
86 $course4item[1] = $this->insert_fake_grade_item_sortorder($course4->id, 3);
87
88 $course4item[2] = $this->insert_fake_grade_item_sortorder($course4->id, 5);
89 $course4item[3] = $this->insert_fake_grade_item_sortorder($course4->id, 6);
90 $course4item[4] = $this->insert_fake_grade_item_sortorder($course4->id, 6);
91
92 $course4item[5] = $this->insert_fake_grade_item_sortorder($course4->id, 9);
93 $course4item[6] = $this->insert_fake_grade_item_sortorder($course4->id, 10);
94 // Create some items with non-sequential id and sortorder relationship.
95 $course4item[7] = $this->insert_fake_grade_item_sortorder($course4->id, 7);
96 $course4item[8] = $this->insert_fake_grade_item_sortorder($course4->id, 8);
97
98 $duplicatedetectionsql = "SELECT courseid, sortorder
99 FROM {grade_items}
100 GROUP BY courseid, sortorder
101 HAVING COUNT(id) > 1";
102
103 // Verify there are duplicates before we start the fix.
104 $dupes = $DB->record_exists_sql($duplicatedetectionsql);
105 $this->assertTrue($dupes);
106
107 // Do the work.
108 upgrade_grade_item_fix_sortorder();
109
110 // Verify that no duplicates are left in the database.
111 $dupes = $DB->record_exists_sql($duplicatedetectionsql);
112 $this->assertFalse($dupes);
113
114 // Load all grade items for ease.
115 $afterfixgradeitems = $DB->get_records('grade_items');
116
117 // Verify that the duplicate sortorders have been removed from course1.
118 $this->assertNotEquals($afterfixgradeitems[$course1item[1]->id]->sortorder,
119 $afterfixgradeitems[$course1item[2]->id]->sortorder);
120 $this->assertNotEquals($afterfixgradeitems[$course1item[3]->id]->sortorder,
121 $afterfixgradeitems[$course1item[4]->id]->sortorder);
122 // Verify that the order has been respected in course1.
123 $this->assertGreaterThan($afterfixgradeitems[$course1item[0]->id]->sortorder,
124 $afterfixgradeitems[$course1item[1]->id]->sortorder);
125 $this->assertGreaterThan($afterfixgradeitems[$course1item[2]->id]->sortorder,
126 $afterfixgradeitems[$course1item[3]->id]->sortorder);
127 $this->assertGreaterThan($afterfixgradeitems[$course1item[3]->id]->sortorder,
128 $afterfixgradeitems[$course1item[5]->id]->sortorder);
129 $this->assertGreaterThan($afterfixgradeitems[$course1item[5]->id]->sortorder,
130 $afterfixgradeitems[$course1item[6]->id]->sortorder);
131
132 // Verify that no other fields have been modified in course1.
133 foreach ($course1item as $originalitem) {
134 $newitem = $afterfixgradeitems[$originalitem->id];
135
136 // Ignore changes to sortorder.
137 unset($originalitem->sortorder);
138 unset($newitem->sortorder);
139
140 $this->assertEquals($originalitem, $newitem);
141 }
142
143 // Verify that course2 items are completely unmodified.
144 foreach ($course2item as $originalitem) {
145 $newitem = $afterfixgradeitems[$originalitem->id];
146 $this->assertEquals($originalitem, $newitem);
147 }
148
149 // Verify that the duplicates in course3 have been removed.
150 $this->assertNotEquals($afterfixgradeitems[$course3item[0]->id]->sortorder,
151 $afterfixgradeitems[$course3item[1]->id]->sortorder);
152
153 // Verify that no other fields in course3 have been modified.
154 foreach ($course3item as $originalitem) {
155 $newitem = $afterfixgradeitems[$originalitem->id];
156
157 // Ignore changes to sortorder.
158 unset($originalitem->sortorder);
159 unset($newitem->sortorder);
160
161 $this->assertEquals($originalitem, $newitem);
162 }
163
164 // Verify that the duplicates in course4 have been removed.
165 $this->assertNotEquals($afterfixgradeitems[$course4item[0]->id]->sortorder,
166 $afterfixgradeitems[$course4item[1]->id]->sortorder);
167 $this->assertNotEquals($afterfixgradeitems[$course4item[3]->id]->sortorder,
168 $afterfixgradeitems[$course4item[4]->id]->sortorder);
169
170 // Verify that the order has been respected in course4.
171 $this->assertGreaterThan($afterfixgradeitems[$course4item[1]->id]->sortorder,
172 $afterfixgradeitems[$course4item[2]->id]->sortorder, "2 grater than 1");
173 $this->assertGreaterThan($afterfixgradeitems[$course4item[4]->id]->sortorder,
174 $afterfixgradeitems[$course4item[5]->id]->sortorder);
175 $this->assertGreaterThan($afterfixgradeitems[$course4item[5]->id]->sortorder,
176 $afterfixgradeitems[$course4item[6]->id]->sortorder);
177
178 // Check the items created with non-sequential id and sortorder relationship
179 // are converted correclty.
180 $this->assertGreaterThan($afterfixgradeitems[$course4item[7]->id]->sortorder,
181 $afterfixgradeitems[$course4item[5]->id]->sortorder);
182 $this->assertGreaterThan($afterfixgradeitems[$course4item[7]->id]->sortorder,
183 $afterfixgradeitems[$course4item[8]->id]->sortorder);
184
185 // Verify that no other fields in course4 have been modified.
186 foreach ($course4item as $originalitem) {
187 $newitem = $afterfixgradeitems[$originalitem->id];
188
189 // Ignore changes to sortorder.
190 unset($originalitem->sortorder);
191 unset($newitem->sortorder);
192
193 $this->assertEquals($originalitem, $newitem);
194 }
195 }
196
197 /**
198 * Populate some fake grade items into the database with specified
199 * sortorder and course id.
200 *
201 * NOTE: This function doesn't make much attempt to respect the
202 * gradebook internals, its simply used to fake some data for
203 * testing the upgradelib function. Please don't use it for other
204 * purposes.
205 *
206 * @param int $courseid id of course
207 * @param int $sortorder numeric sorting order of item
208 * @return stdClass grade item object from the database.
209 */
210 private function insert_fake_grade_item_sortorder($courseid, $sortorder) {
211 global $DB, $CFG;
212 require_once($CFG->libdir.'/gradelib.php');
213
214 $item = new stdClass();
215 $item->courseid = $courseid;
216 $item->sortorder = $sortorder;
217 $item->gradetype = GRADE_TYPE_VALUE;
218 $item->grademin = 30;
219 $item->grademax = 110;
220 $item->itemnumber = 1;
221 $item->iteminfo = '';
222 $item->timecreated = time();
223 $item->timemodified = time();
224
225 $item->id = $DB->insert_record('grade_items', $item);
226
227 return $DB->get_record('grade_items', array('id' => $item->id));
228 }
6b7df0b5 229}