| 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_step;
|
|
|
20 |
use testable_question_attempt;
|
|
|
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_iterator} 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 |
*/
|
| 1441 |
ariadna |
36 |
final class questionattemptstepiterator_test extends \advanced_testcase {
|
| 1 |
efrain |
37 |
private $qa;
|
|
|
38 |
private $iterator;
|
|
|
39 |
|
|
|
40 |
protected function setUp(): void {
|
| 1441 |
ariadna |
41 |
parent::setUp();
|
| 1 |
efrain |
42 |
$question = \test_question_maker::make_question('description');
|
|
|
43 |
$this->qa = new testable_question_attempt($question, 0);
|
|
|
44 |
for ($i = 0; $i < 3; $i++) {
|
|
|
45 |
$step = new question_attempt_step(array('i' => $i));
|
|
|
46 |
$this->qa->add_step($step);
|
|
|
47 |
}
|
|
|
48 |
$this->iterator = $this->qa->get_step_iterator();
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
protected function tearDown(): void {
|
|
|
52 |
$this->qa = null;
|
|
|
53 |
$this->iterator = null;
|
| 1441 |
ariadna |
54 |
parent::tearDown();
|
| 1 |
efrain |
55 |
}
|
|
|
56 |
|
| 11 |
efrain |
57 |
public function test_foreach_loop(): void {
|
| 1 |
efrain |
58 |
$i = 0;
|
|
|
59 |
foreach ($this->iterator as $key => $step) {
|
|
|
60 |
$this->assertEquals($i, $key);
|
|
|
61 |
$this->assertEquals($i, $step->get_qt_var('i'));
|
|
|
62 |
$i++;
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
|
| 11 |
efrain |
66 |
public function test_foreach_loop_add_step_during(): void {
|
| 1 |
efrain |
67 |
$i = 0;
|
|
|
68 |
foreach ($this->iterator as $key => $step) {
|
|
|
69 |
$this->assertEquals($i, $key);
|
|
|
70 |
$this->assertEquals($i, $step->get_qt_var('i'));
|
|
|
71 |
$i++;
|
|
|
72 |
if ($i == 2) {
|
|
|
73 |
$step = new question_attempt_step(array('i' => 3));
|
|
|
74 |
$this->qa->add_step($step);
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
$this->assertEquals(4, $i);
|
|
|
78 |
}
|
|
|
79 |
|
| 11 |
efrain |
80 |
public function test_reverse_foreach_loop(): void {
|
| 1 |
efrain |
81 |
$i = 2;
|
|
|
82 |
foreach ($this->qa->get_reverse_step_iterator() as $key => $step) {
|
|
|
83 |
$this->assertEquals($i, $key);
|
|
|
84 |
$this->assertEquals($i, $step->get_qt_var('i'));
|
|
|
85 |
$i--;
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
|
| 11 |
efrain |
89 |
public function test_offsetExists_before_start(): void {
|
| 1 |
efrain |
90 |
$this->assertFalse(isset($this->iterator[-1]));
|
|
|
91 |
}
|
|
|
92 |
|
| 11 |
efrain |
93 |
public function test_offsetExists_at_start(): void {
|
| 1 |
efrain |
94 |
$this->assertTrue(isset($this->iterator[0]));
|
|
|
95 |
}
|
|
|
96 |
|
| 11 |
efrain |
97 |
public function test_offsetExists_at_endt(): void {
|
| 1 |
efrain |
98 |
$this->assertTrue(isset($this->iterator[2]));
|
|
|
99 |
}
|
|
|
100 |
|
| 11 |
efrain |
101 |
public function test_offsetExists_past_end(): void {
|
| 1 |
efrain |
102 |
$this->assertFalse(isset($this->iterator[3]));
|
|
|
103 |
}
|
|
|
104 |
|
| 11 |
efrain |
105 |
public function test_offsetGet_before_start(): void {
|
| 1 |
efrain |
106 |
$this->expectException(\moodle_exception::class);
|
|
|
107 |
$step = $this->iterator[-1];
|
|
|
108 |
}
|
|
|
109 |
|
| 11 |
efrain |
110 |
public function test_offsetGet_at_start(): void {
|
| 1 |
efrain |
111 |
$step = $this->iterator[0];
|
|
|
112 |
$this->assertEquals(0, $step->get_qt_var('i'));
|
|
|
113 |
}
|
|
|
114 |
|
| 11 |
efrain |
115 |
public function test_offsetGet_at_end(): void {
|
| 1 |
efrain |
116 |
$step = $this->iterator[2];
|
|
|
117 |
$this->assertEquals(2, $step->get_qt_var('i'));
|
|
|
118 |
}
|
|
|
119 |
|
| 11 |
efrain |
120 |
public function test_offsetGet_past_end(): void {
|
| 1 |
efrain |
121 |
$this->expectException(\moodle_exception::class);
|
|
|
122 |
$step = $this->iterator[3];
|
|
|
123 |
}
|
|
|
124 |
|
| 11 |
efrain |
125 |
public function test_cannot_set(): void {
|
| 1 |
efrain |
126 |
$this->expectException(\moodle_exception::class);
|
|
|
127 |
$this->iterator[0] = null;
|
|
|
128 |
}
|
|
|
129 |
|
| 11 |
efrain |
130 |
public function test_cannot_unset(): void {
|
| 1 |
efrain |
131 |
$this->expectException(\moodle_exception::class);
|
|
|
132 |
unset($this->iterator[2]);
|
|
|
133 |
}
|
|
|
134 |
}
|