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 mod_quiz;
18
 
19
use core_question\local\bank\question_version_status;
20
use mod_quiz\external\submit_question_version;
21
use mod_quiz\question\bank\qbank_helper;
22
 
23
defined('MOODLE_INTERNAL') || die();
24
 
25
require_once(__DIR__ . '/quiz_question_helper_test_trait.php');
26
 
27
/**
28
 * Qbank helper test for quiz.
29
 *
30
 * @package    mod_quiz
31
 * @category   test
32
 * @copyright  2021 Catalyst IT Australia Pty Ltd
33
 * @author     Safat Shahin <safatshahin@catalyst-au.net>
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 * @coversDefaultClass \mod_quiz\question\bank\qbank_helper
36
 */
1441 ariadna 37
final class qbank_helper_test extends \advanced_testcase {
1 efrain 38
    use \quiz_question_helper_test_trait;
39
 
40
    /**
41
     * @var \stdClass test student user.
42
     */
43
    protected $student;
44
 
45
    /**
46
     * Called before every test.
47
     */
48
    public function setUp(): void {
49
        global $USER;
50
        parent::setUp();
51
        $this->setAdminUser();
52
        $this->course = $this->getDataGenerator()->create_course();
53
        $this->student = $this->getDataGenerator()->create_user();
54
        $this->user = $USER;
55
    }
56
 
57
    /**
58
     * Test reference records.
59
     *
60
     * @covers ::get_version_options
61
     */
11 efrain 62
    public function test_reference_records(): void {
1 efrain 63
        $this->resetAfterTest();
64
 
65
        $quiz = $this->create_test_quiz($this->course);
66
        // Test for questions from a different context.
67
        $context = \context_module::instance($quiz->cmid);
68
 
69
        // Create a couple of questions.
70
        /** @var \core_question_generator $questiongenerator */
71
        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
72
        $cat = $questiongenerator->create_question_category(['contextid' => $context->id]);
73
        $numq = $questiongenerator->create_question('essay', null,
74
            ['category' => $cat->id, 'name' => 'This is the first version']);
75
 
76
        // Create two version.
77
        $questiongenerator->update_question($numq, null, ['name' => 'This is the second version']);
78
        $questiongenerator->update_question($numq, null, ['name' => 'This is the third version']);
79
        quiz_add_quiz_question($numq->id, $quiz);
80
 
81
        // Create the quiz object.
82
        $quizobj = \mod_quiz\quiz_settings::create($quiz->id);
83
        $quizobj->preload_questions();
84
        $quizobj->load_questions();
85
        $questions = $quizobj->get_questions();
86
        $question = reset($questions);
87
        $structure = structure::create_for_quiz($quizobj);
88
        $slots = $structure->get_slots();
89
        $slot = reset($slots);
90
        $this->assertEquals(3, count(qbank_helper::get_version_options($question->id)));
1441 ariadna 91
        $this->assertDebuggingCalled();
1 efrain 92
        $this->assertEquals($question->id, qbank_helper::choose_question_for_redo(
93
                $quiz->id, $context, $slot->id, new \qubaid_list([])));
94
 
95
        // Create another version.
96
        $questiongenerator->update_question($numq, null, ['name' => 'This is the latest version']);
97
 
98
        // Change to always latest.
99
        submit_question_version::execute($slot->id, 0);
100
        $quizobj->preload_questions();
101
        $quizobj->load_questions();
102
        $questions = $quizobj->get_questions();
103
        $question = reset($questions);
104
        $this->assertEquals($question->id, qbank_helper::choose_question_for_redo(
105
                $quiz->id, $context, $slot->id, new \qubaid_list([])));
106
    }
107
 
108
    /**
109
     * Test question structure data.
110
     *
111
     * @covers ::get_question_structure
112
     * @covers ::get_always_latest_version_question_ids
113
     */
11 efrain 114
    public function test_get_question_structure(): void {
1 efrain 115
        $this->resetAfterTest();
116
 
117
        // Create a quiz.
118
        $quiz = $this->create_test_quiz($this->course);
119
        $quizcontext = \context_module::instance(get_coursemodule_from_instance("quiz", $quiz->id, $this->course->id)->id);
120
 
121
        // Create a question in the quiz question bank.
122
        /** @var \core_question_generator $questiongenerator */
123
        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
124
        $cat = $questiongenerator->create_question_category(['contextid' => $quizcontext->id]);
125
        $q = $questiongenerator->create_question('essay', null,
126
            ['category' => $cat->id, 'name' => 'This is the first version']);
127
 
128
        // Edit it to create a second and third version.
129
        $questiongenerator->update_question($q, null, ['name' => 'This is the second version']);
130
        $finalq = $questiongenerator->update_question($q, null, ['name' => 'This is the third version']);
131
 
132
        // Add the question to the quiz.
133
        quiz_add_quiz_question($q->id, $quiz);
134
 
135
        // Load the quiz object and check.
136
        $quizobj = \mod_quiz\quiz_settings::create($quiz->id);
137
        $quizobj->preload_questions();
138
        $quizobj->load_questions();
139
        $questions = $quizobj->get_questions();
140
        $question = reset($questions);
141
        $this->assertEquals($finalq->id, $question->id);
142
 
143
        $structure = structure::create_for_quiz($quizobj);
144
        $slots = $structure->get_slots();
145
        $slot = reset($slots);
146
        $this->assertEquals($finalq->id, $slot->questionid);
147
    }
148
 
149
    /**
150
     * When a question only has draft versions, we should get those and not a dummy question.
151
     *
152
     * @return void
153
     * @covers ::get_question_structure
154
     */
155
    public function test_get_question_structure_with_drafts(): void {
156
        $this->resetAfterTest();
157
 
158
        // Create a quiz.
159
        $quiz = $this->create_test_quiz($this->course);
160
        $quizcontext = \context_module::instance(get_coursemodule_from_instance("quiz", $quiz->id, $this->course->id)->id);
161
 
162
        // Create some questions with drafts in the quiz question bank.
163
        /** @var \core_question_generator $questiongenerator */
164
        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
165
        $cat = $questiongenerator->create_question_category(['contextid' => $quizcontext->id]);
166
        $q1 = $questiongenerator->create_question('essay', null,
167
                ['category' => $cat->id, 'name' => 'This is q1 the first version']);
168
        $q2 = $questiongenerator->create_question('essay', null,
169
                ['category' => $cat->id, 'name' => 'This is q2 the first version',
170
                        'status' => question_version_status::QUESTION_STATUS_DRAFT]);
171
        $q3 = $questiongenerator->create_question('essay', null,
172
                ['category' => $cat->id, 'name' => 'This is q3 the first version',
173
                        'status' => question_version_status::QUESTION_STATUS_DRAFT]);
174
 
175
        // Create a new draft version of a question.
176
        $q1final = $questiongenerator->update_question(clone $q1, null,
177
                ['name' => 'This is q1 the second version', 'status' => question_version_status::QUESTION_STATUS_DRAFT]);
178
        $q3final = $questiongenerator->update_question(clone $q3, null,
179
                ['name' => 'This is q3 the second version', 'status' => question_version_status::QUESTION_STATUS_DRAFT]);
180
 
181
        // Add the questions to the quiz.
182
        quiz_add_quiz_question($q1->id, $quiz);
183
        quiz_add_quiz_question($q2->id, $quiz);
184
        quiz_add_quiz_question($q3->id, $quiz);
185
 
186
        // Load the quiz object and check.
187
        $quizobj = \mod_quiz\quiz_settings::create($quiz->id);
188
        $quizobj->preload_questions();
189
        $quizobj->load_questions();
190
        $questions = $quizobj->get_questions();
191
        $this->assertCount(3, $questions);
192
        // When a question has a Ready version, we should get that and not he draft.
193
        $this->assertTrue(array_key_exists($q1->id, $questions));
194
        $this->assertFalse(array_key_exists($q1final->id, $questions));
195
        $this->assertEquals(question_version_status::QUESTION_STATUS_READY, $questions[$q1->id]->status);
196
        $this->assertEquals('essay', $questions[$q1->id]->qtype);
197
        // When a question only has a draft, we should get that.
198
        $this->assertTrue(array_key_exists($q2->id, $questions));
199
        $this->assertEquals(question_version_status::QUESTION_STATUS_DRAFT, $questions[$q2->id]->status);
200
        $this->assertEquals('essay', $questions[$q2->id]->qtype);
201
        // When a question has several versions but all draft, we should get the latest draft.
202
        $this->assertFalse(array_key_exists($q3->id, $questions));
203
        $this->assertTrue(array_key_exists($q3final->id, $questions));
204
        $this->assertEquals(question_version_status::QUESTION_STATUS_DRAFT, $questions[$q3final->id]->status);
205
        $this->assertEquals('essay', $questions[$q3final->id]->qtype);
206
    }
207
}