Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of the Zoom plugin for 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
 * Unit tests for supporting advanced password requirements in Zoom.
19
 *
20
 * @package    mod_zoom
21
 * @copyright  2020 UC Regents
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace mod_zoom;
26
 
27
use basic_testcase;
28
 
29
/**
30
 * PHPunit testcase class.
31
 */
32
final class advanced_passcode_test extends basic_testcase {
33
    /**
34
     * Fake data from get_user_security_settings().
35
     * @var object
36
     */
37
    private $zoomdata;
38
 
39
    // phpcs:disable moodle.NamingConventions.ValidFunctionName.LowercaseMethod
40
    /**
41
     * Backward compatibility support for PHPUnit 8 (PHP 7.2 and 7.3).
42
     *
43
     * @param string $pattern Regular expression.
44
     * @param string $string String.
45
     * @param string $message Message.
46
     */
47
    public static function assertMatchesRegularExpression($pattern, $string, $message = ''): void {
48
        // phpcs:enable
49
        if (method_exists('basic_testcase', 'assertMatchesRegularExpression')) {
50
            parent::assertMatchesRegularExpression($pattern, $string, $message);
51
        } else {
52
            parent::assertRegExp($pattern, $string, $message);
53
        }
54
    }
55
 
56
    /**
57
     * Setup to ensure that fixtures are loaded.
58
     */
59
    public static function setUpBeforeClass(): void {
60
        global $CFG;
61
        require_once($CFG->dirroot . '/mod/zoom/locallib.php');
62
    }
63
 
64
    /**
65
     * Tests that a default password of 6 numbers is created when settings are null.
66
     * @covers ::zoom_create_default_passcode
67
     */
68
    public function test_settings_default(): void {
69
        $this->zoomdata = (object) webservice::DEFAULT_MEETING_PASSWORD_REQUIREMENT;
70
 
71
        $passcode = zoom_create_default_passcode($this->zoomdata);
72
        $this->assertEquals(strlen($passcode), 6);
73
        $this->assertTrue(ctype_digit($passcode));
74
    }
75
 
76
    /**
77
     * Tests that a password has the given minimum length.
78
     * @covers ::zoom_create_default_passcode
79
     */
80
    public function test_settings_length(): void {
81
        $data = [
82
            'length' => 8,
83
            'have_letter' => false,
84
            'have_upper_and_lower_characters' => false,
85
            'have_special_character' => false,
86
        ];
87
        $this->zoomdata = (object) $data;
88
 
89
        $passcode = zoom_create_default_passcode($this->zoomdata);
90
        $this->assertEquals(strlen($passcode), 8);
91
        $this->assertTrue(ctype_digit($passcode));
92
    }
93
 
94
    /**
95
     * Tests that a password is all numbers when the setting is specified.
96
     * @covers ::zoom_create_default_passcode
97
     */
98
    public function test_settings_only_numeric(): void {
99
        $data = [
100
            'length' => 10,
101
            'have_letter' => false,
102
            'have_upper_and_lower_characters' => false,
103
            'have_special_character' => false,
104
            'only_allow_numeric' => true,
105
        ];
106
        $this->zoomdata = (object) $data;
107
 
108
        $passcode = zoom_create_default_passcode($this->zoomdata);
109
        $this->assertEquals(strlen($passcode), 10);
110
        $this->assertTrue(ctype_digit($passcode));
111
    }
112
 
113
    /**
114
     * Tests that a password has a letter when the setting is specified.
115
     * @covers ::zoom_create_default_passcode
116
     */
117
    public function test_settings_letter(): void {
118
        $data = [
119
            'length' => null,
120
            'have_letter' => true,
121
            'have_upper_and_lower_characters' => false,
122
            'have_special_character' => null,
123
        ];
124
        $this->zoomdata = (object) $data;
125
 
126
        $passcode = zoom_create_default_passcode($this->zoomdata);
127
        $this->assertEquals(strlen($passcode), 6);
128
        $this->assertMatchesRegularExpression('/\d/', $passcode);
129
        $this->assertMatchesRegularExpression('/[a-zA-Z]/', $passcode);
130
    }
131
 
132
    /**
133
     * Tests that a password has uppercase and lowercase letters when the setting is specified.
134
     * @covers ::zoom_create_default_passcode
135
     */
136
    public function test_settings_upper_and_lower_letters(): void {
137
        $data = [
138
            'length' => null,
139
            'have_letter' => true,
140
            'have_upper_and_lower_characters' => true,
141
            'have_special_character' => null,
142
        ];
143
        $this->zoomdata = (object) $data;
144
 
145
        $passcode = zoom_create_default_passcode($this->zoomdata);
146
        $this->assertEquals(strlen($passcode), 6);
147
        $this->assertMatchesRegularExpression('/\d/', $passcode);
148
        $this->assertMatchesRegularExpression('/[A-Z]/', $passcode);
149
        $this->assertMatchesRegularExpression('/[a-z]/', $passcode);
150
    }
151
 
152
    /**
153
     * Tests that a password has a special character when the setting is specified.
154
     * @covers ::zoom_create_default_passcode
155
     */
156
    public function test_settings_special_character(): void {
157
        $data = [
158
            'length' => null,
159
            'have_letter' => null,
160
            'have_upper_and_lower_characters' => null,
161
            'have_special_character' => true,
162
        ];
163
        $this->zoomdata = (object) $data;
164
 
165
        $passcode = zoom_create_default_passcode($this->zoomdata);
166
        $this->assertEquals(strlen($passcode), 6);
167
        $this->assertMatchesRegularExpression('/\d/', $passcode);
168
        $this->assertMatchesRegularExpression('/[^a-zA-Z\d]/', $passcode);
169
    }
170
 
171
    /**
172
     * Tests that a password has correct length, a letter, and a special character when setting is specified.
173
     * @covers ::zoom_create_default_passcode
174
     */
175
    public function test_settings_all(): void {
176
        $data = [
177
            'length' => 7,
178
            'have_letter' => true,
179
            'have_upper_and_lower_characters' => true,
180
            'have_special_character' => true,
181
        ];
182
        $this->zoomdata = (object) $data;
183
 
184
        $passcode = zoom_create_default_passcode($this->zoomdata);
185
        $this->assertEquals(strlen($passcode), 7);
186
        $this->assertMatchesRegularExpression('/\d/', $passcode);
187
        $this->assertMatchesRegularExpression('/[a-zA-Z]/', $passcode);
188
        $this->assertMatchesRegularExpression('/[^a-zA-Z\d]/', $passcode);
189
    }
190
 
191
    /**
192
     * Tests that the password description is correct when all settings are present.
193
     * @covers ::zoom_create_passcode_description
194
     */
195
    public function test_pasword_description_all(): void {
196
        $data = [
197
            'length' => 9,
198
            'have_letter' => true,
199
            'have_number' => true,
200
            'have_upper_and_lower_characters' => true,
201
            'have_special_character' => true,
202
            'consecutive_characters_length' => 4,
203
            'only_allow_numeric' => false,
204
        ];
205
        $this->zoomdata = (object) $data;
206
 
207
        $description = zoom_create_passcode_description($this->zoomdata);
208
        $expected = 'Passcode must include both lower and uppercase characters. Passcode must contain at least 1 number. ' .
209
         'Passcode must have at least 1 special character (@-_*). Minimum of 9 character(s). Maximum of 3 consecutive ' .
210
         'characters (abcd, 1111, 1234, etc.). Maximum of 10 characters.';
211
        $this->assertEquals($description, $expected);
212
    }
213
 
214
    /**
215
     * Tests that the password description is correct when the only numeric option is present.
216
     * @covers ::zoom_create_passcode_description
217
     */
218
    public function test_pasword_description_only_numeric(): void {
219
        $data = [
220
            'length' => 8,
221
            'have_letter' => false,
222
            'have_number' => true,
223
            'have_upper_and_lower_characters' => false,
224
            'have_special_character' => false,
225
            'consecutive_characters_length' => 0,
226
            'only_allow_numeric' => true,
227
        ];
228
        $this->zoomdata = (object) $data;
229
 
230
        $description = zoom_create_passcode_description($this->zoomdata);
231
        $expected = 'Passcode may only contain numbers and no other characters. Minimum of 8 character(s). ' .
232
            'Maximum of 10 characters.';
233
        $this->assertEquals($description, $expected);
234
    }
235
 
236
    /**
237
     * Tests that the password description is correct when the default settings are present.
238
     * @covers ::zoom_create_passcode_description
239
     */
240
    public function test_pasword_description_default(): void {
241
        $this->zoomdata = (object) webservice::DEFAULT_MEETING_PASSWORD_REQUIREMENT;
242
 
243
        $description = zoom_create_passcode_description($this->zoomdata);
244
        $expected = 'Passcode may only contain the following characters: [a-z A-Z 0-9 @ - _ *]. Maximum of 10 characters.';
245
        $this->assertEquals($description, $expected);
246
    }
247
}