PageRenderTime 50ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/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

Large files files are truncated, but you can click here to view the full file

  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() {

Large files files are truncated, but you can click here to view the full file