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 qbank_bulkmove;
18
 
19
use core_question\local\bank\question_edit_contexts;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
global $CFG;
24
require_once($CFG->dirroot . '/question/editlib.php');
25
 
26
/**
27
 * Bulk move helper tests.
28
 *
29
 * @package    qbank_bulkmove
30
 * @copyright  2021 Catalyst IT Australia Pty Ltd
31
 * @author     Safat Shahin <safatshahin@catalyst-au.net>
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 * @coversDefaultClass \qbank_bulkmove\helper
34
 */
1441 ariadna 35
final class helper_test extends \advanced_testcase {
1 efrain 36
 
37
    /**
38
     * @var false|object|\stdClass|null $cat
39
     */
40
    protected $cat;
41
 
42
    /**
43
     * @var \stdClass $questiondata1
44
     */
45
    protected $questiondata1;
46
 
47
    /**
48
     * @var \stdClass $questiondata2
49
     */
50
    protected $questiondata2;
51
 
52
    /**
53
     * @var bool|\context|\context_course $context
54
     */
55
    protected $context;
56
 
57
    /**
58
     * @var \core_question\local\bank\question_edit_contexts $contexts
59
     */
60
    protected $contexts;
61
 
62
    /**
63
     * @var \stdClass $course
64
     */
65
    protected $course;
66
 
67
    /**
68
     * @var array $rawdata
69
     */
70
    protected $rawdata;
71
 
72
    /**
73
     * @var object $secondcategory
74
     */
75
    protected $secondcategory;
76
 
77
    /**
78
     * Setup the test.
79
     */
80
    protected function helper_setup(): void {
81
        $this->resetAfterTest();
82
        $this->setAdminUser();
83
        $generator = $this->getDataGenerator();
84
        /** @var \core_question_generator $questiongenerator */
85
        $questiongenerator = $generator->get_plugin_generator('core_question');
86
 
87
        // Create a course.
88
        $this->course = $generator->create_course();
1441 ariadna 89
        $qbank = self::getDataGenerator()->create_module('qbank', ['name' => 'QBANK 1', 'course' => $this->course->id]);
90
        $this->context = \context_module::instance($qbank->cmid);
1 efrain 91
 
92
        // Create a question in the default category.
93
        $this->contexts = new question_edit_contexts($this->context);
1441 ariadna 94
        $this->cat = question_get_default_category($this->contexts->lowest()->id, true);
1 efrain 95
        $this->questiondata1 = $questiongenerator->create_question('numerical', null,
96
            ['name' => 'Example question', 'category' => $this->cat->id]);
97
 
98
        // Create a second category to move questions.
99
        $this->secondcategory = $questiongenerator->create_question_category(['contextid' => $this->context->id,
100
            'parent' => $this->cat->id]);
101
 
102
        // Ensure the question is not in the cache.
103
        $cache = \cache::make('core', 'questiondata');
104
        $cache->delete($this->questiondata1->id);
105
 
106
        $this->questiondata2 = $questiongenerator->create_question('numerical', null,
107
            ['name' => 'Example question second', 'category' => $this->cat->id]);
108
 
109
        // Ensure the question is not in the cache.
110
        $cache = \cache::make('core', 'questiondata');
111
        $cache->delete($this->questiondata2->id);
112
 
113
        // Posted raw data.
114
        $this->rawdata = [
115
            'courseid' => $this->course->id,
116
            'cat' => "{$this->cat->id},{$this->context->id}",
117
            'qpage' => '0',
118
            "q{$this->questiondata1->id}" => '1',
119
            "q{$this->questiondata2->id}" => '1',
120
            'move' => 'Move to'
121
        ];
122
    }
123
 
124
    /**
125
     * Count how many questions in the list belong to the given category.
126
     *
127
     * @param string $categoryid a category id
128
     * @param array $questionids list of question ids
129
     * @return int
130
     */
131
    private function count_category_questions(string $categoryid, array $questionids): int {
132
        global $DB;
133
        $this->assertNotEmpty($questionids);
134
        list($insql, $inparams) = $DB->get_in_or_equal($questionids, SQL_PARAMS_NAMED);
135
        $sql = "SELECT COUNT(q.id)
136
                  FROM {question} q
137
                  JOIN {question_versions} qv ON qv.questionid = q.id
138
                  JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
139
                  JOIN {question_categories} qc ON qc.id = qbe.questioncategoryid
140
                 WHERE qc.id = :categoryid
141
                   AND q.id $insql";
142
 
143
        return $DB->count_records_sql($sql, array_merge(['categoryid' => $categoryid], $inparams));
144
    }
145
 
146
    /**
147
     * Assert that the given category contains following questions
148
     *
149
     * @param string $categoryid a category id
150
     * @param array $questionids list of question ids
151
     * @return void
152
     */
153
    protected function assert_category_contains_questions(string $categoryid, array $questionids) {
154
        // The category need to contain all the questions.
155
        $this->assertEquals(count($questionids), $this->count_category_questions($categoryid, $questionids));
156
    }
157
 
158
    /**
159
     * Assert that the given category does not contain following questions
160
     *
161
     * @param string $categoryid a category id
162
     * @param array $questionids list of question ids
163
     * @return void
164
     */
165
    protected function assert_category_does_not_contain_questions(string $categoryid, array $questionids) {
166
        // The category does not contain any question.
167
        $this->assertEquals(0, $this->count_category_questions($categoryid, $questionids));
168
    }
169
 
170
    /**
171
     * Test bulk move of questions.
172
     *
173
     * @covers ::bulk_move_questions
174
     */
11 efrain 175
    public function test_bulk_move_questions(): void {
1 efrain 176
        global $DB;
177
        $this->helper_setup();
178
 
179
        // Get the processed question ids.
180
        $questionlist = $this->process_question_ids_test();
181
        $questionids = array_map('intval', explode(',', $questionlist));
182
 
183
        // Verify that the questions are available in the current view.
184
        $this->assert_category_contains_questions($this->cat->id, $questionids);
185
        helper::bulk_move_questions($questionlist, $this->secondcategory);
186
 
187
        // Verify the questions are not in the current category.
188
        $this->assert_category_does_not_contain_questions($this->cat->id, $questionids);
189
 
190
        // Verify the questions are in the new category.
191
        $this->assert_category_contains_questions($this->secondcategory->id, $questionids);
192
    }
193
 
194
    /**
195
     * Test the question processing and return the question list.
196
     *
197
     * @return mixed
198
     * @covers ::process_question_ids
199
     */
200
    protected function process_question_ids_test() {
201
        // Test the raw data processing.
202
        list($questionids, $questionlist) = helper::process_question_ids($this->rawdata);
203
        $this->assertEquals([$this->questiondata1->id, $this->questiondata2->id], $questionids);
204
        $this->assertEquals("{$this->questiondata1->id},{$this->questiondata2->id}", $questionlist);
205
        return $questionlist;
206
    }
207
 
208
    /**
209
     * Test the question displaydata.
210
     *
211
     * @covers ::get_displaydata
212
     */
11 efrain 213
    public function test_get_displaydata(): void {
1 efrain 214
        $this->helper_setup();
1441 ariadna 215
        $contexts = new question_edit_contexts($this->context);
1 efrain 216
        $addcontexts = $contexts->having_cap('moodle/question:add');
217
        $url = new \moodle_url('/question/bank/bulkmove/move.php');
218
        $displaydata = \qbank_bulkmove\helper::get_displaydata($addcontexts, $url, $url);
1441 ariadna 219
        $this->assertDebuggingCalled();
1 efrain 220
        $this->assertStringContainsString('Test question category 1', $displaydata['categorydropdown']);
1441 ariadna 221
        $this->assertStringContainsString('Default for QBANK 1', $displaydata['categorydropdown']);
1 efrain 222
        $this->assertEquals($url, $displaydata ['moveurl']);
223
        $this->assertEquals($url, $displaydata ['returnurl']);
224
    }
225
}