Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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 markers question definition class.
  19   *
  20   * @package   qtype_ddmarker
  21   * @copyright 2012 The Open University
  22   * @author    Jamie Pratt <me@jamiep.org>
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  global $CFG;
  29  
  30  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  31  require_once($CFG->dirroot . '/question/type/ddmarker/tests/helper.php');
  32  
  33  
  34  /**
  35   * Unit tests for the drag-and-drop markers question definition class.
  36   *
  37   * @copyright 2012 The Open University
  38   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class qtype_ddmarker_question_test extends basic_testcase {
  41  
  42      public function test_get_question_summary() {
  43          $dd = test_question_maker::make_question('ddmarker');
  44          $this->assertEquals('The quick brown fox jumped over the lazy dog.; '.
  45                              '[[Drop zone 1]] -> {quick / fox / lazy}; '.
  46                              '[[Drop zone 2]] -> {quick / fox / lazy}; '.
  47                              '[[Drop zone 3]] -> {quick / fox / lazy}',
  48                              $dd->get_question_summary());
  49      }
  50  
  51      public function test_get_question_summary_maths() {
  52          $dd = test_question_maker::make_question('ddmarker', 'maths');
  53          $this->assertEquals('Fill in the operators to make this equation work:; '.
  54                              '[[Drop zone 1]] -> {+ / - / * / /}; '.
  55                              '[[Drop zone 2]] -> {+ / - / * / /}; '.
  56                              '[[Drop zone 3]] -> {+ / - / * / /}',
  57                                      $dd->get_question_summary());
  58      }
  59  
  60      public function test_summarise_response() {
  61          $dd = test_question_maker::make_question('ddmarker');
  62          $dd->shufflechoices = false;
  63          $dd->start_attempt(new question_attempt_step(), 1);
  64  
  65          $this->assertEquals('{Drop zone 1 -> quick}, '.
  66                              '{Drop zone 2 -> fox}, '.
  67                              '{Drop zone 3 -> lazy}',
  68                  $dd->summarise_response(array('c1' => '50,50',
  69                                                  'c2' => '150,50',
  70                                                  'c3' => '50,150')));
  71      }
  72  
  73      public function test_summarise_response_maths() {
  74          $dd = test_question_maker::make_question('ddmarker', 'maths');
  75          $dd->shufflechoices = false;
  76          $dd->start_attempt(new question_attempt_step(), 1);
  77  
  78          $this->assertEquals('{Drop zone 1 -> +}, '.
  79                              '{Drop zone 2 -> +}, '.
  80                              '{Drop zone 3 -> +}',
  81                  $dd->summarise_response(array('c1' => '50,50;150,50;50,150',
  82                                                  'c2' => '',
  83                                                  'c3' => '')));
  84      }
  85  
  86      public function test_get_random_guess_score() {
  87          $dd = test_question_maker::make_question('ddmarker');
  88          $this->assertEquals(null, $dd->get_random_guess_score());
  89      }
  90  
  91      public function test_get_random_guess_score_maths() {
  92          $dd = test_question_maker::make_question('ddmarker', 'maths');
  93          $this->assertEquals(null, $dd->get_random_guess_score());
  94      }
  95  
  96      public function test_get_right_choice_for() {
  97          $dd = test_question_maker::make_question('ddmarker');
  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          $this->assertEquals(3, $dd->get_right_choice_for(3));
 104      }
 105  
 106      public function test_get_right_choice_for_maths() {
 107          $dd = test_question_maker::make_question('ddmarker', 'maths');
 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(1, $dd->get_right_choice_for(2));
 113          $this->assertEquals(1, $dd->get_right_choice_for(3));
 114      }
 115  
 116      public function test_clear_wrong_from_response() {
 117          $dd = test_question_maker::make_question('ddmarker', 'maths');
 118          $dd->shufflechoices = false;
 119          $dd->start_attempt(new question_attempt_step(), 1);
 120  
 121          $initialresponse = array('c1' => '50,50', 'c2' => '100,100', 'c3' => '100,100;200,200');
 122          $this->assertEquals(array('c1' => '50,50', 'c2' => '', 'c3' => ''),
 123                  $dd->clear_wrong_from_response($initialresponse));
 124      }
 125  
 126      public function test_get_num_parts_right() {
 127          $dd = test_question_maker::make_question('ddmarker');
 128          $dd->shufflechoices = false;
 129          $dd->start_attempt(new question_attempt_step(), 1);
 130  
 131          // The second returned param in array is the max of correct choices or
 132          // the actual number of items dragged.
 133          $response1 = array('c1' => '50,50', 'c2' => '110,110', 'c3' => '90,90;210,210');
 134          $this->assertEquals(array(1, 4), $dd->get_num_parts_right($response1));
 135          $response2 = array('c1' => '50,50;150,50;50,150',
 136                              'c2' => '110,110',
 137                              'c3' => '90,90;210,210');
 138          $this->assertEquals(array(1, 6), $dd->get_num_parts_right($response2));
 139          $response3 = array('c1' => '50,50;150,50;50,150',
 140                              'c2' => '',
 141                              'c3' => '');
 142          $this->assertEquals(array(1, 3), $dd->get_num_parts_right($response3));
 143      }
 144  
 145      public function test_get_num_parts_right_maths() {
 146          $dd = test_question_maker::make_question('ddmarker', 'maths');
 147          $dd->shufflechoices = false;
 148          $dd->start_attempt(new question_attempt_step(), 1);
 149  
 150          $this->assertEquals(array(3, 3),
 151                  $dd->get_num_parts_right(array(
 152                          'c1' => '50,50;150,50;50,150', 'c2' => '', 'c3' => '')));
 153      }
 154  
 155      public function test_get_expected_data() {
 156          $dd = test_question_maker::make_question('ddmarker');
 157          $dd->start_attempt(new question_attempt_step(), 1);
 158  
 159          $this->assertEquals(
 160              array('c1' => PARAM_NOTAGS, 'c2' => PARAM_NOTAGS, 'c3' => PARAM_NOTAGS),
 161              $dd->get_expected_data()
 162          );
 163      }
 164  
 165      public function test_get_correct_response() {
 166          $dd = test_question_maker::make_question('ddmarker');
 167          $dd->shufflechoices = false;
 168          $dd->start_attempt(new question_attempt_step(), 1);
 169  
 170          $this->assertEquals(array('c1' => '50,50', 'c2' => '150,50', 'c3' => '100,150'),
 171                              $dd->get_correct_response());
 172      }
 173  
 174      public function test_get_correct_response_maths() {
 175          $dd = test_question_maker::make_question('ddmarker', 'maths');
 176          $dd->shufflechoices = false;
 177          $dd->start_attempt(new question_attempt_step(), 1);
 178  
 179          $this->assertEquals(array('c1' => '50,50;150,50;50,150'), $dd->get_correct_response());
 180      }
 181  
 182      public function test_is_same_response() {
 183          $dd = test_question_maker::make_question('ddmarker');
 184          $dd->start_attempt(new question_attempt_step(), 1);
 185  
 186          $this->assertTrue($dd->is_same_response(
 187                  array(),
 188                  array('c1' => '', 'c2' => '', 'c3' => '', 'c4' => '')));
 189  
 190          $this->assertFalse($dd->is_same_response(
 191                  array(),
 192                  array('c1' => '100,100', 'c2' => '', 'c3' => '', 'c4' => '')));
 193  
 194          $this->assertFalse($dd->is_same_response(
 195                  array('c1' => '', 'c2' => '', 'c3' => '', 'c4' => ''),
 196                  array('c1' => '100,100', 'c2' => '', 'c3' => '', 'c4' => '')));
 197  
 198          $this->assertTrue($dd->is_same_response(
 199                  array('c1' => '100,100', 'c2' => '2', 'c3' => '3', 'c4' => '400,400'),
 200                  array('c1' => '100,100', 'c2' => '2', 'c3' => '3', 'c4' => '400,400')));
 201  
 202          $this->assertFalse($dd->is_same_response(
 203                  array('c1' => '100,100', 'c2' => '200,200', 'c3' => '300,300', 'c4' => '400,400'),
 204                  array('c1' => '100,100', 'c2' => '200,200', 'c3' => '200,200', 'c4' => '400,400')));
 205  
 206          $this->assertTrue($dd->is_same_response(
 207                  array('c1' => '100,100;200,200', 'c2' => '',
 208                          'c3' => '100,100;300,300', 'c4' => '400,400'),
 209                  array('c1' => '200,200;100,100', 'c2' => '',
 210                          'c3' => '300,300;100,100', 'c4' => '400,400')));
 211  
 212          $this->assertFalse($dd->is_same_response(
 213                  array('c1' => '100,100;200,200', 'c2' => '',
 214                          'c3' => '100,100;400,300', 'c4' => '400,400'),
 215                  array('c1' => '200,200;100,100', 'c2' => '',
 216                          'c3' => '300,300;100,100', 'c4' => '400,400')));
 217  
 218          $this->assertTrue($dd->is_same_response(
 219                  array('c1' => '100,100;100,100;200,200', 'c2' => '',
 220                          'c3' => '100,100;300,300', 'c4' => '400,400'),
 221                  array('c1' => '200,200;100,100;100,100', 'c2' => '',
 222                          'c3' => '300,300;100,100', 'c4' => '400,400')));
 223  
 224          $this->assertFalse($dd->is_same_response(
 225                  array('c1' => '100,100;100,100;200,200', 'c2' => '',
 226                          'c3' => '100,100;300,300', 'c4' => '400,400'),
 227                  array('c1' => '200,200;100,100', 'c2' => '',
 228                          'c3' => '300,300;100,100', 'c4' => '400,400')));
 229      }
 230      public function test_is_complete_response() {
 231          $dd = test_question_maker::make_question('ddmarker');
 232          $dd->start_attempt(new question_attempt_step(), 1);
 233  
 234          $this->assertFalse($dd->is_complete_response(array()));
 235          $this->assertFalse($dd->is_complete_response(
 236                  array('c1' => '', 'c2' => '', 'c3' => '')));
 237          $this->assertFalse($dd->is_complete_response(array('c1' => '')));
 238          $this->assertTrue($dd->is_complete_response(
 239                  array('c1' => '300,300', 'c2' => '300,300', 'c3' => '300,300')));
 240      }
 241  
 242      public function test_is_gradable_response() {
 243          $dd = test_question_maker::make_question('ddmarker');
 244          $dd->start_attempt(new question_attempt_step(), 1);
 245  
 246          $this->assertFalse($dd->is_gradable_response(array()));
 247          $this->assertFalse($dd->is_gradable_response(
 248                  array('c1' => '', 'c2' => '', 'c3' => '', 'c3' => '')));
 249          $this->assertTrue($dd->is_gradable_response(
 250                  array('c1' => '300,300', 'c2' => '300,300', 'c3' => '')));
 251          $this->assertTrue($dd->is_gradable_response(array('c1' => '300,300')));
 252          $this->assertTrue($dd->is_gradable_response(
 253                  array('c1' => '300,300', 'c2' => '300,300', 'c3' => '300,300')));
 254      }
 255  
 256      public function test_grading() {
 257          $dd = test_question_maker::make_question('ddmarker');
 258          $dd->shufflechoices = false;
 259          $dd->start_attempt(new question_attempt_step(), 1);
 260  
 261          $this->assertEquals(array(1, question_state::$gradedright),
 262                  $dd->grade_response(array('c1' => '50,50', 'c2' => '150,50', 'c3' => '100,150')));
 263          $this->assertEquals(array(2 / 3, question_state::$gradedpartial),
 264                  $dd->grade_response(array('c1' => '50,50', 'c2' => '50,50', 'c3' => '100,150')));
 265          $this->assertEquals(array(0, question_state::$gradedwrong),
 266                  $dd->grade_response(array('c1' => '150,50', 'c2' => '50,50', 'c3' => '100,50')));
 267      }
 268  
 269      public function test_grading_maths() {
 270          $dd = test_question_maker::make_question('ddmarker', 'maths');
 271          $dd->shufflechoices = false;
 272          $dd->start_attempt(new question_attempt_step(), 1);
 273  
 274          $this->assertEquals(array(1, question_state::$gradedright),
 275                  $dd->grade_response(array('c1' => '50,50;150,50;50,150', 'c2' => '', 'c3' => '')));
 276          $this->assertEquals(array(0.75, question_state::$gradedpartial),
 277                  $dd->grade_response(array('c1' => '50,50;150,50;50,150',
 278                                              'c2' => '', 'c3' => '50,150')));
 279          $this->assertEquals(array(0, question_state::$gradedwrong),
 280                  $dd->grade_response(array('c1' => '', 'c2' => '50,50;150,50', 'c3' => '100,50')));
 281          $this->assertEquals(array(0, question_state::$gradedwrong),
 282                              $dd->grade_response(array('c1' => '300,300',
 283                                                          'c2' => '50,50;150,50',
 284                                                          'c3' => '100,50')));
 285      }
 286  
 287      public function test_classify_response() {
 288          $dd = test_question_maker::make_question('ddmarker');
 289          $dd->shufflechoices = false;
 290          $dd->start_attempt(new question_attempt_step(), 1);
 291  
 292          $this->assertEquals(array(
 293                                      1 => new question_classified_response(1, 'quick', 1 / 3),
 294                                      2 => new question_classified_response(2, 'fox', 1 / 3),
 295                                      3 => new question_classified_response(3, 'lazy', 1 / 3)),
 296              $dd->classify_response(array('c1' => '50,50', 'c2' => '150,50', 'c3' => '100,150')));
 297  
 298          $this->assertEquals(array(
 299                                      1 => new question_classified_response(1, 'quick', 1 / 3),
 300                                      2 => question_classified_response::no_response(),
 301                                      3 => question_classified_response::no_response()),
 302              $dd->classify_response(array('c1' => '50,50', 'c2' => '100,150', 'c3' => '150,50')));
 303      }
 304  }