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_step class.
  19   *
  20   * @package    moodlecore
  21   * @subpackage questionengine
  22   * @copyright  2009 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 (__DIR__ . '/../lib.php');
  31  require_once (__DIR__ . '/helpers.php');
  32  
  33  
  34  /**
  35   * Unit tests for the {@link question_attempt_step} class.
  36   *
  37   * @copyright  2009 The Open University
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class question_attempt_step_test extends advanced_testcase {
  41      public function test_initial_state_unprocessed() {
  42          $step = new question_attempt_step();
  43          $this->assertEquals(question_state::$unprocessed, $step->get_state());
  44      }
  45  
  46      public function test_get_set_state() {
  47          $step = new question_attempt_step();
  48          $step->set_state(question_state::$gradedright);
  49          $this->assertEquals(question_state::$gradedright, $step->get_state());
  50      }
  51  
  52      public function test_initial_fraction_null() {
  53          $step = new question_attempt_step();
  54          $this->assertNull($step->get_fraction());
  55      }
  56  
  57      public function test_get_set_fraction() {
  58          $step = new question_attempt_step();
  59          $step->set_fraction(0.5);
  60          $this->assertEquals(0.5, $step->get_fraction());
  61      }
  62  
  63      public function test_has_var() {
  64          $step = new question_attempt_step(array('x' => 1, '-y' => 'frog'));
  65          $this->assertTrue($step->has_qt_var('x'));
  66          $this->assertTrue($step->has_behaviour_var('y'));
  67          $this->assertFalse($step->has_qt_var('y'));
  68          $this->assertFalse($step->has_behaviour_var('x'));
  69      }
  70  
  71      public function test_get_var() {
  72          $step = new question_attempt_step(array('x' => 1, '-y' => 'frog'));
  73          $this->assertEquals('1', $step->get_qt_var('x'));
  74          $this->assertEquals('frog', $step->get_behaviour_var('y'));
  75          $this->assertNull($step->get_qt_var('y'));
  76      }
  77  
  78      public function test_set_var() {
  79          $step = new question_attempt_step();
  80          $step->set_qt_var('_x', 1);
  81          $step->set_behaviour_var('_x', 2);
  82          $this->assertEquals('1', $step->get_qt_var('_x'));
  83          $this->assertEquals('2', $step->get_behaviour_var('_x'));
  84      }
  85  
  86      public function test_cannot_set_qt_var_without_underscore() {
  87          $step = new question_attempt_step();
  88          $this->expectException('moodle_exception');
  89          $step->set_qt_var('x', 1);
  90      }
  91  
  92      public function test_cannot_set_behaviour_var_without_underscore() {
  93          $step = new question_attempt_step();
  94          $this->expectException('moodle_exception');
  95          $step->set_behaviour_var('x', 1);
  96      }
  97  
  98      public function test_get_data() {
  99          $step = new question_attempt_step(array('x' => 1, '-y' => 'frog', ':flagged' => 1));
 100          $this->assertEquals(array('x' => '1'), $step->get_qt_data());
 101          $this->assertEquals(array('y' => 'frog'), $step->get_behaviour_data());
 102          $this->assertEquals(array('x' => 1, '-y' => 'frog', ':flagged' => 1), $step->get_all_data());
 103      }
 104  
 105      public function test_get_submitted_data() {
 106          $step = new question_attempt_step(array('x' => 1, '-y' => 'frog'));
 107          $step->set_qt_var('_x', 1);
 108          $step->set_behaviour_var('_x', 2);
 109          $this->assertEquals(array('x' => 1, '-y' => 'frog'), $step->get_submitted_data());
 110      }
 111  
 112      public function test_constructor_default_params() {
 113          global $USER;
 114          $step = new question_attempt_step();
 115          $this->assertEquals(time(), $step->get_timecreated(), 5);
 116          $this->assertEquals($USER->id, $step->get_user_id());
 117          $this->assertEquals(array(), $step->get_qt_data());
 118          $this->assertEquals(array(), $step->get_behaviour_data());
 119  
 120      }
 121  
 122      public function test_constructor_given_params() {
 123          global $USER;
 124          $step = new question_attempt_step(array(), 123, 5);
 125          $this->assertEquals(123, $step->get_timecreated());
 126          $this->assertEquals(5, $step->get_user_id());
 127          $this->assertEquals(array(), $step->get_qt_data());
 128          $this->assertEquals(array(), $step->get_behaviour_data());
 129  
 130      }
 131  }