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 overall tests of calculated questions.
  19   *
  20   * @package    qtype
  21   * @subpackage calculated
  22   * @copyright  2011 The Open University
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  31  
  32  
  33  /**
  34   * Unit tests for the calculated question type.
  35   *
  36   * @copyright  2011 The Open University
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class qtype_calculated_walkthrough_test extends qbehaviour_walkthrough_test_base {
  40      public function test_interactive() {
  41  
  42          // Create a gapselect question.
  43          $q = test_question_maker::make_question('calculated');
  44          $q->hints = array(
  45              new question_hint(1, 'This is the first hint.', FORMAT_HTML),
  46              new question_hint(2, 'This is the second hint.', FORMAT_HTML),
  47          );
  48          $this->start_attempt_at_question($q, 'interactive', 3, 1);
  49          $values = $q->vs->get_values();
  50          $this->assertEquals($values, $q->datasetloader->load_values(1));
  51  
  52          // Check the initial state.
  53          $this->check_current_state(question_state::$todo);
  54          $this->check_current_mark(null);
  55          $this->check_current_output(
  56                  $this->get_contains_marked_out_of_summary(),
  57                  $this->get_contains_submit_button_expectation(true),
  58                  $this->get_does_not_contain_feedback_expectation(),
  59                  $this->get_does_not_contain_validation_error_expectation(),
  60                  $this->get_does_not_contain_try_again_button_expectation(),
  61                  $this->get_no_hint_visible_expectation());
  62  
  63          // Submit blank.
  64          $this->process_submission(array('-submit' => 1, 'answer' => ''));
  65  
  66          // Verify.
  67          $this->check_current_state(question_state::$invalid);
  68          $this->check_current_mark(null);
  69          $this->check_current_output(
  70                  $this->get_contains_marked_out_of_summary(),
  71                  $this->get_contains_submit_button_expectation(true),
  72                  $this->get_does_not_contain_feedback_expectation(),
  73                  $this->get_contains_validation_error_expectation(),
  74                  $this->get_does_not_contain_try_again_button_expectation(),
  75                  $this->get_no_hint_visible_expectation());
  76  
  77          // Sumit something that does not look like a number.
  78          $this->process_submission(array('-submit' => 1, 'answer' => 'newt'));
  79  
  80          // Verify.
  81          $this->check_current_state(question_state::$invalid);
  82          $this->check_current_mark(null);
  83          $this->check_current_output(
  84                  $this->get_contains_marked_out_of_summary(),
  85                  $this->get_contains_submit_button_expectation(true),
  86                  $this->get_does_not_contain_feedback_expectation(),
  87                  $this->get_contains_validation_error_expectation(),
  88                  new question_pattern_expectation('/' .
  89                          preg_quote(get_string('invalidnumber', 'qtype_numerical'), '/') . '/'),
  90                  $this->get_does_not_contain_try_again_button_expectation(),
  91                  $this->get_no_hint_visible_expectation());
  92  
  93          // Now get it right.
  94          $this->process_submission(array('-submit' => 1, 'answer' => $values['a'] + $values['b']));
  95  
  96          // Verify.
  97          $this->check_current_state(question_state::$gradedright);
  98          $this->check_current_mark(3);
  99          $this->check_current_output(
 100                  $this->get_contains_mark_summary(3),
 101                  $this->get_does_not_contain_submit_button_expectation(),
 102                  $this->get_contains_correct_expectation(),
 103                  $this->get_does_not_contain_validation_error_expectation(),
 104                  $this->get_no_hint_visible_expectation());
 105      }
 106  }