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