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 311 and 401] [Versions 311 and 402] [Versions 311 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_engine class.
  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  namespace core_question;
  27  
  28  use advanced_testcase;
  29  use moodle_exception;
  30  use question_engine;
  31  use testable_core_question_renderer;
  32  
  33  /**
  34   * Unit tests for the question_engine class.
  35   *
  36   * @copyright  2009 The Open University
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class question_engine_test extends advanced_testcase {
  40  
  41      /**
  42       * Load required libraries
  43       */
  44      public static function setUpBeforeClass(): void {
  45          global $CFG;
  46  
  47          require_once("{$CFG->dirroot}/question/engine/lib.php");
  48      }
  49  
  50      public function test_load_behaviour_class() {
  51          // Exercise SUT
  52          question_engine::load_behaviour_class('deferredfeedback');
  53          // Verify
  54          $this->assertTrue(class_exists('qbehaviour_deferredfeedback'));
  55      }
  56  
  57      public function test_load_behaviour_class_missing() {
  58          // Exercise SUT
  59          $this->expectException(moodle_exception::class);
  60          question_engine::load_behaviour_class('nonexistantbehaviour');
  61      }
  62  
  63      public function test_get_behaviour_unused_display_options() {
  64          $this->assertEquals(array(), question_engine::get_behaviour_unused_display_options('interactive'));
  65          $this->assertEquals(array('correctness', 'marks', 'specificfeedback', 'generalfeedback', 'rightanswer'),
  66                  question_engine::get_behaviour_unused_display_options('deferredfeedback'));
  67          $this->assertEquals(array('correctness', 'marks', 'specificfeedback', 'generalfeedback', 'rightanswer'),
  68                  question_engine::get_behaviour_unused_display_options('deferredcbm'));
  69          $this->assertEquals(array('correctness', 'marks', 'specificfeedback', 'generalfeedback', 'rightanswer'),
  70                  question_engine::get_behaviour_unused_display_options('manualgraded'));
  71      }
  72  
  73      public function test_can_questions_finish_during_the_attempt() {
  74          $this->assertFalse(question_engine::can_questions_finish_during_the_attempt('deferredfeedback'));
  75          $this->assertTrue(question_engine::can_questions_finish_during_the_attempt('interactive'));
  76      }
  77  
  78      public function test_sort_behaviours() {
  79          $in = array('b1' => 'Behave 1', 'b2' => 'Behave 2', 'b3' => 'Behave 3', 'b4' => 'Behave 4', 'b5' => 'Behave 5', 'b6' => 'Behave 6');
  80  
  81          $out = array('b1' => 'Behave 1', 'b2' => 'Behave 2', 'b3' => 'Behave 3', 'b4' => 'Behave 4', 'b5' => 'Behave 5', 'b6' => 'Behave 6');
  82          $this->assertSame($out, question_engine::sort_behaviours($in, '', '', ''));
  83  
  84          $this->assertSame($out, question_engine::sort_behaviours($in, '', 'b4', 'b4'));
  85  
  86          $out = array('b4' => 'Behave 4', 'b5' => 'Behave 5', 'b6' => 'Behave 6');
  87          $this->assertSame($out, question_engine::sort_behaviours($in, '', 'b1,b2,b3,b4', 'b4'));
  88  
  89          $out = array('b6' => 'Behave 6', 'b1' => 'Behave 1', 'b4' => 'Behave 4');
  90          $this->assertSame($out, question_engine::sort_behaviours($in, 'b6,b1,b4', 'b2,b3,b4,b5', 'b4'));
  91  
  92          $out = array('b6' => 'Behave 6', 'b5' => 'Behave 5', 'b4' => 'Behave 4');
  93          $this->assertSame($out, question_engine::sort_behaviours($in, 'b6,b5,b4', 'b1,b2,b3', 'b4'));
  94  
  95          $out = array('b6' => 'Behave 6', 'b5' => 'Behave 5', 'b4' => 'Behave 4');
  96          $this->assertSame($out, question_engine::sort_behaviours($in, 'b1,b6,b5', 'b1,b2,b3,b4', 'b4'));
  97  
  98          $out = array('b2' => 'Behave 2', 'b4' => 'Behave 4', 'b6' => 'Behave 6');
  99          $this->assertSame($out, question_engine::sort_behaviours($in, 'b2,b4,b6', 'b1,b3,b5', 'b2'));
 100  
 101          // Ignore unknown input in the order argument.
 102          $this->assertSame($in, question_engine::sort_behaviours($in, 'unknown', '', ''));
 103  
 104          // Ignore unknown input in the disabled argument.
 105          $this->assertSame($in, question_engine::sort_behaviours($in, '', 'unknown', ''));
 106      }
 107  
 108      public function test_is_manual_grade_in_range() {
 109          $_POST[] = array('q1:2_-mark' => 0.5, 'q1:2_-maxmark' => 1.0,
 110                  'q1:2_:minfraction' => 0, 'q1:2_:maxfraction' => 1);
 111          $this->assertTrue(question_engine::is_manual_grade_in_range(1, 2));
 112      }
 113  
 114      public function test_is_manual_grade_in_range_bottom_end() {
 115          $_POST[] = array('q1:2_-mark' => -1.0, 'q1:2_-maxmark' => 2.0,
 116                  'q1:2_:minfraction' => -0.5, 'q1:2_:maxfraction' => 1);
 117          $this->assertTrue(question_engine::is_manual_grade_in_range(1, 2));
 118      }
 119  
 120      public function test_is_manual_grade_in_range_too_low() {
 121          $_POST[] = array('q1:2_-mark' => -1.1, 'q1:2_-maxmark' => 2.0,
 122                  'q1:2_:minfraction' => -0.5, 'q1:2_:maxfraction' => 1);
 123          $this->assertTrue(question_engine::is_manual_grade_in_range(1, 2));
 124      }
 125  
 126      public function test_is_manual_grade_in_range_top_end() {
 127          $_POST[] = array('q1:2_-mark' => 3.0, 'q1:2_-maxmark' => 1.0,
 128                  'q1:2_:minfraction' => -6.0, 'q1:2_:maxfraction' => 3.0);
 129          $this->assertTrue(question_engine::is_manual_grade_in_range(1, 2));
 130      }
 131  
 132      public function test_is_manual_grade_in_range_too_high() {
 133          $_POST[] = array('q1:2_-mark' => 3.1, 'q1:2_-maxmark' => 1.0,
 134                  'q1:2_:minfraction' => -6.0, 'q1:2_:maxfraction' => 3.0);
 135          $this->assertTrue(question_engine::is_manual_grade_in_range(1, 2));
 136      }
 137  
 138      public function test_is_manual_grade_in_range_ungraded() {
 139          $this->assertTrue(question_engine::is_manual_grade_in_range(1, 2));
 140      }
 141  
 142      public function test_render_question_number() {
 143          global $CFG, $PAGE;
 144  
 145          require_once("{$CFG->dirroot}/question/engine/tests/helpers.php");
 146          $renderer = new testable_core_question_renderer($PAGE, 'core_question');
 147  
 148          // Test with number is i character.
 149          $this->assertEquals('<h3 class="no">Information</h3>', $renderer->number('i'));
 150          // Test with number is empty string.
 151          $this->assertEquals('', $renderer->number(''));
 152          // Test with number is 0.
 153          $this->assertEquals('<h3 class="no">Question <span class="qno">0</span></h3>', $renderer->number(0));
 154          // Test with number is numeric.
 155          $this->assertEquals('<h3 class="no">Question <span class="qno">1</span></h3>', $renderer->number(1));
 156          // Test with number is string.
 157          $this->assertEquals('<h3 class="no">Question <span class="qno">1 of 2</span></h3>', $renderer->number('1 of 2'));
 158      }
 159  }