Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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 Rubric grading strategy logic
  19   *
  20   * @package    workshopform_rubric
  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_rubric;
  26  
  27  use workshop;
  28  use workshop_rubric_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/rubric/lib.php');
  36  
  37  /**
  38   * Unit tests for Rubric grading strategy 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' => 'rubric', '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_rubric_strategy($this->workshop);
  60  
  61          // prepare dimensions definition
  62          $dim = new \stdClass();
  63          $dim->id = 6;
  64          $dim->levels[10] = (object)array('id' => 10, 'grade' => 0);
  65          $dim->levels[13] = (object)array('id' => 13, 'grade' => 2);
  66          $dim->levels[14] = (object)array('id' => 14, 'grade' => 6);
  67          $dim->levels[16] = (object)array('id' => 16, 'grade' => 8);
  68          $this->strategy->dimensions[$dim->id] = $dim;
  69  
  70          $dim = new \stdClass();
  71          $dim->id = 8;
  72          $dim->levels[17] = (object)array('id' => 17, 'grade' => 0);
  73          $dim->levels[18] = (object)array('id' => 18, 'grade' => 1);
  74          $dim->levels[19] = (object)array('id' => 19, 'grade' => 2);
  75          $dim->levels[20] = (object)array('id' => 20, 'grade' => 3);
  76          $this->strategy->dimensions[$dim->id] = $dim;
  77  
  78          $dim = new \stdClass();
  79          $dim->id = 10;
  80          $dim->levels[27] = (object)array('id' => 27, 'grade' => 10);
  81          $dim->levels[28] = (object)array('id' => 28, 'grade' => 20);
  82          $dim->levels[29] = (object)array('id' => 29, 'grade' => 30);
  83          $dim->levels[30] = (object)array('id' => 30, 'grade' => 40);
  84          $this->strategy->dimensions[$dim->id] = $dim;
  85  
  86      }
  87  
  88      protected function tearDown(): void {
  89          $this->strategy = null;
  90          $this->workshop = null;
  91          parent::tearDown();
  92      }
  93  
  94      public function test_calculate_peer_grade_null_grade() {
  95          // fixture set-up
  96          $grades = array();
  97          // exercise SUT
  98          $suggested = $this->strategy->calculate_peer_grade($grades);
  99          // validate
 100          $this->assertNull($suggested);
 101      }
 102  
 103      public function test_calculate_peer_grade_worst_possible() {
 104          // fixture set-up
 105          $grades[6] = (object)array('dimensionid' => 6, 'grade' => 0);
 106          $grades[8] = (object)array('dimensionid' => 8, 'grade' => 0);
 107          $grades[10] = (object)array('dimensionid' => 10, 'grade' => 10);
 108          // exercise SUT
 109          $suggested = $this->strategy->calculate_peer_grade($grades);
 110          // validate
 111          $this->assertEquals(grade_floatval($suggested), 0.00000);
 112      }
 113  
 114      public function test_calculate_peer_grade_best_possible() {
 115          // fixture set-up
 116          $grades[6] = (object)array('dimensionid' => 6, 'grade' => 8);
 117          $grades[8] = (object)array('dimensionid' => 8, 'grade' => 3);
 118          $grades[10] = (object)array('dimensionid' => 10, 'grade' => 40);
 119          // exercise SUT
 120          $suggested = $this->strategy->calculate_peer_grade($grades);
 121          // validate
 122          $this->assertEquals(grade_floatval($suggested), 100.00000);
 123      }
 124  
 125      public function test_calculate_peer_grade_something() {
 126          // fixture set-up
 127          $grades[6] = (object)array('dimensionid' => 6, 'grade' => 2);
 128          $grades[8] = (object)array('dimensionid' => 8, 'grade' => 2);
 129          $grades[10] = (object)array('dimensionid' => 10, 'grade' => 30);
 130          // exercise SUT
 131          $suggested = $this->strategy->calculate_peer_grade($grades);
 132          // validate
 133          // minimal rubric score is 10, maximal is 51. We have 34 here
 134          $this->assertEquals(grade_floatval($suggested), grade_floatval(100 * 24 / 41));
 135      }
 136  }
 137  
 138  
 139  /**
 140   * Test subclass that makes all the protected methods we want to test public
 141   */
 142  class testable_workshop_rubric_strategy extends workshop_rubric_strategy {
 143  
 144      /** allows to set dimensions manually */
 145      public $dimensions = array();
 146  
 147      /**
 148       * This is where the calculation of suggested grade for submission is done
 149       */
 150      public function calculate_peer_grade(array $grades) {
 151          return parent::calculate_peer_grade($grades);
 152      }
 153  }