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 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [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_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   */
  35  class questionstate_test extends \advanced_testcase {
  36      public function test_is_active() {
  37          $this->assertFalse(question_state::$notstarted->is_active());
  38          $this->assertFalse(question_state::$unprocessed->is_active());
  39          $this->assertTrue(question_state::$todo->is_active());
  40          $this->assertTrue(question_state::$invalid->is_active());
  41          $this->assertTrue(question_state::$complete->is_active());
  42          $this->assertFalse(question_state::$needsgrading->is_active());
  43          $this->assertFalse(question_state::$finished->is_active());
  44          $this->assertFalse(question_state::$gaveup->is_active());
  45          $this->assertFalse(question_state::$gradedwrong->is_active());
  46          $this->assertFalse(question_state::$gradedpartial->is_active());
  47          $this->assertFalse(question_state::$gradedright->is_active());
  48          $this->assertFalse(question_state::$manfinished->is_active());
  49          $this->assertFalse(question_state::$mangaveup->is_active());
  50          $this->assertFalse(question_state::$mangrwrong->is_active());
  51          $this->assertFalse(question_state::$mangrpartial->is_active());
  52          $this->assertFalse(question_state::$mangrright->is_active());
  53      }
  54  
  55      public function test_is_finished() {
  56          $this->assertFalse(question_state::$notstarted->is_finished());
  57          $this->assertFalse(question_state::$unprocessed->is_finished());
  58          $this->assertFalse(question_state::$todo->is_finished());
  59          $this->assertFalse(question_state::$invalid->is_finished());
  60          $this->assertFalse(question_state::$complete->is_finished());
  61          $this->assertTrue(question_state::$needsgrading->is_finished());
  62          $this->assertTrue(question_state::$finished->is_finished());
  63          $this->assertTrue(question_state::$gaveup->is_finished());
  64          $this->assertTrue(question_state::$gradedwrong->is_finished());
  65          $this->assertTrue(question_state::$gradedpartial->is_finished());
  66          $this->assertTrue(question_state::$gradedright->is_finished());
  67          $this->assertTrue(question_state::$manfinished->is_finished());
  68          $this->assertTrue(question_state::$mangaveup->is_finished());
  69          $this->assertTrue(question_state::$mangrwrong->is_finished());
  70          $this->assertTrue(question_state::$mangrpartial->is_finished());
  71          $this->assertTrue(question_state::$mangrright->is_finished());
  72      }
  73  
  74      public function test_is_graded() {
  75          $this->assertFalse(question_state::$notstarted->is_graded());
  76          $this->assertFalse(question_state::$unprocessed->is_graded());
  77          $this->assertFalse(question_state::$todo->is_graded());
  78          $this->assertFalse(question_state::$invalid->is_graded());
  79          $this->assertFalse(question_state::$complete->is_graded());
  80          $this->assertFalse(question_state::$needsgrading->is_graded());
  81          $this->assertFalse(question_state::$finished->is_graded());
  82          $this->assertFalse(question_state::$gaveup->is_graded());
  83          $this->assertTrue(question_state::$gradedwrong->is_graded());
  84          $this->assertTrue(question_state::$gradedpartial->is_graded());
  85          $this->assertTrue(question_state::$gradedright->is_graded());
  86          $this->assertFalse(question_state::$manfinished->is_graded());
  87          $this->assertFalse(question_state::$mangaveup->is_graded());
  88          $this->assertTrue(question_state::$mangrwrong->is_graded());
  89          $this->assertTrue(question_state::$mangrpartial->is_graded());
  90          $this->assertTrue(question_state::$mangrright->is_graded());
  91      }
  92  
  93      public function test_is_commented() {
  94          $this->assertFalse(question_state::$notstarted->is_commented());
  95          $this->assertFalse(question_state::$unprocessed->is_commented());
  96          $this->assertFalse(question_state::$todo->is_commented());
  97          $this->assertFalse(question_state::$invalid->is_commented());
  98          $this->assertFalse(question_state::$complete->is_commented());
  99          $this->assertFalse(question_state::$needsgrading->is_commented());
 100          $this->assertFalse(question_state::$finished->is_commented());
 101          $this->assertFalse(question_state::$gaveup->is_commented());
 102          $this->assertFalse(question_state::$gradedwrong->is_commented());
 103          $this->assertFalse(question_state::$gradedpartial->is_commented());
 104          $this->assertFalse(question_state::$gradedright->is_commented());
 105          $this->assertTrue(question_state::$manfinished->is_commented());
 106          $this->assertTrue(question_state::$mangaveup->is_commented());
 107          $this->assertTrue(question_state::$mangrwrong->is_commented());
 108          $this->assertTrue(question_state::$mangrpartial->is_commented());
 109          $this->assertTrue(question_state::$mangrright->is_commented());
 110      }
 111  
 112      public function test_graded_state_for_fraction() {
 113          $this->assertEquals(question_state::$gradedwrong, question_state::graded_state_for_fraction(-1));
 114          $this->assertEquals(question_state::$gradedwrong, question_state::graded_state_for_fraction(0));
 115          $this->assertEquals(question_state::$gradedpartial, question_state::graded_state_for_fraction(0.000001));
 116          $this->assertEquals(question_state::$gradedpartial, question_state::graded_state_for_fraction(0.999999));
 117          $this->assertEquals(question_state::$gradedright, question_state::graded_state_for_fraction(1));
 118      }
 119  
 120      public function test_manually_graded_state_for_other_state() {
 121          $this->assertEquals(question_state::$manfinished,
 122                  question_state::$finished->corresponding_commented_state(null));
 123          $this->assertEquals(question_state::$mangaveup,
 124                  question_state::$gaveup->corresponding_commented_state(null));
 125          $this->assertEquals(question_state::$manfinished,
 126                  question_state::$manfinished->corresponding_commented_state(null));
 127          $this->assertEquals(question_state::$mangaveup,
 128                  question_state::$mangaveup->corresponding_commented_state(null));
 129          $this->assertEquals(question_state::$needsgrading,
 130                  question_state::$mangrright->corresponding_commented_state(null));
 131          $this->assertEquals(question_state::$needsgrading,
 132                  question_state::$mangrright->corresponding_commented_state(null));
 133  
 134          $this->assertEquals(question_state::$mangrwrong,
 135                  question_state::$gaveup->corresponding_commented_state(0));
 136          $this->assertEquals(question_state::$mangrwrong,
 137                  question_state::$needsgrading->corresponding_commented_state(0));
 138          $this->assertEquals(question_state::$mangrwrong,
 139                  question_state::$gradedwrong->corresponding_commented_state(0));
 140          $this->assertEquals(question_state::$mangrwrong,
 141                  question_state::$gradedpartial->corresponding_commented_state(0));
 142          $this->assertEquals(question_state::$mangrwrong,
 143                  question_state::$gradedright->corresponding_commented_state(0));
 144          $this->assertEquals(question_state::$mangrwrong,
 145                  question_state::$mangrright->corresponding_commented_state(0));
 146          $this->assertEquals(question_state::$mangrwrong,
 147                  question_state::$mangrpartial->corresponding_commented_state(0));
 148          $this->assertEquals(question_state::$mangrwrong,
 149                  question_state::$mangrright->corresponding_commented_state(0));
 150  
 151          $this->assertEquals(question_state::$mangrpartial,
 152                  question_state::$gradedpartial->corresponding_commented_state(0.5));
 153  
 154          $this->assertEquals(question_state::$mangrright,
 155                  question_state::$gradedpartial->corresponding_commented_state(1));
 156      }
 157  }