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
/**
18
 * PHPUnit tool_brickfield tests
19
 *
20
 * @package   tool_brickfield
21
 * @copyright  2020 onward: Brickfield Education Labs, www.brickfield.ie
22
 * @author     Mike Churchward (mike@brickfieldlabs.ie)
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
namespace tool_brickfield;
26
 
27
/**
28
 * Unit tests for {@registration tool_brickfield\registration.php}.
29
 * @group tool_brickfield
30
 */
1441 ariadna 31
final class registration_test extends \advanced_testcase {
1 efrain 32
    public static function setUpBeforeClass(): void {
33
        global $CFG;
34
 
35
        require_once($CFG->dirroot . '/admin/tool/brickfield/tests/generator/mock_registration.php');
36
        require_once($CFG->dirroot . '/admin/tool/brickfield/tests/generator/mock_brickfieldconnect.php');
1441 ariadna 37
        parent::setUpBeforeClass();
1 efrain 38
    }
39
 
40
    /**
41
     * Tests the state of the registration system when first installed.
42
     * @throws \dml_exception
43
     */
11 efrain 44
    public function test_initial_state(): void {
1 efrain 45
        $this->resetAfterTest();
46
        $regobj = new mock_registration();
47
 
48
        // Initial state of system.
49
        $this->assertFalse($regobj->toolkit_is_active());
50
        $this->assertFalse($regobj->validation_pending());
51
        $this->assertFalse($regobj->validation_error());
52
        $this->assertEmpty($regobj->get_api_key());
53
        $this->assertEmpty($regobj->get_secret_key());
54
    }
55
 
56
    /**
57
     * Test the various states for setting registration keys.
58
     * @throws \dml_exception
59
     */
11 efrain 60
    public function test_set_keys_for_registration(): void {
1 efrain 61
        $this->resetAfterTest();
62
        $regobj = new mock_registration();
63
 
64
        // State when invalid format keys are sent.
65
        $this->assertFalse($regobj->set_keys_for_registration('123', 'abc'));
66
        $this->assertTrue($regobj->is_not_entered());
67
        $this->assertFalse($regobj->validation_pending());
68
        $this->assertEmpty($regobj->get_api_key());
69
        $this->assertEmpty($regobj->get_secret_key());
70
 
71
        // State when valid format keys are sent.
72
        $this->assertTrue($regobj->set_keys_for_registration(mock_brickfieldconnect::VALIDAPIKEY,
73
            mock_brickfieldconnect::VALIDSECRETKEY));
74
        $this->assertTrue($regobj->validation_pending());
75
        $this->assertEquals($regobj->get_api_key(), mock_brickfieldconnect::VALIDAPIKEY);
76
        $this->assertEquals($regobj->get_secret_key(), mock_brickfieldconnect::VALIDSECRETKEY);
77
    }
78
 
79
    /**
80
     * Test the validation system through its several states.
81
     * @throws \dml_exception
82
     */
11 efrain 83
    public function test_validation(): void {
1 efrain 84
        $this->resetAfterTest();
85
        $regobj = new mock_registration();
86
 
87
        // Set invalid format keys and validate the system.
88
        $this->assertFalse($regobj->set_keys_for_registration('123', 'abc'));
89
        // Run validate function. State should end up as 'NOT_ENTERED'.
90
        $this->assertFalse($regobj->validate());
91
        $this->assertTrue($regobj->is_not_entered());
92
 
93
        // Set valid keys and validate the system.
94
        $this->assertTrue($regobj->set_keys_for_registration(mock_brickfieldconnect::VALIDAPIKEY,
95
            mock_brickfieldconnect::VALIDSECRETKEY));
96
        // Run validate function. State should end up as valid, 'VALIDATED'.
97
        $this->assertTrue($regobj->validate());
98
        $this->assertTrue($regobj->toolkit_is_active());
99
        $this->assertFalse($regobj->validation_pending());
100
        $this->assertFalse($regobj->validation_error());
101
 
102
        // Set invalid keys and validate the system.
103
        $this->assertTrue($regobj->set_keys_for_registration('123456789012345678901234567890cd',
104
            'cd123456789012345678901234567890'));
105
        // Run validate function. State should end up as valid, not validated, 'ERROR'.
106
        $this->assertTrue($regobj->validate());
107
        $this->assertTrue($regobj->toolkit_is_active());
108
        $this->assertTrue($regobj->validation_pending());
109
        $this->assertTrue($regobj->validation_error());
110
    }
111
 
112
    /**
113
     * Tests the system after validation grace periods expire.
114
     * @throws \dml_exception
115
     */
11 efrain 116
    public function test_validation_time_expiry(): void {
1 efrain 117
        $this->resetAfterTest();
118
        $regobj = new mock_registration();
119
 
120
        // Set valid keys and validate the system.
121
        $this->assertTrue($regobj->set_keys_for_registration(mock_brickfieldconnect::VALIDAPIKEY,
122
            mock_brickfieldconnect::VALIDSECRETKEY));
123
        // Run validate function. State should end up as valid, 'VALIDATED'.
124
        $this->assertTrue($regobj->validate());
125
        $this->assertTrue($regobj->toolkit_is_active());
126
 
127
        // Invalidate the validation time.
128
        $regobj->invalidate_validation_time();
129
        // Run validate function. State should end up as valid, 'VALIDATED'.
130
        $this->assertTrue($regobj->validate());
131
        $this->assertTrue($regobj->toolkit_is_active());
132
 
133
        // Set invalid keys and validate the system.
134
        $this->assertTrue($regobj->set_keys_for_registration('123456789012345678901234567890cd',
135
            'cd123456789012345678901234567890'));
136
        // Run validate function. State should end up as valid, not validated, 'ERROR'.
137
        $this->assertTrue($regobj->validate());
138
        $this->assertTrue($regobj->toolkit_is_active());
139
        $this->assertTrue($regobj->validation_pending());
140
        $this->assertTrue($regobj->validation_error());
141
 
142
        // Invalidate the validation time.
143
        $regobj->invalidate_validation_time();
144
        // Run validate function. State should end up as  not valid.
145
        $this->assertFalse($regobj->validate());
146
        $this->assertFalse($regobj->toolkit_is_active());
147
    }
148
 
149
    /**
150
     * Tests the system after summary data time periods expire.
151
     * @throws \dml_exception
152
     */
11 efrain 153
    public function test_summary_time_expiry(): void {
1 efrain 154
        $this->resetAfterTest();
155
        $regobj = new mock_registration();
156
 
157
        // Set valid keys and validate the system.
158
        $this->assertTrue($regobj->set_keys_for_registration(mock_brickfieldconnect::VALIDAPIKEY,
159
            mock_brickfieldconnect::VALIDSECRETKEY));
160
        // Run validate function. State should end up as valid, 'VALIDATED'.
161
        $this->assertTrue($regobj->validate());
162
        $this->assertTrue($regobj->toolkit_is_active());
163
 
164
        // Invalidate the summary time.
165
        $regobj->invalidate_summary_time();
166
        // Run validate function. State should end up as not valid.
167
        $this->assertFalse($regobj->validate());
168
        $this->assertFalse($regobj->toolkit_is_active());
169
 
170
        // Set invalid keys and validate the system.
171
        $this->assertTrue($regobj->set_keys_for_registration('123456789012345678901234567890cd',
172
            'cd123456789012345678901234567890'));
173
        // Run validate function. State should end up as invalid.
174
        $this->assertFalse($regobj->validate());
175
        $this->assertFalse($regobj->toolkit_is_active());
176
 
177
        // Set invalid keys and validate the system.
178
        $this->assertTrue($regobj->set_keys_for_registration('123456789012345678901234567890cd',
179
            'cd123456789012345678901234567890'));
180
        // Mark the summary data as sent, and revalidate the system.
181
        $regobj->mark_summary_data_sent();
182
        // Run validate function. State should end up as valid, not validated, 'ERROR'.
183
        $this->assertTrue($regobj->validate());
184
        $this->assertTrue($regobj->toolkit_is_active());
185
        $this->assertTrue($regobj->validation_pending());
186
        $this->assertTrue($regobj->validation_error());
187
    }
188
}