MDL-32323 convert question tests
[moodle.git] / question / engine / tests / questionattemptstep_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_attempt_step class.
19 *
20 * @package moodlecore
21 * @subpackage questionengine
22 * @copyright 2009 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 * Unit tests for the {@link question_attempt_step} class.
36 *
37 * @copyright 2009 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class question_attempt_step_test extends advanced_testcase {
41 public function test_initial_state_unprocessed() {
42 $step = new question_attempt_step();
43 $this->assertEquals(question_state::$unprocessed, $step->get_state());
44 }
45
46 public function test_get_set_state() {
47 $step = new question_attempt_step();
48 $step->set_state(question_state::$gradedright);
49 $this->assertEquals(question_state::$gradedright, $step->get_state());
50 }
51
52 public function test_initial_fraction_null() {
53 $step = new question_attempt_step();
54 $this->assertNull($step->get_fraction());
55 }
56
57 public function test_get_set_fraction() {
58 $step = new question_attempt_step();
59 $step->set_fraction(0.5);
60 $this->assertEquals(0.5, $step->get_fraction());
61 }
62
63 public function test_has_var() {
64 $step = new question_attempt_step(array('x' => 1, '-y' => 'frog'));
65 $this->assertTrue($step->has_qt_var('x'));
66 $this->assertTrue($step->has_behaviour_var('y'));
67 $this->assertFalse($step->has_qt_var('y'));
68 $this->assertFalse($step->has_behaviour_var('x'));
69 }
70
71 public function test_get_var() {
72 $step = new question_attempt_step(array('x' => 1, '-y' => 'frog'));
73 $this->assertEquals('1', $step->get_qt_var('x'));
74 $this->assertEquals('frog', $step->get_behaviour_var('y'));
75 $this->assertNull($step->get_qt_var('y'));
76 }
77
78 public function test_set_var() {
79 $step = new question_attempt_step();
80 $step->set_qt_var('_x', 1);
81 $step->set_behaviour_var('_x', 2);
82 $this->assertEquals('1', $step->get_qt_var('_x'));
83 $this->assertEquals('2', $step->get_behaviour_var('_x'));
84 }
85
86 public function test_cannot_set_qt_var_without_underscore() {
87 $step = new question_attempt_step();
88 $this->setExpectedException('moodle_exception');
89 $step->set_qt_var('x', 1);
90 }
91
92 public function test_cannot_set_behaviour_var_without_underscore() {
93 $step = new question_attempt_step();
94 $this->setExpectedException('moodle_exception');
95 $step->set_behaviour_var('x', 1);
96 }
97
98 public function test_get_data() {
99 $step = new question_attempt_step(array('x' => 1, '-y' => 'frog', ':flagged' => 1));
100 $this->assertEquals(array('x' => '1'), $step->get_qt_data());
101 $this->assertEquals(array('y' => 'frog'), $step->get_behaviour_data());
102 $this->assertEquals(array('x' => 1, '-y' => 'frog', ':flagged' => 1), $step->get_all_data());
103 }
104
105 public function test_get_submitted_data() {
106 $step = new question_attempt_step(array('x' => 1, '-y' => 'frog'));
107 $step->set_qt_var('_x', 1);
108 $step->set_behaviour_var('_x', 2);
109 $this->assertEquals(array('x' => 1, '-y' => 'frog'), $step->get_submitted_data());
110 }
111
112 public function test_constructor_default_params() {
113 global $USER;
114 $step = new question_attempt_step();
115 $this->assertEquals(time(), $step->get_timecreated(), '', 5);
116 $this->assertEquals($USER->id, $step->get_user_id());
117 $this->assertEquals(array(), $step->get_qt_data());
118 $this->assertEquals(array(), $step->get_behaviour_data());
119
120 }
121
122 public function test_constructor_given_params() {
123 global $USER;
124 $step = new question_attempt_step(array(), 123, 5);
125 $this->assertEquals(123, $step->get_timecreated());
126 $this->assertEquals(5, $step->get_user_id());
127 $this->assertEquals(array(), $step->get_qt_data());
128 $this->assertEquals(array(), $step->get_behaviour_data());
129
130 }
131}
132
133
134/**
135 * Unit tests for the loading data into the {@link question_attempt_step} class.
136 *
137 * @copyright 2009 The Open University
138 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
139 */
140class question_attempt_step_db_test extends data_loading_method_test_base {
141 public function test_load_with_data() {
142 $records = new question_test_recordset(array(
143 array('attemptstepid', 'questionattemptid', 'sequencenumber', 'state', 'fraction', 'timecreated', 'userid', 'name', 'value'),
144 array( 1, 1, 0, 'todo', null, 1256228502, 13, null, null),
145 array( 2, 1, 1, 'complete', null, 1256228505, 13, 'x', 'a'),
146 array( 2, 1, 1, 'complete', null, 1256228505, 13, '_y', '_b'),
147 array( 2, 1, 1, 'complete', null, 1256228505, 13, '-z', '!c'),
148 array( 2, 1, 1, 'complete', null, 1256228505, 13, '-_t', '!_d'),
149 array( 3, 1, 2, 'gradedright', 1.0, 1256228515, 13, '-finish', '1'),
150 ));
151
152 $step = question_attempt_step::load_from_records($records, 2);
153 $this->assertEquals(question_state::$complete, $step->get_state());
154 $this->assertNull($step->get_fraction());
155 $this->assertEquals(1256228505, $step->get_timecreated());
156 $this->assertEquals(13, $step->get_user_id());
157 $this->assertEquals(array('x' => 'a', '_y' => '_b', '-z' => '!c', '-_t' => '!_d'), $step->get_all_data());
158 }
159
160 public function test_load_without_data() {
161 $records = new question_test_recordset(array(
162 array('attemptstepid', 'questionattemptid', 'sequencenumber', 'state', 'fraction', 'timecreated', 'userid', 'name', 'value'),
163 array( 2, 1, 1, 'complete', null, 1256228505, 13, null, null),
164 ));
165
166 $step = question_attempt_step::load_from_records($records, 2);
167 $this->assertEquals(question_state::$complete, $step->get_state());
168 $this->assertNull($step->get_fraction());
169 $this->assertEquals(1256228505, $step->get_timecreated());
170 $this->assertEquals(13, $step->get_user_id());
171 $this->assertEquals(array(), $step->get_all_data());
172 }
173
174 public function test_load_dont_be_too_greedy() {
175 $records = new question_test_recordset(array(
176 array('attemptstepid', 'questionattemptid', 'sequencenumber', 'state', 'fraction', 'timecreated', 'userid', 'name', 'value'),
177 array( 1, 1, 0, 'todo', null, 1256228502, 13, 'x', 'right'),
178 array( 2, 2, 0, 'complete', null, 1256228505, 13, 'x', 'wrong'),
179 ));
180
181 $step = question_attempt_step::load_from_records($records, 1);
182 $this->assertEquals(array('x' => 'right'), $step->get_all_data());
183 }
184}