2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This file contains tests for the 'missing' behaviour.
22 * @copyright 2009 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 require_once(dirname(__FILE__) . '/../../../engine/lib.php');
30 require_once(dirname(__FILE__) . '/../../../engine/simpletest/helpers.php');
31 require_once(dirname(__FILE__) . '/../behaviour.php');
35 * Unit tests for the 'missing' behaviour.
37 * @copyright 2009 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class qbehaviour_missing_test extends UnitTestCase {
41 public function test_missing_cannot_start() {
42 $qa = new question_attempt(test_question_maker::make_question('truefalse', 'true'), 0);
43 $behaviour = new qbehaviour_missing($qa, 'deferredfeedback');
44 $this->expectException();
45 $behaviour->init_first_step(new question_attempt_step(array()), 1);
48 public function test_missing_cannot_process() {
49 $qa = new question_attempt(test_question_maker::make_question('truefalse', 'true'), 0);
50 $behaviour = new qbehaviour_missing($qa, 'deferredfeedback');
51 $this->expectException();
52 $behaviour->process_action(new question_attempt_pending_step(array()));
55 public function test_missing_cannot_get_min_grade() {
56 $qa = new question_attempt(test_question_maker::make_question('truefalse', 'true'), 0);
57 $behaviour = new qbehaviour_missing($qa, 'deferredfeedback');
58 $this->expectException();
59 $behaviour->get_min_fraction();
62 public function test_render_missing() {
63 $records = testing_db_record_builder::build_db_records(array(
64 array('id', 'questionattemptid', 'contextid', 'questionusageid', 'slot',
65 'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'flagged',
66 'questionsummary', 'rightanswer', 'responsesummary',
67 'timemodified', 'attemptstepid', 'sequencenumber', 'state', 'fraction',
68 'timecreated', 'userid', 'name', 'value'),
69 array(1, 1, 123, 1, 1, 'strangeunknown', -1, 1, 2.0000000, 0.0000000, 0, '', '', '',
70 1256233790, 1, 0, 'todo', null, 1256233700, 1, '_order', '1,2,3'),
71 array(2, 1, 123, 1, 1, 'strangeunknown', -1, 1, 2.0000000, 0.0000000, 0, '', '', '',
72 1256233790, 2, 1, 'complete', 0.50, 1256233705, 1, '-submit', '1'),
73 array(3, 1, 123, 1, 1, 'strangeunknown', -1, 1, 2.0000000, 0.0000000, 0, '', '', '',
74 1256233790, 2, 1, 'complete', 0.50, 1256233705, 1, 'choice0', '1'),
77 $question = test_question_maker::make_question('truefalse', 'true');
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();
86 $this->assertEqual(2, $qa->get_num_steps());
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());
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());
102 $output = $qa->render(new question_display_options(), '1');
103 $this->assertPattern('/' . preg_quote($qa->get_question()->questiontext) . '/', $output);
104 $this->assertPattern('/' . preg_quote(
105 get_string('questionusedunknownmodel', 'qbehaviour_missing')) . '/', $output);
106 $this->assert(new ContainsTagWithAttribute('div', 'class', 'warning'), $output);