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  // 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 the grade condition.
  19   *
  20   * @package availability_grade
  21   * @copyright 2014 The Open University
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use availability_grade\condition;
  28  
  29  /**
  30   * Unit tests for the grade condition.
  31   *
  32   * @package availability_grade
  33   * @copyright 2014 The Open University
  34   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class availability_grade_condition_testcase extends advanced_testcase {
  37      /**
  38       * Tests constructing and using grade condition.
  39       */
  40      public function test_usage() {
  41          global $USER, $CFG;
  42          require_once($CFG->dirroot . '/mod/assign/locallib.php');
  43  
  44          $this->resetAfterTest();
  45          $CFG->enableavailability = true;
  46  
  47          // Make a test course and user.
  48          $course = $this->getDataGenerator()->create_course();
  49          $user = $this->getDataGenerator()->create_user();
  50          $this->getDataGenerator()->enrol_user($user->id, $course->id);
  51  
  52          // Make assign module.
  53          $assignrow = $this->getDataGenerator()->create_module('assign', array(
  54                  'course' => $course->id, 'name' => 'Test!'));
  55          $assign = new assign(context_module::instance($assignrow->cmid), false, false);
  56          $modinfo = get_fast_modinfo($course);
  57          $cm = $modinfo->get_cm($assignrow->cmid);
  58  
  59          $info = new \core_availability\info_module($cm);
  60  
  61          // Get the actual grade item.
  62          $item = $assign->get_grade_item();
  63  
  64          // Construct tree with grade condition (any grade, specified item).
  65          $structure = (object)array('type' => 'grade', 'id' => (int)$item->id);
  66          $cond = new condition($structure);
  67  
  68          // Check if available (not available).
  69          $this->assertFalse($cond->is_available(false, $info, true, $user->id));
  70          $information = $cond->get_description(false, false, $info);
  71          $information = \core_availability\info::format_info($information, $course);
  72          $this->assertRegExp('~have a grade.*Test!~', $information);
  73          $this->assertTrue($cond->is_available(true, $info, true, $user->id));
  74  
  75          // Add grade and check available.
  76          self::set_grade($assignrow, $user->id, 37.2);
  77          $this->assertTrue($cond->is_available(false, $info, true, $user->id));
  78          $this->assertFalse($cond->is_available(true, $info, true, $user->id));
  79          $information = $cond->get_description(false, true, $info);
  80          $information = \core_availability\info::format_info($information, $course);
  81          $this->assertRegExp('~do not have a grade.*Test!~', $information);
  82  
  83          // Construct directly and test remaining conditions; first, min grade (fail).
  84          self::set_grade($assignrow, $user->id, 29.99999);
  85          $structure->min = 30.0;
  86          $cond = new condition($structure);
  87          $this->assertFalse($cond->is_available(false, $info, true, $user->id));
  88          $information = $cond->get_description(false, false, $info);
  89          $information = \core_availability\info::format_info($information, $course);
  90          $this->assertRegExp('~achieve a required score.*Test!~', $information);
  91          $this->assertTrue($cond->is_available(true, $info, true, $user->id));
  92  
  93          // Min grade (success).
  94          self::set_grade($assignrow, $user->id, 30);
  95          $this->assertTrue($cond->is_available(false, $info, true, $user->id));
  96          $this->assertFalse($cond->is_available(true, $info, true, $user->id));
  97          $information = $cond->get_description(false, true, $info);
  98          $information = \core_availability\info::format_info($information, $course);
  99          $this->assertRegExp('~do not get certain scores.*Test!~', $information);
 100  
 101          // Max grade (fail).
 102          unset($structure->min);
 103          $structure->max = 30.0;
 104          $cond = new condition($structure);
 105          $this->assertFalse($cond->is_available(false, $info, true, $user->id));
 106          $information = $cond->get_description(false, false, $info);
 107          $information = \core_availability\info::format_info($information, $course);
 108          $this->assertRegExp('~get an appropriate score.*Test!~', $information);
 109          $this->assertTrue($cond->is_available(true, $info, true, $user->id));
 110  
 111          // Max grade (success).
 112          self::set_grade($assignrow, $user->id, 29.99999);
 113          $this->assertTrue($cond->is_available(false, $info, true, $user->id));
 114          $this->assertFalse($cond->is_available(true, $info, true, $user->id));
 115          $information = $cond->get_description(false, true, $info);
 116          $information = \core_availability\info::format_info($information, $course);
 117          $this->assertRegExp('~do not get certain scores.*Test!~', $information);
 118  
 119          // Max and min (fail).
 120          $structure->min = 30.0;
 121          $structure->max = 34.12345;
 122          $cond = new condition($structure);
 123          $this->assertFalse($cond->is_available(false, $info, true, $user->id));
 124          $information = $cond->get_description(false, false, $info);
 125          $information = \core_availability\info::format_info($information, $course);
 126          $this->assertRegExp('~get a particular score.*Test!~', $information);
 127          $this->assertTrue($cond->is_available(true, $info, true, $user->id));
 128  
 129          // Still fail (other end).
 130          self::set_grade($assignrow, $user->id, 34.12345);
 131          $this->assertFalse($cond->is_available(false, $info, true, $user->id));
 132  
 133          // Success (top end).
 134          self::set_grade($assignrow, $user->id, 34.12344);
 135          $this->assertTrue($cond->is_available(false, $info, true, $user->id));
 136          $this->assertFalse($cond->is_available(true, $info, true, $user->id));
 137          $information = $cond->get_description(false, true, $info);
 138          $information = \core_availability\info::format_info($information, $course);
 139          $this->assertRegExp('~do not get certain scores.*Test!~', $information);
 140  
 141          // Success (bottom end).
 142          self::set_grade($assignrow, $user->id, 30.0);
 143          $this->assertTrue($cond->is_available(false, $info, true, $user->id));
 144          $this->assertFalse($cond->is_available(true, $info, true, $user->id));
 145          $information = $cond->get_description(false, true, $info);
 146          $information = \core_availability\info::format_info($information, $course);
 147          $this->assertRegExp('~do not get certain scores.*Test!~', $information);
 148      }
 149  
 150      /**
 151       * Tests the constructor including error conditions. Also tests the
 152       * string conversion feature (intended for debugging only).
 153       */
 154      public function test_constructor() {
 155          // No parameters.
 156          $structure = new stdClass();
 157          try {
 158              $cond = new condition($structure);
 159              $this->fail();
 160          } catch (coding_exception $e) {
 161              $this->assertStringContainsString('Missing or invalid ->id', $e->getMessage());
 162          }
 163  
 164          // Invalid id (not int).
 165          $structure->id = 'bourne';
 166          try {
 167              $cond = new condition($structure);
 168              $this->fail();
 169          } catch (coding_exception $e) {
 170              $this->assertStringContainsString('Missing or invalid ->id', $e->getMessage());
 171          }
 172  
 173          // Invalid min (not number).
 174          $structure->id = 42;
 175          $structure->min = 'ute';
 176          try {
 177              $cond = new condition($structure);
 178              $this->fail();
 179          } catch (coding_exception $e) {
 180              $this->assertStringContainsString('Missing or invalid ->min', $e->getMessage());
 181          }
 182  
 183          // Invalid max (not number).
 184          $structure->min = 3.89;
 185          $structure->max = '9000';
 186          try {
 187              $cond = new condition($structure);
 188              $this->fail();
 189          } catch (coding_exception $e) {
 190              $this->assertStringContainsString('Missing or invalid ->max', $e->getMessage());
 191          }
 192  
 193          // All valid.
 194          $structure->max = 4.0;
 195          $cond = new condition($structure);
 196          $this->assertEquals('{grade:#42 >= 3.89000, < 4.00000}', (string)$cond);
 197  
 198          // No max.
 199          unset($structure->max);
 200          $cond = new condition($structure);
 201          $this->assertEquals('{grade:#42 >= 3.89000}', (string)$cond);
 202  
 203          // No min.
 204          unset($structure->min);
 205          $structure->max = 32.768;
 206          $cond = new condition($structure);
 207          $this->assertEquals('{grade:#42 < 32.76800}', (string)$cond);
 208  
 209          // No nothing (only requires that grade exists).
 210          unset($structure->max);
 211          $cond = new condition($structure);
 212          $this->assertEquals('{grade:#42}', (string)$cond);
 213      }
 214  
 215      /**
 216       * Tests the save() function.
 217       */
 218      public function test_save() {
 219          $structure = (object)array('id' => 19);
 220          $cond = new condition($structure);
 221          $structure->type = 'grade';
 222          $this->assertEquals($structure, $cond->save());
 223  
 224          $structure = (object)array('id' => 19, 'min' => 4.12345, 'max' => 90);
 225          $cond = new condition($structure);
 226          $structure->type = 'grade';
 227          $this->assertEquals($structure, $cond->save());
 228      }
 229  
 230      /**
 231       * Updates the grade of a user in the given assign module instance.
 232       *
 233       * @param stdClass $assignrow Assignment row from database
 234       * @param int $userid User id
 235       * @param float $grade Grade
 236       */
 237      protected static function set_grade($assignrow, $userid, $grade) {
 238          $grades = array();
 239          $grades[$userid] = (object)array(
 240                  'rawgrade' => $grade, 'userid' => $userid);
 241          $assignrow->cmidnumber = null;
 242          assign_grade_item_update($assignrow, $grades);
 243      }
 244  
 245      /**
 246       * Tests the update_dependency_id() function.
 247       */
 248      public function test_update_dependency_id() {
 249          $cond = new condition((object)array('id' => 123));
 250          $this->assertFalse($cond->update_dependency_id('frogs', 123, 456));
 251          $this->assertFalse($cond->update_dependency_id('grade_items', 12, 34));
 252          $this->assertTrue($cond->update_dependency_id('grade_items', 123, 456));
 253          $after = $cond->save();
 254          $this->assertEquals(456, $after->id);
 255      }
 256  }