|
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 require_once($CFG->dirroot . '/question/type/shortanswer/question.php'); 00031 00032 00039 class qtype_shortanswer_question_test extends UnitTestCase { 00040 public function test_compare_string_with_wildcard() { 00041 // Test case sensitive literal matches. 00042 $this->assertTrue(qtype_shortanswer_question::compare_string_with_wildcard( 00043 'Frog', 'Frog', false)); 00044 $this->assertFalse(qtype_shortanswer_question::compare_string_with_wildcard( 00045 'Frog', 'frog', false)); 00046 $this->assertTrue(qtype_shortanswer_question::compare_string_with_wildcard( 00047 ' Frog ', 'Frog', false)); 00048 $this->assertFalse(qtype_shortanswer_question::compare_string_with_wildcard( 00049 'Frogs', 'Frog', false)); 00050 00051 // Test case insensitive literal matches. 00052 $this->assertTrue(qtype_shortanswer_question::compare_string_with_wildcard( 00053 'Frog', 'frog', true)); 00054 $this->assertTrue(qtype_shortanswer_question::compare_string_with_wildcard( 00055 ' FROG ', 'Frog', true)); 00056 $this->assertFalse(qtype_shortanswer_question::compare_string_with_wildcard( 00057 'Frogs', 'Frog', true)); 00058 00059 // Test case sensitive wildcard matches. 00060 $this->assertTrue(qtype_shortanswer_question::compare_string_with_wildcard( 00061 'Frog', 'F*og', false)); 00062 $this->assertTrue(qtype_shortanswer_question::compare_string_with_wildcard( 00063 'Fog', 'F*og', false)); 00064 $this->assertTrue(qtype_shortanswer_question::compare_string_with_wildcard( 00065 ' Fat dog ', 'F*og', false)); 00066 $this->assertFalse(qtype_shortanswer_question::compare_string_with_wildcard( 00067 'Frogs', 'F*og', false)); 00068 $this->assertFalse(qtype_shortanswer_question::compare_string_with_wildcard( 00069 'Fg', 'F*og', false)); 00070 $this->assertFalse(qtype_shortanswer_question::compare_string_with_wildcard( 00071 'frog', 'F*og', false)); 00072 $this->assertFalse(qtype_shortanswer_question::compare_string_with_wildcard( 00073 ' fat dog ', 'F*og', false)); 00074 00075 // Test case insensitive wildcard matches. 00076 $this->assertTrue(qtype_shortanswer_question::compare_string_with_wildcard( 00077 'Frog', 'F*og', true)); 00078 $this->assertTrue(qtype_shortanswer_question::compare_string_with_wildcard( 00079 'Fog', 'F*og', true)); 00080 $this->assertTrue(qtype_shortanswer_question::compare_string_with_wildcard( 00081 ' Fat dog ', 'F*og', true)); 00082 $this->assertFalse(qtype_shortanswer_question::compare_string_with_wildcard( 00083 'Frogs', 'F*og', true)); 00084 $this->assertFalse(qtype_shortanswer_question::compare_string_with_wildcard( 00085 'Fg', 'F*og', true)); 00086 $this->assertTrue(qtype_shortanswer_question::compare_string_with_wildcard( 00087 'frog', 'F*og', true)); 00088 $this->assertTrue(qtype_shortanswer_question::compare_string_with_wildcard( 00089 ' fat dog ', 'F*og', true)); 00090 00091 // Test match using regexp special chars. 00092 $this->assertTrue(qtype_shortanswer_question::compare_string_with_wildcard( 00093 ' * ', '\*', false)); 00094 $this->assertTrue(qtype_shortanswer_question::compare_string_with_wildcard( 00095 '*', '\*', false)); 00096 $this->assertTrue(qtype_shortanswer_question::compare_string_with_wildcard( 00097 'Frog*toad', 'Frog\*toad', false)); 00098 $this->assertFalse(qtype_shortanswer_question::compare_string_with_wildcard( 00099 'a', '[a-z]', false)); 00100 $this->assertTrue(qtype_shortanswer_question::compare_string_with_wildcard( 00101 '[a-z]', '[a-z]', false)); 00102 $this->assertTrue(qtype_shortanswer_question::compare_string_with_wildcard( 00103 '\{}/', '\{}/', true)); 00104 00105 // See http://moodle.org/mod/forum/discuss.php?d=120557 00106 $this->assertTrue(qtype_shortanswer_question::compare_string_with_wildcard( 00107 'ITÁLIE', 'Itálie', true)); 00108 } 00109 00110 public function test_is_complete_response() { 00111 $question = test_question_maker::make_question('shortanswer'); 00112 00113 $this->assertFalse($question->is_complete_response(array())); 00114 $this->assertFalse($question->is_complete_response(array('answer' => ''))); 00115 $this->assertTrue($question->is_complete_response(array('answer' => '0'))); 00116 $this->assertTrue($question->is_complete_response(array('answer' => '0.0'))); 00117 $this->assertTrue($question->is_complete_response(array('answer' => 'x'))); 00118 } 00119 00120 public function test_is_gradable_response() { 00121 $question = test_question_maker::make_question('shortanswer'); 00122 00123 $this->assertFalse($question->is_gradable_response(array())); 00124 $this->assertFalse($question->is_gradable_response(array('answer' => ''))); 00125 $this->assertTrue($question->is_gradable_response(array('answer' => '0'))); 00126 $this->assertTrue($question->is_gradable_response(array('answer' => '0.0'))); 00127 $this->assertTrue($question->is_gradable_response(array('answer' => 'x'))); 00128 } 00129 00130 public function test_grading() { 00131 $question = test_question_maker::make_question('shortanswer'); 00132 00133 $this->assertEqual(array(0, question_state::$gradedwrong), 00134 $question->grade_response(array('answer' => 'x'))); 00135 $this->assertEqual(array(1, question_state::$gradedright), 00136 $question->grade_response(array('answer' => 'frog'))); 00137 $this->assertEqual(array(0.8, question_state::$gradedpartial), 00138 $question->grade_response(array('answer' => 'toad'))); 00139 } 00140 00141 public function test_get_correct_response() { 00142 $question = test_question_maker::make_question('shortanswer'); 00143 00144 $this->assertEqual(array('answer' => 'frog'), 00145 $question->get_correct_response()); 00146 } 00147 00148 public function test_get_question_summary() { 00149 $sa = test_question_maker::make_question('shortanswer'); 00150 $qsummary = $sa->get_question_summary(); 00151 $this->assertEqual('Name an amphibian: __________', $qsummary); 00152 } 00153 00154 public function test_summarise_response() { 00155 $sa = test_question_maker::make_question('shortanswer'); 00156 $summary = $sa->summarise_response(array('answer' => 'dog')); 00157 $this->assertEqual('dog', $summary); 00158 } 00159 00160 public function test_classify_response() { 00161 $sa = test_question_maker::make_question('shortanswer'); 00162 $sa->start_attempt(new question_attempt_step(), 1); 00163 00164 $this->assertEqual(array( 00165 new question_classified_response(13, 'frog', 1.0)), 00166 $sa->classify_response(array('answer' => 'frog'))); 00167 $this->assertEqual(array( 00168 new question_classified_response(14, 'toad', 0.8)), 00169 $sa->classify_response(array('answer' => 'toad'))); 00170 $this->assertEqual(array( 00171 new question_classified_response(15, 'cat', 0.0)), 00172 $sa->classify_response(array('answer' => 'cat'))); 00173 $this->assertEqual(array( 00174 question_classified_response::no_response()), 00175 $sa->classify_response(array('answer' => ''))); 00176 } 00177 00178 public function test_classify_response_no_star() { 00179 $sa = test_question_maker::make_question('shortanswer', 'frogonly'); 00180 $sa->start_attempt(new question_attempt_step(), 1); 00181 00182 $this->assertEqual(array( 00183 new question_classified_response(13, 'frog', 1.0)), 00184 $sa->classify_response(array('answer' => 'frog'))); 00185 $this->assertEqual(array( 00186 new question_classified_response(0, 'toad', 0.0)), 00187 $sa->classify_response(array('answer' => 'toad'))); 00188 $this->assertEqual(array( 00189 question_classified_response::no_response()), 00190 $sa->classify_response(array('answer' => ''))); 00191 } 00192 }