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