PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/workshop/form/numerrors/simpletest/testlib.php

https://github.com/kpike/moodle
PHP | 264 lines | 160 code | 43 blank | 61 comment | 1 complexity | 5fa806fb1264ce96442423a479cf979a MD5 | raw 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 Number of errors grading logic
  18. *
  19. * @package workshopform
  20. * @subpackage numerrors
  21. * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. // Include the code to test
  26. require_once($CFG->dirroot . '/mod/workshop/locallib.php');
  27. require_once($CFG->dirroot . '/mod/workshop/form/numerrors/lib.php');
  28. global $DB;
  29. Mock::generate(get_class($DB), 'mockDB');
  30. /**
  31. * Test subclass that makes all the protected methods we want to test public
  32. */
  33. class testable_workshop_numerrors_strategy extends workshop_numerrors_strategy {
  34. /** allows to set dimensions manually */
  35. public $dimensions = array();
  36. /** allow to set mappings manually */
  37. public $mappings = array();
  38. /**
  39. * This is where the calculation of suggested grade for submission is done
  40. */
  41. public function calculate_peer_grade(array $grades) {
  42. return parent::calculate_peer_grade($grades);
  43. }
  44. }
  45. class workshop_numerrors_strategy_test extends UnitTestCase {
  46. /** real database */
  47. protected $realDB;
  48. /** workshop instance emulation */
  49. protected $workshop;
  50. /** instance of the strategy logic class being tested */
  51. protected $strategy;
  52. /**
  53. * Setup testing environment
  54. */
  55. public function setUp() {
  56. global $DB;
  57. $this->realDB = $DB;
  58. $DB = new mockDB();
  59. $cm = new stdclass();
  60. $course = new stdclass();
  61. $context = new stdclass();
  62. $workshop = (object)array('id' => 42, 'strategy' => 'numerrors');
  63. $this->workshop = new workshop($workshop, $cm, $course, $context);
  64. $this->strategy = new testable_workshop_numerrors_strategy($this->workshop);
  65. }
  66. public function tearDown() {
  67. global $DB;
  68. $DB = $this->realDB;
  69. $this->workshop = null;
  70. $this->strategy = null;
  71. }
  72. public function test_calculate_peer_grade_null_grade() {
  73. // fixture set-up
  74. $this->strategy->dimensions = array();
  75. $this->strategy->mappings = array();
  76. $grades = array();
  77. // exercise SUT
  78. $suggested = $this->strategy->calculate_peer_grade($grades);
  79. // validate
  80. $this->assertNull($suggested);
  81. }
  82. public function test_calculate_peer_grade_no_error() {
  83. // fixture set-up
  84. $this->strategy->dimensions = array();
  85. $this->strategy->dimensions[108] = (object)array('weight' => '1');
  86. $this->strategy->dimensions[109] = (object)array('weight' => '1');
  87. $this->strategy->dimensions[111] = (object)array('weight' => '1');
  88. $this->strategy->mappings = array();
  89. $grades = array();
  90. $grades[] = (object)array('dimensionid' => 108, 'grade' => '1.00000');
  91. $grades[] = (object)array('dimensionid' => 111, 'grade' => '1.00000');
  92. $grades[] = (object)array('dimensionid' => 109, 'grade' => '1.00000');
  93. // exercise SUT
  94. $suggested = $this->strategy->calculate_peer_grade($grades);
  95. // validate
  96. $this->assertEqual($suggested, 100.00000);
  97. }
  98. public function test_calculate_peer_grade_one_error() {
  99. // fixture set-up
  100. $this->strategy->dimensions = array();
  101. $this->strategy->dimensions[108] = (object)array('weight' => '1');
  102. $this->strategy->dimensions[109] = (object)array('weight' => '1');
  103. $this->strategy->dimensions[111] = (object)array('weight' => '1');
  104. $this->strategy->mappings = array(
  105. 1 => (object)array('grade' => '80.00000'),
  106. 2 => (object)array('grade' => '60.00000'),
  107. );
  108. $grades = array();
  109. $grades[] = (object)array('dimensionid' => 108, 'grade' => '1.00000');
  110. $grades[] = (object)array('dimensionid' => 111, 'grade' => '0.00000');
  111. $grades[] = (object)array('dimensionid' => 109, 'grade' => '1.00000');
  112. // exercise SUT
  113. $suggested = $this->strategy->calculate_peer_grade($grades);
  114. // validate
  115. $this->assertEqual($suggested, 80.00000);
  116. }
  117. public function test_calculate_peer_grade_three_errors_same_weight_a() {
  118. // fixture set-up
  119. $this->strategy->dimensions = array();
  120. $this->strategy->dimensions[108] = (object)array('weight' => '1.00000');
  121. $this->strategy->dimensions[109] = (object)array('weight' => '1.00000');
  122. $this->strategy->dimensions[111] = (object)array('weight' => '1.00000');
  123. $this->strategy->mappings = array(
  124. 1 => (object)array('grade' => '80.00000'),
  125. 2 => (object)array('grade' => '60.00000'),
  126. 3 => (object)array('grade' => '10.00000'),
  127. );
  128. $grades = array();
  129. $grades[] = (object)array('dimensionid' => 108, 'grade' => '0.00000');
  130. $grades[] = (object)array('dimensionid' => 111, 'grade' => '0.00000');
  131. $grades[] = (object)array('dimensionid' => 109, 'grade' => '0.00000');
  132. // exercise SUT
  133. $suggested = $this->strategy->calculate_peer_grade($grades);
  134. // validate
  135. $this->assertEqual($suggested, 10.00000);
  136. }
  137. public function test_calculate_peer_grade_three_errors_same_weight_b() {
  138. // fixture set-up
  139. $this->strategy->dimensions = array();
  140. $this->strategy->dimensions[108] = (object)array('weight' => '1.00000');
  141. $this->strategy->dimensions[109] = (object)array('weight' => '1.00000');
  142. $this->strategy->dimensions[111] = (object)array('weight' => '1.00000');
  143. $this->strategy->mappings = array(
  144. 1 => (object)array('grade' => '80.00000'),
  145. 2 => (object)array('grade' => '60.00000'),
  146. 3 => (object)array('grade' => '0.00000'),
  147. );
  148. $grades = array();
  149. $grades[] = (object)array('dimensionid' => 108, 'grade' => '0.00000');
  150. $grades[] = (object)array('dimensionid' => 111, 'grade' => '0.00000');
  151. $grades[] = (object)array('dimensionid' => 109, 'grade' => '0.00000');
  152. // exercise SUT
  153. $suggested = $this->strategy->calculate_peer_grade($grades);
  154. // validate
  155. $this->assertEqual($suggested, 0.00000);
  156. }
  157. public function test_calculate_peer_grade_one_error_weighted() {
  158. // fixture set-up
  159. $this->strategy->dimensions = array();
  160. $this->strategy->dimensions[108] = (object)array('weight' => '1');
  161. $this->strategy->dimensions[109] = (object)array('weight' => '2');
  162. $this->strategy->dimensions[111] = (object)array('weight' => '0');
  163. $this->strategy->mappings = array(
  164. 1 => (object)array('grade' => '66.00000'),
  165. 2 => (object)array('grade' => '33.00000'),
  166. 3 => (object)array('grade' => '0.00000'),
  167. );
  168. $grades = array();
  169. $grades[] = (object)array('dimensionid' => 108, 'grade' => '1.00000');
  170. $grades[] = (object)array('dimensionid' => 111, 'grade' => '1.00000');
  171. $grades[] = (object)array('dimensionid' => 109, 'grade' => '0.00000');
  172. // exercise SUT
  173. $suggested = $this->strategy->calculate_peer_grade($grades);
  174. // validate
  175. $this->assertEqual($suggested, 33.00000);
  176. }
  177. public function test_calculate_peer_grade_zero_weight() {
  178. // fixture set-up
  179. $this->strategy->dimensions = array();
  180. $this->strategy->dimensions[108] = (object)array('weight' => '1');
  181. $this->strategy->dimensions[109] = (object)array('weight' => '2');
  182. $this->strategy->dimensions[111] = (object)array('weight' => '0');
  183. $this->strategy->mappings = array(
  184. 1 => (object)array('grade' => '66.00000'),
  185. 2 => (object)array('grade' => '33.00000'),
  186. 3 => (object)array('grade' => '0.00000'),
  187. );
  188. $grades = array();
  189. $grades[] = (object)array('dimensionid' => 108, 'grade' => '1.00000');
  190. $grades[] = (object)array('dimensionid' => 111, 'grade' => '0.00000');
  191. $grades[] = (object)array('dimensionid' => 109, 'grade' => '1.00000');
  192. // exercise SUT
  193. $suggested = $this->strategy->calculate_peer_grade($grades);
  194. // validate
  195. $this->assertEqual($suggested, 100.00000);
  196. }
  197. public function test_calculate_peer_grade_sum_weight() {
  198. // fixture set-up
  199. $this->strategy->dimensions = array();
  200. $this->strategy->dimensions[108] = (object)array('weight' => '1');
  201. $this->strategy->dimensions[109] = (object)array('weight' => '2');
  202. $this->strategy->dimensions[111] = (object)array('weight' => '3');
  203. $this->strategy->mappings = array(
  204. 1 => (object)array('grade' => '90.00000'),
  205. 2 => (object)array('grade' => '80.00000'),
  206. 3 => (object)array('grade' => '70.00000'),
  207. 4 => (object)array('grade' => '60.00000'),
  208. 5 => (object)array('grade' => '30.00000'),
  209. 6 => (object)array('grade' => '5.00000'),
  210. 7 => (object)array('grade' => '0.00000'),
  211. );
  212. $grades = array();
  213. $grades[] = (object)array('dimensionid' => 108, 'grade' => '0.00000');
  214. $grades[] = (object)array('dimensionid' => 111, 'grade' => '0.00000');
  215. $grades[] = (object)array('dimensionid' => 109, 'grade' => '0.00000');
  216. // exercise SUT
  217. $suggested = $this->strategy->calculate_peer_grade($grades);
  218. // validate
  219. $this->assertEqual($suggested, 5.00000);
  220. }
  221. }