Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace core_question;
18
 
19
use question_attempt;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
global $CFG;
24
require_once(__DIR__ . '/../lib.php');
25
require_once(__DIR__ . '/helpers.php');
26
 
27
/**
28
 * Unit tests for the {@link question_attempt} class.
29
 *
30
 * Action methods like start, process_action and finish are assumed to be
31
 * tested by walkthrough tests in the various behaviours.
32
 *
33
 * These are the tests that don't require any steps.
34
 *
35
 * @package    core_question
36
 * @category   test
37
 * @copyright  2009 The Open University
38
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
40
class questionattempt_test extends \advanced_testcase {
41
    /** @var question_definition a question that can be used in the tests. */
42
    private $question;
43
    /** @var int fake question_usage id used in some tests. */
44
    private $usageid;
45
    /** @var question_attempt a question attempt that can be used in the tests. */
46
    private $qa;
47
 
48
    protected function setUp(): void {
49
        $this->question = \test_question_maker::make_question('description');
50
        $this->question->defaultmark = 3;
51
        $this->usageid = 13;
52
        $this->qa = new question_attempt($this->question, $this->usageid);
53
    }
54
 
55
    public function test_constructor_sets_maxmark() {
56
        $qa = new question_attempt($this->question, $this->usageid);
57
        $this->assertSame($this->question, $qa->get_question(false));
58
        $this->assertEquals(3, $qa->get_max_mark());
59
    }
60
 
61
    public function test_maxmark_beats_default_mark() {
62
        $qa = new question_attempt($this->question, $this->usageid, null, 2);
63
        $this->assertEquals(2, $qa->get_max_mark());
64
    }
65
 
66
    public function test_get_set_slot() {
67
        $this->qa->set_slot(7);
68
        $this->assertEquals(7, $this->qa->get_slot());
69
    }
70
 
71
    public function test_fagged_initially_false() {
72
        $this->assertEquals(false, $this->qa->is_flagged());
73
    }
74
 
75
    public function test_set_is_flagged() {
76
        $this->qa->set_flagged(true);
77
        $this->assertEquals(true, $this->qa->is_flagged());
78
    }
79
 
80
    public function test_get_qt_field_name() {
81
        $name = $this->qa->get_qt_field_name('test');
82
        $this->assertMatchesRegularExpression('/^' . preg_quote($this->qa->get_field_prefix(), '/') . '/', $name);
83
        $this->assertMatchesRegularExpression('/_test$/', $name);
84
    }
85
 
86
    public function test_get_behaviour_field_name() {
87
        $name = $this->qa->get_behaviour_field_name('test');
88
        $this->assertMatchesRegularExpression('/^' . preg_quote($this->qa->get_field_prefix(), '/') . '/', $name);
89
        $this->assertMatchesRegularExpression('/_-test$/', $name);
90
    }
91
 
92
    public function test_get_field_prefix() {
93
        $this->qa->set_slot(7);
94
        $name = $this->qa->get_field_prefix();
95
        $this->assertMatchesRegularExpression('/' . preg_quote($this->usageid, '/') . '/', $name);
96
        $this->assertMatchesRegularExpression('/' . preg_quote($this->qa->get_slot(), '/') . '/', $name);
97
    }
98
 
99
    public function test_get_submitted_var_not_present_var_returns_null() {
100
        $this->assertNull($this->qa->get_submitted_var(
101
                'reallyunlikelyvariablename', PARAM_BOOL));
102
    }
103
 
104
    public function test_get_all_submitted_qt_vars() {
105
        $this->qa->set_usage_id('MDOgzdhS4W');
106
        $this->qa->set_slot(1);
107
        $this->assertEquals(array('omval_response1' => 1, 'omval_response2' => 666, 'omact_gen_14' => 'Check'),
108
                $this->qa->get_all_submitted_qt_vars(array(
109
                    'qMDOgzdhS4W:1_omval_response1' => 1,
110
                    'qMDOgzdhS4W:1_omval_response2' => 666,
111
                    'qMDOgzdhS4W:1_omact_gen_14' => 'Check',
112
                )));
113
    }
114
}