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_previewquestion;
18
 
19
use context_course;
20
use moodle_url;
21
use core\plugininfo\qbank;
22
use question_bank;
23
use question_engine;
24
use stdClass;
25
 
26
/**
27
 * Helper tests for question preview.
28
 *
29
 * @package    qbank_previewquestion
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_previewquestion\helper
34
 */
1441 ariadna 35
final class qbank_preview_helper_test extends \advanced_testcase {
1 efrain 36
 
37
    /**
38
     * @var bool|\context|\context_course $context
39
     */
40
    public $context;
41
 
42
    /**
43
     * @var object $questiondata;
44
     */
45
    public $questiondata;
46
 
47
    /**
48
     * @var \question_usage_by_activity $quba
49
     */
50
    public $quba;
51
 
52
    /**
53
     * @var question_preview_options $options
54
     */
55
    public $options;
56
 
57
    /**
58
     * @var \moodle_url $returnurl
59
     */
60
    public $returnurl;
61
 
62
    /**
63
     * Test set up.
64
     *
65
     * This is executed before running any test in this file.
66
     */
67
    public function setUp(): void {
68
        global $USER;
1441 ariadna 69
        parent::setUp();
1 efrain 70
        $this->resetAfterTest();
71
        $this->setAdminUser();
72
        $generator = $this->getDataGenerator();
73
        $questiongenerator = $generator->get_plugin_generator('core_question');
74
        // Create a course.
75
        $course = $generator->create_course();
1441 ariadna 76
        $qbank = $generator->create_module('qbank', ['course' => $course->id]);
77
        $qbankcontext = \context_module::instance($qbank->cmid);
78
        $this->context = $qbankcontext;
1 efrain 79
        // Create a question in the default category.
1441 ariadna 80
        $contexts = new \core_question\local\bank\question_edit_contexts($qbankcontext);
81
        $cat = question_get_default_category($contexts->lowest()->id, true);
1 efrain 82
        $this->questiondata = $questiongenerator->create_question('numerical', null,
83
                ['name' => 'Example question', 'category' => $cat->id]);
84
        $this->quba = question_engine::make_questions_usage_by_activity('core_question_preview',
85
            \context_user::instance($USER->id));
86
        $this->options = new question_preview_options($this->questiondata);
87
        $this->options->load_user_defaults();
88
        $this->options->set_from_request();
89
        $this->returnurl = new moodle_url('/question/edit.php');
90
    }
91
 
92
    /**
93
     * Test the preview action url from the helper class.
94
     *
95
     * @covers ::question_preview_action_url
96
     */
11 efrain 97
    public function test_question_preview_action_url(): void {
1 efrain 98
        $actionurl = helper::question_preview_action_url($this->questiondata->id, $this->quba->get_id(), $this->options,
99
                $this->context, $this->returnurl, question_preview_options::ALWAYS_LATEST);
100
        $params = [
101
           'id' => $this->questiondata->id,
102
           'previewid' => $this->quba->get_id(),
103
           'returnurl' => $this->returnurl,
1441 ariadna 104
           'cmid' => $this->context->instanceid,
1 efrain 105
           'restartversion' => question_preview_options::ALWAYS_LATEST,
106
        ];
107
        $params = array_merge($params, $this->options->get_url_params());
108
        $expectedurl = new moodle_url('/question/bank/previewquestion/preview.php', $params);
109
        $this->assertEquals($expectedurl, $actionurl);
110
    }
111
 
112
    /**
113
     * Test the preview action url from the helper class when no restartversion is passed.
114
     *
115
     * @covers ::question_preview_action_url
116
     */
11 efrain 117
    public function test_question_preview_action_url_no_restartversion(): void {
1 efrain 118
        $actionurl = helper::question_preview_action_url($this->questiondata->id, $this->quba->get_id(), $this->options,
119
                $this->context, $this->returnurl);
120
        $params = [
121
            'id' => $this->questiondata->id,
122
            'previewid' => $this->quba->get_id(),
123
            'returnurl' => $this->returnurl,
1441 ariadna 124
            'cmid' => $this->context->instanceid,
1 efrain 125
        ];
126
        $params = array_merge($params, $this->options->get_url_params());
127
        $expectedurl = new moodle_url('/question/bank/previewquestion/preview.php', $params);
128
        $this->assertEquals($expectedurl, $actionurl);
129
    }
130
 
131
    /**
132
     * Test the preview form url from the helper class.
133
     *
134
     * @covers ::question_preview_form_url
135
     */
11 efrain 136
    public function test_question_preview_form_url(): void {
1 efrain 137
        $formurl = helper::question_preview_form_url(
138
                $this->questiondata->id, $this->context, $this->quba->get_id(), $this->returnurl);
139
        $params = [
140
            'id' => $this->questiondata->id,
141
            'previewid' => $this->quba->get_id(),
142
            'returnurl' => $this->returnurl,
1441 ariadna 143
            'cmid' => $this->context->instanceid,
1 efrain 144
        ];
145
        $expectedurl = new moodle_url('/question/bank/previewquestion/preview.php', $params);
146
        $this->assertEquals($expectedurl, $formurl);
147
    }
148
 
149
    /**
150
     * Test the preview url from the helper class.
151
     *
152
     * @covers ::question_preview_url
153
     */
11 efrain 154
    public function test_question_preview_url(): void {
1 efrain 155
        $previewurl = helper::question_preview_url($this->questiondata->id, $this->options->behaviour, $this->options->maxmark,
156
                $this->options, $this->options->variant, $this->context, null, question_preview_options::ALWAYS_LATEST);
157
        $params = [
158
            'id' => $this->questiondata->id,
159
            'behaviour' => $this->options->behaviour,
160
            'maxmark' => $this->options->maxmark,
1441 ariadna 161
            'cmid' => $this->context->instanceid,
1 efrain 162
            'restartversion' => question_preview_options::ALWAYS_LATEST,
163
        ];
164
        // Extra params for options.
165
        $params['correctness']     = $this->options->correctness;
166
        $params['marks']           = $this->options->marks;
167
        $params['markdp']          = $this->options->markdp;
168
        $params['feedback']        = (bool) $this->options->feedback;
169
        $params['generalfeedback'] = (bool) $this->options->generalfeedback;
170
        $params['rightanswer']     = (bool) $this->options->rightanswer;
171
        $params['history']         = (bool) $this->options->history;
172
        $expectedurl = new moodle_url('/question/bank/previewquestion/preview.php', $params);
173
        $this->assertEquals($expectedurl, $previewurl);
174
    }
175
 
176
 
177
    /**
178
     * Test the preview url from the helper class.
179
     *
180
     * @covers ::question_preview_url
181
     */
11 efrain 182
    public function test_question_preview_url_no_restartversion(): void {
1 efrain 183
        $previewurl = helper::question_preview_url($this->questiondata->id, $this->options->behaviour, $this->options->maxmark,
184
                $this->options, $this->options->variant, $this->context, null);
185
        $params = [
186
            'id' => $this->questiondata->id,
187
            'behaviour' => $this->options->behaviour,
188
            'maxmark' => $this->options->maxmark,
1441 ariadna 189
            'cmid' => $this->context->instanceid,
1 efrain 190
        ];
191
        // Extra params for options.
192
        $params['correctness']     = $this->options->correctness;
193
        $params['marks']           = $this->options->marks;
194
        $params['markdp']          = $this->options->markdp;
195
        $params['feedback']        = (bool) $this->options->feedback;
196
        $params['generalfeedback'] = (bool) $this->options->generalfeedback;
197
        $params['rightanswer']     = (bool) $this->options->rightanswer;
198
        $params['history']         = (bool) $this->options->history;
199
        $expectedurl = new moodle_url('/question/bank/previewquestion/preview.php', $params);
200
        $this->assertEquals($expectedurl, $previewurl);
201
    }
202
 
203
    /**
204
     * Test the preview comment callback if available.
205
     *
206
     * @covers ::get_preview_extra_elements
207
     */
11 efrain 208
    public function test_get_preview_extra_elements(): void {
1 efrain 209
        global $PAGE;
210
        $PAGE->set_url('/');
211
 
212
        $question = \question_bank::load_question($this->questiondata->id);
213
        list($comment, $extraelements) = helper::get_preview_extra_elements($question, $this->context->instanceid);
214
        if (qbank::is_plugin_enabled('qbank_comment')) {
215
            $this->assertStringContainsString("comment-area", $comment);
216
        } else {
217
            $this->assertEquals('', $comment);
218
        }
219
    }
220
 
221
    /**
222
     * Test method load_versions().
223
     *
224
     * @covers ::load_versions
225
     */
11 efrain 226
    public function test_load_versions(): void {
1 efrain 227
        $this->resetAfterTest();
228
 
229
        $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
230
        $qcat1 = $generator->create_question_category(['name' => 'My category', 'sortorder' => 1, 'idnumber' => 'myqcat']);
231
        $questiongenerated = $generator->create_question('description', null, ['name' => 'q1', 'category' => $qcat1->id]);
232
 
233
        $qtypeobj = question_bank::get_qtype($questiongenerated->qtype);
234
        $question = question_bank::load_question($questiongenerated->id);
235
        $versionids = helper::load_versions($question->questionbankentryid);
236
        $this->assertEquals([
237
            $question->id => 1,
238
        ], $versionids);
239
 
240
        $fromform = new stdClass();
241
        $fromform->name = 'Name edited';
242
        $fromform->category = $qcat1->id;
243
        $questiontwo = $qtypeobj->save_question($questiongenerated, $fromform);
244
        $versionids = helper::load_versions($question->questionbankentryid);
245
        $this->assertSame([
246
            $question->id => 1,
247
            $questiontwo->id => 2,
248
        ], $versionids);
249
    }
250
 
251
    /**
252
     * Test method get_restart_id().
253
     *
254
     * This should return the value of the specified version number, or the latest version if ALWAYS_LATEST is passed.
255
     *
256
     * @covers ::get_restart_id
257
     * @return void
258
     */
259
    public function test_get_restart_id(): void {
260
        $versions = [
261
            100 => 1,
262
            200 => 2,
263
            300 => 3
264
        ];
265
 
266
        $this->assertEquals(100, helper::get_restart_id($versions, 1));
267
        $this->assertEquals(200, helper::get_restart_id($versions, 2));
268
        $this->assertEquals(300, helper::get_restart_id($versions, 3));
269
        $this->assertEquals(300, helper::get_restart_id($versions, question_preview_options::ALWAYS_LATEST));
270
        $this->assertNull(helper::get_restart_id($versions, 4));
271
        $this->assertNull(helper::get_restart_id([], 1));
272
        $this->assertNull(helper::get_restart_id([], question_preview_options::ALWAYS_LATEST));
273
    }
274
}