PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/grade/simpletest/testgradecategory.php

http://github.com/moodle/moodle
PHP | 669 lines | 445 code | 118 blank | 106 comment | 3 complexity | a33a8625dd6b3d5a7e90546004cd2eeb MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. ///////////////////////////////////////////////////////////////////////////
  3. // //
  4. // NOTICE OF COPYRIGHT //
  5. // //
  6. // Moodle - Modular Object-Oriented Dynamic Learning Environment //
  7. // http://moodle.org //
  8. // //
  9. // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
  10. // //
  11. // This program is free software; you can redistribute it and/or modify //
  12. // it under the terms of the GNU General Public License as published by //
  13. // the Free Software Foundation; either version 2 of the License, or //
  14. // (at your option) any later version. //
  15. // //
  16. // This program is distributed in the hope that it will be useful, //
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  19. // GNU General Public License for more details: //
  20. // //
  21. // http://www.gnu.org/copyleft/gpl.html //
  22. // //
  23. ///////////////////////////////////////////////////////////////////////////
  24. /**
  25. * Unit tests for grade_category object.
  26. *
  27. * @author nicolas@moodle.com
  28. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  29. * @package moodlecore
  30. */
  31. if (!defined('MOODLE_INTERNAL')) {
  32. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  33. }
  34. require_once($CFG->libdir.'/simpletest/fixtures/gradetest.php');
  35. class grade_category_test extends grade_test {
  36. public function __construct() {
  37. $this->starttime = time();
  38. parent::__construct();
  39. }
  40. public function __destruct() {
  41. $this->endtime = time();
  42. //var_dump($this->endtime-$this->starttime);
  43. parent::__destruct();
  44. }
  45. function test_grade_category() {
  46. $this->sub_test_grade_category_construct();
  47. $this->sub_test_grade_category_build_path();
  48. $this->sub_test_grade_category_fetch();
  49. $this->sub_test_grade_category_fetch_all();
  50. $this->sub_test_grade_category_update();
  51. $this->sub_test_grade_category_delete();
  52. $this->sub_test_grade_category_insert();
  53. $this->sub_test_grade_category_qualifies_for_regrading();
  54. $this->sub_test_grade_category_force_regrading();
  55. $this->sub_test_grade_category_aggregate_grades();
  56. $this->sub_test_grade_category_apply_limit_rules();
  57. $this->sub_test_grade_category_is_aggregationcoef_used();
  58. $this->sub_test_grade_category_fetch_course_tree();
  59. $this->sub_test_grade_category_get_children();
  60. $this->sub_test_grade_category_load_grade_item();
  61. $this->sub_test_grade_category_get_grade_item();
  62. $this->sub_test_grade_category_load_parent_category();
  63. $this->sub_test_grade_category_get_parent_category();
  64. $this->sub_test_grade_category_get_name();
  65. $this->sub_test_grade_category_set_parent();
  66. $this->sub_test_grade_category_get_final();
  67. $this->sub_test_grade_category_get_sortorder();
  68. $this->sub_test_grade_category_set_sortorder();
  69. $this->sub_test_grade_category_is_editable();
  70. $this->sub_test_grade_category_move_after_sortorder();
  71. $this->sub_test_grade_category_is_course_category();
  72. $this->sub_test_grade_category_fetch_course_category();
  73. $this->sub_test_grade_category_is_locked();
  74. $this->sub_test_grade_category_set_locked();
  75. $this->sub_test_grade_category_is_hidden();
  76. $this->sub_test_grade_category_set_hidden();
  77. //this won't work until MDL-11837 is complete
  78. //$this->sub_test_grade_category_generate_grades();
  79. //do this last as adding a second course category messes up the data
  80. $this->sub_test_grade_category_insert_course_category();
  81. }
  82. //adds 3 new grade categories at various depths
  83. function sub_test_grade_category_construct() {
  84. $course_category = grade_category::fetch_course_category($this->courseid);
  85. $params = new stdClass();
  86. $params->courseid = $this->courseid;
  87. $params->fullname = 'unittestcategory4';
  88. $grade_category = new grade_category($params, false);
  89. $grade_category->insert();
  90. $this->grade_categories[] = $grade_category;
  91. $this->assertEqual($params->courseid, $grade_category->courseid);
  92. $this->assertEqual($params->fullname, $grade_category->fullname);
  93. $this->assertEqual(2, $grade_category->depth);
  94. $this->assertEqual("/$course_category->id/$grade_category->id/", $grade_category->path);
  95. $parentpath = $grade_category->path;
  96. // Test a child category
  97. $params->parent = $grade_category->id;
  98. $params->fullname = 'unittestcategory5';
  99. $grade_category = new grade_category($params, false);
  100. $grade_category->insert();
  101. $this->grade_categories[] = $grade_category;
  102. $this->assertEqual(3, $grade_category->depth);
  103. $this->assertEqual($parentpath.$grade_category->id."/", $grade_category->path);
  104. $parentpath = $grade_category->path;
  105. // Test a third depth category
  106. $params->parent = $grade_category->id;
  107. $params->fullname = 'unittestcategory6';
  108. $grade_category = new grade_category($params, false);
  109. $grade_category->insert();
  110. $this->grade_categories[50] = $grade_category;//going to delete this one later hence the special index
  111. $this->assertEqual(4, $grade_category->depth);
  112. $this->assertEqual($parentpath.$grade_category->id."/", $grade_category->path);
  113. }
  114. function sub_test_grade_category_build_path() {
  115. $grade_category = new grade_category($this->grade_categories[1]);
  116. $this->assertTrue(method_exists($grade_category, 'build_path'));
  117. $path = grade_category::build_path($grade_category);
  118. $this->assertEqual($grade_category->path, $path);
  119. }
  120. function sub_test_grade_category_fetch() {
  121. $grade_category = new grade_category();
  122. $this->assertTrue(method_exists($grade_category, 'fetch'));
  123. $grade_category = grade_category::fetch(array('id'=>$this->grade_categories[0]->id));
  124. $this->assertEqual($this->grade_categories[0]->id, $grade_category->id);
  125. $this->assertEqual($this->grade_categories[0]->fullname, $grade_category->fullname);
  126. }
  127. function sub_test_grade_category_fetch_all() {
  128. $grade_category = new grade_category();
  129. $this->assertTrue(method_exists($grade_category, 'fetch_all'));
  130. $grade_categories = grade_category::fetch_all(array('courseid'=>$this->courseid));
  131. $this->assertEqual(count($this->grade_categories), count($grade_categories)-1);
  132. }
  133. function sub_test_grade_category_update() {
  134. global $DB;
  135. $grade_category = new grade_category($this->grade_categories[0]);
  136. $this->assertTrue(method_exists($grade_category, 'update'));
  137. $grade_category->fullname = 'Updated info for this unittest grade_category';
  138. $grade_category->path = null; // path must be recalculated if missing
  139. $grade_category->depth = null;
  140. $grade_category->aggregation = GRADE_AGGREGATE_MAX; // should force regrading
  141. $grade_item = $grade_category->get_grade_item();
  142. $this->assertEqual(0, $grade_item->needsupdate);
  143. $this->assertTrue($grade_category->update());
  144. $fullname = $DB->get_field('grade_categories', 'fullname', array('id' => $this->grade_categories[0]->id));
  145. $this->assertEqual($grade_category->fullname, $fullname);
  146. $path = $DB->get_field('grade_categories', 'path', array('id' => $this->grade_categories[0]->id));
  147. $this->assertEqual($grade_category->path, $path);
  148. $depth = $DB->get_field('grade_categories', 'depth', array('id' => $this->grade_categories[0]->id));
  149. $this->assertEqual($grade_category->depth, $depth);
  150. $grade_item = $grade_category->get_grade_item();
  151. $this->assertEqual(1, $grade_item->needsupdate);
  152. }
  153. function sub_test_grade_category_delete() {
  154. global $DB;
  155. $grade_category = new grade_category($this->grade_categories[50]);
  156. $this->assertTrue(method_exists($grade_category, 'delete'));
  157. $this->assertTrue($grade_category->delete());
  158. $this->assertFalse($DB->get_record('grade_categories', array('id' => $grade_category->id)));
  159. }
  160. function sub_test_grade_category_insert() {
  161. $course_category = grade_category::fetch_course_category($this->courseid);
  162. $grade_category = new grade_category();
  163. $this->assertTrue(method_exists($grade_category, 'insert'));
  164. $grade_category->fullname = 'unittestcategory4';
  165. $grade_category->courseid = $this->courseid;
  166. $grade_category->aggregation = GRADE_AGGREGATE_MEAN;
  167. $grade_category->aggregateonlygraded = 1;
  168. $grade_category->keephigh = 100;
  169. $grade_category->droplow = 10;
  170. $grade_category->hidden = 0;
  171. $grade_category->parent = $this->grade_categories[1]->id; //sub_test_grade_category_delete() removed the category at 0
  172. $grade_category->insert();
  173. $this->assertEqual('/'.$course_category->id.'/'.$this->grade_categories[1]->parent.'/'.$this->grade_categories[1]->id.'/'.$grade_category->id.'/', $grade_category->path);
  174. $this->assertEqual(4, $grade_category->depth);
  175. $last_grade_category = end($this->grade_categories);
  176. $this->assertFalse(empty($grade_category->grade_item));
  177. $this->assertEqual($grade_category->id, $grade_category->grade_item->iteminstance);
  178. $this->assertEqual('category', $grade_category->grade_item->itemtype);
  179. $this->assertEqual($grade_category->id, $last_grade_category->id + 1);
  180. $this->assertFalse(empty($grade_category->timecreated));
  181. $this->assertFalse(empty($grade_category->timemodified));
  182. }
  183. function sub_test_grade_category_qualifies_for_regrading() {
  184. $grade_category = new grade_category($this->grade_categories[1]);
  185. $this->assertTrue(method_exists($grade_category, 'qualifies_for_regrading'));
  186. $this->assertFalse($grade_category->qualifies_for_regrading());
  187. $grade_category->aggregation = GRADE_AGGREGATE_MAX;
  188. $this->assertTrue($grade_category->qualifies_for_regrading());
  189. $grade_category = new grade_category($this->grade_categories[1]);
  190. $grade_category->droplow = 99;
  191. $this->assertTrue($grade_category->qualifies_for_regrading());
  192. $grade_category = new grade_category($this->grade_categories[1]);
  193. $grade_category->keephigh = 99;
  194. $this->assertTrue($grade_category->qualifies_for_regrading());
  195. }
  196. function sub_test_grade_category_force_regrading() {
  197. $grade_category = new grade_category($this->grade_categories[1]);
  198. $this->assertTrue(method_exists($grade_category, 'force_regrading'));
  199. $grade_category->load_grade_item();
  200. $this->assertEqual(0, $grade_category->grade_item->needsupdate);
  201. $grade_category->force_regrading();
  202. $grade_category->grade_item = null;
  203. $grade_category->load_grade_item();
  204. $this->assertEqual(1, $grade_category->grade_item->needsupdate);
  205. }
  206. /**
  207. * Tests the calculation of grades using the various aggregation methods with and without hidden grades
  208. * This will not work entirely until MDL-11837 is done
  209. * @global type $DB
  210. */
  211. function sub_test_grade_category_generate_grades() {
  212. global $DB;
  213. //inserting some special grade items to make testing the final grade calculation easier
  214. $params->courseid = $this->courseid;
  215. $params->fullname = 'unittestgradecalccategory';
  216. $params->aggregation = GRADE_AGGREGATE_MEAN;
  217. $params->aggregateonlygraded = 0;
  218. $grade_category = new grade_category($params, false);
  219. $grade_category->insert();
  220. $this->assertTrue(method_exists($grade_category, 'generate_grades'));
  221. $grade_category->load_grade_item();
  222. $cgi = $grade_category->get_grade_item();
  223. $cgi->grademin = 0;
  224. $cgi->grademax = 20;//3 grade items out of 10 but category is out of 20 to force scaling to occur
  225. $cgi->update();
  226. //3 grade items each with a maximum grade of 10
  227. $grade_items = array();
  228. for ($i=0; $i<3; $i++) {
  229. $grade_items[$i] = new grade_item();
  230. $grade_items[$i]->courseid = $this->courseid;
  231. $grade_items[$i]->categoryid = $grade_category->id;
  232. $grade_items[$i]->itemname = 'manual grade_item '.$i;
  233. $grade_items[$i]->itemtype = 'manual';
  234. $grade_items[$i]->itemnumber = 0;
  235. $grade_items[$i]->needsupdate = false;
  236. $grade_items[$i]->gradetype = GRADE_TYPE_VALUE;
  237. $grade_items[$i]->grademin = 0;
  238. $grade_items[$i]->grademax = 10;
  239. $grade_items[$i]->iteminfo = 'Manual grade item used for unit testing';
  240. $grade_items[$i]->timecreated = mktime();
  241. $grade_items[$i]->timemodified = mktime();
  242. //used as the weight by weighted mean and as extra credit by mean with extra credit
  243. //Will be 0, 1 and 2
  244. $grade_items[$i]->aggregationcoef = $i;
  245. $grade_items[$i]->insert();
  246. }
  247. //a grade for each grade item
  248. $grade_grades = array();
  249. for ($i=0; $i<3; $i++) {
  250. $grade_grades[$i] = new grade_grade();
  251. $grade_grades[$i]->itemid = $grade_items[$i]->id;
  252. $grade_grades[$i]->userid = $this->userid;
  253. $grade_grades[$i]->rawgrade = ($i+1)*2;//produce grade grades of 2, 4 and 6
  254. $grade_grades[$i]->finalgrade = ($i+1)*2;
  255. $grade_grades[$i]->timecreated = mktime();
  256. $grade_grades[$i]->timemodified = mktime();
  257. $grade_grades[$i]->information = '1 of 2 grade_grades';
  258. $grade_grades[$i]->informationformat = FORMAT_PLAIN;
  259. $grade_grades[$i]->feedback = 'Good, but not good enough..';
  260. $grade_grades[$i]->feedbackformat = FORMAT_PLAIN;
  261. $grade_grades[$i]->insert();
  262. }
  263. //3 grade items with 1 grade_grade each.
  264. //grade grades have the values 2, 4 and 6
  265. //First correct answer is the aggregate with all 3 grades
  266. //Second correct answer is with the first grade (value 2) hidden
  267. $this->helper_test_grade_agg_method($grade_category, $grade_items, $grade_grades, GRADE_AGGREGATE_MEDIAN, 'GRADE_AGGREGATE_MEDIAN', 8, 8);
  268. $this->helper_test_grade_agg_method($grade_category, $grade_items, $grade_grades, GRADE_AGGREGATE_MAX, 'GRADE_AGGREGATE_MAX', 12, 12);
  269. $this->helper_test_grade_agg_method($grade_category, $grade_items, $grade_grades, GRADE_AGGREGATE_MODE, 'GRADE_AGGREGATE_MODE', 12, 12);
  270. //weighted mean. note grade totals are rounded to an int to prevent rounding discrepancies. correct final grade isnt actually exactly 10
  271. //3 items with grades 2, 4 and 6 with weights 0, 1 and 2 and all out of 10. then doubled to be out of 20.
  272. $this->helper_test_grade_agg_method($grade_category, $grade_items, $grade_grades, GRADE_AGGREGATE_WEIGHTED_MEAN, 'GRADE_AGGREGATE_WEIGHTED_MEAN', 10, 10);
  273. //simple weighted mean
  274. //3 items with grades 2, 4 and 6 equally weighted and all out of 10. then doubled to be out of 20.
  275. $this->helper_test_grade_agg_method($grade_category, $grade_items, $grade_grades, GRADE_AGGREGATE_WEIGHTED_MEAN2, 'GRADE_AGGREGATE_WEIGHTED_MEAN2', 8, 10);
  276. //mean of grades with extra credit
  277. //3 items with grades 2, 4 and 6 with extra credit 0, 1 and 2 equally weighted and all out of 10. then doubled to be out of 20.
  278. $this->helper_test_grade_agg_method($grade_category, $grade_items, $grade_grades, GRADE_AGGREGATE_EXTRACREDIT_MEAN, 'GRADE_AGGREGATE_EXTRACREDIT_MEAN', 10, 13);
  279. //aggregation tests the are affected by a hidden grade currently dont work as we dont store the altered grade in the database
  280. //instead an in memory recalculation is done. This should be remedied by MDL-11837
  281. //fails with 1 grade hidden. still reports 8 as being correct
  282. $this->helper_test_grade_agg_method($grade_category, $grade_items, $grade_grades, GRADE_AGGREGATE_MEAN, 'GRADE_AGGREGATE_MEAN', 8, 10);
  283. //fails with 1 grade hidden. still reports 4 as being correct
  284. $this->helper_test_grade_agg_method($grade_category, $grade_items, $grade_grades, GRADE_AGGREGATE_MIN, 'GRADE_AGGREGATE_MIN', 4, 8);
  285. //fails with 1 grade hidden. still reports 12 as being correct
  286. $this->helper_test_grade_agg_method($grade_category, $grade_items, $grade_grades, GRADE_AGGREGATE_SUM, 'GRADE_AGGREGATE_SUM', 12, 10);
  287. }
  288. /**
  289. * Test grade category aggregation using the supplied grade objects and aggregation method
  290. * @param grade_category $grade_category the category to be tested
  291. * @param array $grade_items array of instance of grade_item
  292. * @param array $grade_grades array of instances of grade_grade
  293. * @param int $aggmethod the aggregation method to apply ie GRADE_AGGREGATE_MEAN
  294. * @param string $aggmethodname the name of the aggregation method to apply. Used to display any test failure messages
  295. * @param int $correct1 the correct final grade for the category with NO items hidden
  296. * @param int $correct2 the correct final grade for the category with the grade at $grade_grades[0] hidden
  297. * @return void
  298. */
  299. function helper_test_grade_agg_method($grade_category, $grade_items, $grade_grades, $aggmethod, $aggmethodname, $correct1, $correct2) {
  300. global $DB;
  301. $grade_category->aggregation = $aggmethod;
  302. $grade_category->update();
  303. //check grade_item isnt hidden from a previous test
  304. $grade_items[0]->set_hidden(0, true);
  305. $this->helper_test_grade_aggregation_result($grade_category, $correct1, 'Testing aggregation method('.$aggmethodname.') with no items hidden %s');
  306. //hide the grade item with grade of 2
  307. $grade_items[0]->set_hidden(1, true);
  308. $this->helper_test_grade_aggregation_result($grade_category, $correct2, 'Testing aggregation method('.$aggmethodname.') with 1 item hidden %s');
  309. }
  310. /**
  311. * Verify the value of the category grade item for $this->userid
  312. * @param grade_category $grade_category the category to be tested
  313. * @param int $correctgrade the expected grade
  314. * @param string msg The message that should be displayed if the correct grade is not found
  315. * @return void
  316. */
  317. function helper_test_grade_aggregation_result($grade_category, $correctgrade, $msg) {
  318. global $DB;
  319. $category_grade_item = $grade_category->get_grade_item();
  320. //this creates all the grade_grades we need
  321. grade_regrade_final_grades($this->courseid);
  322. $grade = $DB->get_record('grade_grades', array('itemid'=>$category_grade_item->id, 'userid'=>$this->userid));
  323. $this->assertWithinMargin($grade->rawgrade, $grade->rawgrademin, $grade->rawgrademax);
  324. $this->assertEqual(intval($correctgrade), intval($grade->finalgrade), $msg);
  325. /*
  326. * TODO this doesnt work as the grade_grades created by $grade_category->generate_grades(); dont
  327. * observe the category's max grade
  328. //delete the grade_grades for the category itself and check they get recreated correctly
  329. $DB->delete_records('grade_grades', array('itemid'=>$category_grade_item->id));
  330. $grade_category->generate_grades();
  331. $grade = $DB->get_record('grade_grades', array('itemid'=>$category_grade_item->id, 'userid'=>$this->userid));
  332. $this->assertWithinMargin($grade->rawgrade, $grade->rawgrademin, $grade->rawgrademax);
  333. $this->assertEqual(intval($correctgrade), intval($grade->finalgrade), $msg);
  334. *
  335. */
  336. }
  337. function sub_test_grade_category_aggregate_grades() {
  338. $category = new grade_category($this->grade_categories[0]);
  339. $this->assertTrue(method_exists($category, 'aggregate_grades'));
  340. // tested more fully via test_grade_category_generate_grades()
  341. }
  342. function sub_test_grade_category_apply_limit_rules() {
  343. $items[$this->grade_items[0]->id] = new grade_item($this->grade_items[0], false);
  344. $items[$this->grade_items[1]->id] = new grade_item($this->grade_items[1], false);
  345. $items[$this->grade_items[2]->id] = new grade_item($this->grade_items[2], false);
  346. $items[$this->grade_items[4]->id] = new grade_item($this->grade_items[4], false);
  347. $category = new grade_category();
  348. $category->droplow = 2;
  349. $grades = array($this->grade_items[0]->id=>5.374,
  350. $this->grade_items[1]->id=>9.4743,
  351. $this->grade_items[2]->id=>2.5474,
  352. $this->grade_items[4]->id=>7.3754);
  353. $category->apply_limit_rules($grades, $items);
  354. $this->assertEqual(count($grades), 2);
  355. $this->assertEqual($grades[$this->grade_items[1]->id], 9.4743);
  356. $this->assertEqual($grades[$this->grade_items[4]->id], 7.3754);
  357. $category = new grade_category();
  358. $category->keephigh = 1;
  359. $category->droplow = 0;
  360. $grades = array($this->grade_items[0]->id=>5.374,
  361. $this->grade_items[1]->id=>9.4743,
  362. $this->grade_items[2]->id=>2.5474,
  363. $this->grade_items[4]->id=>7.3754);
  364. $category->apply_limit_rules($grades, $items);
  365. $this->assertEqual(count($grades), 1);
  366. $grade = reset($grades);
  367. $this->assertEqual(9.4743, $grade);
  368. $category = new grade_category();
  369. $category->droplow = 2;
  370. $category->aggregation = GRADE_AGGREGATE_SUM;
  371. $items[$this->grade_items[2]->id]->aggregationcoef = 1;
  372. $grades = array($this->grade_items[0]->id=>5.374,
  373. $this->grade_items[1]->id=>9.4743,
  374. $this->grade_items[2]->id=>2.5474,
  375. $this->grade_items[4]->id=>7.3754);
  376. $category->apply_limit_rules($grades, $items);
  377. $this->assertEqual(count($grades), 2);
  378. $this->assertEqual($grades[$this->grade_items[1]->id], 9.4743);
  379. $this->assertEqual($grades[$this->grade_items[2]->id], 2.5474);
  380. $category = new grade_category();
  381. $category->keephigh = 1;
  382. $category->droplow = 0;
  383. $category->aggregation = GRADE_AGGREGATE_SUM;
  384. $items[$this->grade_items[2]->id]->aggregationcoef = 1;
  385. $grades = array($this->grade_items[0]->id=>5.374,
  386. $this->grade_items[1]->id=>9.4743,
  387. $this->grade_items[2]->id=>2.5474,
  388. $this->grade_items[4]->id=>7.3754);
  389. $category->apply_limit_rules($grades, $items);
  390. $this->assertEqual(count($grades), 2);
  391. $this->assertEqual($grades[$this->grade_items[1]->id], 9.4743);
  392. $this->assertEqual($grades[$this->grade_items[2]->id], 2.5474);
  393. }
  394. /**
  395. * TODO implement
  396. */
  397. function sub_test_grade_category_is_aggregationcoef_used() {
  398. }
  399. function sub_test_grade_category_fetch_course_tree() {
  400. $category = new grade_category();
  401. $this->assertTrue(method_exists($category, 'fetch_course_tree'));
  402. //TODO: add some tests
  403. }
  404. function sub_test_grade_category_get_children() {
  405. $course_category = grade_category::fetch_course_category($this->courseid);
  406. $category = new grade_category($this->grade_categories[0]);
  407. $this->assertTrue(method_exists($category, 'get_children'));
  408. $children_array = $category->get_children(0);
  409. $this->assertTrue(is_array($children_array));
  410. $this->assertFalse(empty($children_array[2]));
  411. $this->assertFalse(empty($children_array[2]['object']));
  412. $this->assertFalse(empty($children_array[2]['children']));
  413. $this->assertEqual($this->grade_categories[1]->id, $children_array[2]['object']->id);
  414. $this->assertEqual($this->grade_categories[2]->id, $children_array[5]['object']->id);
  415. $this->assertEqual($this->grade_items[0]->id, $children_array[2]['children'][3]['object']->id);
  416. $this->assertEqual($this->grade_items[1]->id, $children_array[2]['children'][4]['object']->id);
  417. $this->assertEqual($this->grade_items[2]->id, $children_array[5]['children'][6]['object']->id);
  418. }
  419. function sub_test_grade_category_load_grade_item() {
  420. $category = new grade_category($this->grade_categories[0]);
  421. $this->assertTrue(method_exists($category, 'load_grade_item'));
  422. $this->assertEqual(null, $category->grade_item);
  423. $category->load_grade_item();
  424. $this->assertEqual($this->grade_items[3]->id, $category->grade_item->id);
  425. }
  426. function sub_test_grade_category_get_grade_item() {
  427. $category = new grade_category($this->grade_categories[0]);
  428. $this->assertTrue(method_exists($category, 'get_grade_item'));
  429. $grade_item = $category->get_grade_item();
  430. $this->assertEqual($this->grade_items[3]->id, $grade_item->id);
  431. }
  432. function sub_test_grade_category_load_parent_category() {
  433. $category = new grade_category($this->grade_categories[1]);
  434. $this->assertTrue(method_exists($category, 'load_parent_category'));
  435. $this->assertEqual(null, $category->parent_category);
  436. $category->load_parent_category();
  437. $this->assertEqual($this->grade_categories[0]->id, $category->parent_category->id);
  438. }
  439. function sub_test_grade_category_get_parent_category() {
  440. $category = new grade_category($this->grade_categories[1]);
  441. $this->assertTrue(method_exists($category, 'get_parent_category'));
  442. $parent_category = $category->get_parent_category();
  443. $this->assertEqual($this->grade_categories[0]->id, $parent_category->id);
  444. }
  445. function sub_test_grade_category_get_name() {
  446. $category = new grade_category($this->grade_categories[0]);
  447. $this->assertTrue(method_exists($category, 'get_name'));
  448. $this->assertEqual($this->grade_categories[0]->fullname, $category->get_name());
  449. }
  450. function sub_test_grade_category_set_parent() {
  451. $category = new grade_category($this->grade_categories[1]);
  452. $this->assertTrue(method_exists($category, 'set_parent'));
  453. // TODO: implement detailed tests
  454. $course_category = grade_category::fetch_course_category($this->courseid);
  455. $this->assertTrue($category->set_parent($course_category->id));
  456. $this->assertEqual($course_category->id, $category->parent);
  457. }
  458. function sub_test_grade_category_get_final() {
  459. $category = new grade_category($this->grade_categories[0]);
  460. $this->assertTrue(method_exists($category, 'get_final'));
  461. $category->load_grade_item();
  462. $this->assertEqual($category->get_final(), $category->grade_item->get_final());
  463. }
  464. function sub_test_grade_category_get_sortorder() {
  465. $category = new grade_category($this->grade_categories[0]);
  466. $this->assertTrue(method_exists($category, 'get_sortorder'));
  467. $category->load_grade_item();
  468. $this->assertEqual($category->get_sortorder(), $category->grade_item->get_sortorder());
  469. }
  470. function sub_test_grade_category_set_sortorder() {
  471. $category = new grade_category($this->grade_categories[0]);
  472. $this->assertTrue(method_exists($category, 'set_sortorder'));
  473. $category->load_grade_item();
  474. $this->assertEqual($category->set_sortorder(10), $category->grade_item->set_sortorder(10));
  475. }
  476. function sub_test_grade_category_move_after_sortorder() {
  477. $category = new grade_category($this->grade_categories[0]);
  478. $this->assertTrue(method_exists($category, 'move_after_sortorder'));
  479. $category->load_grade_item();
  480. $this->assertEqual($category->move_after_sortorder(10), $category->grade_item->move_after_sortorder(10));
  481. }
  482. function sub_test_grade_category_is_course_category() {
  483. $category = grade_category::fetch_course_category($this->courseid);
  484. $this->assertTrue(method_exists($category, 'is_course_category'));
  485. $this->assertTrue($category->is_course_category());
  486. }
  487. function sub_test_grade_category_fetch_course_category() {
  488. $category = new grade_category();
  489. $this->assertTrue(method_exists($category, 'fetch_course_category'));
  490. $category = grade_category::fetch_course_category($this->courseid);
  491. $this->assertTrue(empty($category->parent));
  492. }
  493. /**
  494. * TODO implement
  495. */
  496. function sub_test_grade_category_is_editable() {
  497. }
  498. function sub_test_grade_category_is_locked() {
  499. $category = new grade_category($this->grade_categories[0]);
  500. $this->assertTrue(method_exists($category, 'is_locked'));
  501. $category->load_grade_item();
  502. $this->assertEqual($category->is_locked(), $category->grade_item->is_locked());
  503. }
  504. function sub_test_grade_category_set_locked() {
  505. $category = new grade_category($this->grade_categories[0]);
  506. $this->assertTrue(method_exists($category, 'set_locked'));
  507. //will return false as cannot lock a grade that needs updating
  508. $this->assertFalse($category->set_locked(1));
  509. grade_regrade_final_grades($this->courseid);
  510. //get the category from the db again
  511. $category = new grade_category($this->grade_categories[0]);
  512. $this->assertTrue($category->set_locked(1));
  513. }
  514. function sub_test_grade_category_is_hidden() {
  515. $category = new grade_category($this->grade_categories[0]);
  516. $this->assertTrue(method_exists($category, 'is_hidden'));
  517. $category->load_grade_item();
  518. $this->assertEqual($category->is_hidden(), $category->grade_item->is_hidden());
  519. }
  520. function sub_test_grade_category_set_hidden() {
  521. $category = new grade_category($this->grade_categories[0]);
  522. $this->assertTrue(method_exists($category, 'set_hidden'));
  523. $category->set_hidden(1);
  524. $category->load_grade_item();
  525. $this->assertEqual(true, $category->grade_item->is_hidden());
  526. }
  527. //beware: adding a duplicate course category messes up the data in a way that's hard to recover from
  528. function sub_test_grade_category_insert_course_category() {
  529. $grade_category = new grade_category();
  530. $this->assertTrue(method_exists($grade_category, 'insert_course_category'));
  531. $id = $grade_category->insert_course_category($this->courseid);
  532. $this->assertNotNull($id);
  533. $this->assertEqual('?', $grade_category->fullname);
  534. $this->assertEqual(GRADE_AGGREGATE_WEIGHTED_MEAN2, $grade_category->aggregation);
  535. $this->assertEqual("/$id/", $grade_category->path);
  536. $this->assertEqual(1, $grade_category->depth);
  537. $this->assertNull($grade_category->parent);
  538. }
  539. function generate_random_raw_grade($item, $userid) {
  540. $grade = new grade_grade();
  541. $grade->itemid = $item->id;
  542. $grade->userid = $userid;
  543. $grade->grademin = 0;
  544. $grade->grademax = 1;
  545. $valuetype = "grade$item->gradetype";
  546. $grade->rawgrade = rand(0, 1000) / 1000;
  547. $grade->insert();
  548. return $grade->rawgrade;
  549. }
  550. }