weekly on-sync release 3.2dev
[moodle.git] / question / engine / tests / questionattempt_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 question_attempt class.
19 *
20 * Action methods like start, process_action and finish are assumed to be
21 * tested by walkthrough tests in the various behaviours.
22 *
23 * @package moodlecore
24 * @subpackage questionengine
25 * @copyright 2009 The Open University
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
29
30defined('MOODLE_INTERNAL') || die();
31
32global $CFG;
33require_once(dirname(__FILE__) . '/../lib.php');
34require_once(dirname(__FILE__) . '/helpers.php');
35
36
37/**
38 * Unit tests for the {@link question_attempt} class.
39 *
40 * These are the tests that don't require any steps.
41 *
42 * @copyright 2009 The Open University
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 */
df7d175d 45class question_attempt_testcase extends advanced_testcase {
603bd001
PS
46 private $question;
47 private $usageid;
48 private $qa;
49
50 protected function setUp() {
51 $this->question = test_question_maker::make_question('description');
52 $this->question->defaultmark = 3;
53 $this->usageid = 13;
54 $this->qa = new question_attempt($this->question, $this->usageid);
55 }
56
57 protected function tearDown() {
58 $this->question = null;
59 $this->useageid = null;
60 $this->qa = null;
61 }
62
63 public function test_constructor_sets_maxmark() {
64 $qa = new question_attempt($this->question, $this->usageid);
65 $this->assertSame($this->question, $qa->get_question());
66 $this->assertEquals(3, $qa->get_max_mark());
67 }
68
69 public function test_maxmark_beats_default_mark() {
70 $qa = new question_attempt($this->question, $this->usageid, null, 2);
71 $this->assertEquals(2, $qa->get_max_mark());
72 }
73
74 public function test_get_set_slot() {
75 $this->qa->set_slot(7);
76 $this->assertEquals(7, $this->qa->get_slot());
77 }
78
79 public function test_fagged_initially_false() {
80 $this->assertEquals(false, $this->qa->is_flagged());
81 }
82
83 public function test_set_is_flagged() {
84 $this->qa->set_flagged(true);
85 $this->assertEquals(true, $this->qa->is_flagged());
86 }
87
88 public function test_get_qt_field_name() {
89 $name = $this->qa->get_qt_field_name('test');
03cefcc9 90 $this->assertRegExp('/^' . preg_quote($this->qa->get_field_prefix(), '/') . '/', $name);
603bd001
PS
91 $this->assertRegExp('/_test$/', $name);
92 }
93
94 public function test_get_behaviour_field_name() {
95 $name = $this->qa->get_behaviour_field_name('test');
03cefcc9 96 $this->assertRegExp('/^' . preg_quote($this->qa->get_field_prefix(), '/') . '/', $name);
603bd001
PS
97 $this->assertRegExp('/_-test$/', $name);
98 }
99
100 public function test_get_field_prefix() {
101 $this->qa->set_slot(7);
102 $name = $this->qa->get_field_prefix();
03cefcc9
TH
103 $this->assertRegExp('/' . preg_quote($this->usageid, '/') . '/', $name);
104 $this->assertRegExp('/' . preg_quote($this->qa->get_slot(), '/') . '/', $name);
603bd001
PS
105 }
106
107 public function test_get_submitted_var_not_present_var_returns_null() {
108 $this->assertNull($this->qa->get_submitted_var(
109 'reallyunlikelyvariablename', PARAM_BOOL));
110 }
111
df7d175d
TH
112 public function test_get_all_submitted_qt_vars() {
113 $this->qa->set_usage_id('MDOgzdhS4W');
114 $this->qa->set_slot(1);
115 $this->assertEquals(array('omval_response1' => 1, 'omval_response2' => 666, 'omact_gen_14' => 'Check'),
116 $this->qa->get_all_submitted_qt_vars(array(
117 'qMDOgzdhS4W:1_omval_response1' => 1,
118 'qMDOgzdhS4W:1_omval_response2' => 666,
119 'qMDOgzdhS4W:1_omact_gen_14' => 'Check',
120 )));
121 }
603bd001 122}