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_multichoice;
  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   * Unit tests for the multiple choice, multi-response question definition class.
  31   *
  32   * @package   qtype_multichoice
  33   * @copyright 2009 The Open University
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class question_multi_test extends \advanced_testcase {
  37  
  38      public function test_get_expected_data() {
  39          $question = \test_question_maker::make_a_multichoice_multi_question();
  40          $question->start_attempt(new question_attempt_step(), 1);
  41  
  42          $this->assertEquals(array('choice0' => PARAM_BOOL, 'choice1' => PARAM_BOOL,
  43                  'choice2' => PARAM_BOOL, 'choice3' => PARAM_BOOL), $question->get_expected_data());
  44      }
  45  
  46      public function test_is_complete_response() {
  47          $question = \test_question_maker::make_a_multichoice_multi_question();
  48          $question->start_attempt(new question_attempt_step(), 1);
  49  
  50          $this->assertFalse($question->is_complete_response(array()));
  51          $this->assertFalse($question->is_complete_response(
  52                  array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
  53          $this->assertTrue($question->is_complete_response(array('choice1' => '1')));
  54          $this->assertTrue($question->is_complete_response(
  55                  array('choice0' => '1', 'choice1' => '1', 'choice2' => '1', 'choice3' => '1')));
  56      }
  57  
  58      public function test_is_gradable_response() {
  59          $question = \test_question_maker::make_a_multichoice_multi_question();
  60          $question->start_attempt(new question_attempt_step(), 1);
  61  
  62          $this->assertFalse($question->is_gradable_response(array()));
  63          $this->assertFalse($question->is_gradable_response(
  64                  array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
  65          $this->assertTrue($question->is_gradable_response(array('choice1' => '1')));
  66          $this->assertTrue($question->is_gradable_response(
  67                  array('choice0' => '1', 'choice1' => '1', 'choice2' => '1', 'choice3' => '1')));
  68      }
  69  
  70      public function test_is_same_response() {
  71          $question = \test_question_maker::make_a_multichoice_multi_question();
  72          $question->start_attempt(new question_attempt_step(), 1);
  73  
  74          $this->assertTrue($question->is_same_response(
  75                  array(),
  76                  array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
  77  
  78          $this->assertTrue($question->is_same_response(
  79                  array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0'),
  80                  array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
  81  
  82          $this->assertFalse($question->is_same_response(
  83                  array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0'),
  84                  array('choice0' => '1', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
  85  
  86          $this->assertTrue($question->is_same_response(
  87                  array('choice0' => '1', 'choice1' => '0', 'choice2' => '1', 'choice3' => '0'),
  88                  array('choice0' => '1', 'choice1' => '0', 'choice2' => '1', 'choice3' => '0')));
  89      }
  90  
  91      public function test_grading() {
  92          $question = \test_question_maker::make_a_multichoice_multi_question();
  93          $question->start_attempt(new question_attempt_step(), 1);
  94  
  95          $this->assertEquals(array(1, question_state::$gradedright),
  96                  $question->grade_response($question->prepare_simulated_post_data(array('A' => 1, 'C' => 1))));
  97          $this->assertEquals(array(0.5, question_state::$gradedpartial),
  98                  $question->grade_response($question->prepare_simulated_post_data(array('A' => 1))));
  99          $this->assertEquals(array(0, question_state::$gradedwrong),
 100                  $question->grade_response($question->prepare_simulated_post_data(array('A' => 1, 'B' => 1, 'C' => 1))));
 101          $this->assertEquals(array(0, question_state::$gradedwrong),
 102                  $question->grade_response($question->prepare_simulated_post_data(array('B' => 1))));
 103      }
 104  
 105      public function test_get_correct_response() {
 106          $question = \test_question_maker::make_a_multichoice_multi_question();
 107          $question->start_attempt(new question_attempt_step(), 1);
 108  
 109          $this->assertEquals($question->prepare_simulated_post_data(array('A' => 1, 'C' => 1)), $question->get_correct_response());
 110      }
 111  
 112      public function test_get_question_summary() {
 113          $mc = \test_question_maker::make_a_multichoice_single_question();
 114          $mc->start_attempt(new question_attempt_step(), 1);
 115  
 116          $qsummary = $mc->get_question_summary();
 117  
 118          $this->assertMatchesRegularExpression('/' . preg_quote($mc->questiontext, '/') . '/', $qsummary);
 119          foreach ($mc->answers as $answer) {
 120              $this->assertMatchesRegularExpression('/' . preg_quote($answer->answer, '/') . '/', $qsummary);
 121          }
 122      }
 123  
 124      public function test_summarise_response() {
 125          $mc = \test_question_maker::make_a_multichoice_multi_question();
 126          $mc->shuffleanswers = false;
 127          $mc->start_attempt(new question_attempt_step(), 1);
 128  
 129          $summary = $mc->summarise_response($mc->prepare_simulated_post_data(array('B' => 1, 'C' => 1)),
 130                  \test_question_maker::get_a_qa($mc));
 131  
 132          $this->assertEquals('B; C', $summary);
 133      }
 134  
 135      public function test_summarise_response_clearchoice() {
 136          $mc = \test_question_maker::make_a_multichoice_multi_question();
 137          $mc->shuffleanswers = false;
 138          $mc->start_attempt(new question_attempt_step(), 1);
 139  
 140          $summary = $mc->summarise_response($mc->prepare_simulated_post_data(array('clearchoice' => -1)),
 141              \test_question_maker::get_a_qa($mc));
 142  
 143          $this->assertNull($summary);
 144      }
 145  
 146      public function test_classify_response() {
 147          $mc = \test_question_maker::make_a_multichoice_multi_question();
 148          $mc->start_attempt(new question_attempt_step(), 1);
 149  
 150          $this->assertEquals(array(
 151                      13 => new question_classified_response(13, 'A', 0.5),
 152                      14 => new question_classified_response(14, 'B', -1.0),
 153                  ), $mc->classify_response($mc->prepare_simulated_post_data(array('A' => 1, 'B' => 1))));
 154  
 155          $this->assertEquals(array(), $mc->classify_response(array()));
 156      }
 157  
 158      public function test_prepare_simulated_post_data() {
 159          $mc = \test_question_maker::make_a_multichoice_multi_question();
 160          $mc->start_attempt(new question_attempt_step(), 1);
 161          $correctanswers = array(
 162              array(),
 163              array('A' => 1),
 164              array('B' => 1, 'D' => 0),
 165              array('A' => 0, 'B' => 0, 'C' => 0, 'D' => 0),
 166              array('A' => 1, 'B' => 0, 'C' => 1, 'D' => 0),
 167              array('A' => 1, 'B' => 0, 'C' => 1, 'D' => 1),
 168              array('A' => 1, 'B' => 1, 'C' => 1, 'D' => 1)
 169          );
 170          foreach ($correctanswers as $correctanswer) {
 171              $postdata = $mc->prepare_simulated_post_data($correctanswer);
 172              $simulatedreponse = $mc->get_student_response_values_for_simulation($postdata);
 173              $this->assertEqualsCanonicalizing($correctanswer, $simulatedreponse);
 174          }
 175      }
 176  
 177      /**
 178       * test_get_question_definition_for_external_rendering
 179       */
 180      public function test_get_question_definition_for_external_rendering() {
 181          $question = \test_question_maker::make_a_multichoice_multi_question();
 182          $question->start_attempt(new question_attempt_step(), 1);
 183          $qa = \test_question_maker::get_a_qa($question);
 184          $displayoptions = new question_display_options();
 185  
 186          $options = $question->get_question_definition_for_external_rendering($qa, $displayoptions);
 187          $this->assertEquals(1, $options['shuffleanswers']);
 188          $this->assertEquals('abc', $options['answernumbering']);
 189          $this->assertEquals(0, $options['showstandardinstruction']);
 190          $this->assertEquals(1, $options['shuffleanswers']);
 191      }
 192  }