Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [Versions 402 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Unit tests for the lib/upgradelib.php library.
  19   *
  20   * @package   core
  21   * @category  phpunit
  22   * @copyright 2013 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  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');
  32  
  33  /**
  34   * Tests various classes and functions in upgradelib.php library.
  35   */
  36  class upgradelib_test extends advanced_testcase {
  37  
  38      /**
  39       * Test the {@link upgrade_stale_php_files_present() function
  40       */
  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());
  45      }
  46  
  47      /**
  48       * Populate some fake grade items into the database with specified
  49       * sortorder and course id.
  50       *
  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.
  55       *
  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.
  59       */
  60      private function insert_fake_grade_item_sortorder($courseid, $sortorder) {
  61          global $DB, $CFG;
  62          require_once($CFG->libdir.'/gradelib.php');
  63  
  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();
  74  
  75          $item->id = $DB->insert_record('grade_items', $item);
  76  
  77          return $DB->get_record('grade_items', array('id' => $item->id));
  78      }
  79  
  80      public function test_upgrade_extra_credit_weightoverride() {
  81          global $DB, $CFG;
  82  
  83          $this->resetAfterTest(true);
  84  
  85          require_once($CFG->libdir . '/db/upgradelib.php');
  86  
  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);
  99              }
 100          }
 101  
 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();
 110  
 111          // Case 2: Course $c[1] has neither extra credits nor overrides
 112  
 113          // Case 3: Course $c[2] has extra credits but no overrides
 114          $gi[2][1]->aggregationcoef = 1;
 115          $gi[2][1]->update();
 116  
 117          // Case 4: Course $c[3] has no extra credits and has overrides
 118          $gi[3][2]->weightoverride = 1;
 119          $gi[3][2]->update();
 120  
 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();
 126  
 127          // Run the upgrade script and make sure only course $c[4] was marked as needed to be fixed.
 128          upgrade_extra_credit_weightoverride();
 129  
 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});
 135  
 136          set_config('gradebook_calculations_freeze_' . $c[4]->id, null);
 137  
 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});
 143      }
 144  
 145      /**
 146       * Test the upgrade function for flagging courses with calculated grade item problems.
 147       */
 148      public function test_upgrade_calculated_grade_items_freeze() {
 149          global $DB, $CFG;
 150  
 151          $this->resetAfterTest();
 152  
 153          require_once($CFG->libdir . '/db/upgradelib.php');
 154  
 155          // Create a user.
 156          $user = $this->getDataGenerator()->create_user();
 157  
 158          // Create a couple of courses.
 159          $course1 = $this->getDataGenerator()->create_course();
 160          $course2 = $this->getDataGenerator()->create_course();
 161          $course3 = $this->getDataGenerator()->create_course();
 162  
 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);
 172  
 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;
 178  
 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;
 185  
 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();
 194  
 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();
 199  
 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();
 210  
 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();
 220  
 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();
 229  
 230          // Assigning a grade for the user.
 231          $grade = $gradeitem->get_grade($user->id, true);
 232          $grade->finalgrade = 10;
 233          $grade->update();
 234  
 235          // Setting all of the grade_grade items.
 236          grade_regrade_final_grades($course2->id);
 237  
 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();
 247  
 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();
 256  
 257          // Assigning a grade for the user.
 258          $grade = $gradeitem->get_grade($user->id, true);
 259          $grade->finalgrade = 10;
 260          $grade->update();
 261  
 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});
 268  
 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});
 277      }
 278  
 279      /**
 280       * Test the upgrade function for final grade after setting grade max for category and grade item.
 281       */
 282      public function test_upgrade_update_category_grademax_regrade_final_grades() {
 283          global $DB;
 284  
 285          $this->resetAfterTest();
 286  
 287          $generator = $this->getDataGenerator();
 288          $user = $generator->create_user();
 289  
 290          // Create a new course.
 291          $course = $generator->create_course();
 292  
 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();
 297  
 298          // Set grade max for category.
 299          $gradecategoryitem = grade_item::fetch(array('iteminstance' => $unitcategory->id));
 300          $gradecategoryitem->grademax = 50;
 301          $gradecategoryitem->update();
 302  
 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          ]));
 312  
 313          // Set final grade.
 314          $grade = $gradeitem->get_grade($user->id, true);
 315          $grade->finalgrade = 20;
 316          $grade->update();
 317  
 318          $courseitem = \grade_item::fetch(['courseid' => $course->id, 'itemtype' => 'course']);
 319          $gradeitem->force_regrading();
 320  
 321          // Trigger regrade because the grade items needs to be updated.
 322          grade_regrade_final_grades($course->id);
 323  
 324          $coursegrade = new \grade_grade($courseitem->get_final($user->id), false);
 325          $this->assertEquals(20, $coursegrade->finalgrade);
 326      }
 327  
 328      function test_upgrade_calculated_grade_items_regrade() {
 329          global $DB, $CFG;
 330  
 331          $this->resetAfterTest();
 332  
 333          require_once($CFG->libdir . '/db/upgradelib.php');
 334  
 335          // Create a user.
 336          $user = $this->getDataGenerator()->create_user();
 337  
 338          // Create a course.
 339          $course = $this->getDataGenerator()->create_course();
 340  
 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);
 346  
 347          set_config('upgrade_calculatedgradeitemsonlyregrade', 1);
 348  
 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;
 355  
 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();
 364  
 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();
 371  
 372          // Setting a grade for the student.
 373          $grade = $gradeitem->get_grade($user->id, true);
 374          $grade->finalgrade = 50;
 375          $grade->update();
 376  
 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);
 384  
 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);
 389  
 390          $grade = grade_grade::fetch(array('itemid' => $gradecategoryitem->id, 'userid' => $user->id));
 391  
 392          $this->assertEquals($gradecategoryitem->grademax, $grade->rawgrademax);
 393          $this->assertEquals($gradecategoryitem->grademin, $grade->rawgrademin);
 394      }
 395  
 396      /**
 397       * Test that the upgrade script correctly flags courses to be frozen due to letter boundary problems.
 398       */
 399      public function test_upgrade_course_letter_boundary() {
 400          global $CFG, $DB;
 401          $this->resetAfterTest(true);
 402  
 403          require_once($CFG->libdir . '/db/upgradelib.php');
 404  
 405          // Create a user.
 406          $user = $this->getDataGenerator()->create_user();
 407  
 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);
 417              }
 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);
 421              }
 422  
 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');
 427              }
 428  
 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');
 435              }
 436  
 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();
 449              }
 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();
 460  
 461              $contexts[] = $context;
 462              $courses[] = $course;
 463          }
 464  
 465          upgrade_course_letter_boundary();
 466  
 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});
 482  
 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);
 487          }
 488          upgrade_course_letter_boundary();
 489  
 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});
 508  
 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);
 514          }
 515          upgrade_course_letter_boundary();
 516  
 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});
 539  
 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);
 544          }
 545          upgrade_course_letter_boundary();
 546  
 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}));
 567  
 568          // Previous site conditions still exist.
 569          for ($i = 0; $i < 45; $i++) {
 570              unset_config('gradebook_calculations_freeze_' . $courses[$i]->id);
 571          }
 572          upgrade_course_letter_boundary();
 573  
 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});
 584  
 585          // Previous site conditions still exist.
 586          for ($i = 0; $i < 45; $i++) {
 587              unset_config('gradebook_calculations_freeze_' . $courses[$i]->id);
 588          }
 589          set_config('grade_report_user_showlettergrade', '1');
 590          upgrade_course_letter_boundary();
 591  
 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});
 598      }
 599  
 600      /**
 601       * Test upgrade_letter_boundary_needs_freeze function.
 602       */
 603      public function test_upgrade_letter_boundary_needs_freeze() {
 604          global $CFG;
 605  
 606          $this->resetAfterTest();
 607  
 608          require_once($CFG->libdir . '/db/upgradelib.php');
 609  
 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);
 615          }
 616  
 617          // Course one is not using a letter boundary.
 618          $this->assertFalse(upgrade_letter_boundary_needs_freeze($contexts[0]));
 619  
 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));
 629      }
 630  
 631      /**
 632       * Assigns letter boundaries with comparison problems.
 633       *
 634       * @param int $contextid Context ID.
 635       */
 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'),
 650              );
 651  
 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);
 656          }
 657      }
 658  
 659      /**
 660       * Assigns letter boundaries with no comparison problems.
 661       *
 662       * @param int $contextid Context ID.
 663       */
 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'),
 678              );
 679  
 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);
 684          }
 685      }
 686  
 687      /**
 688       * Test libcurl custom check api.
 689       */
 690      public function test_check_libcurl_version() {
 691          $supportedversion = 0x071304;
 692          $curlinfo = curl_version();
 693          $currentversion = $curlinfo['version_number'];
 694  
 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));
 700          }
 701      }
 702  
 703      /**
 704       * Create a collection of test themes to test determining parent themes.
 705       *
 706       * @return Url to the path containing the test themes
 707       */
 708      public function create_testthemes() {
 709          global $CFG;
 710  
 711          $themedircontent = [
 712              'testtheme' => [
 713                  'config.php' => '<?php $THEME->name = "testtheme"; $THEME->parents = [""];',
 714              ],
 715              'childoftesttheme' => [
 716                  'config.php' => '<?php $THEME->name = "childofboost"; $THEME->parents = ["testtheme"];',
 717              ],
 718              'infinite' => [
 719                  'config.php' => '<?php $THEME->name = "infinite"; $THEME->parents = ["forever"];',
 720              ],
 721              'forever' => [
 722                  'config.php' => '<?php $THEME->name = "forever"; $THEME->parents = ["infinite", "childoftesttheme"];',
 723              ],
 724              'orphantheme' => [
 725                  'config.php' => '<?php $THEME->name = "orphantheme"; $THEME->parents = [];',
 726              ],
 727              'loop' => [
 728                  'config.php' => '<?php $THEME->name = "loop"; $THEME->parents = ["around"];',
 729              ],
 730              'around' => [
 731                  'config.php' => '<?php $THEME->name = "around"; $THEME->parents = ["loop"];',
 732              ],
 733              'themewithbrokenparent' => [
 734                  'config.php' => '<?php $THEME->name = "orphantheme"; $THEME->parents = ["nonexistent", "testtheme"];',
 735              ],
 736          ];
 737          $vthemedir = \org\bovigo\vfs\vfsStream::setup('themes', null, $themedircontent);
 738  
 739          return \org\bovigo\vfs\vfsStream::url('themes');
 740      }
 741  
 742      /**
 743       * Data provider of serialized string.
 744       *
 745       * @return array
 746       */
 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";}'
 753              ],
 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";}'
 758              ],
 759              'A setting I saw when importing a course with blocks from 1.9' => [
 760                  'N;',
 761                  false,
 762                  'N;'
 763              ],
 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";}}'
 768              ],
 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;}'
 773              ]
 774          ];
 775      }
 776  
 777      /**
 778       * Test that objects in serialized strings will be changed over to stdClass.
 779       *
 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.
 784       */
 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);
 789      }
 790  
 791      /**
 792       * Data provider for base64_encoded block instance config data.
 793       */
 794      public function encoded_strings_dataprovider() {
 795          return [
 796              'Normal data using stdClass' => [
 797                  'Tzo4OiJzdGRDbGFzcyI6NTp7czo1OiJ0aXRsZSI7czo0OiJUYWdzIjtzOjEyOiJudW1iZXJvZnRhZ3MiO3M6MjoiODAiO3M6MTI6InNob3dzdGFuZGFyZCI7czoxOiIwIjtzOjM6ImN0eCI7czozOiIyODkiO3M6MzoicmVjIjtzOjE6IjEiO30=',
 798                  'Tzo4OiJzdGRDbGFzcyI6NTp7czo1OiJ0aXRsZSI7czo0OiJUYWdzIjtzOjEyOiJudW1iZXJvZnRhZ3MiO3M6MjoiODAiO3M6MTI6InNob3dzdGFuZGFyZCI7czoxOiIwIjtzOjM6ImN0eCI7czozOiIyODkiO3M6MzoicmVjIjtzOjE6IjEiO30='
 799              ],
 800              'No data at all' => [
 801                  '',
 802                  ''
 803              ],
 804              'Old data using object' => [
 805                  'Tzo2OiJvYmplY3QiOjM6e3M6NDoidGV4dCI7czozMjoiTm90aGluZyB0aGF0IGFueW9uZSBjYXJlcyBhYm91dC4iO3M6NToidGl0bGUiO3M6MTY6IlJlYWxseSBvbGQgYmxvY2siO3M6NjoiZm9ybWF0IjtzOjE6IjEiO30=',
 806                  'Tzo4OiJzdGRDbGFzcyI6Mzp7czo0OiJ0ZXh0IjtzOjMyOiJOb3RoaW5nIHRoYXQgYW55b25lIGNhcmVzIGFib3V0LiI7czo1OiJ0aXRsZSI7czoxNjoiUmVhbGx5IG9sZCBibG9jayI7czo2OiJmb3JtYXQiO3M6MToiMSI7fQ=='
 807              ]
 808          ];
 809      }
 810  
 811      /**
 812       * Check that orphaned files are deleted.
 813       */
 814      public function test_upgrade_delete_orphaned_file_records() {
 815          global $DB, $CFG;
 816          require_once($CFG->dirroot . '/repository/lib.php');
 817  
 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();
 825  
 826          $fs = get_file_storage();
 827  
 828          $userrepository = array();
 829          $newstoredfile = array();
 830          $repositorypluginname = array('user', 'areafiles');
 831  
 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);
 838  
 839              $args = array();
 840              $args['type'] = $value;
 841              $repos = repository::get_instances($args);
 842              $userrepository[$key] = reset($repos);
 843  
 844              $this->assertInstanceOf('repository', $userrepository[$key]);
 845  
 846              $component = 'user';
 847              $filearea  = 'private';
 848              $itemid    = $key;
 849              $filepath  = '/';
 850              $filename  = 'userfile.txt';
 851  
 852              $filerecord = array(
 853                  'contextid' => $usercontext->id,
 854                  'component' => $component,
 855                  'filearea'  => $filearea,
 856                  'itemid'    => $itemid,
 857                  'filepath'  => $filepath,
 858                  'filename'  => $filename,
 859              );
 860  
 861              $content = 'Test content';
 862              $originalfile = $fs->create_file_from_string($filerecord, $content);
 863              $this->assertInstanceOf('stored_file', $originalfile);
 864  
 865              $newfilerecord = array(
 866                  'contextid' => $syscontext->id,
 867                  'component' => 'core',
 868                  'filearea'  => 'phpunit',
 869                  'itemid'    => $key,
 870                  'filepath'  => $filepath,
 871                  'filename'  => $filename,
 872              );
 873              $ref = $fs->pack_reference($filerecord);
 874              $newstoredfile[$key] = $fs->create_file_from_reference($newfilerecord, $userrepository[$key]->id, $ref);
 875  
 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]);
 880          }
 881  
 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));
 885  
 886          upgrade_delete_orphaned_file_records();
 887  
 888          $files = $fs->get_external_files($userrepository[0]->id);
 889          $file = reset($files);
 890          $this->assertFalse($file);
 891  
 892          $files = $fs->get_external_files($userrepository[1]->id);
 893          $file = reset($files);
 894          $this->assertEquals($file, $newstoredfile[1]);
 895      }
 896  
 897      /**
 898       * Test the functionality of {@link upgrade_core_licenses} function.
 899       */
 900      public function test_upgrade_core_licenses() {
 901          global $CFG, $DB;
 902  
 903          $this->resetAfterTest();
 904  
 905          // Emulate that upgrade is in process.
 906          $CFG->upgraderunning = time();
 907  
 908          $deletedcorelicenseshortname = 'unknown';
 909          $DB->delete_records('license', ['shortname' => $deletedcorelicenseshortname]);
 910  
 911          upgrade_core_licenses();
 912  
 913          $expectedshortnames = [
 914              'allrightsreserved',
 915              'cc-4.0',
 916              'cc-nc-4.0',
 917              'cc-nc-nd-4.0',
 918              'cc-nc-sa-4.0',
 919              'cc-nd-4.0',
 920              'cc-sa-4.0',
 921              'public',
 922          ];
 923          $licenses = $DB->get_records('license');
 924  
 925          foreach ($licenses as $license) {
 926              $this->assertContains($license->shortname, $expectedshortnames);
 927              $this->assertObjectHasAttribute('custom', $license);
 928              $this->assertObjectHasAttribute('sortorder', $license);
 929          }
 930          // A core license which was deleted prior to upgrade should not be reinstalled.
 931          $actualshortnames = $DB->get_records_menu('license', null, '', 'id, shortname');
 932          $this->assertNotContains($deletedcorelicenseshortname, $actualshortnames);
 933      }
 934  
 935      /**
 936       * Execute same problematic query from upgrade step.
 937       *
 938       * @return bool
 939       */
 940      public function run_upgrade_step_query() {
 941          global $DB;
 942  
 943          return $DB->execute("UPDATE {event} SET userid = 0 WHERE eventtype <> 'user' OR priority <> 0");
 944      }
 945  
 946      /**
 947       * Test the functionality of upgrade_calendar_events_status() function.
 948       */
 949      public function test_upgrade_calendar_events_status() {
 950  
 951          $this->resetAfterTest();
 952          $this->setAdminUser();
 953  
 954          $events = create_standard_events(5);
 955          $eventscount = count($events);
 956  
 957          // Run same DB query as the problematic upgrade step.
 958          $this->run_upgrade_step_query();
 959  
 960          // Get the events info.
 961          $status = upgrade_calendar_events_status(false);
 962  
 963          // Total events.
 964          $expected = [
 965              'total' => (object)[
 966                  'count' => $eventscount,
 967                  'bad' => $eventscount - 5, // Event count excluding user events.
 968              ],
 969              'standard' => (object)[
 970                  'count' => $eventscount,
 971                  'bad' => $eventscount - 5, // Event count excluding user events.
 972              ],
 973          ];
 974  
 975          $this->assertEquals($expected['standard']->count, $status['standard']->count);
 976          $this->assertEquals($expected['standard']->bad, $status['standard']->bad);
 977          $this->assertEquals($expected['total']->count, $status['total']->count);
 978          $this->assertEquals($expected['total']->bad, $status['total']->bad);
 979      }
 980  
 981      /**
 982       * Test the functionality of upgrade_calendar_events_get_teacherid() function.
 983       */
 984      public function test_upgrade_calendar_events_get_teacherid() {
 985          global $DB;
 986  
 987          $this->resetAfterTest();
 988  
 989          // Create a new course and enrol a user as editing teacher.
 990          $generator = $this->getDataGenerator();
 991          $course = $generator->create_course();
 992          $teacher = $generator->create_and_enrol($course, 'editingteacher');
 993  
 994          // There's a teacher enrolled in the course, return its user id.
 995          $userid = upgrade_calendar_events_get_teacherid($course->id);
 996  
 997          // It should return the enrolled teacher by default.
 998          $this->assertEquals($teacher->id, $userid);
 999  
1000          // Un-enrol teacher from course.
1001          $instance = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'manual']);
1002          enrol_get_plugin('manual')->unenrol_user($instance, $teacher->id);
1003  
1004          // Since there are no teachers enrolled in the course, fallback to admin user id.
1005          $admin = get_admin();
1006          $userid = upgrade_calendar_events_get_teacherid($course->id);
1007          $this->assertEquals($admin->id, $userid);
1008      }
1009  
1010      /**
1011       * Test the functionality of upgrade_calendar_standard_events_fix() function.
1012       */
1013      public function test_upgrade_calendar_standard_events_fix() {
1014  
1015          $this->resetAfterTest();
1016          $this->setAdminUser();
1017  
1018          $events = create_standard_events(5);
1019          $eventscount = count($events);
1020  
1021          // Get the events info.
1022          $info = upgrade_calendar_events_status(false);
1023  
1024          // There should be no standard events to be fixed.
1025          $this->assertEquals(0, $info['standard']->bad);
1026  
1027          // No events to be fixed, should return false.
1028          $this->assertFalse(upgrade_calendar_standard_events_fix($info['standard'], false));
1029  
1030          // Run same problematic DB query.
1031          $this->run_upgrade_step_query();
1032  
1033          // Get the events info.
1034          $info = upgrade_calendar_events_status(false);
1035  
1036          // There should be 20 events to be fixed (five from each type except user).
1037          $this->assertEquals($eventscount - 5, $info['standard']->bad);
1038  
1039          // Test the function runtime, passing -1 as end time.
1040          // It should not be able to fix all events so fast, so some events should remain to be fixed in the next run.
1041          $result = upgrade_calendar_standard_events_fix($info['standard'], false, -1);
1042          $this->assertNotFalse($result);
1043  
1044          // Call the function again, this time it will run until all events have been fixed.
1045          $this->assertFalse(upgrade_calendar_standard_events_fix($info['standard'], false));
1046  
1047          // Get the events info again.
1048          $info = upgrade_calendar_events_status(false);
1049  
1050          // All standard events should have been recovered.
1051          // There should be no standard events flagged to be fixed.
1052          $this->assertEquals(0, $info['standard']->bad);
1053      }
1054  
1055      /**
1056       * Test the functionality of upgrade_calendar_subscription_events_fix() function.
1057       */
1058      public function test_upgrade_calendar_subscription_events_fix() {
1059          global $CFG, $DB;
1060  
1061          require_once($CFG->dirroot . '/calendar/lib.php');
1062          require_once($CFG->dirroot . '/lib/bennu/bennu.inc.php');
1063  
1064          $this->resetAfterTest();
1065          $this->setAdminUser();
1066  
1067          // Create event subscription.
1068          $subscription = new stdClass;
1069          $subscription->name = 'Repeated events';
1070          $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
1071          $subscription->eventtype = 'site';
1072          $id = calendar_add_subscription($subscription);
1073  
1074          // Get repeated events ICS file.
1075          $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/repeated_events.ics');
1076          $ical = new iCalendar();
1077          $ical->unserialize($calendar);
1078  
1079          // Import subscription events.
1080          calendar_import_events_from_ical($ical, $id);
1081  
1082          // Subscription should have added 18 events.
1083          $eventscount = $DB->count_records('event');
1084  
1085          // Get the events info.
1086          $info = upgrade_calendar_events_status(false);
1087  
1088          // There should be no subscription events to be fixed at this point.
1089          $this->assertEquals(0, $info['subscription']->bad);
1090  
1091          // No events to be fixed, should return false.
1092          $this->assertFalse(upgrade_calendar_subscription_events_fix($info['subscription'], false));
1093  
1094          // Run same problematic DB query.
1095          $this->run_upgrade_step_query();
1096  
1097          // Get the events info and assert total number of events is correct.
1098          $info = upgrade_calendar_events_status(false);
1099          $subscriptioninfo = $info['subscription'];
1100  
1101          $this->assertEquals($eventscount, $subscriptioninfo->count);
1102  
1103          // Since we have added our subscription as site, all sub events have been affected.
1104          $this->assertEquals($eventscount, $subscriptioninfo->bad);
1105  
1106          // Test the function runtime, passing -1 as end time.
1107          // It should not be able to fix all events so fast, so some events should remain to be fixed in the next run.
1108          $result = upgrade_calendar_subscription_events_fix($subscriptioninfo, false, -1);
1109          $this->assertNotFalse($result);
1110  
1111          // Call the function again, this time it will run until all events have been fixed.
1112          $this->assertFalse(upgrade_calendar_subscription_events_fix($subscriptioninfo, false));
1113  
1114          // Get the events info again.
1115          $info = upgrade_calendar_events_status(false);
1116  
1117          // All standard events should have been recovered.
1118          // There should be no standard events flagged to be fixed.
1119          $this->assertEquals(0, $info['subscription']->bad);
1120      }
1121  
1122      /**
1123       * Test the functionality of upgrade_calendar_action_events_fix() function.
1124       */
1125      public function test_upgrade_calendar_action_events_fix() {
1126          global $DB;
1127  
1128          $this->resetAfterTest();
1129          $this->setAdminUser();
1130  
1131          // Create a new course and a choice activity.
1132          $course = $this->getDataGenerator()->create_course();
1133          $choice = $this->getDataGenerator()->create_module('choice', ['course' => $course->id]);
1134  
1135          // Create some action events.
1136          create_action_event(['courseid' => $course->id, 'modulename' => 'choice', 'instance' => $choice->id,
1137              'eventtype' => CHOICE_EVENT_TYPE_OPEN]);
1138          create_action_event(['courseid' => $course->id, 'modulename' => 'choice', 'instance' => $choice->id,
1139              'eventtype' => CHOICE_EVENT_TYPE_CLOSE]);
1140  
1141          $eventscount = $DB->count_records('event');
1142  
1143          // Get the events info.
1144          $info = upgrade_calendar_events_status(false);
1145          $actioninfo = $info['action'];
1146  
1147          // There should be no standard events to be fixed.
1148          $this->assertEquals(0, $actioninfo->bad);
1149  
1150          // No events to be fixed, should return false.
1151          $this->assertFalse(upgrade_calendar_action_events_fix($actioninfo, false));
1152  
1153          // Run same problematic DB query.
1154          $this->run_upgrade_step_query();
1155  
1156          // Get the events info.
1157          $info = upgrade_calendar_events_status(false);
1158          $actioninfo = $info['action'];
1159  
1160          // There should be 2 events to be fixed.
1161          $this->assertEquals($eventscount, $actioninfo->bad);
1162  
1163          // Test the function runtime, passing -1 as end time.
1164          // It should not be able to fix all events so fast, so some events should remain to be fixed in the next run.
1165          $this->assertNotFalse(upgrade_calendar_action_events_fix($actioninfo, false, -1));
1166  
1167          // Call the function again, this time it will run until all events have been fixed.
1168          $this->assertFalse(upgrade_calendar_action_events_fix($actioninfo, false));
1169  
1170          // Get the events info again.
1171          $info = upgrade_calendar_events_status(false);
1172  
1173          // All standard events should have been recovered.
1174          // There should be no standard events flagged to be fixed.
1175          $this->assertEquals(0, $info['action']->bad);
1176      }
1177  
1178      /**
1179       * Test the user override part of upgrade_calendar_override_events_fix() function.
1180       */
1181      public function test_upgrade_calendar_user_override_events_fix() {
1182          global $DB;
1183  
1184          $this->resetAfterTest();
1185          $this->setAdminUser();
1186  
1187          $generator = $this->getDataGenerator();
1188  
1189          // Create a new course.
1190          $course = $generator->create_course();
1191  
1192          // Create few users and enrol as students.
1193          $student1 = $generator->create_and_enrol($course, 'student');
1194          $student2 = $generator->create_and_enrol($course, 'student');
1195          $student3 = $generator->create_and_enrol($course, 'student');
1196  
1197          // Create some activities and some override events.
1198          foreach (['assign', 'lesson', 'quiz'] as $modulename) {
1199              $instance = $generator->create_module($modulename, ['course' => $course->id]);
1200              create_user_override_event($modulename, $instance->id, $student1->id);
1201              create_user_override_event($modulename, $instance->id, $student2->id);
1202              create_user_override_event($modulename, $instance->id, $student3->id);
1203          }
1204  
1205          // There should be 9 override events to be fixed (three from each module).
1206          $eventscount = $DB->count_records('event');
1207          $this->assertEquals(9, $eventscount);
1208  
1209          // Get the events info.
1210          $info = upgrade_calendar_events_status(false);
1211          $overrideinfo = $info['override'];
1212  
1213          // There should be no standard events to be fixed.
1214          $this->assertEquals(0, $overrideinfo->bad);
1215  
1216          // No events to be fixed, should return false.
1217          $this->assertFalse(upgrade_calendar_override_events_fix($overrideinfo, false));
1218  
1219          // Run same problematic DB query.
1220          $this->run_upgrade_step_query();
1221  
1222          // Get the events info.
1223          $info = upgrade_calendar_events_status(false);
1224          $overrideinfo = $info['override'];
1225  
1226          // There should be 9 events to be fixed (three from each module).
1227          $this->assertEquals($eventscount, $overrideinfo->bad);
1228  
1229          // Call the function again, this time it will run until all events have been fixed.
1230          $this->assertFalse(upgrade_calendar_override_events_fix($overrideinfo, false));
1231  
1232          // Get the events info again.
1233          $info = upgrade_calendar_events_status(false);
1234  
1235          // All standard events should have been recovered.
1236          // There should be no standard events flagged to be fixed.
1237          $this->assertEquals(0, $info['override']->bad);
1238      }
1239  
1240      /**
1241       * Test the group override part of upgrade_calendar_override_events_fix() function.
1242       */
1243      public function test_upgrade_calendar_group_override_events_fix() {
1244          global $DB;
1245  
1246          $this->resetAfterTest();
1247          $this->setAdminUser();
1248  
1249          $generator = $this->getDataGenerator();
1250  
1251          // Create a new course and few groups.
1252          $course = $generator->create_course();
1253          $group1 = $generator->create_group(['courseid' => $course->id]);
1254          $group2 = $generator->create_group(['courseid' => $course->id]);
1255          $group3 = $generator->create_group(['courseid' => $course->id]);
1256  
1257          // Create some activities and some override events.
1258          foreach (['assign', 'lesson', 'quiz'] as $modulename) {
1259              $instance = $generator->create_module($modulename, ['course' => $course->id]);
1260              create_group_override_event($modulename, $instance->id, $course->id, $group1->id);
1261              create_group_override_event($modulename, $instance->id, $course->id, $group2->id);
1262              create_group_override_event($modulename, $instance->id, $course->id, $group3->id);
1263          }
1264  
1265          // There should be 9 override events to be fixed (three from each module).
1266          $eventscount = $DB->count_records('event');
1267          $this->assertEquals(9, $eventscount);
1268  
1269          // Get the events info.
1270          $info = upgrade_calendar_events_status(false);
1271  
1272          // We classify group overrides as action events since they do not record the userid.
1273          $groupoverrideinfo = $info['action'];
1274  
1275          // There should be no events to be fixed.
1276          $this->assertEquals(0, $groupoverrideinfo->bad);
1277  
1278          // No events to be fixed, should return false.
1279          $this->assertFalse(upgrade_calendar_action_events_fix($groupoverrideinfo, false));
1280  
1281          // Run same problematic DB query.
1282          $this->run_upgrade_step_query();
1283  
1284          // Get the events info.
1285          $info = upgrade_calendar_events_status(false);
1286          $this->assertEquals(9, $info['action']->bad);
1287  
1288          // Call the function again, this time it will run until all events have been fixed.
1289          $this->assertFalse(upgrade_calendar_action_events_fix($info['action'], false));
1290  
1291          // Since group override events do not set userid, these events should not be flagged to be fixed.
1292          $this->assertEquals(0, $groupoverrideinfo->bad);
1293      }
1294  
1295      /**
1296       * Test the admin_dir_usage check with no admin setting specified.
1297       */
1298      public function test_admin_dir_usage_not_set(): void {
1299          $result = new environment_results("custom_checks");
1300  
1301          $this->assertNull(check_admin_dir_usage($result));
1302      }
1303  
1304      /**
1305       * Test the admin_dir_usage check with the default admin setting specified.
1306       */
1307      public function test_admin_dir_usage_is_default(): void {
1308          global $CFG;
1309  
1310          $CFG->admin = 'admin';
1311  
1312          $result = new environment_results("custom_checks");
1313          $this->assertNull(check_admin_dir_usage($result));
1314      }
1315  
1316      /**
1317       * Test the admin_dir_usage check with a custom admin setting specified.
1318       */
1319      public function test_admin_dir_usage_non_standard(): void {
1320          global $CFG;
1321  
1322          $this->resetAfterTest(true);
1323          $CFG->admin = 'notadmin';
1324  
1325          $result = new environment_results("custom_checks");
1326          $this->assertInstanceOf(environment_results::class, check_admin_dir_usage($result));
1327          $this->assertEquals('admin_dir_usage', $result->getInfo());
1328          $this->assertFalse($result->getStatus());
1329      }
1330  
1331      /**
1332       * Test the check_xmlrpc_usage check when the XML-RPC web service method is not set.
1333       *
1334       * @return void
1335       */
1336      public function test_check_xmlrpc_webservice_is_not_set(): void {
1337          global $CFG;
1338  
1339          $this->resetAfterTest();
1340  
1341          $result = new environment_results('custom_checks');
1342          $this->assertNull(check_xmlrpc_usage($result));
1343  
1344          $CFG->webserviceprotocols = 'rest';
1345          $result = new environment_results('custom_checks');
1346          $this->assertNull(check_xmlrpc_usage($result));
1347      }
1348  
1349      /**
1350       * Test the check_xmlrpc_usage check when the XML-RPC web service method is set.
1351       *
1352       * @return void
1353       */
1354      public function test_check_xmlrpc_webservice_is_set(): void {
1355          global $CFG;
1356  
1357          $this->resetAfterTest();
1358          $CFG->webserviceprotocols = 'xmlrpc,rest';
1359  
1360          $result = new environment_results('custom_checks');
1361          $this->assertInstanceOf(environment_results::class, check_xmlrpc_usage($result));
1362          $this->assertEquals('xmlrpc_webservice_usage', $result->getInfo());
1363          $this->assertFalse($result->getStatus());
1364      }
1365  
1366      /**
1367       * Test the check_mod_assignment check if mod_assignment is still used.
1368       *
1369       * @covers ::check_mod_assignment
1370       * @return void
1371       */
1372      public function test_check_mod_assignment_is_used(): void {
1373          global $CFG, $DB;
1374  
1375          $this->resetAfterTest();
1376          $result = new environment_results('custom_checks');
1377  
1378          if (file_exists("{$CFG->dirroot}/mod/assignment/version.php")) {
1379              // This is for when the test is run on sites where mod_assignment is most likely reinstalled.
1380              $this->assertNull(check_mod_assignment($result));
1381          } else {
1382              // This is for when the test is run on sites with mod_assignment now gone.
1383              $this->assertFalse($DB->get_manager()->table_exists('assignment'));
1384              $this->assertNull(check_mod_assignment($result));
1385  
1386              // Then we can simulate a scenario here where the assignment records are still present during the upgrade
1387              // by recreating the assignment table and adding a record to it.
1388              $dbman = $DB->get_manager();
1389              $table = new xmldb_table('assignment');
1390              $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE);
1391              $table->add_field('name', XMLDB_TYPE_CHAR, '255');
1392              $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
1393              $dbman->create_table($table);
1394              $DB->insert_record('assignment', (object)['name' => 'test_assign']);
1395  
1396              $this->assertNotNull(check_mod_assignment($result));
1397              $this->assertEquals('Assignment 2.2 is in use', $result->getInfo());
1398              $this->assertFalse($result->getStatus());
1399          }
1400      }
1401  
1402      /**
1403       * Data provider of usermenu items.
1404       *
1405       * @return array
1406       */
1407      public function usermenu_items_dataprovider(): array {
1408          return [
1409              'Add new item to empty usermenu' => [
1410                  '',
1411                  'reports,core_reportbuilder|/reportbuilder/index.php',
1412                  'reports,core_reportbuilder|/reportbuilder/index.php',
1413              ],
1414              'Add new item to usermenu' => [
1415                  'profile,moodle|/user/profile.php
1416  grades,grades|/grade/report/mygrades.php',
1417                  'reports,core_reportbuilder|/reportbuilder/index.php',
1418                  'profile,moodle|/user/profile.php
1419  grades,grades|/grade/report/mygrades.php
1420  reports,core_reportbuilder|/reportbuilder/index.php',
1421              ],
1422              'Add existing item to usermenu' => [
1423                  'profile,moodle|/user/profile.php
1424  reports,core_reportbuilder|/reportbuilder/index.php
1425  calendar,core_calendar|/calendar/view.php?view=month',
1426                  'reports,core_reportbuilder|/reportbuilder/index.php',
1427                  'profile,moodle|/user/profile.php
1428  reports,core_reportbuilder|/reportbuilder/index.php
1429  calendar,core_calendar|/calendar/view.php?view=month',
1430              ],
1431          ];
1432      }
1433  
1434      /**
1435       * Test the functionality of the {@link upgrade_add_item_to_usermenu()} function.
1436       *
1437       * @covers ::upgrade_add_item_to_usermenu
1438       * @dataProvider usermenu_items_dataprovider
1439       */
1440      public function test_upgrade_add_item_to_usermenu(string $initialmenu, string $newmenuitem, string $expectedmenu) {
1441          global $CFG;
1442  
1443          $this->resetAfterTest();
1444          // Set the base user menu.
1445          $CFG->customusermenuitems = $initialmenu;
1446  
1447          // Add the new item to the user menu.
1448          upgrade_add_item_to_usermenu($newmenuitem);
1449          $newcustomusermenu = $CFG->customusermenuitems;
1450  
1451          $this->assertEquals($expectedmenu, $newcustomusermenu);
1452      }
1453  
1454      /**
1455       * Test that file timestamps are corrected for copied files.
1456       */
1457      public function test_upgrade_fix_file_timestamps() {
1458          global $DB;
1459          $this->resetAfterTest();
1460  
1461          // Add 2 files for testing, one with edited old timestamps.
1462          $origtime = time();
1463          $new = [
1464              'contextid' => 123,
1465              'component' => 'mod_label',
1466              'filearea' => 'intro',
1467              'itemid' => 0,
1468              'filepath' => '/',
1469              'filename' => 'file.txt',
1470          ];
1471          $old = [
1472              'contextid' => 321,
1473              'component' => 'mod_label',
1474              'filearea' => 'intro',
1475              'itemid' => 0,
1476              'filepath' => '/',
1477              'filename' => 'file.txt',
1478          ];
1479  
1480          // Create the file records. This will create a directory listing with the current time.
1481          $fs = get_file_storage();
1482          $newfile = $fs->create_file_from_string($new, 'new');
1483          $oldfile = $fs->create_file_from_string($old, 'old');
1484  
1485          // Manually set the timestamps to use on files.
1486          $DB->set_field('files', 'timecreated', $origtime, [
1487              'contextid' => $newfile->get_contextid(),
1488              'component' => $newfile->get_component(),
1489              'filearea' => $newfile->get_filearea(),
1490              'itemid' => $newfile->get_itemid(),
1491          ]);
1492          $DB->set_field('files', 'timemodified', $origtime, [
1493              'contextid' => $newfile->get_contextid(),
1494              'component' => $newfile->get_component(),
1495              'filearea' => $newfile->get_filearea(),
1496              'itemid' => $newfile->get_itemid(),
1497          ]);
1498  
1499          $DB->set_field('files', 'timecreated', 1, ['id' => $oldfile->get_id()]);
1500          $DB->set_field('files', 'timemodified', 1, ['id' => $oldfile->get_id()]);
1501  
1502          upgrade_fix_file_timestamps();
1503  
1504          // Check nothing changed on the new file.
1505          $updatednew = $DB->get_record('files', ['id' => $newfile->get_id()]);
1506          $this->assertEquals($origtime, $updatednew->timecreated);
1507          $this->assertEquals($origtime, $updatednew->timemodified);
1508  
1509          // Confirm that the file with old timestamps has been fixed.
1510          $updatedold = $DB->get_record('files', ['id' => $oldfile->get_id()]);
1511          $this->assertNotEquals(1, $updatedold->timecreated);
1512          $this->assertNotEquals(1, $updatedold->timemodified);
1513          $this->assertTrue($updatedold->timecreated >= $origtime);
1514          $this->assertTrue($updatedold->timemodified >= $origtime);
1515      }
1516  
1517      /**
1518       * Test the upgrade status check alongside the outageless flags.
1519       *
1520       * @covers ::moodle_needs_upgrading
1521       */
1522      public function test_moodle_upgrade_check_outageless() {
1523          global $CFG;
1524          $this->resetAfterTest();
1525          // Get a baseline.
1526          $this->assertFalse(moodle_needs_upgrading());
1527  
1528          // First lets check a plain upgrade ready.
1529          $CFG->version = '';
1530          $this->assertTrue(moodle_needs_upgrading());
1531  
1532          // Now set the locking config and confirm we shouldn't upgrade.
1533          set_config('outagelessupgrade', true);
1534          $this->assertFalse(moodle_needs_upgrading());
1535  
1536          // Test the ignorelock flag is functioning.
1537          $this->assertTrue(moodle_needs_upgrading(false));
1538      }
1539  
1540      /**
1541       * Test the upgrade status check alongside the outageless flags.
1542       *
1543       * @covers ::upgrade_started
1544       */
1545      public function test_moodle_start_upgrade_outageless() {
1546          global $CFG;
1547          $this->resetAfterTest();
1548          $this->assertObjectNotHasAttribute('upgraderunning', $CFG);
1549  
1550          // Confirm that starting normally sets the upgraderunning flag.
1551          upgrade_started();
1552          $upgrade = get_config('core', 'upgraderunning');
1553          $this->assertTrue($upgrade > (time() - 5));
1554  
1555          // Confirm that the config flag doesnt affect the internal upgrade processes.
1556          unset($CFG->upgraderunning);
1557          set_config('upgraderunning', null);
1558          set_config('outagelessupgrade', true);
1559          upgrade_started();
1560          $upgrade = get_config('core', 'upgraderunning');
1561          $this->assertTrue($upgrade > (time() - 5));
1562      }
1563  
1564      /**
1565       * Test the upgrade timeout setter alongside the outageless flags.
1566       *
1567       * @covers ::upgrade_set_timeout
1568       */
1569      public function test_moodle_set_upgrade_timeout_outageless() {
1570          global $CFG;
1571          $this->resetAfterTest();
1572          $this->assertObjectNotHasAttribute('upgraderunning', $CFG);
1573  
1574          // Confirm running normally sets the timeout.
1575          upgrade_set_timeout(120);
1576          $upgrade = get_config('core', 'upgraderunning');
1577          $this->assertTrue($upgrade > (time() - 5));
1578  
1579          // Confirm that the config flag doesnt affect the internal upgrade processes.
1580          unset($CFG->upgraderunning);
1581          set_config('upgraderunning', null);
1582          set_config('outagelessupgrade', true);
1583          upgrade_set_timeout(120);
1584          $upgrade = get_config('core', 'upgraderunning');
1585          $this->assertTrue($upgrade > (time() - 5));
1586      }
1587  
1588      /**
1589       * Test the components of the upgrade process being run outageless.
1590       *
1591       * @covers ::moodle_needs_upgrading
1592       * @covers ::upgrade_started
1593       * @covers ::upgrade_set_timeout
1594       */
1595      public function test_upgrade_components_with_outageless() {
1596          global $CFG;
1597          $this->resetAfterTest();
1598  
1599          // We can now define the outageless constant for use in upgrade, and test the effects.
1600          define('CLI_UPGRADE_RUNNING', true);
1601  
1602          // First test the upgrade check. Even when locked via config this should return true.
1603          // This can happen when attempting to fix a broken upgrade, so needs to work.
1604          set_config('outagelessupgrade', true);
1605          $CFG->version = '';
1606          $this->assertTrue(moodle_needs_upgrading());
1607  
1608          // Now confirm that starting upgrade with the constant will not set the upgraderunning flag.
1609          set_config('upgraderunning', null);
1610          upgrade_started();
1611          $upgrade = get_config('core', 'upgraderunning');
1612          $this->assertFalse($upgrade);
1613  
1614          // The same for timeouts, it should not be set if the constant is set.
1615          set_config('upgraderunning', null);
1616          upgrade_set_timeout(120);
1617          $upgrade = get_config('core', 'upgraderunning');
1618          $this->assertFalse($upgrade);
1619      }
1620  }