|
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_match_question_test extends UnitTestCase { 00039 00040 public function test_get_expected_data() { 00041 $question = test_question_maker::make_a_matching_question(); 00042 $question->start_attempt(new question_attempt_step(), 1); 00043 00044 $this->assertEqual(array('sub0' => PARAM_INT, 'sub1' => PARAM_INT, 00045 'sub2' => PARAM_INT, 'sub3' => PARAM_INT), $question->get_expected_data()); 00046 } 00047 00048 public function test_is_complete_response() { 00049 $question = test_question_maker::make_a_matching_question(); 00050 $question->start_attempt(new question_attempt_step(), 1); 00051 00052 $this->assertFalse($question->is_complete_response(array())); 00053 $this->assertFalse($question->is_complete_response( 00054 array('sub0' => '1', 'sub1' => '1', 'sub2' => '1', 'sub3' => '0'))); 00055 $this->assertFalse($question->is_complete_response(array('sub1' => '1'))); 00056 $this->assertTrue($question->is_complete_response( 00057 array('sub0' => '1', 'sub1' => '1', 'sub2' => '1', 'sub3' => '1'))); 00058 } 00059 00060 public function test_is_gradable_response() { 00061 $question = test_question_maker::make_a_matching_question(); 00062 $question->start_attempt(new question_attempt_step(), 1); 00063 00064 $this->assertFalse($question->is_gradable_response(array())); 00065 $this->assertFalse($question->is_gradable_response( 00066 array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'))); 00067 $this->assertTrue($question->is_gradable_response( 00068 array('sub0' => '1', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'))); 00069 $this->assertTrue($question->is_gradable_response(array('sub1' => '1'))); 00070 $this->assertTrue($question->is_gradable_response( 00071 array('sub0' => '1', 'sub1' => '1', 'sub2' => '3', 'sub3' => '1'))); 00072 } 00073 00074 public function test_is_same_response() { 00075 $question = test_question_maker::make_a_matching_question(); 00076 $question->start_attempt(new question_attempt_step(), 1); 00077 00078 $this->assertTrue($question->is_same_response( 00079 array(), 00080 array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'))); 00081 00082 $this->assertTrue($question->is_same_response( 00083 array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'), 00084 array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'))); 00085 00086 $this->assertFalse($question->is_same_response( 00087 array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'), 00088 array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1'))); 00089 00090 $this->assertTrue($question->is_same_response( 00091 array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1'), 00092 array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1'))); 00093 00094 $this->assertFalse($question->is_same_response( 00095 array('sub0' => '2', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1'), 00096 array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1'))); 00097 } 00098 00099 public function test_grading() { 00100 $question = test_question_maker::make_a_matching_question(); 00101 $question->shufflestems = false; 00102 $question->start_attempt(new question_attempt_step(), 1); 00103 00104 $choiceorder = $question->get_choice_order(); 00105 $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder)); 00106 00107 $this->assertEqual(array(1, question_state::$gradedright), 00108 $question->grade_response(array('sub0' => $orderforchoice[1], 00109 'sub1' => $orderforchoice[2], 'sub2' => $orderforchoice[2], 00110 'sub3' => $orderforchoice[1]))); 00111 $this->assertEqual(array(0.25, question_state::$gradedpartial), 00112 $question->grade_response(array('sub0' => $orderforchoice[1]))); 00113 $this->assertEqual(array(0, question_state::$gradedwrong), 00114 $question->grade_response(array('sub0' => $orderforchoice[2], 00115 'sub1' => $orderforchoice[3], 'sub2' => $orderforchoice[1], 00116 'sub3' => $orderforchoice[2]))); 00117 } 00118 00119 public function test_get_correct_response() { 00120 $question = test_question_maker::make_a_matching_question(); 00121 $question->shufflestems = false; 00122 $question->start_attempt(new question_attempt_step(), 1); 00123 00124 $choiceorder = $question->get_choice_order(); 00125 $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder)); 00126 00127 $this->assertEqual(array('sub0' => $orderforchoice[1], 'sub1' => $orderforchoice[2], 00128 'sub2' => $orderforchoice[2], 'sub3' => $orderforchoice[1]), 00129 $question->get_correct_response()); 00130 } 00131 00132 public function test_get_question_summary() { 00133 $match = test_question_maker::make_a_matching_question(); 00134 $match->start_attempt(new question_attempt_step(), 1); 00135 $qsummary = $match->get_question_summary(); 00136 $this->assertPattern('/' . preg_quote($match->questiontext) . '/', $qsummary); 00137 foreach ($match->stems as $stem) { 00138 $this->assertPattern('/' . preg_quote($stem) . '/', $qsummary); 00139 } 00140 foreach ($match->choices as $choice) { 00141 $this->assertPattern('/' . preg_quote($choice) . '/', $qsummary); 00142 } 00143 } 00144 00145 public function test_summarise_response() { 00146 $match = test_question_maker::make_a_matching_question(); 00147 $match->shufflestems = false; 00148 $match->start_attempt(new question_attempt_step(), 1); 00149 00150 $summary = $match->summarise_response(array('sub0' => 2, 'sub1' => 1)); 00151 00152 $this->assertPattern('/Dog -> \w+; Frog -> \w+/', $summary); 00153 } 00154 00155 public function test_classify_response() { 00156 $match = test_question_maker::make_a_matching_question(); 00157 $match->shufflestems = false; 00158 $match->start_attempt(new question_attempt_step(), 1); 00159 00160 $choiceorder = $match->get_choice_order(); 00161 $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder)); 00162 $choices = array(0 => get_string('choose') . '...'); 00163 foreach ($choiceorder as $key => $choice) { 00164 $choices[$key] = $match->choices[$choice]; 00165 } 00166 00167 $this->assertEqual(array( 00168 1 => new question_classified_response(2, 'Amphibian', 0), 00169 2 => new question_classified_response(3, 'Insect', 0), 00170 3 => question_classified_response::no_response(), 00171 4 => question_classified_response::no_response(), 00172 ), $match->classify_response(array('sub0' => $orderforchoice[2], 00173 'sub1' => $orderforchoice[3], 'sub2' => 0, 'sub3' => 0))); 00174 $this->assertEqual(array( 00175 1 => new question_classified_response(1, 'Mammal', 0.25), 00176 2 => new question_classified_response(2, 'Amphibian', 0.25), 00177 3 => new question_classified_response(2, 'Amphibian', 0.25), 00178 4 => new question_classified_response(1, 'Mammal', 0.25), 00179 ), $match->classify_response(array('sub0' => $orderforchoice[1], 00180 'sub1' => $orderforchoice[2], 'sub2' => $orderforchoice[2], 00181 'sub3' => $orderforchoice[1]))); 00182 } 00183 }