MDL-70153 qtype_essay: Add behat test for attachments max size
[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;
1fcf0ca8
RS
33require_once(__DIR__ . '/../lib.php');
34require_once(__DIR__ . '/helpers.php');
603bd001
PS
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 {
6c91e515 46 /** @var question_definition a question that can be used in the tests. */
603bd001 47 private $question;
6c91e515 48 /** @var int fake question_usage id used in some tests. */
603bd001 49 private $usageid;
6c91e515 50 /** @var question_attempt a question attempt that can be used in the tests. */
603bd001
PS
51 private $qa;
52
d81a9480 53 protected function setUp(): void {
603bd001
PS
54 $this->question = test_question_maker::make_question('description');
55 $this->question->defaultmark = 3;
56 $this->usageid = 13;
57 $this->qa = new question_attempt($this->question, $this->usageid);
58 }
59
d81a9480 60 protected function tearDown(): void {
603bd001
PS
61 $this->question = null;
62 $this->useageid = null;
63 $this->qa = null;
64 }
65
66 public function test_constructor_sets_maxmark() {
67 $qa = new question_attempt($this->question, $this->usageid);
64207dab 68 $this->assertSame($this->question, $qa->get_question(false));
603bd001
PS
69 $this->assertEquals(3, $qa->get_max_mark());
70 }
71
72 public function test_maxmark_beats_default_mark() {
73 $qa = new question_attempt($this->question, $this->usageid, null, 2);
74 $this->assertEquals(2, $qa->get_max_mark());
75 }
76
77 public function test_get_set_slot() {
78 $this->qa->set_slot(7);
79 $this->assertEquals(7, $this->qa->get_slot());
80 }
81
82 public function test_fagged_initially_false() {
83 $this->assertEquals(false, $this->qa->is_flagged());
84 }
85
86 public function test_set_is_flagged() {
87 $this->qa->set_flagged(true);
88 $this->assertEquals(true, $this->qa->is_flagged());
89 }
90
91 public function test_get_qt_field_name() {
92 $name = $this->qa->get_qt_field_name('test');
03cefcc9 93 $this->assertRegExp('/^' . preg_quote($this->qa->get_field_prefix(), '/') . '/', $name);
603bd001
PS
94 $this->assertRegExp('/_test$/', $name);
95 }
96
97 public function test_get_behaviour_field_name() {
98 $name = $this->qa->get_behaviour_field_name('test');
03cefcc9 99 $this->assertRegExp('/^' . preg_quote($this->qa->get_field_prefix(), '/') . '/', $name);
603bd001
PS
100 $this->assertRegExp('/_-test$/', $name);
101 }
102
103 public function test_get_field_prefix() {
104 $this->qa->set_slot(7);
105 $name = $this->qa->get_field_prefix();
03cefcc9
TH
106 $this->assertRegExp('/' . preg_quote($this->usageid, '/') . '/', $name);
107 $this->assertRegExp('/' . preg_quote($this->qa->get_slot(), '/') . '/', $name);
603bd001
PS
108 }
109
110 public function test_get_submitted_var_not_present_var_returns_null() {
111 $this->assertNull($this->qa->get_submitted_var(
112 'reallyunlikelyvariablename', PARAM_BOOL));
113 }
114
df7d175d
TH
115 public function test_get_all_submitted_qt_vars() {
116 $this->qa->set_usage_id('MDOgzdhS4W');
117 $this->qa->set_slot(1);
118 $this->assertEquals(array('omval_response1' => 1, 'omval_response2' => 666, 'omact_gen_14' => 'Check'),
119 $this->qa->get_all_submitted_qt_vars(array(
120 'qMDOgzdhS4W:1_omval_response1' => 1,
121 'qMDOgzdhS4W:1_omval_response2' => 666,
122 'qMDOgzdhS4W:1_omact_gen_14' => 'Check',
123 )));
124 }
603bd001 125}