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