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] [Versions 39 and 310]

   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 for the question_attempt class.
  19   *
  20   * Action methods like start, process_action and finish are assumed to be
  21   * tested by walkthrough tests in the various behaviours.
  22   *
  23   * @package    moodlecore
  24   * @subpackage questionengine
  25   * @copyright  2009 The Open University
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  global $CFG;
  33  require_once (__DIR__ . '/../lib.php');
  34  require_once (__DIR__ . '/helpers.php');
  35  
  36  
  37  /**
  38   * Unit tests for the {@link question_attempt} class.
  39   *
  40   * These are the tests that don't require any steps.
  41   *
  42   * @copyright  2009 The Open University
  43   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  44   */
  45  class question_attempt_testcase extends advanced_testcase {
  46      /** @var question_definition a question that can be used in the tests. */
  47      private $question;
  48      /** @var int fake question_usage id used in some tests. */
  49      private $usageid;
  50      /** @var question_attempt a question attempt that can be used in the tests. */
  51      private $qa;
  52  
  53      protected function setUp(): void {
  54          $this->question = test_question_maker::make_question('description');
  55          $this->question->defaultmark = 3;
  56          $this->usageid = 13;
  57          $this->qa = new question_attempt($this->question, $this->usageid);
  58      }
  59  
  60      protected function tearDown(): void {
  61          $this->question = null;
  62          $this->useageid = null;
  63          $this->qa = null;
  64      }
  65  
  66      public function test_constructor_sets_maxmark() {
  67          $qa = new question_attempt($this->question, $this->usageid);
  68          $this->assertSame($this->question, $qa->get_question(false));
  69          $this->assertEquals(3, $qa->get_max_mark());
  70      }
  71  
  72      public function test_maxmark_beats_default_mark() {
  73          $qa = new question_attempt($this->question, $this->usageid, null, 2);
  74          $this->assertEquals(2, $qa->get_max_mark());
  75      }
  76  
  77      public function test_get_set_slot() {
  78          $this->qa->set_slot(7);
  79          $this->assertEquals(7, $this->qa->get_slot());
  80      }
  81  
  82      public function test_fagged_initially_false() {
  83          $this->assertEquals(false, $this->qa->is_flagged());
  84      }
  85  
  86      public function test_set_is_flagged() {
  87          $this->qa->set_flagged(true);
  88          $this->assertEquals(true, $this->qa->is_flagged());
  89      }
  90  
  91      public function test_get_qt_field_name() {
  92          $name = $this->qa->get_qt_field_name('test');
  93          $this->assertRegExp('/^' . preg_quote($this->qa->get_field_prefix(), '/') . '/', $name);
  94          $this->assertRegExp('/_test$/', $name);
  95      }
  96  
  97      public function test_get_behaviour_field_name() {
  98          $name = $this->qa->get_behaviour_field_name('test');
  99          $this->assertRegExp('/^' . preg_quote($this->qa->get_field_prefix(), '/') . '/', $name);
 100          $this->assertRegExp('/_-test$/', $name);
 101      }
 102  
 103      public function test_get_field_prefix() {
 104          $this->qa->set_slot(7);
 105          $name = $this->qa->get_field_prefix();
 106          $this->assertRegExp('/' . preg_quote($this->usageid, '/') . '/', $name);
 107          $this->assertRegExp('/' . preg_quote($this->qa->get_slot(), '/') . '/', $name);
 108      }
 109  
 110      public function test_get_submitted_var_not_present_var_returns_null() {
 111          $this->assertNull($this->qa->get_submitted_var(
 112                  'reallyunlikelyvariablename', PARAM_BOOL));
 113      }
 114  
 115      public function test_get_all_submitted_qt_vars() {
 116          $this->qa->set_usage_id('MDOgzdhS4W');
 117          $this->qa->set_slot(1);
 118          $this->assertEquals(array('omval_response1' => 1, 'omval_response2' => 666, 'omact_gen_14' => 'Check'),
 119                  $this->qa->get_all_submitted_qt_vars(array(
 120                      'qMDOgzdhS4W:1_omval_response1' => 1,
 121                      'qMDOgzdhS4W:1_omval_response2' => 666,
 122                      'qMDOgzdhS4W:1_omact_gen_14' => 'Check',
 123                  )));
 124      }
 125  }