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_match;
18
 
19
use qtype_match;
20
use qtype_match_edit_form;
21
use question_bank;
22
use question_possible_response;
23
 
24
defined('MOODLE_INTERNAL') || die();
25
 
26
global $CFG;
27
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
28
require_once($CFG->dirroot . '/question/type/match/questiontype.php');
29
require_once($CFG->dirroot . '/question/type/edit_question_form.php');
30
require_once($CFG->dirroot . '/question/type/match/edit_match_form.php');
31
 
32
 
33
/**
34
 * Unit tests for the matching question definition class.
35
 *
36
 * @package   qtype_match
37
 * @copyright 2009 The Open University
38
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
1441 ariadna 40
final class question_type_test extends \advanced_testcase {
1 efrain 41
    /** @var qtype_match instance of the question type class to test. */
42
    protected $qtype;
43
 
44
    protected function setUp(): void {
1441 ariadna 45
        parent::setUp();
1 efrain 46
        $this->qtype = new qtype_match();
47
    }
48
 
49
    protected function tearDown(): void {
50
        $this->qtype = null;
1441 ariadna 51
        parent::tearDown();
1 efrain 52
    }
53
 
54
    protected function get_test_question_data() {
55
        global $USER;
56
        $q = new \stdClass();
57
        $q->id = 0;
58
        $q->name = 'Matching question';
59
        $q->category = 0;
60
        $q->contextid = 0;
61
        $q->parent = 0;
62
        $q->questiontext = 'Classify the animals.';
63
        $q->questiontextformat = FORMAT_HTML;
64
        $q->generalfeedback = 'General feedback.';
65
        $q->generalfeedbackformat = FORMAT_HTML;
66
        $q->defaultmark = 1;
67
        $q->penalty = 0.3333333;
68
        $q->length = 1;
69
        $q->stamp = make_unique_id_code();
70
        $q->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
71
        $q->version = 1;
72
        $q->versionid = 0;
73
        $q->questionbankentryid = 0;
74
        $q->idnumber = null;
75
        $q->timecreated = time();
76
        $q->timemodified = time();
77
        $q->createdby = $USER->id;
78
        $q->modifiedby = $USER->id;
79
 
80
        $q->options = new \stdClass();
81
        $q->options->shuffleanswers = false;
82
        \test_question_maker::set_standard_combined_feedback_fields($q->options);
83
 
84
        $q->options->subquestions = array(
85
            14 => (object) array(
86
                'id' => 14,
87
                'questiontext' => 'frog',
88
                'questiontextformat' => FORMAT_HTML,
89
                'answertext' => 'amphibian'),
90
            15 => (object) array(
91
                'id' => 15,
92
                'questiontext' => 'cat',
93
                'questiontextformat' => FORMAT_HTML,
94
                'answertext' => 'mammal'),
95
            16 => (object) array(
96
                'id' => 16,
97
                'questiontext' => 'newt',
98
                'questiontextformat' => FORMAT_HTML,
99
                'answertext' => 'amphibian'),
100
            17 => (object) array(
101
                'id' => 17,
102
                'questiontext' => '',
103
                'questiontextformat' => FORMAT_HTML,
104
                'answertext' => 'insect'),
105
        );
106
 
107
        return $q;
108
    }
109
 
11 efrain 110
    public function test_name(): void {
1 efrain 111
        $this->assertEquals($this->qtype->name(), 'match');
112
    }
113
 
11 efrain 114
    public function test_can_analyse_responses(): void {
1 efrain 115
        $this->assertTrue($this->qtype->can_analyse_responses());
116
    }
117
 
11 efrain 118
    public function test_make_question_instance(): void {
1 efrain 119
        $questiondata = \test_question_maker::get_question_data('match', 'trickynums');
120
        $question = question_bank::make_question($questiondata);
121
        $this->assertEquals($questiondata->name, $question->name);
122
        $this->assertEquals($questiondata->questiontext, $question->questiontext);
123
        $this->assertEquals($questiondata->questiontextformat, $question->questiontextformat);
124
        $this->assertEquals($questiondata->generalfeedback, $question->generalfeedback);
125
        $this->assertEquals($questiondata->generalfeedbackformat, $question->generalfeedbackformat);
126
        $this->assertInstanceOf('qtype_match', $question->qtype);
127
        $this->assertEquals($questiondata->options->shuffleanswers, $question->shufflestems);
128
 
129
        $this->assertEquals(
130
                [14 => 'System.out.println(0);', 15 => 'System.out.println(0.0);'],
131
                $question->stems);
132
 
133
        $this->assertEquals([14 => '0', 15 => '0.0', 16 => 'NULL'], $question->choices);
134
 
135
        $this->assertEquals([14 => 14, 15 => 15], $question->right);
136
    }
