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_truefalse;
|
|
|
18 |
|
|
|
19 |
use qtype_truefalse;
|
|
|
20 |
use qtype_truefalse_edit_form;
|
|
|
21 |
use question_bank;
|
|
|
22 |
use question_possible_response;
|
|
|
23 |
|
|
|
24 |
defined('MOODLE_INTERNAL') || die();
|
|
|
25 |
|
|
|
26 |
global $CFG;
|
|
|
27 |
require_once($CFG->dirroot . '/question/type/truefalse/questiontype.php');
|
|
|
28 |
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
|
|
|
29 |
require_once($CFG->dirroot . '/question/type/edit_question_form.php');
|
|
|
30 |
require_once($CFG->dirroot . '/question/type/truefalse/edit_truefalse_form.php');
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Unit tests for the true-false question definition class.
|
|
|
34 |
*
|
|
|
35 |
* @package qtype_truefalse
|
|
|
36 |
* @copyright 2007 The Open University
|
|
|
37 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
38 |
*/
|
1441 |
ariadna |
39 |
final class question_type_test extends \advanced_testcase {
|
1 |
efrain |
40 |
protected $qtype;
|
|
|
41 |
|
|
|
42 |
protected function setUp(): void {
|
1441 |
ariadna |
43 |
parent::setUp();
|
1 |
efrain |
44 |
$this->qtype = new qtype_truefalse();
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
protected function tearDown(): void {
|
|
|
48 |
$this->qtype = null;
|
1441 |
ariadna |
49 |
parent::tearDown();
|
1 |
efrain |
50 |
}
|
|
|
51 |
|
11 |
efrain |
52 |
public function test_name(): void {
|
1 |
efrain |
53 |
$this->assertEquals($this->qtype->name(), 'truefalse');
|
|
|
54 |
}
|
|
|
55 |
|
11 |
efrain |
56 |
public function test_can_analyse_responses(): void {
|
1 |
efrain |
57 |
$this->assertTrue($this->qtype->can_analyse_responses());
|
|
|
58 |
}
|
|
|
59 |
|
11 |
efrain |
60 |
public function test_get_random_guess_score(): void {
|
1 |
efrain |
61 |
$this->assertEquals(0.5, $this->qtype->get_random_guess_score(null));
|
|
|
62 |
}
|
|
|
63 |
|
11 |
efrain |
64 |
public function test_load_question(): void {
|
1 |
efrain |
65 |
$this->resetAfterTest();
|
|
|
66 |
|
|
|
67 |
$syscontext = \context_system::instance();
|
|
|
68 |
/** @var core_question_generator $generator */
|
|
|
69 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
70 |
$category = $generator->create_question_category(['contextid' => $syscontext->id]);
|
|
|
71 |
|
|
|
72 |
$fromform = \test_question_maker::get_question_form_data('truefalse');
|
|
|
73 |
$fromform->category = $category->id . ',' . $syscontext->id;
|
|
|
74 |
|
|
|
75 |
$question = new \stdClass();
|
|
|
76 |
$question->category = $category->id;
|
|
|
77 |
$question->qtype = 'truefalse';
|
|
|
78 |
$question->createdby = 0;
|
|
|
79 |
|
|
|
80 |
$this->qtype->save_question($question, $fromform);
|
|
|
81 |
$questiondata = question_bank::load_question_data($question->id);
|
|
|
82 |
|
|
|
83 |
$this->assertEquals(['id', 'category', 'parent', 'name', 'questiontext', 'questiontextformat',
|
|
|
84 |
'generalfeedback', 'generalfeedbackformat', 'defaultmark', 'penalty', 'qtype',
|
|
|
85 |
'length', 'stamp', 'timecreated', 'timemodified', 'createdby', 'modifiedby', 'idnumber', 'contextid',
|
|
|
86 |
'status', 'versionid', 'version', 'questionbankentryid', 'categoryobject', 'options', 'hints'],
|
|
|
87 |
array_keys(get_object_vars($questiondata)));
|
|
|
88 |
$this->assertEquals($category->id, $questiondata->category);
|
|
|
89 |
$this->assertEquals(0, $questiondata->parent);
|
|
|
90 |
$this->assertEquals($fromform->name, $questiondata->name);
|
|
|
91 |
$this->assertEquals($fromform->questiontext, $questiondata->questiontext);
|
|
|
92 |
$this->assertEquals($fromform->questiontextformat, $questiondata->questiontextformat);
|
|
|
93 |
$this->assertEquals($fromform->generalfeedback['text'], $questiondata->generalfeedback);
|
|
|
94 |
$this->assertEquals($fromform->generalfeedback['format'], $questiondata->generalfeedbackformat);
|
|
|
95 |
$this->assertEquals($fromform->defaultmark, $questiondata->defaultmark);
|
|
|
96 |
$this->assertEquals(1, $questiondata->penalty);
|
|
|
97 |
$this->assertEquals('truefalse', $questiondata->qtype);
|
|
|
98 |
$this->assertEquals(1, $questiondata->length);
|
|
|
99 |
$this->assertEquals(\core_question\local\bank\question_version_status::QUESTION_STATUS_READY, $questiondata->status);
|
|
|
100 |
$this->assertEquals($question->createdby, $questiondata->createdby);
|
|
|
101 |
$this->assertEquals($question->createdby, $questiondata->modifiedby);
|
|
|
102 |
$this->assertEquals('', $questiondata->idnumber);
|
1441 |
ariadna |
103 |
$this->assertEquals($category->contextid, $questiondata->contextid);
|
1 |
efrain |
104 |
|
|
|
105 |
// Options.
|
|
|
106 |
$this->assertEquals($questiondata->id, $questiondata->options->question);
|
|
|
107 |
$this->assertEquals('True', $questiondata->options->answers[$questiondata->options->trueanswer]->answer);
|
|
|
108 |
$this->assertEquals('False', $questiondata->options->answers[$questiondata->options->falseanswer]->answer);
|
|
|
109 |
$this->assertEquals(1.0, $questiondata->options->answers[$questiondata->options->trueanswer]->fraction);
|
|
|
110 |
$this->assertEquals(0.0, $questiondata->options->answers[$questiondata->options->falseanswer]->fraction);
|
|
|
111 |
$this->assertEquals('This is the right answer.',
|
|
|
112 |
$questiondata->options->answers[$questiondata->options->trueanswer]->feedback);
|
|
|
113 |
$this->assertEquals('This is the wrong answer.',
|
|
|
114 |
$questiondata->options->answers[$questiondata->options->falseanswer]->feedback);
|
|
|
115 |
$this->assertEquals(FORMAT_HTML, $questiondata->options->answers[$questiondata->options->trueanswer]->feedbackformat);
|
|
|
116 |
$this->assertEquals(FORMAT_HTML, $questiondata->options->answers[$questiondata->options->falseanswer]->feedbackformat);
|
|
|
117 |
|
|
|
118 |
// Hints.
|
|
|
119 |
$this->assertEquals([], $questiondata->hints);
|
|
|
120 |
}
|
|
|
121 |
|
11 |
efrain |
122 |
public function test_get_possible_responses(): void {
|
1 |
efrain |
123 |
$q = new \stdClass();
|
|
|
124 |
$q->id = 1;
|
|
|
125 |
$q->options = new \stdClass();
|
|
|
126 |
$q->options->trueanswer = 1;
|
|
|
127 |
$q->options->falseanswer = 2;
|
|
|
128 |
$q->options->answers[1] = (object) array('fraction' => 1);
|
|
|
129 |
$q->options->answers[2] = (object) array('fraction' => 0);
|
|
|
130 |
|
|
|
131 |
$this->assertEquals(array(
|
|
|
132 |
$q->id => array(
|
|
|
133 |
|
|
|
134 |
1 => new question_possible_response(get_string('true', 'qtype_truefalse'), 1),
|
|
|
135 |
null => question_possible_response::no_response()),
|
|
|
136 |
), $this->qtype->get_possible_responses($q));
|
|
|
137 |
}
|
|
|
138 |
|
11 |
efrain |
139 |
public function test_question_saving_true(): void {
|
1 |
efrain |
140 |
$this->resetAfterTest(true);
|
|
|
141 |
$this->setAdminUser();
|
|
|
142 |
|
|
|
143 |
$questiondata = \test_question_maker::get_question_data('truefalse');
|
|
|
144 |
$formdata = \test_question_maker::get_question_form_data('truefalse');
|
|
|
145 |
|
|
|
146 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
147 |
$cat = $generator->create_question_category(array());
|
|
|
148 |
|
|
|
149 |
$formdata->category = "{$cat->id},{$cat->contextid}";
|
|
|
150 |
qtype_truefalse_edit_form::mock_submit((array)$formdata);
|
|
|
151 |
|
|
|
152 |
$form = \qtype_truefalse_test_helper::get_question_editing_form($cat, $questiondata);
|
|
|
153 |
|
|
|
154 |
$this->assertTrue($form->is_validated());
|
|
|
155 |
|
|
|
156 |
$fromform = $form->get_data();
|
|
|
157 |
|
|
|
158 |
$returnedfromsave = $this->qtype->save_question($questiondata, $fromform);
|
|
|
159 |
$actualquestionsdata = question_load_questions([$returnedfromsave->id], 'qbe.idnumber');
|
|
|
160 |
$actualquestiondata = end($actualquestionsdata);
|
|
|
161 |
|
|
|
162 |
foreach ($questiondata as $property => $value) {
|
|
|
163 |
if (!in_array($property, array('options'))) {
|
|
|
164 |
$this->assertEquals($value, $actualquestiondata->$property);
|
|
|
165 |
}
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
foreach ($questiondata->options as $optionname => $value) {
|
|
|
169 |
if (!in_array($optionname, array('trueanswer', 'falseanswer', 'answers'))) {
|
|
|
170 |
$this->assertEquals($value, $actualquestiondata->options->$optionname);
|
|
|
171 |
}
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
$answerindexes = array();
|
|
|
175 |
foreach ($questiondata->options->answers as $ansindex => $answer) {
|
|
|
176 |
$actualanswer = array_shift($actualquestiondata->options->answers);
|
|
|
177 |
foreach ($answer as $ansproperty => $ansvalue) {
|
|
|
178 |
// This question does not use 'answerformat', will ignore it.
|
|
|
179 |
if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) {
|
|
|
180 |
$this->assertEquals($ansvalue, $actualanswer->$ansproperty);
|
|
|
181 |
}
|
|
|
182 |
}
|
|
|
183 |
$answerindexes[$answer->answer] = $ansindex;
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
$this->assertEquals($questiondata->options->trueanswer, $answerindexes['True']);
|
|
|
187 |
$this->assertEquals($questiondata->options->falseanswer, $answerindexes['False']);
|
|
|
188 |
}
|
|
|
189 |
}
|