/mod/workshop/form/numerrors/tests/lib_test.php

https://github.com/markn86/moodle · PHP · 257 lines · 156 code · 41 blank · 60 comment · 1 complexity · 4a8dd9bda2490cdc0f9cbf50c630701d 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_numerrors
  20. * @category phpunit
  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. global $CFG;
  27. require_once($CFG->dirroot . '/mod/workshop/locallib.php');
  28. require_once($CFG->dirroot . '/mod/workshop/form/numerrors/lib.php');
  29. class workshopform_numerrors_strategy_testcase extends advanced_testcase {
  30. /** workshop instance emulation */
  31. protected $workshop;
  32. /** instance of the strategy logic class being tested */
  33. protected $strategy;
  34. /**
  35. * Setup testing environment
  36. */
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->resetAfterTest();
  40. $this->setAdminUser();
  41. $course = $this->getDataGenerator()->create_course();
  42. $workshop = $this->getDataGenerator()->create_module('workshop', array('strategy' => 'numerrors', 'course' => $course));
  43. $cm = get_fast_modinfo($course)->instances['workshop'][$workshop->id];
  44. $this->workshop = new workshop($workshop, $cm, $course);
  45. $this->strategy = new testable_workshop_numerrors_strategy($this->workshop);
  46. }
  47. protected function tearDown(): void {
  48. $this->workshop = null;
  49. $this->strategy = null;
  50. parent::tearDown();
  51. }
  52. public function test_calculate_peer_grade_null_grade() {
  53. // fixture set-up
  54. $this->strategy->dimensions = array();
  55. $this->strategy->mappings = array();
  56. $grades = array();
  57. // exercise SUT
  58. $suggested = $this->strategy->calculate_peer_grade($grades);
  59. // validate
  60. $this->assertNull($suggested);
  61. }
  62. public function test_calculate_peer_grade_no_error() {
  63. // fixture set-up
  64. $this->strategy->dimensions = array();
  65. $this->strategy->dimensions[108] = (object)array('weight' => '1');
  66. $this->strategy->dimensions[109] = (object)array('weight' => '1');
  67. $this->strategy->dimensions[111] = (object)array('weight' => '1');
  68. $this->strategy->mappings = array();
  69. $grades = array();
  70. $grades[] = (object)array('dimensionid' => 108, 'grade' => '1.00000');
  71. $grades[] = (object)array('dimensionid' => 111, 'grade' => '1.00000');
  72. $grades[] = (object)array('dimensionid' => 109, 'grade' => '1.00000');
  73. // exercise SUT
  74. $suggested = $this->strategy->calculate_peer_grade($grades);
  75. // validate
  76. $this->assertEquals($suggested, 100.00000);
  77. }
  78. public function test_calculate_peer_grade_one_error() {
  79. // fixture set-up
  80. $this->strategy->dimensions = array();
  81. $this->strategy->dimensions[108] = (object)array('weight' => '1');
  82. $this->strategy->dimensions[109] = (object)array('weight' => '1');
  83. $this->strategy->dimensions[111] = (object)array('weight' => '1');
  84. $this->strategy->mappings = array(
  85. 1 => (object)array('grade' => '80.00000'),
  86. 2 => (object)array('grade' => '60.00000'),
  87. );
  88. $grades = array();
  89. $grades[] = (object)array('dimensionid' => 108, 'grade' => '1.00000');
  90. $grades[] = (object)array('dimensionid' => 111, 'grade' => '0.00000');
  91. $grades[] = (object)array('dimensionid' => 109, 'grade' => '1.00000');
  92. // exercise SUT
  93. $suggested = $this->strategy->calculate_peer_grade($grades);
  94. // validate
  95. $this->assertEquals($suggested, 80.00000);
  96. }
  97. public function test_calculate_peer_grade_three_errors_same_weight_a() {
  98. // fixture set-up
  99. $this->strategy->dimensions = array();
  100. $this->strategy->dimensions[108] = (object)array('weight' => '1.00000');
  101. $this->strategy->dimensions[109] = (object)array('weight' => '1.00000');
  102. $this->strategy->dimensions[111] = (object)array('weight' => '1.00000');
  103. $this->strategy->mappings = array(
  104. 1 => (object)array('grade' => '80.00000'),
  105. 2 => (object)array('grade' => '60.00000'),
  106. 3 => (object)array('grade' => '10.00000'),
  107. );
  108. $grades = array();
  109. $grades[] = (object)array('dimensionid' => 108, 'grade' => '0.00000');
  110. $grades[] = (object)array('dimensionid' => 111, 'grade' => '0.00000');
  111. $grades[] = (object)array('dimensionid' => 109, 'grade' => '0.00000');
  112. // exercise SUT
  113. $suggested = $this->strategy->calculate_peer_grade($grades);
  114. // validate
  115. $this->assertEquals($suggested, 10.00000);
  116. }
  117. public function test_calculate_peer_grade_three_errors_same_weight_b() {
  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' => '0.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->assertEquals($suggested, 0.00000);
  136. }
  137. public function test_calculate_peer_grade_one_error_weighted() {
  138. // fixture set-up
  139. $this->strategy->dimensions = array();
  140. $this->strategy->dimensions[108] = (object)array('weight' => '1');
  141. $this->strategy->dimensions[109] = (object)array('weight' => '2');
  142. $this->strategy->dimensions[111] = (object)array('weight' => '0');
  143. $this->strategy->mappings = array(
  144. 1 => (object)array('grade' => '66.00000'),
  145. 2 => (object)array('grade' => '33.00000'),
  146. 3 => (object)array('grade' => '0.00000'),
  147. );
  148. $grades = array();
  149. $grades[] = (object)array('dimensionid' => 108, 'grade' => '1.00000');
  150. $grades[] = (object)array('dimensionid' => 111, 'grade' => '1.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->assertEquals($suggested, 33.00000);
  156. }
  157. public function test_calculate_peer_grade_zero_weight() {
  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' => '0.00000');
  171. $grades[] = (object)array('dimensionid' => 109, 'grade' => '1.00000');
  172. // exercise SUT
  173. $suggested = $this->strategy->calculate_peer_grade($grades);
  174. // validate
  175. $this->assertEquals($suggested, 100.00000);
  176. }
  177. public function test_calculate_peer_grade_sum_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' => '3');
  183. $this->strategy->mappings = array(
  184. 1 => (object)array('grade' => '90.00000'),
  185. 2 => (object)array('grade' => '80.00000'),
  186. 3 => (object)array('grade' => '70.00000'),
  187. 4 => (object)array('grade' => '60.00000'),
  188. 5 => (object)array('grade' => '30.00000'),
  189. 6 => (object)array('grade' => '5.00000'),
  190. 7 => (object)array('grade' => '0.00000'),
  191. );
  192. $grades = array();
  193. $grades[] = (object)array('dimensionid' => 108, 'grade' => '0.00000');
  194. $grades[] = (object)array('dimensionid' => 111, 'grade' => '0.00000');
  195. $grades[] = (object)array('dimensionid' => 109, 'grade' => '0.00000');
  196. // exercise SUT
  197. $suggested = $this->strategy->calculate_peer_grade($grades);
  198. // validate
  199. $this->assertEquals($suggested, 5.00000);
  200. }
  201. }
  202. /**
  203. * Test subclass that makes all the protected methods we want to test public
  204. */
  205. class testable_workshop_numerrors_strategy extends workshop_numerrors_strategy {
  206. /** allows to set dimensions manually */
  207. public $dimensions = array();
  208. /** allow to set mappings manually */
  209. public $mappings = array();
  210. /**
  211. * This is where the calculation of suggested grade for submission is done
  212. */
  213. public function calculate_peer_grade(array $grades) {
  214. return parent::calculate_peer_grade($grades);
  215. }
  216. }