Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | 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
 */
1441 ariadna 40
final class questionattempt_test extends \advanced_testcase {
1 efrain 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 {
1441 ariadna 49
        parent::setUp();
1 efrain 50
        $this->question = \test_question_maker::make_question('description');
51
        $this->question->defaultmark = 3;
52
        $this->usageid = 13;
53
        $this->qa = new question_attempt($this->question, $this->usageid);
54
    }
55
 
11 efrain 56
    public function test_constructor_sets_maxmark(): void {
1 efrain 57
        $qa = new question_attempt($this->question, $this->usageid);
58
        $this->assertSame($this->question, $qa->get_question(false));
59
        $this->assertEquals(3, $qa->get_max_mark());
60
    }
61
 
11 efrain 62
    public function test_maxmark_beats_default_mark(): void {
1 efrain 63
        $qa = new question_attempt($this->question, $this->usageid, null, 2);
64
        $this->assertEquals(2, $qa->get_max_mark());
65
    }
66
 
11 efrain 67
    public function test_get_set_slot(): void {
1 efrain 68
        $this->qa->set_slot(7);
69
        $this->assertEquals(7, $this->qa->get_slot());
70
    }
71
 
11 efrain 72
    public function test_fagged_initially_false(): void {
1 efrain 73
        $this->assertEquals(false, $this->qa->is_flagged());
74
    }
75
 
11 efrain 76
    public function test_set_is_flagged(): void {
1 efrain 77
        $this->qa->set_flagged(true);
78
        $this->assertEquals(true, $this->qa->is_flagged());
79
    }
80
 
11 efrain 81
    public function test_get_qt_field_name(): void {
1 efrain 82
        $name = $this->qa->get_qt_field_name('test');
83
        $this->assertMatchesRegularExpression('/^' . preg_quote($this->qa->get_field_prefix(), '/') . '/', $name);
84
        $this->assertMatchesRegularExpression('/_test$/', $name);
85
    }
86
 
11 efrain 87
    public function test_get_behaviour_field_name(): void {
1 efrain 88
        $name = $this->qa->get_behaviour_field_name('test');
89
        $this->assertMatchesRegularExpression('/^' . preg_quote($this->qa->get_field_prefix(), '/') . '/', $name);
90
        $this->assertMatchesRegularExpression('/_-test$/', $name);
91
    }
92
 
11 efrain 93
    public function test_get_field_prefix(): void {
1 efrain 94
        $this->qa->set_slot(7);
95
        $name = $this->qa->get_field_prefix();
96
        $this->assertMatchesRegularExpression('/' . preg_quote($this->usageid, '/') . '/', $name);
97
        $this->assertMatchesRegularExpression('/' . preg_quote($this->qa->get_slot(), '/') . '/', $name);
98
    }
99
 
11 efrain 100
    public function test_get_submitted_var_not_present_var_returns_null(): void {
1 efrain 101
        $this->assertNull($this->qa->get_submitted_var(
102
                'reallyunlikelyvariablename', PARAM_BOOL));
103
    }
104
 
11 efrain 105
    public function test_get_all_submitted_qt_vars(): void {
1 efrain 106
        $this->qa->set_usage_id('MDOgzdhS4W');
107
        $this->qa->set_slot(1);
108
        $this->assertEquals(array('omval_response1' => 1, 'omval_response2' => 666, 'omact_gen_14' => 'Check'),
109
                $this->qa->get_all_submitted_qt_vars(array(
110
                    'qMDOgzdhS4W:1_omval_response1' => 1,
111
                    'qMDOgzdhS4W:1_omval_response2' => 666,
112
                    'qMDOgzdhS4W:1_omact_gen_14' => 'Check',
113
                )));
114
    }
115
}