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 core_question;
  18  
  19  use question_attempt_step;
  20  use question_state;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  global $CFG;
  25  require_once (__DIR__ . '/../lib.php');
  26  require_once (__DIR__ . '/helpers.php');
  27  
  28  /**
  29   * Unit tests for the {@link question_attempt_step} class.
  30   *
  31   * @package    core_question
  32   * @category   test
  33   * @copyright  2009 The Open University
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class questionattemptstep_test extends \advanced_testcase {
  37      public function test_initial_state_unprocessed() {
  38          $step = new question_attempt_step();
  39          $this->assertEquals(question_state::$unprocessed, $step->get_state());
  40      }
  41  
  42      public function test_get_set_state() {
  43          $step = new question_attempt_step();
  44          $step->set_state(question_state::$gradedright);
  45          $this->assertEquals(question_state::$gradedright, $step->get_state());
  46      }
  47  
  48      public function test_initial_fraction_null() {
  49          $step = new question_attempt_step();
  50          $this->assertNull($step->get_fraction());
  51      }
  52  
  53      public function test_get_set_fraction() {
  54          $step = new question_attempt_step();
  55          $step->set_fraction(0.5);
  56          $this->assertEquals(0.5, $step->get_fraction());
  57      }
  58  
  59      public function test_has_var() {
  60          $step = new question_attempt_step(array('x' => 1, '-y' => 'frog'));
  61          $this->assertTrue($step->has_qt_var('x'));
  62          $this->assertTrue($step->has_behaviour_var('y'));
  63          $this->assertFalse($step->has_qt_var('y'));
  64          $this->assertFalse($step->has_behaviour_var('x'));
  65      }
  66  
  67      public function test_get_var() {
  68          $step = new question_attempt_step(array('x' => 1, '-y' => 'frog'));
  69          $this->assertEquals('1', $step->get_qt_var('x'));
  70          $this->assertEquals('frog', $step->get_behaviour_var('y'));
  71          $this->assertNull($step->get_qt_var('y'));
  72      }
  73  
  74      public function test_set_var() {
  75          $step = new question_attempt_step();
  76          $step->set_qt_var('_x', 1);
  77          $step->set_behaviour_var('_x', 2);
  78          $this->assertEquals('1', $step->get_qt_var('_x'));
  79          $this->assertEquals('2', $step->get_behaviour_var('_x'));
  80      }
  81  
  82      public function test_cannot_set_qt_var_without_underscore() {
  83          $step = new question_attempt_step();
  84          $this->expectException('moodle_exception');
  85          $step->set_qt_var('x', 1);
  86      }
  87  
  88      public function test_cannot_set_behaviour_var_without_underscore() {
  89          $step = new question_attempt_step();
  90          $this->expectException('moodle_exception');
  91          $step->set_behaviour_var('x', 1);
  92      }
  93  
  94      public function test_get_data() {
  95          $step = new question_attempt_step(array('x' => 1, '-y' => 'frog', ':flagged' => 1));
  96          $this->assertEquals(array('x' => '1'), $step->get_qt_data());
  97          $this->assertEquals(array('y' => 'frog'), $step->get_behaviour_data());
  98          $this->assertEquals(array('x' => 1, '-y' => 'frog', ':flagged' => 1), $step->get_all_data());
  99      }
 100  
 101      public function test_get_submitted_data() {
 102          $step = new question_attempt_step(array('x' => 1, '-y' => 'frog'));
 103          $step->set_qt_var('_x', 1);
 104          $step->set_behaviour_var('_x', 2);
 105          $this->assertEquals(array('x' => 1, '-y' => 'frog'), $step->get_submitted_data());
 106      }
 107  
 108      public function test_constructor_default_params() {
 109          global $USER;
 110          $step = new question_attempt_step();
 111          $this->assertEquals(time(), $step->get_timecreated(), 5);
 112          $this->assertEquals($USER->id, $step->get_user_id());
 113          $this->assertEquals(array(), $step->get_qt_data());
 114          $this->assertEquals(array(), $step->get_behaviour_data());
 115  
 116      }
 117  
 118      public function test_constructor_given_params() {
 119          global $USER;
 120          $step = new question_attempt_step(array(), 123, 5);
 121          $this->assertEquals(123, $step->get_timecreated());
 122          $this->assertEquals(5, $step->get_user_id());
 123          $this->assertEquals(array(), $step->get_qt_data());
 124          $this->assertEquals(array(), $step->get_behaviour_data());
 125  
 126      }
 127  
 128      /**
 129       * Test get_user function.
 130       */
 131      public function test_get_user() {
 132          $this->resetAfterTest(true);
 133          $student = $this->getDataGenerator()->create_user();
 134  
 135          $step = new question_attempt_step(array(), 123, $student->id);
 136          $step->add_full_user_object($student);
 137  
 138          $this->assertEquals($student, $step->get_user());
 139      }
 140  
 141      /**
 142       * Test get_user_fullname function.
 143       */
 144      public function test_get_user_fullname() {
 145          $this->resetAfterTest(true);
 146          $student = $this->getDataGenerator()->create_user();
 147  
 148          $step = new question_attempt_step(array(), 123, $student->id);
 149          $step->add_full_user_object($student);
 150  
 151          $this->assertEquals(fullname($student), $step->get_user_fullname());
 152      }
 153  
 154      /**
 155       * Test add_full_user_object function.
 156       */
 157      public function test_add_full_user_object() {
 158          $this->resetAfterTest(true);
 159          $student1 = $this->getDataGenerator()->create_user();
 160          $student2 = $this->getDataGenerator()->create_user();
 161  
 162          $step = new question_attempt_step(array(), 123, $student1->id);
 163  
 164          // Add full user with the valid user.
 165          $step->add_full_user_object($student1);
 166          $this->assertEquals($student1, $step->get_user());
 167  
 168          // Throw exception with the invalid user.
 169          $this->expectException('coding_exception');
 170          $this->expectExceptionMessage('Wrong user passed to add_full_user_object');
 171          $step->add_full_user_object($student2);
 172      }
 173  }