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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [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   * Unit tests for the select missing words question definition class.
  19   *
  20   * @package   qtype_gapselect
  21   * @copyright 2012 The Open University
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  global $CFG;
  28  
  29  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  30  require_once($CFG->dirroot . '/question/type/gapselect/tests/helper.php');
  31  
  32  
  33  /**
  34   * Unit tests for the select missing words question definition class.
  35   *
  36   * @copyright 2012 The Open University
  37   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class qtype_gapselect_question_test extends basic_testcase {
  40  
  41      public function test_get_question_summary() {
  42          $gapselect = test_question_maker::make_question('gapselect');
  43          $this->assertEquals('The [[1]] brown [[2]] jumped over the [[3]] dog.; ' .
  44                  '[[1]] -> {quick / slow}; [[2]] -> {fox / dog}; [[3]] -> {lazy / assiduous}',
  45                  $gapselect->get_question_summary());
  46      }
  47  
  48      public function test_get_question_summary_maths() {
  49          $gapselect = test_question_maker::make_question('gapselect', 'maths');
  50          $this->assertEquals('Fill in the operators to make this equation work: ' .
  51                  '7 [[1]] 11 [[2]] 13 [[1]] 17 [[2]] 19 = 3; [[1]] -> {+ / - / * / /}',
  52                  $gapselect->get_question_summary());
  53      }
  54  
  55      public function test_summarise_response() {
  56          $gapselect = test_question_maker::make_question('gapselect');
  57          $gapselect->shufflechoices = false;
  58          $gapselect->start_attempt(new question_attempt_step(), 1);
  59  
  60          $this->assertEquals('{quick} {fox} {lazy}',
  61                  $gapselect->summarise_response(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
  62      }
  63  
  64      public function test_summarise_response_maths() {
  65          $gapselect = test_question_maker::make_question('gapselect', 'maths');
  66          $gapselect->shufflechoices = false;
  67          $gapselect->start_attempt(new question_attempt_step(), 1);
  68  
  69          $this->assertEquals('{+} {-} {+} {-}', $gapselect->summarise_response(
  70                  array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
  71      }
  72  
  73      public function test_get_random_guess_score() {
  74          $gapselect = test_question_maker::make_question('gapselect');
  75          $this->assertEquals(0.5, $gapselect->get_random_guess_score());
  76      }
  77  
  78      public function test_get_random_guess_score_maths() {
  79          $gapselect = test_question_maker::make_question('gapselect', 'maths');
  80          $this->assertEquals(0.25, $gapselect->get_random_guess_score());
  81      }
  82  
  83      public function test_get_right_choice_for() {
  84          $gapselect = test_question_maker::make_question('gapselect');
  85          $gapselect->shufflechoices = false;
  86          $gapselect->start_attempt(new question_attempt_step(), 1);
  87  
  88          $this->assertEquals(1, $gapselect->get_right_choice_for(1));
  89          $this->assertEquals(1, $gapselect->get_right_choice_for(2));
  90      }
  91  
  92      public function test_get_right_choice_for_maths() {
  93          $gapselect = test_question_maker::make_question('gapselect', 'maths');
  94          $gapselect->shufflechoices = false;
  95          $gapselect->start_attempt(new question_attempt_step(), 1);
  96  
  97          $this->assertEquals(1, $gapselect->get_right_choice_for(1));
  98          $this->assertEquals(2, $gapselect->get_right_choice_for(2));
  99      }
 100  
 101      public function test_clear_wrong_from_response() {
 102          $gapselect = test_question_maker::make_question('gapselect', 'maths');
 103          $gapselect->shufflechoices = false;
 104          $gapselect->start_attempt(new question_attempt_step(), 1);
 105  
 106          $initialresponse = array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1');
 107          $this->assertEquals(array('p1' => '1', 'p2' => '0', 'p3' => '1', 'p4' => '0'),
 108                  $gapselect->clear_wrong_from_response($initialresponse));
 109      }
 110  
 111      public function test_get_num_parts_right() {
 112          $gapselect = test_question_maker::make_question('gapselect');
 113          $gapselect->shufflechoices = false;
 114          $gapselect->start_attempt(new question_attempt_step(), 1);
 115  
 116          $this->assertEquals(array(2, 3),
 117                  $gapselect->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '2')));
 118          $this->assertEquals(array(3, 3),
 119                  $gapselect->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
 120      }
 121  
 122      public function test_get_num_parts_right_maths() {
 123          $gapselect = test_question_maker::make_question('gapselect', 'maths');
 124          $gapselect->shufflechoices = false;
 125          $gapselect->start_attempt(new question_attempt_step(), 1);
 126  
 127          $this->assertEquals(array(2, 4), $gapselect->get_num_parts_right(
 128                  array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
 129      }
 130  
 131      public function test_get_expected_data() {
 132          $gapselect = test_question_maker::make_question('gapselect');
 133          $gapselect->start_attempt(new question_attempt_step(), 1);
 134  
 135          $this->assertEquals(array('p1' => PARAM_INT, 'p2' => PARAM_INT, 'p3' => PARAM_INT),
 136                  $gapselect->get_expected_data());
 137      }
 138  
 139      public function test_get_correct_response() {
 140          $gapselect = test_question_maker::make_question('gapselect');
 141          $gapselect->shufflechoices = false;
 142          $gapselect->start_attempt(new question_attempt_step(), 1);
 143  
 144          $this->assertEquals(array('p1' => '1', 'p2' => '1', 'p3' => '1'),
 145                  $gapselect->get_correct_response());
 146      }
 147  
 148      public function test_get_correct_response_maths() {
 149          $gapselect = test_question_maker::make_question('gapselect', 'maths');
 150          $gapselect->shufflechoices = false;
 151          $gapselect->start_attempt(new question_attempt_step(), 1);
 152  
 153          $this->assertEquals(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2'),
 154                  $gapselect->get_correct_response());
 155      }
 156  
 157      public function test_is_same_response() {
 158          $gapselect = test_question_maker::make_question('gapselect');
 159          $gapselect->start_attempt(new question_attempt_step(), 1);
 160  
 161          $this->assertTrue($gapselect->is_same_response(
 162                  array(),
 163                  array('p1' => '0', 'p2' => '0', 'p3' => '0')));
 164  
 165          $this->assertFalse($gapselect->is_same_response(
 166                  array(),
 167                  array('p1' => '1', 'p2' => '0', 'p3' => '0')));
 168  
 169          $this->assertFalse($gapselect->is_same_response(
 170                  array('p1' => '0', 'p2' => '0', 'p3' => '0'),
 171                  array('p1' => '1', 'p2' => '0', 'p3' => '0')));
 172  
 173          $this->assertTrue($gapselect->is_same_response(
 174                  array('p1' => '1', 'p2' => '2', 'p3' => '3'),
 175                  array('p1' => '1', 'p2' => '2', 'p3' => '3')));
 176  
 177          $this->assertFalse($gapselect->is_same_response(
 178                  array('p1' => '1', 'p2' => '2', 'p3' => '3'),
 179                  array('p1' => '1', 'p2' => '2', 'p3' => '2')));
 180      }
 181      public function test_is_complete_response() {
 182          $gapselect = test_question_maker::make_question('gapselect');
 183          $gapselect->start_attempt(new question_attempt_step(), 1);
 184  
 185          $this->assertFalse($gapselect->is_complete_response(array()));
 186          $this->assertFalse($gapselect->is_complete_response(
 187                  array('p1' => '1', 'p2' => '1', 'p3' => '0')));
 188          $this->assertFalse($gapselect->is_complete_response(array('p1' => '1')));
 189          $this->assertTrue($gapselect->is_complete_response(
 190                  array('p1' => '1', 'p2' => '1', 'p3' => '1')));
 191      }
 192  
 193      public function test_is_gradable_response() {
 194          $gapselect = test_question_maker::make_question('gapselect');
 195          $gapselect->start_attempt(new question_attempt_step(), 1);
 196  
 197          $this->assertFalse($gapselect->is_gradable_response(array()));
 198          $this->assertFalse($gapselect->is_gradable_response(
 199                  array('p1' => '0', 'p2' => '0', 'p3' => '0')));
 200          $this->assertTrue($gapselect->is_gradable_response(
 201                  array('p1' => '1', 'p2' => '1', 'p3' => '0')));
 202          $this->assertTrue($gapselect->is_gradable_response(array('p1' => '1')));
 203          $this->assertTrue($gapselect->is_gradable_response(
 204                  array('p1' => '1', 'p2' => '1', 'p3' => '1')));
 205      }
 206  
 207      public function test_grading() {
 208          $gapselect = test_question_maker::make_question('gapselect');
 209          $gapselect->shufflechoices = false;
 210          $gapselect->start_attempt(new question_attempt_step(), 1);
 211  
 212          $this->assertEquals(array(1, question_state::$gradedright),
 213                  $gapselect->grade_response(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
 214          $this->assertEquals(array(1 / 3, question_state::$gradedpartial),
 215                  $gapselect->grade_response(array('p1' => '1')));
 216          $this->assertEquals(array(0, question_state::$gradedwrong),
 217                  $gapselect->grade_response(array('p1' => '2', 'p2' => '2', 'p3' => '2')));
 218      }
 219  
 220      public function test_grading_maths() {
 221          $gapselect = test_question_maker::make_question('gapselect', 'maths');
 222          $gapselect->shufflechoices = false;
 223          $gapselect->start_attempt(new question_attempt_step(), 1);
 224  
 225          $this->assertEquals(array(1, question_state::$gradedright), $gapselect->grade_response(
 226                  array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
 227          $this->assertEquals(array(0.5, question_state::$gradedpartial), $gapselect->grade_response(
 228                  array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
 229          $this->assertEquals(array(0, question_state::$gradedwrong), $gapselect->grade_response(
 230                  array('p1' => '0', 'p2' => '1', 'p3' => '2', 'p4' => '1')));
 231      }
 232  
 233      public function test_classify_response() {
 234          $gapselect = test_question_maker::make_question('gapselect');
 235          $gapselect->shufflechoices = false;
 236          $gapselect->start_attempt(new question_attempt_step(), 1);
 237  
 238          $this->assertEquals(array(
 239                      1 => new question_classified_response(1, 'quick', 1 / 3),
 240                      2 => new question_classified_response(2, 'dog', 0),
 241                      3 => new question_classified_response(1, 'lazy', 1 / 3),
 242                  ), $gapselect->classify_response(array('p1' => '1', 'p2' => '2', 'p3' => '1')));
 243          $this->assertEquals(array(
 244                      1 => question_classified_response::no_response(),
 245                      2 => new question_classified_response(1, 'fox', 1 / 3),
 246                      3 => new question_classified_response(2, 'assiduous', 0),
 247                  ), $gapselect->classify_response(array('p1' => '0', 'p2' => '1', 'p3' => '2')));
 248      }
 249  
 250      /**
 251       * test_get_question_definition_for_external_rendering
 252       */
 253      public function test_get_question_definition_for_external_rendering() {
 254          $question = test_question_maker::make_question('gapselect', 'maths');
 255          $question->start_attempt(new question_attempt_step(), 1);
 256          $qa = test_question_maker::get_a_qa($question);
 257          $displayoptions = new question_display_options();
 258  
 259          $options = $question->get_question_definition_for_external_rendering($qa, $displayoptions);
 260          $this->assertEquals(1, $options['shufflechoices']);
 261      }
 262  }