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_state;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  require_once($CFG->dirroot . '/question/engine/lib.php');
  25  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  26  
  27  
  28  /**
  29   * Unit tests for the mutiple choice question type.
  30   *
  31   * Note, there are already lots of tests of the multichoice type in the behaviour
  32   * tests. (Search for \test_question_maker::make_a_multichoice.) This file only
  33   * contains a few additional tests for problems that were found during testing.
  34   *
  35   * @package    qtype_multichoice
  36   * @copyright  2010 The Open University
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class walkthrough_test extends \qbehaviour_walkthrough_test_base {
  40      public function test_deferredfeedback_feedback_multichoice_single() {
  41  
  42          // Create a multichoice, single question.
  43          $mc = \test_question_maker::make_a_multichoice_single_question();
  44          $mc->shuffleanswers = false;
  45          $mc->answers[14]->fraction = 0.1; // Make one of the choices partially right.
  46          $rightindex = 0;
  47  
  48          $this->start_attempt_at_question($mc, 'deferredfeedback', 3);
  49          $this->process_submission(array('answer' => $rightindex));
  50  
  51          // Verify.
  52          $this->check_current_state(question_state::$complete);
  53          $this->check_current_mark(null);
  54          $this->check_current_output(
  55                  $this->get_contains_mc_radio_expectation($rightindex, true, true),
  56                  $this->get_contains_mc_radio_expectation($rightindex + 1, true, false),
  57                  $this->get_contains_mc_radio_expectation($rightindex + 2, true, false),
  58                  $this->get_does_not_contain_correctness_expectation(),
  59                  $this->get_does_not_contain_feedback_expectation());
  60  
  61          // Finish the attempt.
  62          $this->quba->finish_all_questions();
  63  
  64          // Verify.
  65          $this->check_current_state(question_state::$gradedright);
  66          $this->check_current_mark(3);
  67          $this->check_current_output(
  68                  $this->get_contains_mc_radio_expectation($rightindex, false, true),
  69                  $this->get_contains_correct_expectation(),
  70                  new \question_pattern_expectation('/class="r0 correct"/'),
  71                  new \question_pattern_expectation('/class="r1"/'));
  72      }
  73  
  74      public function test_deferredfeedback_feedback_multichoice_single_showstandardunstruction_yes() {
  75  
  76          // Create a multichoice, single question.
  77          $mc = \test_question_maker::make_a_multichoice_single_question();
  78          $mc->showstandardinstruction = true;
  79  
  80          $this->start_attempt_at_question($mc, 'deferredfeedback', 3);
  81          $this->render();
  82  
  83          // Check for 'Show standard instruction'.
  84          $standardinstruction = get_string('selectone', 'qtype_multichoice');
  85          $this->assertStringContainsString($standardinstruction, $this->currentoutput);
  86      }
  87  
  88      public function test_deferredfeedback_feedback_multichoice_single_showstandardunstruction_no() {
  89  
  90          // Create a multichoice, single question.
  91          $mc = \test_question_maker::make_a_multichoice_single_question();
  92          $mc->showstandardinstruction = false;
  93  
  94          $this->start_attempt_at_question($mc, 'deferredfeedback', 3);
  95          $this->render();
  96  
  97          // Check for 'Show standard instruction'.
  98          $standardinstruction = get_string('selectmulti', 'qtype_multichoice');
  99          $this->assertStringNotContainsString($standardinstruction, $this->currentoutput);
 100      }
 101  
 102      public function test_deferredfeedback_feedback_multichoice_multi() {
 103          // Create a multichoice, multi question.
 104          $mc = \test_question_maker::make_a_multichoice_multi_question();
 105          $mc->shuffleanswers = false;
 106  
 107          $this->start_attempt_at_question($mc, 'deferredfeedback', 2);
 108          $this->process_submission($mc->get_correct_response());
 109          $this->quba->finish_all_questions();
 110  
 111          // Verify.
 112          $this->check_current_state(question_state::$gradedright);
 113          $this->check_current_mark(2);
 114          $this->check_current_output(
 115                  $this->get_contains_mc_checkbox_expectation('choice0', false, true),
 116                  $this->get_contains_mc_checkbox_expectation('choice1', false, false),
 117                  $this->get_contains_mc_checkbox_expectation('choice2', false, true),
 118                  $this->get_contains_mc_checkbox_expectation('choice3', false, false),
 119                  $this->get_contains_correct_expectation(),
 120                  new \question_pattern_expectation('/class="r0 correct"/'),
 121                  new \question_pattern_expectation('/class="r1"/'));
 122      }
 123  
 124      /**
 125       * Test for clear choice option.
 126       */
 127      public function test_deferredfeedback_feedback_multichoice_clearchoice() {
 128  
 129          // Create a multichoice, single question.
 130          $mc = \test_question_maker::make_a_multichoice_single_question();
 131          $mc->shuffleanswers = false;
 132  
 133          $clearchoice = -1;
 134          $rightchoice = 0;
 135          $wrongchoice = 2;
 136  
 137          $this->start_attempt_at_question($mc, 'deferredfeedback', 3);
 138  
 139          // Let's first submit the wrong choice (2).
 140          $this->process_submission(array('answer' => $wrongchoice));  // Wrong choice (2).
 141  
 142          $this->check_current_mark(null);
 143          // Clear choice radio should not be checked.
 144          $this->check_current_output(
 145              $this->get_contains_mc_radio_expectation($rightchoice, true, false), // Not checked.
 146              $this->get_contains_mc_radio_expectation($rightchoice + 1, true, false), // Not checked.
 147              $this->get_contains_mc_radio_expectation($rightchoice + 2, true, true), // Wrong choice (2) checked.
 148              $this->get_contains_mc_radio_expectation($clearchoice, true, false), // Not checked.
 149              $this->get_does_not_contain_correctness_expectation(),
 150              $this->get_does_not_contain_feedback_expectation()
 151          );
 152  
 153          // Now, let's clear our previous choice.
 154          $this->process_submission(array('answer' => $clearchoice)); // Clear choice (-1).
 155          $this->check_current_mark(null);
 156  
 157          // This time, the clear choice radio should be the only one checked.
 158          $this->check_current_output(
 159              $this->get_contains_mc_radio_expectation($rightchoice, true, false), // Not checked.
 160              $this->get_contains_mc_radio_expectation($rightchoice + 1, true, false), // Not checked.
 161              $this->get_contains_mc_radio_expectation($rightchoice + 2, true, false), // Not checked.
 162              $this->get_contains_mc_radio_expectation($clearchoice, true, true), // Clear choice radio checked.
 163              $this->get_does_not_contain_correctness_expectation(),
 164              $this->get_does_not_contain_feedback_expectation()
 165          );
 166  
 167          // Finally, let's submit the right choice.
 168          $this->process_submission(array('answer' => $rightchoice)); // Right choice (0).
 169          $this->check_current_state(question_state::$complete);
 170          $this->check_current_mark(null);
 171          $this->check_current_output(
 172              $this->get_contains_mc_radio_expectation($rightchoice, true, true),
 173              $this->get_contains_mc_radio_expectation($rightchoice + 1, true, false),
 174              $this->get_contains_mc_radio_expectation($rightchoice + 2, true, false),
 175              $this->get_contains_mc_radio_expectation($clearchoice, true, false),
 176              $this->get_does_not_contain_correctness_expectation(),
 177              $this->get_does_not_contain_feedback_expectation()
 178          );
 179  
 180          // Finish the attempt.
 181          $this->finish();
 182  
 183          // Verify.
 184          $this->check_current_state(question_state::$gradedright);
 185          $this->check_current_mark(3);
 186          $this->check_current_output(
 187              $this->get_contains_mc_radio_expectation($rightchoice, false, true),
 188              $this->get_contains_correct_expectation(),
 189              new \question_pattern_expectation('/class="r0 correct"/'),
 190              new \question_pattern_expectation('/class="r1"/'));
 191      }
 192  
 193      public function test_deferredfeedback_feedback_multichoice_multi_showstandardunstruction_yes() {
 194  
 195          // Create a multichoice, multi question.
 196          $mc = \test_question_maker::make_a_multichoice_multi_question();
 197          $mc->showstandardinstruction = true;
 198  
 199          $this->start_attempt_at_question($mc, 'deferredfeedback', 3);
 200          $this->render();
 201  
 202          // Check for 'Show standard instruction'.
 203          $standardinstruction = get_string('selectmulti', 'qtype_multichoice');
 204          $this->assertStringContainsString($standardinstruction, $this->currentoutput);
 205      }
 206  
 207      public function test_deferredfeedback_feedback_multichoice_multi_showstandardunstruction_no() {
 208  
 209          // Create a multichoice, multi question.
 210          $mc = \test_question_maker::make_a_multichoice_multi_question();
 211          $mc->showstandardinstruction = false;
 212  
 213          $this->start_attempt_at_question($mc, 'deferredfeedback', 3);
 214          $this->render();
 215  
 216          // Check for 'Show standard instruction'.
 217          $standardinstruction = get_string('selectmulti', 'qtype_multichoice');
 218          $this->assertStringNotContainsString($standardinstruction, $this->currentoutput);
 219      }
 220  
 221  }