Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

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