|
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($CFG->dirroot . '/question/engine/simpletest/helpers.php'); 00030 00031 00038 class qtype_multichoice_single_question_test extends UnitTestCase { 00039 00040 public function test_get_expected_data() { 00041 $question = test_question_maker::make_a_multichoice_single_question(); 00042 $this->assertEqual(array('answer' => PARAM_INT), $question->get_expected_data()); 00043 } 00044 00045 public function test_is_complete_response() { 00046 $question = test_question_maker::make_a_multichoice_single_question(); 00047 00048 $this->assertFalse($question->is_complete_response(array())); 00049 $this->assertTrue($question->is_complete_response(array('answer' => '0'))); 00050 $this->assertTrue($question->is_complete_response(array('answer' => '2'))); 00051 } 00052 00053 public function test_is_gradable_response() { 00054 $question = test_question_maker::make_a_multichoice_single_question(); 00055 00056 $this->assertFalse($question->is_gradable_response(array())); 00057 $this->assertTrue($question->is_gradable_response(array('answer' => '0'))); 00058 $this->assertTrue($question->is_gradable_response(array('answer' => '2'))); 00059 } 00060 00061 public function test_grading() { 00062 $question = test_question_maker::make_a_multichoice_single_question(); 00063 $question->shuffleanswers = false; 00064 $question->start_attempt(new question_attempt_step(), 1); 00065 00066 $this->assertEqual(array(1, question_state::$gradedright), 00067 $question->grade_response(array('answer' => 0))); 00068 $this->assertEqual(array(-0.3333333, question_state::$gradedwrong), 00069 $question->grade_response(array('answer' => 1))); 00070 $this->assertEqual(array(-0.3333333, question_state::$gradedwrong), 00071 $question->grade_response(array('answer' => 2))); 00072 } 00073 00074 public function test_grading_rounding_three_right() { 00075 question_bank::load_question_definition_classes('multichoice'); 00076 $mc = new qtype_multichoice_multi_question(); 00077 test_question_maker::initialise_a_question($mc); 00078 $mc->name = 'Odd numbers'; 00079 $mc->questiontext = 'Which are the odd numbers?'; 00080 $mc->generalfeedback = '1, 3 and 5 are the odd numbers.'; 00081 $mc->qtype = question_bank::get_qtype('multichoice'); 00082 00083 $mc->shuffleanswers = 0; 00084 $mc->answernumbering = 'abc'; 00085 00086 test_question_maker::set_standard_combined_feedback_fields($mc); 00087 00088 $mc->answers = array( 00089 11 => new question_answer(11, '1', 0.3333333, '', FORMAT_HTML), 00090 12 => new question_answer(12, '2', -1, '', FORMAT_HTML), 00091 13 => new question_answer(13, '3', 0.3333333, '', FORMAT_HTML), 00092 14 => new question_answer(14, '4', -1, '', FORMAT_HTML), 00093 15 => new question_answer(15, '5', 0.3333333, '', FORMAT_HTML), 00094 16 => new question_answer(16, '6', -1, '', FORMAT_HTML), 00095 ); 00096 00097 $mc->start_attempt(new question_attempt_step(), 1); 00098 00099 list($grade, $state) = $mc->grade_response( 00100 array('choice0' => 1, 'choice2' => 1, 'choice4' => 1)); 00101 $this->assertWithinMargin(1, $grade, 0.000001); 00102 $this->assertEqual(question_state::$gradedright, $state); 00103 } 00104 00105 public function test_get_correct_response() { 00106 $question = test_question_maker::make_a_multichoice_single_question(); 00107 $question->shuffleanswers = false; 00108 $question->start_attempt(new question_attempt_step(), 1); 00109 00110 $this->assertEqual(array('answer' => 0), 00111 $question->get_correct_response()); 00112 } 00113 00114 public function test_summarise_response() { 00115 $mc = test_question_maker::make_a_multichoice_single_question(); 00116 $mc->shuffleanswers = false; 00117 $mc->start_attempt(new question_attempt_step(), 1); 00118 00119 $summary = $mc->summarise_response(array('answer' => 0), 00120 test_question_maker::get_a_qa($mc)); 00121 00122 $this->assertEqual('A', $summary); 00123 } 00124 00125 public function test_classify_response() { 00126 $mc = test_question_maker::make_a_multichoice_single_question(); 00127 $mc->shuffleanswers = false; 00128 $mc->start_attempt(new question_attempt_step(), 1); 00129 00130 $this->assertEqual(array( 00131 $mc->id => new question_classified_response(14, 'B', -0.3333333), 00132 ), $mc->classify_response(array('answer' => 1))); 00133 00134 $this->assertEqual(array( 00135 $mc->id => question_classified_response::no_response(), 00136 ), $mc->classify_response(array())); 00137 } 00138 00139 public function test_make_html_inline() { 00140 $mc = test_question_maker::make_a_multichoice_single_question(); 00141 $this->assertEqual('Frog', $mc->make_html_inline('<p>Frog</p>')); 00142 $this->assertEqual('Frog<br />Toad', $mc->make_html_inline("<p>Frog</p>\n<p>Toad</p>")); 00143 $this->assertEqual('<img src="http://example.com/pic.png" alt="Graph" />', 00144 $mc->make_html_inline( 00145 '<p><img src="http://example.com/pic.png" alt="Graph" /></p>')); 00146 $this->assertEqual("Frog<br />XXX <img src='http://example.com/pic.png' alt='Graph' />", 00147 $mc->make_html_inline(" <p> Frog </p> \n\r 00148 <p> XXX <img src='http://example.com/pic.png' alt='Graph' /> </p> ")); 00149 } 00150 } 00151 00152 00159 class qtype_multichoice_multi_question_test extends UnitTestCase { 00160 00161 public function test_get_expected_data() { 00162 $question = test_question_maker::make_a_multichoice_multi_question(); 00163 $question->start_attempt(new question_attempt_step(), 1); 00164 00165 $this->assertEqual(array('choice0' => PARAM_BOOL, 'choice1' => PARAM_BOOL, 00166 'choice2' => PARAM_BOOL, 'choice3' => PARAM_BOOL), $question->get_expected_data()); 00167 } 00168 00169 public function test_is_complete_response() { 00170 $question = test_question_maker::make_a_multichoice_multi_question(); 00171 $question->start_attempt(new question_attempt_step(), 1); 00172 00173 $this->assertFalse($question->is_complete_response(array())); 00174 $this->assertFalse($question->is_complete_response( 00175 array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0'))); 00176 $this->assertTrue($question->is_complete_response(array('choice1' => '1'))); 00177 $this->assertTrue($question->is_complete_response( 00178 array('choice0' => '1', 'choice1' => '1', 'choice2' => '1', 'choice3' => '1'))); 00179 } 00180 00181 public function test_is_gradable_response() { 00182 $question = test_question_maker::make_a_multichoice_multi_question(); 00183 $question->start_attempt(new question_attempt_step(), 1); 00184 00185 $this->assertFalse($question->is_gradable_response(array())); 00186 $this->assertFalse($question->is_gradable_response( 00187 array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0'))); 00188 $this->assertTrue($question->is_gradable_response(array('choice1' => '1'))); 00189 $this->assertTrue($question->is_gradable_response( 00190 array('choice0' => '1', 'choice1' => '1', 'choice2' => '1', 'choice3' => '1'))); 00191 } 00192 00193 public function test_grading() { 00194 $question = test_question_maker::make_a_multichoice_multi_question(); 00195 $question->shuffleanswers = false; 00196 $question->start_attempt(new question_attempt_step(), 1); 00197 00198 $this->assertEqual(array(1, question_state::$gradedright), 00199 $question->grade_response(array('choice0' => '1', 'choice2' => '1'))); 00200 $this->assertEqual(array(0.5, question_state::$gradedpartial), 00201 $question->grade_response(array('choice0' => '1'))); 00202 $this->assertEqual(array(0, question_state::$gradedwrong), 00203 $question->grade_response( 00204 array('choice0' => '1', 'choice1' => '1', 'choice2' => '1'))); 00205 $this->assertEqual(array(0, question_state::$gradedwrong), 00206 $question->grade_response(array('choice1' => '1'))); 00207 } 00208 00209 public function test_get_correct_response() { 00210 $question = test_question_maker::make_a_multichoice_multi_question(); 00211 $question->shuffleanswers = false; 00212 $question->start_attempt(new question_attempt_step(), 1); 00213 00214 $this->assertEqual(array('choice0' => '1', 'choice2' => '1'), 00215 $question->get_correct_response()); 00216 } 00217 00218 public function test_get_question_summary() { 00219 $mc = test_question_maker::make_a_multichoice_single_question(); 00220 $mc->start_attempt(new question_attempt_step(), 1); 00221 00222 $qsummary = $mc->get_question_summary(); 00223 00224 $this->assertPattern('/' . preg_quote($mc->questiontext) . '/', $qsummary); 00225 foreach ($mc->answers as $answer) { 00226 $this->assertPattern('/' . preg_quote($answer->answer) . '/', $qsummary); 00227 } 00228 } 00229 00230 public function test_summarise_response() { 00231 $mc = test_question_maker::make_a_multichoice_multi_question(); 00232 $mc->shuffleanswers = false; 00233 $mc->start_attempt(new question_attempt_step(), 1); 00234 00235 $summary = $mc->summarise_response(array('choice1' => 1, 'choice2' => 1), 00236 test_question_maker::get_a_qa($mc)); 00237 00238 $this->assertEqual('B; C', $summary); 00239 } 00240 00241 public function test_classify_response() { 00242 $mc = test_question_maker::make_a_multichoice_multi_question(); 00243 $mc->shuffleanswers = false; 00244 $mc->start_attempt(new question_attempt_step(), 1); 00245 00246 $this->assertEqual(array( 00247 13 => new question_classified_response(13, 'A', 0.5), 00248 14 => new question_classified_response(14, 'B', -1.0), 00249 ), $mc->classify_response(array('choice0' => 1, 'choice1' => 1))); 00250 00251 $this->assertEqual(array(), $mc->classify_response(array())); 00252 } 00253 }