weekly release 3.10.4+
[moodle.git] / question / behaviour / missing / tests / missingbehaviour_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 * This file contains tests for the 'missing' behaviour.
19 *
20 * @package qbehaviour
21 * @subpackage missing
22 * @copyright 2009 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;
1fcf0ca8
RS
30require_once(__DIR__ . '/../../../engine/lib.php');
31require_once(__DIR__ . '/../../../engine/tests/helpers.php');
32require_once(__DIR__ . '/../behaviour.php');
603bd001
PS
33
34
35/**
36 * Unit tests for the 'missing' behaviour.
37 *
38 * @copyright 2009 The Open University
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41class qbehaviour_missing_test extends advanced_testcase {
52f3e060 42
603bd001
PS
43 public function test_missing_cannot_start() {
44 $qa = new question_attempt(test_question_maker::make_question('truefalse', 'true'), 0);
45 $behaviour = new qbehaviour_missing($qa, 'deferredfeedback');
ddf72dd6 46 $this->expectException(moodle_exception::class);
603bd001
PS
47 $behaviour->init_first_step(new question_attempt_step(array()), 1);
48 }
49
50 public function test_missing_cannot_process() {
51 $qa = new question_attempt(test_question_maker::make_question('truefalse', 'true'), 0);
52 $behaviour = new qbehaviour_missing($qa, 'deferredfeedback');
ddf72dd6 53 $this->expectException(moodle_exception::class);
603bd001
PS
54 $behaviour->process_action(new question_attempt_pending_step(array()));
55 }
56
4e3d8293 57 public function test_missing_cannot_get_min_fraction() {
603bd001
PS
58 $qa = new question_attempt(test_question_maker::make_question('truefalse', 'true'), 0);
59 $behaviour = new qbehaviour_missing($qa, 'deferredfeedback');
ddf72dd6 60 $this->expectException(moodle_exception::class);
603bd001
PS
61 $behaviour->get_min_fraction();
62 }
63
4e3d8293
TH
64 public function test_missing_cannot_get_max_fraction() {
65 $qa = new question_attempt(test_question_maker::make_question('truefalse', 'true'), 0);
66 $behaviour = new qbehaviour_missing($qa, 'deferredfeedback');
ddf72dd6 67 $this->expectException(moodle_exception::class);
4e3d8293
TH
68 $behaviour->get_max_fraction();
69 }
70
603bd001
PS
71 public function test_render_missing() {
72 $records = new question_test_recordset(array(
73 array('questionattemptid', 'contextid', 'questionusageid', 'slot',
4e3d8293 74 'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'maxfraction', 'flagged',
603bd001
PS
75 'questionsummary', 'rightanswer', 'responsesummary',
76 'timemodified', 'attemptstepid', 'sequencenumber', 'state', 'fraction',
77 'timecreated', 'userid', 'name', 'value'),
4e3d8293 78 array(1, 123, 1, 1, 'strangeunknown', -1, 1, 2.0000000, 0.0000000, 1.0000000, 0, '', '', '',
603bd001 79 1256233790, 1, 0, 'todo', null, 1256233700, 1, '_order', '1,2,3'),
4e3d8293 80 array(1, 123, 1, 1, 'strangeunknown', -1, 1, 2.0000000, 0.0000000, 1.0000000, 0, '', '', '',
603bd001 81 1256233790, 2, 1, 'complete', 0.50, 1256233705, 1, '-submit', '1'),
4e3d8293 82 array(1, 123, 1, 1, 'strangeunknown', -1, 1, 2.0000000, 0.0000000, 1.0000000, 0, '', '', '',
603bd001
PS
83 1256233790, 2, 1, 'complete', 0.50, 1256233705, 1, 'choice0', '1'),
84 ));
85
86 $question = test_question_maker::make_question('truefalse', 'true');
87 $question->id = -1;
88
89 question_bank::start_unit_test();
90 question_bank::load_test_question_data($question);
91 $qa = question_attempt::load_from_records($records, 1,
92 new question_usage_null_observer(), 'deferredfeedback');
93 question_bank::end_unit_test();
94
95 $this->assertEquals(2, $qa->get_num_steps());
96
97 $step = $qa->get_step(0);
98 $this->assertEquals(question_state::$todo, $step->get_state());
99 $this->assertNull($step->get_fraction());
100 $this->assertEquals(1256233700, $step->get_timecreated());
101 $this->assertEquals(1, $step->get_user_id());
102 $this->assertEquals(array('_order' => '1,2,3'), $step->get_all_data());
103
104 $step = $qa->get_step(1);
105 $this->assertEquals(question_state::$complete, $step->get_state());
106 $this->assertEquals(0.5, $step->get_fraction());
107 $this->assertEquals(1256233705, $step->get_timecreated());
108 $this->assertEquals(1, $step->get_user_id());
109 $this->assertEquals(array('-submit' => '1', 'choice0' => '1'), $step->get_all_data());
110
111 $output = $qa->render(new question_display_options(), '1');
64207dab 112 $this->assertRegExp('/' . preg_quote($qa->get_question(false)->questiontext, '/') . '/', $output);
603bd001 113 $this->assertRegExp('/' . preg_quote(
03cefcc9 114 get_string('questionusedunknownmodel', 'qbehaviour_missing'), '/') . '/', $output);
603bd001
PS
115 $this->assertTag(array('tag'=>'div', 'attributes'=>array('class'=>'warning')), $output);
116 }
117}