Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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 that the previous records are updated according to the reworded actions.
 899       * @return null
 900       */
 901      public function test_upgrade_rename_prediction_actions_useful_incorrectly_flagged() {
 902          global $DB;
 903  
 904          $this->resetAfterTest();
 905          $this->setAdminUser();
 906  
 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);
 913              }
 914              if ($model->target === '\\core_course\\analytics\\target\\no_teaching') {
 915                  $noteaching = new \core_analytics\model($model);
 916              }
 917          }
 918  
 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);
 929  
 930          $predictionaction = (object)[
 931              'predictionid' => $prediction->get_prediction_data()->id,
 932              'userid' => 2,
 933              'actionname' => 'fixed',
 934              'timecreated' => time()
 935          ];
 936          $DB->insert_record('analytics_prediction_actions', $predictionaction);
 937          $predictionaction->actionname = 'notuseful';
 938          $DB->insert_record('analytics_prediction_actions', $predictionaction);
 939  
 940          upgrade_rename_prediction_actions_useful_incorrectly_flagged();
 941  
 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]));
 950  
 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);
 956  
 957          $predictionaction = (object)[
 958              'predictionid' => $prediction->get_prediction_data()->id,
 959              'userid' => 2,
 960              'actionname' => 'notuseful',
 961              'timecreated' => time()
 962          ];
 963          $DB->insert_record('analytics_prediction_actions', $predictionaction);
 964          $predictionaction->actionname = 'fixed';
 965          $DB->insert_record('analytics_prediction_actions', $predictionaction);
 966  
 967          upgrade_rename_prediction_actions_useful_incorrectly_flagged();
 968  
 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]));
 977  
 978          // We also check that there are no records incorrectly switched in upcomingactivitiesdue.
 979          $upcomingactivitiesdue->clear();
 980  
 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]));
 989  
 990          $upcomingactivitiesdue->predict();
 991          list($ignored, $predictions) = $upcomingactivitiesdue->get_predictions($usercontext, true);
 992          $prediction = reset($predictions);
 993  
 994          $predictionaction = (object)[
 995              'predictionid' => $prediction->get_prediction_data()->id,
 996              'userid' => 2,
 997              'actionname' => 'fixed',
 998              'timecreated' => time()
 999          ];
