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
/**
18
 * Unit tests for the {@link \mod_quiz\repaginate} class.
19
 * @package   mod_quiz
20
 * @category  test
21
 * @copyright 2014 The Open Univsersity
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace mod_quiz;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
global $CFG;
30
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
31
require_once($CFG->dirroot . '/mod/quiz/classes/repaginate.php');
32
 
33
/**
34
 * Test for {@see \mod_quiz\repaginate}
35
 * @copyright 2014 The Open Univsersity
36
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class mod_quiz_repaginate_testable extends repaginate {
39
 
40
    public function __construct($quizid = 0, $slots = null) {
41
        return parent::__construct($quizid, $slots);
42
    }
43
    public function get_this_slot($slots, $slotnumber) {
44
        return parent::get_this_slot($slots, $slotnumber);
45
    }
46
    public function get_slots_by_slotid($slots = null) {
47
        return parent::get_slots_by_slotid($slots);
48
    }
49
    public function get_slots_by_slot_number($slots = null) {
50
        return parent::get_slots_by_slot_number($slots);
51
    }
52
    public function repaginate_this_slot($slot, $newpagenumber) {
53
        return parent::repaginate_this_slot($slot, $newpagenumber);
54
    }
55
    public function repaginate_next_slot($nextslotnumber, $type) {
56
        return parent::repaginate_next_slot($nextslotnumber, $type);
57
    }
58
}
59
 
60
/**
61
 * Test for some parts of the repaginate class.
62
 * @copyright 2014 The Open University
63
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
64
 */
