|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 require_once(dirname(__FILE__) . '/../lib.php'); 00030 require_once(dirname(__FILE__) . '/helpers.php'); 00031 00032 00039 class question_attempt_step_test extends UnitTestCase { 00040 public function test_initial_state_unprocessed() { 00041 $step = new question_attempt_step(); 00042 $this->assertEqual(question_state::$unprocessed, $step->get_state()); 00043 } 00044 00045 public function test_get_set_state() { 00046 $step = new question_attempt_step(); 00047 $step->set_state(question_state::$gradedright); 00048 $this->assertEqual(question_state::$gradedright, $step->get_state()); 00049 } 00050 00051 public function test_initial_fraction_null() { 00052 $step = new question_attempt_step(); 00053 $this->assertNull($step->get_fraction()); 00054 } 00055 00056 public function test_get_set_fraction() { 00057 $step = new question_attempt_step(); 00058 $step->set_fraction(0.5); 00059 $this->assertEqual(0.5, $step->get_fraction()); 00060 } 00061 00062 public function test_has_var() { 00063 $step = new question_attempt_step(array('x' => 1, '-y' => 'frog')); 00064 $this->assertTrue($step->has_qt_var('x')); 00065 $this->assertTrue($step->has_behaviour_var('y')); 00066 $this->assertFalse($step->has_qt_var('y')); 00067 $this->assertFalse($step->has_behaviour_var('x')); 00068 } 00069 00070 public function test_get_var() { 00071 $step = new question_attempt_step(array('x' => 1, '-y' => 'frog')); 00072 $this->assertEqual('1', $step->get_qt_var('x')); 00073 $this->assertEqual('frog', $step->get_behaviour_var('y')); 00074 $this->assertNull($step->get_qt_var('y')); 00075 } 00076 00077 public function test_set_var() { 00078 $step = new question_attempt_step(); 00079 $step->set_qt_var('_x', 1); 00080 $step->set_behaviour_var('_x', 2); 00081 $this->assertEqual('1', $step->get_qt_var('_x')); 00082 $this->assertEqual('2', $step->get_behaviour_var('_x')); 00083 } 00084 00085 public function test_cannot_set_qt_var_without_underscore() { 00086 $step = new question_attempt_step(); 00087 $this->expectException(); 00088 $step->set_qt_var('x', 1); 00089 } 00090 00091 public function test_cannot_set_behaviour_var_without_underscore() { 00092 $step = new question_attempt_step(); 00093 $this->expectException(); 00094 $step->set_behaviour_var('x', 1); 00095 } 00096 00097 public function test_get_data() { 00098 $step = new question_attempt_step(array('x' => 1, '-y' => 'frog', ':flagged' => 1)); 00099 $this->assertEqual(array('x' => '1'), $step->get_qt_data()); 00100 $this->assertEqual(array('y' => 'frog'), $step->get_behaviour_data()); 00101 $this->assertEqual(array('x' => 1, '-y' => 'frog', ':flagged' => 1), $step->get_all_data()); 00102 } 00103 00104 public function test_get_submitted_data() { 00105 $step = new question_attempt_step(array('x' => 1, '-y' => 'frog')); 00106 $step->set_qt_var('_x', 1); 00107 $step->set_behaviour_var('_x', 2); 00108 $this->assertEqual(array('x' => 1, '-y' => 'frog'), $step->get_submitted_data()); 00109 } 00110 00111 public function test_constructor_default_params() { 00112 global $USER; 00113 $step = new question_attempt_step(); 00114 $this->assertWithinMargin(time(), $step->get_timecreated(), 5); 00115 $this->assertEqual($USER->id, $step->get_user_id()); 00116 $this->assertEqual(array(), $step->get_qt_data()); 00117 $this->assertEqual(array(), $step->get_behaviour_data()); 00118 00119 } 00120 00121 public function test_constructor_given_params() { 00122 global $USER; 00123 $step = new question_attempt_step(array(), 123, 5); 00124 $this->assertEqual(123, $step->get_timecreated()); 00125 $this->assertEqual(5, $step->get_user_id()); 00126 $this->assertEqual(array(), $step->get_qt_data()); 00127 $this->assertEqual(array(), $step->get_behaviour_data()); 00128 00129 } 00130 } 00131 00132 00139 class question_attempt_step_db_test extends data_loading_method_test_base { 00140 public function test_load_with_data() { 00141 $records = new test_recordset(array( 00142 array('attemptstepid', 'questionattemptid', 'sequencenumber', 'state', 'fraction', 'timecreated', 'userid', 'name', 'value'), 00143 array( 1, 1, 0, 'todo', null, 1256228502, 13, null, null), 00144 array( 2, 1, 1, 'complete', null, 1256228505, 13, 'x', 'a'), 00145 array( 2, 1, 1, 'complete', null, 1256228505, 13, '_y', '_b'), 00146 array( 2, 1, 1, 'complete', null, 1256228505, 13, '-z', '!c'), 00147 array( 2, 1, 1, 'complete', null, 1256228505, 13, '-_t', '!_d'), 00148 array( 3, 1, 2, 'gradedright', 1.0, 1256228515, 13, '-finish', '1'), 00149 )); 00150 00151 $step = question_attempt_step::load_from_records($records, 2); 00152 $this->assertEqual(question_state::$complete, $step->get_state()); 00153 $this->assertNull($step->get_fraction()); 00154 $this->assertEqual(1256228505, $step->get_timecreated()); 00155 $this->assertEqual(13, $step->get_user_id()); 00156 $this->assertEqual(array('x' => 'a', '_y' => '_b', '-z' => '!c', '-_t' => '!_d'), $step->get_all_data()); 00157 } 00158 00159 public function test_load_without_data() { 00160 $records = new test_recordset(array( 00161 array('attemptstepid', 'questionattemptid', 'sequencenumber', 'state', 'fraction', 'timecreated', 'userid', 'name', 'value'), 00162 array( 2, 1, 1, 'complete', null, 1256228505, 13, null, null), 00163 )); 00164 00165 $step = question_attempt_step::load_from_records($records, 2); 00166 $this->assertEqual(question_state::$complete, $step->get_state()); 00167 $this->assertNull($step->get_fraction()); 00168 $this->assertEqual(1256228505, $step->get_timecreated()); 00169 $this->assertEqual(13, $step->get_user_id()); 00170 $this->assertEqual(array(), $step->get_all_data()); 00171 } 00172 00173 public function test_load_dont_be_too_greedy() { 00174 $records = new test_recordset(array( 00175 array('attemptstepid', 'questionattemptid', 'sequencenumber', 'state', 'fraction', 'timecreated', 'userid', 'name', 'value'), 00176 array( 1, 1, 0, 'todo', null, 1256228502, 13, 'x', 'right'), 00177 array( 2, 2, 0, 'complete', null, 1256228505, 13, 'x', 'wrong'), 00178 )); 00179 00180 $step = question_attempt_step::load_from_records($records, 1); 00181 $this->assertEqual(array('x' => 'right'), $step->get_all_data()); 00182 } 00183 }