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 39 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_numerical;
  18  
  19  use question_hint;
  20  use question_state;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  global $CFG;
  25  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  26  require_once($CFG->dirroot . '/question/type/numerical/tests/helper.php');
  27  
  28  
  29  /**
  30   * Unit tests for the numerical question type.
  31   *
  32   * @package    qtype_numerical
  33   * @copyright  2011 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_currency() {
  38  
  39          // Create a gapselect question.
  40          $q = \test_question_maker::make_question('numerical', 'currency');
  41          $q->hints = array(
  42              new question_hint(1, 'This is the first hint.', FORMAT_HTML),
  43              new question_hint(2, 'This is the second hint.', FORMAT_HTML),
  44          );
  45          $this->start_attempt_at_question($q, 'interactive', 3);
  46  
  47          // Check the initial state.
  48          $this->check_current_state(question_state::$todo);
  49          $this->check_current_mark(null);
  50          $this->check_current_output(
  51                  $this->get_contains_marked_out_of_summary(),
  52                  $this->get_contains_submit_button_expectation(true),
  53                  $this->get_does_not_contain_feedback_expectation(),
  54                  $this->get_does_not_contain_validation_error_expectation(),
  55                  $this->get_does_not_contain_try_again_button_expectation(),
  56                  $this->get_no_hint_visible_expectation());
  57  
  58          // Submit blank.
  59          $this->process_submission(array('-submit' => 1, 'answer' => ''));
  60  
  61          // Verify.
  62          $this->check_current_state(question_state::$invalid);
  63          $this->check_current_mark(null);
  64          $this->check_current_output(
  65                  $this->get_contains_marked_out_of_summary(),
  66                  $this->get_contains_submit_button_expectation(true),
  67                  $this->get_does_not_contain_feedback_expectation(),
  68                  $this->get_contains_validation_error_expectation(),
  69                  $this->get_does_not_contain_try_again_button_expectation(),
  70                  $this->get_no_hint_visible_expectation());
  71  
  72          // Sumit something that does not look liek a number.
  73          $this->process_submission(array('-submit' => 1, 'answer' => 'newt'));
  74  
  75          // Verify.
  76          $this->check_current_state(question_state::$invalid);
  77          $this->check_current_mark(null);
  78          $this->check_current_output(
  79                  $this->get_contains_marked_out_of_summary(),
  80                  $this->get_contains_submit_button_expectation(true),
  81                  $this->get_does_not_contain_feedback_expectation(),
  82                  $this->get_contains_validation_error_expectation(),
  83                  new \question_pattern_expectation('/' .
  84                          preg_quote(get_string('invalidnumber', 'qtype_numerical'), '/') . '/'),
  85                  $this->get_does_not_contain_try_again_button_expectation(),
  86                  $this->get_no_hint_visible_expectation());
  87  
  88          // Now get it right.
  89          $this->process_submission(array('-submit' => 1, 'answer' => '$1332'));
  90  
  91          // Verify.
  92          $this->check_current_state(question_state::$gradedright);
  93          $this->check_current_mark(3);
  94          $this->check_current_output(
  95                  $this->get_contains_mark_summary(3),
  96                  $this->get_does_not_contain_submit_button_expectation(),
  97                  $this->get_contains_correct_expectation(),
  98                  $this->get_does_not_contain_validation_error_expectation(),
  99                  $this->get_no_hint_visible_expectation());
 100          $this->assertEquals('$1332',
 101                  $this->quba->get_response_summary($this->slot));
 102      }
 103  
 104      public function test_deferredfeedback_currency() {
 105  
 106          // Create a gapselect question.
 107          $q = \test_question_maker::make_question('numerical', 'currency');
 108          $this->start_attempt_at_question($q, 'deferredfeedback', 3);
 109  
 110          // Check the initial state.
 111          $this->check_current_state(question_state::$todo);
 112          $this->check_current_mark(null);
 113          $this->check_current_output(
 114                  $this->get_contains_marked_out_of_summary(),
 115                  $this->get_does_not_contain_feedback_expectation(),
 116                  $this->get_does_not_contain_validation_error_expectation(),
 117                  $this->get_does_not_contain_try_again_button_expectation(),
 118                  $this->get_no_hint_visible_expectation());
 119  
 120          // Submit blank.
 121          $this->process_submission(array('answer' => ''));
 122  
 123          // Verify.
 124          $this->check_current_state(question_state::$todo);
 125          $this->check_current_mark(null);
 126          $this->check_current_output(
 127                  $this->get_contains_marked_out_of_summary(),
 128                  $this->get_does_not_contain_feedback_expectation(),
 129                  $this->get_does_not_contain_validation_error_expectation(),
 130                  $this->get_does_not_contain_try_again_button_expectation(),
 131                  $this->get_no_hint_visible_expectation());
 132  
 133          // Sumit something that does not look like a number.
 134          $this->process_submission(array('answer' => '$newt'));
 135  
 136          // Verify.
 137          $this->check_current_state(question_state::$invalid);
 138          $this->check_current_mark(null);
 139          $this->check_current_output(
 140                  $this->get_contains_marked_out_of_summary(),
 141                  $this->get_does_not_contain_feedback_expectation(),
 142                  $this->get_contains_validation_error_expectation(),
 143                  new \question_pattern_expectation('/' .
 144                          preg_quote(get_string('invalidnumber', 'qtype_numerical'), '/') . '/'),
 145                  $this->get_does_not_contain_try_again_button_expectation(),
 146                  $this->get_no_hint_visible_expectation());
 147  
 148          // Now put in the right answer but without a unit.
 149          $this->process_submission(array('answer' => '1332'));
 150  
 151          $this->check_current_state(question_state::$complete);
 152          $this->check_current_mark(null);
 153          $this->check_current_output(
 154                  $this->get_contains_marked_out_of_summary(),
 155                  $this->get_does_not_contain_feedback_expectation(),
 156                  $this->get_does_not_contain_validation_error_expectation(),
 157                  $this->get_does_not_contain_try_again_button_expectation(),
 158                  $this->get_no_hint_visible_expectation());
 159  
 160          // Submit all and finish.
 161          $this->finish();
 162  
 163          // Verify.
 164          $this->check_current_state(question_state::$gradedpartial);
 165          $this->check_current_mark(2.4);
 166          $this->check_current_output(
 167                  $this->get_contains_mark_summary(2.4),
 168                  $this->get_contains_partcorrect_expectation(),
 169                  $this->get_does_not_contain_validation_error_expectation(),
 170                  $this->get_no_hint_visible_expectation());
 171          $this->assertEquals('1332',
 172                  $this->quba->get_response_summary($this->slot));
 173      }
 174  
 175      // Todo. Test validation if you try to type a unit into a question that does
 176      // not expect one.
 177  
 178      public function test_deferredfeedback_unit() {
 179  
 180          // Create a gapselect question.
 181          $q = \test_question_maker::make_question('numerical', 'unit');
 182          $this->start_attempt_at_question($q, 'deferredfeedback', 1);
 183  
 184          $unitchoices = array(
 185              'm' => 'm',
 186              'cm' => 'cm',
 187          );
 188  
 189          // Check the initial state.
 190          $this->check_current_state(question_state::$todo);
 191          $this->check_current_mark(null);
 192          $this->check_current_output(
 193                  $this->get_contains_marked_out_of_summary(),
 194                  $this->get_does_not_contain_feedback_expectation(),
 195                  $this->get_does_not_contain_validation_error_expectation(),
 196                  $this->get_does_not_contain_try_again_button_expectation(),
 197                  $this->get_no_hint_visible_expectation());
 198          $this->check_output_contains_selectoptions(
 199                  $this->get_contains_select_expectation('unit', $unitchoices, null, true));
 200  
 201          // Submit blank.
 202          $this->process_submission(array('answer' => ''));
 203  
 204          // Verify.
 205          $this->check_current_state(question_state::$todo);
 206          $this->check_current_mark(null);
 207          $this->check_current_output(
 208                  $this->get_contains_marked_out_of_summary(),
 209                  $this->get_does_not_contain_feedback_expectation(),
 210                  $this->get_does_not_contain_validation_error_expectation(),
 211                  $this->get_does_not_contain_try_again_button_expectation(),
 212                  $this->get_no_hint_visible_expectation());
 213          $this->check_output_contains_selectoptions(
 214                  $this->get_contains_select_expectation('unit', $unitchoices, null, true));
 215  
 216          // Sumit something that does not look like a number, but with a unit.
 217          $this->process_submission(array('answer' => 'newt', 'unit' => 'cm'));
 218  
 219          // Verify.
 220          $this->check_current_state(question_state::$invalid);
 221          $this->check_current_mark(null);
 222          $this->check_current_output(
 223                  $this->get_contains_marked_out_of_summary(),
 224                  $this->get_does_not_contain_feedback_expectation(),
 225                  $this->get_contains_validation_error_expectation(),
 226                  new \question_pattern_expectation('/' .
 227                          preg_quote(get_string('invalidnumber', 'qtype_numerical'), '/') . '/'),
 228                  $this->get_does_not_contain_try_again_button_expectation(),
 229                  $this->get_no_hint_visible_expectation());
 230          $this->check_output_contains_selectoptions(
 231                  $this->get_contains_select_expectation('unit', $unitchoices, 'cm', true));
 232  
 233          // Now put in the right answer but without a unit.
 234          $this->process_submission(array('answer' => '1.25', 'unit' => ''));
 235  
 236          $this->check_current_state(question_state::$invalid);
 237          $this->check_current_mark(null);
 238          $this->check_current_output(
 239                  $this->get_contains_marked_out_of_summary(),
 240                  $this->get_does_not_contain_feedback_expectation(),
 241                  $this->get_contains_validation_error_expectation(),
 242                  new \question_pattern_expectation('/' .
 243                          preg_quote(get_string('unitnotselected', 'qtype_numerical'), '/') . '/'),
 244                  $this->get_does_not_contain_try_again_button_expectation(),
 245                  $this->get_no_hint_visible_expectation());
 246          $this->check_output_contains_selectoptions(
 247                  $this->get_contains_select_expectation('unit', $unitchoices, '', true));
 248  
 249          // Now put in the right answer with a unit.
 250          $this->process_submission(array('answer' => '1.25', 'unit' => 'm'));
 251  
 252          $this->check_current_state(question_state::$complete);
 253          $this->check_current_mark(null);
 254          $this->check_current_output(
 255                  $this->get_contains_marked_out_of_summary(),
 256                  $this->get_does_not_contain_feedback_expectation(),
 257                  $this->get_does_not_contain_validation_error_expectation(),
 258                  $this->get_does_not_contain_try_again_button_expectation(),
 259                  $this->get_no_hint_visible_expectation());
 260          $this->check_output_contains_selectoptions(
 261                  $this->get_contains_select_expectation('unit', $unitchoices, 'm', true));
 262  
 263          // Submit all and finish.
 264          $this->finish();
 265  
 266          // Verify.
 267          $this->check_current_state(question_state::$gradedright);
 268          $this->check_current_mark(1);
 269          $this->check_current_output(
 270                  $this->get_contains_mark_summary(1),
 271                  $this->get_contains_correct_expectation(),
 272                  $this->get_does_not_contain_validation_error_expectation(),
 273                  $this->get_no_hint_visible_expectation());
 274          $this->check_output_contains_selectoptions(
 275                  $this->get_contains_select_expectation('unit', $unitchoices, 'm', false));
 276          $this->assertEquals('1.25 m',
 277                  $this->quba->get_response_summary($this->slot));
 278      }
 279  }