Merge branch 'MDL-70065_310' of https://github.com/timhunt/moodle into MOODLE_310_STABLE
[moodle.git] / question / type / calculatedsimple / tests / questiontype_test.php
CommitLineData
01c898ec
JP
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/**
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 */
24
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->dirroot . '/question/type/calculatedsimple/questiontype.php');
30require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
31require_once($CFG->dirroot . '/question/type/edit_question_form.php');
32require_once($CFG->dirroot . '/question/type/calculatedsimple/edit_calculatedsimple_form.php');
33
34
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 */
41class 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 );
48
49 protected $qtype;
50
d81a9480 51 protected function setUp(): void {
01c898ec
JP
52 $this->qtype = new qtype_calculatedsimple();
53 }
54
d81a9480 55 protected function tearDown(): void {
01c898ec
JP
56 $this->qtype = null;
57 }
58
59 public function test_name() {
60 $this->assertEquals($this->qtype->name(), 'calculatedsimple');
61 }
62
63 public function test_can_analyse_responses() {
64 $this->assertTrue($this->qtype->can_analyse_responses());
65 }
66
67
68 public function test_question_saving_sumwithvariants() {
69 $this->resetAfterTest(true);
70 $this->setAdminUser();
71
72 $questiondata = test_question_maker::get_question_data('calculatedsimple', 'sumwithvariants');
73 $formdata = test_question_maker::get_question_form_data('calculatedsimple', 'sumwithvariants');
74
75 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
76 $cat = $generator->create_question_category(array());
77
78 $formdata->category = "{$cat->id},{$cat->contextid}";
79 qtype_calculatedsimple_edit_form::mock_submit((array)$formdata);
80
81 $form = qtype_calculatedsimple_test_helper::get_question_editing_form($cat, $questiondata);
82
83 $this->assertTrue($form->is_validated());
84
85 $fromform = $form->get_data();
86
87 $returnedfromsave = $this->qtype->save_question($questiondata, $fromform);
88 $actualquestionsdata = question_load_questions(array($returnedfromsave->id));
89 $actualquestiondata = end($actualquestionsdata);
90
91 foreach ($questiondata as $property => $value) {
92 if (!in_array($property, array('id', 'version', 'timemodified', 'timecreated', 'options'))) {
106c64ac 93 $this->assertEquals($value, $actualquestiondata->$property);
01c898ec
JP
94 }
95 }
96
97 foreach ($questiondata->options as $optionname => $value) {
98 if ($optionname != 'answers') {
106c64ac 99 $this->assertEquals($value, $actualquestiondata->options->$optionname);
01c898ec
JP
100 }
101 }
102
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'))) {
106c64ac 107 $this->assertEquals($ansvalue, $actualanswer->$ansproperty);
01c898ec
JP
108 }
109 }
110 }
111
112 $datasetloader = new qtype_calculated_dataset_loader($actualquestiondata->id);
113
114 $this->assertEquals(10, $datasetloader->get_number_of_items());
115
116 for ($itemno = 1; $itemno <= 10; $itemno++) {
117 $item = $datasetloader->get_values($itemno);
47538a6b
SR
118 $this->assertEquals((float)$formdata->number[($itemno -1)*2 + 2], $item['a']);
119 $this->assertEquals((float)$formdata->number[($itemno -1)*2 + 1], $item['b']);
01c898ec
JP
120 }
121 }
122}