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 words into sentences question definition class.
  19   *
  20   * @package   qtype_ddwtos
  21   * @copyright 2012 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  global $CFG;
  28  
  29  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  30  require_once($CFG->dirroot . '/question/type/ddwtos/tests/helper.php');
  31  
  32  
  33  /**
  34   * Unit tests for the matching question definition class.
  35   *
  36   * @copyright 2012 The Open University
  37   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class qtype_ddwtos_question_test extends basic_testcase {
  40  
  41      public function test_get_question_summary() {
  42          $dd = test_question_maker::make_question('ddwtos');
  43          $this->assertEquals('The [[1]] brown [[2]] jumped over the [[3]] dog.; ' .
  44                  '[[1]] -> {quick / slow}; [[2]] -> {fox / dog}; [[3]] -> {lazy / assiduous}',
  45                  $dd->get_question_summary());
  46      }
  47  
  48      public function test_get_question_summary_maths() {
  49          $dd = test_question_maker::make_question('ddwtos', 'maths');
  50          $this->assertEquals('Fill in the operators to make this equation work: ' .
  51                  '7 [[1]] 11 [[2]] 13 [[1]] 17 [[2]] 19 = 3; [[1]] -> {+ / - / * / /}',
  52                  $dd->get_question_summary());
  53      }
  54  
  55      public function test_summarise_response() {
  56          $dd = test_question_maker::make_question('ddwtos');
  57          $dd->shufflechoices = false;
  58          $dd->start_attempt(new question_attempt_step(), 1);
  59  
  60          $this->assertEquals('{quick} {fox} {lazy}',
  61                  $dd->summarise_response(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
  62      }
  63  
  64      public function test_summarise_response_maths() {
  65          $dd = test_question_maker::make_question('ddwtos', 'maths');
  66          $dd->shufflechoices = false;
  67          $dd->start_attempt(new question_attempt_step(), 1);
  68  
  69          $this->assertEquals('{+} {-} {+} {-}',
  70                  $dd->summarise_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
  71      }
  72  
  73      public function test_get_random_guess_score() {
  74          $dd = test_question_maker::make_question('ddwtos');
  75          $this->assertEquals(0.5, $dd->get_random_guess_score());
  76      }
  77  
  78      public function test_get_random_guess_score_maths() {
  79          $dd = test_question_maker::make_question('ddwtos', 'maths');
  80          $this->assertEquals(0.25, $dd->get_random_guess_score());
  81      }
  82  
  83      public function test_get_right_choice_for() {
  84          $dd = test_question_maker::make_question('ddwtos');
  85          $dd->shufflechoices = false;
  86          $dd->start_attempt(new question_attempt_step(), 1);
  87  
  88          $this->assertEquals(1, $dd->get_right_choice_for(1));
  89          $this->assertEquals(1, $dd->get_right_choice_for(2));
  90      }
  91  
  92      public function test_get_right_choice_for_maths() {
  93          $dd = test_question_maker::make_question('ddwtos', 'maths');
  94          $dd->shufflechoices = false;
  95          $dd->start_attempt(new question_attempt_step(), 1);
  96  
  97          $this->assertEquals(1, $dd->get_right_choice_for(1));
  98          $this->assertEquals(2, $dd->get_right_choice_for(2));
  99      }
 100  
 101      public function test_clear_wrong_from_response() {
 102          $dd = test_question_maker::make_question('ddwtos', 'maths');
 103          $dd->shufflechoices = false;
 104          $dd->start_attempt(new question_attempt_step(), 1);
 105  
 106          $initialresponse = array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1');
 107          $this->assertEquals(array('p1' => '1', 'p2' => '0', 'p3' => '1', 'p4' => '0'),
 108                  $dd->clear_wrong_from_response($initialresponse));
 109      }
 110  
 111      public function test_get_num_parts_right() {
 112          $dd = test_question_maker::make_question('ddwtos');
 113          $dd->shufflechoices = false;
 114          $dd->start_attempt(new question_attempt_step(), 1);
 115  
 116          $this->assertEquals(array(2, 3),
 117                  $dd->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '2')));
 118          $this->assertEquals(array(3, 3),
 119                  $dd->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
 120      }
 121  
 122      public function test_get_num_parts_right_maths() {
 123          $dd = test_question_maker::make_question('ddwtos', 'maths');
 124          $dd->shufflechoices = false;
 125          $dd->start_attempt(new question_attempt_step(), 1);
 126  
 127          $this->assertEquals(array(2, 4),
 128                  $dd->get_num_parts_right(array(
 129                          'p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
 130      }
 131  
 132      public function test_get_expected_data() {
 133          $dd = test_question_maker::make_question('ddwtos');
 134          $dd->start_attempt(new question_attempt_step(), 1);
 135  
 136          $this->assertEquals(array('p1' => PARAM_INT, 'p2' => PARAM_INT, 'p3' => PARAM_INT),
 137                  $dd->get_expected_data());
 138      }
 139  
 140      public function test_get_correct_response() {
 141          $dd = test_question_maker::make_question('ddwtos');
 142          $dd->shufflechoices = false;
 143          $dd->start_attempt(new question_attempt_step(), 1);
 144  
 145          $this->assertEquals(array('p1' => '1', 'p2' => '1', 'p3' => '1'),
 146                  $dd->get_correct_response());
 147      }
 148  
 149      public function test_get_correct_response_maths() {
 150          $dd = test_question_maker::make_question('ddwtos', 'maths');
 151          $dd->shufflechoices = false;
 152          $dd->start_attempt(new question_attempt_step(), 1);
 153  
 154          $this->assertEquals(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2'),
 155                  $dd->get_correct_response());
 156      }
 157  
 158      public function test_is_same_response() {
 159          $dd = test_question_maker::make_question('ddwtos');
 160          $dd->start_attempt(new question_attempt_step(), 1);
 161  
 162          $this->assertTrue($dd->is_same_response(
 163                  array(),
 164                  array('p1' => '0', 'p2' => '0', 'p3' => '0')));
 165  
 166          $this->assertFalse($dd->is_same_response(
 167                  array(),
 168                  array('p1' => '1', 'p2' => '0', 'p3' => '0')));
 169  
 170          $this->assertFalse($dd->is_same_response(
 171                  array('p1' => '0', 'p2' => '0', 'p3' => '0'),
 172                  array('p1' => '1', 'p2' => '0', 'p3' => '0')));
 173  
 174          $this->assertTrue($dd->is_same_response(
 175                  array('p1' => '1', 'p2' => '2', 'p3' => '3'),
 176                  array('p1' => '1', 'p2' => '2', 'p3' => '3')));
 177  
 178          $this->assertFalse($dd->is_same_response(
 179                  array('p1' => '1', 'p2' => '2', 'p3' => '3'),
 180                  array('p1' => '1', 'p2' => '2', 'p3' => '2')));
 181      }
 182      public function test_is_complete_response() {
 183          $dd = test_question_maker::make_question('ddwtos');
 184          $dd->start_attempt(new question_attempt_step(), 1);
 185  
 186          $this->assertFalse($dd->is_complete_response(array()));
 187          $this->assertFalse($dd->is_complete_response(
 188                  array('p1' => '1', 'p2' => '1', 'p3' => '0')));
 189          $this->assertFalse($dd->is_complete_response(array('p1' => '1')));
 190          $this->assertTrue($dd->is_complete_response(
 191                  array('p1' => '1', 'p2' => '1', 'p3' => '1')));
 192      }
 193  
 194      public function test_is_gradable_response() {
 195          $dd = test_question_maker::make_question('ddwtos');
 196          $dd->start_attempt(new question_attempt_step(), 1);
 197  
 198          $this->assertFalse($dd->is_gradable_response(array()));
 199          $this->assertFalse($dd->is_gradable_response(
 200                  array('p1' => '0', 'p2' => '0', 'p3' => '0')));
 201          $this->assertTrue($dd->is_gradable_response(
 202                  array('p1' => '1', 'p2' => '1', 'p3' => '0')));
 203          $this->assertTrue($dd->is_gradable_response(array('p1' => '1')));
 204          $this->assertTrue($dd->is_gradable_response(
 205                  array('p1' => '1', 'p2' => '1', 'p3' => '1')));
 206      }
 207  
 208      public function test_grading() {
 209          $dd = test_question_maker::make_question('ddwtos');
 210          $dd->shufflechoices = false;
 211          $dd->start_attempt(new question_attempt_step(), 1);
 212  
 213          $this->assertEquals(array(1, question_state::$gradedright),
 214                  $dd->grade_response(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
 215          $this->assertEquals(array(1 / 3, question_state::$gradedpartial),
 216                  $dd->grade_response(array('p1' => '1')));
 217          $this->assertEquals(array(0, question_state::$gradedwrong),
 218                  $dd->grade_response(array('p1' => '2', 'p2' => '2', 'p3' => '2')));
 219      }
 220  
 221      public function test_grading_maths() {
 222          $dd = test_question_maker::make_question('ddwtos', 'maths');
 223          $dd->shufflechoices = false;
 224          $dd->start_attempt(new question_attempt_step(), 1);
 225  
 226          $this->assertEquals(array(1, question_state::$gradedright),
 227                  $dd->grade_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
 228          $this->assertEquals(array(0.5, question_state::$gradedpartial),
 229                  $dd->grade_response(array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
 230          $this->assertEquals(array(0, question_state::$gradedwrong),
 231                  $dd->grade_response(array('p1' => '0', 'p2' => '1', 'p3' => '2', 'p4' => '1')));
 232      }
 233  
 234      public function test_classify_response() {
 235          $dd = test_question_maker::make_question('ddwtos');
 236          $dd->shufflechoices = false;
 237          $dd->start_attempt(new question_attempt_step(), 1);
 238  
 239          $this->assertEquals(array(
 240                      1 => new question_classified_response(1, 'quick', 1 / 3),
 241                      2 => new question_classified_response(2, 'dog', 0),
 242                      3 => new question_classified_response(1, 'lazy', 1 / 3),
 243                  ), $dd->classify_response(array('p1' => '1', 'p2' => '2', 'p3' => '1')));
 244          $this->assertEquals(array(
 245                      1 => question_classified_response::no_response(),
 246                      2 => new question_classified_response(1, 'fox', 1 / 3),
 247                      3 => new question_classified_response(2, 'assiduous', 0),
 248                  ), $dd->classify_response(array('p1' => '0', 'p2' => '1', 'p3' => '2')));
 249      }
 250  }