Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402]

   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   * @covers \qtype_multichoice_multi_question
  36   */
  37  class question_multi_test extends \advanced_testcase {
  38  
  39      public function test_get_expected_data() {
  40          $question = \test_question_maker::make_a_multichoice_multi_question();
  41          $question->start_attempt(new question_attempt_step(), 1);
  42  
  43          $this->assertEquals(array('choice0' => PARAM_BOOL, 'choice1' => PARAM_BOOL,
  44                  'choice2' => PARAM_BOOL, 'choice3' => PARAM_BOOL), $question->get_expected_data());
  45      }
  46  
  47      public function test_is_complete_response() {
  48          $question = \test_question_maker::make_a_multichoice_multi_question();
  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('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
  54          $this->assertTrue($question->is_complete_response(array('choice1' => '1')));
  55          $this->assertTrue($question->is_complete_response(
  56                  array('choice0' => '1', 'choice1' => '1', 'choice2' => '1', 'choice3' => '1')));
  57      }
  58  
  59      public function test_is_gradable_response() {
  60          $question = \test_question_maker::make_a_multichoice_multi_question();
  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('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
  66          $this->assertTrue($question->is_gradable_response(array('choice1' => '1')));
  67          $this->assertTrue($question->is_gradable_response(
  68                  array('choice0' => '1', 'choice1' => '1', 'choice2' => '1', 'choice3' => '1')));
  69      }
  70  
  71      public function test_is_same_response() {
  72          $question = \test_question_maker::make_a_multichoice_multi_question();
  73          $question->start_attempt(new question_attempt_step(), 1);
  74  
  75          $this->assertTrue($question->is_same_response(
  76                  array(),
  77                  array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
  78  
  79          $this->assertTrue($question->is_same_response(
  80                  array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0'),
  81                  array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
  82  
  83          $this->assertFalse($question->is_same_response(
  84                  array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0'),
  85                  array('choice0' => '1', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
  86  
  87          $this->assertTrue($question->is_same_response(
  88                  array('choice0' => '1', 'choice1' => '0', 'choice2' => '1', 'choice3' => '0'),
  89                  array('choice0' => '1', 'choice1' => '0', 'choice2' => '1', 'choice3' => '0')));
  90      }
  91  
  92      public function test_grading() {
  93          $question = \test_question_maker::make_a_multichoice_multi_question();
  94          $question->start_attempt(new question_attempt_step(), 1);
  95  
  96          $this->assertEquals(array(1, question_state::$gradedright),
  97                  $question->grade_response($question->prepare_simulated_post_data(array('A' => 1, 'C' => 1))));
  98          $this->assertEquals(array(0.5, question_state::$gradedpartial),
  99                  $question->grade_response($question->prepare_simulated_post_data(array('A' => 1))));
 100          $this->assertEquals(array(0, question_state::$gradedwrong),
 101                  $question->grade_response($question->prepare_simulated_post_data(array('A' => 1, 'B' => 1, 'C' => 1))));
 102          $this->assertEquals(array(0, question_state::$gradedwrong),
 103                  $question->grade_response($question->prepare_simulated_post_data(array('B' => 1))));
 104      }
 105  
 106      public function test_get_correct_response() {
 107          $question = \test_question_maker::make_a_multichoice_multi_question();
 108          $question->start_attempt(new question_attempt_step(), 1);
 109  
 110          $this->assertEquals($question->prepare_simulated_post_data(array('A' => 1, 'C' => 1)), $question->get_correct_response());
 111      }
 112  
 113      public function test_get_question_summary() {
 114          $mc = \test_question_maker::make_a_multichoice_single_question();
 115          $mc->start_attempt(new question_attempt_step(), 1);
 116  
 117          $qsummary = $mc->get_question_summary();
 118  
 119          $this->assertMatchesRegularExpression('/' . preg_quote($mc->questiontext, '/') . '/', $qsummary);
 120          foreach ($mc->answers as $answer) {
 121              $this->assertMatchesRegularExpression('/' . preg_quote($answer->answer, '/') . '/', $qsummary);
 122          }
 123      }
 124  
 125      public function test_summarise_response() {
 126          $mc = \test_question_maker::make_a_multichoice_multi_question();
 127          $mc->shuffleanswers = false;
 128          $mc->start_attempt(new question_attempt_step(), 1);
 129  
 130          $summary = $mc->summarise_response($mc->prepare_simulated_post_data(['B' => 1, 'C' => 1]));
 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(['clearchoice' => -1]));
 141  
 142          $this->assertNull($summary);
 143      }
 144  
 145      public function test_un_summarise_response() {
 146          $mc = \test_question_maker::make_a_multichoice_multi_question();
 147          $mc->shuffleanswers = false;
 148          $mc->start_attempt(new question_attempt_step(), 1);
 149  
 150          $this->assertEquals(['choice1' => '1', 'choice2' => '1'], $mc->un_summarise_response('B; C'));
 151  
 152          $this->assertEquals([], $mc->un_summarise_response(''));
 153      }
 154  
 155      public function test_classify_response() {
 156          $mc = \test_question_maker::make_a_multichoice_multi_question();
 157          $mc->start_attempt(new question_attempt_step(), 1);
 158  
 159          $this->assertEquals(array(
 160                      13 => new question_classified_response(13, 'A', 0.5),
 161                      14 => new question_classified_response(14, 'B', -1.0),
 162                  ), $mc->classify_response($mc->prepare_simulated_post_data(array('A' => 1, 'B' => 1))));
 163  
 164          $this->assertEquals(array(), $mc->classify_response(array()));
 165      }
 166  
 167      public function test_prepare_simulated_post_data() {
 168          $mc = \test_question_maker::make_a_multichoice_multi_question();
 169          $mc->start_attempt(new question_attempt_step(), 1);
 170          $correctanswers = array(
 171              array(),
 172              array('A' => 1),
 173              array('B' => 1, 'D' => 0),
 174              array('A' => 0, 'B' => 0, 'C' => 0, 'D' => 0),
 175              array('A' => 1, 'B' => 0, 'C' => 1, 'D' => 0),
 176              array('A' => 1, 'B' => 0, 'C' => 1, 'D' => 1),
 177              array('A' => 1, 'B' => 1, 'C' => 1, 'D' => 1)
 178          );
 179          foreach ($correctanswers as $correctanswer) {
 180              $postdata = $mc->prepare_simulated_post_data($correctanswer);
 181              $simulatedreponse = $mc->get_student_response_values_for_simulation($postdata);
 182              $this->assertEqualsCanonicalizing($correctanswer, $simulatedreponse);
 183          }
 184      }
 185  
 186      /**
 187       * test_get_question_definition_for_external_rendering
 188       */
 189      public function test_get_question_definition_for_external_rendering() {
 190          $question = \test_question_maker::make_a_multichoice_multi_question();
 191          $question->start_attempt(new question_attempt_step(), 1);
 192          $qa = \test_question_maker::get_a_qa($question);
 193          $displayoptions = new question_display_options();
 194  
 195          $options = $question->get_question_definition_for_external_rendering($qa, $displayoptions);
 196          $this->assertEquals(1, $options['shuffleanswers']);
 197          $this->assertEquals('abc', $options['answernumbering']);
 198          $this->assertEquals(0, $options['showstandardinstruction']);
 199          $this->assertEquals(1, $options['shuffleanswers']);
 200      }
 201  }