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_numerical;
18
 
19
use qtype_numerical;
20
use qtype_numerical_edit_form;
21
use question_possible_response;
22
 
23
defined('MOODLE_INTERNAL') || die();
24
 
25
global $CFG;
26
require_once($CFG->dirroot . '/question/type/numerical/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/numerical/edit_numerical_form.php');
30
 
31
/**
32
 * Unit tests for question/type/numerical/questiontype.php.
33
 *
34
 * @package    qtype_numerical
35
 * @copyright  2006 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_numerical
40
 */
1441 ariadna 41
final class question_type_test extends \advanced_testcase {
1 efrain 42
    protected $tolerance = 0.00000001;
43
    protected $qtype;
44
 
45
    protected function setUp(): void {
1441 ariadna 46
        parent::setUp();
1 efrain 47
        $this->qtype = new qtype_numerical();
48
    }
49
 
50
    protected function tearDown(): void {
51
        $this->qtype = null;
1441 ariadna 52
        parent::tearDown();
1 efrain 53
    }
54
 
55
    protected function get_test_question_data() {
56
        $q = new \stdClass;
57
        $q->id = 1;
58
        $q->options = new \stdClass();
59
        $q->options->unitpenalty = 0;
60
        $q->options->answers[13] = (object) array(
61
            'id' => 13,
62
            'answer' => 42,
63
            'fraction' => 1,
64
            'feedback' => 'yes',
65
            'feedbackformat' => FORMAT_MOODLE,
66
            'tolerance' => 0.5
67
        );
68
        $q->options->answers[14] = (object) array(
69
            'id' => 14,
70
            'answer' => '*',
71
            'fraction' => 0.1,
72
            'feedback' => 'no',
73
            'feedbackformat' => FORMAT_MOODLE,
74
            'tolerance' => ''
75
        );
76
 
77
        $q->options->units = array(
78
            (object) array('unit' => 'm', 'multiplier' => 1),
79
            (object) array('unit' => 'cm', 'multiplier' => 0.01)
80
        );
81
 
82
        return $q;
83
    }
84
 
11 efrain 85
    public function test_name(): void {
1 efrain 86
        $this->assertEquals($this->qtype->name(), 'numerical');
87
    }
88
 
11 efrain 89
    public function test_can_analyse_responses(): void {
1 efrain 90
        $this->assertTrue($this->qtype->can_analyse_responses());
91
    }
92
 
11 efrain 93
    public function test_get_random_guess_score(): void {
1 efrain 94
        $q = $this->get_test_question_data();
95
        $this->assertEquals(0.1, $this->qtype->get_random_guess_score($q));
96
    }
97
 
11 efrain 98
    public function test_get_possible_responses(): void {
1 efrain 99
        $q = $this->get_test_question_data();
100
 
101
        $this->assertEquals(array(
102
            $q->id => array(
103
                13 => new question_possible_response('42 m (41.5..42.5)', 1),
104
                14 => new question_possible_response('*', 0.1),
105
                null => question_possible_response::no_response()
106
            ),
107
        ), $this->qtype->get_possible_responses($q));
108
    }
109
 
11 efrain 110
    public function test_get_possible_responses_no_star(): void {
1 efrain 111
        $q = $this->get_test_question_data();
112
        unset($q->options->answers[14]);
113
 
114
        $this->assertEquals(array(
115
            $q->id => array(
116
                13 => new question_possible_response('42 m (41.5..42.5)', 1),
117
 
118
                        get_string('didnotmatchanyanswer', 'question'), 0),
119
                null => question_possible_response::no_response()
120
            ),
121
        ), $this->qtype->get_possible_responses($q));
122
    }
123
 
11 efrain 124
    public function test_question_saving_pi(): void {
1 efrain 125
        $this->resetAfterTest(true);
126
        $this->setAdminUser();
127
 
128
        $questiondata = \test_question_maker::get_question_data('numerical');
129
        $formdata = \test_question_maker::get_question_form_data('numerical');
130
 
131
        $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
132
        $cat = $generator->create_question_category(array());
133
 
134
        $formdata->category = "{$cat->id},{$cat->contextid}";
135
        qtype_numerical_edit_form::mock_submit((array)$formdata);
136
 
137
        $form = \qtype_numerical_test_helper::get_question_editing_form($cat, $questiondata);
138
 
139
        $this->assertTrue($form->is_validated());
140
 
141
        $fromform = $form->get_data();
142
 
143
        $returnedfromsave = $this->qtype->save_question($questiondata, $fromform);
144
        $actualquestionsdata = question_load_questions([$returnedfromsave->id], 'qbe.idnumber');
145
        $actualquestiondata = end($actualquestionsdata);
146
 
147
        foreach ($questiondata as $property => $value) {
148
            if (!in_array($property, array('options'))) {
149
                $this->assertEquals($value, $actualquestiondata->$property);
150
            }
151
        }
152
 
153
        foreach ($questiondata->options as $optionname => $value) {
154
            if (!in_array($optionname, array('answers'))) {
155
                $this->assertEquals($value, $actualquestiondata->options->$optionname);
156
            }
157
        }
158
 
159
        foreach ($questiondata->options->answers as $ansindex => $answer) {
160
            $actualanswer = array_shift($actualquestiondata->options->answers);
161
            foreach ($answer as $ansproperty => $ansvalue) {
162
                // This question does not use 'answerformat', will ignore it.
163
                if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) {
164
                    $this->assertEquals($ansvalue, $actualanswer->$ansproperty);
165
                }
166
            }
167
        }
168
    }
169
 
11 efrain 170
    public function test_is_valid_number(): void {
1 efrain 171
        $this->assertTrue(qtype_numerical::is_valid_number('1,001'));
172
        $this->assertTrue(qtype_numerical::is_valid_number('1.001'));
173
        $this->assertTrue(qtype_numerical::is_valid_number('1'));
174
        $this->assertTrue(qtype_numerical::is_valid_number('1,e8'));
175
        $this->assertFalse(qtype_numerical::is_valid_number('1001 xxx'));
176
        $this->assertTrue(qtype_numerical::is_valid_number('1.e8'));
177
    }
178
}