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_match;
  18  
  19  use question_attempt_step;
  20  use question_classified_response;
  21  use question_display_options;
  22  use question_state;
  23  
  24  defined('MOODLE_INTERNAL') || die();
  25  
  26  global $CFG;
  27  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  28  
  29  
  30  /**
  31   * Unit tests for the matching question definition class.
  32   *
  33   * @package   qtype_match
  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 \advanced_testcase {
  38  
  39      public function test_get_expected_data() {
  40          $question = \test_question_maker::make_question('match');
  41          $question->start_attempt(new question_attempt_step(), 1);
  42  
  43          $this->assertEquals(array('sub0' => PARAM_INT, 'sub1' => PARAM_INT,
  44                  'sub2' => PARAM_INT, 'sub3' => PARAM_INT), $question->get_expected_data());
  45      }
  46  
  47      public function test_is_complete_response() {
  48          $question = \test_question_maker::make_question('match');
  49          $question->start_attempt(new question_attempt_step(), 1);
  50  
  51          $this->assertFalse($question->is_complete_response(array()));
  52          $this->assertFalse($question->is_complete_response(
  53                  array('sub0' => '1', 'sub1' => '1', 'sub2' => '1', 'sub3' => '0')));
  54          $this->assertFalse($question->is_complete_response(array('sub1' => '1')));
  55          $this->assertTrue($question->is_complete_response(
  56                  array('sub0' => '1', 'sub1' => '1', 'sub2' => '1', 'sub3' => '1')));
  57      }
  58  
  59      public function test_is_gradable_response() {
  60          $question = \test_question_maker::make_question('match');
  61          $question->start_attempt(new question_attempt_step(), 1);
  62  
  63          $this->assertFalse($question->is_gradable_response(array()));
  64          $this->assertFalse($question->is_gradable_response(
  65                  array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0')));
  66          $this->assertTrue($question->is_gradable_response(
  67                  array('sub0' => '1', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0')));
  68          $this->assertTrue($question->is_gradable_response(array('sub1' => '1')));
  69          $this->assertTrue($question->is_gradable_response(
  70                  array('sub0' => '1', 'sub1' => '1', 'sub2' => '3', 'sub3' => '1')));
  71      }
  72  
  73      public function test_is_same_response() {
  74          $question = \test_question_maker::make_question('match');
  75          $question->start_attempt(new question_attempt_step(), 1);
  76  
  77          $this->assertTrue($question->is_same_response(
  78                  array(),
  79                  array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0')));
  80  
  81          $this->assertTrue($question->is_same_response(
  82                  array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'),
  83                  array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0')));
  84  
  85          $this->assertFalse($question->is_same_response(
  86                  array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'),
  87                  array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1')));
  88  
  89          $this->assertTrue($question->is_same_response(
  90                  array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1'),
  91                  array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1')));
  92  
  93          $this->assertFalse($question->is_same_response(
  94                  array('sub0' => '2', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1'),
  95                  array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1')));
  96      }
  97  
  98      public function test_grading() {
  99          $question = \test_question_maker::make_question('match');
 100          $question->start_attempt(new question_attempt_step(), 1);
 101  
 102          $correctresponse = $question->prepare_simulated_post_data(
 103                                                  array('Dog' => 'Mammal',
 104                                                        'Frog' => 'Amphibian',
 105                                                        'Toad' => 'Amphibian',
 106                                                        'Cat' => 'Mammal'));
 107          $this->assertEquals(array(1, question_state::$gradedright), $question->grade_response($correctresponse));
 108  
 109          $partialresponse = $question->prepare_simulated_post_data(array('Dog' => 'Mammal'));
 110          $this->assertEquals(array(0.25, question_state::$gradedpartial), $question->grade_response($partialresponse));
 111  
 112          $partiallycorrectresponse = $question->prepare_simulated_post_data(
 113                                                  array('Dog' => 'Mammal',
 114                                                        'Frog' => 'Insect',
 115                                                        'Toad' => 'Insect',
 116                                                        'Cat' => 'Amphibian'));
 117          $this->assertEquals(array(0.25, question_state::$gradedpartial), $question->grade_response($partiallycorrectresponse));
 118  
 119          $wrongresponse = $question->prepare_simulated_post_data(
 120                                                  array('Dog' => 'Amphibian',
 121                                                        'Frog' => 'Insect',
 122                                                        'Toad' => 'Insect',
 123                                                        'Cat' => 'Amphibian'));
 124          $this->assertEquals(array(0, question_state::$gradedwrong), $question->grade_response($wrongresponse));
 125      }
 126  
 127      public function test_get_correct_response() {
 128          $question = \test_question_maker::make_question('match');
 129          $question->start_attempt(new question_attempt_step(), 1);
 130  
 131          $correct = $question->prepare_simulated_post_data(array('Dog' => 'Mammal',
 132                                                                  'Frog' => 'Amphibian',
 133                                                                  'Toad' => 'Amphibian',
 134                                                                  'Cat' => 'Mammal'));
 135          $this->assertEquals($correct, $question->get_correct_response());
 136      }
 137  
 138      public function test_get_question_summary() {
 139          $match = \test_question_maker::make_question('match');
 140          $match->start_attempt(new question_attempt_step(), 1);
 141          $qsummary = $match->get_question_summary();
 142          $this->assertMatchesRegularExpression('/' . preg_quote($match->questiontext, '/') . '/', $qsummary);
 143          foreach ($match->stems as $stem) {
 144              $this->assertMatchesRegularExpression('/' . preg_quote($stem, '/') . '/', $qsummary);
 145          }
 146          foreach ($match->choices as $choice) {
 147              $this->assertMatchesRegularExpression('/' . preg_quote($choice, '/') . '/', $qsummary);
 148          }
 149      }
 150  
 151      public function test_summarise_response() {
 152          $match = \test_question_maker::make_question('match');
 153          $match->start_attempt(new question_attempt_step(), 1);
 154  
 155          $summary = $match->summarise_response($match->prepare_simulated_post_data(array('Dog' => 'Amphibian', 'Frog' => 'Mammal')));
 156  
 157          $this->assertMatchesRegularExpression('/Dog -> Amphibian/', $summary);
 158          $this->assertMatchesRegularExpression('/Frog -> Mammal/', $summary);
 159      }
 160  
 161      public function test_classify_response() {
 162          $match = \test_question_maker::make_question('match');
 163          $match->start_attempt(new question_attempt_step(), 1);
 164  
 165          $response = $match->prepare_simulated_post_data(array('Dog' => 'Amphibian', 'Frog' => 'Insect', 'Toad' => '', 'Cat' => ''));
 166          $this->assertEquals(array(
 167                      1 => new question_classified_response(2, 'Amphibian', 0),
 168                      2 => new question_classified_response(3, 'Insect', 0),
 169                      3 => question_classified_response::no_response(),
 170                      4 => question_classified_response::no_response(),
 171                  ), $match->classify_response($response));
 172  
 173          $response = $match->prepare_simulated_post_data(array('Dog' => 'Mammal', 'Frog' => 'Amphibian',
 174                                                                'Toad' => 'Amphibian', 'Cat' => 'Mammal'));
 175          $this->assertEquals(array(
 176                      1 => new question_classified_response(1, 'Mammal', 0.25),
 177                      2 => new question_classified_response(2, 'Amphibian', 0.25),
 178                      3 => new question_classified_response(2, 'Amphibian', 0.25),
 179                      4 => new question_classified_response(1, 'Mammal', 0.25),
 180                  ), $match->classify_response($response));
 181      }
 182  
 183      public function test_classify_response_choice_deleted_after_attempt() {
 184          $match = \test_question_maker::make_question('match');
 185          $firststep = new question_attempt_step();
 186  
 187          $match->start_attempt($firststep, 1);
 188          $response = $match->prepare_simulated_post_data(array(
 189                  'Dog' => 'Amphibian', 'Frog' => 'Insect', 'Toad' => '', 'Cat' => 'Mammal'));
 190  
 191          $match = \test_question_maker::make_question('match');
 192          unset($match->stems[4]);
 193          unset($match->stemsformat[4]);
 194          unset($match->right[4]);
 195          $match->apply_attempt_state($firststep);
 196  
 197          $this->assertEquals(array(
 198                  1 => new question_classified_response(2, 'Amphibian', 0),
 199                  2 => new question_classified_response(3, 'Insect', 0),
 200                  3 => question_classified_response::no_response(),
 201          ), $match->classify_response($response));
 202      }
 203  
 204      public function test_classify_response_choice_added_after_attempt() {
 205          $match = \test_question_maker::make_question('match');
 206          $firststep = new question_attempt_step();
 207  
 208          $match->start_attempt($firststep, 1);
 209          $response = $match->prepare_simulated_post_data(array(
 210                  'Dog' => 'Amphibian', 'Frog' => 'Insect', 'Toad' => '', 'Cat' => 'Mammal'));
 211  
 212          $match = \test_question_maker::make_question('match');
 213          $match->stems[5] = "Snake";
 214          $match->stemsformat[5] = FORMAT_HTML;
 215          $match->choices[5] = "Reptile";
 216          $match->right[5] = 5;
 217          $match->apply_attempt_state($firststep);
 218  
 219          $this->assertEquals(array(
 220                  1 => new question_classified_response(2, 'Amphibian', 0),
 221                  2 => new question_classified_response(3, 'Insect', 0),
 222                  3 => question_classified_response::no_response(),
 223                  4 => new question_classified_response(1, 'Mammal', 0.20),
 224          ), $match->classify_response($response));
 225      }
 226  
 227      public function test_prepare_simulated_post_data() {
 228          $m = \test_question_maker::make_question('match');
 229          $m->start_attempt(new question_attempt_step(), 1);
 230          $postdata = $m->prepare_simulated_post_data(array('Dog' => 'Mammal', 'Frog' => 'Amphibian',
 231                                                            'Toad' => 'Amphibian', 'Cat' => 'Mammal'));
 232          $this->assertEquals(array(4, 4), $m->get_num_parts_right($postdata));
 233      }
 234  
 235      /**
 236       * test_get_question_definition_for_external_rendering
 237       */
 238      public function test_get_question_definition_for_external_rendering() {
 239          $question = \test_question_maker::make_question('match');
 240          $question->start_attempt(new question_attempt_step(), 1);
 241          $qa = \test_question_maker::get_a_qa($question);
 242          $displayoptions = new question_display_options();
 243  
 244          $options = $question->get_question_definition_for_external_rendering($qa, $displayoptions);
 245          $this->assertEquals(1, $options['shufflestems']);
 246      }
 247  }