MDL-75553 mod_data: Fix wording of data fields in Behat tests
[moodle.git] / lib / tests / upgradelib_test.php
blobe6bc221f1c7df35841b6ae94fc8370f6cb25a438
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.
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/>.
17 /**
18 * Unit tests for the lib/upgradelib.php library.
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
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->libdir.'/upgradelib.php');
30 require_once($CFG->libdir.'/db/upgradelib.php');
31 require_once($CFG->dirroot . '/calendar/tests/helpers.php');
33 /**
34 * Tests various classes and functions in upgradelib.php library.
36 class upgradelib_test extends advanced_testcase {
38 /**
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());
47 /**
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
54 * purposes.
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) {
61 global $DB, $CFG;
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;
68 $item->grademin = 30;
69 $item->grademax = 110;
70 $item->itemnumber = 1;
71 $item->iteminfo = '';
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() {
81 global $DB, $CFG;
83 $this->resetAfterTest(true);
85 require_once($CFG->libdir . '/db/upgradelib.php');
87 $c = array();
88 $a = array();
89 $gi = array();
90 for ($i=0; $i<5; $i++) {
91 $c[$i] = $this->getDataGenerator()->create_course();
92 $a[$i] = array();
93 $gi[$i] = array();
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;
107 $gi[0][1]->update();
108 $gi[0][2]->weightoverride = 1;
109 $gi[0][2]->update();
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;
115 $gi[2][1]->update();
117 // Case 4: Course $c[3] has no extra credits and has overrides
118 $gi[3][2]->weightoverride = 1;
119 $gi[3][2]->update();
121 // Case 5: Course $c[4] has both extra credits and overrides
122 $gi[4][1]->aggregationcoef = 1;
123 $gi[4][1]->update();
124 $gi[4][2]->weightoverride = 1;
125 $gi[4][2]->update();
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() {
149 global $DB, $CFG;
151 $this->resetAfterTest();
153 require_once($CFG->libdir . '/db/upgradelib.php');
155 // Create a user.
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;
203 $grade->update();
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;
233 $grade->update();
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;
260 $grade->update();
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);
272 // Run the upgrade.
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() {
283 global $DB;
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,
308 'grademin' => 0,
309 'grademax' => 50,
310 'aggregationcoef' => 100.0,
311 ]));
313 // Set final grade.
314 $grade = $gradeitem->get_grade($user->id, true);
315 $grade->finalgrade = 20;
316 $grade->update();
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() {
329 global $DB, $CFG;
331 $this->resetAfterTest();
333 require_once($CFG->libdir . '/db/upgradelib.php');
335 // Create a user.
336 $user = $this->getDataGenerator()->create_user();
338 // Create a course.
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;
375 $grade->update();
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;
381 $grade->update();
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() {
400 global $CFG, $DB;
401 $this->resetAfterTest(true);
403 require_once($CFG->libdir . '/db/upgradelib.php');
405 // Create a user.
406 $user = $this->getDataGenerator()->create_user();
408 // Create some courses.
409 $courses = array();
410 $contexts = array();
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))) {
430 // Show.
431 grade_set_setting($course->id, 'report_user_showlettergrade', '1');
432 } else if (in_array($i, array(38, 42))) {
433 // Hide.
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));
445 $gi->update();
446 } else if (in_array($i, array(12, 21, 32))) {
447 grade_item::set_properties($gi, array('display' => 2));
448 $gi->update();
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() {
604 global $CFG;
606 $this->resetAfterTest();
608 require_once($CFG->libdir . '/db/upgradelib.php');
610 $courses = array();
611 $contexts = array();
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) {
637 global $DB;
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) {
665 global $DB;
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());
698 } else {
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() {
709 global $CFG;
711 $themedircontent = [
712 'testtheme' => [
713 'config.php' => '<?php $THEME->name = "testtheme"; $THEME->parents = [""];',
715 'childoftesttheme' => [
716 'config.php' => '<?php $THEME->name = "childofboost"; $THEME->parents = ["testtheme"];',
718 'infinite' => [
719 'config.php' => '<?php $THEME->name = "infinite"; $THEME->parents = ["forever"];',
721 'forever' => [
722 'config.php' => '<?php $THEME->name = "forever"; $THEME->parents = ["infinite", "childoftesttheme"];',
724 'orphantheme' => [
725 'config.php' => '<?php $THEME->name = "orphantheme"; $THEME->parents = [];',
727 'loop' => [
728 'config.php' => '<?php $THEME->name = "loop"; $THEME->parents = ["around"];',
730 '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.
745 * @return array
747 public function serialized_strings_dataprovider() {
748 return [
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";}',
751 true,
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";}',
756 false,
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' => [
760 'N;',
761 false,
762 'N;'
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";}}',
766 true,
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;}',
771 true,
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() {
795 return [
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() {
815 global $DB, $CFG;
816 require_once($CFG->dirroot . '/repository/lib.php');
818 $this->resetAfterTest();
819 // Create user.
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);
839 $args = array();
840 $args['type'] = $value;
841 $repos = repository::get_instances($args);
842 $userrepository[$key] = reset($repos);
844 $this->assertInstanceOf('repository', $userrepository[$key]);
846 $component = 'user';
847 $filearea = 'private';
848 $itemid = $key;
849 $filepath = '/';
850 $filename = 'userfile.txt';
852 $filerecord = array(
853 'contextid' => $usercontext->id,
854 'component' => $component,
855 'filearea' => $filearea,
856 'itemid' => $itemid,
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',
869 'itemid' => $key,
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 that the previous records are updated according to the reworded actions.
899 * @return null
901 public function test_upgrade_rename_prediction_actions_useful_incorrectly_flagged() {
902 global $DB;
904 $this->resetAfterTest();
905 $this->setAdminUser();
907 $models = $DB->get_records('analytics_models');
908 $upcomingactivitiesdue = null;
909 $noteaching = null;
910 foreach ($models as $model) {
911 if ($model->target === '\\core_user\\analytics\\target\\upcoming_activities_due') {
912 $upcomingactivitiesdue = new \core_analytics\model($model);
914 if ($model->target === '\\core_course\\analytics\\target\\no_teaching') {
915 $noteaching = new \core_analytics\model($model);
919 // Upcoming activities due generating some insights.
920 $course1 = $this->getDataGenerator()->create_course();
921 $attrs = ['course' => $course1, 'duedate' => time() + WEEKSECS - DAYSECS];
922 $assign = $this->getDataGenerator()->get_plugin_generator('mod_assign')->create_instance($attrs);
923 $student = $this->getDataGenerator()->create_user();
924 $usercontext = \context_user::instance($student->id);
925 $this->getDataGenerator()->enrol_user($student->id, $course1->id, 'student');
926 $upcomingactivitiesdue->predict();
927 list($ignored, $predictions) = $upcomingactivitiesdue->get_predictions($usercontext, true);
928 $prediction = reset($predictions);
930 $predictionaction = (object)[
931 'predictionid' => $prediction->get_prediction_data()->id,
932 'userid' => 2,
933 'actionname' => 'fixed',
934 'timecreated' => time()
936 $DB->insert_record('analytics_prediction_actions', $predictionaction);
937 $predictionaction->actionname = 'notuseful';
938 $DB->insert_record('analytics_prediction_actions', $predictionaction);
940 upgrade_rename_prediction_actions_useful_incorrectly_flagged();
942 $this->assertEquals(0, $DB->count_records('analytics_prediction_actions',
943 ['actionname' => \core_analytics\prediction::ACTION_FIXED]));
944 $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
945 ['actionname' => \core_analytics\prediction::ACTION_USEFUL]));
946 $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
947 ['actionname' => \core_analytics\prediction::ACTION_NOT_USEFUL]));
948 $this->assertEquals(0, $DB->count_records('analytics_prediction_actions',
949 ['actionname' => \core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED]));
951 // No teaching generating some insights.
952 $course2 = $this->getDataGenerator()->create_course(['startdate' => time() + (2 * DAYSECS)]);
953 $noteaching->predict();
954 list($ignored, $predictions) = $noteaching->get_predictions(\context_system::instance(), true);
955 $prediction = reset($predictions);
957 $predictionaction = (object)[
958 'predictionid' => $prediction->get_prediction_data()->id,
959 'userid' => 2,
960 'actionname' => 'notuseful',
961 'timecreated' => time()
963 $DB->insert_record('analytics_prediction_actions', $predictionaction);
964 $predictionaction->actionname = 'fixed';
965 $DB->insert_record('analytics_prediction_actions', $predictionaction);
967 upgrade_rename_prediction_actions_useful_incorrectly_flagged();
969 $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
970 ['actionname' => \core_analytics\prediction::ACTION_FIXED]));
971 $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
972 ['actionname' => \core_analytics\prediction::ACTION_USEFUL]));
973 $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
974 ['actionname' => \core_analytics\prediction::ACTION_NOT_USEFUL]));
975 $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
976 ['actionname' => \core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED]));
978 // We also check that there are no records incorrectly switched in upcomingactivitiesdue.
979 $upcomingactivitiesdue->clear();
981 $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
982 ['actionname' => \core_analytics\prediction::ACTION_FIXED]));
983 $this->assertEquals(0, $DB->count_records('analytics_prediction_actions',
984 ['actionname' => \core_analytics\prediction::ACTION_USEFUL]));
985 $this->assertEquals(0, $DB->count_records('analytics_prediction_actions',
986 ['actionname' => \core_analytics\prediction::ACTION_NOT_USEFUL]));
987 $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
988 ['actionname' => \core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED]));
990 $upcomingactivitiesdue->predict();
991 list($ignored, $predictions) = $upcomingactivitiesdue->get_predictions($usercontext, true);
992 $prediction = reset($predictions);
994 $predictionaction = (object)[
995 'predictionid' => $prediction->get_prediction_data()->id,
996 'userid' => 2,
997 'actionname' => 'fixed',
998 'timecreated' => time()
1000 $DB->insert_record('analytics_prediction_actions', $predictionaction);
1001 $predictionaction->actionname = 'notuseful';
1002 $DB->insert_record('analytics_prediction_actions', $predictionaction);
1004 upgrade_rename_prediction_actions_useful_incorrectly_flagged();
1006 $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
1007 ['actionname' => \core_analytics\prediction::ACTION_FIXED]));
1008 $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
1009 ['actionname' => \core_analytics\prediction::ACTION_USEFUL]));
1010 $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
1011 ['actionname' => \core_analytics\prediction::ACTION_NOT_USEFUL]));
1012 $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
1013 ['actionname' => \core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED]));
1017 * Test the functionality of the {@link upgrade_convert_hub_config_site_param_names()} function.
1019 public function test_upgrade_convert_hub_config_site_param_names() {
1021 $config = (object) [
1022 // This is how site settings related to registration at https://moodle.net are stored.
1023 'site_name_httpsmoodlenet' => 'Foo Site',
1024 'site_language_httpsmoodlenet' => 'en',
1025 'site_emailalert_httpsmoodlenet' => 1,
1026 // These are unexpected relics of a value as registered at the old http://hub.moodle.org site.
1027 'site_name_httphubmoodleorg' => 'Bar Site',
1028 'site_description_httphubmoodleorg' => 'Old description',
1029 // This is the target value we are converting to - here it already somehow exists.
1030 'site_emailalert' => 0,
1031 // This is a setting not related to particular hub.
1032 'custom' => 'Do not touch this',
1033 // A setting defined for multiple alternative hubs.
1034 'site_foo_httpfirsthuborg' => 'First',
1035 'site_foo_httpanotherhubcom' => 'Another',
1036 'site_foo_httpyetanotherhubcom' => 'Yet another',
1037 // A setting defined for multiple alternative hubs and one referential one.
1038 'site_bar_httpfirsthuborg' => 'First',
1039 'site_bar_httpanotherhubcom' => 'Another',
1040 'site_bar_httpsmoodlenet' => 'One hub to rule them all!',
1041 'site_bar_httpyetanotherhubcom' => 'Yet another',
1044 $converted = upgrade_convert_hub_config_site_param_names($config, 'https://moodle.net');
1046 // Values defined for the moodle.net take precedence over the ones defined for other hubs.
1047 $this->assertSame($converted->site_name, 'Foo Site');
1048 $this->assertSame($converted->site_bar, 'One hub to rule them all!');
1049 $this->assertNull($converted->site_name_httpsmoodlenet);
1050 $this->assertNull($converted->site_bar_httpfirsthuborg);
1051 $this->assertNull($converted->site_bar_httpanotherhubcom);
1052 $this->assertNull($converted->site_bar_httpyetanotherhubcom);
1053 // Values defined for alternative hubs only do not have any guaranteed value. Just for convenience, we use the first one.
1054 $this->assertSame($converted->site_foo, 'First');
1055 $this->assertNull($converted->site_foo_httpfirsthuborg);
1056 $this->assertNull($converted->site_foo_httpanotherhubcom);
1057 $this->assertNull($converted->site_foo_httpyetanotherhubcom);
1058 // Values that are already defined with the new name format are kept.
1059 $this->assertSame($converted->site_emailalert, 0);
1060 // Eventual custom values not following the expected hub-specific naming format, are kept.
1061 $this->assertSame($converted->custom, 'Do not touch this');
1065 * Test the functionality of the {@link upgrade_analytics_fix_contextids_defaults} function.
1067 public function test_upgrade_analytics_fix_contextids_defaults() {
1068 global $DB, $USER;
1070 $this->resetAfterTest();
1072 $model = (object)[
1073 'name' => 'asd',
1074 'target' => 'ou',
1075 'indicators' => '[]',
1076 'version' => '1',
1077 'timecreated' => time(),
1078 'timemodified' => time(),
1079 'usermodified' => $USER->id,
1080 'contextids' => ''
1082 $DB->insert_record('analytics_models', $model);
1084 $model->contextids = null;
1085 $DB->insert_record('analytics_models', $model);
1087 unset($model->contextids);
1088 $DB->insert_record('analytics_models', $model);
1090 $model->contextids = '0';
1091 $DB->insert_record('analytics_models', $model);
1093 $model->contextids = 'null';
1094 $DB->insert_record('analytics_models', $model);
1096 $select = $DB->sql_compare_text('contextids') . ' = :zero OR ' . $DB->sql_compare_text('contextids') . ' = :null';
1097 $params = ['zero' => '0', 'null' => 'null'];
1098 $this->assertEquals(2, $DB->count_records_select('analytics_models', $select, $params));
1100 upgrade_analytics_fix_contextids_defaults();
1102 $this->assertEquals(0, $DB->count_records_select('analytics_models', $select, $params));
1106 * Test the functionality of {@link upgrade_core_licenses} function.
1108 public function test_upgrade_core_licenses() {
1109 global $CFG, $DB;
1111 $this->resetAfterTest();
1113 // Emulate that upgrade is in process.
1114 $CFG->upgraderunning = time();
1116 $deletedcorelicenseshortname = 'unknown';
1117 $DB->delete_records('license', ['shortname' => $deletedcorelicenseshortname]);
1119 upgrade_core_licenses();
1121 $expectedshortnames = ['allrightsreserved', 'cc', 'cc-nc', 'cc-nc-nd', 'cc-nc-sa', 'cc-nd', 'cc-sa', 'public'];
1122 $licenses = $DB->get_records('license');
1124 foreach ($licenses as $license) {
1125 $this->assertContains($license->shortname, $expectedshortnames);
1126 $this->assertObjectHasAttribute('custom', $license);
1127 $this->assertObjectHasAttribute('sortorder', $license);
1129 // A core license which was deleted prior to upgrade should not be reinstalled.
1130 $actualshortnames = $DB->get_records_menu('license', null, '', 'id, shortname');
1131 $this->assertNotContains($deletedcorelicenseshortname, $actualshortnames);
1135 * Execute same problematic query from upgrade step.
1137 * @return bool
1139 public function run_upgrade_step_query() {
1140 global $DB;
1142 return $DB->execute("UPDATE {event} SET userid = 0 WHERE eventtype <> 'user' OR priority <> 0");
1146 * Test the functionality of upgrade_calendar_events_status() function.
1148 public function test_upgrade_calendar_events_status() {
1150 $this->resetAfterTest();
1151 $this->setAdminUser();
1153 $events = create_standard_events(5);
1154 $eventscount = count($events);
1156 // Run same DB query as the problematic upgrade step.
1157 $this->run_upgrade_step_query();
1159 // Get the events info.
1160 $status = upgrade_calendar_events_status(false);
1162 // Total events.
1163 $expected = [
1164 'total' => (object)[
1165 'count' => $eventscount,
1166 'bad' => $eventscount - 5, // Event count excluding user events.
1168 'standard' => (object)[
1169 'count' => $eventscount,
1170 'bad' => $eventscount - 5, // Event count excluding user events.
1174 $this->assertEquals($expected['standard']->count, $status['standard']->count);
1175 $this->assertEquals($expected['standard']->bad, $status['standard']->bad);
1176 $this->assertEquals($expected['total']->count, $status['total']->count);
1177 $this->assertEquals($expected['total']->bad, $status['total']->bad);
1181 * Test the functionality of upgrade_calendar_events_get_teacherid() function.
1183 public function test_upgrade_calendar_events_get_teacherid() {
1184 global $DB;
1186 $this->resetAfterTest();
1188 // Create a new course and enrol a user as editing teacher.
1189 $generator = $this->getDataGenerator();
1190 $course = $generator->create_course();
1191 $teacher = $generator->create_and_enrol($course, 'editingteacher');
1193 // There's a teacher enrolled in the course, return its user id.
1194 $userid = upgrade_calendar_events_get_teacherid($course->id);
1196 // It should return the enrolled teacher by default.
1197 $this->assertEquals($teacher->id, $userid);
1199 // Un-enrol teacher from course.
1200 $instance = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'manual']);
1201 enrol_get_plugin('manual')->unenrol_user($instance, $teacher->id);
1203 // Since there are no teachers enrolled in the course, fallback to admin user id.
1204 $admin = get_admin();
1205 $userid = upgrade_calendar_events_get_teacherid($course->id);
1206 $this->assertEquals($admin->id, $userid);
1210 * Test the functionality of upgrade_calendar_standard_events_fix() function.
1212 public function test_upgrade_calendar_standard_events_fix() {
1214 $this->resetAfterTest();
1215 $this->setAdminUser();
1217 $events = create_standard_events(5);
1218 $eventscount = count($events);
1220 // Get the events info.
1221 $info = upgrade_calendar_events_status(false);
1223 // There should be no standard events to be fixed.
1224 $this->assertEquals(0, $info['standard']->bad);
1226 // No events to be fixed, should return false.
1227 $this->assertFalse(upgrade_calendar_standard_events_fix($info['standard'], false));
1229 // Run same problematic DB query.
1230 $this->run_upgrade_step_query();
1232 // Get the events info.
1233 $info = upgrade_calendar_events_status(false);
1235 // There should be 20 events to be fixed (five from each type except user).
1236 $this->assertEquals($eventscount - 5, $info['standard']->bad);
1238 // Test the function runtime, passing -1 as end time.
1239 // It should not be able to fix all events so fast, so some events should remain to be fixed in the next run.
1240 $result = upgrade_calendar_standard_events_fix($info['standard'], false, -1);
1241 $this->assertNotFalse($result);
1243 // Call the function again, this time it will run until all events have been fixed.
1244 $this->assertFalse(upgrade_calendar_standard_events_fix($info['standard'], false));
1246 // Get the events info again.
1247 $info = upgrade_calendar_events_status(false);
1249 // All standard events should have been recovered.
1250 // There should be no standard events flagged to be fixed.
1251 $this->assertEquals(0, $info['standard']->bad);
1255 * Test the functionality of upgrade_calendar_subscription_events_fix() function.
1257 public function test_upgrade_calendar_subscription_events_fix() {
1258 global $CFG, $DB;
1260 require_once($CFG->dirroot . '/calendar/lib.php');
1261 require_once($CFG->dirroot . '/lib/bennu/bennu.inc.php');
1263 $this->resetAfterTest();
1264 $this->setAdminUser();
1266 // Create event subscription.
1267 $subscription = new stdClass;
1268 $subscription->name = 'Repeated events';
1269 $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
1270 $subscription->eventtype = 'site';
1271 $id = calendar_add_subscription($subscription);
1273 // Get repeated events ICS file.
1274 $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/repeated_events.ics');
1275 $ical = new iCalendar();
1276 $ical->unserialize($calendar);
1278 // Import subscription events.
1279 calendar_import_icalendar_events($ical, null, $id);
1281 // Subscription should have added 18 events.
1282 $eventscount = $DB->count_records('event');
1284 // Get the events info.
1285 $info = upgrade_calendar_events_status(false);
1287 // There should be no subscription events to be fixed at this point.
1288 $this->assertEquals(0, $info['subscription']->bad);
1290 // No events to be fixed, should return false.
1291 $this->assertFalse(upgrade_calendar_subscription_events_fix($info['subscription'], false));
1293 // Run same problematic DB query.
1294 $this->run_upgrade_step_query();
1296 // Get the events info and assert total number of events is correct.
1297 $info = upgrade_calendar_events_status(false);
1298 $subscriptioninfo = $info['subscription'];
1300 $this->assertEquals($eventscount, $subscriptioninfo->count);
1302 // Since we have added our subscription as site, all sub events have been affected.
1303 $this->assertEquals($eventscount, $subscriptioninfo->bad);
1305 // Test the function runtime, passing -1 as end time.
1306 // It should not be able to fix all events so fast, so some events should remain to be fixed in the next run.
1307 $result = upgrade_calendar_subscription_events_fix($subscriptioninfo, false, -1);
1308 $this->assertNotFalse($result);
1310 // Call the function again, this time it will run until all events have been fixed.
1311 $this->assertFalse(upgrade_calendar_subscription_events_fix($subscriptioninfo, false));
1313 // Get the events info again.
1314 $info = upgrade_calendar_events_status(false);
1316 // All standard events should have been recovered.
1317 // There should be no standard events flagged to be fixed.
1318 $this->assertEquals(0, $info['subscription']->bad);
1322 * Test the functionality of upgrade_calendar_action_events_fix() function.
1324 public function test_upgrade_calendar_action_events_fix() {
1325 global $DB;
1327 $this->resetAfterTest();
1328 $this->setAdminUser();
1330 // Create a new course and a choice activity.
1331 $course = $this->getDataGenerator()->create_course();
1332 $choice = $this->getDataGenerator()->create_module('choice', ['course' => $course->id]);
1334 // Create some action events.
1335 create_action_event(['courseid' => $course->id, 'modulename' => 'choice', 'instance' => $choice->id,
1336 'eventtype' => CHOICE_EVENT_TYPE_OPEN]);
1337 create_action_event(['courseid' => $course->id, 'modulename' => 'choice', 'instance' => $choice->id,
1338 'eventtype' => CHOICE_EVENT_TYPE_CLOSE]);
1340 $eventscount = $DB->count_records('event');
1342 // Get the events info.
1343 $info = upgrade_calendar_events_status(false);
1344 $actioninfo = $info['action'];
1346 // There should be no standard events to be fixed.
1347 $this->assertEquals(0, $actioninfo->bad);
1349 // No events to be fixed, should return false.
1350 $this->assertFalse(upgrade_calendar_action_events_fix($actioninfo, false));
1352 // Run same problematic DB query.
1353 $this->run_upgrade_step_query();
1355 // Get the events info.
1356 $info = upgrade_calendar_events_status(false);
1357 $actioninfo = $info['action'];
1359 // There should be 2 events to be fixed.
1360 $this->assertEquals($eventscount, $actioninfo->bad);
1362 // Test the function runtime, passing -1 as end time.
1363 // It should not be able to fix all events so fast, so some events should remain to be fixed in the next run.
1364 $this->assertNotFalse(upgrade_calendar_action_events_fix($actioninfo, false, -1));
1366 // Call the function again, this time it will run until all events have been fixed.
1367 $this->assertFalse(upgrade_calendar_action_events_fix($actioninfo, false));
1369 // Get the events info again.
1370 $info = upgrade_calendar_events_status(false);
1372 // All standard events should have been recovered.
1373 // There should be no standard events flagged to be fixed.
1374 $this->assertEquals(0, $info['action']->bad);
1378 * Test the user override part of upgrade_calendar_override_events_fix() function.
1380 public function test_upgrade_calendar_user_override_events_fix() {
1381 global $DB;
1383 $this->resetAfterTest();
1384 $this->setAdminUser();
1386 $generator = $this->getDataGenerator();
1388 // Create a new course.
1389 $course = $generator->create_course();
1391 // Create few users and enrol as students.
1392 $student1 = $generator->create_and_enrol($course, 'student');
1393 $student2 = $generator->create_and_enrol($course, 'student');
1394 $student3 = $generator->create_and_enrol($course, 'student');
1396 // Create some activities and some override events.
1397 foreach (['assign', 'lesson', 'quiz'] as $modulename) {
1398 $instance = $generator->create_module($modulename, ['course' => $course->id]);
1399 create_user_override_event($modulename, $instance->id, $student1->id);
1400 create_user_override_event($modulename, $instance->id, $student2->id);
1401 create_user_override_event($modulename, $instance->id, $student3->id);
1404 // There should be 9 override events to be fixed (three from each module).
1405 $eventscount = $DB->count_records('event');
1406 $this->assertEquals(9, $eventscount);
1408 // Get the events info.
1409 $info = upgrade_calendar_events_status(false);
1410 $overrideinfo = $info['override'];
1412 // There should be no standard events to be fixed.
1413 $this->assertEquals(0, $overrideinfo->bad);
1415 // No events to be fixed, should return false.
1416 $this->assertFalse(upgrade_calendar_override_events_fix($overrideinfo, false));
1418 // Run same problematic DB query.
1419 $this->run_upgrade_step_query();
1421 // Get the events info.
1422 $info = upgrade_calendar_events_status(false);
1423 $overrideinfo = $info['override'];
1425 // There should be 9 events to be fixed (three from each module).
1426 $this->assertEquals($eventscount, $overrideinfo->bad);
1428 // Call the function again, this time it will run until all events have been fixed.
1429 $this->assertFalse(upgrade_calendar_override_events_fix($overrideinfo, false));
1431 // Get the events info again.
1432 $info = upgrade_calendar_events_status(false);
1434 // All standard events should have been recovered.
1435 // There should be no standard events flagged to be fixed.
1436 $this->assertEquals(0, $info['override']->bad);
1440 * Test the group override part of upgrade_calendar_override_events_fix() function.
1442 public function test_upgrade_calendar_group_override_events_fix() {
1443 global $DB;
1445 $this->resetAfterTest();
1446 $this->setAdminUser();
1448 $generator = $this->getDataGenerator();
1450 // Create a new course and few groups.
1451 $course = $generator->create_course();
1452 $group1 = $generator->create_group(['courseid' => $course->id]);
1453 $group2 = $generator->create_group(['courseid' => $course->id]);
1454 $group3 = $generator->create_group(['courseid' => $course->id]);
1456 // Create some activities and some override events.
1457 foreach (['assign', 'lesson', 'quiz'] as $modulename) {
1458 $instance = $generator->create_module($modulename, ['course' => $course->id]);
1459 create_group_override_event($modulename, $instance->id, $course->id, $group1->id);
1460 create_group_override_event($modulename, $instance->id, $course->id, $group2->id);
1461 create_group_override_event($modulename, $instance->id, $course->id, $group3->id);
1464 // There should be 9 override events to be fixed (three from each module).
1465 $eventscount = $DB->count_records('event');
1466 $this->assertEquals(9, $eventscount);
1468 // Get the events info.
1469 $info = upgrade_calendar_events_status(false);
1471 // We classify group overrides as action events since they do not record the userid.
1472 $groupoverrideinfo = $info['action'];
1474 // There should be no events to be fixed.
1475 $this->assertEquals(0, $groupoverrideinfo->bad);
1477 // No events to be fixed, should return false.
1478 $this->assertFalse(upgrade_calendar_action_events_fix($groupoverrideinfo, false));
1480 // Run same problematic DB query.
1481 $this->run_upgrade_step_query();
1483 // Get the events info.
1484 $info = upgrade_calendar_events_status(false);
1485 $this->assertEquals(9, $info['action']->bad);
1487 // Call the function again, this time it will run until all events have been fixed.
1488 $this->assertFalse(upgrade_calendar_action_events_fix($info['action'], false));
1490 // Since group override events do not set userid, these events should not be flagged to be fixed.
1491 $this->assertEquals(0, $groupoverrideinfo->bad);
1495 * Test the admin_dir_usage check with no admin setting specified.
1497 public function test_admin_dir_usage_not_set(): void {
1498 $result = new environment_results("custom_checks");
1500 $this->assertNull(check_admin_dir_usage($result));
1504 * Test the admin_dir_usage check with the default admin setting specified.
1506 public function test_admin_dir_usage_is_default(): void {
1507 global $CFG;
1509 $CFG->admin = 'admin';
1511 $result = new environment_results("custom_checks");
1512 $this->assertNull(check_admin_dir_usage($result));
1516 * Test the admin_dir_usage check with a custom admin setting specified.
1518 public function test_admin_dir_usage_non_standard(): void {
1519 global $CFG;
1521 $this->resetAfterTest(true);
1522 $CFG->admin = 'notadmin';
1524 $result = new environment_results("custom_checks");
1525 $this->assertInstanceOf(environment_results::class, check_admin_dir_usage($result));
1526 $this->assertEquals('admin_dir_usage', $result->getInfo());
1527 $this->assertFalse($result->getStatus());