|
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_usage_by_activity_test extends UnitTestCase { 00040 00041 public function test_set_get_preferred_model() { 00042 // Set up 00043 $quba = question_engine::make_questions_usage_by_activity('unit_test', 00044 get_context_instance(CONTEXT_SYSTEM)); 00045 00046 // Exercise SUT and verify. 00047 $quba->set_preferred_behaviour('deferredfeedback'); 00048 $this->assertEqual('deferredfeedback', $quba->get_preferred_behaviour()); 00049 } 00050 00051 public function test_set_get_id() { 00052 // Set up 00053 $quba = question_engine::make_questions_usage_by_activity('unit_test', 00054 get_context_instance(CONTEXT_SYSTEM)); 00055 00056 // Exercise SUT and verify 00057 $quba->set_id_from_database(123); 00058 $this->assertEqual(123, $quba->get_id()); 00059 } 00060 00061 public function test_fake_id() { 00062 // Set up 00063 $quba = question_engine::make_questions_usage_by_activity('unit_test', 00064 get_context_instance(CONTEXT_SYSTEM)); 00065 00066 // Exercise SUT and verify 00067 $this->assertTrue($quba->get_id()); 00068 } 00069 00070 public function test_create_usage_and_add_question() { 00071 // Exercise SUT 00072 $context = get_context_instance(CONTEXT_SYSTEM); 00073 $quba = question_engine::make_questions_usage_by_activity('unit_test', $context); 00074 $quba->set_preferred_behaviour('deferredfeedback'); 00075 $tf = test_question_maker::make_question('truefalse', 'true'); 00076 $slot = $quba->add_question($tf); 00077 00078 // Verify. 00079 $this->assertEqual($slot, 1); 00080 $this->assertEqual('unit_test', $quba->get_owning_component()); 00081 $this->assertIdentical($context, $quba->get_owning_context()); 00082 $this->assertEqual($quba->question_count(), 1); 00083 $this->assertEqual($quba->get_question_state($slot), question_state::$notstarted); 00084 } 00085 00086 public function test_get_question() { 00087 // Set up. 00088 $quba = question_engine::make_questions_usage_by_activity('unit_test', 00089 get_context_instance(CONTEXT_SYSTEM)); 00090 $quba->set_preferred_behaviour('deferredfeedback'); 00091 $tf = test_question_maker::make_question('truefalse', 'true'); 00092 $slot = $quba->add_question($tf); 00093 00094 // Exercise SUT and verify. 00095 $this->assertIdentical($tf, $quba->get_question($slot)); 00096 00097 $this->expectException(); 00098 $quba->get_question($slot + 1); 00099 } 00100 00101 public function test_extract_responses() { 00102 // Start a deferred feedback attempt with CBM and add the question to it. 00103 $tf = test_question_maker::make_question('truefalse', 'true'); 00104 $quba = question_engine::make_questions_usage_by_activity('unit_test', 00105 get_context_instance(CONTEXT_SYSTEM)); 00106 $quba->set_preferred_behaviour('deferredcbm'); 00107 $slot = $quba->add_question($tf); 00108 $quba->start_all_questions(); 00109 00110 // Prepare data to be submitted 00111 $prefix = $quba->get_field_prefix($slot); 00112 $answername = $prefix . 'answer'; 00113 $certaintyname = $prefix . '-certainty'; 00114 $getdata = array( 00115 $answername => 1, 00116 $certaintyname => 3, 00117 'irrelevant' => 'should be ignored', 00118 ); 00119 00120 // Exercise SUT 00121 $submitteddata = $quba->extract_responses($slot, $getdata); 00122 00123 // Verify. 00124 $this->assertEqual(array('answer' => 1, '-certainty' => 3), $submitteddata); 00125 } 00126 00127 public function test_access_out_of_sequence_throws_exception() { 00128 // Start a deferred feedback attempt with CBM and add the question to it. 00129 $tf = test_question_maker::make_question('truefalse', 'true'); 00130 $quba = question_engine::make_questions_usage_by_activity('unit_test', 00131 get_context_instance(CONTEXT_SYSTEM)); 00132 $quba->set_preferred_behaviour('deferredcbm'); 00133 $slot = $quba->add_question($tf); 00134 $quba->start_all_questions(); 00135 00136 // Prepare data to be submitted 00137 $prefix = $quba->get_field_prefix($slot); 00138 $answername = $prefix . 'answer'; 00139 $certaintyname = $prefix . '-certainty'; 00140 $postdata = array( 00141 $answername => 1, 00142 $certaintyname => 3, 00143 $prefix . ':sequencecheck' => 1, 00144 'irrelevant' => 'should be ignored', 00145 ); 00146 00147 // Exercise SUT - no exception yet. 00148 $quba->process_all_actions($slot, $postdata); 00149 00150 $postdata = array( 00151 $answername => 1, 00152 $certaintyname => 3, 00153 $prefix . ':sequencecheck' => 3, 00154 'irrelevant' => 'should be ignored', 00155 ); 00156 00157 // Exercise SUT - now it should fail. 00158 $this->expectException('question_out_of_sequence_exception'); 00159 $quba->process_all_actions($slot, $postdata); 00160 } 00161 } 00162 00169 class question_usage_db_test extends data_loading_method_test_base { 00170 public function test_load() { 00171 $records = new test_recordset(array( 00172 array('qubaid', 'contextid', 'component', 'preferredbehaviour', 00173 'questionattemptid', 'contextid', 'questionusageid', 'slot', 00174 'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'flagged', 00175 'questionsummary', 'rightanswer', 'responsesummary', 'timemodified', 00176 'attemptstepid', 'sequencenumber', 'state', 'fraction', 00177 'timecreated', 'userid', 'name', 'value'), 00178 array(1, 1, 'unit_test', 'interactive', 1, 123, 1, 1, 'interactive', -1, 1, 2.0000000, 0.0000000, 0, '', '', '', 1256233790, 1, 0, 'todo', null, 1256233700, 1, null, null), 00179 array(1, 1, 'unit_test', 'interactive', 1, 123, 1, 1, 'interactive', -1, 1, 2.0000000, 0.0000000, 0, '', '', '', 1256233790, 2, 1, 'todo', null, 1256233705, 1, 'answer', '1'), 00180 array(1, 1, 'unit_test', 'interactive', 1, 123, 1, 1, 'interactive', -1, 1, 2.0000000, 0.0000000, 0, '', '', '', 1256233790, 5, 2, 'gradedright', 1.0000000, 1256233720, 1, '-finish', '1'), 00181 )); 00182 00183 $question = test_question_maker::make_question('truefalse', 'true'); 00184 $question->id = -1; 00185 00186 question_bank::start_unit_test(); 00187 question_bank::load_test_question_data($question); 00188 $quba = question_usage_by_activity::load_from_records($records, 1); 00189 question_bank::end_unit_test(); 00190 00191 $this->assertEqual('unit_test', $quba->get_owning_component()); 00192 $this->assertEqual(1, $quba->get_id()); 00193 $this->assertIsA($quba->get_observer(), 'question_engine_unit_of_work'); 00194 $this->assertEqual('interactive', $quba->get_preferred_behaviour()); 00195 00196 $qa = $quba->get_question_attempt(1); 00197 00198 $this->assertEqual($question->questiontext, $qa->get_question()->questiontext); 00199 00200 $this->assertEqual(3, $qa->get_num_steps()); 00201 00202 $step = $qa->get_step(0); 00203 $this->assertEqual(question_state::$todo, $step->get_state()); 00204 $this->assertNull($step->get_fraction()); 00205 $this->assertEqual(1256233700, $step->get_timecreated()); 00206 $this->assertEqual(1, $step->get_user_id()); 00207 $this->assertEqual(array(), $step->get_all_data()); 00208 00209 $step = $qa->get_step(1); 00210 $this->assertEqual(question_state::$todo, $step->get_state()); 00211 $this->assertNull($step->get_fraction()); 00212 $this->assertEqual(1256233705, $step->get_timecreated()); 00213 $this->assertEqual(1, $step->get_user_id()); 00214 $this->assertEqual(array('answer' => '1'), $step->get_all_data()); 00215 00216 $step = $qa->get_step(2); 00217 $this->assertEqual(question_state::$gradedright, $step->get_state()); 00218 $this->assertEqual(1, $step->get_fraction()); 00219 $this->assertEqual(1256233720, $step->get_timecreated()); 00220 $this->assertEqual(1, $step->get_user_id()); 00221 $this->assertEqual(array('-finish' => '1'), $step->get_all_data()); 00222 } 00223 }