| 1 | 
           efrain | 
           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 | 
           use question_attempt_step;
  | 
        
        
            | 
            | 
           21 | 
           use question_state;
  | 
        
        
            | 
            | 
           22 | 
           use testable_question_attempt;
  | 
        
        
            | 
            | 
           23 | 
              | 
        
        
            | 
            | 
           24 | 
           defined('MOODLE_INTERNAL') || die();
  | 
        
        
            | 
            | 
           25 | 
              | 
        
        
            | 
            | 
           26 | 
           global $CFG;
  | 
        
        
            | 
            | 
           27 | 
           require_once(__DIR__ . '/../lib.php');
  | 
        
        
            | 
            | 
           28 | 
           require_once(__DIR__ . '/helpers.php');
  | 
        
        
            | 
            | 
           29 | 
              | 
        
        
            | 
            | 
           30 | 
           /**
  | 
        
        
            | 
            | 
           31 | 
            * These tests use a standard fixture of a {@link question_attempt} with three steps.
  | 
        
        
            | 
            | 
           32 | 
            *
  | 
        
        
            | 
            | 
           33 | 
            * Action methods like start, process_action and finish are assumed to be
  | 
        
        
            | 
            | 
           34 | 
            * tested by walkthrough tests in the various behaviours.
  | 
        
        
            | 
            | 
           35 | 
            *
  | 
        
        
            | 
            | 
           36 | 
            * @package    core_question
  | 
        
        
            | 
            | 
           37 | 
            * @category   test
  | 
        
        
            | 
            | 
           38 | 
            * @copyright  2009 The Open University
  | 
        
        
            | 
            | 
           39 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           40 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           41 | 
           final class questionattempt_with_steps_test extends \advanced_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           42 | 
               private $question;
  | 
        
        
            | 
            | 
           43 | 
               private $qa;
  | 
        
        
            | 
            | 
           44 | 
              | 
        
        
            | 
            | 
           45 | 
               protected function setUp(): void {
  | 
        
        
           | 1441 | 
           ariadna | 
           46 | 
                   parent::setUp();
  | 
        
        
           | 1 | 
           efrain | 
           47 | 
                   $this->question = \test_question_maker::make_question('description');
  | 
        
        
            | 
            | 
           48 | 
                   $this->qa = new testable_question_attempt($this->question, 0, null, 2);
  | 
        
        
            | 
            | 
           49 | 
                   for ($i = 0; $i < 3; $i++) {
  | 
        
        
            | 
            | 
           50 | 
                       $step = new question_attempt_step(array('i' => $i));
  | 
        
        
            | 
            | 
           51 | 
                       $this->qa->add_step($step);
  | 
        
        
            | 
            | 
           52 | 
                   }
  | 
        
        
            | 
            | 
           53 | 
               }
  | 
        
        
            | 
            | 
           54 | 
              | 
        
        
            | 
            | 
           55 | 
               protected function tearDown(): void {
  | 
        
        
            | 
            | 
           56 | 
                   $this->qa = null;
  | 
        
        
           | 1441 | 
           ariadna | 
           57 | 
                   parent::tearDown();
  | 
        
        
           | 1 | 
           efrain | 
           58 | 
               }
  | 
        
        
            | 
            | 
           59 | 
              | 
        
        
           | 11 | 
           efrain | 
           60 | 
               public function test_get_step_before_start(): void {
  | 
        
        
           | 1 | 
           efrain | 
           61 | 
                   $this->expectException(\moodle_exception::class);
  | 
        
        
            | 
            | 
           62 | 
                   $step = $this->qa->get_step(-1);
  | 
        
        
            | 
            | 
           63 | 
               }
  | 
        
        
            | 
            | 
           64 | 
              | 
        
        
           | 11 | 
           efrain | 
           65 | 
               public function test_get_step_at_start(): void {
  | 
        
        
           | 1 | 
           efrain | 
           66 | 
                   $step = $this->qa->get_step(0);
  | 
        
        
            | 
            | 
           67 | 
                   $this->assertEquals(0, $step->get_qt_var('i'));
  | 
        
        
            | 
            | 
           68 | 
               }
  | 
        
        
            | 
            | 
           69 | 
              | 
        
        
           | 11 | 
           efrain | 
           70 | 
               public function test_get_step_at_end(): void {
  | 
        
        
           | 1 | 
           efrain | 
           71 | 
                   $step = $this->qa->get_step(2);
  | 
        
        
            | 
            | 
           72 | 
                   $this->assertEquals(2, $step->get_qt_var('i'));
  | 
        
        
            | 
            | 
           73 | 
               }
  | 
        
        
            | 
            | 
           74 | 
              | 
        
        
           | 11 | 
           efrain | 
           75 | 
               public function test_get_step_past_end(): void {
  | 
        
        
           | 1 | 
           efrain | 
           76 | 
                   $this->expectException(\moodle_exception::class);
  | 
        
        
            | 
            | 
           77 | 
                   $step = $this->qa->get_step(3);
  | 
        
        
            | 
            | 
           78 | 
               }
  | 
        
        
            | 
            | 
           79 | 
              | 
        
        
           | 11 | 
           efrain | 
           80 | 
               public function test_get_num_steps(): void {
  | 
        
        
           | 1 | 
           efrain | 
           81 | 
                   $this->assertEquals(3, $this->qa->get_num_steps());
  | 
        
        
            | 
            | 
           82 | 
               }
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
           | 11 | 
           efrain | 
           84 | 
               public function test_get_last_step(): void {
  | 
        
        
           | 1 | 
           efrain | 
           85 | 
                   $step = $this->qa->get_last_step();
  | 
        
        
            | 
            | 
           86 | 
                   $this->assertEquals(2, $step->get_qt_var('i'));
  | 
        
        
            | 
            | 
           87 | 
               }
  | 
        
        
            | 
            | 
           88 | 
              | 
        
        
           | 11 | 
           efrain | 
           89 | 
               public function test_get_last_qt_var_there1(): void {
  | 
        
        
           | 1 | 
           efrain | 
           90 | 
                   $this->assertEquals(2, $this->qa->get_last_qt_var('i'));
  | 
        
        
            | 
            | 
           91 | 
               }
  | 
        
        
            | 
            | 
           92 | 
              | 
        
        
           | 11 | 
           efrain | 
           93 | 
               public function test_get_last_qt_var_there2(): void {
  | 
        
        
           | 1 | 
           efrain | 
           94 | 
                   $this->qa->get_step(0)->set_qt_var('_x', 'a value');
  | 
        
        
            | 
            | 
           95 | 
                   $this->assertEquals('a value', $this->qa->get_last_qt_var('_x'));
  | 
        
        
            | 
            | 
           96 | 
               }
  | 
        
        
            | 
            | 
           97 | 
              | 
        
        
           | 11 | 
           efrain | 
           98 | 
               public function test_get_last_qt_var_missing(): void {
  | 
        
        
           | 1 | 
           efrain | 
           99 | 
                   $this->assertNull($this->qa->get_last_qt_var('notthere'));
  | 
        
        
            | 
            | 
           100 | 
               }
  | 
        
        
            | 
            | 
           101 | 
              | 
        
        
           | 11 | 
           efrain | 
           102 | 
               public function test_get_last_qt_var_missing_default(): void {
  | 
        
        
           | 1 | 
           efrain | 
           103 | 
                   $this->assertEquals('default', $this->qa->get_last_qt_var('notthere', 'default'));
  | 
        
        
            | 
            | 
           104 | 
               }
  | 
        
        
            | 
            | 
           105 | 
              | 
        
        
           | 11 | 
           efrain | 
           106 | 
               public function test_get_last_behaviour_var_missing(): void {
  | 
        
        
           | 1 | 
           efrain | 
           107 | 
                   $this->assertNull($this->qa->get_last_qt_var('notthere'));
  | 
        
        
            | 
            | 
           108 | 
               }
  | 
        
        
            | 
            | 
           109 | 
              | 
        
        
           | 11 | 
           efrain | 
           110 | 
               public function test_get_last_behaviour_var_there(): void {
  | 
        
        
           | 1 | 
           efrain | 
           111 | 
                   $this->qa->get_step(1)->set_behaviour_var('_x', 'a value');
  | 
        
        
            | 
            | 
           112 | 
                   $this->assertEquals('a value', '' . $this->qa->get_last_behaviour_var('_x'));
  | 
        
        
            | 
            | 
           113 | 
               }
  | 
        
        
            | 
            | 
           114 | 
              | 
        
        
           | 11 | 
           efrain | 
           115 | 
               public function test_get_state_gets_state_of_last(): void {
  | 
        
        
           | 1 | 
           efrain | 
           116 | 
                   $this->qa->get_step(2)->set_state(question_state::$gradedright);
  | 
        
        
            | 
            | 
           117 | 
                   $this->qa->get_step(1)->set_state(question_state::$gradedwrong);
  | 
        
        
            | 
            | 
           118 | 
                   $this->assertEquals(question_state::$gradedright, $this->qa->get_state());
  | 
        
        
            | 
            | 
           119 | 
               }
  | 
        
        
            | 
            | 
           120 | 
              | 
        
        
           | 11 | 
           efrain | 
           121 | 
               public function test_get_mark_gets_mark_of_last(): void {
  | 
        
        
           | 1 | 
           efrain | 
           122 | 
                   $this->assertEquals(2, $this->qa->get_max_mark());
  | 
        
        
            | 
            | 
           123 | 
                   $this->qa->get_step(2)->set_fraction(0.5);
  | 
        
        
            | 
            | 
           124 | 
                   $this->qa->get_step(1)->set_fraction(0.1);
  | 
        
        
            | 
            | 
           125 | 
                   $this->assertEquals(1, $this->qa->get_mark());
  | 
        
        
            | 
            | 
           126 | 
               }
  | 
        
        
            | 
            | 
           127 | 
              | 
        
        
           | 11 | 
           efrain | 
           128 | 
               public function test_get_fraction_gets_fraction_of_last(): void {
  | 
        
        
           | 1 | 
           efrain | 
           129 | 
                   $this->qa->get_step(2)->set_fraction(0.5);
  | 
        
        
            | 
            | 
           130 | 
                   $this->qa->get_step(1)->set_fraction(0.1);
  | 
        
        
            | 
            | 
           131 | 
                   $this->assertEquals(0.5, $this->qa->get_fraction());
  | 
        
        
            | 
            | 
           132 | 
               }
  | 
        
        
            | 
            | 
           133 | 
              | 
        
        
           | 11 | 
           efrain | 
           134 | 
               public function test_get_fraction_returns_null_if_none(): void {
  | 
        
        
           | 1 | 
           efrain | 
           135 | 
                   $this->assertNull($this->qa->get_fraction());
  | 
        
        
            | 
            | 
           136 | 
               }
  | 
        
        
            | 
            | 
           137 | 
              | 
        
        
           | 11 | 
           efrain | 
           138 | 
               public function test_format_mark(): void {
  | 
        
        
           | 1 | 
           efrain | 
           139 | 
                   $this->qa->get_step(2)->set_fraction(0.5);
  | 
        
        
            | 
            | 
           140 | 
                   $this->assertEquals('1.00', $this->qa->format_mark(2));
  | 
        
        
            | 
            | 
           141 | 
               }
  | 
        
        
            | 
            | 
           142 | 
              | 
        
        
           | 11 | 
           efrain | 
           143 | 
               public function test_format_max_mark(): void {
  | 
        
        
           | 1 | 
           efrain | 
           144 | 
                   $this->assertEquals('2.0000000', $this->qa->format_max_mark(7));
  | 
        
        
            | 
            | 
           145 | 
               }
  | 
        
        
            | 
            | 
           146 | 
              | 
        
        
           | 11 | 
           efrain | 
           147 | 
               public function test_get_min_fraction(): void {
  | 
        
        
           | 1 | 
           efrain | 
           148 | 
                   $this->qa->set_min_fraction(-1);
  | 
        
        
            | 
            | 
           149 | 
                   $this->assertEquals(-1, $this->qa->get_min_fraction());
  | 
        
        
            | 
            | 
           150 | 
               }
  | 
        
        
            | 
            | 
           151 | 
              | 
        
        
           | 11 | 
           efrain | 
           152 | 
               public function test_cannot_get_min_fraction_before_start(): void {
  | 
        
        
           | 1 | 
           efrain | 
           153 | 
                   $qa = new question_attempt($this->question, 0);
  | 
        
        
            | 
            | 
           154 | 
                   $this->expectException('moodle_exception');
  | 
        
        
            | 
            | 
           155 | 
                   $qa->get_min_fraction();
  | 
        
        
            | 
            | 
           156 | 
               }
  | 
        
        
            | 
            | 
           157 | 
              | 
        
        
           | 11 | 
           efrain | 
           158 | 
               public function test_get_max_fraction(): void {
  | 
        
        
           | 1 | 
           efrain | 
           159 | 
                   $this->qa->set_max_fraction(2);
  | 
        
        
            | 
            | 
           160 | 
                   $this->assertEquals(2, $this->qa->get_max_fraction());
  | 
        
        
            | 
            | 
           161 | 
               }
  | 
        
        
            | 
            | 
           162 | 
              | 
        
        
           | 11 | 
           efrain | 
           163 | 
               public function test_cannot_get_max_fraction_before_start(): void {
  | 
        
        
           | 1 | 
           efrain | 
           164 | 
                   $qa = new question_attempt($this->question, 0);
  | 
        
        
            | 
            | 
           165 | 
                   $this->expectException('moodle_exception');
  | 
        
        
            | 
            | 
           166 | 
                   $qa->get_max_fraction();
  | 
        
        
            | 
            | 
           167 | 
               }
  | 
        
        
            | 
            | 
           168 | 
              | 
        
        
            | 
            | 
           169 | 
               /**
  | 
        
        
            | 
            | 
           170 | 
                * Test cases for {@see test_validate_manual_mark()}.
  | 
        
        
            | 
            | 
           171 | 
                *
  | 
        
        
            | 
            | 
           172 | 
                * @return array test cases
  | 
        
        
            | 
            | 
           173 | 
                */
  | 
        
        
           | 1441 | 
           ariadna | 
           174 | 
               public static function validate_manual_mark_cases(): array {
  | 
        
        
           | 1 | 
           efrain | 
           175 | 
                   // Recall, the DB schema stores question grade information to 7 decimal places.
  | 
        
        
            | 
            | 
           176 | 
                   return [
  | 
        
        
            | 
            | 
           177 | 
                       [0, 1, 2, null, ''],
  | 
        
        
            | 
            | 
           178 | 
                       [0, 1, 2, '', ''],
  | 
        
        
            | 
            | 
           179 | 
                       [0, 1, 2, '0', ''],
  | 
        
        
            | 
            | 
           180 | 
                       [0, 1, 2, '0.0', ''],
  | 
        
        
            | 
            | 
           181 | 
                       [0, 1, 2, '2,0', ''],
  | 
        
        
            | 
            | 
           182 | 
                       [0, 1, 2, 'frog', get_string('manualgradeinvalidformat', 'question')],
  | 
        
        
            | 
            | 
           183 | 
                       [0, 1, 2, '2.1', get_string('manualgradeoutofrange', 'question')],
  | 
        
        
            | 
            | 
           184 | 
                       [0, 1, 2, '-0,01', get_string('manualgradeoutofrange', 'question')],
  | 
        
        
            | 
            | 
           185 | 
                       [-0.3333333, 1, 0.75, '0.75', ''],
  | 
        
        
            | 
            | 
           186 | 
                       [-0.3333333, 1, 0.75, '0.7500001', get_string('manualgradeoutofrange', 'question')],
  | 
        
        
            | 
            | 
           187 | 
                       [-0.3333333, 1, 0.75, '-0.25', ''],
  | 
        
        
            | 
            | 
           188 | 
                       [-0.3333333, 1, 0.75, '-0.2500001', get_string('manualgradeoutofrange', 'question')],
  | 
        
        
            | 
            | 
           189 | 
                   ];
  | 
        
        
            | 
            | 
           190 | 
               }
  | 
        
        
            | 
            | 
           191 | 
              | 
        
        
            | 
            | 
           192 | 
               /**
  | 
        
        
            | 
            | 
           193 | 
                * Test validate_manual_mark.
  | 
        
        
            | 
            | 
           194 | 
                *
  | 
        
        
            | 
            | 
           195 | 
                * @dataProvider validate_manual_mark_cases
  | 
        
        
            | 
            | 
           196 | 
                *
  | 
        
        
            | 
            | 
           197 | 
                * @param float $minfraction minimum fraction for the question being attempted.
  | 
        
        
            | 
            | 
           198 | 
                * @param float $maxfraction maximum fraction for the question being attempted.
  | 
        
        
            | 
            | 
           199 | 
                * @param float $maxmark marks for the question attempt.
  | 
        
        
            | 
            | 
           200 | 
                * @param string|null $currentmark submitted mark.
  | 
        
        
            | 
            | 
           201 | 
                * @param string $expectederror expected error, if any.
  | 
        
        
            | 
            | 
           202 | 
                */
  | 
        
        
            | 
            | 
           203 | 
               public function test_validate_manual_mark(float $minfraction, float $maxfraction,
  | 
        
        
           | 11 | 
           efrain | 
           204 | 
                       float $maxmark, ?string $currentmark, string $expectederror): void {
  | 
        
        
           | 1 | 
           efrain | 
           205 | 
                   $this->qa->set_min_fraction($minfraction);
  | 
        
        
            | 
            | 
           206 | 
                   $this->qa->set_max_fraction($maxfraction);
  | 
        
        
            | 
            | 
           207 | 
                   $this->qa->set_max_mark($maxmark);
  | 
        
        
            | 
            | 
           208 | 
                   $this->assertSame($expectederror, $this->qa->validate_manual_mark($currentmark));
  | 
        
        
            | 
            | 
           209 | 
               }
  | 
        
        
            | 
            | 
           210 | 
           }
  |