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_calculatedsimple;
18
 
19
use qtype_calculated_dataset_loader;
20
use qtype_calculatedsimple;
21
use qtype_calculatedsimple_edit_form;
22
 
23
defined('MOODLE_INTERNAL') || die();
24
 
25
global $CFG;
26
require_once($CFG->dirroot . '/question/type/calculatedsimple/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/calculatedsimple/edit_calculatedsimple_form.php');
30
 
31
 
32
/**
33
 * Unit tests for the calculatedsimple question type class.
34
 *
35
 * @package    qtype_calculatedsimple
36
 * @copyright  2007 The Open University
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 *
39
 * @covers \question_type
40
 * @covers \qtype_calculatedsimple
41
 * @covers \question_wizard_form
42
 * @covers \question_edit_form
43
 * @covers \qtype_calculated_edit_form
44
 * @covers \qtype_calculatedsimple_edit_form
45
 *
46
 */
1441 ariadna 47
final class question_type_test extends \advanced_testcase {
1 efrain 48
    protected $qtype;
49
 
50
    protected function setUp(): void {
1441 ariadna 51
        parent::setUp();
1 efrain 52
        $this->qtype = new qtype_calculatedsimple();
53
    }
54
 
55
    protected function tearDown(): void {
56
        $this->qtype = null;
1441 ariadna 57
        parent::tearDown();
1 efrain 58
    }
59
 
11 efrain 60
    public function test_name(): void {
1 efrain 61
        $this->assertEquals($this->qtype->name(), 'calculatedsimple');
62
    }
63
 
11 efrain 64
    public function test_can_analyse_responses(): void {
1 efrain 65
        $this->assertTrue($this->qtype->can_analyse_responses());
66
    }
67
 
68
 
11 efrain 69
    public function test_question_saving_sumwithvariants(): void {
1 efrain 70
        $this->resetAfterTest(true);
71
        $this->setAdminUser();
72
 
73
        $questiondata = \test_question_maker::get_question_data('calculatedsimple', 'sumwithvariants');
74
        $formdata = \test_question_maker::get_question_form_data('calculatedsimple', 'sumwithvariants');
75
 
76
        $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
77
        $cat = $generator->create_question_category(array());
78
 
79
        $formdata->category = "{$cat->id},{$cat->contextid}";
80
        qtype_calculatedsimple_edit_form::mock_submit((array)$formdata);
81
 
82
        $form = \qtype_calculatedsimple_test_helper::get_question_editing_form($cat, $questiondata);
83
 
84
        $this->assertTrue($form->is_validated());
85
 
86
        $fromform = $form->get_data();
87
 
88
        $returnedfromsave = $this->qtype->save_question($questiondata, $fromform);
89
        $actualquestionsdata = question_load_questions(array($returnedfromsave->id));
90
        $actualquestiondata = end($actualquestionsdata);
91
 
92
        foreach ($questiondata as $property => $value) {
1441 ariadna 93
            if (!in_array($property, ['id', 'timemodified', 'timecreated', 'options', 'idnumber', 'hints'])) {
1 efrain 94
                $this->assertEquals($value, $actualquestiondata->$property);
95
            }
96
        }
97
 
98
        foreach ($questiondata->options as $optionname => $value) {
99
            if ($optionname != 'answers') {
100
                $this->assertEquals($value, $actualquestiondata->options->$optionname);
101
            }
102
        }
103
 
104
        foreach ($questiondata->options->answers as $answer) {
105
            $actualanswer = array_shift($actualquestiondata->options->answers);
106
            foreach ($answer as $ansproperty => $ansvalue) {
107
                if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) {
108
                    $this->assertEquals($ansvalue, $actualanswer->$ansproperty);
109
                }
110
            }
111
        }
112
 
1441 ariadna 113
        $this->assertCount(1, $actualquestiondata->hints);
114
        $hint = array_pop($actualquestiondata->hints);
115
        $this->assertEquals($formdata->hint[0]['text'], $hint->hint);
116
        $this->assertEquals($formdata->hint[0]['format'], $hint->hintformat);
117
 
1 efrain 118
        $datasetloader = new qtype_calculated_dataset_loader($actualquestiondata->id);
119
 
120
        $this->assertEquals(10, $datasetloader->get_number_of_items());
121
 
122
        for ($itemno = 1; $itemno <= 10; $itemno++) {
123
            $item = $datasetloader->get_values($itemno);
124
            $this->assertEquals((float)$formdata->number[($itemno -1)*2 + 2], $item['a']);
125
            $this->assertEquals((float)$formdata->number[($itemno -1)*2 + 1], $item['b']);
126
        }
127
    }
128
}