1441 ariadna 65
final class repaginate_test extends \advanced_testcase {
1 efrain 66
 
67
    /** @var array stores the slots. */
68
    private $quizslots;
69
    /** @var mod_quiz_repaginate_testable the object being tested. */
70
    private $repaginate = null;
71
 
72
    public function setUp(): void {
1441 ariadna 73
        parent::setUp();
1 efrain 74
        $this->set_quiz_slots($this->get_quiz_object()->get_slots());
75
        $this->repaginate = new mod_quiz_repaginate_testable(0, $this->quizslots);
76
    }
77
 
78
    /**
79
     * Create a quiz, add five questions to the quiz
80
     * which are all on one page and return the quiz object.
81
     */
82
    private function get_quiz_object() {
83
        global $SITE;
84
        $this->resetAfterTest(true);
85
 
86
        // Make a quiz.
87
        $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
88
 
89
        $quiz = $quizgenerator->create_instance([
90
                'course' => $SITE->id, 'questionsperpage' => 0, 'grade' => 100.0, 'sumgrades' => 2]);
91
        $cm = get_coursemodule_from_instance('quiz', $quiz->id, $SITE->id);
92
 
93
        // Create five questions.
94
        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
95
        $cat = $questiongenerator->create_question_category();
96
 
97
        $shortanswer = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
98
        $numerical = $questiongenerator->create_question('numerical', null, ['category' => $cat->id]);
99
        $essay = $questiongenerator->create_question('essay', null, ['category' => $cat->id]);
100
        $truefalse = $questiongenerator->create_question('truefalse', null, ['category' => $cat->id]);
101
        $match = $questiongenerator->create_question('match', null, ['category' => $cat->id]);
102
 
103
        // Add them to the quiz.
104
        quiz_add_quiz_question($shortanswer->id, $quiz);
105
        quiz_add_quiz_question($numerical->id, $quiz);
106
        quiz_add_quiz_question($essay->id, $quiz);
107
        quiz_add_quiz_question($truefalse->id, $quiz);
108
        quiz_add_quiz_question($match->id, $quiz);
109
 
110
        // Return the quiz object.
111
        $quizobj = new quiz_settings($quiz, $cm, $SITE);
112
        return structure::create_for_quiz($quizobj);
113
    }
114
 
115
    /**
116
     * Set the quiz slots
117
     * @param string $slots
118
     */
119
    private function set_quiz_slots($slots = null) {
120
        if (!$slots) {
121
            $this->quizslots = $this->get_quiz_object()->get_slots();
122
        } else {
123
            $this->quizslots = $slots;
124
        }
125
    }
126
 
127
    /**
128
     * Test the get_this_slot() method
129
     */
11 efrain 130
    public function test_get_this_slot(): void {
1 efrain 131
        $this->set_quiz_slots();
132
        $actual = [];
133
        $expected = $this->repaginate->get_slots_by_slot_number();
134
        $this->assertEquals($expected, $actual);
135
 
136
        $slotsbyno = $this->repaginate->get_slots_by_slot_number($this->quizslots);
137
        $slotnumber = 5;
138
        $thisslot = $this->repaginate->get_this_slot($this->quizslots, $slotnumber);
139
        $this->assertEquals($slotsbyno[$slotnumber], $thisslot);
140
    }
141
 
11 efrain 142
    public function test_get_slots_by_slotnumber(): void {
1 efrain 143
        $this->set_quiz_slots();
144
        $expected = [];
145
        $actual = $this->repaginate->get_slots_by_slot_number();
146
        $this->assertEquals($expected, $actual);
147
 
148
        foreach ($this->quizslots as $slot) {
149
            $expected[$slot->slot] = $slot;
150
        }
151
        $actual = $this->repaginate->get_slots_by_slot_number($this->quizslots);
152
        $this->assertEquals($expected, $actual);
153
    }
154
 
11 efrain 155
    public function test_get_slots_by_slotid(): void {
1 efrain 156
        $this->set_quiz_slots();
157
        $actual = $this->repaginate->get_slots_by_slotid();
158
        $this->assertEquals([], $actual);
159
 
160
        $slotsbyno = $this->repaginate->get_slots_by_slot_number($this->quizslots);
161
        $actual = $this->repaginate->get_slots_by_slotid($slotsbyno);
162
        $this->assertEquals($this->quizslots, $actual);
163
    }
164
 
11 efrain 165
    public function test_repaginate_n_questions_per_page(): void {
1 efrain 166
        $this->set_quiz_slots();
167
 
168
        // Expect 2 questions per page.
169
        $expected = [];
170
        foreach ($this->quizslots as $slot) {
171
            // Page 1 contains Slots 1 and 2.
172
            if ($slot->slot >= 1 && $slot->slot <= 2) {
173
                $slot->page = 1;
174
            }
175
            // Page 2 contains slots 3 and 4.
176
            if ($slot->slot >= 3 && $slot->slot <= 4) {
177
                $slot->page = 2;
178
            }
179
            // Page 3 contains slots 5.
180
            if ($slot->slot >= 5 && $slot->slot <= 6) {
181
                $slot->page = 3;
182
            }
183
            $expected[$slot->id] = $slot;
184
        }
185
        $actual = $this->repaginate->repaginate_n_question_per_page($this->quizslots, 2);
186
        $this->assertEquals($expected, $actual);
187
 
188
        // Expect 3 questions per page.
189
        $expected = [];
190
        foreach ($this->quizslots as $slot) {
191
            // Page 1 contains Slots 1, 2 and 3.
192
            if ($slot->slot >= 1 && $slot->slot <= 3) {
193
                $slot->page = 1;
194
            }
195
            // Page 2 contains slots 4 and 5.
196
            if ($slot->slot >= 4 && $slot->slot <= 6) {
197
                $slot->page = 2;
198
            }
199
            $expected[$slot->id] = $slot;
200
        }
201
        $actual = $this->repaginate->repaginate_n_question_per_page($this->quizslots, 3);
202
        $this->assertEquals($expected, $actual);
203
 
204
        // Expect 5 questions per page.
205
        $expected = [];
206
        foreach ($this->quizslots as $slot) {
207
            // Page 1 contains Slots 1, 2, 3, 4 and 5.
208
            if ($slot->slot > 0 && $slot->slot < 6) {
209
                $slot->page = 1;
210
            }
211
            // Page 2 contains slots 6, 7, 8, 9 and 10.
212
            if ($slot->slot > 5 && $slot->slot < 11) {
213
                $slot->page = 2;
214
            }
215
            $expected[$slot->id] = $slot;
216
        }
217
        $actual = $this->repaginate->repaginate_n_question_per_page($this->quizslots, 5);
218
        $this->assertEquals($expected, $actual);
219
 
220
        // Expect 10 questions per page.
221
        $expected = [];
222
        foreach ($this->quizslots as $slot) {
223
            // Page 1 contains Slots 1 to 10.
224
            if ($slot->slot >= 1 && $slot->slot <= 10) {
225
                $slot->page = 1;
226
            }
227
            // Page 2 contains slots 11 to 20.
228
            if ($slot->slot >= 11 && $slot->slot <= 20) {
229
                $slot->page = 2;
230
            }
231
            $expected[$slot->id] = $slot;
232
        }
233
        $actual = $this->repaginate->repaginate_n_question_per_page($this->quizslots, 10);
234
        $this->assertEquals($expected, $actual);
235
 
236
        // Expect 1 questions per page.
237
        $expected = [];
238
        $page = 1;
239
        foreach ($this->quizslots as $slot) {
240
            $slot->page = $page++;
241
            $expected[$slot->id] = $slot;
242
        }
243
        $actual = $this->repaginate->repaginate_n_question_per_page($this->quizslots, 1);
244
        $this->assertEquals($expected, $actual);
245
    }
246
 
11 efrain 247
    public function test_repaginate_this_slot(): void {
1 efrain 248
        $this->set_quiz_slots();
249
        $slotsbyslotno = $this->repaginate->get_slots_by_slot_number($this->quizslots);
250
        $slotnumber = 3;
251
        $newpagenumber = 2;
252
        $thisslot = $slotsbyslotno[3];
253
        $thisslot->page = $newpagenumber;
254
        $expected = $thisslot;
255
        $actual = $this->repaginate->repaginate_this_slot($slotsbyslotno[3], $newpagenumber);
256
        $this->assertEquals($expected, $actual);
257
    }
258
 
11 efrain 259
    public function test_repaginate_the_rest(): void {
1 efrain 260
        $this->set_quiz_slots();
261
        $slotfrom = 1;
262
        $type = repaginate::LINK;
263
        $expected = [];
264
        foreach ($this->quizslots as $slot) {
265
            if ($slot->slot > $slotfrom) {
266
                $slot->page = $slot->page - 1;
267
                $expected[$slot->id] = $slot;
268
            }
269
        }
270
        $actual = $this->repaginate->repaginate_the_rest($this->quizslots, $slotfrom, $type, false);
271
        $this->assertEquals($expected, $actual);
272
 
273
        $slotfrom = 2;
274
        $newslots = [];
275
        foreach ($this->quizslots as $s) {
276
            if ($s->slot === $slotfrom) {
277
                $s->page = $s->page - 1;
278
            }
279
            $newslots[$s->id] = $s;
280
        }
281
 
282
        $type = repaginate::UNLINK;
283
        $expected = [];
284
        foreach ($this->quizslots as $slot) {
285
            if ($slot->slot > ($slotfrom - 1)) {
286
                $slot->page = $slot->page - 1;
287
                $expected[$slot->id] = $slot;
288
            }
289
        }
290
        $actual = $this->repaginate->repaginate_the_rest($newslots, $slotfrom, $type, false);
291
        $this->assertEquals($expected, $actual);
292
    }
293
 
294
}