Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 39 and 400]

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