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 qtype_multichoice;
18
 
19
use qtype_multichoice;
20
use qtype_multichoice_edit_form;
21
use question_possible_response;
22
 
23
defined('MOODLE_INTERNAL') || die();
24
 
25
global $CFG;
26
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
27
require_once($CFG->dirroot . '/question/type/multichoice/questiontype.php');
28
require_once($CFG->dirroot . '/question/type/edit_question_form.php');
29
require_once($CFG->dirroot . '/question/type/multichoice/edit_multichoice_form.php');
30
 
31
/**
32
 * Unit tests for the multiple choice question definition class.
33
 *
34
 * @package   qtype_multichoice
35
 * @copyright 2009 The Open University
36
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 * @covers    \qtype_multichoice
38
 */
1441 ariadna 39
final class question_type_test extends \advanced_testcase {
1 efrain 40
    protected $qtype;
41
 
42
    protected function setUp(): void {
1441 ariadna 43
        parent::setUp();
1 efrain 44
        $this->qtype = new qtype_multichoice();
45
    }
46
 
47
    protected function tearDown(): void {
48
        $this->qtype = null;
1441 ariadna 49
        parent::tearDown();
1 efrain 50
    }
51
 
11 efrain 52
    public function test_name(): void {
1 efrain 53
        $this->assertEquals($this->qtype->name(), 'multichoice');
54
    }
55
 
56
    protected function get_test_question_data() {
57
        $q = new \stdClass();
58
        $q->id = 1;
59
        $q->options = new \stdClass();
60
        $q->options->single = true;
61
        $q->options->answers[1] = (object) array('answer' => 'frog',
62
                'answerformat' => FORMAT_HTML, 'fraction' => 1);
63
        $q->options->answers[2] = (object) array('answer' => 'toad',
64
                'answerformat' => FORMAT_HTML, 'fraction' => 0);
65
 
66
        return $q;
67
    }
68
 
11 efrain 69
    public function test_can_analyse_responses(): void {
1 efrain 70
        $this->assertTrue($this->qtype->can_analyse_responses());
71
    }
72
 
11 efrain 73
    public function test_get_random_guess_score(): void {
1 efrain 74
        $q = $this->get_test_question_data();
75
        $this->assertEquals(0.5, $this->qtype->get_random_guess_score($q));
76
    }
77
 
11 efrain 78
    public function test_get_random_guess_score_broken_question(): void {
1 efrain 79
        $q = $this->get_test_question_data();
80
        $q->options->answers = [];
81
        $this->assertNull($this->qtype->get_random_guess_score($q));
82
    }
83
 
11 efrain 84
    public function test_get_random_guess_score_multi(): void {
1 efrain 85
        $q = $this->get_test_question_data();
86
        $q->options->single = false;
87
        $this->assertNull($this->qtype->get_random_guess_score($q));
88
    }
89
 
11 efrain 90
    public function test_get_possible_responses_single(): void {
1 efrain 91
        $q = $this->get_test_question_data();
92
        $responses = $this->qtype->get_possible_responses($q);
93
 
94
        $this->assertEquals(array(
95
            $q->id => array(
96
                1 => new question_possible_response('frog', 1),
97
                2 => new question_possible_response('toad', 0),
98
                null => question_possible_response::no_response(),
99
            )), $this->qtype->get_possible_responses($q));
100
    }
101
 
11 efrain 102
    public function test_get_possible_responses_multi(): void {
1 efrain 103
        $q = $this->get_test_question_data();
104
        $q->options->single = false;
105
 
106
        $this->assertEquals(array(
107
            1 => array(1 => new question_possible_response('frog', 1)),
108
            2 => array(2 => new question_possible_response('toad', 0)),
109
        ), $this->qtype->get_possible_responses($q));
110
    }
111
 
1441 ariadna 112
    public static function get_question_saving_which(): array {
1 efrain 113
        return array(array('two_of_four'), array('one_of_four'));
114
    }
115
 
116
    /**
117
     * @dataProvider get_question_saving_which
118
     */
11 efrain 119
    public function test_question_saving_two_of_four($which): void {
1 efrain 120
        $this->resetAfterTest(true);
121
        $this->setAdminUser();
122
 
123
        $questiondata = \test_question_maker::get_question_data('multichoice', $which);
124
        $formdata = \test_question_maker::get_question_form_data('multichoice', $which);
125
 
126
        $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
127
        $cat = $generator->create_question_category(array());
128
 
129
        $formdata->category = "{$cat->id},{$cat->contextid}";
130
        qtype_multichoice_edit_form::mock_submit((array)$formdata);
131
 
132
        $form = \qtype_multichoice_test_helper::get_question_editing_form($cat, $questiondata);
133
 
134
        $this->assertTrue($form->is_validated());
135
 
136
        $fromform = $form->get_data();
137
 
138
        $returnedfromsave = $this->qtype->save_question($questiondata, $fromform);
139
        $actualquestionsdata = question_load_questions([$returnedfromsave->id], 'qbe.idnumber');
140
        $actualquestiondata = end($actualquestionsdata);
141
 
142
        foreach ($questiondata as $property => $value) {
143
            if (!in_array($property, ['id', 'timemodified', 'timecreated', 'options', 'hints', 'stamp',
144
                'versionid', 'questionbankentryid'])) {
145
                $this->assertEquals($value, $actualquestiondata->$property);
146
            }
147
        }
148
 
149
        foreach ($questiondata->options as $optionname => $value) {
150
            if ($optionname != 'answers') {
151
                $this->assertEquals($value, $actualquestiondata->options->$optionname);
152
            }
153
        }
154
 
155
        foreach ($questiondata->hints as $hint) {
156
            $actualhint = array_shift($actualquestiondata->hints);
157
            foreach ($hint as $property => $value) {
158
                if (!in_array($property, array('id', 'questionid', 'options'))) {
159
                    $this->assertEquals($value, $actualhint->$property);
160
                }
161
            }
162
        }
163
 
164
        foreach ($questiondata->options->answers as $answer) {
165
            $actualanswer = array_shift($actualquestiondata->options->answers);
166
            foreach ($answer as $ansproperty => $ansvalue) {
167
                // This question does not use 'answerformat', will ignore it.
168
                if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) {
169
                    $this->assertEquals($ansvalue, $actualanswer->$ansproperty);
170
                }
171
            }
172
        }
173
    }
