Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

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