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 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_gapselect;
  18  
  19  use question_hint_with_parts;
  20  use question_state;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  global $CFG;
  24  
  25  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  26  require_once($CFG->dirroot . '/question/type/gapselect/tests/helper.php');
  27  
  28  
  29  /**
  30   * Unit tests for the gap-select question type.
  31   *
  32   * @package   qtype_gapselect
  33   * @copyright 2012 The Open University
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class walkthrough_test extends \qbehaviour_walkthrough_test_base {
  37      public function test_interactive_behaviour() {
  38  
  39          // Create a gapselect question.
  40          $q = \test_question_maker::make_question('gapselect');
  41          $q->hints = array(
  42              new question_hint_with_parts(1, 'This is the first hint.', FORMAT_HTML, false, false),
  43              new question_hint_with_parts(2, 'This is the second hint.', FORMAT_HTML, true, true),
  44          );
  45          $q->shufflechoices = false;
  46          $this->start_attempt_at_question($q, 'interactive', 3);
  47  
  48          // Check the initial state.
  49          $this->check_current_state(question_state::$todo);
  50          $this->check_current_mark(null);
  51          $this->check_current_output(
  52                  $this->get_contains_submit_button_expectation(true),
  53                  $this->get_does_not_contain_feedback_expectation(),
  54                  $this->get_tries_remaining_expectation(3),
  55                  $this->get_no_hint_visible_expectation());
  56          // Note it is possible to check the first select option as below, but it is not required.
  57          // Also note the ' ' in the p2 example below is a nbsp (used when names are short).
  58          $this->check_output_contains_selectoptions(
  59                  $this->get_contains_select_expectation('p1',
  60                          ['0' => '&nbsp;', '1' => 'quick', '2' => 'slow'], null, true),
  61                  $this->get_contains_select_expectation('p2',
  62                          ['0' => '&nbsp;', '1' => 'fox', '2' => 'dog'], null, true),
  63                  $this->get_contains_select_expectation('p3',
  64                          ['1' => 'lazy', '2' => 'assiduous'], null, true));
  65  
  66          // Save the wrong answer.
  67          $this->process_submission(array('p1' => '2', 'p2' => '2', 'p3' => '2'));
  68  
  69          // Verify.
  70          $this->check_current_state(question_state::$todo);
  71          $this->check_current_mark(null);
  72          $this->check_current_output(
  73                  $this->get_contains_submit_button_expectation(true),
  74                  $this->get_does_not_contain_correctness_expectation(),
  75                  $this->get_does_not_contain_feedback_expectation(),
  76                  $this->get_tries_remaining_expectation(3),
  77                  $this->get_no_hint_visible_expectation());
  78          $this->check_output_contains_selectoptions(
  79                  $this->get_contains_select_expectation('p1',
  80                          ['1' => 'quick', '2' => 'slow'], 2, true),
  81                  $this->get_contains_select_expectation('p2',
  82                          ['1' => 'fox', '2' => 'dog'], 2, true),
  83                  $this->get_contains_select_expectation('p3',
  84                          ['1' => 'lazy', '2' => 'assiduous'], 2, true));
  85  
  86          // Submit the wrong answer.
  87          $this->process_submission(array('p1' => '2', 'p2' => '2', 'p3' => '2', '-submit' => 1));
  88  
  89          // Verify.
  90          $this->check_current_state(question_state::$todo);
  91          $this->check_current_mark(null);
  92          $this->check_current_output(
  93                  $this->get_does_not_contain_submit_button_expectation(),
  94                  $this->get_contains_try_again_button_expectation(true),
  95                  $this->get_does_not_contain_correctness_expectation(),
  96                  $this->get_contains_hint_expectation('This is the first hint'));
  97          $this->check_output_contains_selectoptions(
  98                  $this->get_contains_select_expectation('p1',
  99                          ['1' => 'quick', '2' => 'slow'], 2, false),
 100                  $this->get_contains_select_expectation('p2',
 101                          ['1' => 'fox', '2' => 'dog'], 2, false),
 102                  $this->get_contains_select_expectation('p3',
 103                          ['1' => 'lazy', '2' => 'assiduous'], 2, false));
 104  
 105          // Do try again.
 106          $this->process_submission(array('-tryagain' => 1));
 107  
 108          // Verify.
 109          $this->check_current_state(question_state::$todo);
 110          $this->check_current_mark(null);
 111          $this->check_current_output(
 112                  $this->get_contains_submit_button_expectation(true),
 113                  $this->get_does_not_contain_correctness_expectation(),
 114                  $this->get_does_not_contain_feedback_expectation(),
 115                  $this->get_tries_remaining_expectation(2),
 116                  $this->get_no_hint_visible_expectation());
 117          $this->check_output_contains_selectoptions(
 118                  $this->get_contains_select_expectation('p1',
 119                          ['1' => 'quick', '2' => 'slow'], 2, true),
 120                  $this->get_contains_select_expectation('p2',
 121                          ['1' => 'fox', '2' => 'dog'], 2, true),
 122                  $this->get_contains_select_expectation('p3',
 123                          ['1' => 'lazy', '2' => 'assiduous'], 2, true));
 124  
 125          // Submit the right answer.
 126          $this->process_submission(array('p1' => '1', 'p2' => '1', 'p3' => '1', '-submit' => 1));
 127  
 128          // Verify.
 129          $this->check_current_state(question_state::$gradedright);
 130          $this->check_current_mark(2);
 131          $this->check_current_output(
 132                  $this->get_does_not_contain_submit_button_expectation(),
 133                  $this->get_contains_correct_expectation(),
 134                  $this->get_no_hint_visible_expectation());
 135          $this->check_output_contains_selectoptions(
 136                  $this->get_contains_select_expectation('p1',
 137                          ['1' => 'quick', '2' => 'slow'], 1, false),
 138                  $this->get_contains_select_expectation('p2',
 139                          ['1' => 'fox', '2' => 'dog'], 1, false),
 140                  $this->get_contains_select_expectation('p3',
 141                          ['1' => 'lazy', '2' => 'assiduous'], 1, false));
 142  
 143          // Check regrading does not mess anything up.
 144          $this->quba->regrade_all_questions();
 145  
 146          // Verify.
 147          $this->check_current_state(question_state::$gradedright);
 148          $this->check_current_mark(2);
 149      }
 150  
 151      public function test_multilang_behaviour() {
 152  
 153          // Enable multilang filter to on content and heading.
 154          filter_set_global_state('multilang', TEXTFILTER_ON);
 155          filter_set_applies_to_strings('multilang', 1);
 156          $filtermanager = \filter_manager::instance();
 157          $filtermanager->reset_caches();
 158  
 159          // Create a multilang gapselect question.
 160          $q = \test_question_maker::make_question('gapselect', 'multilang');
 161          $q->shufflechoices = false;
 162          $this->start_attempt_at_question($q, 'interactive', 3);
 163  
 164          // Check the initial state.
 165          $this->check_current_state(question_state::$todo);
 166          $this->check_current_mark(null);
 167          $this->check_output_contains_selectoptions(
 168                  $this->get_contains_select_expectation('p1',
 169                          ['1' => 'cat', '2' => 'dog'], null, true),
 170                  $this->get_contains_select_expectation('p2',
 171                          ['1' => 'mat', '2' => 'bat'], null, true));
 172      }
 173  
 174      public function test_choices_containing_dollars() {
 175  
 176          // Choices with a currency like entry (e.g. $3) should display.
 177          $q = \test_question_maker::make_question('gapselect', 'currency');
 178          $q->shufflechoices = false;
 179          $this->start_attempt_at_question($q, 'interactive', 1);
 180  
 181          // Check the initial state.
 182          $this->check_current_state(question_state::$todo);
 183          $this->check_current_mark(null);
 184          $html = $this->quba->render_question($this->slot, $this->displayoptions);
 185          preg_match_all('/<option value="([^>]*)">([^<]*)<\/option>/', $html, $matches);
 186          $this->assertEquals('$2', $matches[2][1]);
 187          $this->assertEquals('$3', $matches[2][2]);
 188          $this->assertEquals('$4.99', $matches[2][3]);
 189          $this->assertEquals('-1', $matches[2][4]);
 190      }
 191  }