Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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 mod_quiz\output;
18
 
19
use advanced_testcase;
20
 
21
/**
22
 * Tests for {@see attempt_summary_information}.
23
 *
24
 * @package   mod_quiz
25
 * @copyright The Open University
26
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 * @covers    \mod_quiz\output\attempt_summary_information
28
 */
29
final class attempt_summary_information_test extends advanced_testcase {
30
 
31
    public function test_add_item(): void {
32
        global $PAGE;
33
 
34
        $summary = new attempt_summary_information();
35
        $summary->add_item('test', 'Test name', 'Test value');
36
 
37
        $data = $summary->export_for_template($PAGE->get_renderer('mod_quiz'));
38
        $this->assertEquals([(object) ['title' => 'Test name', 'content' => 'Test value']], $data['items']);
39
    }
40
 
41
    public function test_add_item_before_start(): void {
42
        global $PAGE;
43
 
44
        $summary = new attempt_summary_information();
45
        $summary->add_item('test', 'Test name', 'Test value');
46
 
47
        $summary->add_item_before('newitem', 'New name', 'New value', 'test');
48
 
49
        $data = $summary->export_for_template($PAGE->get_renderer('mod_quiz'));
50
        $this->assertEquals(
51
            [
52
                (object) ['title' => 'New name', 'content' => 'New value'],
53
                (object) ['title' => 'Test name', 'content' => 'Test value'],
54
            ],
55
            $data['items'],
56
        );
57
    }
58
 
59
    public function test_add_item_before_middle(): void {
60
        global $PAGE;
61
 
62
        $summary = new attempt_summary_information();
63
        $summary->add_item('item1', 'Existing 1', 'One');
64
        $summary->add_item('item2', 'Existing 2', 'Two');
65
 
66
        $summary->add_item_before('newitem', 'New name', 'New value', 'item2');
67
 
68
        $data = $summary->export_for_template($PAGE->get_renderer('mod_quiz'));
69
        $this->assertEquals(
70
            [
71
                (object) ['title' => 'Existing 1', 'content' => 'One'],
72
                (object) ['title' => 'New name', 'content' => 'New value'],
73
                (object) ['title' => 'Existing 2', 'content' => 'Two'],
74
            ],
75
            $data['items'],
76
        );
77
    }
78
 
79
    public function test_add_item_before_no_match(): void {
80
        global $PAGE;
81
 
82
        $summary = new attempt_summary_information();
83
        $summary->add_item('test', 'Test name', 'Test value');
84
 
85
        $summary->add_item_before('newitem', 'New name', 'New value', 'unknown');
86
 
87
        $data = $summary->export_for_template($PAGE->get_renderer('mod_quiz'));
88
        $this->assertEquals(
89
            [
90
                (object) ['title' => 'New name', 'content' => 'New value'],
91
                (object) ['title' => 'Test name', 'content' => 'Test value'],
92
            ],
93
            $data['items'],
94
        );
95
    }
96
 
97
    public function test_add_item_before_empty(): void {
98
        global $PAGE;
99
 
100
        $summary = new attempt_summary_information();
101
 
102
        $summary->add_item_before('newitem', 'New name', 'New value', 'unknown');
103
 
104
        $data = $summary->export_for_template($PAGE->get_renderer('mod_quiz'));
105
        $this->assertEquals([(object) ['title' => 'New name', 'content' => 'New value']], $data['items']);
106
    }
107
 
108
    public function test_add_item_after_end(): void {
109
        global $PAGE;
110
 
111
        $summary = new attempt_summary_information();
112
        $summary->add_item('test', 'Test name', 'Test value');
113
 
114
        $summary->add_item_after('newitem', 'New name', 'New value', 'test');
115
 
116
        $data = $summary->export_for_template($PAGE->get_renderer('mod_quiz'));
117
        $this->assertEquals(
118
            [
119
                (object) ['title' => 'Test name', 'content' => 'Test value'],
120
                (object) ['title' => 'New name', 'content' => 'New value'],
121
            ],
122
            $data['items'],
123
        );
124
    }
125
 
126
    public function test_add_item_after_middle(): void {
127
        global $PAGE;
128
 
129
        $summary = new attempt_summary_information();
130
        $summary->add_item('item1', 'Existing 1', 'One');
131
        $summary->add_item('item2', 'Existing 2', 'Two');
132
 
133
        $summary->add_item_after('newitem', 'New name', 'New value', 'item1');
134
 
135
        $data = $summary->export_for_template($PAGE->get_renderer('mod_quiz'));
136
        $this->assertEquals(
137
            [
138
                (object) ['title' => 'Existing 1', 'content' => 'One'],
139
                (object) ['title' => 'New name', 'content' => 'New value'],
140
                (object) ['title' => 'Existing 2', 'content' => 'Two'],
141
            ],
142
            $data['items'],
143
        );
144
    }
145
 
146
    public function test_add_item_after_no_match(): void {
147
        global $PAGE;
148
 
149
        $summary = new attempt_summary_information();
150
        $summary->add_item('test', 'Test name', 'Test value');
151
 
152
        $summary->add_item_after('newitem', 'New name', 'New value', 'unknown');
153
 
154
        $data = $summary->export_for_template($PAGE->get_renderer('mod_quiz'));
155
        $this->assertEquals(
156
            [
157
                (object) ['title' => 'Test name', 'content' => 'Test value'],
158
                (object) ['title' => 'New name', 'content' => 'New value'],
159
            ],
160
            $data['items'],
161
        );
162
    }
163
 
164
    public function test_add_item_after_empty(): void {
165
        global $PAGE;
166
 
167
        $summary = new attempt_summary_information();
168
 
169
        $summary->add_item_after('newitem', 'New name', 'New value', 'unknown');
170
 
171
        $data = $summary->export_for_template($PAGE->get_renderer('mod_quiz'));
172
        $this->assertEquals([(object) ['title' => 'New name', 'content' => 'New value']], $data['items']);
173
    }
174
 
175
    public function test_remove_item(): void {
176
        global $PAGE;
177
 
178
        $summary = new attempt_summary_information();
179
        $summary->add_item('item1', 'Existing 1', 'One');
180
        $summary->add_item('item2', 'Existing 2', 'Two');
181
 
182
        $summary->remove_item('item1');
183
 
184
        $data = $summary->export_for_template($PAGE->get_renderer('mod_quiz'));
185
        $this->assertEquals([(object) ['title' => 'Existing 2', 'content' => 'Two']], $data['items']);
186
    }
187
 
188
    public function test_remove_item_not_present(): void {
189
        global $PAGE;
190
 
191
        $summary = new attempt_summary_information();
192
        $summary->add_item('item1', 'Existing 1', 'One');
193
        $summary->add_item('item2', 'Existing 2', 'Two');
194
 
195
        $summary->remove_item('item3');
196
 
197
        $data = $summary->export_for_template($PAGE->get_renderer('mod_quiz'));
198
        $this->assertEquals(
199
            [
200
                (object) ['title' => 'Existing 1', 'content' => 'One'],
201
                (object) ['title' => 'Existing 2', 'content' => 'Two'],
202
            ],
203
            $data['items'],
204
        );
205
    }
206
}