MDL-27789 The new question plugins reported as standard ones
[moodle.git] / question / behaviour / missing / simpletest / testmissingbehaviour.php
CommitLineData
d1b7e03d 1<?php
d1b7e03d
TH
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
d1b7e03d
TH
17/**
18 * This file contains tests for the 'missing' behaviour.
19 *
017bc1d9
TH
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
d1b7e03d
TH
24 */
25
26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
d1b7e03d
TH
29require_once(dirname(__FILE__) . '/../../../engine/lib.php');
30require_once(dirname(__FILE__) . '/../../../engine/simpletest/helpers.php');
31require_once(dirname(__FILE__) . '/../behaviour.php');
32
017bc1d9
TH
33
34/**
35 * Unit tests for the 'missing' behaviour.
36 *
37 * @copyright 2009 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
d1b7e03d
TH
40class qbehaviour_missing_test extends UnitTestCase {
41 public function test_missing_cannot_start() {
a4c98291 42 $qa = new question_attempt(test_question_maker::make_question('truefalse', 'true'), 0);
d1b7e03d
TH
43 $behaviour = new qbehaviour_missing($qa, 'deferredfeedback');
44 $this->expectException();
1da821bb 45 $behaviour->init_first_step(new question_attempt_step(array()), 1);
d1b7e03d
TH
46 }
47
48 public function test_missing_cannot_process() {
a4c98291 49 $qa = new question_attempt(test_question_maker::make_question('truefalse', 'true'), 0);
d1b7e03d
TH
50 $behaviour = new qbehaviour_missing($qa, 'deferredfeedback');
51 $this->expectException();
52 $behaviour->process_action(new question_attempt_pending_step(array()));
53 }
54
55 public function test_missing_cannot_get_min_grade() {
a4c98291 56 $qa = new question_attempt(test_question_maker::make_question('truefalse', 'true'), 0);
d1b7e03d
TH
57 $behaviour = new qbehaviour_missing($qa, 'deferredfeedback');
58 $this->expectException();
59 $behaviour->get_min_fraction();
60 }
61
62 public function test_render_missing() {
63 $records = testing_db_record_builder::build_db_records(array(
2ac0843f 64 array('id', 'questionattemptid', 'contextid', 'questionusageid', 'slot',
1da821bb 65 'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'flagged',
1631ceea
TH
66 'questionsummary', 'rightanswer', 'responsesummary',
67 'timemodified', 'attemptstepid', 'sequencenumber', 'state', 'fraction',
68 'timecreated', 'userid', 'name', 'value'),
1da821bb 69 array(1, 1, 123, 1, 1, 'strangeunknown', -1, 1, 2.0000000, 0.0000000, 0, '', '', '',
1631ceea 70 1256233790, 1, 0, 'todo', null, 1256233700, 1, '_order', '1,2,3'),
1da821bb 71 array(2, 1, 123, 1, 1, 'strangeunknown', -1, 1, 2.0000000, 0.0000000, 0, '', '', '',
1631ceea 72 1256233790, 2, 1, 'complete', 0.50, 1256233705, 1, '-submit', '1'),
1da821bb 73 array(3, 1, 123, 1, 1, 'strangeunknown', -1, 1, 2.0000000, 0.0000000, 0, '', '', '',
1631ceea 74 1256233790, 2, 1, 'complete', 0.50, 1256233705, 1, 'choice0', '1'),
d1b7e03d
TH
75 ));
76
a4c98291 77 $question = test_question_maker::make_question('truefalse', 'true');
d1b7e03d
TH
78 $question->id = -1;
79
80 question_bank::start_unit_test();
81 question_bank::load_test_question_data($question);
82 $qa = question_attempt::load_from_records($records, 1,
83 new question_usage_null_observer(), 'deferredfeedback');
84 question_bank::end_unit_test();
85
86 $this->assertEqual(2, $qa->get_num_steps());
87
88 $step = $qa->get_step(0);
89 $this->assertEqual(question_state::$todo, $step->get_state());
90 $this->assertNull($step->get_fraction());
91 $this->assertEqual(1256233700, $step->get_timecreated());
92 $this->assertEqual(1, $step->get_user_id());
93 $this->assertEqual(array('_order' => '1,2,3'), $step->get_all_data());
94
95 $step = $qa->get_step(1);
96 $this->assertEqual(question_state::$complete, $step->get_state());
97 $this->assertEqual(0.5, $step->get_fraction());
98 $this->assertEqual(1256233705, $step->get_timecreated());
99 $this->assertEqual(1, $step->get_user_id());
100 $this->assertEqual(array('-submit' => '1', 'choice0' => '1'), $step->get_all_data());
101
102 $output = $qa->render(new question_display_options(), '1');
103 $this->assertPattern('/' . preg_quote($qa->get_question()->questiontext) . '/', $output);
1631ceea
TH
104 $this->assertPattern('/' . preg_quote(
105 get_string('questionusedunknownmodel', 'qbehaviour_missing')) . '/', $output);
d1b7e03d
TH
106 $this->assert(new ContainsTagWithAttribute('div', 'class', 'warning'), $output);
107 }
108}