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]

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