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