MDL-50028 questions: split into one testcase class per file
[moodle.git] / question / type / tests / question_first_matching_answer_grading_strategy_test.php
CommitLineData
603bd001
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
c70c18c0 18 * Unit tests for the question_first_matching_answer_grading_strategy class.
603bd001 19 *
c70c18c0
TH
20 * @package core_question
21 * @copyright 2008 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
603bd001
PS
23 */
24
603bd001
PS
25defined('MOODLE_INTERNAL') || die();
26
27global $CFG;
28require_once($CFG->dirroot . '/question/type/questiontypebase.php');
29
30
31/**
c70c18c0 32 * Helper used by the testcases in this file.
603bd001
PS
33 *
34 * @copyright 2008 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class test_response_answer_comparer implements question_response_answer_comparer {
38 protected $answers = array();
39
40 public function __construct($answers) {
41 $this->answers = $answers;
42 }
43
44 public function get_answers() {
45 return $this->answers;
46 }
47
48 public function compare_response_with_answer(array $response, question_answer $answer) {
49 return $response['answer'] == $answer->answer;
50 }
51}
52
c70c18c0 53
603bd001
PS
54/**
55 * Tests for {@link question_first_matching_answer_grading_strategy}.
56 *
57 * @copyright 2008 The Open University
58 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
59 */
c70c18c0 60class question_first_matching_answer_grading_strategy_testcase extends advanced_testcase {
603bd001
PS
61 protected function setUp() {
62 }
63
64 protected function tearDown() {
65 }
66
67 public function test_no_answers_gives_null() {
68 $question = new test_response_answer_comparer(array());
69 $strategy = new question_first_matching_answer_grading_strategy($question);
70 $this->assertNull($strategy->grade(array()));
71 }
72
73 public function test_matching_answer_returned1() {
74 $answer = new question_answer(0, 'frog', 1, '', FORMAT_HTML);
75 $question = new test_response_answer_comparer(array($answer));
76 $strategy = new question_first_matching_answer_grading_strategy($question);
77 $this->assertSame($answer, $strategy->grade(array('answer' => 'frog')));
78 }
79
80 public function test_matching_answer_returned2() {
81 $answer = new question_answer(0, 'frog', 1, '', FORMAT_HTML);
82 $answer2 = new question_answer(0, 'frog', 0.5, '', FORMAT_HTML);
83 $question = new test_response_answer_comparer(array($answer, $answer2));
84 $strategy = new question_first_matching_answer_grading_strategy($question);
85 $this->assertSame($answer, $strategy->grade(array('answer' => 'frog')));
86 }
87
88 public function test_no_matching_answer_gives_null() {
89 $answer = new question_answer(0, 'frog', 1, '', FORMAT_HTML);
90 $answer2 = new question_answer(0, 'frog', 0.5, '', FORMAT_HTML);
91 $question = new test_response_answer_comparer(array($answer, $answer2));
92 $strategy = new question_first_matching_answer_grading_strategy($question);
93 $this->assertNull($strategy->grade(array('answer' => 'toad')));
94 }
95}