Search moodle.org's
Developer Documentation

  • 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 37 and 311] [Versions 38 and 311] [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  /**
      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  
     132  
     133      /**
     134       * Test get_user function.
     135       */
     136      public function test_get_user() {
     137          $this->resetAfterTest(true);
     138          $student = $this->getDataGenerator()->create_user();
     139  
     140          $step = new question_attempt_step(array(), 123, $student->id);
     141          $step->add_full_user_object($student);
     142  
     143          $this->assertEquals($student, $step->get_user());
     144      }
     145  
     146      /**
     147       * Test get_user_fullname function.
     148       */
     149      public function test_get_user_fullname() {
     150          $this->resetAfterTest(true);
     151          $student = $this->getDataGenerator()->create_user();
     152  
     153          $step = new question_attempt_step(array(), 123, $student->id);
     154          $step->add_full_user_object($student);
     155  
     156          $this->assertEquals(fullname($student), $step->get_user_fullname());
     157      }
     158  
     159      /**
     160       * Test add_full_user_object function.
     161       */
     162      public function test_add_full_user_object() {
     163          $this->resetAfterTest(true);
     164          $student1 = $this->getDataGenerator()->create_user();
     165          $student2 = $this->getDataGenerator()->create_user();
     166  
     167          $step = new question_attempt_step(array(), 123, $student1->id);
     168  
     169          // Add full user with the valid user.
     170          $step->add_full_user_object($student1);
     171          $this->assertEquals($student1, $step->get_user());
     172  
     173          // Throw exception with the invalid user.
     174          $this->expectException('coding_exception');
     175          $this->expectExceptionMessage('Wrong user passed to add_full_user_object');
     176          $step->add_full_user_object($student2);
     177      }
     178  }