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 core_question\local\bank;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Unit tests for the version_options class and associated methods.
|
|
|
21 |
*
|
|
|
22 |
* @package core_question
|
|
|
23 |
* @copyright 2024 onwards Catalyst IT {@link http://www.catalyst-eu.net/}
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
* @author Conn Warwicker <conn.warwicker@catalyst-eu.net>
|
|
|
26 |
* @covers \core_question\local\bank\version_options
|
|
|
27 |
*/
|
|
|
28 |
final class version_options_test extends \advanced_testcase {
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Tests the retrieval and correctness of version selection menu options for a given question.
|
|
|
33 |
*
|
|
|
34 |
* This method verifies that the correct version options are available for a specific question
|
|
|
35 |
* within a course, ensuring that the options are accurately labeled and ordered. It includes
|
|
|
36 |
* checks for version labeling, the presence of an "Always latest" option, and the correct count
|
|
|
37 |
* and sequence of version numbers after creating multiple versions of a question.
|
|
|
38 |
*
|
|
|
39 |
* @return void
|
|
|
40 |
*/
|
|
|
41 |
public function test_get_version_options(): void {
|
|
|
42 |
|
|
|
43 |
// Reset everything after this test completes.
|
|
|
44 |
$this->resetAfterTest();
|
|
|
45 |
|
|
|
46 |
// Load the generators we need.
|
|
|
47 |
$questiongenerator = self::getDataGenerator()->get_plugin_generator('core_question');
|
|
|
48 |
|
|
|
49 |
// Create a test course we can use the question bank on.
|
|
|
50 |
$category = self::getDataGenerator()->create_category();
|
|
|
51 |
$course = self::getDataGenerator()->create_course(['category' => $category->id]);
|
|
|
52 |
$coursecontext = \core\context\course::instance($course->id);
|
|
|
53 |
|
|
|
54 |
// Create a question category on the course.
|
|
|
55 |
$cat = $questiongenerator->create_question_category(['contextid' => $coursecontext->id]);
|
|
|
56 |
|
|
|
57 |
// Create a question within that category.
|
|
|
58 |
$question = $questiongenerator->create_question('essay', null,
|
|
|
59 |
['category' => $cat->id, 'name' => 'This is the first version']);
|
|
|
60 |
|
|
|
61 |
// Get the select menu options for the question versions.
|
|
|
62 |
$options = version_options::get_version_menu_options($question->id);
|
|
|
63 |
|
|
|
64 |
// Assert that there are 2 and that they "Always latest" is first.
|
|
|
65 |
$this->assertCount(2, $options);
|
|
|
66 |
$this->assertEquals('Always latest', $options[0]);
|
|
|
67 |
$this->assertEquals('v1 (latest)', $options[1]);
|
|
|
68 |
|
|
|
69 |
// Create some new versions of the question.
|
|
|
70 |
$questiongenerator->update_question($question, null, ['name' => 'This is the second version']);
|
|
|
71 |
$questiongenerator->update_question($question, null, ['name' => 'This is the third version']);
|
|
|
72 |
|
|
|
73 |
// Get the options which would be used for the select menu.
|
|
|
74 |
$options = version_options::get_version_menu_options($question->id);
|
|
|
75 |
|
|
|
76 |
// Assert that there are now 4.
|
|
|
77 |
$this->assertCount(4, $options);
|
|
|
78 |
|
|
|
79 |
// Assert that they are keyed to their verison numbers.
|
|
|
80 |
$this->assertEquals('Always latest', $options[0]);
|
|
|
81 |
$this->assertEquals('v1', $options[1]);
|
|
|
82 |
$this->assertEquals('v2', $options[2]);
|
|
|
83 |
$this->assertEquals('v3 (latest)', $options[3]);
|
|
|
84 |
|
|
|
85 |
// Assert that they are in the correct order of "Always latest" first, then descending versions.
|
|
|
86 |
$this->assertEquals(array_keys($options), [0, 3, 2, 1]);
|
|
|
87 |
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
}
|