137
 
11 efrain 138
    public function test_get_random_guess_score(): void {
1 efrain 139
        $q = $this->get_test_question_data();
140
        $this->assertEqualsWithDelta(0.3333333, $this->qtype->get_random_guess_score($q), 0.0000001);
141
    }
142
 
11 efrain 143
    public function test_get_possible_responses(): void {
1 efrain 144
        $q = $this->get_test_question_data();
145
 
146
        $this->assertEquals(array(
147
            14 => array(
148
                14 => new question_possible_response('frog: amphibian', 1/3),
149
                15 => new question_possible_response('frog: mammal', 0),
150
                17 => new question_possible_response('frog: insect', 0),
151
                null => question_possible_response::no_response()),
152
            15 => array(
153
                14 => new question_possible_response('cat: amphibian', 0),
154
                15 => new question_possible_response('cat: mammal', 1/3),
155
                17 => new question_possible_response('cat: insect', 0),
156
                null => question_possible_response::no_response()),
157
            16 => array(
158
                14 => new question_possible_response('newt: amphibian', 1/3),
159
                15 => new question_possible_response('newt: mammal', 0),
160
                17 => new question_possible_response('newt: insect', 0),
161
                null => question_possible_response::no_response()),
162
        ), $this->qtype->get_possible_responses($q));
163
    }
164
 
165
 
11 efrain 166
    public function test_question_saving_foursubq(): void {
1 efrain 167
        $this->resetAfterTest(true);
168
        $this->setAdminUser();
169
 
170
        $questiondata = \test_question_maker::get_question_data('match');
171
        $formdata = \test_question_maker::get_question_form_data('match');
172
 
173
        $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
174
        $cat = $generator->create_question_category(array());
175
 
176
        $formdata->category = "{$cat->id},{$cat->contextid}";
177
 
178
        qtype_match_edit_form::mock_submit((array)$formdata);
179
 
180
        $form = \qtype_match_test_helper::get_question_editing_form($cat, $questiondata);
181
        $this->assertTrue($form->is_validated());
182
 
183
        $fromform = $form->get_data();
184
 
185
        // Create a new question version with the form submission.
186
        unset($questiondata->id);
187
        $returnedfromsave = $this->qtype->save_question($questiondata, $fromform);
188
        $actualquestionsdata = question_load_questions([$returnedfromsave->id], 'qbe.idnumber');
189
        $actualquestiondata = end($actualquestionsdata);
190
 
191
        foreach ($questiondata as $property => $value) {
192
            if (!in_array($property, ['id', 'timemodified', 'timecreated', 'options', 'stamp',
1441 ariadna 193
                'versionid', 'questionbankentryid', 'hints'])) {
1 efrain 194
                if (!empty($actualquestiondata)) {
195
                    $this->assertEquals($value, $actualquestiondata->$property);
196
                }
197
            }
198
        }
199
 
200
        foreach ($questiondata->options as $optionname => $value) {
201
            if ($optionname != 'subquestions') {
202
                $this->assertEquals($value, $actualquestiondata->options->$optionname);
203
            }
204
        }
205
 
1441 ariadna 206
        $this->assertCount(1, $actualquestiondata->hints);
207
        $hint = array_pop($actualquestiondata->hints);
208
        $this->assertEquals($formdata->hint[0]['text'], $hint->hint);
209
        $this->assertEquals($formdata->hint[0]['format'], $hint->hintformat);
210
 
1 efrain 211
        $this->assertObjectHasProperty('subquestions', $actualquestiondata->options);
212
 
213
        $subqpropstoignore = array('id');
214
        foreach ($questiondata->options->subquestions as $subq) {
215
            $actualsubq = array_shift($actualquestiondata->options->subquestions);
216
            foreach ($subq as $subqproperty => $subqvalue) {
217
                if (!in_array($subqproperty, $subqpropstoignore)) {
218
                    $this->assertEquals($subqvalue, $actualsubq->$subqproperty);
219
                }
220
            }
221
        }
222
    }
223
}