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 radom shortanswer matching question definition classes.
  19   *
  20   * @package   qtype_randomsamatch
  21   * @copyright 2013 Jean-Michel Vedrine
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  30  
  31  
  32  /**
  33   * Unit tests for the random shortanswer matching question definition class.
  34   *
  35   * @copyright 2013 Jean-Michel Vedrine
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class qtype_randomsamatch_question_test extends advanced_testcase {
  39  
  40      public function test_get_expected_data() {
  41          $question = test_question_maker::make_question('randomsamatch');
  42          $question->start_attempt(new question_attempt_step(), 1);
  43  
  44          $this->assertEquals(array('sub0' => PARAM_INT, 'sub1' => PARAM_INT,
  45                  'sub2' => PARAM_INT, 'sub3' => PARAM_INT), $question->get_expected_data());
  46      }
  47  
  48      public function test_is_complete_response() {
  49          $question = test_question_maker::make_question('randomsamatch');
  50          $question->start_attempt(new question_attempt_step(), 1);
  51  
  52          $this->assertFalse($question->is_complete_response(array()));
  53          $this->assertFalse($question->is_complete_response(
  54                  array('sub0' => '1', 'sub1' => '1', 'sub2' => '1', 'sub3' => '0')));
  55          $this->assertFalse($question->is_complete_response(array('sub1' => '1')));
  56          $this->assertTrue($question->is_complete_response(
  57                  array('sub0' => '1', 'sub1' => '1', 'sub2' => '1', 'sub3' => '1')));
  58      }
  59  
  60      public function test_is_gradable_response() {
  61          $question = test_question_maker::make_question('randomsamatch');
  62          $question->start_attempt(new question_attempt_step(), 1);
  63  
  64          $this->assertFalse($question->is_gradable_response(array()));
  65          $this->assertFalse($question->is_gradable_response(
  66                  array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0')));
  67          $this->assertTrue($question->is_gradable_response(
  68                  array('sub0' => '1', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0')));
  69          $this->assertTrue($question->is_gradable_response(array('sub1' => '1')));
  70          $this->assertTrue($question->is_gradable_response(
  71                  array('sub0' => '1', 'sub1' => '1', 'sub2' => '3', 'sub3' => '1')));
  72      }
  73  
  74      public function test_is_same_response() {
  75          $question = test_question_maker::make_question('randomsamatch');
  76          $question->start_attempt(new question_attempt_step(), 1);
  77  
  78          $this->assertTrue($question->is_same_response(
  79                  array(),
  80                  array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0')));
  81  
  82          $this->assertTrue($question->is_same_response(
  83                  array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'),
  84                  array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0')));
  85  
  86          $this->assertFalse($question->is_same_response(
  87                  array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'),
  88                  array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1')));
  89  
  90          $this->assertTrue($question->is_same_response(
  91                  array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1'),
  92                  array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1')));
  93  
  94          $this->assertFalse($question->is_same_response(
  95                  array('sub0' => '2', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1'),
  96                  array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1')));
  97      }
  98  
  99      public function test_grading() {
 100          $question = test_question_maker::make_question('randomsamatch');
 101          $question->start_attempt(new question_attempt_step(), 1);
 102  
 103          $choiceorder = $question->get_choice_order();
 104          $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
 105          $this->assertEquals(array(1, question_state::$gradedright),
 106                  $question->grade_response(array('sub0' => $orderforchoice[13],
 107                          'sub1' => $orderforchoice[16], 'sub2' => $orderforchoice[16],
 108                          'sub3' => $orderforchoice[13])));
 109          $this->assertEquals(array(0.25, question_state::$gradedpartial),
 110                  $question->grade_response(array('sub0' => $orderforchoice[13])));
 111          $this->assertEquals(array(0, question_state::$gradedwrong),
 112                  $question->grade_response(array('sub0' => $orderforchoice[16],
 113                          'sub1' => $orderforchoice[13], 'sub2' => $orderforchoice[13],
 114                          'sub3' => $orderforchoice[16])));
 115      }
 116  
 117      public function test_get_correct_response() {
 118          $question = test_question_maker::make_question('randomsamatch');
 119          $question->start_attempt(new question_attempt_step(), 1);
 120  
 121          $choiceorder = $question->get_choice_order();
 122          $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
 123  
 124          $this->assertEquals(array('sub0' => $orderforchoice[13], 'sub1' => $orderforchoice[16],
 125                  'sub2' => $orderforchoice[16], 'sub3' => $orderforchoice[13]),
 126                  $question->get_correct_response());
 127      }
 128  
 129      public function test_get_question_summary() {
 130          $question = test_question_maker::make_question('randomsamatch');
 131          $question->start_attempt(new question_attempt_step(), 1);
 132          $qsummary = $question->get_question_summary();
 133          $this->assertRegExp('/' . preg_quote($question->questiontext, '/') . '/', $qsummary);
 134          foreach ($question->stems as $stem) {
 135              $this->assertRegExp('/' . preg_quote($stem, '/') . '/', $qsummary);
 136          }
 137          foreach ($question->choices as $choice) {
 138              $this->assertRegExp('/' . preg_quote($choice, '/') . '/', $qsummary);
 139          }
 140      }
 141  
 142      public function test_summarise_response() {
 143          $question = test_question_maker::make_question('randomsamatch');
 144          $question->shufflestems = false;
 145          $question->start_attempt(new question_attempt_step(), 1);
 146  
 147          $summary = $question->summarise_response(array('sub0' => 2, 'sub1' => 1));
 148  
 149          $this->assertRegExp('/Dog -> \w+; Frog -> \w+/', $summary);
 150      }
 151  
 152  
 153  }