174
 
175
    /**
176
     * Test to make sure that loading of question options works, including in an error case.
177
     */
11 efrain 178
    public function test_get_question_options(): void {
1 efrain 179
        global $DB;
180
 
181
        $this->resetAfterTest(true);
182
        $this->setAdminUser();
183
 
184
        // Create a complete, in DB question to use.
185
        $questiondata = \test_question_maker::get_question_data('multichoice', 'two_of_four');
186
        $formdata = \test_question_maker::get_question_form_data('multichoice', 'two_of_four');
187
 
188
        $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
189
        $cat = $generator->create_question_category(array());
190
 
191
        $formdata->category = "{$cat->id},{$cat->contextid}";
192
        qtype_multichoice_edit_form::mock_submit((array)$formdata);
193
 
194
        $form = \qtype_multichoice_test_helper::get_question_editing_form($cat, $questiondata);
195
 
196
        $this->assertTrue($form->is_validated());
197
 
198
        $fromform = $form->get_data();
199
 
200
        $returnedfromsave = $this->qtype->save_question($questiondata, $fromform);
201
 
202
        // Now get just the raw DB record.
203
        $question = $DB->get_record('question', ['id' => $returnedfromsave->id], '*', MUST_EXIST);
204
 
205
        // Load it.
206
        $this->qtype->get_question_options($question);
207
        $this->assertDebuggingNotCalled();
208
        $this->assertInstanceOf(\stdClass::class, $question->options);
209
 
210
        $options = $question->options;
211
        $this->assertEquals($question->id, $options->questionid);
212
        $this->assertEquals(0, $options->single);
213
 
214
        $this->assertCount(4, $options->answers);
215
 
216
        // Now we are going to delete the options record.
217
        $DB->delete_records('qtype_multichoice_options', ['questionid' => $question->id]);
218
 
219
        // Now see what happens.
220
        $question = $DB->get_record('question', ['id' => $returnedfromsave->id], '*', MUST_EXIST);
221
        $this->qtype->get_question_options($question);
222
 
223
        $this->assertDebuggingCalled('Question ID '.$question->id.' was missing an options record. Using default.');
224
        $this->assertInstanceOf(\stdClass::class, $question->options);
225
        $options = $question->options;
226
        $this->assertEquals($question->id, $options->questionid);
227
        $this->assertCount(4, $options->answers);
228
 
229
        $this->assertEquals(get_string('correctfeedbackdefault', 'question'), $options->correctfeedback);
230
        $this->assertEquals(FORMAT_HTML, $options->correctfeedbackformat);
231
 
232
        // We no longer know how many answers, so it just has to guess with the default value.
233
        $this->assertEquals(get_config('qtype_multichoice', 'answerhowmany'), $options->single);
234
 
235
        // And finally we try again with no answer either.
236
        $DB->delete_records('question_answers', ['question' => $question->id]);
237
 
238
        $question = $DB->get_record('question', ['id' => $returnedfromsave->id], '*', MUST_EXIST);
239
        $this->qtype->get_question_options($question);
240
 
241
        $this->assertDebuggingCalled('Question ID '.$question->id.' was missing an options record. Using default.');
242
        $this->assertInstanceOf(\stdClass::class, $question->options);
243
        $options = $question->options;
244
        $this->assertEquals($question->id, $options->questionid);
245
        $this->assertCount(0, $options->answers);
246
    }
247
}