on-demand release 2.8beta+
[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
b35ef82f
MG
55 // The purpose of this test is to make sure that after upgrade script
56 // there is no duplicates in the field grade_items.sortorder (for each course)
57 // and the result of query "SELECT id FROM grade_items WHERE courseid=? ORDER BY sortorder, id" does not change.
58 $sequencesql = 'SELECT id FROM {grade_items} WHERE courseid=? ORDER BY sortorder, id';
59
60 // Each set is used for filling the db with fake data and will be representing the result of query:
61 // "SELECT sortorder from {grade_items} WHERE courseid=? ORDER BY id".
62 $testsets = array(
63 // Items that need no action.
64 array(1,2,3),
65 array(5,6,7),
66 array(7,6,1,3,2,5),
67 // Items with sortorder duplicates
68 array(1,2,2,3,3,4,5),
69 // Only one sortorder duplicate.
70 array(1,1),
71 array(3,3),
72 // Non-sequential sortorders with one or multiple duplicates.
73 array(3,3,7,5,6,6,9,10,8,3),
74 array(7,7,3),
75 array(3,4,5,3,5,4,7,1)
76 );
77 $origsequences = array();
78
79 // Generate the data and remember the initial sequence or items.
80 foreach ($testsets as $testset) {
81 $course = $this->getDataGenerator()->create_course();
82 foreach ($testset as $sortorder) {
83 $this->insert_fake_grade_item_sortorder($course->id, $sortorder);
84 }
85 $DB->get_records('grade_items');
86 $origsequences[$course->id] = $DB->get_fieldset_sql($sequencesql, array($course->id));
87 }
2a9d7a42
DP
88
89 $duplicatedetectionsql = "SELECT courseid, sortorder
90 FROM {grade_items}
91 GROUP BY courseid, sortorder
92 HAVING COUNT(id) > 1";
93
94 // Verify there are duplicates before we start the fix.
95 $dupes = $DB->record_exists_sql($duplicatedetectionsql);
96 $this->assertTrue($dupes);
97
98 // Do the work.
99 upgrade_grade_item_fix_sortorder();
100
101 // Verify that no duplicates are left in the database.
102 $dupes = $DB->record_exists_sql($duplicatedetectionsql);
103 $this->assertFalse($dupes);
104
b35ef82f
MG
105 // Verify that sequences are exactly the same as they were before upgrade script.
106 $idx = 0;
107 foreach ($origsequences as $courseid => $origsequence) {
108 if (count(($testsets[$idx])) == count(array_unique($testsets[$idx]))) {
109 // If there were no duplicates for this course verify that sortorders are not modified.
110 $newsortorders = $DB->get_fieldset_sql("SELECT sortorder from {grade_items} WHERE courseid=? ORDER BY id", array($courseid));
111 $this->assertEquals($testsets[$idx], $newsortorders);
112 }
113 $newsequence = $DB->get_fieldset_sql($sequencesql, array($courseid));
114 $this->assertEquals($origsequence, $newsequence,
115 "Sequences do not match for test set $idx : ".join(',', $testsets[$idx]));
116 $idx++;
2a9d7a42
DP
117 }
118 }
119
120 /**
121 * Populate some fake grade items into the database with specified
122 * sortorder and course id.
123 *
124 * NOTE: This function doesn't make much attempt to respect the
125 * gradebook internals, its simply used to fake some data for
126 * testing the upgradelib function. Please don't use it for other
127 * purposes.
128 *
129 * @param int $courseid id of course
130 * @param int $sortorder numeric sorting order of item
131 * @return stdClass grade item object from the database.
132 */
133 private function insert_fake_grade_item_sortorder($courseid, $sortorder) {
134 global $DB, $CFG;
135 require_once($CFG->libdir.'/gradelib.php');
136
137 $item = new stdClass();
138 $item->courseid = $courseid;
139 $item->sortorder = $sortorder;
140 $item->gradetype = GRADE_TYPE_VALUE;
141 $item->grademin = 30;
142 $item->grademax = 110;
143 $item->itemnumber = 1;
144 $item->iteminfo = '';
145 $item->timecreated = time();
146 $item->timemodified = time();
147
148 $item->id = $DB->insert_record('grade_items', $item);
149
150 return $DB->get_record('grade_items', array('id' => $item->id));
151 }
119cf17e
DP
152
153 public function test_upgrade_fix_missing_root_folders() {
154 global $DB, $SITE;
155
156 $this->resetAfterTest(true);
157
158 // Setup some broken data...
159 // Create two resources (and associated file areas).
160 $this->setAdminUser();
161 $resource1 = $this->getDataGenerator()->get_plugin_generator('mod_resource')
162 ->create_instance(array('course' => $SITE->id));
163 $resource2 = $this->getDataGenerator()->get_plugin_generator('mod_resource')
164 ->create_instance(array('course' => $SITE->id));
165
166 // Delete the folder record of resource1 to simulate broken data.
167 $context = context_module::instance($resource1->cmid);
168 $selectargs = array('contextid' => $context->id,
169 'component' => 'mod_resource',
170 'filearea' => 'content',
171 'itemid' => 0);
172
173 // Verify file records exist.
174 $areafilecount = $DB->count_records('files', $selectargs);
175 $this->assertNotEmpty($areafilecount);
176
177 // Delete the folder record.
178 $folderrecord = $selectargs;
179 $folderrecord['filepath'] = '/';
180 $folderrecord['filename'] = '.';
6c704b15
DP
181
182 // Get previous folder record.
183 $oldrecord = $DB->get_record('files', $folderrecord);
119cf17e
DP
184 $DB->delete_records('files', $folderrecord);
185
186 // Verify the folder record has been removed.
187 $newareafilecount = $DB->count_records('files', $selectargs);
188 $this->assertSame($newareafilecount, $areafilecount - 1);
189
190 $this->assertFalse($DB->record_exists('files', $folderrecord));
191
192 // Run the upgrade step!
193 upgrade_fix_missing_root_folders();
194
195 // Verify the folder record has been restored.
196 $newareafilecount = $DB->count_records('files', $selectargs);
197 $this->assertSame($newareafilecount, $areafilecount);
198
6c704b15
DP
199 $newrecord = $DB->get_record('files', $folderrecord, '*', MUST_EXIST);
200 // Verify the hash is correctly created.
201 $this->assertSame($oldrecord->pathnamehash, $newrecord->pathnamehash);
119cf17e 202 }
8e97006a 203
204 /**
205 * Tests the upgrade of an individual course-module or section from the
206 * old to new availability system. (This test does not use the database
207 * so it can run any time.)
208 */
209 public function test_upgrade_availability_item() {
210 global $CFG;
211 $this->resetAfterTest();
212
213 // This function is in the other upgradelib.
214 require_once($CFG->libdir . '/db/upgradelib.php');
215
216 // Groupmembersonly (or nothing). Show option on but ignored.
061e6b28 217 // Note: This $CFG option doesn't exist any more but we are testing the
218 // upgrade function so it did exist then...
8e97006a 219 $CFG->enablegroupmembersonly = 0;
220 $this->assertNull(
221 upgrade_availability_item(1, 0, 0, 0, 1, array(), array()));
222 $CFG->enablegroupmembersonly = 1;
223 $this->assertNull(
224 upgrade_availability_item(0, 0, 0, 0, 1, array(), array()));
225 $this->assertEquals(
226 '{"op":"&","showc":[false],"c":[{"type":"group"}]}',
227 upgrade_availability_item(1, 0, 0, 0, 1, array(), array()));
228 $this->assertEquals(
229 '{"op":"&","showc":[false],"c":[{"type":"grouping","id":4}]}',
230 upgrade_availability_item(1, 4, 0, 0, 1, array(), array()));
231
232 // Dates (with show/hide options - until date always hides).
233 $this->assertEquals(
234 '{"op":"&","showc":[true],"c":[{"type":"date","d":">=","t":996}]}',
235 upgrade_availability_item(0, 0, 996, 0, 1, array(), array()));
236 $this->assertEquals(
237 '{"op":"&","showc":[false],"c":[{"type":"date","d":">=","t":997}]}',
238 upgrade_availability_item(0, 0, 997, 0, 0, array(), array()));
239 $this->assertEquals(
240 '{"op":"&","showc":[false],"c":[{"type":"date","d":"<","t":998}]}',
241 upgrade_availability_item(0, 0, 0, 998, 1, array(), array()));
242 $this->assertEquals(
243 '{"op":"&","showc":[true,false],"c":[' .
244 '{"type":"date","d":">=","t":995},{"type":"date","d":"<","t":999}]}',
245 upgrade_availability_item(0, 0, 995, 999, 1, array(), array()));
246
247 // Grade (show option works as normal).
248 $availrec = (object)array(
249 'sourcecmid' => null, 'requiredcompletion' => null,
250 'gradeitemid' => 13, 'grademin' => null, 'grademax' => null);
251 $this->assertEquals(
252 '{"op":"&","showc":[true],"c":[{"type":"grade","id":13}]}',
253 upgrade_availability_item(0, 0, 0, 0, 1, array($availrec), array()));
254 $availrec->grademin = 4.1;
255 $this->assertEquals(
256 '{"op":"&","showc":[false],"c":[{"type":"grade","id":13,"min":4.10000}]}',
257 upgrade_availability_item(0, 0, 0, 0, 0, array($availrec), array()));
258 $availrec->grademax = 9.9;
259 $this->assertEquals(
260 '{"op":"&","showc":[true],"c":[{"type":"grade","id":13,"min":4.10000,"max":9.90000}]}',
261 upgrade_availability_item(0, 0, 0, 0, 1, array($availrec), array()));
262 $availrec->grademin = null;
263 $this->assertEquals(
264 '{"op":"&","showc":[true],"c":[{"type":"grade","id":13,"max":9.90000}]}',
265 upgrade_availability_item(0, 0, 0, 0, 1, array($availrec), array()));
266
267 // Completion (show option normal).
268 $availrec->grademax = null;
269 $availrec->gradeitemid = null;
270 $availrec->sourcecmid = 666;
271 $availrec->requiredcompletion = 1;
272 $this->assertEquals(
273 '{"op":"&","showc":[true],"c":[{"type":"completion","cm":666,"e":1}]}',
274 upgrade_availability_item(0, 0, 0, 0, 1, array($availrec), array()));
275 $this->assertEquals(
276 '{"op":"&","showc":[false],"c":[{"type":"completion","cm":666,"e":1}]}',
277 upgrade_availability_item(0, 0, 0, 0, 0, array($availrec), array()));
278
279 // Profile conditions (custom/standard field, values/not, show option normal).
280 $fieldrec = (object)array('userfield' => 'email', 'operator' => 'isempty',
281 'value' => '', 'shortname' => null);
282 $this->assertEquals(
283 '{"op":"&","showc":[true],"c":[{"type":"profile","op":"isempty","sf":"email"}]}',
284 upgrade_availability_item(0, 0, 0, 0, 1, array(), array($fieldrec)));
285 $fieldrec->value = '@';
286 $fieldrec->operator = 'contains';
287 $this->assertEquals(
288 '{"op":"&","showc":[true],"c":[{"type":"profile","op":"contains","sf":"email","v":"@"}]}',
289 upgrade_availability_item(0, 0, 0, 0, 1, array(), array($fieldrec)));
290 $fieldrec->operator = 'isnotempty';
291 $fieldrec->userfield = null;
292 $fieldrec->shortname = 'frogtype';
293 $this->assertEquals(
294 '{"op":"&","showc":[false],"c":[{"type":"profile","op":"isnotempty","cf":"frogtype"}]}',
295 upgrade_availability_item(0, 0, 0, 0, 0, array(), array($fieldrec)));
296
297 // Everything at once.
298 $this->assertEquals('{"op":"&","showc":[false,true,false,true,true,true],' .
299 '"c":[{"type":"grouping","id":13},' .
300 '{"type":"date","d":">=","t":990},' .
301 '{"type":"date","d":"<","t":991},' .
302 '{"type":"grade","id":665,"min":70.00000},' .
303 '{"type":"completion","cm":42,"e":2},' .
304 '{"type":"profile","op":"isempty","sf":"email"}]}',
305 upgrade_availability_item(1, 13, 990, 991, 1, array(
306 (object)array('sourcecmid' => null, 'gradeitemid' => 665, 'grademin' => 70),
307 (object)array('sourcecmid' => 42, 'gradeitemid' => null, 'requiredcompletion' => 2)
308 ), array(
309 (object)array('userfield' => 'email', 'shortname' => null, 'operator' => 'isempty'),
310 )));
311 }
6b7df0b5 312}