PageRenderTime 34ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/tests/upgradelib_test.php

https://github.com/dongsheng/moodle
PHP | 1529 lines | 925 code | 260 blank | 344 comment | 25 complexity | cf4d5db01615bce45828ee1a9f9858e4 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, GPL-3.0, Apache-2.0, LGPL-2.1
  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. * Unit tests for the lib/upgradelib.php library.
  18. *
  19. * @package core
  20. * @category phpunit
  21. * @copyright 2013 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. global $CFG;
  26. require_once($CFG->libdir.'/upgradelib.php');
  27. require_once($CFG->libdir.'/db/upgradelib.php');
  28. require_once($CFG->dirroot . '/calendar/tests/helpers.php');
  29. /**
  30. * Tests various classes and functions in upgradelib.php library.
  31. */
  32. class upgradelib_test extends advanced_testcase {
  33. /**
  34. * Test the {@link upgrade_stale_php_files_present() function
  35. */
  36. public function test_upgrade_stale_php_files_present() {
  37. // Just call the function, must return bool false always
  38. // if there aren't any old files in the codebase.
  39. $this->assertFalse(upgrade_stale_php_files_present());
  40. }
  41. /**
  42. * Populate some fake grade items into the database with specified
  43. * sortorder and course id.
  44. *
  45. * NOTE: This function doesn't make much attempt to respect the
  46. * gradebook internals, its simply used to fake some data for
  47. * testing the upgradelib function. Please don't use it for other
  48. * purposes.
  49. *
  50. * @param int $courseid id of course
  51. * @param int $sortorder numeric sorting order of item
  52. * @return stdClass grade item object from the database.
  53. */
  54. private function insert_fake_grade_item_sortorder($courseid, $sortorder) {
  55. global $DB, $CFG;
  56. require_once($CFG->libdir.'/gradelib.php');
  57. $item = new stdClass();
  58. $item->courseid = $courseid;
  59. $item->sortorder = $sortorder;
  60. $item->gradetype = GRADE_TYPE_VALUE;
  61. $item->grademin = 30;
  62. $item->grademax = 110;
  63. $item->itemnumber = 1;
  64. $item->iteminfo = '';
  65. $item->timecreated = time();
  66. $item->timemodified = time();
  67. $item->id = $DB->insert_record('grade_items', $item);
  68. return $DB->get_record('grade_items', array('id' => $item->id));
  69. }
  70. public function test_upgrade_extra_credit_weightoverride() {
  71. global $DB, $CFG;
  72. $this->resetAfterTest(true);
  73. require_once($CFG->libdir . '/db/upgradelib.php');
  74. $c = array();
  75. $a = array();
  76. $gi = array();
  77. for ($i=0; $i<5; $i++) {
  78. $c[$i] = $this->getDataGenerator()->create_course();
  79. $a[$i] = array();
  80. $gi[$i] = array();
  81. for ($j=0;$j<3;$j++) {
  82. $a[$i][$j] = $this->getDataGenerator()->create_module('assign', array('course' => $c[$i], 'grade' => 100));
  83. $giparams = array('itemtype' => 'mod', 'itemmodule' => 'assign', 'iteminstance' => $a[$i][$j]->id,
  84. 'courseid' => $c[$i]->id, 'itemnumber' => 0);
  85. $gi[$i][$j] = grade_item::fetch($giparams);
  86. }
  87. }
  88. // Case 1: Course $c[0] has aggregation method different from natural.
  89. $coursecategory = grade_category::fetch_course_category($c[0]->id);
  90. $coursecategory->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN;
  91. $coursecategory->update();
  92. $gi[0][1]->aggregationcoef = 1;
  93. $gi[0][1]->update();
  94. $gi[0][2]->weightoverride = 1;
  95. $gi[0][2]->update();
  96. // Case 2: Course $c[1] has neither extra credits nor overrides
  97. // Case 3: Course $c[2] has extra credits but no overrides
  98. $gi[2][1]->aggregationcoef = 1;
  99. $gi[2][1]->update();
  100. // Case 4: Course $c[3] has no extra credits and has overrides
  101. $gi[3][2]->weightoverride = 1;
  102. $gi[3][2]->update();
  103. // Case 5: Course $c[4] has both extra credits and overrides
  104. $gi[4][1]->aggregationcoef = 1;
  105. $gi[4][1]->update();
  106. $gi[4][2]->weightoverride = 1;
  107. $gi[4][2]->update();
  108. // Run the upgrade script and make sure only course $c[4] was marked as needed to be fixed.
  109. upgrade_extra_credit_weightoverride();
  110. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $c[0]->id}));
  111. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $c[1]->id}));
  112. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $c[2]->id}));
  113. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $c[3]->id}));
  114. $this->assertEquals(20150619, $CFG->{'gradebook_calculations_freeze_' . $c[4]->id});
  115. set_config('gradebook_calculations_freeze_' . $c[4]->id, null);
  116. // Run the upgrade script for a single course only.
  117. upgrade_extra_credit_weightoverride($c[0]->id);
  118. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $c[0]->id}));
  119. upgrade_extra_credit_weightoverride($c[4]->id);
  120. $this->assertEquals(20150619, $CFG->{'gradebook_calculations_freeze_' . $c[4]->id});
  121. }
  122. /**
  123. * Test the upgrade function for flagging courses with calculated grade item problems.
  124. */
  125. public function test_upgrade_calculated_grade_items_freeze() {
  126. global $DB, $CFG;
  127. $this->resetAfterTest();
  128. require_once($CFG->libdir . '/db/upgradelib.php');
  129. // Create a user.
  130. $user = $this->getDataGenerator()->create_user();
  131. // Create a couple of courses.
  132. $course1 = $this->getDataGenerator()->create_course();
  133. $course2 = $this->getDataGenerator()->create_course();
  134. $course3 = $this->getDataGenerator()->create_course();
  135. // Enrol the user in the courses.
  136. $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  137. $maninstance1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST);
  138. $maninstance2 = $DB->get_record('enrol', array('courseid' => $course2->id, 'enrol' => 'manual'), '*', MUST_EXIST);
  139. $maninstance3 = $DB->get_record('enrol', array('courseid' => $course3->id, 'enrol' => 'manual'), '*', MUST_EXIST);
  140. $manual = enrol_get_plugin('manual');
  141. $manual->enrol_user($maninstance1, $user->id, $studentrole->id);
  142. $manual->enrol_user($maninstance2, $user->id, $studentrole->id);
  143. $manual->enrol_user($maninstance3, $user->id, $studentrole->id);
  144. // To create the data we need we freeze the grade book to use the old behaviour.
  145. set_config('gradebook_calculations_freeze_' . $course1->id, 20150627);
  146. set_config('gradebook_calculations_freeze_' . $course2->id, 20150627);
  147. set_config('gradebook_calculations_freeze_' . $course3->id, 20150627);
  148. $CFG->grade_minmaxtouse = 2;
  149. // Creating a category for a grade item.
  150. $gradecategory = new grade_category();
  151. $gradecategory->fullname = 'calculated grade category';
  152. $gradecategory->courseid = $course1->id;
  153. $gradecategory->insert();
  154. $gradecategoryid = $gradecategory->id;
  155. // This is a manual grade item.
  156. $gradeitem = new grade_item();
  157. $gradeitem->itemname = 'grade item one';
  158. $gradeitem->itemtype = 'manual';
  159. $gradeitem->categoryid = $gradecategoryid;
  160. $gradeitem->courseid = $course1->id;
  161. $gradeitem->idnumber = 'gi1';
  162. $gradeitem->insert();
  163. // Changing the category into a calculated grade category.
  164. $gradecategoryitem = grade_item::fetch(array('iteminstance' => $gradecategory->id));
  165. $gradecategoryitem->calculation = '=##gi' . $gradeitem->id . '##/2';
  166. $gradecategoryitem->update();
  167. // Setting a grade for the student.
  168. $grade = $gradeitem->get_grade($user->id, true);
  169. $grade->finalgrade = 50;
  170. $grade->update();
  171. // Creating all the grade_grade items.
  172. grade_regrade_final_grades($course1->id);
  173. // Updating the grade category to a new grade max and min.
  174. $gradecategoryitem->grademax = 50;
  175. $gradecategoryitem->grademin = 5;
  176. $gradecategoryitem->update();
  177. // Different manual grade item for course 2. We are creating a course with a calculated grade item that has a grade max of
  178. // 50. The grade_grade will have a rawgrademax of 100 regardless.
  179. $gradeitem = new grade_item();
  180. $gradeitem->itemname = 'grade item one';
  181. $gradeitem->itemtype = 'manual';
  182. $gradeitem->courseid = $course2->id;
  183. $gradeitem->idnumber = 'gi1';
  184. $gradeitem->grademax = 25;
  185. $gradeitem->insert();
  186. // Calculated grade item for course 2.
  187. $calculatedgradeitem = new grade_item();
  188. $calculatedgradeitem->itemname = 'calculated grade';
  189. $calculatedgradeitem->itemtype = 'manual';
  190. $calculatedgradeitem->courseid = $course2->id;
  191. $calculatedgradeitem->calculation = '=##gi' . $gradeitem->id . '##*2';
  192. $calculatedgradeitem->grademax = 50;
  193. $calculatedgradeitem->insert();
  194. // Assigning a grade for the user.
  195. $grade = $gradeitem->get_grade($user->id, true);
  196. $grade->finalgrade = 10;
  197. $grade->update();
  198. // Setting all of the grade_grade items.
  199. grade_regrade_final_grades($course2->id);
  200. // Different manual grade item for course 3. We are creating a course with a calculated grade item that has a grade max of
  201. // 50. The grade_grade will have a rawgrademax of 100 regardless.
  202. $gradeitem = new grade_item();
  203. $gradeitem->itemname = 'grade item one';
  204. $gradeitem->itemtype = 'manual';
  205. $gradeitem->courseid = $course3->id;
  206. $gradeitem->idnumber = 'gi1';
  207. $gradeitem->grademax = 25;
  208. $gradeitem->insert();
  209. // Calculated grade item for course 2.
  210. $calculatedgradeitem = new grade_item();
  211. $calculatedgradeitem->itemname = 'calculated grade';
  212. $calculatedgradeitem->itemtype = 'manual';
  213. $calculatedgradeitem->courseid = $course3->id;
  214. $calculatedgradeitem->calculation = '=##gi' . $gradeitem->id . '##*2';
  215. $calculatedgradeitem->grademax = 50;
  216. $calculatedgradeitem->insert();
  217. // Assigning a grade for the user.
  218. $grade = $gradeitem->get_grade($user->id, true);
  219. $grade->finalgrade = 10;
  220. $grade->update();
  221. // Setting all of the grade_grade items.
  222. grade_regrade_final_grades($course3->id);
  223. // Need to do this first before changing the other courses, otherwise they will be flagged too early.
  224. set_config('gradebook_calculations_freeze_' . $course3->id, null);
  225. upgrade_calculated_grade_items($course3->id);
  226. $this->assertEquals(20150627, $CFG->{'gradebook_calculations_freeze_' . $course3->id});
  227. // Change the setting back to null.
  228. set_config('gradebook_calculations_freeze_' . $course1->id, null);
  229. set_config('gradebook_calculations_freeze_' . $course2->id, null);
  230. // Run the upgrade.
  231. upgrade_calculated_grade_items();
  232. // The setting should be set again after the upgrade.
  233. $this->assertEquals(20150627, $CFG->{'gradebook_calculations_freeze_' . $course1->id});
  234. $this->assertEquals(20150627, $CFG->{'gradebook_calculations_freeze_' . $course2->id});
  235. }
  236. /**
  237. * Test the upgrade function for final grade after setting grade max for category and grade item.
  238. */
  239. public function test_upgrade_update_category_grademax_regrade_final_grades() {
  240. global $DB;
  241. $this->resetAfterTest();
  242. $generator = $this->getDataGenerator();
  243. $user = $generator->create_user();
  244. // Create a new course.
  245. $course = $generator->create_course();
  246. // Set the course aggregation to weighted mean of grades.
  247. $unitcategory = \grade_category::fetch_course_category($course->id);
  248. $unitcategory->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN;
  249. $unitcategory->update();
  250. // Set grade max for category.
  251. $gradecategoryitem = grade_item::fetch(array('iteminstance' => $unitcategory->id));
  252. $gradecategoryitem->grademax = 50;
  253. $gradecategoryitem->update();
  254. // Make new grade item.
  255. $gradeitem = new \grade_item($generator->create_grade_item([
  256. 'itemname' => 'Grade item',
  257. 'idnumber' => 'git1',
  258. 'courseid' => $course->id,
  259. 'grademin' => 0,
  260. 'grademax' => 50,
  261. 'aggregationcoef' => 100.0,
  262. ]));
  263. // Set final grade.
  264. $grade = $gradeitem->get_grade($user->id, true);
  265. $grade->finalgrade = 20;
  266. $grade->update();
  267. $courseitem = \grade_item::fetch(['courseid' => $course->id, 'itemtype' => 'course']);
  268. $gradeitem->force_regrading();
  269. // Trigger regrade because the grade items needs to be updated.
  270. grade_regrade_final_grades($course->id);
  271. $coursegrade = new \grade_grade($courseitem->get_final($user->id), false);
  272. $this->assertEquals(20, $coursegrade->finalgrade);
  273. }
  274. function test_upgrade_calculated_grade_items_regrade() {
  275. global $DB, $CFG;
  276. $this->resetAfterTest();
  277. require_once($CFG->libdir . '/db/upgradelib.php');
  278. // Create a user.
  279. $user = $this->getDataGenerator()->create_user();
  280. // Create a course.
  281. $course = $this->getDataGenerator()->create_course();
  282. // Enrol the user in the course.
  283. $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  284. $maninstance1 = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
  285. $manual = enrol_get_plugin('manual');
  286. $manual->enrol_user($maninstance1, $user->id, $studentrole->id);
  287. set_config('upgrade_calculatedgradeitemsonlyregrade', 1);
  288. // Creating a category for a grade item.
  289. $gradecategory = new grade_category();
  290. $gradecategory->fullname = 'calculated grade category';
  291. $gradecategory->courseid = $course->id;
  292. $gradecategory->insert();
  293. $gradecategoryid = $gradecategory->id;
  294. // This is a manual grade item.
  295. $gradeitem = new grade_item();
  296. $gradeitem->itemname = 'grade item one';
  297. $gradeitem->itemtype = 'manual';
  298. $gradeitem->categoryid = $gradecategoryid;
  299. $gradeitem->courseid = $course->id;
  300. $gradeitem->idnumber = 'gi1';
  301. $gradeitem->insert();
  302. // Changing the category into a calculated grade category.
  303. $gradecategoryitem = grade_item::fetch(array('iteminstance' => $gradecategory->id));
  304. $gradecategoryitem->calculation = '=##gi' . $gradeitem->id . '##/2';
  305. $gradecategoryitem->grademax = 50;
  306. $gradecategoryitem->grademin = 15;
  307. $gradecategoryitem->update();
  308. // Setting a grade for the student.
  309. $grade = $gradeitem->get_grade($user->id, true);
  310. $grade->finalgrade = 50;
  311. $grade->update();
  312. grade_regrade_final_grades($course->id);
  313. $grade = grade_grade::fetch(array('itemid' => $gradecategoryitem->id, 'userid' => $user->id));
  314. $grade->rawgrademax = 100;
  315. $grade->rawgrademin = 0;
  316. $grade->update();
  317. $this->assertNotEquals($gradecategoryitem->grademax, $grade->rawgrademax);
  318. $this->assertNotEquals($gradecategoryitem->grademin, $grade->rawgrademin);
  319. // This is the function that we are testing. If we comment out this line, then the test fails because the grade items
  320. // are not flagged for regrading.
  321. upgrade_calculated_grade_items();
  322. grade_regrade_final_grades($course->id);
  323. $grade = grade_grade::fetch(array('itemid' => $gradecategoryitem->id, 'userid' => $user->id));
  324. $this->assertEquals($gradecategoryitem->grademax, $grade->rawgrademax);
  325. $this->assertEquals($gradecategoryitem->grademin, $grade->rawgrademin);
  326. }
  327. /**
  328. * Test that the upgrade script correctly flags courses to be frozen due to letter boundary problems.
  329. */
  330. public function test_upgrade_course_letter_boundary() {
  331. global $CFG, $DB;
  332. $this->resetAfterTest(true);
  333. require_once($CFG->libdir . '/db/upgradelib.php');
  334. // Create a user.
  335. $user = $this->getDataGenerator()->create_user();
  336. // Create some courses.
  337. $courses = array();
  338. $contexts = array();
  339. for ($i = 0; $i < 45; $i++) {
  340. $course = $this->getDataGenerator()->create_course();
  341. $context = context_course::instance($course->id);
  342. if (in_array($i, array(2, 5, 10, 13, 14, 19, 23, 25, 30, 34, 36))) {
  343. // Assign good letter boundaries.
  344. $this->assign_good_letter_boundary($context->id);
  345. }
  346. if (in_array($i, array(3, 6, 11, 15, 20, 24, 26, 31, 35))) {
  347. // Assign bad letter boundaries.
  348. $this->assign_bad_letter_boundary($context->id);
  349. }
  350. if (in_array($i, array(3, 9, 10, 11, 18, 19, 20, 29, 30, 31, 40))) {
  351. grade_set_setting($course->id, 'displaytype', '3');
  352. } else if (in_array($i, array(8, 17, 28))) {
  353. grade_set_setting($course->id, 'displaytype', '2');
  354. }
  355. if (in_array($i, array(37, 43))) {
  356. // Show.
  357. grade_set_setting($course->id, 'report_user_showlettergrade', '1');
  358. } else if (in_array($i, array(38, 42))) {
  359. // Hide.
  360. grade_set_setting($course->id, 'report_user_showlettergrade', '0');
  361. }
  362. $assignrow = $this->getDataGenerator()->create_module('assign', array('course' => $course->id, 'name' => 'Test!'));
  363. $gi = grade_item::fetch(
  364. array('itemtype' => 'mod',
  365. 'itemmodule' => 'assign',
  366. 'iteminstance' => $assignrow->id,
  367. 'courseid' => $course->id));
  368. if (in_array($i, array(6, 13, 14, 15, 23, 24, 34, 35, 36, 41))) {
  369. grade_item::set_properties($gi, array('display' => 3));
  370. $gi->update();
  371. } else if (in_array($i, array(12, 21, 32))) {
  372. grade_item::set_properties($gi, array('display' => 2));
  373. $gi->update();
  374. }
  375. $gradegrade = new grade_grade();
  376. $gradegrade->itemid = $gi->id;
  377. $gradegrade->userid = $user->id;
  378. $gradegrade->rawgrade = 55.5563;
  379. $gradegrade->finalgrade = 55.5563;
  380. $gradegrade->rawgrademax = 100;
  381. $gradegrade->rawgrademin = 0;
  382. $gradegrade->timecreated = time();
  383. $gradegrade->timemodified = time();
  384. $gradegrade->insert();
  385. $contexts[] = $context;
  386. $courses[] = $course;
  387. }
  388. upgrade_course_letter_boundary();
  389. // No system setting for grade letter boundaries.
  390. // [0] A course with no letter boundaries.
  391. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[0]->id}));
  392. // [1] A course with letter boundaries which are default.
  393. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[1]->id}));
  394. // [2] A course with letter boundaries which are custom but not affected.
  395. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[2]->id}));
  396. // [3] A course with letter boundaries which are custom and will be affected.
  397. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[3]->id});
  398. // [4] A course with no letter boundaries, but with a grade item with letter boundaries which are default.
  399. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[4]->id}));
  400. // [5] A course with no letter boundaries, but with a grade item with letter boundaries which are not default, but not affected.
  401. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[5]->id}));
  402. // [6] A course with no letter boundaries, but with a grade item with letter boundaries which are not default which will be affected.
  403. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[6]->id});
  404. // System setting for grade letter boundaries (default).
  405. set_config('grade_displaytype', '3');
  406. for ($i = 0; $i < 45; $i++) {
  407. unset_config('gradebook_calculations_freeze_' . $courses[$i]->id);
  408. }
  409. upgrade_course_letter_boundary();
  410. // [7] A course with no grade display settings for the course or grade items.
  411. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[7]->id}));
  412. // [8] A course with grade display settings, but for something that isn't letters.
  413. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[8]->id}));
  414. // [9] A course with grade display settings of letters which are default.
  415. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[9]->id}));
  416. // [10] A course with grade display settings of letters which are not default, but not affected.
  417. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[10]->id}));
  418. // [11] A course with grade display settings of letters which are not default, which will be affected.
  419. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[11]->id});
  420. // [12] A grade item with display settings that are not letters.
  421. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[12]->id}));
  422. // [13] A grade item with display settings of letters which are default.
  423. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[13]->id}));
  424. // [14] A grade item with display settings of letters which are not default, but not affected.
  425. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[14]->id}));
  426. // [15] A grade item with display settings of letters which are not default, which will be affected.
  427. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[15]->id});
  428. // System setting for grade letter boundaries (custom with problem).
  429. $systemcontext = context_system::instance();
  430. $this->assign_bad_letter_boundary($systemcontext->id);
  431. for ($i = 0; $i < 45; $i++) {
  432. unset_config('gradebook_calculations_freeze_' . $courses[$i]->id);
  433. }
  434. upgrade_course_letter_boundary();
  435. // [16] A course with no grade display settings for the course or grade items.
  436. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[16]->id});
  437. // [17] A course with grade display settings, but for something that isn't letters.
  438. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[17]->id}));
  439. // [18] A course with grade display settings of letters which are default.
  440. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[18]->id});
  441. // [19] A course with grade display settings of letters which are not default, but not affected.
  442. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[19]->id}));
  443. // [20] A course with grade display settings of letters which are not default, which will be affected.
  444. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[20]->id});
  445. // [21] A grade item with display settings which are not letters. Grade total will be affected so should be frozen.
  446. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[21]->id});
  447. // [22] A grade item with display settings of letters which are default.
  448. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[22]->id});
  449. // [23] A grade item with display settings of letters which are not default, but not affected. Course uses new letter boundary setting.
  450. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[23]->id}));
  451. // [24] A grade item with display settings of letters which are not default, which will be affected.
  452. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[24]->id});
  453. // [25] A course which is using the default grade display setting, but has updated the grade letter boundary (not 57) Should not be frozen.
  454. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[25]->id}));
  455. // [26] A course that is using the default display setting (letters) and altered the letter boundary with 57. Should be frozen.
  456. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[26]->id});
  457. // System setting not showing letters.
  458. set_config('grade_displaytype', '2');
  459. for ($i = 0; $i < 45; $i++) {
  460. unset_config('gradebook_calculations_freeze_' . $courses[$i]->id);
  461. }
  462. upgrade_course_letter_boundary();
  463. // [27] A course with no grade display settings for the course or grade items.
  464. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[27]->id}));
  465. // [28] A course with grade display settings, but for something that isn't letters.
  466. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[28]->id}));
  467. // [29] A course with grade display settings of letters which are default.
  468. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[29]->id});
  469. // [30] A course with grade display settings of letters which are not default, but not affected.
  470. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[30]->id}));
  471. // [31] A course with grade display settings of letters which are not default, which will be affected.
  472. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[31]->id});
  473. // [32] A grade item with display settings which are not letters.
  474. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[32]->id}));
  475. // [33] All system defaults.
  476. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[33]->id}));
  477. // [34] A grade item with display settings of letters which are not default, but not affected. Course uses new letter boundary setting.
  478. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[34]->id}));
  479. // [35] A grade item with display settings of letters which are not default, which will be affected.
  480. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[35]->id});
  481. // [36] A course with grade display settings of letters with modified and good boundary (not 57) Should not be frozen.
  482. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[36]->id}));
  483. // Previous site conditions still exist.
  484. for ($i = 0; $i < 45; $i++) {
  485. unset_config('gradebook_calculations_freeze_' . $courses[$i]->id);
  486. }
  487. upgrade_course_letter_boundary();
  488. // [37] Site setting for not showing the letter column and course setting set to show (frozen).
  489. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[37]->id});
  490. // [38] Site setting for not showing the letter column and course setting set to hide.
  491. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[38]->id}));
  492. // [39] Site setting for not showing the letter column and course setting set to default.
  493. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[39]->id}));
  494. // [40] Site setting for not showing the letter column and course setting set to default. Course display set to letters (frozen).
  495. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[40]->id});
  496. // [41] Site setting for not showing the letter column and course setting set to default. Grade item display set to letters (frozen).
  497. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[41]->id});
  498. // Previous site conditions still exist.
  499. for ($i = 0; $i < 45; $i++) {
  500. unset_config('gradebook_calculations_freeze_' . $courses[$i]->id);
  501. }
  502. set_config('grade_report_user_showlettergrade', '1');
  503. upgrade_course_letter_boundary();
  504. // [42] Site setting for showing the letter column, but course setting set to hide.
  505. $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[42]->id}));
  506. // [43] Site setting for showing the letter column and course setting set to show (frozen).
  507. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[43]->id});
  508. // [44] Site setting for showing the letter column and course setting set to default (frozen).
  509. $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[44]->id});
  510. }
  511. /**
  512. * Test upgrade_letter_boundary_needs_freeze function.
  513. */
  514. public function test_upgrade_letter_boundary_needs_freeze() {
  515. global $CFG;
  516. $this->resetAfterTest();
  517. require_once($CFG->libdir . '/db/upgradelib.php');
  518. $courses = array();
  519. $contexts = array();
  520. for ($i = 0; $i < 3; $i++) {
  521. $courses[] = $this->getDataGenerator()->create_course();
  522. $contexts[] = context_course::instance($courses[$i]->id);
  523. }
  524. // Course one is not using a letter boundary.
  525. $this->assertFalse(upgrade_letter_boundary_needs_freeze($contexts[0]));
  526. // Let's make course 2 use the bad boundary.
  527. $this->assign_bad_letter_boundary($contexts[1]->id);
  528. $this->assertTrue(upgrade_letter_boundary_needs_freeze($contexts[1]));
  529. // Course 3 has letter boundaries that are fine.
  530. $this->assign_good_letter_boundary($contexts[2]->id);
  531. $this->assertFalse(upgrade_letter_boundary_needs_freeze($contexts[2]));
  532. // Try the system context not using a letter boundary.
  533. $systemcontext = context_system::instance();
  534. $this->assertFalse(upgrade_letter_boundary_needs_freeze($systemcontext));
  535. }
  536. /**
  537. * Assigns letter boundaries with comparison problems.
  538. *
  539. * @param int $contextid Context ID.
  540. */
  541. private function assign_bad_letter_boundary($contextid) {
  542. global $DB;
  543. $newlettersscale = array(
  544. array('contextid' => $contextid, 'lowerboundary' => 90.00000, 'letter' => 'A'),
  545. array('contextid' => $contextid, 'lowerboundary' => 85.00000, 'letter' => 'A-'),
  546. array('contextid' => $contextid, 'lowerboundary' => 80.00000, 'letter' => 'B+'),
  547. array('contextid' => $contextid, 'lowerboundary' => 75.00000, 'letter' => 'B'),
  548. array('contextid' => $contextid, 'lowerboundary' => 70.00000, 'letter' => 'B-'),
  549. array('contextid' => $contextid, 'lowerboundary' => 65.00000, 'letter' => 'C+'),
  550. array('contextid' => $contextid, 'lowerboundary' => 57.00000, 'letter' => 'C'),
  551. array('contextid' => $contextid, 'lowerboundary' => 50.00000, 'letter' => 'C-'),
  552. array('contextid' => $contextid, 'lowerboundary' => 40.00000, 'letter' => 'D+'),
  553. array('contextid' => $contextid, 'lowerboundary' => 25.00000, 'letter' => 'D'),
  554. array('contextid' => $contextid, 'lowerboundary' => 0.00000, 'letter' => 'F'),
  555. );
  556. $DB->delete_records('grade_letters', array('contextid' => $contextid));
  557. foreach ($newlettersscale as $record) {
  558. // There is no API to do this, so we have to manually insert into the database.
  559. $DB->insert_record('grade_letters', $record);
  560. }
  561. }
  562. /**
  563. * Assigns letter boundaries with no comparison problems.
  564. *
  565. * @param int $contextid Context ID.
  566. */
  567. private function assign_good_letter_boundary($contextid) {
  568. global $DB;
  569. $newlettersscale = array(
  570. array('contextid' => $contextid, 'lowerboundary' => 90.00000, 'letter' => 'A'),
  571. array('contextid' => $contextid, 'lowerboundary' => 85.00000, 'letter' => 'A-'),
  572. array('contextid' => $contextid, 'lowerboundary' => 80.00000, 'letter' => 'B+'),
  573. array('contextid' => $contextid, 'lowerboundary' => 75.00000, 'letter' => 'B'),
  574. array('contextid' => $contextid, 'lowerboundary' => 70.00000, 'letter' => 'B-'),
  575. array('contextid' => $contextid, 'lowerboundary' => 65.00000, 'letter' => 'C+'),
  576. array('contextid' => $contextid, 'lowerboundary' => 54.00000, 'letter' => 'C'),
  577. array('contextid' => $contextid, 'lowerboundary' => 50.00000, 'letter' => 'C-'),
  578. array('contextid' => $contextid, 'lowerboundary' => 40.00000, 'letter' => 'D+'),
  579. array('contextid' => $contextid, 'lowerboundary' => 25.00000, 'letter' => 'D'),
  580. array('contextid' => $contextid, 'lowerboundary' => 0.00000, 'letter' => 'F'),
  581. );
  582. $DB->delete_records('grade_letters', array('contextid' => $contextid));
  583. foreach ($newlettersscale as $record) {
  584. // There is no API to do this, so we have to manually insert into the database.
  585. $DB->insert_record('grade_letters', $record);
  586. }
  587. }
  588. /**
  589. * Test libcurl custom check api.
  590. */
  591. public function test_check_libcurl_version() {
  592. $supportedversion = 0x071304;
  593. $curlinfo = curl_version();
  594. $currentversion = $curlinfo['version_number'];
  595. $result = new environment_results("custom_checks");
  596. if ($currentversion < $supportedversion) {
  597. $this->assertFalse(check_libcurl_version($result)->getStatus());
  598. } else {
  599. $this->assertNull(check_libcurl_version($result));
  600. }
  601. }
  602. /**
  603. * Create a collection of test themes to test determining parent themes.
  604. *
  605. * @return Url to the path containing the test themes
  606. */
  607. public function create_testthemes() {
  608. global $CFG;
  609. $themedircontent = [
  610. 'testtheme' => [
  611. 'config.php' => '<?php $THEME->name = "testtheme"; $THEME->parents = [""];',
  612. ],
  613. 'childoftesttheme' => [
  614. 'config.php' => '<?php $THEME->name = "childofboost"; $THEME->parents = ["testtheme"];',
  615. ],
  616. 'infinite' => [
  617. 'config.php' => '<?php $THEME->name = "infinite"; $THEME->parents = ["forever"];',
  618. ],
  619. 'forever' => [
  620. 'config.php' => '<?php $THEME->name = "forever"; $THEME->parents = ["infinite", "childoftesttheme"];',
  621. ],
  622. 'orphantheme' => [
  623. 'config.php' => '<?php $THEME->name = "orphantheme"; $THEME->parents = [];',
  624. ],
  625. 'loop' => [
  626. 'config.php' => '<?php $THEME->name = "loop"; $THEME->parents = ["around"];',
  627. ],
  628. 'around' => [
  629. 'config.php' => '<?php $THEME->name = "around"; $THEME->parents = ["loop"];',
  630. ],
  631. 'themewithbrokenparent' => [
  632. 'config.php' => '<?php $THEME->name = "orphantheme"; $THEME->parents = ["nonexistent", "testtheme"];',
  633. ],
  634. ];
  635. $vthemedir = \org\bovigo\vfs\vfsStream::setup('themes', null, $themedircontent);
  636. return \org\bovigo\vfs\vfsStream::url('themes');
  637. }
  638. /**
  639. * Data provider of serialized string.
  640. *
  641. * @return array
  642. */
  643. public function serialized_strings_dataprovider() {
  644. return [
  645. 'A configuration that uses the old object' => [
  646. '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";}',
  647. true,
  648. '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";}'
  649. ],
  650. 'A configuration that uses stdClass' => [
  651. '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";}',
  652. false,
  653. '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";}'
  654. ],
  655. 'A setting I saw when importing a course with blocks from 1.9' => [
  656. 'N;',
  657. false,
  658. 'N;'
  659. ],
  660. 'An object in an object' => [
  661. 'O:6:"object":2:{s:2:"id";i:5;s:5:"other";O:6:"object":1:{s:4:"text";s:13:"something new";}}',
  662. true,
  663. 'O:8:"stdClass":2:{s:2:"id";i:5;s:5:"other";O:8:"stdClass":1:{s:4:"text";s:13:"something new";}}'
  664. ],
  665. 'An array with an object in it' => [
  666. '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;}',
  667. true,
  668. '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;}'
  669. ]
  670. ];
  671. }
  672. /**
  673. * Test that objects in serialized strings will be changed over to stdClass.
  674. *
  675. * @dataProvider serialized_strings_dataprovider
  676. * @param string $initialstring The initial serialized setting.
  677. * @param bool $expectededited If the string is expected to be edited.
  678. * @param string $expectedresult The expected serialized setting to be returned.
  679. */
  680. public function test_upgrade_fix_serialized_objects($initialstring, $expectededited, $expectedresult) {
  681. list($edited, $resultstring) = upgrade_fix_serialized_objects($initialstring);
  682. $this->assertEquals($expectededited, $edited);
  683. $this->assertEquals($expectedresult, $resultstring);
  684. }
  685. /**
  686. * Data provider for base64_encoded block instance config data.
  687. */
  688. public function encoded_strings_dataprovider() {
  689. return [
  690. 'Normal data using stdClass' => [
  691. 'Tzo4OiJzdGRDbGFzcyI6NTp7czo1OiJ0aXRsZSI7czo0OiJUYWdzIjtzOjEyOiJudW1iZXJvZnRhZ3MiO3M6MjoiODAiO3M6MTI6InNob3dzdGFuZGFyZCI7czoxOiIwIjtzOjM6ImN0eCI7czozOiIyODkiO3M6MzoicmVjIjtzOjE6IjEiO30=',
  692. 'Tzo4OiJzdGRDbGFzcyI6NTp7czo1OiJ0aXRsZSI7czo0OiJUYWdzIjtzOjEyOiJudW1iZXJvZnRhZ3MiO3M6MjoiODAiO3M6MTI6InNob3dzdGFuZGFyZCI7czoxOiIwIjtzOjM6ImN0eCI7czozOiIyODkiO3M6MzoicmVjIjtzOjE6IjEiO30='
  693. ],
  694. 'No data at all' => [
  695. '',
  696. ''
  697. ],
  698. 'Old data using object' => [
  699. 'Tzo2OiJvYmplY3QiOjM6e3M6NDoidGV4dCI7czozMjoiTm90aGluZyB0aGF0IGFueW9uZSBjYXJlcyBhYm91dC4iO3M6NToidGl0bGUiO3M6MTY6IlJlYWxseSBvbGQgYmxvY2siO3M6NjoiZm9ybWF0IjtzOjE6IjEiO30=',
  700. 'Tzo4OiJzdGRDbGFzcyI6Mzp7czo0OiJ0ZXh0IjtzOjMyOiJOb3RoaW5nIHRoYXQgYW55b25lIGNhcmVzIGFib3V0LiI7czo1OiJ0aXRsZSI7czoxNjoiUmVhbGx5IG9sZCBibG9jayI7czo2OiJmb3JtYXQiO3M6MToiMSI7fQ=='
  701. ]
  702. ];
  703. }
  704. /**
  705. * Check that orphaned files are deleted.
  706. */
  707. public function test_upgrade_delete_orphaned_file_records() {
  708. global $DB, $CFG;
  709. require_once($CFG->dirroot . '/repository/lib.php');
  710. $this->resetAfterTest();
  711. // Create user.
  712. $generator = $this->getDataGenerator();
  713. $user = $generator->create_user();
  714. $this->setUser($user);
  715. $usercontext = context_user::instance($user->id);
  716. $syscontext = context_system::instance();
  717. $fs = get_file_storage();
  718. $userrepository = array();
  719. $newstoredfile = array();
  720. $repositorypluginname = array('user', 'areafiles');
  721. // Create two repositories with one file in each.
  722. foreach ($repositorypluginname as $key => $value) {
  723. // Override repository permission.
  724. $capability = 'repository/' . $value . ':view';
  725. $guestroleid = $DB->get_field('role', 'id', array('shortname' => 'guest'));
  726. assign_capability($capability, CAP_ALLOW, $guestroleid, $syscontext->id, true);
  727. $args = array();
  728. $args['type'] = $value;
  729. $repos = repository::get_instances($args);
  730. $userrepository[$key] = reset($repos);
  731. $this->assertInstanceOf('repository', $userrepository[$key]);
  732. $component = 'user';
  733. $filearea = 'private';
  734. $itemid = $key;
  735. $filepath = '/';
  736. $filename = 'userfile.txt';
  737. $filerecord = array(
  738. 'contextid' => $usercontext->id,
  739. 'component' => $component,
  740. 'filearea' => $filearea,
  741. 'itemid' => $itemid,
  742. 'filepath' => $filepath,
  743. 'filename' => $filename,
  744. );
  745. $content = 'Test content';
  746. $originalfile = $fs->create_file_from_string($filerecord, $content);
  747. $this->assertInstanceOf('stored_file', $originalfile);
  748. $newfilerecord = array(
  749. 'contextid' => $syscontext->id,
  750. 'component' => 'core',
  751. 'filearea' => 'phpunit',
  752. 'itemid' => $key,
  753. 'filepath' => $filepath,
  754. 'filename' => $filename,
  755. );
  756. $ref = $fs->pack_reference($filerecord);
  757. $newstoredfile[$key] = $fs->create_file_from_reference($newfilerecord, $userrepository[$key]->id, $ref);
  758. // Look for references by repository ID.
  759. $files = $fs->get_external_files($userrepository[$key]->id);
  760. $file = reset($files);
  761. $this->assertEquals($file, $newstoredfile[$key]);
  762. }
  763. // Make one file orphaned by deleting first repository.
  764. $DB->delete_records('repository_instances', array('id' => $userrepository[0]->id));
  765. $DB->delete_records('repository_instance_config', array('instanceid' => $userrepository[0]->id));
  766. upgrade_delete_orphaned_file_records();
  767. $files = $fs->get_external_files($userrepository[0]->id);
  768. $file = reset($files);
  769. $this->assertFalse($file);
  770. $files = $fs->get_external_files($userrepository[1]->id);
  771. $file = reset($files);
  772. $this->assertEquals($file, $newstoredfile[1]);
  773. }
  774. /**
  775. * Test that the previous records are updated according to the reworded actions.
  776. * @return null
  777. */
  778. public function test_upgrade_rename_prediction_actions_useful_incorrectly_flagged() {
  779. global $DB;
  780. $this->resetAfterTest();
  781. $this->setAdminUser();
  782. $models = $DB->get_records('analytics_models');
  783. $upcomingactivitiesdue = null;
  784. $noteaching = null;
  785. foreach ($models as $model) {
  786. if ($model->target === '\\core_user\\analytics\\target\\upcoming_activities_due') {
  787. $upcomingactivitiesdue = new \core_analytics\model($model);
  788. }
  789. if ($model->target === '\\core_course\\analytics\\target\\no_teaching') {
  790. $noteaching = new \core_analytics\model($model);
  791. }
  792. }
  793. // Upcoming activities due generating some insights.
  794. $course1 = $this->getDataGenerator()->create_course();
  795. $attrs = ['course' => $course1, 'duedate' => time() + WEEKSECS - DAYSECS];
  796. $assign = $this->getDataGenerator()->get_plugin_generator('mod_assign')->create_instance($attrs);
  797. $student = $this->getDataGenerator()->create_user();
  798. $usercontext = \context_user::instance($student->id);
  799. $this->getDataGenerator()->enrol_user($student->id, $course1->id, 'student');
  800. $upcomingactivitiesdue->predict();
  801. list($ignored, $predictions) = $upcomingactivitiesdue->get_predictions($usercontext, true);
  802. $prediction = reset($predictions);
  803. $predictionaction = (object)[
  804. 'predictionid' => $prediction->get_prediction_data()->id,
  805. 'userid' => 2,
  806. 'actionname' => 'fixed',
  807. 'timecreated' => time()
  808. ];
  809. $DB->insert_record('analytics_prediction_actions', $predictionaction);
  810. $predictionaction->actionname = 'notuseful';
  811. $DB->insert_record('analytics_prediction_actions', $predictionaction);
  812. upgrade_rename_prediction_actions_useful_incorrectly_flagged();
  813. $this->assertEquals(0, $DB->count_records('analytics_prediction_actions',
  814. ['actionname' => \core_analytics\prediction::ACTION_FIXED]));
  815. $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
  816. ['actionname' => \core_analytics\prediction::ACTION_USEFUL]));
  817. $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
  818. ['actionname' => \core_analytics\prediction::ACTION_NOT_USEFUL]));
  819. $this->assertEquals(0, $DB->count_records('analytics_prediction_actions',
  820. ['actionname' => \core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED]));
  821. // No teaching generating some insights.
  822. $course2 = $this->getDataGenerator()->create_course(['startdate' => time() + (2 * DAYSECS)]);
  823. $noteaching->predict();
  824. list($ignored, $predictions) = $noteaching->get_predictions(\context_system::instance(), true);
  825. $prediction = reset($predictions);
  826. $predictionaction = (object)[
  827. 'predictionid' => $prediction->get_prediction_data()->id,
  828. 'userid' => 2,
  829. 'actionname' => 'notuseful',
  830. 'timecreated' => time()
  831. ];
  832. $DB->insert_record('analytics_prediction_actions', $predictionaction);
  833. $predictionaction->actionname = 'fixed';
  834. $DB->insert_record('analytics_prediction_actions', $predictionaction);
  835. upgrade_rename_prediction_actions_useful_incorrectly_flagged();
  836. $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
  837. ['actionname' => \core_analytics\prediction::ACTION_FIXED]));
  838. $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
  839. ['actionname' => \core_analytics\prediction::ACTION_USEFUL]));
  840. $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
  841. ['actionname' => \core_analytics\prediction::ACTION_NOT_USEFUL]));
  842. $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
  843. ['actionname' => \core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED]));
  844. // We also check that there are no records incorrectly switched in upcomingactivitiesdue.
  845. $upcomingactivitiesdue->clear();
  846. $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
  847. ['actionname' => \core_analytics\prediction::ACTION_FIXED]));
  848. $this->assertEquals(0, $DB->count_records('analytics_prediction_actions',
  849. ['actionname' => \core_analytics\prediction::ACTION_USEFUL]));
  850. $this->assertEquals(0, $DB->count_records('analytics_prediction_actions',
  851. ['actionname' => \core_analytics\prediction::ACTION_NOT_USEFUL]));
  852. $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
  853. ['actionname' => \core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED]));
  854. $upcomingactivitiesdue->predict();
  855. list($ignored, $predictions) = $upcomingactivitiesdue->get_predictions($usercontext, true);
  856. $prediction = reset($predictions);
  857. $predictionaction = (object)[
  858. 'predictionid' => $prediction->get_prediction_data()->id,
  859. 'userid' => 2,
  860. 'actionname' => 'fixed',
  861. 'timecreated' => time()
  862. ];
  863. $DB->insert_record('analytics_prediction_actions', $predictionaction);
  864. $predictionaction->actionname = 'notuseful';
  865. $DB->insert_record('analytics_prediction_actions', $predictionaction);
  866. upgrade_rename_prediction_actions_useful_incorrectly_flagged();
  867. $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
  868. ['actionname' => \core_analytics\prediction::ACTION_FIXED]));
  869. $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
  870. ['actionname' => \core_analytics\prediction::ACTION_USEFUL]));
  871. $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
  872. ['actionname' => \core_analytics\prediction::ACTION_NOT_USEFUL]));
  873. $this->assertEquals(1, $DB->count_records('analytics_prediction_actions',
  874. ['actionname' => \core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED]));
  875. }
  876. /**
  877. * Test the functionality of the {@link upgrade_convert_hub_config_site_param_names()} function.
  878. */
  879. public function test_upgrade_convert_hub_config_site_param_names() {
  880. $config = (object) [
  881. // This is how site settings related to registration at https://moodle.net are stored.
  882. 'site_name_httpsmoodlenet' => 'Foo Site',
  883. 'site_language_httpsmoodlenet' => 'en',
  884. 'site_emailalert_httpsmoodlenet' => 1,
  885. // These are unexpected relics of a value as registered at the old http://hub.moodle.org site.
  886. 'site_name_httphubmoodleorg' => 'Bar Site',
  887. 'site_description_httphubmoodleorg' => 'Old description',
  888. // This is the target value we are converting to - here it already somehow exists.
  889. 'site_emailalert' => 0,
  890. // This is a setting not related to particular hub.
  891. 'custom' => 'Do not touch this',
  892. // A setting defined for multiple alternative hubs.
  893. 'site_foo_httpfirsthuborg' => 'First',
  894. 'site_foo_httpanotherhubcom' => 'Another',
  895. 'site_foo_httpyetanotherhubcom' => 'Yet another',
  896. // A setting defined for multiple alternative hubs and one referential one.
  897. 'site_bar_httpfirsthuborg' => 'First',
  898. 'site_bar_httpanotherhubcom' => 'Another',
  899. 'site_bar_httpsmoodlenet' => 'One hub to rule them all!',
  900. 'site_bar_httpyetanotherhubcom' => 'Yet another',
  901. ];
  902. $converted = upgrade_convert_hub_config_site_param_names($config, 'https://moodle.net');
  903. // Values defined for the moodle.net take precedence over the ones defined for other hubs.
  904. $this->assertSame($converted->site_name, 'Foo Site');
  905. $this->assertSame($converted->site_bar, 'One hub to rule them all!');
  906. $this->assertNull($converted->site_name_httpsmoodlenet);
  907. $this->assertNull($converted->site_bar_httpfirsthuborg);
  908. $this->assertNull($converted->site_bar_httpanotherhubcom);
  909. $this->assertNull($converted->site_bar_httpyetanotherhubcom);
  910. // Values defined for alternative hubs only do not have any guaranteed value. Just for convenience, we use the first one.
  911. $this->assertSame($converted->site_foo, 'First');
  912. $this->assertNull($converted->site_foo_httpfirsthuborg);
  913. $this->assertNull($converted->site_foo_httpanotherhubcom);
  914. $this->assertNull($converted->site_foo_httpyetanotherhubcom);
  915. // Values that are already defined with the new name format are kept.
  916. $this->assertSame($converted->site_emailalert, 0);
  917. // Eventual custom values not following the expected hub-specific naming format, are kept.
  918. $this->assertSame($converted->custom, 'Do not touch this');
  919. }
  920. /**
  921. * Test the functionality of the {@link upgrade_analytics_fix_contextids_defaults} function.
  922. */
  923. public function test_upgrade_analytics_fix_contextids_defaults() {
  924. global $DB, $USER;
  925. $this->resetAfterTest();
  926. $model = (object)[
  927. 'name' => 'asd',
  928. 'target' => 'ou',
  929. 'indicators' => '[]',
  930. 'version' => '1',
  931. 'timecreated' => time(),
  932. 'timemodified' => time(),
  933. 'usermodified' => $USER->id,
  934. 'contextids' => ''
  935. ];
  936. $DB->insert_record('analytics_models', $model);
  937. $model->contextids = null;
  938. $DB->insert_record('analytics_models', $model);
  939. unset($model->contextids);
  940. $DB->insert_record('analytics_models', $model);
  941. $model->contextids = '0';
  942. $DB->insert_record('analytics_models', $model);
  943. $model->contextids = 'null';
  944. $DB->insert_record('analytics_models', $model);
  945. $select = $DB->sql_compare_text('contextids') . ' = :zero OR ' . $DB->sql_compare_text('contextids') . ' = :null';
  946. $params = ['zero' => '0', 'null' => 'null'];
  947. $this->assertEquals(2, $DB->count_records_select('analytics_models', $select, $params));
  948. upgrade_analytics_fix_contextids_defaults();
  949. $this->assertEquals(0, $DB->count_records_select('analytics_models', $select, $params));
  950. }
  951. /**
  952. * Test the functionality of {@link upgrade_core_licenses} function.
  953. */
  954. public function test_upgrade_core_licenses() {
  955. global $CFG, $DB;
  956. $this->resetAfterTest();
  957. // Emulate that upgrade is in process.
  958. $CFG->upgraderunning = time();
  959. $deletedcorelicenseshortname = 'unknown';
  960. $DB->delete_records('license', ['shortname' => $deletedcorelicenseshortname]);
  961. upgrade_core_licenses();
  962. $expectedshortnames = ['allrightsreserved', 'cc', 'cc-nc', 'cc-nc-nd', 'cc-nc-sa', 'cc-nd', 'cc-sa', 'public'];
  963. $licenses = $DB->get_records('license');
  964. foreach ($licenses as $license) {
  965. $this->assertContains($license->shortname, $expectedshortnames);
  966. $this->assertObjectHasAttribute('custom', $license);
  967. $this->assertObjectHasAttribute('sortorder', $license);
  968. }
  969. // A core license which was deleted prior to upgrade should not be reinstalled.
  970. $actualshortnames = $DB->get_records_menu('license', null, '', 'id, shortname');
  971. $this->assertNotContains($deletedcorelicenseshortname, $actualshortnames);
  972. }
  973. /**
  974. * Execute same problematic query from upgrade step.
  975. *
  976. * @return bool
  977. */
  978. public function run_upgrade_step_query() {
  979. global $DB;
  980. return $DB->execute("UPDATE {event} SET userid = 0 WHERE eventtype <> 'user' OR priority <> 0");
  981. }
  982. /**
  983. * Test the functionality of upgrade_calendar_events_status() function.
  984. */
  985. public function test_upgrade_calendar_events_status() {
  986. $this->resetAfterTest();
  987. $this->setAdminUser();
  988. $events = create_standard_events(5);
  989. $eventscount = count($events);
  990. // Run same DB query as the problematic upgrade step.
  991. $this->run_upgrade_step_query();
  992. // Get the events info.
  993. $status = upgrade_calendar_events_status(false);
  994. // Total events.
  995. $expected = [
  996. 'total' => (object)[
  997. 'count' => $eventscount,
  998. 'bad' => $eventscount - 5, // Event count excluding user events.
  999. ],
  1000. 'standard' => (object)[
  1001. 'count' => $eventscount,
  1002. 'bad' => $eventscount - 5, // Event count excluding user events.
  1003. ],
  1004. ];
  1005. $this->assertEquals($expected['standard']->count, $status['standard']->count);
  1006. $this->assertEquals($expected['standard']->bad, $status['standard']->bad);
  1007. $this->assertEquals($expected['total']->count, $status['total']->count);
  1008. $this->assertEquals($expected['total']->bad, $status['total']->bad);
  1009. }
  1010. /**
  1011. * Test the functionality of upgrade_calendar_events_get_teacherid() function.
  1012. */
  1013. public function test_upgrade_calendar_events_get_teacherid() {
  1014. global $DB;
  1015. $this->resetAfterTest();
  1016. // Create a new course and enrol a user as editing teacher.
  1017. $generator = $this->getDataGenerator();
  1018. $course = $generator->create_course();
  1019. $teacher = $generator->create_and_enrol($course, 'editingteacher');
  1020. // There's a teacher enrolled in the course, return its user id.
  1021. $userid = upgrade_calendar_events_get_teacherid($course->id);
  1022. // It should return the enrolled teacher by default.
  1023. $this->assertEquals($teacher->id, $userid);
  1024. // Un-enrol teacher from course.
  1025. $instance = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'manual']);
  1026. enrol_get_plugin('manual')->unenrol_user($instance, $teacher->id);
  1027. // Since there are no teachers enrolled in the course, fallback to admin user id.
  1028. $admin = get_admin();
  1029. $userid = upgrade_calendar_events_get_teacherid($course->id);
  1030. $this->assertEquals($admin->id, $userid);
  1031. }
  1032. /**
  1033. * Test the functionality of upgrade_calendar_standard_events_fix() function.
  1034. */
  1035. public function test_upgrade_calendar_standard_events_fix() {
  1036. $this->resetAfterTest();
  1037. $this->setAdminUser();
  1038. $events = create_standard_events(5);
  1039. $eventscount = count($events);
  1040. // Get the events info.
  1041. $info = upgrade_calendar_events_status(false);
  1042. // There should be no standard events to be fixed.
  1043. $this->assertEquals(0, $info['standard']->bad);
  1044. // No events to be fixed, should return false.
  1045. $this->assertFalse(upgrade_calendar_standard_events_fix($info['standard'], false));
  1046. // Run same problematic DB query.
  1047. $this->run_upgrade_step_query();
  1048. // Get the events info.
  1049. $info = upgrade_calendar_events_status(false);
  1050. // There should be 20 events to be fixed (five from each type except user).
  1051. $this->assertEquals($eventscount - 5, $info['standard']->bad);
  1052. // Test the function runtime, passing -1 as end time.
  1053. // It should not be able to fix all events so fast, so some events should remain to be fixed in the next run.
  1054. $result = upgrade_calendar_standard_events_fix($info['standard'], false, -1);
  1055. $this->assertNotFalse($result);
  1056. // Call the function again, this time it will run until all events have been fixed.
  1057. $this->assertFalse(upgrade_calendar_standard_events_fix($info['standard'], false));
  1058. // Get the events info again.
  1059. $info = upgrade_calendar_events_status(false);
  1060. // All standard events should have been recovered.
  1061. // There should be no standard events flagged to be fixed.
  1062. $this->assertEquals(0, $info['standard']->bad);
  1063. }
  1064. /**
  1065. * Test the functionality of upgrade_calendar_subscription_events_fix() function.
  1066. */
  1067. public function test_upgrade_calendar_subscription_events_fix() {
  1068. global $CFG, $DB;
  1069. require_once($CFG->dirroot . '/calendar/lib.php');
  1070. require_once($CFG->dirroot . '/lib/bennu/bennu.inc.php');
  1071. $this->resetAfterTest();
  1072. $this->setAdminUser();
  1073. // Create event subscription.
  1074. $subscription = new stdClass;
  1075. $subscription->name = 'Repeated events';
  1076. $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
  1077. $subscription->eventtype = 'site';
  1078. $id = calendar_add_subscription($subscription);
  1079. // Get repeated events ICS file.
  1080. $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/repeated_events.ics');
  1081. $ical = new iCalendar();
  1082. $ical->unserialize($calendar);
  1083. // Import subscription events.
  1084. calendar_import_events_from_ical($ical, $id);
  1085. // Subscription should have added 18 events.
  1086. $eventscount = $DB->count_records('event');
  1087. // Get the events info.
  1088. $info = upgrade_calendar_events_status(false);
  1089. // There should be no subscription events to be fixed at this point.
  1090. $this->assertEquals(0, $info['subscription']->bad);
  1091. // No events to be fixed, should return false.
  1092. $this->assertFalse(upgrade_calendar_subscription_events_fix($info['subscription'], false));
  1093. // Run same problematic DB query.
  1094. $this->run_upgrade_step_query();
  1095. // Get the events info and assert total number of events is correct.
  1096. $info = upgrade_calendar_events_status(false);
  1097. $subscriptioninfo = $info['subscription'];
  1098. $this->assertEquals($eventscount, $subscriptioninfo->count);
  1099. // Since we have added our subscription as site, all sub events have been affected.
  1100. $this->assertEquals($eventscount, $subscriptioninfo->bad);
  1101. // Test the function runtime, passing -1 as end time.
  1102. // It should not be able to fix all events so fast, so some events should remain to be fixed in the next run.
  1103. $result = upgrade_calendar_subscription_events_fix($subscriptioninfo, false, -1);
  1104. $this->assertNotFalse($result);
  1105. // Call the function again, this time it will run until all events have been fixed.
  1106. $this->assertFalse(upgrade_calendar_subscription_events_fix($subscriptioninfo, false));
  1107. // Get the events info again.
  1108. $info = upgrade_calendar_events_status(false);
  1109. // All standard events should have been recovered.
  1110. // There should be no standard events flagged to be fixed.
  1111. $this->assertEquals(0, $info['subscription']->bad);
  1112. }
  1113. /**
  1114. * Test the functionality of upgrade_calendar_action_events_fix() function.
  1115. */
  1116. public function test_upgrade_calendar_action_events_fix() {
  1117. global $DB;
  1118. $this->resetAfterTest();
  1119. $this->setAdminUser();
  1120. // Create a new course and a choice activity.
  1121. $course = $this->getDataGenerator()->create_course();
  1122. $choice = $this->getDataGenerator()->create_module('choice', ['course' => $course->id]);
  1123. // Create some action events.
  1124. create_action_event(['courseid' => $course->id, 'modulename' => 'choice', 'instance' => $choice->id,
  1125. 'eventtype' => CHOICE_EVENT_TYPE_OPEN]);
  1126. create_action_event(['courseid' => $course->id, 'modulename' => 'choice', 'instance' => $choice->id,
  1127. 'eventtype' => CHOICE_EVENT_TYPE_CLOSE]);
  1128. $eventscount = $DB->count_records('event');
  1129. // Get the events info.
  1130. $info = upgrade_calendar_events_status(false);
  1131. $actioninfo = $info['action'];
  1132. // There should be no standard events to be fixed.
  1133. $this->assertEquals(0, $actioninfo->bad);
  1134. // No events to be fixed, should return false.
  1135. $this->assertFalse(upgrade_calendar_action_events_fix($actioninfo, false));
  1136. // Run same problematic DB query.
  1137. $this->run_upgrade_step_query();
  1138. // Get the events info.
  1139. $info = upgrade_calendar_events_status(false);
  1140. $actioninfo = $info['action'];
  1141. // There should be 2 events to be fixed.
  1142. $this->assertEquals($eventscount, $actioninfo->bad);
  1143. // Test the function runtime, passing -1 as end time.
  1144. // It should not be able to fix all events so fast, so some events should remain to be fixed in the next run.
  1145. $this->assertNotFalse(upgrade_calendar_action_events_fix($actioninfo, false, -1));
  1146. // Call the function again, this time it will run until all events have been fixed.
  1147. $this->assertFalse(upgrade_calendar_action_events_fix($actioninfo, false));
  1148. // Get the events info again.
  1149. $info = upgrade_calendar_events_status(false);
  1150. // All standard events should have been recovered.
  1151. // There should be no standard events flagged to be fixed.
  1152. $this->assertEquals(0, $info['action']->bad);
  1153. }
  1154. /**
  1155. * Test the user override part of upgrade_calendar_override_events_fix() function.
  1156. */
  1157. public function test_upgrade_calendar_user_override_events_fix() {
  1158. global $DB;
  1159. $this->resetAfterTest();
  1160. $this->setAdminUser();
  1161. $generator = $this->getDataGenerator();
  1162. // Create a new course.
  1163. $course = $generator->create_course();
  1164. // Create few users and enrol as students.
  1165. $student1 = $generator->create_and_enrol($course, 'student');
  1166. $student2 = $generator->create_and_enrol($course, 'student');
  1167. $student3 = $generator->create_and_enrol($course, 'student');
  1168. // Create some activities and some override events.
  1169. foreach (['assign', 'lesson', 'quiz'] as $modulename) {
  1170. $instance = $generator->create_module($modulename, ['course' => $course->id]);
  1171. create_user_override_event($modulename, $instance->id, $student1->id);
  1172. create_user_override_event($modulename, $instance->id, $student2->id);
  1173. create_user_override_event($modulename, $instance->id, $student3->id);
  1174. }
  1175. // There should be 9 override events to be fixed (three from each module).
  1176. $eventscount = $DB->count_records('event');
  1177. $this->assertEquals(9, $eventscount);
  1178. // Get the events info.
  1179. $info = upgrade_calendar_events_status(false);
  1180. $overrideinfo = $info['override'];
  1181. // There should be no standard events to be fixed.
  1182. $this->assertEquals(0, $overrideinfo->bad);
  1183. // No events to be fixed, should return false.
  1184. $this->assertFalse(upgrade_calendar_override_events_fix($overrideinfo, false));
  1185. // Run same problematic DB query.
  1186. $this->run_upgrade_step_query();
  1187. // Get the events info.
  1188. $info = upgrade_calendar_events_status(false);
  1189. $overrideinfo = $info['override'];
  1190. // There should be 9 events to be fixed (three from each module).
  1191. $this->assertEquals($eventscount, $overrideinfo->bad);
  1192. // Call the function again, this time it will run until all events have been fixed.
  1193. $this->assertFalse(upgrade_calendar_override_events_fix($overrideinfo, false));
  1194. // Get the events info again.
  1195. $info = upgrade_calendar_events_status(false);
  1196. // All standard events should have been recovered.
  1197. // There should be no standard events flagged to be fixed.
  1198. $this->assertEquals(0, $info['override']->bad);
  1199. }
  1200. /**
  1201. * Test the group override part of upgrade_calendar_override_events_fix() function.
  1202. */
  1203. public function test_upgrade_calendar_group_override_events_fix() {
  1204. global $DB;
  1205. $this->resetAfterTest();
  1206. $this->setAdminUser();
  1207. $generator = $this->getDataGenerator();
  1208. // Create a new course and few groups.
  1209. $course = $generator->create_course();
  1210. $group1 = $generator->create_group(['courseid' => $course->id]);
  1211. $group2 = $generator->create_group(['courseid' => $course->id]);
  1212. $group3 = $generator->create_group(['courseid' => $course->id]);
  1213. // Create some activities and some override events.
  1214. foreach (['assign', 'lesson', 'quiz'] as $modulename) {
  1215. $instance = $generator->create_module($modulename, ['course' => $course->id]);
  1216. create_group_override_event($modulename, $instance->id, $course->id, $group1->id);
  1217. create_group_override_event($modulename, $instance->id, $course->id, $group2->id);
  1218. create_group_override_event($modulename, $instance->id, $course->id, $group3->id);
  1219. }
  1220. // There should be 9 override events to be fixed (three from each module).
  1221. $eventscount = $DB->count_records('event');
  1222. $this->assertEquals(9, $eventscount);
  1223. // Get the events info.
  1224. $info = upgrade_calendar_events_status(false);
  1225. // We classify group overrides as action events since they do not record the userid.
  1226. $groupoverrideinfo = $info['action'];
  1227. // There should be no events to be fixed.
  1228. $this->assertEquals(0, $groupoverrideinfo->bad);
  1229. // No events to be fixed, should return false.
  1230. $this->assertFalse(upgrade_calendar_action_events_fix($groupoverrideinfo, false));
  1231. // Run same problematic DB query.
  1232. $this->run_upgrade_step_query();
  1233. // Get the events info.
  1234. $info = upgrade_calendar_events_status(false);
  1235. $this->assertEquals(9, $info['action']->bad);
  1236. // Call the function again, this time it will run until all events have been fixed.
  1237. $this->assertFalse(upgrade_calendar_action_events_fix($info['action'], false));
  1238. // Since group override events do not set userid, these events should not be flagged to be fixed.
  1239. $this->assertEquals(0, $groupoverrideinfo->bad);
  1240. }
  1241. /**
  1242. * Test the admin_dir_usage check with no admin setting specified.
  1243. */
  1244. public function test_admin_dir_usage_not_set(): void {
  1245. $result = new environment_results("custom_checks");
  1246. $this->assertNull(check_admin_dir_usage($result));
  1247. }
  1248. /**
  1249. * Test the admin_dir_usage check with the default admin setting specified.
  1250. */
  1251. public function test_admin_dir_usage_is_default(): void {
  1252. global $CFG;
  1253. $CFG->admin = 'admin';
  1254. $result = new environment_results("custom_checks");
  1255. $this->assertNull(check_admin_dir_usage($result));
  1256. }
  1257. /**
  1258. * Test the admin_dir_usage check with a custom admin setting specified.
  1259. */
  1260. public function test_admin_dir_usage_non_standard(): void {
  1261. global $CFG;
  1262. $this->resetAfterTest(true);
  1263. $CFG->admin = 'notadmin';
  1264. $result = new environment_results("custom_checks");
  1265. $this->assertInstanceOf(environment_results::class, check_admin_dir_usage($result));
  1266. $this->assertEquals('admin_dir_usage', $result->getInfo());
  1267. $this->assertFalse($result->getStatus());
  1268. }
  1269. }