2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Unit tests for the lib/upgradelib.php library.
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
26 defined('MOODLE_INTERNAL') ||
die();
29 require_once($CFG->libdir
.'/upgradelib.php');
30 require_once($CFG->libdir
.'/db/upgradelib.php');
31 require_once($CFG->dirroot
. '/calendar/tests/helpers.php');
34 * Tests various classes and functions in upgradelib.php library.
36 class upgradelib_test
extends advanced_testcase
{
39 * Test the {@link upgrade_stale_php_files_present() function
41 public function test_upgrade_stale_php_files_present() {
42 // Just call the function, must return bool false always
43 // if there aren't any old files in the codebase.
44 $this->assertFalse(upgrade_stale_php_files_present());
48 * Populate some fake grade items into the database with specified
49 * sortorder and course id.
51 * NOTE: This function doesn't make much attempt to respect the
52 * gradebook internals, its simply used to fake some data for
53 * testing the upgradelib function. Please don't use it for other
56 * @param int $courseid id of course
57 * @param int $sortorder numeric sorting order of item
58 * @return stdClass grade item object from the database.
60 private function insert_fake_grade_item_sortorder($courseid, $sortorder) {
62 require_once($CFG->libdir
.'/gradelib.php');
64 $item = new stdClass();
65 $item->courseid
= $courseid;
66 $item->sortorder
= $sortorder;
67 $item->gradetype
= GRADE_TYPE_VALUE
;
69 $item->grademax
= 110;
70 $item->itemnumber
= 1;
72 $item->timecreated
= time();
73 $item->timemodified
= time();
75 $item->id
= $DB->insert_record('grade_items', $item);
77 return $DB->get_record('grade_items', array('id' => $item->id
));
80 public function test_upgrade_extra_credit_weightoverride() {
83 $this->resetAfterTest(true);
85 require_once($CFG->libdir
. '/db/upgradelib.php');
90 for ($i=0; $i<5; $i++
) {
91 $c[$i] = $this->getDataGenerator()->create_course();
94 for ($j=0;$j<3;$j++
) {
95 $a[$i][$j] = $this->getDataGenerator()->create_module('assign', array('course' => $c[$i], 'grade' => 100));
96 $giparams = array('itemtype' => 'mod', 'itemmodule' => 'assign', 'iteminstance' => $a[$i][$j]->id
,
97 'courseid' => $c[$i]->id
, 'itemnumber' => 0);
98 $gi[$i][$j] = grade_item
::fetch($giparams);
102 // Case 1: Course $c[0] has aggregation method different from natural.
103 $coursecategory = grade_category
::fetch_course_category($c[0]->id
);
104 $coursecategory->aggregation
= GRADE_AGGREGATE_WEIGHTED_MEAN
;
105 $coursecategory->update();
106 $gi[0][1]->aggregationcoef
= 1;
108 $gi[0][2]->weightoverride
= 1;
111 // Case 2: Course $c[1] has neither extra credits nor overrides
113 // Case 3: Course $c[2] has extra credits but no overrides
114 $gi[2][1]->aggregationcoef
= 1;
117 // Case 4: Course $c[3] has no extra credits and has overrides
118 $gi[3][2]->weightoverride
= 1;
121 // Case 5: Course $c[4] has both extra credits and overrides
122 $gi[4][1]->aggregationcoef
= 1;
124 $gi[4][2]->weightoverride
= 1;
127 // Run the upgrade script and make sure only course $c[4] was marked as needed to be fixed.
128 upgrade_extra_credit_weightoverride();
130 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $c[0]->id
}));
131 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $c[1]->id
}));
132 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $c[2]->id
}));
133 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $c[3]->id
}));
134 $this->assertEquals(20150619, $CFG->{'gradebook_calculations_freeze_' . $c[4]->id
});
136 set_config('gradebook_calculations_freeze_' . $c[4]->id
, null);
138 // Run the upgrade script for a single course only.
139 upgrade_extra_credit_weightoverride($c[0]->id
);
140 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $c[0]->id
}));
141 upgrade_extra_credit_weightoverride($c[4]->id
);
142 $this->assertEquals(20150619, $CFG->{'gradebook_calculations_freeze_' . $c[4]->id
});
146 * Test the upgrade function for flagging courses with calculated grade item problems.
148 public function test_upgrade_calculated_grade_items_freeze() {
151 $this->resetAfterTest();
153 require_once($CFG->libdir
. '/db/upgradelib.php');
156 $user = $this->getDataGenerator()->create_user();
158 // Create a couple of courses.
159 $course1 = $this->getDataGenerator()->create_course();
160 $course2 = $this->getDataGenerator()->create_course();
161 $course3 = $this->getDataGenerator()->create_course();
163 // Enrol the user in the courses.
164 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
165 $maninstance1 = $DB->get_record('enrol', array('courseid' => $course1->id
, 'enrol' => 'manual'), '*', MUST_EXIST
);
166 $maninstance2 = $DB->get_record('enrol', array('courseid' => $course2->id
, 'enrol' => 'manual'), '*', MUST_EXIST
);
167 $maninstance3 = $DB->get_record('enrol', array('courseid' => $course3->id
, 'enrol' => 'manual'), '*', MUST_EXIST
);
168 $manual = enrol_get_plugin('manual');
169 $manual->enrol_user($maninstance1, $user->id
, $studentrole->id
);
170 $manual->enrol_user($maninstance2, $user->id
, $studentrole->id
);
171 $manual->enrol_user($maninstance3, $user->id
, $studentrole->id
);
173 // To create the data we need we freeze the grade book to use the old behaviour.
174 set_config('gradebook_calculations_freeze_' . $course1->id
, 20150627);
175 set_config('gradebook_calculations_freeze_' . $course2->id
, 20150627);
176 set_config('gradebook_calculations_freeze_' . $course3->id
, 20150627);
177 $CFG->grade_minmaxtouse
= 2;
179 // Creating a category for a grade item.
180 $gradecategory = new grade_category();
181 $gradecategory->fullname
= 'calculated grade category';
182 $gradecategory->courseid
= $course1->id
;
183 $gradecategory->insert();
184 $gradecategoryid = $gradecategory->id
;
186 // This is a manual grade item.
187 $gradeitem = new grade_item();
188 $gradeitem->itemname
= 'grade item one';
189 $gradeitem->itemtype
= 'manual';
190 $gradeitem->categoryid
= $gradecategoryid;
191 $gradeitem->courseid
= $course1->id
;
192 $gradeitem->idnumber
= 'gi1';
193 $gradeitem->insert();
195 // Changing the category into a calculated grade category.
196 $gradecategoryitem = grade_item
::fetch(array('iteminstance' => $gradecategory->id
));
197 $gradecategoryitem->calculation
= '=##gi' . $gradeitem->id
. '##/2';
198 $gradecategoryitem->update();
200 // Setting a grade for the student.
201 $grade = $gradeitem->get_grade($user->id
, true);
202 $grade->finalgrade
= 50;
204 // Creating all the grade_grade items.
205 grade_regrade_final_grades($course1->id
);
206 // Updating the grade category to a new grade max and min.
207 $gradecategoryitem->grademax
= 50;
208 $gradecategoryitem->grademin
= 5;
209 $gradecategoryitem->update();
211 // Different manual grade item for course 2. We are creating a course with a calculated grade item that has a grade max of
212 // 50. The grade_grade will have a rawgrademax of 100 regardless.
213 $gradeitem = new grade_item();
214 $gradeitem->itemname
= 'grade item one';
215 $gradeitem->itemtype
= 'manual';
216 $gradeitem->courseid
= $course2->id
;
217 $gradeitem->idnumber
= 'gi1';
218 $gradeitem->grademax
= 25;
219 $gradeitem->insert();
221 // Calculated grade item for course 2.
222 $calculatedgradeitem = new grade_item();
223 $calculatedgradeitem->itemname
= 'calculated grade';
224 $calculatedgradeitem->itemtype
= 'manual';
225 $calculatedgradeitem->courseid
= $course2->id
;
226 $calculatedgradeitem->calculation
= '=##gi' . $gradeitem->id
. '##*2';
227 $calculatedgradeitem->grademax
= 50;
228 $calculatedgradeitem->insert();
230 // Assigning a grade for the user.
231 $grade = $gradeitem->get_grade($user->id
, true);
232 $grade->finalgrade
= 10;
235 // Setting all of the grade_grade items.
236 grade_regrade_final_grades($course2->id
);
238 // Different manual grade item for course 3. We are creating a course with a calculated grade item that has a grade max of
239 // 50. The grade_grade will have a rawgrademax of 100 regardless.
240 $gradeitem = new grade_item();
241 $gradeitem->itemname
= 'grade item one';
242 $gradeitem->itemtype
= 'manual';
243 $gradeitem->courseid
= $course3->id
;
244 $gradeitem->idnumber
= 'gi1';
245 $gradeitem->grademax
= 25;
246 $gradeitem->insert();
248 // Calculated grade item for course 2.
249 $calculatedgradeitem = new grade_item();
250 $calculatedgradeitem->itemname
= 'calculated grade';
251 $calculatedgradeitem->itemtype
= 'manual';
252 $calculatedgradeitem->courseid
= $course3->id
;
253 $calculatedgradeitem->calculation
= '=##gi' . $gradeitem->id
. '##*2';
254 $calculatedgradeitem->grademax
= 50;
255 $calculatedgradeitem->insert();
257 // Assigning a grade for the user.
258 $grade = $gradeitem->get_grade($user->id
, true);
259 $grade->finalgrade
= 10;
262 // Setting all of the grade_grade items.
263 grade_regrade_final_grades($course3->id
);
264 // Need to do this first before changing the other courses, otherwise they will be flagged too early.
265 set_config('gradebook_calculations_freeze_' . $course3->id
, null);
266 upgrade_calculated_grade_items($course3->id
);
267 $this->assertEquals(20150627, $CFG->{'gradebook_calculations_freeze_' . $course3->id
});
269 // Change the setting back to null.
270 set_config('gradebook_calculations_freeze_' . $course1->id
, null);
271 set_config('gradebook_calculations_freeze_' . $course2->id
, null);
273 upgrade_calculated_grade_items();
274 // The setting should be set again after the upgrade.
275 $this->assertEquals(20150627, $CFG->{'gradebook_calculations_freeze_' . $course1->id
});
276 $this->assertEquals(20150627, $CFG->{'gradebook_calculations_freeze_' . $course2->id
});
280 * Test the upgrade function for final grade after setting grade max for category and grade item.
282 public function test_upgrade_update_category_grademax_regrade_final_grades() {
285 $this->resetAfterTest();
287 $generator = $this->getDataGenerator();
288 $user = $generator->create_user();
290 // Create a new course.
291 $course = $generator->create_course();
293 // Set the course aggregation to weighted mean of grades.
294 $unitcategory = \grade_category
::fetch_course_category($course->id
);
295 $unitcategory->aggregation
= GRADE_AGGREGATE_WEIGHTED_MEAN
;
296 $unitcategory->update();
298 // Set grade max for category.
299 $gradecategoryitem = grade_item
::fetch(array('iteminstance' => $unitcategory->id
));
300 $gradecategoryitem->grademax
= 50;
301 $gradecategoryitem->update();
303 // Make new grade item.
304 $gradeitem = new \
grade_item($generator->create_grade_item([
305 'itemname' => 'Grade item',
306 'idnumber' => 'git1',
307 'courseid' => $course->id
,
310 'aggregationcoef' => 100.0,
314 $grade = $gradeitem->get_grade($user->id
, true);
315 $grade->finalgrade
= 20;
318 $courseitem = \grade_item
::fetch(['courseid' => $course->id
, 'itemtype' => 'course']);
319 $gradeitem->force_regrading();
321 // Trigger regrade because the grade items needs to be updated.
322 grade_regrade_final_grades($course->id
);
324 $coursegrade = new \
grade_grade($courseitem->get_final($user->id
), false);
325 $this->assertEquals(20, $coursegrade->finalgrade
);
328 function test_upgrade_calculated_grade_items_regrade() {
331 $this->resetAfterTest();
333 require_once($CFG->libdir
. '/db/upgradelib.php');
336 $user = $this->getDataGenerator()->create_user();
339 $course = $this->getDataGenerator()->create_course();
341 // Enrol the user in the course.
342 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
343 $maninstance1 = $DB->get_record('enrol', array('courseid' => $course->id
, 'enrol' => 'manual'), '*', MUST_EXIST
);
344 $manual = enrol_get_plugin('manual');
345 $manual->enrol_user($maninstance1, $user->id
, $studentrole->id
);
347 set_config('upgrade_calculatedgradeitemsonlyregrade', 1);
349 // Creating a category for a grade item.
350 $gradecategory = new grade_category();
351 $gradecategory->fullname
= 'calculated grade category';
352 $gradecategory->courseid
= $course->id
;
353 $gradecategory->insert();
354 $gradecategoryid = $gradecategory->id
;
356 // This is a manual grade item.
357 $gradeitem = new grade_item();
358 $gradeitem->itemname
= 'grade item one';
359 $gradeitem->itemtype
= 'manual';
360 $gradeitem->categoryid
= $gradecategoryid;
361 $gradeitem->courseid
= $course->id
;
362 $gradeitem->idnumber
= 'gi1';
363 $gradeitem->insert();
365 // Changing the category into a calculated grade category.
366 $gradecategoryitem = grade_item
::fetch(array('iteminstance' => $gradecategory->id
));
367 $gradecategoryitem->calculation
= '=##gi' . $gradeitem->id
. '##/2';
368 $gradecategoryitem->grademax
= 50;
369 $gradecategoryitem->grademin
= 15;
370 $gradecategoryitem->update();
372 // Setting a grade for the student.
373 $grade = $gradeitem->get_grade($user->id
, true);
374 $grade->finalgrade
= 50;
377 grade_regrade_final_grades($course->id
);
378 $grade = grade_grade
::fetch(array('itemid' => $gradecategoryitem->id
, 'userid' => $user->id
));
379 $grade->rawgrademax
= 100;
380 $grade->rawgrademin
= 0;
382 $this->assertNotEquals($gradecategoryitem->grademax
, $grade->rawgrademax
);
383 $this->assertNotEquals($gradecategoryitem->grademin
, $grade->rawgrademin
);
385 // This is the function that we are testing. If we comment out this line, then the test fails because the grade items
386 // are not flagged for regrading.
387 upgrade_calculated_grade_items();
388 grade_regrade_final_grades($course->id
);
390 $grade = grade_grade
::fetch(array('itemid' => $gradecategoryitem->id
, 'userid' => $user->id
));
392 $this->assertEquals($gradecategoryitem->grademax
, $grade->rawgrademax
);
393 $this->assertEquals($gradecategoryitem->grademin
, $grade->rawgrademin
);
397 * Test that the upgrade script correctly flags courses to be frozen due to letter boundary problems.
399 public function test_upgrade_course_letter_boundary() {
401 $this->resetAfterTest(true);
403 require_once($CFG->libdir
. '/db/upgradelib.php');
406 $user = $this->getDataGenerator()->create_user();
408 // Create some courses.
411 for ($i = 0; $i < 45; $i++
) {
412 $course = $this->getDataGenerator()->create_course();
413 $context = context_course
::instance($course->id
);
414 if (in_array($i, array(2, 5, 10, 13, 14, 19, 23, 25, 30, 34, 36))) {
415 // Assign good letter boundaries.
416 $this->assign_good_letter_boundary($context->id
);
418 if (in_array($i, array(3, 6, 11, 15, 20, 24, 26, 31, 35))) {
419 // Assign bad letter boundaries.
420 $this->assign_bad_letter_boundary($context->id
);
423 if (in_array($i, array(3, 9, 10, 11, 18, 19, 20, 29, 30, 31, 40))) {
424 grade_set_setting($course->id
, 'displaytype', '3');
425 } else if (in_array($i, array(8, 17, 28))) {
426 grade_set_setting($course->id
, 'displaytype', '2');
429 if (in_array($i, array(37, 43))) {
431 grade_set_setting($course->id
, 'report_user_showlettergrade', '1');
432 } else if (in_array($i, array(38, 42))) {
434 grade_set_setting($course->id
, 'report_user_showlettergrade', '0');
437 $assignrow = $this->getDataGenerator()->create_module('assign', array('course' => $course->id
, 'name' => 'Test!'));
438 $gi = grade_item
::fetch(
439 array('itemtype' => 'mod',
440 'itemmodule' => 'assign',
441 'iteminstance' => $assignrow->id
,
442 'courseid' => $course->id
));
443 if (in_array($i, array(6, 13, 14, 15, 23, 24, 34, 35, 36, 41))) {
444 grade_item
::set_properties($gi, array('display' => 3));
446 } else if (in_array($i, array(12, 21, 32))) {
447 grade_item
::set_properties($gi, array('display' => 2));
450 $gradegrade = new grade_grade();
451 $gradegrade->itemid
= $gi->id
;
452 $gradegrade->userid
= $user->id
;
453 $gradegrade->rawgrade
= 55.5563;
454 $gradegrade->finalgrade
= 55.5563;
455 $gradegrade->rawgrademax
= 100;
456 $gradegrade->rawgrademin
= 0;
457 $gradegrade->timecreated
= time();
458 $gradegrade->timemodified
= time();
459 $gradegrade->insert();
461 $contexts[] = $context;
462 $courses[] = $course;
465 upgrade_course_letter_boundary();
467 // No system setting for grade letter boundaries.
468 // [0] A course with no letter boundaries.
469 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[0]->id
}));
470 // [1] A course with letter boundaries which are default.
471 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[1]->id
}));
472 // [2] A course with letter boundaries which are custom but not affected.
473 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[2]->id
}));
474 // [3] A course with letter boundaries which are custom and will be affected.
475 $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[3]->id
});
476 // [4] A course with no letter boundaries, but with a grade item with letter boundaries which are default.
477 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[4]->id
}));
478 // [5] A course with no letter boundaries, but with a grade item with letter boundaries which are not default, but not affected.
479 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[5]->id
}));
480 // [6] A course with no letter boundaries, but with a grade item with letter boundaries which are not default which will be affected.
481 $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[6]->id
});
483 // System setting for grade letter boundaries (default).
484 set_config('grade_displaytype', '3');
485 for ($i = 0; $i < 45; $i++
) {
486 unset_config('gradebook_calculations_freeze_' . $courses[$i]->id
);
488 upgrade_course_letter_boundary();
490 // [7] A course with no grade display settings for the course or grade items.
491 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[7]->id
}));
492 // [8] A course with grade display settings, but for something that isn't letters.
493 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[8]->id
}));
494 // [9] A course with grade display settings of letters which are default.
495 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[9]->id
}));
496 // [10] A course with grade display settings of letters which are not default, but not affected.
497 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[10]->id
}));
498 // [11] A course with grade display settings of letters which are not default, which will be affected.
499 $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[11]->id
});
500 // [12] A grade item with display settings that are not letters.
501 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[12]->id
}));
502 // [13] A grade item with display settings of letters which are default.
503 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[13]->id
}));
504 // [14] A grade item with display settings of letters which are not default, but not affected.
505 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[14]->id
}));
506 // [15] A grade item with display settings of letters which are not default, which will be affected.
507 $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[15]->id
});
509 // System setting for grade letter boundaries (custom with problem).
510 $systemcontext = context_system
::instance();
511 $this->assign_bad_letter_boundary($systemcontext->id
);
512 for ($i = 0; $i < 45; $i++
) {
513 unset_config('gradebook_calculations_freeze_' . $courses[$i]->id
);
515 upgrade_course_letter_boundary();
517 // [16] A course with no grade display settings for the course or grade items.
518 $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[16]->id
});
519 // [17] A course with grade display settings, but for something that isn't letters.
520 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[17]->id
}));
521 // [18] A course with grade display settings of letters which are default.
522 $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[18]->id
});
523 // [19] A course with grade display settings of letters which are not default, but not affected.
524 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[19]->id
}));
525 // [20] A course with grade display settings of letters which are not default, which will be affected.
526 $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[20]->id
});
527 // [21] A grade item with display settings which are not letters. Grade total will be affected so should be frozen.
528 $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[21]->id
});
529 // [22] A grade item with display settings of letters which are default.
530 $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[22]->id
});
531 // [23] A grade item with display settings of letters which are not default, but not affected. Course uses new letter boundary setting.
532 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[23]->id
}));
533 // [24] A grade item with display settings of letters which are not default, which will be affected.
534 $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[24]->id
});
535 // [25] A course which is using the default grade display setting, but has updated the grade letter boundary (not 57) Should not be frozen.
536 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[25]->id
}));
537 // [26] A course that is using the default display setting (letters) and altered the letter boundary with 57. Should be frozen.
538 $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[26]->id
});
540 // System setting not showing letters.
541 set_config('grade_displaytype', '2');
542 for ($i = 0; $i < 45; $i++
) {
543 unset_config('gradebook_calculations_freeze_' . $courses[$i]->id
);
545 upgrade_course_letter_boundary();
547 // [27] A course with no grade display settings for the course or grade items.
548 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[27]->id
}));
549 // [28] A course with grade display settings, but for something that isn't letters.
550 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[28]->id
}));
551 // [29] A course with grade display settings of letters which are default.
552 $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[29]->id
});
553 // [30] A course with grade display settings of letters which are not default, but not affected.
554 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[30]->id
}));
555 // [31] A course with grade display settings of letters which are not default, which will be affected.
556 $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[31]->id
});
557 // [32] A grade item with display settings which are not letters.
558 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[32]->id
}));
559 // [33] All system defaults.
560 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[33]->id
}));
561 // [34] A grade item with display settings of letters which are not default, but not affected. Course uses new letter boundary setting.
562 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[34]->id
}));
563 // [35] A grade item with display settings of letters which are not default, which will be affected.
564 $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[35]->id
});
565 // [36] A course with grade display settings of letters with modified and good boundary (not 57) Should not be frozen.
566 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[36]->id
}));
568 // Previous site conditions still exist.
569 for ($i = 0; $i < 45; $i++
) {
570 unset_config('gradebook_calculations_freeze_' . $courses[$i]->id
);
572 upgrade_course_letter_boundary();
574 // [37] Site setting for not showing the letter column and course setting set to show (frozen).
575 $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[37]->id
});
576 // [38] Site setting for not showing the letter column and course setting set to hide.
577 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[38]->id
}));
578 // [39] Site setting for not showing the letter column and course setting set to default.
579 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[39]->id
}));
580 // [40] Site setting for not showing the letter column and course setting set to default. Course display set to letters (frozen).
581 $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[40]->id
});
582 // [41] Site setting for not showing the letter column and course setting set to default. Grade item display set to letters (frozen).
583 $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[41]->id
});
585 // Previous site conditions still exist.
586 for ($i = 0; $i < 45; $i++
) {
587 unset_config('gradebook_calculations_freeze_' . $courses[$i]->id
);
589 set_config('grade_report_user_showlettergrade', '1');
590 upgrade_course_letter_boundary();
592 // [42] Site setting for showing the letter column, but course setting set to hide.
593 $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[42]->id
}));
594 // [43] Site setting for showing the letter column and course setting set to show (frozen).
595 $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[43]->id
});
596 // [44] Site setting for showing the letter column and course setting set to default (frozen).
597 $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[44]->id
});
601 * Test upgrade_letter_boundary_needs_freeze function.
603 public function test_upgrade_letter_boundary_needs_freeze() {
606 $this->resetAfterTest();
608 require_once($CFG->libdir
. '/db/upgradelib.php');
612 for ($i = 0; $i < 3; $i++
) {
613 $courses[] = $this->getDataGenerator()->create_course();
614 $contexts[] = context_course
::instance($courses[$i]->id
);
617 // Course one is not using a letter boundary.
618 $this->assertFalse(upgrade_letter_boundary_needs_freeze($contexts[0]));
620 // Let's make course 2 use the bad boundary.
621 $this->assign_bad_letter_boundary($contexts[1]->id
);
622 $this->assertTrue(upgrade_letter_boundary_needs_freeze($contexts[1]));
623 // Course 3 has letter boundaries that are fine.
624 $this->assign_good_letter_boundary($contexts[2]->id
);
625 $this->assertFalse(upgrade_letter_boundary_needs_freeze($contexts[2]));
626 // Try the system context not using a letter boundary.
627 $systemcontext = context_system
::instance();
628 $this->assertFalse(upgrade_letter_boundary_needs_freeze($systemcontext));
632 * Assigns letter boundaries with comparison problems.
634 * @param int $contextid Context ID.
636 private function assign_bad_letter_boundary($contextid) {
638 $newlettersscale = array(
639 array('contextid' => $contextid, 'lowerboundary' => 90.00000, 'letter' => 'A'),
640 array('contextid' => $contextid, 'lowerboundary' => 85.00000, 'letter' => 'A-'),
641 array('contextid' => $contextid, 'lowerboundary' => 80.00000, 'letter' => 'B+'),
642 array('contextid' => $contextid, 'lowerboundary' => 75.00000, 'letter' => 'B'),
643 array('contextid' => $contextid, 'lowerboundary' => 70.00000, 'letter' => 'B-'),
644 array('contextid' => $contextid, 'lowerboundary' => 65.00000, 'letter' => 'C+'),
645 array('contextid' => $contextid, 'lowerboundary' => 57.00000, 'letter' => 'C'),
646 array('contextid' => $contextid, 'lowerboundary' => 50.00000, 'letter' => 'C-'),
647 array('contextid' => $contextid, 'lowerboundary' => 40.00000, 'letter' => 'D+'),
648 array('contextid' => $contextid, 'lowerboundary' => 25.00000, 'letter' => 'D'),
649 array('contextid' => $contextid, 'lowerboundary' => 0.00000, 'letter' => 'F'),
652 $DB->delete_records('grade_letters', array('contextid' => $contextid));
653 foreach ($newlettersscale as $record) {
654 // There is no API to do this, so we have to manually insert into the database.
655 $DB->insert_record('grade_letters', $record);
660 * Assigns letter boundaries with no comparison problems.
662 * @param int $contextid Context ID.
664 private function assign_good_letter_boundary($contextid) {
666 $newlettersscale = array(
667 array('contextid' => $contextid, 'lowerboundary' => 90.00000, 'letter' => 'A'),
668 array('contextid' => $contextid, 'lowerboundary' => 85.00000, 'letter' => 'A-'),
669 array('contextid' => $contextid, 'lowerboundary' => 80.00000, 'letter' => 'B+'),
670 array('contextid' => $contextid, 'lowerboundary' => 75.00000, 'letter' => 'B'),
671 array('contextid' => $contextid, 'lowerboundary' => 70.00000, 'letter' => 'B-'),
672 array('contextid' => $contextid, 'lowerboundary' => 65.00000, 'letter' => 'C+'),
673 array('contextid' => $contextid, 'lowerboundary' => 54.00000, 'letter' => 'C'),
674 array('contextid' => $contextid, 'lowerboundary' => 50.00000, 'letter' => 'C-'),
675 array('contextid' => $contextid, 'lowerboundary' => 40.00000, 'letter' => 'D+'),
676 array('contextid' => $contextid, 'lowerboundary' => 25.00000, 'letter' => 'D'),
677 array('contextid' => $contextid, 'lowerboundary' => 0.00000, 'letter' => 'F'),
680 $DB->delete_records('grade_letters', array('contextid' => $contextid));
681 foreach ($newlettersscale as $record) {
682 // There is no API to do this, so we have to manually insert into the database.
683 $DB->insert_record('grade_letters', $record);
688 * Test libcurl custom check api.
690 public function test_check_libcurl_version() {
691 $supportedversion = 0x071304;
692 $curlinfo = curl_version();
693 $currentversion = $curlinfo['version_number'];
695 $result = new environment_results("custom_checks");
696 if ($currentversion < $supportedversion) {
697 $this->assertFalse(check_libcurl_version($result)->getStatus());
699 $this->assertNull(check_libcurl_version($result));
704 * Create a collection of test themes to test determining parent themes.
706 * @return Url to the path containing the test themes
708 public function create_testthemes() {
713 'config.php' => '<?php $THEME->name = "testtheme"; $THEME->parents = [""];',
715 'childoftesttheme' => [
716 'config.php' => '<?php $THEME->name = "childofboost"; $THEME->parents = ["testtheme"];',
719 'config.php' => '<?php $THEME->name = "infinite"; $THEME->parents = ["forever"];',
722 'config.php' => '<?php $THEME->name = "forever"; $THEME->parents = ["infinite", "childoftesttheme"];',
725 'config.php' => '<?php $THEME->name = "orphantheme"; $THEME->parents = [];',
728 'config.php' => '<?php $THEME->name = "loop"; $THEME->parents = ["around"];',
731 'config.php' => '<?php $THEME->name = "around"; $THEME->parents = ["loop"];',
733 'themewithbrokenparent' => [
734 'config.php' => '<?php $THEME->name = "orphantheme"; $THEME->parents = ["nonexistent", "testtheme"];',
737 $vthemedir = \org\bovigo\vfs\vfsStream
::setup('themes', null, $themedircontent);
739 return \org\bovigo\vfs\vfsStream
::url('themes');
743 * Data provider of serialized string.
747 public function serialized_strings_dataprovider() {
749 'A configuration that uses the old object' => [
750 'O:6:"object":3:{s:4:"text";s:32:"Nothing that anyone cares about.";s:5:"title";s:16:"Really old block";s:6:"format";s:1:"1";}',
752 'O:8:"stdClass":3:{s:4:"text";s:32:"Nothing that anyone cares about.";s:5:"title";s:16:"Really old block";s:6:"format";s:1:"1";}'
754 'A configuration that uses stdClass' => [
755 'O:8:"stdClass":5:{s:5:"title";s:4:"Tags";s:12:"numberoftags";s:2:"80";s:12:"showstandard";s:1:"0";s:3:"ctx";s:3:"289";s:3:"rec";s:1:"1";}',
757 'O:8:"stdClass":5:{s:5:"title";s:4:"Tags";s:12:"numberoftags";s:2:"80";s:12:"showstandard";s:1:"0";s:3:"ctx";s:3:"289";s:3:"rec";s:1:"1";}'
759 'A setting I saw when importing a course with blocks from 1.9' => [
764 'An object in an object' => [
765 'O:6:"object":2:{s:2:"id";i:5;s:5:"other";O:6:"object":1:{s:4:"text";s:13:"something new";}}',
767 'O:8:"stdClass":2:{s:2:"id";i:5;s:5:"other";O:8:"stdClass":1:{s:4:"text";s:13:"something new";}}'
769 'An array with an object in it' => [
770 'a:3:{s:4:"name";s:4:"Test";s:10:"additional";O:6:"object":2:{s:2:"id";i:5;s:4:"info";s:18:"text in the object";}s:4:"type";i:1;}',
772 'a:3:{s:4:"name";s:4:"Test";s:10:"additional";O:8:"stdClass":2:{s:2:"id";i:5;s:4:"info";s:18:"text in the object";}s:4:"type";i:1;}'
778 * Test that objects in serialized strings will be changed over to stdClass.
780 * @dataProvider serialized_strings_dataprovider
781 * @param string $initialstring The initial serialized setting.
782 * @param bool $expectededited If the string is expected to be edited.
783 * @param string $expectedresult The expected serialized setting to be returned.
785 public function test_upgrade_fix_serialized_objects($initialstring, $expectededited, $expectedresult) {
786 list($edited, $resultstring) = upgrade_fix_serialized_objects($initialstring);
787 $this->assertEquals($expectededited, $edited);
788 $this->assertEquals($expectedresult, $resultstring);
792 * Data provider for base64_encoded block instance config data.
794 public function encoded_strings_dataprovider() {
796 'Normal data using stdClass' => [
797 'Tzo4OiJzdGRDbGFzcyI6NTp7czo1OiJ0aXRsZSI7czo0OiJUYWdzIjtzOjEyOiJudW1iZXJvZnRhZ3MiO3M6MjoiODAiO3M6MTI6InNob3dzdGFuZGFyZCI7czoxOiIwIjtzOjM6ImN0eCI7czozOiIyODkiO3M6MzoicmVjIjtzOjE6IjEiO30=',
798 'Tzo4OiJzdGRDbGFzcyI6NTp7czo1OiJ0aXRsZSI7czo0OiJUYWdzIjtzOjEyOiJudW1iZXJvZnRhZ3MiO3M6MjoiODAiO3M6MTI6InNob3dzdGFuZGFyZCI7czoxOiIwIjtzOjM6ImN0eCI7czozOiIyODkiO3M6MzoicmVjIjtzOjE6IjEiO30='
800 'No data at all' => [
804 'Old data using object' => [
805 'Tzo2OiJvYmplY3QiOjM6e3M6NDoidGV4dCI7czozMjoiTm90aGluZyB0aGF0IGFueW9uZSBjYXJlcyBhYm91dC4iO3M6NToidGl0bGUiO3M6MTY6IlJlYWxseSBvbGQgYmxvY2siO3M6NjoiZm9ybWF0IjtzOjE6IjEiO30=',
806 'Tzo4OiJzdGRDbGFzcyI6Mzp7czo0OiJ0ZXh0IjtzOjMyOiJOb3RoaW5nIHRoYXQgYW55b25lIGNhcmVzIGFib3V0LiI7czo1OiJ0aXRsZSI7czoxNjoiUmVhbGx5IG9sZCBibG9jayI7czo2OiJmb3JtYXQiO3M6MToiMSI7fQ=='
812 * Check that orphaned files are deleted.
814 public function test_upgrade_delete_orphaned_file_records() {
816 require_once($CFG->dirroot
. '/repository/lib.php');
818 $this->resetAfterTest();
820 $generator = $this->getDataGenerator();
821 $user = $generator->create_user();
822 $this->setUser($user);
823 $usercontext = context_user
::instance($user->id
);
824 $syscontext = context_system
::instance();
826 $fs = get_file_storage();
828 $userrepository = array();
829 $newstoredfile = array();
830 $repositorypluginname = array('user', 'areafiles');
832 // Create two repositories with one file in each.
833 foreach ($repositorypluginname as $key => $value) {
834 // Override repository permission.
835 $capability = 'repository/' . $value . ':view';
836 $guestroleid = $DB->get_field('role', 'id', array('shortname' => 'guest'));
837 assign_capability($capability, CAP_ALLOW
, $guestroleid, $syscontext->id
, true);
840 $args['type'] = $value;
841 $repos = repository
::get_instances($args);
842 $userrepository[$key] = reset($repos);
844 $this->assertInstanceOf('repository', $userrepository[$key]);
847 $filearea = 'private';
850 $filename = 'userfile.txt';
853 'contextid' => $usercontext->id
,
854 'component' => $component,
855 'filearea' => $filearea,
857 'filepath' => $filepath,
858 'filename' => $filename,
861 $content = 'Test content';
862 $originalfile = $fs->create_file_from_string($filerecord, $content);
863 $this->assertInstanceOf('stored_file', $originalfile);
865 $newfilerecord = array(
866 'contextid' => $syscontext->id
,
867 'component' => 'core',
868 'filearea' => 'phpunit',
870 'filepath' => $filepath,
871 'filename' => $filename,
873 $ref = $fs->pack_reference($filerecord);
874 $newstoredfile[$key] = $fs->create_file_from_reference($newfilerecord, $userrepository[$key]->id
, $ref);
876 // Look for references by repository ID.
877 $files = $fs->get_external_files($userrepository[$key]->id
);
878 $file = reset($files);
879 $this->assertEquals($file, $newstoredfile[$key]);
882 // Make one file orphaned by deleting first repository.
883 $DB->delete_records('repository_instances', array('id' => $userrepository[0]->id
));
884 $DB->delete_records('repository_instance_config', array('instanceid' => $userrepository[0]->id
));
886 upgrade_delete_orphaned_file_records();
888 $files = $fs->get_external_files($userrepository[0]->id
);
889 $file = reset($files);
890 $this->assertFalse($file);
892 $files = $fs->get_external_files($userrepository[1]->id
);
893 $file = reset($files);
894 $this->assertEquals($file, $newstoredfile[1]);
898 * Test the functionality of {@link upgrade_core_licenses} function.
900 public function test_upgrade_core_licenses() {
903 $this->resetAfterTest();
905 // Emulate that upgrade is in process.
906 $CFG->upgraderunning
= time();
908 $deletedcorelicenseshortname = 'unknown';
909 $DB->delete_records('license', ['shortname' => $deletedcorelicenseshortname]);
911 upgrade_core_licenses();
913 $expectedshortnames = ['allrightsreserved', 'cc-4.0', 'cc-nc-4.0', 'cc-nc-nd-4.0', 'cc-nc-sa-4.0', 'cc-nd-4.0', 'cc-sa-4.0', 'public'];
914 $licenses = $DB->get_records('license');
916 foreach ($licenses as $license) {
917 $this->assertContains($license->shortname
, $expectedshortnames);
918 $this->assertObjectHasAttribute('custom', $license);
919 $this->assertObjectHasAttribute('sortorder', $license);
921 // A core license which was deleted prior to upgrade should not be reinstalled.
922 $actualshortnames = $DB->get_records_menu('license', null, '', 'id, shortname');
923 $this->assertNotContains($deletedcorelicenseshortname, $actualshortnames);
927 * Execute same problematic query from upgrade step.
931 public function run_upgrade_step_query() {
934 return $DB->execute("UPDATE {event} SET userid = 0 WHERE eventtype <> 'user' OR priority <> 0");
938 * Test the functionality of upgrade_calendar_events_status() function.
940 public function test_upgrade_calendar_events_status() {
942 $this->resetAfterTest();
943 $this->setAdminUser();
945 $events = create_standard_events(5);
946 $eventscount = count($events);
948 // Run same DB query as the problematic upgrade step.
949 $this->run_upgrade_step_query();
951 // Get the events info.
952 $status = upgrade_calendar_events_status(false);
957 'count' => $eventscount,
958 'bad' => $eventscount - 5, // Event count excluding user events.
960 'standard' => (object)[
961 'count' => $eventscount,
962 'bad' => $eventscount - 5, // Event count excluding user events.
966 $this->assertEquals($expected['standard']->count
, $status['standard']->count
);
967 $this->assertEquals($expected['standard']->bad
, $status['standard']->bad
);
968 $this->assertEquals($expected['total']->count
, $status['total']->count
);
969 $this->assertEquals($expected['total']->bad
, $status['total']->bad
);
973 * Test the functionality of upgrade_calendar_events_get_teacherid() function.
975 public function test_upgrade_calendar_events_get_teacherid() {
978 $this->resetAfterTest();
980 // Create a new course and enrol a user as editing teacher.
981 $generator = $this->getDataGenerator();
982 $course = $generator->create_course();
983 $teacher = $generator->create_and_enrol($course, 'editingteacher');
985 // There's a teacher enrolled in the course, return its user id.
986 $userid = upgrade_calendar_events_get_teacherid($course->id
);
988 // It should return the enrolled teacher by default.
989 $this->assertEquals($teacher->id
, $userid);
991 // Un-enrol teacher from course.
992 $instance = $DB->get_record('enrol', ['courseid' => $course->id
, 'enrol' => 'manual']);
993 enrol_get_plugin('manual')->unenrol_user($instance, $teacher->id
);
995 // Since there are no teachers enrolled in the course, fallback to admin user id.
996 $admin = get_admin();
997 $userid = upgrade_calendar_events_get_teacherid($course->id
);
998 $this->assertEquals($admin->id
, $userid);
1002 * Test the functionality of upgrade_calendar_standard_events_fix() function.
1004 public function test_upgrade_calendar_standard_events_fix() {
1006 $this->resetAfterTest();
1007 $this->setAdminUser();
1009 $events = create_standard_events(5);
1010 $eventscount = count($events);
1012 // Get the events info.
1013 $info = upgrade_calendar_events_status(false);
1015 // There should be no standard events to be fixed.
1016 $this->assertEquals(0, $info['standard']->bad
);
1018 // No events to be fixed, should return false.
1019 $this->assertFalse(upgrade_calendar_standard_events_fix($info['standard'], false));
1021 // Run same problematic DB query.
1022 $this->run_upgrade_step_query();
1024 // Get the events info.
1025 $info = upgrade_calendar_events_status(false);
1027 // There should be 20 events to be fixed (five from each type except user).
1028 $this->assertEquals($eventscount - 5, $info['standard']->bad
);
1030 // Test the function runtime, passing -1 as end time.
1031 // It should not be able to fix all events so fast, so some events should remain to be fixed in the next run.
1032 $result = upgrade_calendar_standard_events_fix($info['standard'], false, -1);
1033 $this->assertNotFalse($result);
1035 // Call the function again, this time it will run until all events have been fixed.
1036 $this->assertFalse(upgrade_calendar_standard_events_fix($info['standard'], false));
1038 // Get the events info again.
1039 $info = upgrade_calendar_events_status(false);
1041 // All standard events should have been recovered.
1042 // There should be no standard events flagged to be fixed.
1043 $this->assertEquals(0, $info['standard']->bad
);
1047 * Test the functionality of upgrade_calendar_subscription_events_fix() function.
1049 public function test_upgrade_calendar_subscription_events_fix() {
1052 require_once($CFG->dirroot
. '/calendar/lib.php');
1053 require_once($CFG->dirroot
. '/lib/bennu/bennu.inc.php');
1055 $this->resetAfterTest();
1056 $this->setAdminUser();
1058 // Create event subscription.
1059 $subscription = new stdClass
;
1060 $subscription->name
= 'Repeated events';
1061 $subscription->importfrom
= CALENDAR_IMPORT_FROM_FILE
;
1062 $subscription->eventtype
= 'site';
1063 $id = calendar_add_subscription($subscription);
1065 // Get repeated events ICS file.
1066 $calendar = file_get_contents($CFG->dirroot
. '/lib/tests/fixtures/repeated_events.ics');
1067 $ical = new iCalendar();
1068 $ical->unserialize($calendar);
1070 // Import subscription events.
1071 calendar_import_events_from_ical($ical, $id);
1073 // Subscription should have added 18 events.
1074 $eventscount = $DB->count_records('event');
1076 // Get the events info.
1077 $info = upgrade_calendar_events_status(false);
1079 // There should be no subscription events to be fixed at this point.
1080 $this->assertEquals(0, $info['subscription']->bad
);
1082 // No events to be fixed, should return false.
1083 $this->assertFalse(upgrade_calendar_subscription_events_fix($info['subscription'], false));
1085 // Run same problematic DB query.
1086 $this->run_upgrade_step_query();
1088 // Get the events info and assert total number of events is correct.
1089 $info = upgrade_calendar_events_status(false);
1090 $subscriptioninfo = $info['subscription'];
1092 $this->assertEquals($eventscount, $subscriptioninfo->count
);
1094 // Since we have added our subscription as site, all sub events have been affected.
1095 $this->assertEquals($eventscount, $subscriptioninfo->bad
);
1097 // Test the function runtime, passing -1 as end time.
1098 // It should not be able to fix all events so fast, so some events should remain to be fixed in the next run.
1099 $result = upgrade_calendar_subscription_events_fix($subscriptioninfo, false, -1);
1100 $this->assertNotFalse($result);
1102 // Call the function again, this time it will run until all events have been fixed.
1103 $this->assertFalse(upgrade_calendar_subscription_events_fix($subscriptioninfo, false));
1105 // Get the events info again.
1106 $info = upgrade_calendar_events_status(false);
1108 // All standard events should have been recovered.
1109 // There should be no standard events flagged to be fixed.
1110 $this->assertEquals(0, $info['subscription']->bad
);
1114 * Test the functionality of upgrade_calendar_action_events_fix() function.
1116 public function test_upgrade_calendar_action_events_fix() {
1119 $this->resetAfterTest();
1120 $this->setAdminUser();
1122 // Create a new course and a choice activity.
1123 $course = $this->getDataGenerator()->create_course();
1124 $choice = $this->getDataGenerator()->create_module('choice', ['course' => $course->id
]);
1126 // Create some action events.
1127 create_action_event(['courseid' => $course->id
, 'modulename' => 'choice', 'instance' => $choice->id
,
1128 'eventtype' => CHOICE_EVENT_TYPE_OPEN
]);
1129 create_action_event(['courseid' => $course->id
, 'modulename' => 'choice', 'instance' => $choice->id
,
1130 'eventtype' => CHOICE_EVENT_TYPE_CLOSE
]);
1132 $eventscount = $DB->count_records('event');
1134 // Get the events info.
1135 $info = upgrade_calendar_events_status(false);
1136 $actioninfo = $info['action'];
1138 // There should be no standard events to be fixed.
1139 $this->assertEquals(0, $actioninfo->bad
);
1141 // No events to be fixed, should return false.
1142 $this->assertFalse(upgrade_calendar_action_events_fix($actioninfo, false));
1144 // Run same problematic DB query.
1145 $this->run_upgrade_step_query();
1147 // Get the events info.
1148 $info = upgrade_calendar_events_status(false);
1149 $actioninfo = $info['action'];
1151 // There should be 2 events to be fixed.
1152 $this->assertEquals($eventscount, $actioninfo->bad
);
1154 // Test the function runtime, passing -1 as end time.
1155 // It should not be able to fix all events so fast, so some events should remain to be fixed in the next run.
1156 $this->assertNotFalse(upgrade_calendar_action_events_fix($actioninfo, false, -1));
1158 // Call the function again, this time it will run until all events have been fixed.
1159 $this->assertFalse(upgrade_calendar_action_events_fix($actioninfo, false));
1161 // Get the events info again.
1162 $info = upgrade_calendar_events_status(false);
1164 // All standard events should have been recovered.
1165 // There should be no standard events flagged to be fixed.
1166 $this->assertEquals(0, $info['action']->bad
);
1170 * Test the user override part of upgrade_calendar_override_events_fix() function.
1172 public function test_upgrade_calendar_user_override_events_fix() {
1175 $this->resetAfterTest();
1176 $this->setAdminUser();
1178 $generator = $this->getDataGenerator();
1180 // Create a new course.
1181 $course = $generator->create_course();
1183 // Create few users and enrol as students.
1184 $student1 = $generator->create_and_enrol($course, 'student');
1185 $student2 = $generator->create_and_enrol($course, 'student');
1186 $student3 = $generator->create_and_enrol($course, 'student');
1188 // Create some activities and some override events.
1189 foreach (['assign', 'lesson', 'quiz'] as $modulename) {
1190 $instance = $generator->create_module($modulename, ['course' => $course->id
]);
1191 create_user_override_event($modulename, $instance->id
, $student1->id
);
1192 create_user_override_event($modulename, $instance->id
, $student2->id
);
1193 create_user_override_event($modulename, $instance->id
, $student3->id
);
1196 // There should be 9 override events to be fixed (three from each module).
1197 $eventscount = $DB->count_records('event');
1198 $this->assertEquals(9, $eventscount);
1200 // Get the events info.
1201 $info = upgrade_calendar_events_status(false);
1202 $overrideinfo = $info['override'];
1204 // There should be no standard events to be fixed.
1205 $this->assertEquals(0, $overrideinfo->bad
);
1207 // No events to be fixed, should return false.
1208 $this->assertFalse(upgrade_calendar_override_events_fix($overrideinfo, false));
1210 // Run same problematic DB query.
1211 $this->run_upgrade_step_query();
1213 // Get the events info.
1214 $info = upgrade_calendar_events_status(false);
1215 $overrideinfo = $info['override'];
1217 // There should be 9 events to be fixed (three from each module).
1218 $this->assertEquals($eventscount, $overrideinfo->bad
);
1220 // Call the function again, this time it will run until all events have been fixed.
1221 $this->assertFalse(upgrade_calendar_override_events_fix($overrideinfo, false));
1223 // Get the events info again.
1224 $info = upgrade_calendar_events_status(false);
1226 // All standard events should have been recovered.
1227 // There should be no standard events flagged to be fixed.
1228 $this->assertEquals(0, $info['override']->bad
);
1232 * Test the group override part of upgrade_calendar_override_events_fix() function.
1234 public function test_upgrade_calendar_group_override_events_fix() {
1237 $this->resetAfterTest();
1238 $this->setAdminUser();
1240 $generator = $this->getDataGenerator();
1242 // Create a new course and few groups.
1243 $course = $generator->create_course();
1244 $group1 = $generator->create_group(['courseid' => $course->id
]);
1245 $group2 = $generator->create_group(['courseid' => $course->id
]);
1246 $group3 = $generator->create_group(['courseid' => $course->id
]);
1248 // Create some activities and some override events.
1249 foreach (['assign', 'lesson', 'quiz'] as $modulename) {
1250 $instance = $generator->create_module($modulename, ['course' => $course->id
]);
1251 create_group_override_event($modulename, $instance->id
, $course->id
, $group1->id
);
1252 create_group_override_event($modulename, $instance->id
, $course->id
, $group2->id
);
1253 create_group_override_event($modulename, $instance->id
, $course->id
, $group3->id
);
1256 // There should be 9 override events to be fixed (three from each module).
1257 $eventscount = $DB->count_records('event');
1258 $this->assertEquals(9, $eventscount);
1260 // Get the events info.
1261 $info = upgrade_calendar_events_status(false);
1263 // We classify group overrides as action events since they do not record the userid.
1264 $groupoverrideinfo = $info['action'];
1266 // There should be no events to be fixed.
1267 $this->assertEquals(0, $groupoverrideinfo->bad
);
1269 // No events to be fixed, should return false.
1270 $this->assertFalse(upgrade_calendar_action_events_fix($groupoverrideinfo, false));
1272 // Run same problematic DB query.
1273 $this->run_upgrade_step_query();
1275 // Get the events info.
1276 $info = upgrade_calendar_events_status(false);
1277 $this->assertEquals(9, $info['action']->bad
);
1279 // Call the function again, this time it will run until all events have been fixed.
1280 $this->assertFalse(upgrade_calendar_action_events_fix($info['action'], false));
1282 // Since group override events do not set userid, these events should not be flagged to be fixed.
1283 $this->assertEquals(0, $groupoverrideinfo->bad
);
1287 * Test the admin_dir_usage check with no admin setting specified.
1289 public function test_admin_dir_usage_not_set(): void
{
1290 $result = new environment_results("custom_checks");
1292 $this->assertNull(check_admin_dir_usage($result));
1296 * Test the admin_dir_usage check with the default admin setting specified.
1298 public function test_admin_dir_usage_is_default(): void
{
1301 $CFG->admin
= 'admin';
1303 $result = new environment_results("custom_checks");
1304 $this->assertNull(check_admin_dir_usage($result));
1308 * Test the admin_dir_usage check with a custom admin setting specified.
1310 public function test_admin_dir_usage_non_standard(): void
{
1313 $this->resetAfterTest(true);
1314 $CFG->admin
= 'notadmin';
1316 $result = new environment_results("custom_checks");
1317 $this->assertInstanceOf(environment_results
::class, check_admin_dir_usage($result));
1318 $this->assertEquals('admin_dir_usage', $result->getInfo());
1319 $this->assertFalse($result->getStatus());
1323 * Test the check_xmlrpc_usage check when the XML-RPC web service method is not set.
1327 public function test_check_xmlrpc_webservice_is_not_set(): void
{
1330 $this->resetAfterTest();
1332 $result = new environment_results('custom_checks');
1333 $this->assertNull(check_xmlrpc_usage($result));
1335 $CFG->webserviceprotocols
= 'rest';
1336 $result = new environment_results('custom_checks');
1337 $this->assertNull(check_xmlrpc_usage($result));
1341 * Test the check_xmlrpc_usage check when the XML-RPC web service method is set.
1345 public function test_check_xmlrpc_webservice_is_set(): void
{
1348 $this->resetAfterTest();
1349 $CFG->webserviceprotocols
= 'xmlrpc,rest';
1351 $result = new environment_results('custom_checks');
1352 $this->assertInstanceOf(environment_results
::class, check_xmlrpc_usage($result));
1353 $this->assertEquals('xmlrpc_webservice_usage', $result->getInfo());
1354 $this->assertFalse($result->getStatus());
1358 * Test the check_mod_assignment check if mod_assignment is still used.
1360 * @covers ::check_mod_assignment
1363 public function test_check_mod_assignment_is_used(): void
{
1366 $this->resetAfterTest();
1367 $result = new environment_results('custom_checks');
1369 if (file_exists("{$CFG->dirroot}/mod/assignment/version.php")) {
1370 // This is for when the test is run on sites where mod_assignment is most likely reinstalled.
1371 $this->assertNull(check_mod_assignment($result));
1373 // This is for when the test is run on sites with mod_assignment now gone.
1374 $this->assertFalse($DB->get_manager()->table_exists('assignment'));
1375 $this->assertNull(check_mod_assignment($result));
1377 // Then we can simulate a scenario here where the assignment records are still present during the upgrade
1378 // by recreating the assignment table and adding a record to it.
1379 $dbman = $DB->get_manager();
1380 $table = new xmldb_table('assignment');
1381 $table->add_field('id', XMLDB_TYPE_INTEGER
, '10', null, XMLDB_NOTNULL
, XMLDB_SEQUENCE
);
1382 $table->add_field('name', XMLDB_TYPE_CHAR
, '255');
1383 $table->add_key('primary', XMLDB_KEY_PRIMARY
, ['id']);
1384 $dbman->create_table($table);
1385 $DB->insert_record('assignment', (object)['name' => 'test_assign']);
1387 $this->assertNotNull(check_mod_assignment($result));
1388 $this->assertEquals('Assignment 2.2 is in use', $result->getInfo());
1389 $this->assertFalse($result->getStatus());
1394 * Data provider of usermenu items.
1398 public function usermenu_items_dataprovider(): array {
1400 'Add new item to empty usermenu' => [
1402 'reports,core_reportbuilder|/reportbuilder/index.php',
1403 'reports,core_reportbuilder|/reportbuilder/index.php',
1405 'Add new item to usermenu' => [
1406 'profile,moodle|/user/profile.php
1407 grades,grades|/grade/report/mygrades.php',
1408 'reports,core_reportbuilder|/reportbuilder/index.php',
1409 'profile,moodle|/user/profile.php
1410 grades,grades|/grade/report/mygrades.php
1411 reports,core_reportbuilder|/reportbuilder/index.php',
1413 'Add existing item to usermenu' => [
1414 'profile,moodle|/user/profile.php
1415 reports,core_reportbuilder|/reportbuilder/index.php
1416 calendar,core_calendar|/calendar/view.php?view=month',
1417 'reports,core_reportbuilder|/reportbuilder/index.php',
1418 'profile,moodle|/user/profile.php
1419 reports,core_reportbuilder|/reportbuilder/index.php
1420 calendar,core_calendar|/calendar/view.php?view=month',
1426 * Test the functionality of the {@link upgrade_add_item_to_usermenu()} function.
1428 * @covers ::upgrade_add_item_to_usermenu
1429 * @dataProvider usermenu_items_dataprovider
1431 public function test_upgrade_add_item_to_usermenu(string $initialmenu, string $newmenuitem, string $expectedmenu) {
1434 $this->resetAfterTest();
1435 // Set the base user menu.
1436 $CFG->customusermenuitems
= $initialmenu;
1438 // Add the new item to the user menu.
1439 upgrade_add_item_to_usermenu($newmenuitem);
1440 $newcustomusermenu = $CFG->customusermenuitems
;
1442 $this->assertEquals($expectedmenu, $newcustomusermenu);
1446 * Test that file timestamps are corrected for copied files.
1448 public function test_upgrade_fix_file_timestamps() {
1450 $this->resetAfterTest();
1452 // Add 2 files for testing, one with edited old timestamps.
1456 'component' => 'mod_label',
1457 'filearea' => 'intro',
1460 'filename' => 'file.txt',
1464 'component' => 'mod_label',
1465 'filearea' => 'intro',
1468 'filename' => 'file.txt',
1471 // Create the file records. This will create a directory listing with the current time.
1472 $fs = get_file_storage();
1473 $newfile = $fs->create_file_from_string($new, 'new');
1474 $oldfile = $fs->create_file_from_string($old, 'old');
1476 // Manually set the timestamps to use on files.
1477 $DB->set_field('files', 'timecreated', $origtime, [
1478 'contextid' => $newfile->get_contextid(),
1479 'component' => $newfile->get_component(),
1480 'filearea' => $newfile->get_filearea(),
1481 'itemid' => $newfile->get_itemid(),
1483 $DB->set_field('files', 'timemodified', $origtime, [
1484 'contextid' => $newfile->get_contextid(),
1485 'component' => $newfile->get_component(),
1486 'filearea' => $newfile->get_filearea(),
1487 'itemid' => $newfile->get_itemid(),
1490 $DB->set_field('files', 'timecreated', 1, ['id' => $oldfile->get_id()]);
1491 $DB->set_field('files', 'timemodified', 1, ['id' => $oldfile->get_id()]);
1493 upgrade_fix_file_timestamps();
1495 // Check nothing changed on the new file.
1496 $updatednew = $DB->get_record('files', ['id' => $newfile->get_id()]);
1497 $this->assertEquals($origtime, $updatednew->timecreated
);
1498 $this->assertEquals($origtime, $updatednew->timemodified
);
1500 // Confirm that the file with old timestamps has been fixed.
1501 $updatedold = $DB->get_record('files', ['id' => $oldfile->get_id()]);
1502 $this->assertNotEquals(1, $updatedold->timecreated
);
1503 $this->assertNotEquals(1, $updatedold->timemodified
);
1504 $this->assertTrue($updatedold->timecreated
>= $origtime);
1505 $this->assertTrue($updatedold->timemodified
>= $origtime);
1509 * Test the upgrade status check alongside the outageless flags.
1511 * @covers ::moodle_needs_upgrading
1513 public function test_moodle_upgrade_check_outageless() {
1515 $this->resetAfterTest();
1517 $this->assertFalse(moodle_needs_upgrading());
1519 // First lets check a plain upgrade ready.
1521 $this->assertTrue(moodle_needs_upgrading());
1523 // Now set the locking config and confirm we shouldn't upgrade.
1524 set_config('outagelessupgrade', true);
1525 $this->assertFalse(moodle_needs_upgrading());
1527 // Test the ignorelock flag is functioning.
1528 $this->assertTrue(moodle_needs_upgrading(false));
1532 * Test the upgrade status check alongside the outageless flags.
1534 * @covers ::upgrade_started
1536 public function test_moodle_start_upgrade_outageless() {
1538 $this->resetAfterTest();
1539 $this->assertObjectNotHasAttribute('upgraderunning', $CFG);
1541 // Confirm that starting normally sets the upgraderunning flag.
1543 $upgrade = get_config('core', 'upgraderunning');
1544 $this->assertTrue($upgrade > (time() - 5));
1546 // Confirm that the config flag doesnt affect the internal upgrade processes.
1547 unset($CFG->upgraderunning
);
1548 set_config('upgraderunning', null);
1549 set_config('outagelessupgrade', true);
1551 $upgrade = get_config('core', 'upgraderunning');
1552 $this->assertTrue($upgrade > (time() - 5));
1556 * Test the upgrade timeout setter alongside the outageless flags.
1558 * @covers ::upgrade_set_timeout
1560 public function test_moodle_set_upgrade_timeout_outageless() {
1562 $this->resetAfterTest();
1563 $this->assertObjectNotHasAttribute('upgraderunning', $CFG);
1565 // Confirm running normally sets the timeout.
1566 upgrade_set_timeout(120);
1567 $upgrade = get_config('core', 'upgraderunning');
1568 $this->assertTrue($upgrade > (time() - 5));
1570 // Confirm that the config flag doesnt affect the internal upgrade processes.
1571 unset($CFG->upgraderunning
);
1572 set_config('upgraderunning', null);
1573 set_config('outagelessupgrade', true);
1574 upgrade_set_timeout(120);
1575 $upgrade = get_config('core', 'upgraderunning');
1576 $this->assertTrue($upgrade > (time() - 5));
1580 * Test the components of the upgrade process being run outageless.
1582 * @covers ::moodle_needs_upgrading
1583 * @covers ::upgrade_started
1584 * @covers ::upgrade_set_timeout
1586 public function test_upgrade_components_with_outageless() {
1588 $this->resetAfterTest();
1590 // We can now define the outageless constant for use in upgrade, and test the effects.
1591 define('CLI_UPGRADE_RUNNING', true);
1593 // First test the upgrade check. Even when locked via config this should return true.
1594 // This can happen when attempting to fix a broken upgrade, so needs to work.
1595 set_config('outagelessupgrade', true);
1597 $this->assertTrue(moodle_needs_upgrading());
1599 // Now confirm that starting upgrade with the constant will not set the upgraderunning flag.
1600 set_config('upgraderunning', null);
1602 $upgrade = get_config('core', 'upgraderunning');
1603 $this->assertFalse($upgrade);
1605 // The same for timeouts, it should not be set if the constant is set.
1606 set_config('upgraderunning', null);
1607 upgrade_set_timeout(120);
1608 $upgrade = get_config('core', 'upgraderunning');
1609 $this->assertFalse($upgrade);