Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

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