Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

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