MDL-32323 convert question tests
[moodle.git] / question / engine / tests / unitofwork_test.php
CommitLineData
603bd001
PS
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 * This file contains tests for the question_engine_unit_of_work class.
19 *
20 * @package moodlecore
21 * @subpackage questionengine
22 * @copyright 2012 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30require_once(dirname(__FILE__) . '/../lib.php');
31require_once(dirname(__FILE__) . '/helpers.php');
32
33
34/**
35 * Test subclass to allow access to some protected data so that the correct
36 * behaviour can be verified.
37 *
38 * @copyright 2012 The Open University
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41class testable_question_engine_unit_of_work extends question_engine_unit_of_work {
42 public function get_modified() {
43 return $this->modified;
44 }
45
46 public function get_attempts_added() {
47 return $this->attemptsadded;
48 }
49
50 public function get_attempts_modified() {
51 return $this->attemptsmodified;
52 }
53
54 public function get_steps_added() {
55 return $this->stepsadded;
56 }
57
58 public function get_steps_modified() {
59 return $this->stepsmodified;
60 }
61
62 public function get_steps_deleted() {
63 return $this->stepsdeleted;
64 }
65}
66
67
68/**
69 * Unit tests for the {@link question_engine_unit_of_work} class.
70 *
71 * @copyright 2012 The Open University
72 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
73 */
74class question_engine_unit_of_work_test extends data_loading_method_test_base {
75 /** @var question_usage_by_activity the test question usage. */
76 protected $quba;
77
78 /** @var int the slot number of the one qa in the test usage.*/
79 protected $slot;
80
81 /** @var testable_question_engine_unit_of_work the unit of work we are testing. */
82 protected $observer;
83
84 protected function setUp() {
85 // Create a usage in an initial state, with one shortanswer question added,
86 // and attempted in interactive mode submitted responses 'toad' then 'frog'.
87 // Then set it to use a new unit of work for any subsequent changes.
88 // Create a short answer question.
89 $question = test_question_maker::make_question('shortanswer');
90 $question->hints = array(
91 new question_hint(0, 'This is the first hint.', FORMAT_HTML),
92 new question_hint(0, 'This is the second hint.', FORMAT_HTML),
93 );
94 $question->id = -1;
95 question_bank::start_unit_test();
96 question_bank::load_test_question_data($question);
97
98 $this->setup_initial_test_state($this->get_test_data());
99 }
100
101 public function testDown() {
102 question_bank::end_unit_test();
103 }
104
105 protected function setup_initial_test_state($testdata) {
106 $records = new question_test_recordset($testdata);
107
108 $this->quba = question_usage_by_activity::load_from_records($records, 1);
109
110 $this->slot = 1;
111 $this->observer = new testable_question_engine_unit_of_work($this->quba);
112 $this->quba->set_observer($this->observer);
113 }
114
115 protected function get_test_data() {
116 return array(
117 array('qubaid', 'contextid', 'component', 'preferredbehaviour',
118 'questionattemptid', 'contextid', 'questionusageid', 'slot',
119 'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'flagged',
120 'questionsummary', 'rightanswer', 'responsesummary', 'timemodified',
121 'attemptstepid', 'sequencenumber', 'state', 'fraction',
122 'timecreated', 'userid', 'name', 'value'),
123 array(1, 1, 'unit_test', 'interactive', 1, 123, 1, 1, 'interactive', -1, 1, 1.0000000, 0.0000000, 0, '', '', '', 1256233790, 1, 0, 'todo', null, 1256233700, 1, '-_triesleft', 3),
124 array(1, 1, 'unit_test', 'interactive', 1, 123, 1, 1, 'interactive', -1, 1, 1.0000000, 0.0000000, 0, '', '', '', 1256233790, 2, 1, 'todo', null, 1256233720, 1, 'answer', 'toad'),
125 array(1, 1, 'unit_test', 'interactive', 1, 123, 1, 1, 'interactive', -1, 1, 1.0000000, 0.0000000, 0, '', '', '', 1256233790, 2, 1, 'todo', null, 1256233720, 1, '-submit', 1),
126 array(1, 1, 'unit_test', 'interactive', 1, 123, 1, 1, 'interactive', -1, 1, 1.0000000, 0.0000000, 0, '', '', '', 1256233790, 2, 1, 'todo', null, 1256233720, 1, '-_triesleft', 1),
127 array(1, 1, 'unit_test', 'interactive', 1, 123, 1, 1, 'interactive', -1, 1, 1.0000000, 0.0000000, 0, '', '', '', 1256233790, 3, 2, 'todo', null, 1256233740, 1, '-tryagain', 1),
128 array(1, 1, 'unit_test', 'interactive', 1, 123, 1, 1, 'interactive', -1, 1, 1.0000000, 0.0000000, 0, '', '', '', 1256233790, 5, 3, 'gradedright', null, 1256233790, 1, 'answer', 'frog'),
129 array(1, 1, 'unit_test', 'interactive', 1, 123, 1, 1, 'interactive', -1, 1, 1.0000000, 0.0000000, 0, '', '', '', 1256233790, 5, 3, 'gradedright', 1.0000000, 1256233790, 1, '-finish', 1),
130 );
131 }
132
133 public function test_initial_state() {
134 $this->assertFalse($this->observer->get_modified());
135 $this->assertEquals(0, count($this->observer->get_attempts_added()));
136 $this->assertEquals(0, count($this->observer->get_attempts_modified()));
137 $this->assertEquals(0, count($this->observer->get_steps_added()));
138 $this->assertEquals(0, count($this->observer->get_steps_modified()));
139 $this->assertEquals(0, count($this->observer->get_steps_deleted()));
140 }
141
142 public function test_update_usage() {
143
144 $this->quba->set_preferred_behaviour('deferredfeedback');
145
146 $this->assertTrue($this->observer->get_modified());
147 }
148
149 public function test_add_question() {
150
151 $slot = $this->quba->add_question(test_question_maker::make_question('truefalse'));
152
153 $newattempts = $this->observer->get_attempts_added();
154 $this->assertEquals(1, count($newattempts));
155 $this->assertTrue($this->quba->get_question_attempt($slot) === reset($newattempts));
156 $this->assertSame($slot, key($newattempts));
157 }
158
159 public function test_add_and_start_question() {
160
161 $slot = $this->quba->add_question(test_question_maker::make_question('truefalse'));
162 $this->quba->start_question($slot);
163
164 // The point here is that, although we have added a step, it is not listed
165 // separately becuase it is part of a newly added attempt, and all steps
166 // for a newly added attempt are automatically added to the DB, so it does
167 // not need to be tracked separately.
168 $newattempts = $this->observer->get_attempts_added();
169 $this->assertEquals(1, count($newattempts));
170 $this->assertTrue($this->quba->get_question_attempt($slot) === reset($newattempts));
171 $this->assertSame($slot, key($newattempts));
172 $this->assertEquals(0, count($this->observer->get_steps_added()));
173 }
174
175 public function test_process_action() {
176
177 $this->quba->manual_grade($this->slot, 'Acutally, that is not quite right', 0.5);
178
179 // Here, however, were we are adding a step to an existing qa, we do need to track that.
180 $this->assertEquals(0, count($this->observer->get_attempts_added()));
181
182 $updatedattempts = $this->observer->get_attempts_modified();
183 $this->assertEquals(1, count($updatedattempts));
184
185 $updatedattempt = reset($updatedattempts);
186 $this->assertTrue($this->quba->get_question_attempt($this->slot) === $updatedattempt);
187 $this->assertSame($this->slot, key($updatedattempts));
188
189 $newsteps = $this->observer->get_steps_added();
190 $this->assertEquals(1, count($newsteps));
191
192 list($newstep, $qaid, $seq) = reset($newsteps);
193 $this->assertSame($this->quba->get_question_attempt($this->slot)->get_last_step(), $newstep);
194 }
195
196 public function test_regrade_same_steps() {
197
198 // Change the question in a minor way and regrade.
199 if (!isset($this->quba->get_question($this->slot)->answer)) {
200 $this->quba->get_question($this->slot)->answer = array();
201 }
202 if (!isset($this->quba->get_question($this->slot)->answer[14])) {
203 $this->quba->get_question($this->slot)->answer[14] = new stdClass();
204 }
205 $this->quba->get_question($this->slot)->answer[14]->fraction = 0.5;
206 $this->quba->regrade_all_questions();
207
208 // Here, the qa, and all the steps, should be marked as updated.
209 // Here, however, were we are adding a step to an existing qa, we do need to track that.
210 $this->assertEquals(0, count($this->observer->get_attempts_added()));
211 $this->assertEquals(0, count($this->observer->get_steps_added()));
212 $this->assertEquals(0, count($this->observer->get_steps_deleted()));
213
214 $updatedattempts = $this->observer->get_attempts_modified();
215 $this->assertEquals(1, count($updatedattempts));
216
217 $updatedattempt = reset($updatedattempts);
218 $this->assertTrue($this->quba->get_question_attempt($this->slot) === $updatedattempt);
219
220 $updatedsteps = $this->observer->get_steps_modified();
221 $this->assertEquals($updatedattempt->get_num_steps(), count($updatedsteps));
222
223 foreach ($updatedattempt->get_step_iterator() as $seq => $step) {
224 $this->assertSame(array($step, $updatedattempt->get_database_id(), $seq),
225 $updatedsteps[$seq]);
226 }
227 }
228
229 public function test_regrade_losing_steps() {
230
231 // Change the question so that 'toad' is also right, and regrade. This
232 // will mean that the try again, and second try states are no longer
233 // needed, so they should be dropped.
234 $this->quba->get_question($this->slot)->answers[14]->fraction = 1;
235 $this->quba->regrade_all_questions();
236
237 $this->assertEquals(0, count($this->observer->get_attempts_added()));
238 $this->assertEquals(0, count($this->observer->get_steps_added()));
239
240 $updatedattempts = $this->observer->get_attempts_modified();
241 $this->assertEquals(1, count($updatedattempts));
242
243 $updatedattempt = reset($updatedattempts);
244 $this->assertTrue($this->quba->get_question_attempt($this->slot) === $updatedattempt);
245
246 $updatedsteps = $this->observer->get_steps_modified();
247 $this->assertEquals($updatedattempt->get_num_steps(), count($updatedsteps));
248
249 foreach ($updatedattempt->get_step_iterator() as $seq => $step) {
250 $this->assertSame(array($step, $updatedattempt->get_database_id(), $seq),
251 $updatedsteps[$seq]);
252 }
253
254 $deletedsteps = $this->observer->get_steps_deleted();
255 $this->assertEquals(2, count($deletedsteps));
256
257 $firstdeletedstep = reset($deletedsteps);
258 $this->assertEquals(array('-tryagain' => 1), $firstdeletedstep->get_all_data());
259
260 $seconddeletedstep = end($deletedsteps);
261 $this->assertEquals(array('answer' => 'frog', '-finish' => 1),
262 $seconddeletedstep->get_all_data());
263 }
264
265 public function test_tricky_regrade() {
266
267 // The tricky thing here is that we take a half-complete question-attempt,
268 // and then as one transaction, we submit some more responses, and then
269 // change the question attempt as in test_regrade_losing_steps, and regrade
270 // before the steps are even written to the database the first time.
271 $somedata = $this->get_test_data();
272 $somedata = array_slice($somedata, 0, 5);
273 $this->setup_initial_test_state($somedata);
274
275 $this->quba->process_action($this->slot, array('-tryagain' => 1));
276 $this->quba->process_action($this->slot, array('answer' => 'frog', '-submit' => 1));
277 $this->quba->finish_all_questions();
278
279 $this->quba->get_question($this->slot)->answers[14]->fraction = 1;
280 $this->quba->regrade_all_questions();
281
282 $this->assertEquals(0, count($this->observer->get_attempts_added()));
283
284 $updatedattempts = $this->observer->get_attempts_modified();
285 $this->assertEquals(1, count($updatedattempts));
286
287 $updatedattempt = reset($updatedattempts);
288 $this->assertTrue($this->quba->get_question_attempt($this->slot) === $updatedattempt);
289
290 $this->assertEquals(0, count($this->observer->get_steps_added()));
291
292 $updatedsteps = $this->observer->get_steps_modified();
293 $this->assertEquals($updatedattempt->get_num_steps(), count($updatedsteps));
294
295 foreach ($updatedattempt->get_step_iterator() as $seq => $step) {
296 $this->assertSame(array($step, $updatedattempt->get_database_id(), $seq),
297 $updatedsteps[$seq]);
298 }
299
300 $this->assertEquals(0, count($this->observer->get_steps_deleted()));
301 }
302}