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