Merge branch 'install_310_STABLE' of https://git.in.moodle.com/amosbot/moodle-install...
[moodle.git] / question / engine / tests / questionattemptiterator_test.php
CommitLineData
603bd001
PS
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_iterator 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
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
1fcf0ca8
RS
30require_once(__DIR__ . '/../lib.php');
31require_once(__DIR__ . '/helpers.php');
603bd001
PS
32
33
34/**
35 * This file contains tests for the {@link question_attempt_iterator} class.
36 *
37 * @copyright 2009 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class question_attempt_iterator_test extends advanced_testcase {
41 private $quba;
42 private $qas = array();
43 private $iterator;
44
d81a9480 45 protected function setUp(): void {
603bd001 46 $this->quba = question_engine::make_questions_usage_by_activity('unit_test',
21c08c63 47 context_system::instance());
603bd001
PS
48 $this->quba->set_preferred_behaviour('deferredfeedback');
49
50 $slot = $this->quba->add_question(test_question_maker::make_question('description'));
51 $this->qas[$slot] = $this->quba->get_question_attempt($slot);
52
53 $slot = $this->quba->add_question(test_question_maker::make_question('description'));
54 $this->qas[$slot] = $this->quba->get_question_attempt($slot);
55
56 $this->iterator = $this->quba->get_attempt_iterator();
57 }
58
d81a9480 59 protected function tearDown(): void {
603bd001
PS
60 $this->quba = null;
61 $this->iterator = null;
62 }
63
64 public function test_foreach_loop() {
65 $i = 1;
66 foreach ($this->iterator as $key => $qa) {
67 $this->assertEquals($i, $key);
68 $this->assertSame($this->qas[$i], $qa);
69 $i++;
70 }
71 $this->assertEquals(3, $i);
72 }
73
74 public function test_offsetExists_before_start() {
75 $this->assertFalse(isset($this->iterator[0]));
76 }
77
78 public function test_offsetExists_at_start() {
79 $this->assertTrue(isset($this->iterator[1]));
80 }
81
82 public function test_offsetExists_at_endt() {
83 $this->assertTrue(isset($this->iterator[2]));
84 }
85
86 public function test_offsetExists_past_end() {
87 $this->assertFalse(isset($this->iterator[3]));
88 }
89
90 public function test_offsetGet_before_start() {
ddf72dd6 91 $this->expectException(moodle_exception::class);
603bd001
PS
92 $step = $this->iterator[0];
93 }
94
95 public function test_offsetGet_at_start() {
96 $this->assertSame($this->qas[1], $this->iterator[1]);
97 }
98
99 public function test_offsetGet_at_end() {
100 $this->assertSame($this->qas[2], $this->iterator[2]);
101 }
102
103 public function test_offsetGet_past_end() {
ddf72dd6 104 $this->expectException(moodle_exception::class);
603bd001
PS
105 $step = $this->iterator[3];
106 }
107
108 public function test_cannot_set() {
ddf72dd6 109 $this->expectException(moodle_exception::class);
603bd001
PS
110 $this->iterator[0] = null;
111 }
112
113 public function test_cannot_unset() {
ddf72dd6 114 $this->expectException(moodle_exception::class);
603bd001
PS
115 unset($this->iterator[2]);
116 }
d81a9480 117}