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