Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402]

   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   * @covers \qtype_gapselect_question_base
  38   * @covers \qtype_gapselect_question
  39   */
  40  class question_test extends \basic_testcase {
  41  
  42      public function test_get_question_summary() {
  43          $gapselect = \test_question_maker::make_question('gapselect');
  44          $this->assertEquals('The [[1]] brown [[2]] jumped over the [[3]] dog.; ' .
  45                  '[[1]] -> {quick / slow}; [[2]] -> {fox / dog}; [[3]] -> {lazy / assiduous}',
  46                  $gapselect->get_question_summary());
  47      }
  48  
  49      public function test_get_question_summary_maths() {
  50          $gapselect = \test_question_maker::make_question('gapselect', 'maths');
  51          $this->assertEquals('Fill in the operators to make this equation work: ' .
  52                  '7 [[1]] 11 [[2]] 13 [[1]] 17 [[2]] 19 = 3; [[1]] -> {+ / - / * / /}',
  53                  $gapselect->get_question_summary());
  54      }
  55  
  56      public function test_summarise_response() {
  57          $gapselect = \test_question_maker::make_question('gapselect');
  58          $gapselect->shufflechoices = false;
  59          $gapselect->start_attempt(new question_attempt_step(), 1);
  60  
  61          $this->assertEquals('{quick} {fox} {lazy}',
  62                  $gapselect->summarise_response(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
  63      }
  64  
  65      public function test_summarise_response_maths() {
  66          $gapselect = \test_question_maker::make_question('gapselect', 'maths');
  67          $gapselect->shufflechoices = false;
  68          $gapselect->start_attempt(new question_attempt_step(), 1);
  69  
  70          $this->assertEquals('{+} {-} {+} {-}', $gapselect->summarise_response(
  71                  array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
  72      }
  73  
  74      public function test_get_random_guess_score() {
  75          $gapselect = \test_question_maker::make_question('gapselect');
  76          $this->assertEquals(0.5, $gapselect->get_random_guess_score());
  77      }
  78  
  79      public function test_get_random_guess_score_maths() {
  80          $gapselect = \test_question_maker::make_question('gapselect', 'maths');
  81          $this->assertEquals(0.25, $gapselect->get_random_guess_score());
  82      }
  83  
  84      public function test_get_right_choice_for() {
  85          $gapselect = \test_question_maker::make_question('gapselect');
  86          $gapselect->shufflechoices = false;
  87          $gapselect->start_attempt(new question_attempt_step(), 1);
  88  
  89          $this->assertEquals(1, $gapselect->get_right_choice_for(1));
  90          $this->assertEquals(1, $gapselect->get_right_choice_for(2));
  91      }
  92  
  93      public function test_get_right_choice_for_maths() {
  94          $gapselect = \test_question_maker::make_question('gapselect', 'maths');
  95          $gapselect->shufflechoices = false;
  96          $gapselect->start_attempt(new question_attempt_step(), 1);
  97  
  98          $this->assertEquals(1, $gapselect->get_right_choice_for(1));
  99          $this->assertEquals(2, $gapselect->get_right_choice_for(2));
 100      }
 101  
 102      public function test_clear_wrong_from_response() {
 103          $gapselect = \test_question_maker::make_question('gapselect', 'maths');
 104          $gapselect->shufflechoices = false;
 105          $gapselect->start_attempt(new question_attempt_step(), 1);
 106  
 107          $initialresponse = array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1');
 108          $this->assertEquals(array('p1' => '1', 'p2' => '0', 'p3' => '1', 'p4' => '0'),
 109                  $gapselect->clear_wrong_from_response($initialresponse));
 110      }
 111  
 112      public function test_get_num_parts_right() {
 113          $gapselect = \test_question_maker::make_question('gapselect');
 114          $gapselect->shufflechoices = false;
 115          $gapselect->start_attempt(new question_attempt_step(), 1);
 116  
 117          $this->assertEquals(array(2, 3),
 118                  $gapselect->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '2')));
 119          $this->assertEquals(array(3, 3),
 120                  $gapselect->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
 121      }
 122  
 123      public function test_get_num_parts_right_maths() {
 124          $gapselect = \test_question_maker::make_question('gapselect', 'maths');
 125          $gapselect->shufflechoices = false;
 126          $gapselect->start_attempt(new question_attempt_step(), 1);
 127  
 128          $this->assertEquals(array(2, 4), $gapselect->get_num_parts_right(
 129                  array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
 130      }
 131  
 132      public function test_get_expected_data() {
 133          $gapselect = \test_question_maker::make_question('gapselect');
 134          $gapselect->start_attempt(new question_attempt_step(), 1);
 135  
 136          $this->assertEquals(array('p1' => PARAM_INT, 'p2' => PARAM_INT, 'p3' => PARAM_INT),
 137                  $gapselect->get_expected_data());
 138      }
 139  
 140      public function test_get_correct_response() {
 141          $gapselect = \test_question_maker::make_question('gapselect');
 142          $gapselect->shufflechoices = false;
 143          $gapselect->start_attempt(new question_attempt_step(), 1);
 144  
 145          $this->assertEquals(array('p1' => '1', 'p2' => '1', 'p3' => '1'),
 146                  $gapselect->get_correct_response());
 147      }
 148  
 149      public function test_get_correct_response_maths() {
 150          $gapselect = \test_question_maker::make_question('gapselect', 'maths');
 151          $gapselect->shufflechoices = false;
 152          $gapselect->start_attempt(new question_attempt_step(), 1);
 153  
 154          $this->assertEquals(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2'),
 155                  $gapselect->get_correct_response());
 156      }
 157  
 158      public function test_is_same_response() {
 159          $gapselect = \test_question_maker::make_question('gapselect');
 160          $gapselect->start_attempt(new question_attempt_step(), 1);
 161  
 162          $this->assertTrue($gapselect->is_same_response(
 163                  array(),
 164                  array('p1' => '0', 'p2' => '0', 'p3' => '0')));
 165  
 166          $this->assertFalse($gapselect->is_same_response(
 167                  array(),
 168                  array('p1' => '1', 'p2' => '0', 'p3' => '0')));
 169  
 170          $this->assertFalse($gapselect->is_same_response(
 171                  array('p1' => '0', 'p2' => '0', 'p3' => '0'),
 172                  array('p1' => '1', 'p2' => '0', 'p3' => '0')));
 173  
 174          $this->assertTrue($gapselect->is_same_response(
 175                  array('p1' => '1', 'p2' => '2', 'p3' => '3'),
 176                  array('p1' => '1', 'p2' => '2', 'p3' => '3')));
 177  
 178          $this->assertFalse($gapselect->is_same_response(
 179                  array('p1' => '1', 'p2' => '2', 'p3' => '3'),
 180                  array('p1' => '1', 'p2' => '2', 'p3' => '2')));
 181      }
 182      public function test_is_complete_response() {
 183          $gapselect = \test_question_maker::make_question('gapselect');
 184          $gapselect->start_attempt(new question_attempt_step(), 1);
 185  
 186          $this->assertFalse($gapselect->is_complete_response(array()));
 187          $this->assertFalse($gapselect->is_complete_response(
 188                  array('p1' => '1', 'p2' => '1', 'p3' => '0')));
 189          $this->assertFalse($gapselect->is_complete_response(array('p1' => '1')));
 190          $this->assertTrue($gapselect->is_complete_response(
 191                  array('p1' => '1', 'p2' => '1', 'p3' => '1')));
 192      }
 193  
 194      public function test_is_gradable_response() {
 195          $gapselect = \test_question_maker::make_question('gapselect');
 196          $gapselect->start_attempt(new question_attempt_step(), 1);
 197  
 198          $this->assertFalse($gapselect->is_gradable_response(array()));
 199          $this->assertFalse($gapselect->is_gradable_response(
 200                  array('p1' => '0', 'p2' => '0', 'p3' => '0')));
 201          $this->assertTrue($gapselect->is_gradable_response(
 202                  array('p1' => '1', 'p2' => '1', 'p3' => '0')));
 203          $this->assertTrue($gapselect->is_gradable_response(array('p1' => '1')));
 204          $this->assertTrue($gapselect->is_gradable_response(
 205                  array('p1' => '1', 'p2' => '1', 'p3' => '1')));
 206      }
 207  
 208      public function test_grading() {
 209          $gapselect = \test_question_maker::make_question('gapselect');
 210          $gapselect->shufflechoices = false;
 211          $gapselect->start_attempt(new question_attempt_step(), 1);
 212  
 213          $this->assertEquals(array(1, question_state::$gradedright),
 214                  $gapselect->grade_response(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
 215          $this->assertEquals(array(1 / 3, question_state::$gradedpartial),
 216                  $gapselect->grade_response(array('p1' => '1')));
 217          $this->assertEquals(array(0, question_state::$gradedwrong),
 218                  $gapselect->grade_response(array('p1' => '2', 'p2' => '2', 'p3' => '2')));
 219      }
 220  
 221      public function test_grading_maths() {
 222          $gapselect = \test_question_maker::make_question('gapselect', 'maths');
 223          $gapselect->shufflechoices = false;
 224          $gapselect->start_attempt(new question_attempt_step(), 1);
 225  
 226          $this->assertEquals(array(1, question_state::$gradedright), $gapselect->grade_response(
 227                  array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
 228          $this->assertEquals(array(0.5, question_state::$gradedpartial), $gapselect->grade_response(
 229                  array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
 230          $this->assertEquals(array(0, question_state::$gradedwrong), $gapselect->grade_response(
 231                  array('p1' => '0', 'p2' => '1', 'p3' => '2', 'p4' => '1')));
 232      }
 233  
 234      public function test_classify_response() {
 235          $gapselect = \test_question_maker::make_question('gapselect');
 236          $gapselect->shufflechoices = false;
 237          $gapselect->start_attempt(new question_attempt_step(), 1);
 238  
 239          $this->assertEquals(array(
 240                      1 => new question_classified_response(1, 'quick', 1 / 3),
 241                      2 => new question_classified_response(2, 'dog', 0),
 242                      3 => new question_classified_response(1, 'lazy', 1 / 3),
 243                  ), $gapselect->classify_response(array('p1' => '1', 'p2' => '2', 'p3' => '1')));
 244          $this->assertEquals(array(
 245                      1 => question_classified_response::no_response(),
 246                      2 => new question_classified_response(1, 'fox', 1 / 3),
 247                      3 => new question_classified_response(2, 'assiduous', 0),
 248                  ), $gapselect->classify_response(array('p1' => '0', 'p2' => '1', 'p3' => '2')));
 249      }
 250  
 251      /**
 252       * test_get_question_definition_for_external_rendering
 253       */
 254      public function test_get_question_definition_for_external_rendering() {
 255          $question = \test_question_maker::make_question('gapselect', 'maths');
 256          $question->start_attempt(new question_attempt_step(), 1);
 257          $qa = \test_question_maker::get_a_qa($question);
 258          $displayoptions = new question_display_options();
 259  
 260          $options = $question->get_question_definition_for_external_rendering($qa, $displayoptions);
 261          $this->assertEquals(1, $options['shufflechoices']);
 262      }
 263  
 264      public function test_validate_can_regrade_with_other_version_ok() {
 265          $question = \test_question_maker::make_question('gapselect');
 266  
 267          $newquestion = clone($question);
 268  
 269          $this->assertNull($newquestion->validate_can_regrade_with_other_version($question));
 270      }
 271  
 272      public function test_validate_can_regrade_with_other_version_bad_groups() {
 273          $question = \test_question_maker::make_question('gapselect');
 274  
 275          $newquestion = clone($question);
 276          unset($newquestion->choices[3]);
 277  
 278          $this->assertEquals(get_string('regradeissuenumgroupsschanged', 'qtype_gapselect'),
 279                  $newquestion->validate_can_regrade_with_other_version($question));
 280      }
 281  
 282      public function test_validate_can_regrade_with_other_version_bad_choices() {
 283          $question = \test_question_maker::make_question('gapselect');
 284  
 285          $newquestion = clone($question);
 286          unset($newquestion->choices[2][2]);
 287  
 288          $this->assertEquals(get_string('regradeissuenumchoiceschanged', 'qtype_gapselect', 2),
 289                  $newquestion->validate_can_regrade_with_other_version($question));
 290      }
 291  }