Proyectos de Subversion Moodle

Rev

Rev 1 | | 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 qbank_deletequestion;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
23
 
24
/**
25
 * Class containing unit tests for the helper class
26
 *
27
 * @package qbank_deletequestion
28
 * @copyright 2023 The Open University
29
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class helper_test extends \advanced_testcase {
32
 
33
    /**
34
     * @var \context_module module context.
35
     */
36
    protected $context;
37
 
38
    /**
39
     * @var \stdClass course object.
40
     */
41
    protected $course;
42
 
43
    /**
44
     * @var \component_generator_base question generator.
45
     */
46
    protected $qgenerator;
47
 
48
    /**
49
     * @var \stdClass quiz object.
50
     */
51
    protected $quiz;
52
 
53
    /**
54
     * Called before every test.
55
     */
56
    protected function setUp(): void {
57
        parent::setUp();
58
        self::setAdminUser();
59
        $this->resetAfterTest();
60
 
61
        $datagenerator = $this->getDataGenerator();
62
        $this->course = $datagenerator->create_course();
63
        $this->quiz = $datagenerator->create_module('quiz', ['course' => $this->course->id]);
64
        $this->qgenerator = $datagenerator->get_plugin_generator('core_question');
65
        $this->context = \context_module::instance($this->quiz->cmid);
66
    }
67
 
68
    /**
69
     * Test get a confirmation message when deleting the question in the (question bank/history) page.
70
     *
71
     * @covers \qbank_deletequestion\helper::get_delete_confirmation_message
72
     */
73
    public function test_get_delete_confirmation_message(): void {
74
        $qcategory = $this->qgenerator->create_question_category(['contextid' => $this->context->id]);
75
        $question = $this->qgenerator->create_question('shortanswer', null, ['category' => $qcategory->id,
76
            'name' => 'Question 1']);
77
        $questionfirstversionid = $question->id;
78
 
79
        // Verify confirmation title and confirmation message with question not in use in question bank page.
80
        $deleteallversions = true;
81
        [$title1, $message1] = \qbank_deletequestion\helper::get_delete_confirmation_message([$questionfirstversionid],
82
            $deleteallversions);
83
        $this->assertEquals(['confirmtitle' => get_string('deletequestiontitle', 'question')],
84
            $title1);
85
        $this->assertEquals(get_string('deletequestioncheck', 'question',
86
            $question->name). ' v1' . '<br />', $message1);
87
 
88
        // Create a new version and adding it to a quiz.
89
        $question2 = $this->qgenerator->update_question($question, null, ['name' => 'Question 1']);
90
        $questionsecondversionid = $question2->id;
91
 
92
        // Verify confirmation title and confirmation message with question has multiple versions in question bank page.
93
        $listnameofquestionversion2 = $question->name . ' v1' . '<br />' . $question2->name . ' v2' .'<br />';
94
        [$title2, $message2] = \qbank_deletequestion\helper::get_delete_confirmation_message([$questionsecondversionid],
95
            $deleteallversions);
96
        $this->assertEquals(['confirmtitle' => get_string('deletequestiontitle', 'question')],
97
            $title2);
98
        $this->assertEquals(get_string('deletequestioncheck', 'question',
99
            $listnameofquestionversion2), $message2);
100
 
101
        // Verify confirmation title and confirmation message with multiple question selected.
102
        $listnameofquestionversion3 = $question->name . ' v1' . '<br />' . $question2->name . ' v2' .'<br />';
103
        [$title3, $message3] = \qbank_deletequestion\helper::get_delete_confirmation_message([$questionfirstversionid,
104
            $questionsecondversionid], $deleteallversions);
105
        $this->assertEquals(['confirmtitle' => get_string('deletequestiontitle_plural', 'question')],
106
            $title3);
107
        $this->assertEquals(get_string('deletequestionscheck', 'question',
108
            $listnameofquestionversion3), $message3);
109
 
110
        // Add second question version to the quiz to become question in use.
111
        quiz_add_quiz_question($questionsecondversionid, $this->quiz);
112
 
113
        // Verify confirmation message with question in use and has multiple versions in question bank page.
114
        $listnameofquestionversion4 = $question->name . ' v1' . '<br />' . '* ' . $question2->name . ' v2' . '<br />';
115
        $message4 = \qbank_deletequestion\helper::get_delete_confirmation_message([$questionsecondversionid],
116
            $deleteallversions)[1];
117
        $this->assertEquals(get_string('deletequestioncheck', 'question',
118
            $listnameofquestionversion4) . '<br />' . get_string('questionsinuse',
119
                'question'), $message4);
120
 
121
        // Verify confirmation title and confirmation message in history page with one question selected.
122
        $deleteallversions = false;
123
        [$title5, $message5] = \qbank_deletequestion\helper::get_delete_confirmation_message([$questionfirstversionid],
124
            $deleteallversions);
125
        $this->assertEquals(['confirmtitle' => get_string('deleteversiontitle', 'question')],
126
            $title5);
127
        $this->assertEquals(get_string('deleteselectedquestioncheck', 'question',
128
            $question->name) . '<br />', $message5);
129
 
130
        // Verify confirmation title and confirmation message in history page with multiple question selected.
131
        $listnameofquestionversion6 = 'Question 1<br />* Question 1<br />';
132
        [$title6, $message6] = \qbank_deletequestion\helper::get_delete_confirmation_message([$questionfirstversionid,
133
            $questionsecondversionid], $deleteallversions);
134
        $this->assertEquals(['confirmtitle' => get_string('deleteversiontitle_plural', 'question')],
135
            $title6);
136
        $this->assertEquals(get_string('deleteselectedquestioncheck', 'question',
137
            $listnameofquestionversion6) . '<br />'. get_string('questionsinuse', 'question'),
138
                $message6);
139
    }
140
 
141
    /**
142
     * Test delete questions have single/multiple version.
143
     *
144
     * @covers \qbank_deletequestion\helper::delete_questions
145
     */
11 efrain 146
    public function test_delete_question_has_multiple_version(): void {
1 efrain 147
        global $DB;
148
        $qcategory = $this->qgenerator->create_question_category(['contextid' => $this->context->id]);
149
        $question1 = $this->qgenerator->create_question('shortanswer', null, ['category' => $qcategory->id,
150
            'name' => 'Question 1 version 1']);
151
        $question1v1id = $question1->id;
152
        // Create a new version for question 1.
153
        $question1v2 = $this->qgenerator->update_question($question1, null, ['name' => 'Question 1 version 2']);
154
        $question1v2id = $question1v2->id;
155
 
156
        $question2 = $this->qgenerator->create_question('shortanswer', null, ['category' => $qcategory->id,
157
            'name' => 'Question 2 version 1']);
158
        $question2v1id = $question2->id;
159
 
160
        $question3 = $this->qgenerator->create_question('shortanswer', null, ['category' => $qcategory->id,
161
                'name' => 'Question 3 version 1']);
162
        $question3v1id = $question3->id;
163
 
164
        // Do.
165
        \qbank_deletequestion\helper::delete_questions([$question1v2id, $question2v1id], true);
166
 
167
        // All the versions of question1 will be deleted.
168
        $this->assertFalse($DB->record_exists('question', ['id' => $question1v1id]));
169
        $this->assertFalse($DB->record_exists('question', ['id' => $question1v2id]));
170
 
171
        // The question2 have single version will be deleted.
172
        $this->assertFalse($DB->record_exists('question', ['id' => $question2v1id]));
173
 
174
        // Check that we did not delete too much.
175
        $this->assertTrue($DB->record_exists('question', ['id' => $question3v1id]));
176
    }
177
}