Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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  namespace core;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  require_once (__DIR__.'/fixtures/lib.php');
  22  
  23  /**
  24   * Unit tests for grade_scale
  25   *
  26   * @package    core
  27   * @category   test
  28   * @copyright  nicolas@moodle.com
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class grade_scale_test extends \grade_base_testcase {
  32  
  33      public function test_grade_scale() {
  34          $this->sub_test_scale_construct();
  35          $this->sub_test_grade_scale_insert();
  36          $this->sub_test_grade_scale_update();
  37          $this->sub_test_grade_scale_delete();
  38          $this->sub_test_grade_scale_fetch();
  39          $this->sub_test_scale_load_items();
  40          $this->sub_test_scale_compact_items();
  41          $this->sub_test_scale_one_item();
  42      }
  43  
  44      protected function sub_test_scale_construct() {
  45          $params = new \stdClass();
  46          $params->name        = 'unittestscale3';
  47          $params->courseid    = $this->course->id;
  48          $params->userid      = $this->userid;
  49          $params->scale       = 'Distinction, Very Good, Good, Pass, Fail';
  50          $params->description = 'This scale is used to mark standard assignments.';
  51          $params->timemodified = time();
  52  
  53          $scale = new \grade_scale($params, false);
  54  
  55          $this->assertEquals($params->name, $scale->name);
  56          $this->assertEquals($params->scale, $scale->scale);
  57          $this->assertEquals($params->description, $scale->description);
  58  
  59      }
  60  
  61      protected function sub_test_grade_scale_insert() {
  62          $grade_scale = new \grade_scale();
  63          $this->assertTrue(method_exists($grade_scale, 'insert'));
  64  
  65          $grade_scale->name        = 'unittestscale3';
  66          $grade_scale->courseid    = $this->courseid;
  67          $grade_scale->userid      = $this->userid;
  68          $grade_scale->scale       = 'Distinction, Very Good, Good, Pass, Fail';
  69          $grade_scale->description = 'This scale is used to mark standard assignments.';
  70  
  71          $grade_scale->insert();
  72  
  73          $last_grade_scale = end($this->scale);
  74  
  75          $this->assertEquals($grade_scale->id, $last_grade_scale->id + 1);
  76          $this->assertNotEmpty($grade_scale->timecreated);
  77          $this->assertNotEmpty($grade_scale->timemodified);
  78      }
  79  
  80      protected function sub_test_grade_scale_update() {
  81          global $DB;
  82          $grade_scale = new \grade_scale($this->scale[1], false);
  83          $this->assertTrue(method_exists($grade_scale, 'update'));
  84  
  85          $grade_scale->name = 'Updated info for this unittest grade_scale';
  86          $this->assertTrue($grade_scale->update());
  87          $name = $DB->get_field('scale', 'name', array('id' => $this->scale[1]->id));
  88          $this->assertEquals($grade_scale->name, $name);
  89      }
  90  
  91      protected function sub_test_grade_scale_delete() {
  92          global $DB;
  93          $grade_scale = new \grade_scale($this->scale[4], false); // Choose one we're not using elsewhere.
  94          $this->assertTrue(method_exists($grade_scale, 'delete'));
  95  
  96          $this->assertTrue($grade_scale->delete());
  97          $this->assertFalse($DB->get_record('scale', array('id' => $grade_scale->id)));
  98  
  99          // Keep the reference collection the same as what is in the database.
 100          unset($this->scale[4]);
 101      }
 102  
 103      protected function sub_test_grade_scale_fetch() {
 104          $grade_scale = new \grade_scale();
 105          $this->assertTrue(method_exists($grade_scale, 'fetch'));
 106  
 107          $grade_scale = \grade_scale::fetch(array('id'=>$this->scale[0]->id));
 108          $this->assertEquals($this->scale[0]->id, $grade_scale->id);
 109          $this->assertEquals($this->scale[0]->name, $grade_scale->name);
 110      }
 111  
 112      protected function sub_test_scale_load_items() {
 113          $scale = new \grade_scale($this->scale[0], false);
 114          $this->assertTrue(method_exists($scale, 'load_items'));
 115  
 116          $scale->load_items();
 117          $this->assertCount(7, $scale->scale_items);
 118          $this->assertEquals('Fairly neutral', $scale->scale_items[2]);
 119  
 120      }
 121  
 122      protected function sub_test_scale_compact_items() {
 123          $scale = new \grade_scale($this->scale[0], false);
 124          $this->assertTrue(method_exists($scale, 'compact_items'));
 125  
 126          $scale->load_items();
 127          $scale->scale = null;
 128          $scale->compact_items();
 129  
 130          // The original string and the new string may have differences in whitespace around the delimiter, and that's OK.
 131          $this->assertEquals(preg_replace('/\s*,\s*/', ',', $this->scale[0]->scale), $scale->scale);
 132      }
 133  
 134      protected function sub_test_scale_one_item() {
 135          $params = new \stdClass();
 136          $params->name         = 'unittestscale1i';
 137          $params->courseid     = $this->course->id;
 138          $params->userid       = $this->userid;
 139          $params->scale        = 'Like';
 140          $params->description  = 'This scale is used to like something.';
 141          $params->timemodified = time();
 142  
 143          $scale = new \grade_scale($params, false);
 144          $scale->load_items();
 145  
 146          $this->assertCount(1, $scale->scale_items);
 147          $this->assertSame(array('Like'), $scale->scale_items);
 148          $this->assertSame('Like', $scale->compact_items());
 149  
 150          $scale->insert();
 151  
 152          // Manual grade item with 1 item scale.
 153          $grade_item = new \stdClass();
 154          $grade_item->courseid = $this->course->id;
 155          $grade_item->categoryid = $this->grade_categories[0]->id;
 156          $grade_item->itemname = 'manual grade_item scale_1';
 157          $grade_item->itemtype = 'manual';
 158          $grade_item->itemnumber = 0;
 159          $grade_item->needsupdate = false;
 160          $grade_item->gradetype = GRADE_TYPE_SCALE;
 161          $grade_item->scaleid = $scale->id;
 162          $grade_item->iteminfo = 'Manual grade item used for unit testing';
 163          $grade_item->timecreated = time();
 164          $grade_item->timemodified = time();
 165  
 166          $grade_item = new \grade_item($grade_item);
 167          $grade_item->insert();
 168  
 169          $this->assertNotEmpty($grade_item->id);
 170          $this->assertEquals(1, $grade_item->grademin);
 171          $this->assertEquals(1, $grade_item->grademax);
 172  
 173          $status = $scale->is_used();
 174  
 175          $this->assertTrue($status);
 176      }
 177  }