|
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/type/questiontypebase.php'); 00030 00031 00038 class test_response_answer_comparer implements question_response_answer_comparer { 00039 protected $answers = array(); 00040 00041 public function __construct($answers) { 00042 $this->answers = $answers; 00043 } 00044 00045 public function get_answers() { 00046 return $this->answers; 00047 } 00048 00049 public function compare_response_with_answer(array $response, question_answer $answer) { 00050 return $response['answer'] == $answer->answer; 00051 } 00052 } 00053 00060 class question_first_matching_answer_grading_strategy_test extends UnitTestCase { 00061 public function setUp() { 00062 } 00063 00064 public function tearDown() { 00065 } 00066 00067 public function test_no_answers_gives_null() { 00068 $question = new test_response_answer_comparer(array()); 00069 $strategy = new question_first_matching_answer_grading_strategy($question); 00070 $this->assertNull($strategy->grade(array())); 00071 } 00072 00073 public function test_matching_answer_returned1() { 00074 $answer = new question_answer(0, 'frog', 1, '', FORMAT_HTML); 00075 $question = new test_response_answer_comparer(array($answer)); 00076 $strategy = new question_first_matching_answer_grading_strategy($question); 00077 $this->assertIdentical($answer, $strategy->grade(array('answer' => 'frog'))); 00078 } 00079 00080 public function test_matching_answer_returned2() { 00081 $answer = new question_answer(0, 'frog', 1, '', FORMAT_HTML); 00082 $answer2 = new question_answer(0, 'frog', 0.5, '', FORMAT_HTML); 00083 $question = new test_response_answer_comparer(array($answer, $answer2)); 00084 $strategy = new question_first_matching_answer_grading_strategy($question); 00085 $this->assertIdentical($answer, $strategy->grade(array('answer' => 'frog'))); 00086 } 00087 00088 public function test_no_matching_answer_gives_null() { 00089 $answer = new question_answer(0, 'frog', 1, '', FORMAT_HTML); 00090 $answer2 = new question_answer(0, 'frog', 0.5, '', FORMAT_HTML); 00091 $question = new test_response_answer_comparer(array($answer, $answer2)); 00092 $strategy = new question_first_matching_answer_grading_strategy($question); 00093 $this->assertNull($strategy->grade(array('answer' => 'toad'))); 00094 } 00095 } 00096 00097 00104 class question_hint_test extends UnitTestCase { 00105 public function test_basic() { 00106 $row = new stdClass(); 00107 $row->id = 123; 00108 $row->hint = 'A hint'; 00109 $row->hintformat = FORMAT_HTML; 00110 $hint = question_hint::load_from_record($row); 00111 $this->assertEqual($row->id, $hint->id); 00112 $this->assertEqual($row->hint, $hint->hint); 00113 $this->assertEqual($row->hintformat, $hint->hintformat); 00114 } 00115 00116 public function test_with_parts() { 00117 $row = new stdClass(); 00118 $row->id = 123; 00119 $row->hint = 'A hint'; 00120 $row->hintformat = FORMAT_HTML; 00121 $row->shownumcorrect = 1; 00122 $row->clearwrong = 1; 00123 00124 $hint = question_hint_with_parts::load_from_record($row); 00125 $this->assertEqual($row->id, $hint->id); 00126 $this->assertEqual($row->hint, $hint->hint); 00127 $this->assertEqual($row->hintformat, $hint->hintformat); 00128 $this->assertTrue($hint->shownumcorrect); 00129 $this->assertTrue($hint->clearwrong); 00130 } 00131 }