Merge branch 'MDL-70065_310' of https://github.com/timhunt/moodle into MOODLE_310_STABLE
[moodle.git] / question / type / calculatedsimple / tests / questiontype_test.php
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/>.
17 /**
18  * Unit tests for the calculatedsimple question type class.
19  *
20  * @package    qtype_calculatedsimple
21  * @copyright  2013 The Open University
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->dirroot . '/question/type/calculatedsimple/questiontype.php');
30 require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
31 require_once($CFG->dirroot . '/question/type/edit_question_form.php');
32 require_once($CFG->dirroot . '/question/type/calculatedsimple/edit_calculatedsimple_form.php');
35 /**
36  * Unit tests for the calculatedsimple question type class.
37  *
38  * @copyright  2007 The Open University
39  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40  */
41 class qtype_calculatedsimple_test extends advanced_testcase {
42     public static $includecoverage = array(
43         'question/type/questiontypebase.php',
44         'question/type/calculatedsimple/questiontype.php',
45         'question/type/edit_question_form.php',
46         'question/type/calculatedsimple/edit_calculatedsimple_form.php'
47     );
49     protected $qtype;
51     protected function setUp(): void {
52         $this->qtype = new qtype_calculatedsimple();
53     }
55     protected function tearDown(): void {
56         $this->qtype = null;
57     }
59     public function test_name() {
60         $this->assertEquals($this->qtype->name(), 'calculatedsimple');
61     }
63     public function test_can_analyse_responses() {
64         $this->assertTrue($this->qtype->can_analyse_responses());
65     }
68     public function test_question_saving_sumwithvariants() {
69         $this->resetAfterTest(true);
70         $this->setAdminUser();
72         $questiondata = test_question_maker::get_question_data('calculatedsimple', 'sumwithvariants');
73         $formdata = test_question_maker::get_question_form_data('calculatedsimple', 'sumwithvariants');
75         $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
76         $cat = $generator->create_question_category(array());
78         $formdata->category = "{$cat->id},{$cat->contextid}";
79         qtype_calculatedsimple_edit_form::mock_submit((array)$formdata);
81         $form = qtype_calculatedsimple_test_helper::get_question_editing_form($cat, $questiondata);
83         $this->assertTrue($form->is_validated());
85         $fromform = $form->get_data();
87         $returnedfromsave = $this->qtype->save_question($questiondata, $fromform);
88         $actualquestionsdata = question_load_questions(array($returnedfromsave->id));
89         $actualquestiondata = end($actualquestionsdata);
91         foreach ($questiondata as $property => $value) {
92             if (!in_array($property, array('id', 'version', 'timemodified', 'timecreated', 'options'))) {
93                 $this->assertEquals($value, $actualquestiondata->$property);
94             }
95         }
97         foreach ($questiondata->options as $optionname => $value) {
98             if ($optionname != 'answers') {
99                 $this->assertEquals($value, $actualquestiondata->options->$optionname);
100             }
101         }
103         foreach ($questiondata->options->answers as $answer) {
104             $actualanswer = array_shift($actualquestiondata->options->answers);
105             foreach ($answer as $ansproperty => $ansvalue) {
106                 if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) {
107                     $this->assertEquals($ansvalue, $actualanswer->$ansproperty);
108                 }
109             }
110         }
112         $datasetloader = new qtype_calculated_dataset_loader($actualquestiondata->id);
114         $this->assertEquals(10, $datasetloader->get_number_of_items());
116         for ($itemno = 1; $itemno <= 10; $itemno++) {
117             $item = $datasetloader->get_values($itemno);
118             $this->assertEquals((float)$formdata->number[($itemno -1)*2 + 2], $item['a']);
119             $this->assertEquals((float)$formdata->number[($itemno -1)*2 + 1], $item['b']);
120         }
121     }