MDL-40176 mock_submit method for testing
[moodle.git] / question / type / multianswer / tests / questiontype_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 * Unit tests for the multianswer question definition class.
19 *
20 * @package qtype
21 * @subpackage multianswer
22 * @copyright 2011 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
31require_once($CFG->dirroot . '/question/type/multianswer/questiontype.php');
32
33
34/**
35 * Unit tests for the multianswer question definition class.
36 *
37 * @copyright 2011 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class qtype_multianswer_test extends advanced_testcase {
41 /** @var qtype_multianswer instance of the question type class to test. */
42 protected $qtype;
43
44 protected function setUp() {
45 $this->qtype = new qtype_multianswer();
46 }
47
48 protected function tearDown() {
49 $this->qtype = null;
50 }
51
52 protected function get_test_question_data() {
53 global $USER;
54 $q = new stdClass();
55 $q->id = 0;
56 $q->name = 'Simple multianswer';
57 $q->category = 0;
58 $q->contextid = 0;
59 $q->parent = 0;
60 $q->questiontext =
61 'Complete this opening line of verse: "The {#1} and the {#2} went to sea".';
62 $q->questiontextformat = FORMAT_HTML;
63 $q->generalfeedback = 'Generalfeedback: It\'s from "The Owl and the Pussy-cat" by Lear: ' .
64 '"The owl and the pussycat went to see';
65 $q->generalfeedbackformat = FORMAT_HTML;
66 $q->defaultmark = 2;
67 $q->penalty = 0.3333333;
68 $q->length = 1;
69 $q->stamp = make_unique_id_code();
70 $q->version = make_unique_id_code();
71 $q->hidden = 0;
72 $q->timecreated = time();
73 $q->timemodified = time();
74 $q->createdby = $USER->id;
75 $q->modifiedby = $USER->id;
76
77 $sadata = new stdClass();
78 $sadata->id = 1;
79 $sadata->qtype = 'shortanswer';
80 $sadata->defaultmark = 1;
81 $sadata->options->usecase = true;
82 $sadata->options->answers[1] = (object) array('answer' => 'Bow-wow', 'fraction' => 0);
83 $sadata->options->answers[2] = (object) array('answer' => 'Wiggly worm', 'fraction' => 0);
84 $sadata->options->answers[3] = (object) array('answer' => 'Pussy-cat', 'fraction' => 1);
85
86 $mcdata = new stdClass();
87 $mcdata->id = 1;
88 $mcdata->qtype = 'multichoice';
89 $mcdata->defaultmark = 1;
90 $mcdata->options->single = true;
91 $mcdata->options->answers[1] = (object) array('answer' => 'Dog', 'fraction' => 0);
92 $mcdata->options->answers[2] = (object) array('answer' => 'Owl', 'fraction' => 1);
93 $mcdata->options->answers[3] = (object) array('answer' => '*', 'fraction' => 0);
94
95 $q->options->questions = array(
96 1 => $sadata,
97 2 => $mcdata,
98 );
99
100 return $q;
101 }
102
103 public function test_name() {
104 $this->assertEquals($this->qtype->name(), 'multianswer');
105 }
106
107 public function test_can_analyse_responses() {
108 $this->assertFalse($this->qtype->can_analyse_responses());
109 }
110
111 public function test_get_random_guess_score() {
112 $q = test_question_maker::get_question_data('multianswer', 'twosubq');
113 $this->assertEquals(0.1666667, $this->qtype->get_random_guess_score($q), '', 0.0000001);
114 }
115}