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]

   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   * This file contains tests for the question_state class and subclasses.
  19   *
  20   * @package    moodlecore
  21   * @subpackage questionengine
  22   * @copyright  2009 The Open University
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once (__DIR__ . '/../lib.php');
  31  require_once($CFG->libdir . '/questionlib.php');
  32  
  33  
  34  /**
  35   * Unit tests for the {@link question_state} class and subclasses.
  36   *
  37   * @copyright  2009 The Open University
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class question_state_test extends advanced_testcase {
  41      public function test_is_active() {
  42          $this->assertFalse(question_state::$notstarted->is_active());
  43          $this->assertFalse(question_state::$unprocessed->is_active());
  44          $this->assertTrue(question_state::$todo->is_active());
  45          $this->assertTrue(question_state::$invalid->is_active());
  46          $this->assertTrue(question_state::$complete->is_active());
  47          $this->assertFalse(question_state::$needsgrading->is_active());
  48          $this->assertFalse(question_state::$finished->is_active());
  49          $this->assertFalse(question_state::$gaveup->is_active());
  50          $this->assertFalse(question_state::$gradedwrong->is_active());
  51          $this->assertFalse(question_state::$gradedpartial->is_active());
  52          $this->assertFalse(question_state::$gradedright->is_active());
  53          $this->assertFalse(question_state::$manfinished->is_active());
  54          $this->assertFalse(question_state::$mangaveup->is_active());
  55          $this->assertFalse(question_state::$mangrwrong->is_active());
  56          $this->assertFalse(question_state::$mangrpartial->is_active());
  57          $this->assertFalse(question_state::$mangrright->is_active());
  58      }
  59  
  60      public function test_is_finished() {
  61          $this->assertFalse(question_state::$notstarted->is_finished());
  62          $this->assertFalse(question_state::$unprocessed->is_finished());
  63          $this->assertFalse(question_state::$todo->is_finished());
  64          $this->assertFalse(question_state::$invalid->is_finished());
  65          $this->assertFalse(question_state::$complete->is_finished());
  66          $this->assertTrue(question_state::$needsgrading->is_finished());
  67          $this->assertTrue(question_state::$finished->is_finished());
  68          $this->assertTrue(question_state::$gaveup->is_finished());
  69          $this->assertTrue(question_state::$gradedwrong->is_finished());
  70          $this->assertTrue(question_state::$gradedpartial->is_finished());
  71          $this->assertTrue(question_state::$gradedright->is_finished());
  72          $this->assertTrue(question_state::$manfinished->is_finished());
  73          $this->assertTrue(question_state::$mangaveup->is_finished());
  74          $this->assertTrue(question_state::$mangrwrong->is_finished());
  75          $this->assertTrue(question_state::$mangrpartial->is_finished());
  76          $this->assertTrue(question_state::$mangrright->is_finished());
  77      }
  78  
  79      public function test_is_graded() {
  80          $this->assertFalse(question_state::$notstarted->is_graded());
  81          $this->assertFalse(question_state::$unprocessed->is_graded());
  82          $this->assertFalse(question_state::$todo->is_graded());
  83          $this->assertFalse(question_state::$invalid->is_graded());
  84          $this->assertFalse(question_state::$complete->is_graded());
  85          $this->assertFalse(question_state::$needsgrading->is_graded());
  86          $this->assertFalse(question_state::$finished->is_graded());
  87          $this->assertFalse(question_state::$gaveup->is_graded());
  88          $this->assertTrue(question_state::$gradedwrong->is_graded());
  89          $this->assertTrue(question_state::$gradedpartial->is_graded());
  90          $this->assertTrue(question_state::$gradedright->is_graded());
  91          $this->assertFalse(question_state::$manfinished->is_graded());
  92          $this->assertFalse(question_state::$mangaveup->is_graded());
  93          $this->assertTrue(question_state::$mangrwrong->is_graded());
  94          $this->assertTrue(question_state::$mangrpartial->is_graded());
  95          $this->assertTrue(question_state::$mangrright->is_graded());
  96      }
  97  
  98      public function test_is_commented() {
  99          $this->assertFalse(question_state::$notstarted->is_commented());
 100          $this->assertFalse(question_state::$unprocessed->is_commented());
 101          $this->assertFalse(question_state::$todo->is_commented());
 102          $this->assertFalse(question_state::$invalid->is_commented());
 103          $this->assertFalse(question_state::$complete->is_commented());
 104          $this->assertFalse(question_state::$needsgrading->is_commented());
 105          $this->assertFalse(question_state::$finished->is_commented());
 106          $this->assertFalse(question_state::$gaveup->is_commented());
 107          $this->assertFalse(question_state::$gradedwrong->is_commented());
 108          $this->assertFalse(question_state::$gradedpartial->is_commented());
 109          $this->assertFalse(question_state::$gradedright->is_commented());
 110          $this->assertTrue(question_state::$manfinished->is_commented());
 111          $this->assertTrue(question_state::$mangaveup->is_commented());
 112          $this->assertTrue(question_state::$mangrwrong->is_commented());
 113          $this->assertTrue(question_state::$mangrpartial->is_commented());
 114          $this->assertTrue(question_state::$mangrright->is_commented());
 115      }
 116  
 117      public function test_graded_state_for_fraction() {
 118          $this->assertEquals(question_state::$gradedwrong, question_state::graded_state_for_fraction(-1));
 119          $this->assertEquals(question_state::$gradedwrong, question_state::graded_state_for_fraction(0));
 120          $this->assertEquals(question_state::$gradedpartial, question_state::graded_state_for_fraction(0.000001));
 121          $this->assertEquals(question_state::$gradedpartial, question_state::graded_state_for_fraction(0.999999));
 122          $this->assertEquals(question_state::$gradedright, question_state::graded_state_for_fraction(1));
 123      }
 124  
 125      public function test_manually_graded_state_for_other_state() {
 126          $this->assertEquals(question_state::$manfinished,
 127                  question_state::$finished->corresponding_commented_state(null));
 128          $this->assertEquals(question_state::$mangaveup,
 129                  question_state::$gaveup->corresponding_commented_state(null));
 130          $this->assertEquals(question_state::$manfinished,
 131                  question_state::$manfinished->corresponding_commented_state(null));
 132          $this->assertEquals(question_state::$mangaveup,
 133                  question_state::$mangaveup->corresponding_commented_state(null));
 134          $this->assertEquals(question_state::$needsgrading,
 135                  question_state::$mangrright->corresponding_commented_state(null));
 136          $this->assertEquals(question_state::$needsgrading,
 137                  question_state::$mangrright->corresponding_commented_state(null));
 138  
 139          $this->assertEquals(question_state::$mangrwrong,
 140                  question_state::$gaveup->corresponding_commented_state(0));
 141          $this->assertEquals(question_state::$mangrwrong,
 142                  question_state::$needsgrading->corresponding_commented_state(0));
 143          $this->assertEquals(question_state::$mangrwrong,
 144                  question_state::$gradedwrong->corresponding_commented_state(0));
 145          $this->assertEquals(question_state::$mangrwrong,
 146                  question_state::$gradedpartial->corresponding_commented_state(0));
 147          $this->assertEquals(question_state::$mangrwrong,
 148                  question_state::$gradedright->corresponding_commented_state(0));
 149          $this->assertEquals(question_state::$mangrwrong,
 150                  question_state::$mangrright->corresponding_commented_state(0));
 151          $this->assertEquals(question_state::$mangrwrong,
 152                  question_state::$mangrpartial->corresponding_commented_state(0));
 153          $this->assertEquals(question_state::$mangrwrong,
 154                  question_state::$mangrright->corresponding_commented_state(0));
 155  
 156          $this->assertEquals(question_state::$mangrpartial,
 157                  question_state::$gradedpartial->corresponding_commented_state(0.5));
 158  
 159          $this->assertEquals(question_state::$mangrright,
 160                  question_state::$gradedpartial->corresponding_commented_state(1));
 161      }
 162  }