1000          $DB->insert_record('analytics_prediction_actions', $predictionaction);
1001          $predictionaction->actionname = 'notuseful';
1002          $DB->insert_record('analytics_prediction_actions', $predictionaction);
1003  
1004          upgrade_rename_prediction_actions_useful_incorrectly_flagged();
1005  
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]));
1014      }
1015  
1016      /**
1017       * Test the functionality of the {@link upgrade_convert_hub_config_site_param_names()} function.
1018       */
1019      public function test_upgrade_convert_hub_config_site_param_names() {
1020  
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',
1042          ];
1043  
1044          $converted = upgrade_convert_hub_config_site_param_names($config, 'https://moodle.net');
1045  
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');
1062      }
1063  
1064      /**
1065       * Test the functionality of the {@link upgrade_analytics_fix_contextids_defaults} function.
1066       */
1067      public function test_upgrade_analytics_fix_contextids_defaults() {
1068          global $DB, $USER;
1069  
1070          $this->resetAfterTest();
1071  
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' => ''
1081          ];
1082          $DB->insert_record('analytics_models', $model);
1083  
1084          $model->contextids = null;
1085          $DB->insert_record('analytics_models', $model);
1086  
1087          unset($model->contextids);
1088          $DB->insert_record('analytics_models', $model);
1089  
1090          $model->contextids = '0';
1091          $DB->insert_record('analytics_models', $model);
1092  
1093          $model->contextids = 'null';
1094          $DB->insert_record('analytics_models', $model);
1095  
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));
1099  
1100          upgrade_analytics_fix_contextids_defaults();
1101  
1102          $this->assertEquals(0, $DB->count_records_select('analytics_models', $select, $params));
1103      }
1104  
1105      /**
1106       * Test the functionality of {@link upgrade_core_licenses} function.
1107       */
1108      public function test_upgrade_core_licenses() {
1109          global $CFG, $DB;
1110  
1111          $this->resetAfterTest();
1112  
1113          // Emulate that upgrade is in process.
1114          $CFG->upgraderunning = time();
1115  
1116          $deletedcorelicenseshortname = 'unknown';
1117          $DB->delete_records('license', ['shortname' => $deletedcorelicenseshortname]);
1118  
1119          upgrade_core_licenses();
1120  
1121          $expectedshortnames = ['allrightsreserved', 'cc', 'cc-nc', 'cc-nc-nd', 'cc-nc-sa', 'cc-nd', 'cc-sa', 'public'];
1122          $licenses = $DB->get_records('license');
1123  
1124          foreach ($licenses as $license) {
1125              $this->assertContains($license->shortname, $expectedshortnames);
1126              $this->assertObjectHasAttribute('custom', $license);
1127              $this->assertObjectHasAttribute('sortorder', $license);
1128          }
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);
1132      }
1133  
1134      /**
1135       * Execute same problematic query from upgrade step.
1136       *
1137       * @return bool
1138       */
1139      public function run_upgrade_step_query() {
1140          global $DB;
1141  
1142          return $DB->execute("UPDATE {event} SET userid = 0 WHERE eventtype <> 'user' OR priority <> 0");
1143      }
1144  
1145      /**
1146       * Test the functionality of upgrade_calendar_events_status() function.
1147       */
1148      public function test_upgrade_calendar_events_status() {
1149  
1150          $this->resetAfterTest();
1151          $this->setAdminUser();
1152  
1153          $events = create_standard_events(5);
1154          $eventscount = count($events);
1155  
1156          // Run same DB query as the problematic upgrade step.
1157          $this->run_upgrade_step_query();
1158  
1159          // Get the events info.
1160          $status = upgrade_calendar_events_status(false);
1161  
1162          // Total events.
1163          $expected = [
1164              'total' => (object)[
1165                  'count' => $eventscount,
1166                  'bad' => $eventscount - 5, // Event count excluding user events.
1167              ],
1168              'standard' => (object)[
1169                  'count' => $eventscount,
1170                  'bad' => $eventscount - 5, // Event count excluding user events.
1171              ],
1172          ];
1173  
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);
1178      }
1179  
1180      /**
1181       * Test the functionality of upgrade_calendar_events_get_teacherid() function.
1182       */
1183      public function test_upgrade_calendar_events_get_teacherid() {
1184          global $DB;
1185  
1186          $this->resetAfterTest();
1187  
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');
1192  
1193          // There's a teacher enrolled in the course, return its user id.
1194          $userid = upgrade_calendar_events_get_teacherid($course->id);
1195  
1196          // It should return the enrolled teacher by default.
1197          $this->assertEquals($teacher->id, $userid);
1198  
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);
1202  
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);
1207      }
1208  
1209      /**
1210       * Test the functionality of upgrade_calendar_standard_events_fix() function.
1211       */
1212      public function test_upgrade_calendar_standard_events_fix() {
1213  
1214          $this->resetAfterTest();
1215          $this->setAdminUser();
1216  
1217          $events = create_standard_events(5);
1218          $eventscount = count($events);
1219  
1220          // Get the events info.
1221          $info = upgrade_calendar_events_status(false);
1222  
1223          // There should be no standard events to be fixed.
1224          $this->assertEquals(0, $info['standard']->bad);
1225  
1226          // No events to be fixed, should return false.
1227          $this->assertFalse(upgrade_calendar_standard_events_fix($info['standard'], false));
1228  
1229          // Run same problematic DB query.
1230          $this->run_upgrade_step_query();
1231  
1232          // Get the events info.
1233          $info = upgrade_calendar_events_status(false);
1234  
1235          // There should be 20 events to be fixed (five from each type except user).
1236          $this->assertEquals($eventscount - 5, $info['standard']->bad);
1237  
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);
1242  
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));
1245  
1246          // Get the events info again.
1247          $info = upgrade_calendar_events_status(false);
1248  
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);
1252      }
1253  
1254      /**
1255       * Test the functionality of upgrade_calendar_subscription_events_fix() function.
1256       */
1257      public function test_upgrade_calendar_subscription_events_fix() {
1258          global $CFG, $DB;
1259  
1260          require_once($CFG->dirroot . '/calendar/lib.php');
1261          require_once($CFG->dirroot . '/lib/bennu/bennu.inc.php');
1262  
1263          $this->resetAfterTest();
1264          $this->setAdminUser();
1265  
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);
1272  
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);
1277  
1278          // Import subscription events.
1279          calendar_import_events_from_ical($ical, $id);
1280  
1281          // Subscription should have added 18 events.
1282          $eventscount = $DB->count_records('event');
1283  
1284          // Get the events info.
1285          $info = upgrade_calendar_events_status(false);
1286  
1287          // There should be no subscription events to be fixed at this point.
1288          $this->assertEquals(0, $info['subscription']->bad);
1289  
1290          // No events to be fixed, should return false.
1291          $this->assertFalse(upgrade_calendar_subscription_events_fix($info['subscription'], false));
1292  
1293          // Run same problematic DB query.
1294          $this->run_upgrade_step_query();
1295  
1296          // Get the events info and assert total number of events is correct.
1297          $info = upgrade_calendar_events_status(false);
1298          $subscriptioninfo = $info['subscription'];
1299  
1300          $this->assertEquals($eventscount, $subscriptioninfo->count);
1301  
1302          // Since we have added our subscription as site, all sub events have been affected.
1303          $this->assertEquals($eventscount, $subscriptioninfo->bad);
1304  
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);
1309  
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));
1312  
1313          // Get the events info again.
1314          $info = upgrade_calendar_events_status(false);
1315  
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);
1319      }
1320  
1321      /**
1322       * Test the functionality of upgrade_calendar_action_events_fix() function.
1323       */
1324      public function test_upgrade_calendar_action_events_fix() {
1325          global $DB;
1326  
1327          $this->resetAfterTest();
1328          $this->setAdminUser();
1329  
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]);
1333  
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]);
1339  
1340          $eventscount = $DB->count_records('event');
1341  
1342          // Get the events info.
1343          $info = upgrade_calendar_events_status(false);
1344          $actioninfo = $info['action'];
1345  
1346          // There should be no standard events to be fixed.
1347          $this->assertEquals(0, $actioninfo->bad);
1348  
1349          // No events to be fixed, should return false.
1350          $this->assertFalse(upgrade_calendar_action_events_fix($actioninfo, false));
1351  
1352          // Run same problematic DB query.
1353          $this->run_upgrade_step_query();
1354  
1355          // Get the events info.
1356          $info = upgrade_calendar_events_status(false);
1357          $actioninfo = $info['action'];
1358  
1359          // There should be 2 events to be fixed.
1360          $this->assertEquals($eventscount, $actioninfo->bad);
1361  
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));
1365  
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));
1368  
1369          // Get the events info again.
1370          $info = upgrade_calendar_events_status(false);
1371  
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);
1375      }
1376  
1377      /**
1378       * Test the user override part of upgrade_calendar_override_events_fix() function.
1379       */
1380      public function test_upgrade_calendar_user_override_events_fix() {
1381          global $DB;
1382  
1383          $this->resetAfterTest();
1384          $this->setAdminUser();
1385  
1386          $generator = $this->getDataGenerator();
1387  
1388          // Create a new course.
1389          $course = $generator->create_course();
1390  
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');
1395  
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);
1402          }
1403  
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);
1407  
1408          // Get the events info.
1409          $info = upgrade_calendar_events_status(false);
1410          $overrideinfo = $info['override'];
1411  
1412          // There should be no standard events to be fixed.
1413          $this->assertEquals(0, $overrideinfo->bad);
1414  
1415          // No events to be fixed, should return false.
1416          $this->assertFalse(upgrade_calendar_override_events_fix($overrideinfo, false));
1417  
1418          // Run same problematic DB query.
1419          $this->run_upgrade_step_query();
1420  
1421          // Get the events info.
1422          $info = upgrade_calendar_events_status(false);
1423          $overrideinfo = $info['override'];
1424  
1425          // There should be 9 events to be fixed (three from each module).
1426          $this->assertEquals($eventscount, $overrideinfo->bad);
1427  
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));
1430  
1431          // Get the events info again.
1432          $info = upgrade_calendar_events_status(false);
1433  
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);
1437      }
1438  
1439      /**
1440       * Test the group override part of upgrade_calendar_override_events_fix() function.
1441       */
1442      public function test_upgrade_calendar_group_override_events_fix() {
1443          global $DB;
1444  
1445          $this->resetAfterTest();
1446          $this->setAdminUser();
1447  
1448          $generator = $this->getDataGenerator();
1449  
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]);
1455  
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);
1462          }
1463  
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);
1467  
1468          // Get the events info.
1469          $info = upgrade_calendar_events_status(false);
1470  
1471          // We classify group overrides as action events since they do not record the userid.
1472          $groupoverrideinfo = $info['action'];
1473  
1474          // There should be no events to be fixed.
1475          $this->assertEquals(0, $groupoverrideinfo->bad);
1476  
1477          // No events to be fixed, should return false.
1478          $this->assertFalse(upgrade_calendar_action_events_fix($groupoverrideinfo, false));
1479  
1480          // Run same problematic DB query.
1481          $this->run_upgrade_step_query();
1482  
1483          // Get the events info.
1484          $info = upgrade_calendar_events_status(false);
1485          $this->assertEquals(9, $info['action']->bad);
1486  
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));
1489  
1490          // Since group override events do not set userid, these events should not be flagged to be fixed.
1491          $this->assertEquals(0, $groupoverrideinfo->bad);
1492      }
1493  
1494      /**
1495       * Test the admin_dir_usage check with no admin setting specified.
1496       */
1497      public function test_admin_dir_usage_not_set(): void {
1498          $result = new environment_results("custom_checks");
1499  
1500          $this->assertNull(check_admin_dir_usage($result));
1501      }
1502  
1503      /**
1504       * Test the admin_dir_usage check with the default admin setting specified.
1505       */
1506      public function test_admin_dir_usage_is_default(): void {
1507          global $CFG;
1508  
1509          $CFG->admin = 'admin';
1510  
1511          $result = new environment_results("custom_checks");
1512          $this->assertNull(check_admin_dir_usage($result));
1513      }
1514  
1515      /**
1516       * Test the admin_dir_usage check with a custom admin setting specified.
1517       */
1518      public function test_admin_dir_usage_non_standard(): void {
1519          global $CFG;
1520  
1521          $this->resetAfterTest(true);
1522          $CFG->admin = 'notadmin';
1523  
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());
1528      }
1529  
1530      /**
1531       * Test the check_xmlrpc_usage check when the XML-RPC web service method is not set.
1532       *
1533       * @return void
1534       */
1535      public function test_check_xmlrpc_webservice_is_not_set(): void {
1536          global $CFG;
1537  
1538          $this->resetAfterTest();
1539  
1540          $result = new environment_results('custom_checks');
1541          $this->assertNull(check_xmlrpc_usage($result));
1542  
1543          $CFG->webserviceprotocols = 'rest';
1544          $result = new environment_results('custom_checks');
1545          $this->assertNull(check_xmlrpc_usage($result));
1546      }
1547  
1548      /**
1549       * Test the check_xmlrpc_usage check when the XML-RPC web service method is set.
1550       *
1551       * @return void
1552       */
1553      public function test_check_xmlrpc_webservice_is_set(): void {
1554          global $CFG;
1555  
1556          $this->resetAfterTest();
1557          $CFG->webserviceprotocols = 'xmlrpc,rest';
1558  
1559          $result = new environment_results('custom_checks');
1560          $this->assertInstanceOf(environment_results::class, check_xmlrpc_usage($result));
1561          $this->assertEquals('xmlrpc_webservice_usage', $result->getInfo());
1562          $this->assertFalse($result->getStatus());
1563      }
1564  
1565      /**
1566       * Test the check_xmlrpc_usage check when the MNet is turned on but no host was set up.
1567       *
1568       * @return void
1569       */
1570      public function test_check_xmlrpc_mnet_host_is_not_set(): void {
1571          global $CFG;
1572  
1573          $this->resetAfterTest();
1574          $CFG->mnet_dispatcher_mode = 'strict';
1575  
1576          $result = new environment_results('custom_checks');
1577          $this->assertNull(check_xmlrpc_usage($result));
1578      }
1579  
1580      /**
1581       * Test the check_xmlrpc_usage check when the MNet is turned on and the host was set up.
1582       *
1583       * @return void
1584       */
1585      public function test_check_xmlrpc_mnet_host_is_set(): void {
1586          global $CFG, $DB;
1587  
1588          $this->resetAfterTest();
1589          $CFG->mnet_dispatcher_mode = 'strict';
1590  
1591          // Add a mnet host.
1592          $mnethost = new stdClass();
1593          $mnethost->name = 'A mnet host';
1594          $mnethost->public_key = 'A random public key!';
1595          $mnethost->id = $DB->insert_record('mnet_host', $mnethost);
1596  
1597          $result = new environment_results('custom_checks');
1598          $this->assertInstanceOf(environment_results::class, check_xmlrpc_usage($result));
1599          $this->assertEquals('xmlrpc_mnet_usage', $result->getInfo());
1600          $this->assertFalse($result->getStatus());
1601      }
1602  
1603      /**
1604       * Test the check_xmlrpc_usage check when the MNet is turned on and the Mahara portfolios was set up.
1605       *
1606       * @return void
1607       */
1608      public function test_check_xmlrpc_mahara_portfolios_is_set(): void {
1609          global $CFG;
1610  
1611          $this->resetAfterTest();
1612          $CFG->mnet_dispatcher_mode = 'strict';
1613  
1614          // Enable the Mahara portfolios.
1615          \core\plugininfo\portfolio::enable_plugin('mahara', 1);
1616  
1617          $result = new environment_results('custom_checks');
1618          $this->assertInstanceOf(environment_results::class, check_xmlrpc_usage($result));
1619          $this->assertEquals('xmlrpc_mahara_usage', $result->getInfo());
1620          $this->assertFalse($result->getStatus());
1621      }
1622  
1623      /**
1624       * Test the check_mod_assignment check if mod_assignment is still used.
1625       *
1626       * @covers ::check_mod_assignment
1627       * @return void
1628       */
1629      public function test_check_mod_assignment_is_used(): void {
1630          global $DB;
1631  
1632          $this->resetAfterTest();
1633          $result = new environment_results('custom_checks');
1634  
1635          if ($DB->get_manager()->table_exists('assignment')) {
1636              $DB->insert_record('assignment', (object)['name' => 'test_assign', 'intro' => 'test_assign_intro']);
1637  
1638              $this->assertNotNull(check_mod_assignment($result));
1639              $this->assertEquals('Assignment 2.2 is in use', $result->getInfo());
1640              $this->assertFalse($result->getStatus());
1641          } else {
1642              $this->assertTrue($result->getStatus());
1643          }
1644      }
1645  
1646      /**
1647       * Data provider of usermenu items.
1648       *
1649       * @return array
1650       */
1651      public function usermenu_items_dataprovider(): array {
1652          return [
1653              'Add new item to empty usermenu' => [
1654                  '',
1655                  'reports,core_reportbuilder|/reportbuilder/index.php',
1656                  'reports,core_reportbuilder|/reportbuilder/index.php',
1657              ],
1658              'Add new item to usermenu' => [
1659                  'profile,moodle|/user/profile.php
1660  grades,grades|/grade/report/mygrades.php',
1661                  'reports,core_reportbuilder|/reportbuilder/index.php',
1662                  'profile,moodle|/user/profile.php
1663  grades,grades|/grade/report/mygrades.php
1664  reports,core_reportbuilder|/reportbuilder/index.php',
1665              ],
1666              'Add existing item to usermenu' => [
1667                  'profile,moodle|/user/profile.php
1668  reports,core_reportbuilder|/reportbuilder/index.php
1669  calendar,core_calendar|/calendar/view.php?view=month',
1670                  'reports,core_reportbuilder|/reportbuilder/index.php',
1671                  'profile,moodle|/user/profile.php
1672  reports,core_reportbuilder|/reportbuilder/index.php
1673  calendar,core_calendar|/calendar/view.php?view=month',
1674              ],
1675          ];
1676      }
1677  
1678      /**
1679       * Test the functionality of the {@link upgrade_add_item_to_usermenu()} function.
1680       *
1681       * @covers ::upgrade_add_item_to_usermenu
1682       * @dataProvider usermenu_items_dataprovider
1683       */
1684      public function test_upgrade_add_item_to_usermenu(string $initialmenu, string $newmenuitem, string $expectedmenu) {
1685          global $CFG;
1686  
1687          $this->resetAfterTest();
1688          // Set the base user menu.
1689          $CFG->customusermenuitems = $initialmenu;
1690  
1691          // Add the new item to the user menu.
1692          upgrade_add_item_to_usermenu($newmenuitem);
1693          $newcustomusermenu = $CFG->customusermenuitems;
1694  
1695          $this->assertEquals($expectedmenu, $newcustomusermenu);
1696      }
1697  }