MDL-32323 convert question tests
[moodle.git] / question / type / multianswer / tests / question_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
18/**
19 * Unit tests for the multianswer question definition class.
20 *
21 * @package qtype
22 * @subpackage multianswer
23 * @copyright 2011 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27global $CFG;
28require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
29
30
31/**
32 * Unit tests for qtype_multianswer_question.
33 *
34 * @copyright 2011 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class qtype_multianswer_question_test extends advanced_testcase {
38 public function test_get_expected_data() {
39 $question = test_question_maker::make_question('multianswer');
40 $this->assertEquals(array('sub1_answer' => PARAM_RAW_TRIMMED,
41 'sub2_answer' => PARAM_RAW), $question->get_expected_data());
42 }
43
44 public function test_is_complete_response() {
45 $question = test_question_maker::make_question('multianswer');
46
47 $this->assertFalse($question->is_complete_response(array()));
48 $this->assertTrue($question->is_complete_response(array('sub1_answer' => 'Owl',
49 'sub2_answer' => '2')));
50 $this->assertTrue($question->is_complete_response(array('sub1_answer' => '0',
51 'sub2_answer' => 0)));
52 $this->assertFalse($question->is_complete_response(array('sub1_answer' => 'Owl')));
53 }
54
55 public function test_is_gradable_response() {
56 $question = test_question_maker::make_question('multianswer');
57
58 $this->assertFalse($question->is_gradable_response(array()));
59 $this->assertTrue($question->is_gradable_response(array('sub1_answer' => 'Owl',
60 'sub2_answer' => '2')));
61 $this->assertTrue($question->is_gradable_response(array('sub1_answer' => '0',
62 'sub2_answer' => 0)));
63 $this->assertTrue($question->is_gradable_response(array('sub1_answer' => 'Owl')));
64 }
65
66 public function test_grading() {
67 $question = test_question_maker::make_question('multianswer');
68 $question->start_attempt(new question_attempt_step(), 1);
69
70 $rightchoice = $question->subquestions[2]->get_correct_response();
71
72 $this->assertEquals(array(1, question_state::$gradedright), $question->grade_response(
73 array('sub1_answer' => 'Owl', 'sub2_answer' => reset($rightchoice))));
74 $this->assertEquals(array(0.5, question_state::$gradedpartial), $question->grade_response(
75 array('sub1_answer' => 'Owl')));
76 $this->assertEquals(array(0.5, question_state::$gradedpartial), $question->grade_response(
77 array('sub1_answer' => 'Goat', 'sub2_answer' => reset($rightchoice))));
78 $this->assertEquals(array(0, question_state::$gradedwrong), $question->grade_response(
79 array('sub1_answer' => 'Dog')));
80 }
81
82 public function test_get_correct_response() {
83 $question = test_question_maker::make_question('multianswer');
84 $question->start_attempt(new question_attempt_step(), 1);
85
86 $rightchoice = $question->subquestions[2]->get_correct_response();
87
88 $this->assertEquals(array('sub1_answer' => 'Owl', 'sub2_answer' => reset($rightchoice)),
89 $question->get_correct_response());
90 }
91
92 public function test_get_question_summary() {
93 $question = test_question_maker::make_question('multianswer');
94
95 // Bit of a hack to make testing easier.
96 $question->subquestions[2]->shuffleanswers = false;
97
98 $question->start_attempt(new question_attempt_step(), 1);
99
100 $qsummary = $question->get_question_summary();
101 $this->assertEquals('Complete this opening line of verse: "The _____ and the ' .
102 '{Bow-wow; Wiggly worm; Pussy-cat} went to sea".', $qsummary);
103 }
104
105 public function test_summarise_response() {
106 $question = test_question_maker::make_question('multianswer');
107 $question->start_attempt(new question_attempt_step(), 1);
108
109 $rightchoice = $question->subquestions[2]->get_correct_response();
110
111 $this->assertEquals(get_string('subqresponse', 'qtype_multianswer',
112 array('i' => 1, 'response' => 'Owl')) . '; ' .
113 get_string('subqresponse', 'qtype_multianswer',
114 array('i' => 2, 'response' => 'Pussy-cat')), $question->summarise_response(
115 array('sub1_answer' => 'Owl', 'sub2_answer' => reset($rightchoice))));
116 }
117}