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_shortanswer;
18
 
19
use qtype_shortanswer;
20
use qtype_shortanswer_edit_form;
21
use question_possible_response;
22
 
23
defined('MOODLE_INTERNAL') || die();
24
 
25
global $CFG;
26
require_once($CFG->dirroot . '/question/type/shortanswer/questiontype.php');
27
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
28
require_once($CFG->dirroot . '/question/type/edit_question_form.php');
29
require_once($CFG->dirroot . '/question/type/shortanswer/edit_shortanswer_form.php');
30
 
31
/**
32
 * Unit tests for the shortanswer question type class.
33
 *
34
 * @package    qtype_shortanswer
35
 * @copyright  2007 The Open University
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 *
38
 * @covers \question_type
39
 * @covers \qtype_shortanswer
40
 */
1441 ariadna 41
final class question_type_test extends \advanced_testcase {
1 efrain 42
    protected $qtype;
43
 
44
    protected function setUp(): void {
1441 ariadna 45
        parent::setUp();
1 efrain 46
        $this->qtype = new qtype_shortanswer();
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
        return \test_question_maker::get_question_data('shortanswer');
56
    }
57
 
11 efrain 58
    public function test_name(): void {
1 efrain 59
        $this->assertEquals($this->qtype->name(), 'shortanswer');
60
    }
61
 
11 efrain 62
    public function test_can_analyse_responses(): void {
1 efrain 63
        $this->assertTrue($this->qtype->can_analyse_responses());
64
    }
65
 
11 efrain 66
    public function test_get_random_guess_score(): void {
1 efrain 67
        $q = \test_question_maker::get_question_data('shortanswer');
68
        $q->options->answers[15]->fraction = 0.1;
69
        $this->assertEquals(0.1, $this->qtype->get_random_guess_score($q));
70
    }
71
 
11 efrain 72
    public function test_get_possible_responses(): void {
1 efrain 73
        $q = \test_question_maker::get_question_data('shortanswer');
74
 
75
        $this->assertEquals(array(
76
            $q->id => array(
77
                13 => new question_possible_response('frog', 1),
78
                14 => new question_possible_response('toad', 0.8),
79
                15 => new question_possible_response('*', 0),
80
                null => question_possible_response::no_response()
81
            ),
82
        ), $this->qtype->get_possible_responses($q));
83
    }
84
 
11 efrain 85
    public function test_get_possible_responses_no_star(): void {
1 efrain 86
        $q = \test_question_maker::get_question_data('shortanswer', 'frogonly');
87
 
88
        $this->assertEquals(array(
89
            $q->id => array(
90
                13 => new question_possible_response('frog', 1),
91
 
92
                null => question_possible_response::no_response()
93
            ),
94
        ), $this->qtype->get_possible_responses($q));
95
    }
96
 
11 efrain 97
    public function test_question_saving_frogtoad(): void {
1 efrain 98
        $this->resetAfterTest(true);
99
        $this->setAdminUser();
100
 
101
        $questiondata = \test_question_maker::get_question_data('shortanswer');
102
        $formdata = \test_question_maker::get_question_form_data('shortanswer');
103
 
104
        $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
105
        $cat = $generator->create_question_category(array());
106
 
107
        $formdata->category = "{$cat->id},{$cat->contextid}";
108
        qtype_shortanswer_edit_form::mock_submit((array)$formdata);
109
 
110
        $form = \qtype_shortanswer_test_helper::get_question_editing_form($cat, $questiondata);
111
 
112
        $this->assertTrue($form->is_validated());
113
 
114
        $fromform = $form->get_data();
115
 
116
        // Create a new question version with the form submission.
117
        unset($questiondata->id);
118
        $returnedfromsave = $this->qtype->save_question($questiondata, $fromform);
119
        $actualquestionsdata = question_load_questions([$returnedfromsave->id], 'qbe.idnumber');
120
        $actualquestiondata = end($actualquestionsdata);
121
 
122
        foreach ($questiondata as $property => $value) {
1441 ariadna 123
            if (!in_array($property, ['id', 'timemodified', 'timecreated', 'options', 'hints'])) {
1 efrain 124
                $this->assertEquals($value, $actualquestiondata->$property);
125
            }
126
        }
127
 
128
        foreach ($questiondata->options as $optionname => $value) {
129
            if ($optionname != 'answers') {
130
                $this->assertEquals($value, $actualquestiondata->options->$optionname);
131
            }
132
        }
133
 
134
        foreach ($questiondata->options->answers as $answer) {
135
            $actualanswer = array_shift($actualquestiondata->options->answers);
136
            foreach ($answer as $ansproperty => $ansvalue) {
137
                // This question does not use 'answerformat', will ignore it.
138
                if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) {
139
                    $this->assertEquals($ansvalue, $actualanswer->$ansproperty);
140
                }
141
            }
142
        }
1441 ariadna 143
 
144
        $this->assertCount(1, $actualquestiondata->hints);
145
        $hint = array_pop($actualquestiondata->hints);
146
        $this->assertEquals($formdata->hint[0]['text'], $hint->hint);
147
        $this->assertEquals($formdata->hint[0]['format'], $hint->hintformat);
1 efrain 148
    }
149
 
11 efrain 150
    public function test_question_saving_trims_answers(): void {
1 efrain 151
        $this->resetAfterTest(true);
152
        $this->setAdminUser();
153
 
154
        $questiondata = \test_question_maker::get_question_data('shortanswer');
155
        $formdata = \test_question_maker::get_question_form_data('shortanswer');
156
 
157
        $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
158
        $cat = $generator->create_question_category(array());
159
 
160
        $formdata->category = "{$cat->id},{$cat->contextid}";
161
        $formdata->answer[0] = '   frog   ';
162
        qtype_shortanswer_edit_form::mock_submit((array)$formdata);
163
 
164
        $form = \qtype_shortanswer_test_helper::get_question_editing_form($cat, $questiondata);
165
 
166
        $this->assertTrue($form->is_validated());
167
 
168
        $fromform = $form->get_data();
169
 
170
        $returnedfromsave = $this->qtype->save_question($questiondata, $fromform);
171
        $actualquestionsdata = question_load_questions(array($returnedfromsave->id));
172
        $actualquestiondata = end($actualquestionsdata);
173
 
174
        $firstsavedanswer = reset($questiondata->options->answers);
175
        $this->assertEquals('frog', $firstsavedanswer->answer);
176
    }
177
}