Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Unit tests for Number of errors grading logic
  20   *
  21   * @package    workshopform_numerrors
  22   * @category   phpunit
  23   * @copyright  2009 David Mudrak <david.mudrak@gmail.com>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  // Include the code to test
  30  global $CFG;
  31  require_once($CFG->dirroot . '/mod/workshop/locallib.php');
  32  require_once($CFG->dirroot . '/mod/workshop/form/numerrors/lib.php');
  33  
  34  
  35  class workshopform_numerrors_strategy_testcase extends advanced_testcase {
  36  
  37      /** workshop instance emulation */
  38      protected $workshop;
  39  
  40      /** instance of the strategy logic class being tested */
  41      protected $strategy;
  42  
  43      /**
  44       * Setup testing environment
  45       */
  46      protected function setUp(): void {
  47          parent::setUp();
  48          $this->resetAfterTest();
  49          $this->setAdminUser();
  50          $course = $this->getDataGenerator()->create_course();
  51          $workshop = $this->getDataGenerator()->create_module('workshop', array('strategy' => 'numerrors', 'course' => $course));
  52          $cm = get_fast_modinfo($course)->instances['workshop'][$workshop->id];
  53          $this->workshop = new workshop($workshop, $cm, $course);
  54          $this->strategy = new testable_workshop_numerrors_strategy($this->workshop);
  55      }
  56  
  57      protected function tearDown(): void {
  58          $this->workshop = null;
  59          $this->strategy = null;
  60          parent::tearDown();
  61      }
  62  
  63      public function test_calculate_peer_grade_null_grade() {
  64          // fixture set-up
  65          $this->strategy->dimensions   = array();
  66          $this->strategy->mappings     = array();
  67          $grades = array();
  68          // exercise SUT
  69          $suggested = $this->strategy->calculate_peer_grade($grades);
  70          // validate
  71          $this->assertNull($suggested);
  72      }
  73  
  74      public function test_calculate_peer_grade_no_error() {
  75          // fixture set-up
  76          $this->strategy->dimensions      = array();
  77          $this->strategy->dimensions[108] = (object)array('weight' => '1');
  78          $this->strategy->dimensions[109] = (object)array('weight' => '1');
  79          $this->strategy->dimensions[111] = (object)array('weight' => '1');
  80          $this->strategy->mappings        = array();
  81          $grades = array();
  82          $grades[] = (object)array('dimensionid' => 108, 'grade' => '1.00000');
  83          $grades[] = (object)array('dimensionid' => 111, 'grade' => '1.00000');
  84          $grades[] = (object)array('dimensionid' => 109, 'grade' => '1.00000');
  85          // exercise SUT
  86          $suggested = $this->strategy->calculate_peer_grade($grades);
  87          // validate
  88          $this->assertEquals($suggested, 100.00000);
  89      }
  90  
  91      public function test_calculate_peer_grade_one_error() {
  92          // fixture set-up
  93          $this->strategy->dimensions      = array();
  94          $this->strategy->dimensions[108] = (object)array('weight' => '1');
  95          $this->strategy->dimensions[109] = (object)array('weight' => '1');
  96          $this->strategy->dimensions[111] = (object)array('weight' => '1');
  97  
  98          $this->strategy->mappings        = array(
  99              1 => (object)array('grade' => '80.00000'),
 100              2 => (object)array('grade' => '60.00000'),
 101          );
 102  
 103          $grades = array();
 104          $grades[] = (object)array('dimensionid' => 108, 'grade' => '1.00000');
 105          $grades[] = (object)array('dimensionid' => 111, 'grade' => '0.00000');
 106          $grades[] = (object)array('dimensionid' => 109, 'grade' => '1.00000');
 107  
 108          // exercise SUT
 109          $suggested = $this->strategy->calculate_peer_grade($grades);
 110          // validate
 111          $this->assertEquals($suggested, 80.00000);
 112      }
 113  
 114      public function test_calculate_peer_grade_three_errors_same_weight_a() {
 115          // fixture set-up
 116          $this->strategy->dimensions      = array();
 117          $this->strategy->dimensions[108] = (object)array('weight' => '1.00000');
 118          $this->strategy->dimensions[109] = (object)array('weight' => '1.00000');
 119          $this->strategy->dimensions[111] = (object)array('weight' => '1.00000');
 120  
 121          $this->strategy->mappings        = array(
 122              1 => (object)array('grade' => '80.00000'),
 123              2 => (object)array('grade' => '60.00000'),
 124              3 => (object)array('grade' => '10.00000'),
 125          );
 126  
 127          $grades = array();
 128          $grades[] = (object)array('dimensionid' => 108, 'grade' => '0.00000');
 129          $grades[] = (object)array('dimensionid' => 111, 'grade' => '0.00000');
 130          $grades[] = (object)array('dimensionid' => 109, 'grade' => '0.00000');
 131  
 132          // exercise SUT
 133          $suggested = $this->strategy->calculate_peer_grade($grades);
 134          // validate
 135          $this->assertEquals($suggested, 10.00000);
 136      }
 137  
 138      public function test_calculate_peer_grade_three_errors_same_weight_b() {
 139          // fixture set-up
 140          $this->strategy->dimensions      = array();
 141          $this->strategy->dimensions[108] = (object)array('weight' => '1.00000');
 142          $this->strategy->dimensions[109] = (object)array('weight' => '1.00000');
 143          $this->strategy->dimensions[111] = (object)array('weight' => '1.00000');
 144  
 145          $this->strategy->mappings        = array(
 146              1 => (object)array('grade' => '80.00000'),
 147              2 => (object)array('grade' => '60.00000'),
 148              3 => (object)array('grade' => '0.00000'),
 149          );
 150  
 151          $grades = array();
 152          $grades[] = (object)array('dimensionid' => 108, 'grade' => '0.00000');
 153          $grades[] = (object)array('dimensionid' => 111, 'grade' => '0.00000');
 154          $grades[] = (object)array('dimensionid' => 109, 'grade' => '0.00000');
 155  
 156          // exercise SUT
 157          $suggested = $this->strategy->calculate_peer_grade($grades);
 158          // validate
 159          $this->assertEquals($suggested, 0.00000);
 160      }
 161  
 162      public function test_calculate_peer_grade_one_error_weighted() {
 163          // fixture set-up
 164          $this->strategy->dimensions      = array();
 165          $this->strategy->dimensions[108] = (object)array('weight' => '1');
 166          $this->strategy->dimensions[109] = (object)array('weight' => '2');
 167          $this->strategy->dimensions[111] = (object)array('weight' => '0');
 168  
 169          $this->strategy->mappings        = array(
 170              1 => (object)array('grade' => '66.00000'),
 171              2 => (object)array('grade' => '33.00000'),
 172              3 => (object)array('grade' => '0.00000'),
 173          );
 174  
 175          $grades = array();
 176          $grades[] = (object)array('dimensionid' => 108, 'grade' => '1.00000');
 177          $grades[] = (object)array('dimensionid' => 111, 'grade' => '1.00000');
 178          $grades[] = (object)array('dimensionid' => 109, 'grade' => '0.00000');
 179  
 180          // exercise SUT
 181          $suggested = $this->strategy->calculate_peer_grade($grades);
 182          // validate
 183          $this->assertEquals($suggested, 33.00000);
 184      }
 185  
 186      public function test_calculate_peer_grade_zero_weight() {
 187          // fixture set-up
 188          $this->strategy->dimensions      = array();
 189          $this->strategy->dimensions[108] = (object)array('weight' => '1');
 190          $this->strategy->dimensions[109] = (object)array('weight' => '2');
 191          $this->strategy->dimensions[111] = (object)array('weight' => '0');
 192  
 193          $this->strategy->mappings        = array(
 194              1 => (object)array('grade' => '66.00000'),
 195              2 => (object)array('grade' => '33.00000'),
 196              3 => (object)array('grade' => '0.00000'),
 197          );
 198  
 199          $grades = array();
 200          $grades[] = (object)array('dimensionid' => 108, 'grade' => '1.00000');
 201          $grades[] = (object)array('dimensionid' => 111, 'grade' => '0.00000');
 202          $grades[] = (object)array('dimensionid' => 109, 'grade' => '1.00000');
 203  
 204          // exercise SUT
 205          $suggested = $this->strategy->calculate_peer_grade($grades);
 206          // validate
 207          $this->assertEquals($suggested, 100.00000);
 208      }
 209  
 210      public function test_calculate_peer_grade_sum_weight() {
 211          // fixture set-up
 212          $this->strategy->dimensions      = array();
 213          $this->strategy->dimensions[108] = (object)array('weight' => '1');
 214          $this->strategy->dimensions[109] = (object)array('weight' => '2');
 215          $this->strategy->dimensions[111] = (object)array('weight' => '3');
 216  
 217          $this->strategy->mappings        = array(
 218              1 => (object)array('grade' => '90.00000'),
 219              2 => (object)array('grade' => '80.00000'),
 220              3 => (object)array('grade' => '70.00000'),
 221              4 => (object)array('grade' => '60.00000'),
 222              5 => (object)array('grade' => '30.00000'),
 223              6 => (object)array('grade' => '5.00000'),
 224              7 => (object)array('grade' => '0.00000'),
 225          );
 226  
 227          $grades = array();
 228          $grades[] = (object)array('dimensionid' => 108, 'grade' => '0.00000');
 229          $grades[] = (object)array('dimensionid' => 111, 'grade' => '0.00000');
 230          $grades[] = (object)array('dimensionid' => 109, 'grade' => '0.00000');
 231  
 232          // exercise SUT
 233          $suggested = $this->strategy->calculate_peer_grade($grades);
 234          // validate
 235          $this->assertEquals($suggested, 5.00000);
 236      }
 237  }
 238  
 239  
 240  /**
 241   * Test subclass that makes all the protected methods we want to test public
 242   */
 243  class testable_workshop_numerrors_strategy extends workshop_numerrors_strategy {
 244  
 245      /** allows to set dimensions manually */
 246      public $dimensions = array();
 247  
 248      /** allow to set mappings manually */
 249      public $mappings = array();
 250  
 251      /**
 252       * This is where the calculation of suggested grade for submission is done
 253       */
 254      public function calculate_peer_grade(array $grades) {
 255          return parent::calculate_peer_grade($grades);
 256      }
 257  }