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