Passed
Pull Request — master (#416)
by Sergei
04:39 queued 01:56
created

EmailTest::dataValidationPassed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 64
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 52
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 64
rs 9.0472

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\TestEnvironments\WithIntl\Rule;
6
7
use Yiisoft\Validator\Rule\Email;
8
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
9
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
10
11
final class EmailTest extends RuleTestCase
12
{
13
    use RuleWithOptionsTestTrait;
14
15
    public function dataOptions(): array
16
    {
17
        return [
18
            [
19
                new Email(),
20
                [
21
                    'pattern' => '/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/',
22
                    'fullPattern' => '/^[^@]*<[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?>$/',
23
                    'idnEmailPattern' => '/^([a-zA-Z0-9._%+-]+)@((\[\d{1,3}\.\d{1,3}\.\d{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|\d{1,3})(\]?)$/',
24
                    'allowName' => false,
25
                    'checkDNS' => false,
26
                    'enableIDN' => false,
27
                    'incorrectInputMessage' => [
28
                        'template' => 'The value must have a string type.',
29
                        'parameters' => [],
30
                    ],
31
                    'message' => [
32
                        'template' => 'This value is not a valid email address.',
33
                        'parameters' => [],
34
                    ],
35
                    'skipOnEmpty' => false,
36
                    'skipOnError' => false,
37
                ],
38
            ],
39
            [
40
                new Email(allowName: true),
41
                [
42
                    'pattern' => '/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/',
43
                    'fullPattern' => '/^[^@]*<[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?>$/',
44
                    'idnEmailPattern' => '/^([a-zA-Z0-9._%+-]+)@((\[\d{1,3}\.\d{1,3}\.\d{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|\d{1,3})(\]?)$/',
45
                    'allowName' => true,
46
                    'checkDNS' => false,
47
                    'enableIDN' => false,
48
                    'incorrectInputMessage' => [
49
                        'template' => 'The value must have a string type.',
50
                        'parameters' => [],
51
                    ],
52
                    'message' => [
53
                        'template' => 'This value is not a valid email address.',
54
                        'parameters' => [],
55
                    ],
56
                    'skipOnEmpty' => false,
57
                    'skipOnError' => false,
58
                ],
59
            ],
60
            [
61
                new Email(allowName: true, checkDNS: true),
62
                [
63
                    'pattern' => '/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/',
64
                    'fullPattern' => '/^[^@]*<[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?>$/',
65
                    'idnEmailPattern' => '/^([a-zA-Z0-9._%+-]+)@((\[\d{1,3}\.\d{1,3}\.\d{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|\d{1,3})(\]?)$/',
66
                    'allowName' => true,
67
                    'checkDNS' => true,
68
                    'enableIDN' => false,
69
                    'incorrectInputMessage' => [
70
                        'template' => 'The value must have a string type.',
71
                        'parameters' => [],
72
                    ],
73
                    'message' => [
74
                        'template' => 'This value is not a valid email address.',
75
                        'parameters' => [],
76
                    ],
77
                    'skipOnEmpty' => false,
78
                    'skipOnError' => false,
79
                ],
80
            ],
81
            [
82
                new Email(allowName: true, enableIDN: true),
83
                [
84
                    'pattern' => '/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/',
85
                    'fullPattern' => '/^[^@]*<[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?>$/',
86
                    'idnEmailPattern' => '/^([a-zA-Z0-9._%+-]+)@((\[\d{1,3}\.\d{1,3}\.\d{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|\d{1,3})(\]?)$/',
87
                    'allowName' => true,
88
                    'checkDNS' => false,
89
                    'enableIDN' => true,
90
                    'incorrectInputMessage' => [
91
                        'template' => 'The value must have a string type.',
92
                        'parameters' => [],
93
                    ],
94
                    'message' => [
95
                        'template' => 'This value is not a valid email address.',
96
                        'parameters' => [],
97
                    ],
98
                    'skipOnEmpty' => false,
99
                    'skipOnError' => false,
100
                ],
101
            ],
102
        ];
103
    }
104
105
    public function dataValidationPassed(): array
106
    {
107
        $rule = new Email();
108
        $ruleAllowedName = new Email(allowName: true);
109
        $ruleEnabledIDN = new Email(enableIDN: true);
110
        $ruleEnabledIDNandAllowedName = new Email(allowName: true, enableIDN: true);
111
112
        return [
113
            ['[email protected]', [$rule]],
114
            ['[email protected]', [$rule]],
115
            ['[email protected]', [$rule]],
116
            ['[email protected]', [$rule]],
117
            ['user+mailbox/[email protected]', [$rule]],
118
            ['!#$%&\'*+-/=?^_`.{|}[email protected]', [$rule]],
119
            ['[email protected]', [$rule]], // checkDNS is disabled
120
            ['[email protected]', [$rule]],
121
            [str_repeat('a', 64) . '@gmail.com', [$rule]],
122
            ['name@' . str_repeat('a', 245) . '.com', [$rule]],
123
            ['[email protected]', [$rule]],
124
125
            ['[email protected]', [$ruleAllowedName]],
126
            ['[email protected]', [$ruleAllowedName]],
127
            ['[email protected]', [$ruleAllowedName]],
128
            ['Carsten Brandt <[email protected]>', [$ruleAllowedName]],
129
            ['"Carsten Brandt" <[email protected]>', [$ruleAllowedName]],
130
            ['<[email protected]>', [$ruleAllowedName]],
131
            ['[email protected]', [$ruleAllowedName]],
132
            ['John Smith <[email protected]>', [$ruleAllowedName]],
133
            [
134
                '"This name is longer than 64 characters. Blah blah blah blah blah" <[email protected]>',
135
                [$ruleAllowedName],
136
            ],
137
138
            ['[email protected]', [$ruleEnabledIDN]],
139
            ['[email protected]', [$ruleEnabledIDN]],
140
            ['example@äüößìà.de', [$ruleEnabledIDN]],
141
            ['[email protected]', [$ruleEnabledIDN]],
142
            ['info@örtliches.de', [$ruleEnabledIDN]],
143
            ['sam@рмкреатиф.ru', [$ruleEnabledIDN]],
144
            ['[email protected]', [$ruleEnabledIDN]],
145
            ['[email protected]', [$ruleEnabledIDN]],
146
            ['üñîçøðé@üñîçøðé.com', [$ruleEnabledIDN]],
147
148
            ['info@örtliches.de', [$ruleEnabledIDNandAllowedName]],
149
            ['Information <info@örtliches.de>', [$ruleEnabledIDNandAllowedName]],
150
            ['sam@рмкреатиф.ru', [$ruleEnabledIDNandAllowedName]],
151
            ['[email protected]', [$ruleEnabledIDNandAllowedName]],
152
            ['[email protected]', [$ruleEnabledIDNandAllowedName]],
153
            ['Carsten Brandt <[email protected]>', [$ruleEnabledIDNandAllowedName]],
154
            ['"Carsten Brandt" <[email protected]>', [$ruleEnabledIDNandAllowedName]],
155
            ['üñîçøðé 日本国 <üñîçøðé@üñîçøðé.com>', [$ruleEnabledIDNandAllowedName]],
156
            ['<[email protected]>', [$ruleEnabledIDNandAllowedName]],
157
            ['[email protected]', [$ruleEnabledIDNandAllowedName]],
158
            ['John Smith <[email protected]>', [$ruleEnabledIDNandAllowedName]],
159
            [
160
                '"Такое имя достаточно длинное, но оно все равно может пройти валидацию" <[email protected]>',
161
                [$ruleEnabledIDNandAllowedName],
162
            ],
163
164
            ['[email protected]', [new Email(checkDNS: true)]],
165
166
            ['[email protected]', [new Email(allowName: true, checkDNS: true)]],
167
            ['Ivan Petrov <[email protected]>', [new Email(allowName: true, checkDNS: true)]],
168
            ['name@ñandu.cl', [new Email(checkDNS: true, enableIDN: true)]],
169
        ];
170
    }
171
172
    public function dataValidationFailed(): array
173
    {
174
        $rule = new Email();
175
        $ruleAllowedName = new Email(allowName: true);
176
        $ruleEnabledIDN = new Email(enableIDN: true);
177
        $ruleEnabledIDNandAllowedName = new Email(allowName: true, enableIDN: true);
178
        $errors = ['' => ['This value is not a valid email address.']];
179
        $incorrectInputErrors = ['' => ['The value must have a string type.']];
180
181
        return [
182
            'incorrect input, integer' => [1, [$rule], $incorrectInputErrors],
183
            'incorrect input, array containing string element' => [
184
                ['[email protected]'],
185
                [$ruleAllowedName],
186
                $incorrectInputErrors,
187
            ],
188
            'custom incorrect input message' => [
189
                1,
190
                [new Email(incorrectInputMessage: 'Custom incorrect input message.')],
191
                ['' => ['Custom incorrect input message.']],
192
            ],
193
            'custom incorrect input message with parameters' => [
194
                1,
195
                [new Email(incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')],
196
                ['' => ['Attribute - , type - int.']],
197
            ],
198
            'custom incorrect input message with parameters, attribute set' => [
199
                ['data' => 1],
200
                ['data' => [new Email(incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')]],
201
                ['data' => ['Attribute - data, type - int.']],
202
            ],
203
204
            ['rmcreative.ru', [$rule], $errors],
205
            ['Carsten Brandt <[email protected]>', [$rule], $errors],
206
            ['"Carsten Brandt" <[email protected]>', [$rule], $errors],
207
            ['<[email protected]>', [$rule], $errors],
208
            ['info@örtliches.de', [$rule], $errors],
209
            ['sam@рмкреатиф.ru', [$rule], $errors],
210
            ['[email protected]', [$rule], $errors],
211
            [str_repeat('a', 65) . '@gmail.com', [$rule], $errors],
212
            ['name@' . str_repeat('a', 246) . '.com', [$rule], $errors],
213
214
            // Malicious email addresses that can be used to exploit SwiftMailer vulnerability CVE-2016-10074 while IDN
215
            // is disabled.
216
            // https://legalhackers.com/advisories/SwiftMailer-Exploit-Remote-Code-Exec-CVE-2016-10074-Vuln.html
217
218
            // This is the demo email used in the proof of concept of the exploit
219
            ['"attacker\" -oQ/tmp/ -X/var/www/cache/phpcode.php "@email.com', [$rule], $errors],
220
221
            // Trying more addresses
222
            ['"Attacker -Param2 -Param3"@test.com', [$rule], $errors],
223
            ['\'Attacker -Param2 -Param3\'@test.com', [$rule], $errors],
224
            ['"Attacker \" -Param2 -Param3"@test.com', [$rule], $errors],
225
            ["'Attacker \\' -Param2 -Param3'@test.com", [$rule], $errors],
226
            ['"attacker\" -oQ/tmp/ -X/var/www/cache/phpcode.php "@email.com', [$rule], $errors],
227
228
            // And even more variants
229
            ['"attacker\"\ -oQ/tmp/\ -X/var/www/cache/phpcode.php"@email.com', [$rule], $errors],
230
            ["\"attacker\\\"\0-oQ/tmp/\0-X/var/www/cache/phpcode.php\"@email.com", [$rule], $errors],
231
            ['"[email protected]\"-Xbeep"@email.com', [$rule], $errors],
232
            ["'attacker\\' -oQ/tmp/ -X/var/www/cache/phpcode.php'@email.com", [$rule], $errors],
233
            ["'attacker\\\\' -oQ/tmp/ -X/var/www/cache/phpcode.php'@email.com", [$rule], $errors],
234
            ["'attacker\\\\'\\ -oQ/tmp/ -X/var/www/cache/phpcode.php'@email.com", [$rule], $errors],
235
            ["'attacker\\';touch /tmp/hackme'@email.com", [$rule], $errors],
236
            ["'attacker\\\\';touch /tmp/hackme'@email.com", [$rule], $errors],
237
            ["'attacker\\';touch/tmp/hackme'@email.com", [$rule], $errors],
238
            ["'attacker\\\\';touch/tmp/hackme'@email.com", [$rule], $errors],
239
            ['"attacker\" -oQ/tmp/ -X/var/www/cache/phpcode.php "@email.com', [$rule], $errors],
240
241
            ['rmcreative.ru', [$ruleAllowedName], $errors],
242
            ['info@örtliches.de', [$ruleAllowedName], $errors],
243
            ['üñîçøðé@üñîçøðé.com', [$ruleAllowedName], $errors],
244
            ['sam@рмкреатиф.ru', [$ruleAllowedName], $errors],
245
            ['Informtation [email protected]', [$ruleAllowedName], $errors],
246
            ['John Smith <example.com>', [$ruleAllowedName], $errors],
247
            [
248
                'Short Name <localPartMoreThan64Characters-blah-blah-blah-blah-blah-blah-blah-blah@example.com>',
249
                [$ruleAllowedName],
250
                $errors,
251
            ],
252
            [
253
                'Short Name <domainNameIsMoreThan254Characters@example-blah-blah-blah-blah-blah-blah-blah-blah-blah-' .
254
                'blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-' .
255
                'blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah' .
256
                '.com>',
257
                [$ruleAllowedName],
258
                $errors,
259
            ],
260
261
            ['rmcreative.ru', [$ruleEnabledIDN], $errors],
262
            ['Carsten Brandt <[email protected]>', [$ruleEnabledIDN], $errors],
263
            ['"Carsten Brandt" <[email protected]>', [$ruleEnabledIDN], $errors],
264
            ['<[email protected]>', [$ruleEnabledIDN], $errors],
265
266
            [
267
                'Короткое имя <тест@это-доменное-имя.после-преобразования-в-idn.будет-содержать-больше-254-символов.' .
268
                'бла-бла-бла-бла-бла-бла-бла-бла.бла-бла-бла-бла-бла-бла.бла-бла-бла-бла-бла-бла.бла-бла-бла-бла-бла-' .
269
                'бла.com>',
270
                [$ruleEnabledIDNandAllowedName],
271
                $errors,
272
            ],
273
            ['Information info@örtliches.de', [$ruleEnabledIDNandAllowedName], $errors],
274
            ['rmcreative.ru', [$ruleEnabledIDNandAllowedName], $errors],
275
            ['John Smith <example.com>', [$ruleEnabledIDNandAllowedName], $errors],
276
            [
277
                'Короткое имя <после-преобразования-в-idn-тут-будет-больше-чем-64-символа@пример.com>',
278
                [$ruleEnabledIDNandAllowedName],
279
                $errors,
280
            ],
281
282
            ['name@ñandu.cl', [new Email(checkDNS: true)], $errors],
283
            ['gmail.con', [new Email(checkDNS: true)], $errors],
284
            [
285
                '[email protected]',
286
                [new Email(checkDNS: true)],
287
                $errors,
288
            ],
289
290
            'custom message' => [
291
                '[email protected]',
292
                [new Email(checkDNS: true, message: 'Custom message.')],
293
                ['' => ['Custom message.']],
294
            ],
295
            'custom message with parameters' => [
296
                '[email protected]',
297
                [new Email(checkDNS: true, message: 'Attribute - {attribute}, value - {value}.')],
298
                ['' => ['Attribute - , value - [email protected].']],
299
            ],
300
            'custom message with parameters, attribute set' => [
301
                ['data' => '[email protected]'],
302
                ['data' => new Email(checkDNS: true, message: 'Attribute - {attribute}, value - {value}.')],
303
                ['data' => ['Attribute - data, value - [email protected].']],
304
            ],
305
        ];
306
    }
307
}
308