Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 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   * 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  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once (__DIR__ . '/../lib.php');
  31  
  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      public function test_load_behaviour_class() {
  42          // Exercise SUT
  43          question_engine::load_behaviour_class('deferredfeedback');
  44          // Verify
  45          $this->assertTrue(class_exists('qbehaviour_deferredfeedback'));
  46      }
  47  
  48      /**
  49       * @expectedException moodle_exception
  50       */
  51      public function test_load_behaviour_class_missing() {
  52          // Exercise SUT
  53          question_engine::load_behaviour_class('nonexistantbehaviour');
  54      }
  55  
  56      public function test_get_behaviour_unused_display_options() {
  57          $this->assertEquals(array(), question_engine::get_behaviour_unused_display_options('interactive'));
  58          $this->assertEquals(array('correctness', 'marks', 'specificfeedback', 'generalfeedback', 'rightanswer'),
  59                  question_engine::get_behaviour_unused_display_options('deferredfeedback'));
  60          $this->assertEquals(array('correctness', 'marks', 'specificfeedback', 'generalfeedback', 'rightanswer'),
  61                  question_engine::get_behaviour_unused_display_options('deferredcbm'));
  62          $this->assertEquals(array('correctness', 'marks', 'specificfeedback', 'generalfeedback', 'rightanswer'),
  63                  question_engine::get_behaviour_unused_display_options('manualgraded'));
  64      }
  65  
  66      public function test_can_questions_finish_during_the_attempt() {
  67          $this->assertFalse(question_engine::can_questions_finish_during_the_attempt('deferredfeedback'));
  68          $this->assertTrue(question_engine::can_questions_finish_during_the_attempt('interactive'));
  69      }
  70  
  71      public function test_sort_behaviours() {
  72          $in = array('b1' => 'Behave 1', 'b2' => 'Behave 2', 'b3' => 'Behave 3', 'b4' => 'Behave 4', 'b5' => 'Behave 5', 'b6' => 'Behave 6');
  73  
  74          $out = array('b1' => 'Behave 1', 'b2' => 'Behave 2', 'b3' => 'Behave 3', 'b4' => 'Behave 4', 'b5' => 'Behave 5', 'b6' => 'Behave 6');
  75          $this->assertSame($out, question_engine::sort_behaviours($in, '', '', ''));
  76  
  77          $this->assertSame($out, question_engine::sort_behaviours($in, '', 'b4', 'b4'));
  78  
  79          $out = array('b4' => 'Behave 4', 'b5' => 'Behave 5', 'b6' => 'Behave 6');
  80          $this->assertSame($out, question_engine::sort_behaviours($in, '', 'b1,b2,b3,b4', 'b4'));
  81  
  82          $out = array('b6' => 'Behave 6', 'b1' => 'Behave 1', 'b4' => 'Behave 4');
  83          $this->assertSame($out, question_engine::sort_behaviours($in, 'b6,b1,b4', 'b2,b3,b4,b5', 'b4'));
  84  
  85          $out = array('b6' => 'Behave 6', 'b5' => 'Behave 5', 'b4' => 'Behave 4');
  86          $this->assertSame($out, question_engine::sort_behaviours($in, 'b6,b5,b4', 'b1,b2,b3', 'b4'));
  87  
  88          $out = array('b6' => 'Behave 6', 'b5' => 'Behave 5', 'b4' => 'Behave 4');
  89          $this->assertSame($out, question_engine::sort_behaviours($in, 'b1,b6,b5', 'b1,b2,b3,b4', 'b4'));
  90  
  91          $out = array('b2' => 'Behave 2', 'b4' => 'Behave 4', 'b6' => 'Behave 6');
  92          $this->assertSame($out, question_engine::sort_behaviours($in, 'b2,b4,b6', 'b1,b3,b5', 'b2'));
  93  
  94          // Ignore unknown input in the order argument.
  95          $this->assertSame($in, question_engine::sort_behaviours($in, 'unknown', '', ''));
  96  
  97          // Ignore unknown input in the disabled argument.
  98          $this->assertSame($in, question_engine::sort_behaviours($in, '', 'unknown', ''));
  99      }
 100  
 101      public function test_is_manual_grade_in_range() {
 102          $_POST[] = array('q1:2_-mark' => 0.5, 'q1:2_-maxmark' => 1.0,
 103                  'q1:2_:minfraction' => 0, 'q1:2_:maxfraction' => 1);
 104          $this->assertTrue(question_engine::is_manual_grade_in_range(1, 2));
 105      }
 106  
 107      public function test_is_manual_grade_in_range_bottom_end() {
 108          $_POST[] = array('q1:2_-mark' => -1.0, 'q1:2_-maxmark' => 2.0,
 109                  'q1:2_:minfraction' => -0.5, 'q1:2_:maxfraction' => 1);
 110          $this->assertTrue(question_engine::is_manual_grade_in_range(1, 2));
 111      }
 112  
 113      public function test_is_manual_grade_in_range_too_low() {
 114          $_POST[] = array('q1:2_-mark' => -1.1, 'q1:2_-maxmark' => 2.0,
 115                  'q1:2_:minfraction' => -0.5, 'q1:2_:maxfraction' => 1);
 116          $this->assertTrue(question_engine::is_manual_grade_in_range(1, 2));
 117      }
 118  
 119      public function test_is_manual_grade_in_range_top_end() {
 120          $_POST[] = array('q1:2_-mark' => 3.0, 'q1:2_-maxmark' => 1.0,
 121                  'q1:2_:minfraction' => -6.0, 'q1:2_:maxfraction' => 3.0);
 122          $this->assertTrue(question_engine::is_manual_grade_in_range(1, 2));
 123      }
 124  
 125      public function test_is_manual_grade_in_range_too_high() {
 126          $_POST[] = array('q1:2_-mark' => 3.1, 'q1:2_-maxmark' => 1.0,
 127                  'q1:2_:minfraction' => -6.0, 'q1:2_:maxfraction' => 3.0);
 128          $this->assertTrue(question_engine::is_manual_grade_in_range(1, 2));
 129      }
 130  
 131      public function test_is_manual_grade_in_range_ungraded() {
 132          $this->assertTrue(question_engine::is_manual_grade_in_range(1, 2));
 133      }
 134  
 135      public function test_render_question_number() {
 136          global $PAGE;
 137          $renderer = new testable_core_question_renderer($PAGE, 'core_question');
 138  
 139          // Test with number is i character.
 140          $this->assertEquals('<h3 class="no">Information</h3>', $renderer->number('i'));
 141          // Test with number is empty string.
 142          $this->assertEquals('', $renderer->number(''));
 143          // Test with number is 0.
 144          $this->assertEquals('<h3 class="no">Question <span class="qno">0</span></h3>', $renderer->number(0));
 145          // Test with number is numeric.
 146          $this->assertEquals('<h3 class="no">Question <span class="qno">1</span></h3>', $renderer->number(1));
 147          // Test with number is string.
 148          $this->assertEquals('<h3 class="no">Question <span class="qno">1 of 2</span></h3>', $renderer->number('1 of 2'));
 149      }
 150  }