Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   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_truefalse;
  18  
  19  use question_state;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  25  
  26  
  27  /**
  28   * Walkthrough tests for the truefalse question type.
  29   *
  30   * @package    qtype_truefalse
  31   * @copyright  2011 The Open University
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   * @covers     \qtype_truefalse_question
  34   */
  35  class walkthrough_test extends \qbehaviour_walkthrough_test_base {
  36      public function test_false_right_does_not_show_feedback_when_not_answered() {
  37  
  38          // Create a true-false question with correct answer false.
  39          $tf = \test_question_maker::make_question('truefalse', 'false');
  40          $this->start_attempt_at_question($tf, 'deferredfeedback', 1);
  41  
  42          // Check the initial state.
  43          $this->check_current_state(question_state::$todo);
  44          $this->check_current_mark(null);
  45          $this->check_current_output(
  46                  $this->get_contains_question_text_expectation($tf),
  47                  $this->get_does_not_contain_feedback_expectation(),
  48                  new \question_contains_tag_with_contents('h4',
  49                          get_string('questiontext', 'question')));
  50          $this->assertEquals(get_string('false', 'qtype_truefalse'),
  51                  $this->quba->get_right_answer_summary($this->slot));
  52          $this->assertMatchesRegularExpression('/' . preg_quote($tf->questiontext, '/') . '/',
  53                  $this->quba->get_question_summary($this->slot));
  54          $this->assertNull($this->quba->get_response_summary($this->slot));
  55  
  56          // Finish the attempt without answering.
  57          $this->quba->finish_all_questions();
  58  
  59          // Verify.
  60          $this->check_current_state(question_state::$gaveup);
  61          $this->check_current_mark(null);
  62          $this->check_current_output(
  63                  $this->get_contains_tf_true_radio_expectation(false, false),
  64                  $this->get_contains_tf_false_radio_expectation(false, false),
  65  
  66                  // In particular, check that the false feedback is not displayed.
  67                  new \question_no_pattern_expectation('/' . preg_quote($tf->falsefeedback, '/') . '/'));
  68  
  69      }
  70  
  71      public function test_each_attempt_builds_on_last_and_regrade() {
  72  
  73          // Create a true-false question with correct answer false.
  74          $tf = \test_question_maker::make_question('truefalse', 'false');
  75          $this->start_attempt_at_question(clone($tf), 'deferredfeedback', 1);
  76  
  77          // Submit the answer false (correct).
  78          $this->process_submission(['answer' => 0]);
  79  
  80          // Finish the attempt.
  81          $this->quba->finish_all_questions();
  82  
  83          // Check the state.
  84          $this->check_current_state(question_state::$gradedright);
  85          $this->check_current_mark(1);
  86          $this->check_current_output(
  87                  $this->get_contains_tf_true_radio_expectation(false, false),
  88                  $this->get_contains_tf_false_radio_expectation(false, true));
  89  
  90          // Start a new attempt based on the first one.
  91          $firstattemptqa = $this->quba->get_question_attempt($this->slot);
  92          $this->quba = \question_engine::make_questions_usage_by_activity('unit_test',
  93                  \context_system::instance());
  94          $this->quba->set_preferred_behaviour('deferredfeedback');
  95          $this->slot = $this->quba->add_question(clone($tf), 1);
  96          $this->quba->start_question_based_on($this->slot, $firstattemptqa);
  97  
  98          // Verify.
  99          $this->check_current_state(question_state::$complete);
 100          $this->check_current_mark(null);
 101          $this->check_current_output(
 102                  $this->get_contains_tf_true_radio_expectation(true, false),
 103                  $this->get_contains_tf_false_radio_expectation(true, true));
 104  
 105          // Finish the attempt without changing the answer.
 106          $this->quba->finish_all_questions();
 107  
 108          // Check the state.
 109          $this->check_current_state(question_state::$gradedright);
 110          $this->check_current_mark(1);
 111          $this->check_current_output(
 112                  $this->get_contains_tf_true_radio_expectation(false, false),
 113                  $this->get_contains_tf_false_radio_expectation(false, true));
 114  
 115          // Regrade the attempt - code based on question_usage_by_activity::regrade_question.
 116          $oldqa = $this->quba->get_question_attempt($this->slot);
 117          $newqa = new \question_attempt(clone($tf),
 118                  $oldqa->get_usage_id(), $this->quba->get_observer());
 119          $newqa->set_database_id($oldqa->get_database_id());
 120          $newqa->set_slot($oldqa->get_slot());
 121          $newqa->regrade($oldqa, true);
 122  
 123          // Check the state.
 124          $this->assertEquals(question_state::$gradedright, $newqa->get_state());
 125          $this->assertEquals(1, $newqa->get_mark());
 126      }
 127  
 128      /**
 129       * @covers \qtype_truefalse_renderer::formulation_and_controls
 130       */
 131      public function test_deferredfeedback_feedback_multichoice_single_showstandardinstruction_yes() {
 132  
 133          // Create a true-false question with correct answer false.
 134          $tf = \test_question_maker::make_question('truefalse', 'false');
 135          $tf->showstandardinstruction = true;
 136  
 137          $this->start_attempt_at_question($tf, 'deferredfeedback', 1);
 138          $this->render();
 139  
 140          // Check for 'Show standard instruction'.
 141          $standardinstruction = get_string('selectone', 'qtype_truefalse');
 142          $this->assertStringContainsString($standardinstruction, $this->currentoutput);
 143      }
 144  
 145      /**
 146       * @covers \qtype_truefalse_renderer::formulation_and_controls
 147       */
 148      public function test_deferredfeedback_feedback_multichoice_single_showstandardinstruction_no() {
 149  
 150          // Create a true-false question with correct answer false.
 151          $tf = \test_question_maker::make_question('truefalse', 'false');
 152          $tf->showstandardinstruction = false;
 153  
 154          $this->start_attempt_at_question($tf, 'deferredfeedback', 1);
 155          $this->render();
 156  
 157          // Check for 'Show standard instruction'.
 158          $standardinstruction = \html_writer::tag('legend', get_string('answer'), [
 159              'class' => 'prompt h6 font-weight-normal sr-only'
 160          ]);
 161          $this->assertStringContainsString($standardinstruction, $this->currentoutput);
 162      }
 163  }