Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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_ddimageortext;
  18  
  19  use question_attempt_step;
  20  use question_classified_response;
  21  use question_state;
  22  
  23  defined('MOODLE_INTERNAL') || die();
  24  
  25  global $CFG;
  26  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  27  require_once($CFG->dirroot . '/question/type/ddimageortext/tests/helper.php');
  28  
  29  
  30  /**
  31   * Unit tests for the matching question definition class.
  32   *
  33   * @package   qtype_ddimageortext
  34   * @copyright 2009 The Open University
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   * @covers qtype_ddimageortext_question
  37   */
  38  class question_test extends \basic_testcase {
  39  
  40      public function test_get_question_summary() {
  41          $dd = \test_question_maker::make_question('ddimageortext');
  42          $this->assertEquals('The quick brown fox jumped over the lazy dog.; '.
  43                  '[[Drop zone 1]] -> {1. quick / 2. fox}; '.
  44                  '[[Drop zone 2]] -> {1. quick / 2. fox}; '.
  45                  '[[Drop zone 3]] -> {3. lazy / 4. dog}; '.
  46                  '[[Drop zone 4]] -> {3. lazy / 4. dog}',
  47              $dd->get_question_summary());
  48      }
  49  
  50      public function test_get_question_summary_maths() {
  51          $dd = \test_question_maker::make_question('ddimageortext', 'maths');
  52          $this->assertEquals('Fill in the operators to make this equation work: '.
  53                  '7 [[1]] 11 [[2]] 13 [[1]] 17 [[2]] 19 = 3; '.
  54                  '[[Drop zone 1]] -> {1. + / 2. -}; '.
  55                  '[[Drop zone 2]] -> {1. + / 2. -}; '.
  56                  '[[Drop zone 3]] -> {1. + / 2. -}; '.
  57                  '[[Drop zone 4]] -> {1. + / 2. -}',
  58              $dd->get_question_summary());
  59      }
  60  
  61      public function test_summarise_response() {
  62          $dd = \test_question_maker::make_question('ddimageortext');
  63          $dd->shufflechoices = false;
  64          $dd->start_attempt(new question_attempt_step(), 1);
  65  
  66          $this->assertEquals('Drop zone 1 -> {1. quick} '.
  67                  'Drop zone 2 -> {1. quick} '.
  68                  'Drop zone 3 -> {3. lazy} '.
  69                  'Drop zone 4 -> {3. lazy}',
  70              $dd->summarise_response(array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
  71      }
  72  
  73      public function test_summarise_response_maths() {
  74          $dd = \test_question_maker::make_question('ddimageortext', 'maths');
  75          $dd->shufflechoices = false;
  76          $dd->start_attempt(new question_attempt_step(), 1);
  77  
  78          $this->assertEquals('Drop zone 1 -> {1. +} '.
  79                  'Drop zone 2 -> {2. -} '.
  80                  'Drop zone 3 -> {1. +} '.
  81                  'Drop zone 4 -> {2. -}',
  82              $dd->summarise_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
  83      }
  84  
  85      public function test_get_random_guess_score() {
  86          $dd = \test_question_maker::make_question('ddimageortext');
  87          $this->assertEquals(0.5, $dd->get_random_guess_score());
  88      }
  89  
  90      public function test_get_random_guess_score_maths() {
  91          $dd = \test_question_maker::make_question('ddimageortext', 'maths');
  92          $this->assertEquals(0.5, $dd->get_random_guess_score());
  93      }
  94  
  95      public function test_get_random_guess_score_broken_question() {
  96          // Before MDL-76298 was fixed, it was possible to create a question with
  97          // no drop zones, and that caused a fatal division by zero error. Because
  98          // people might have questions like that in their database, we need to
  99          // check the calculation is robust to it.
 100          /** @var \qtype_ddimageortext_question $dd */
 101          $dd = \test_question_maker::make_question('ddimageortext');
 102          $dd->places = [];
 103          $this->assertNull($dd->get_random_guess_score());
 104      }
 105  
 106      public function test_get_right_choice_for() {
 107          $dd = \test_question_maker::make_question('ddimageortext');
 108          $dd->shufflechoices = false;
 109          $dd->start_attempt(new question_attempt_step(), 1);
 110  
 111          $this->assertEquals(1, $dd->get_right_choice_for(1));
 112          $this->assertEquals(2, $dd->get_right_choice_for(2));
 113      }
 114  
 115      public function test_get_right_choice_for_maths() {
 116          $dd = \test_question_maker::make_question('ddimageortext', 'maths');
 117          $dd->shufflechoices = false;
 118          $dd->start_attempt(new question_attempt_step(), 1);
 119  
 120          $this->assertEquals(1, $dd->get_right_choice_for(1));
 121          $this->assertEquals(2, $dd->get_right_choice_for(2));
 122          $this->assertEquals(1, $dd->get_right_choice_for(3));
 123          $this->assertEquals(2, $dd->get_right_choice_for(4));
 124      }
 125  
 126      public function test_clear_wrong_from_response() {
 127          $dd = \test_question_maker::make_question('ddimageortext', 'maths');
 128          $dd->shufflechoices = false;
 129          $dd->start_attempt(new question_attempt_step(), 1);
 130  
 131          $initialresponse = array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1');
 132          $this->assertEquals(array('p1' => '1', 'p2' => '', 'p3' => '1', 'p4' => ''),
 133              $dd->clear_wrong_from_response($initialresponse));
 134      }
 135  
 136      public function test_get_num_parts_right() {
 137          $dd = \test_question_maker::make_question('ddimageortext');
 138          $dd->shufflechoices = false;
 139          $dd->start_attempt(new question_attempt_step(), 1);
 140  
 141          $this->assertEquals(array(2, 4),
 142              $dd->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '2', 'p4' => '2')));
 143          $this->assertEquals(array(4, 4),
 144              $dd->get_num_parts_right(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
 145      }
 146  
 147      public function test_get_num_parts_right_maths() {
 148          $dd = \test_question_maker::make_question('ddimageortext', 'maths');
 149          $dd->shufflechoices = false;
 150          $dd->start_attempt(new question_attempt_step(), 1);
 151  
 152          $this->assertEquals(array(2, 4),
 153              $dd->get_num_parts_right(array(
 154                  'p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
 155      }
 156  
 157      public function test_get_expected_data() {
 158          $dd = \test_question_maker::make_question('ddimageortext');
 159          $dd->start_attempt(new question_attempt_step(), 1);
 160  
 161          $this->assertEquals(
 162              array('p1' => PARAM_INT, 'p2' => PARAM_INT, 'p3' => PARAM_INT, 'p4' => PARAM_INT),
 163              $dd->get_expected_data()
 164          );
 165      }
 166  
 167      public function test_get_correct_response() {
 168          $dd = \test_question_maker::make_question('ddimageortext');
 169          $dd->shufflechoices = false;
 170          $dd->start_attempt(new question_attempt_step(), 1);
 171  
 172          $this->assertEquals(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2'),
 173              $dd->get_correct_response());
 174      }
 175  
 176      public function test_get_correct_response_maths() {
 177          $dd = \test_question_maker::make_question('ddimageortext', 'maths');
 178          $dd->shufflechoices = false;
 179          $dd->start_attempt(new question_attempt_step(), 1);
 180  
 181          $this->assertEquals(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2'),
 182              $dd->get_correct_response());
 183      }
 184  
 185      public function test_is_same_response() {
 186          $dd = \test_question_maker::make_question('ddimageortext');
 187          $dd->start_attempt(new question_attempt_step(), 1);
 188  
 189          $this->assertTrue($dd->is_same_response(
 190              array(),
 191              array('p1' => '', 'p2' => '', 'p3' => '', 'p4' => '')));
 192  
 193          $this->assertFalse($dd->is_same_response(
 194              array(),
 195              array('p1' => '1', 'p2' => '', 'p3' => '', 'p4' => '')));
 196  
 197          $this->assertFalse($dd->is_same_response(
 198              array('p1' => '', 'p2' => '', 'p3' => '', 'p4' => ''),
 199              array('p1' => '1', 'p2' => '', 'p3' => '', 'p4' => '')));
 200  
 201          $this->assertTrue($dd->is_same_response(
 202              array('p1' => '1', 'p2' => '2', 'p3' => '3', 'p4' => '4'),
 203              array('p1' => '1', 'p2' => '2', 'p3' => '3', 'p4' => '4')));
 204  
 205          $this->assertFalse($dd->is_same_response(
 206              array('p1' => '1', 'p2' => '2', 'p3' => '3', 'p4' => '4'),
 207              array('p1' => '1', 'p2' => '2', 'p3' => '2', 'p4' => '4')));
 208      }
 209      public function test_is_complete_response() {
 210          $dd = \test_question_maker::make_question('ddimageortext');
 211          $dd->start_attempt(new question_attempt_step(), 1);
 212  
 213          $this->assertFalse($dd->is_complete_response(array()));
 214          $this->assertFalse($dd->is_complete_response(
 215              array('p1' => '1', 'p2' => '1', 'p3' => '', 'p4' => '')));
 216          $this->assertFalse($dd->is_complete_response(array('p1' => '1')));
 217          $this->assertTrue($dd->is_complete_response(
 218              array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
 219      }
 220  
 221      public function test_is_gradable_response() {
 222          $dd = \test_question_maker::make_question('ddimageortext');
 223          $dd->start_attempt(new question_attempt_step(), 1);
 224  
 225          $this->assertFalse($dd->is_gradable_response(array()));
 226          $this->assertFalse($dd->is_gradable_response(
 227              array('p1' => '', 'p2' => '', 'p3' => '', 'p3' => '')));
 228          $this->assertTrue($dd->is_gradable_response(
 229              array('p1' => '1', 'p2' => '1', 'p3' => '')));
 230          $this->assertTrue($dd->is_gradable_response(array('p1' => '1')));
 231          $this->assertTrue($dd->is_gradable_response(
 232              array('p1' => '1', 'p2' => '1', 'p3' => '1')));
 233      }
 234  
 235      public function test_grading() {
 236          $dd = \test_question_maker::make_question('ddimageortext');
 237          $dd->shufflechoices = false;
 238          $dd->start_attempt(new question_attempt_step(), 1);
 239  
 240          $this->assertEquals(array(1, question_state::$gradedright),
 241              $dd->grade_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
 242          $this->assertEquals(array(0.25, question_state::$gradedpartial),
 243              $dd->grade_response(array('p1' => '1')));
 244          $this->assertEquals(array(0, question_state::$gradedwrong),
 245              $dd->grade_response(array('p1' => '2', 'p2' => '1', 'p3' => '2', 'p4' => '1')));
 246      }
 247  
 248      public function test_grading_maths() {
 249          $dd = \test_question_maker::make_question('ddimageortext', 'maths');
 250          $dd->shufflechoices = false;
 251          $dd->start_attempt(new question_attempt_step(), 1);
 252  
 253          $this->assertEquals(array(1, question_state::$gradedright),
 254              $dd->grade_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
 255          $this->assertEquals(array(0.5, question_state::$gradedpartial),
 256              $dd->grade_response(array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
 257          $this->assertEquals(array(0, question_state::$gradedwrong),
 258              $dd->grade_response(array('p1' => '', 'p2' => '1', 'p3' => '2', 'p4' => '1')));
 259      }
 260  
 261      public function test_classify_response() {
 262          $dd = \test_question_maker::make_question('ddimageortext');
 263          $dd->shufflechoices = false;
 264          $dd->start_attempt(new question_attempt_step(), 1);
 265  
 266          $this->assertEquals(array(
 267              1 => new question_classified_response(1, '1. quick', 1),
 268              2 => new question_classified_response(2, '2. fox', 1),
 269              3 => new question_classified_response(3, '3. lazy', 1),
 270              4 => new question_classified_response(4, '4. dog', 1)
 271          ), $dd->classify_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
 272          $this->assertEquals(array(
 273              1 => question_classified_response::no_response(),
 274              2 => new question_classified_response(1, '1. quick', 0),
 275              3 => new question_classified_response(4, '4. dog', 0),
 276              4 => new question_classified_response(4, '4. dog', 1)
 277          ), $dd->classify_response(array('p1' => '', 'p2' => '1', 'p3' => '2', 'p4' => '2')));
 278      }
 279  
 280      public function test_summarise_response_choice_deleted() {
 281          /** @var qtype_ddtoimage_question_base $dd */
 282          $dd = \test_question_maker::make_question('ddimageortext');
 283          $dd->shufflechoices = false;
 284          $dd->start_attempt(new question_attempt_step(), 1);
 285          // Simulation of an instructor deleting 1 choice after an attempt has been made.
 286          unset($dd->choices[1][1]);
 287          $delquestionstr = get_string('deletedchoice', 'qtype_ddimageortext');
 288          $this->assertEquals("Drop zone 1 -> {{$delquestionstr}} ".
 289              "Drop zone 2 -> {{$delquestionstr}} ".
 290              'Drop zone 3 -> {3. lazy} '.
 291              'Drop zone 4 -> {3. lazy}',
 292              $dd->summarise_response(array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
 293      }
 294  }