validatePasswordComplexityDataProvider()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 96
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 69
c 0
b 0
f 0
dl 0
loc 96
rs 8.6763
cc 1
nc 1
nop 0

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
/*
4
 * This file is part of the Extension "fe_change_pwd" for TYPO3 CMS.
5
 *
6
 * For the full copyright and license information, please read the
7
 * LICENSE.txt file that was distributed with this source code.
8
 */
9
10
namespace Derhansen\FeChangePwd\Tests\Unit\Validation\Validator;
11
12
use Derhansen\FeChangePwd\Domain\Model\Dto\ChangePassword;
13
use Derhansen\FeChangePwd\Service\LocalizationService;
14
use Derhansen\FeChangePwd\Service\OldPasswordService;
15
use Derhansen\FeChangePwd\Service\SettingsService;
16
use Derhansen\FeChangePwd\Validation\Validator\ChangePasswordValidator;
17
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
18
19
/**
20
 * Class ChangePasswordValidatorTest
21
 */
22
class ChangePasswordValidatorTest extends UnitTestCase
23
{
24
    /**
25
     * @var ChangePasswordValidator
26
     */
27
    protected $validator;
28
29
    /**
30
     * Initialize validator
31
     */
32
    public function initialize()
33
    {
34
        $this->validator = $this->getAccessibleMock(
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getAccessibleMock...'), array(), '', false) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Derhansen\FeChangePwd\Va...ChangePasswordValidator of property $validator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
            ChangePasswordValidator::class,
36
            ['translateErrorMessage', 'getValidator'],
37
            [],
38
            '',
39
            false
40
        );
41
    }
42
43
    /**
44
     * @return array
45
     */
46
    public function validatePasswordComplexityDataProvider()
47
    {
48
        return [
49
            'no password given' => [
50
                '',
51
                '',
52
                true,
53
                [],
54
            ],
55
            'passwords not equal' => [
56
                'password1',
57
                'password2',
58
                true,
59
                [],
60
            ],
61
            'length below min length' => [
62
                'password',
63
                'password',
64
                true,
65
                [
66
                    'passwordComplexity' => [
67
                        'minLength' => 9,
68
                        'capitalCharCheck' => 0,
69
                        'lowerCaseCharCheck' => 0,
70
                        'digitCheck' => 0,
71
                        'specialCharCheck' => 0,
72
                    ],
73
                ],
74
            ],
75
            'no capital char' => [
76
                'password',
77
                'password',
78
                true,
79
                [
80
                    'passwordComplexity' => [
81
                        'minLength' => 7,
82
                        'capitalCharCheck' => 1,
83
                        'lowerCaseCharCheck' => 0,
84
                        'digitCheck' => 0,
85
                        'specialCharCheck' => 0,
86
                    ],
87
                ],
88
            ],
89
            'no lower case char' => [
90
                'PASSWORD',
91
                'PASSWORD',
92
                true,
93
                [
94
                    'passwordComplexity' => [
95
                        'minLength' => 7,
96
                        'capitalCharCheck' => 0,
97
                        'lowerCaseCharCheck' => 1,
98
                        'digitCheck' => 0,
99
                        'specialCharCheck' => 0,
100
                    ],
101
                ],
102
            ],
103
            'no digit' => [
104
                'password',
105
                'password',
106
                true,
107
                [
108
                    'passwordComplexity' => [
109
                        'minLength' => 7,
110
                        'capitalCharCheck' => 0,
111
                        'lowerCaseCharCheck' => 0,
112
                        'digitCheck' => 1,
113
                        'specialCharCheck' => 0,
114
                    ],
115
                ],
116
            ],
117
            'no special char' => [
118
                'password',
119
                'password',
120
                true,
121
                [
122
                    'passwordComplexity' => [
123
                        'minLength' => 7,
124
                        'capitalCharCheck' => 0,
125
                        'lowerCaseCharCheck' => 0,
126
                        'digitCheck' => 0,
127
                        'specialCharCheck' => 1,
128
                    ],
129
                ],
130
            ],
131
            'strong password' => [
132
                'Th!s_i$_a_$+r0ng_passw0rd#',
133
                'Th!s_i$_a_$+r0ng_passw0rd#',
134
                false,
135
                [
136
                    'passwordComplexity' => [
137
                        'minLength' => 20,
138
                        'capitalCharCheck' => 1,
139
                        'lowerCaseCharCheck' => 1,
140
                        'digitCheck' => 1,
141
                        'specialCharCheck' => 1,
142
                    ],
143
                ],
144
            ],
145
        ];
146
    }
147
148
    /**
149
     * @test
150
     * @dataProvider validatePasswordComplexityDataProvider
151
     */
152
    public function validatePasswordComplexityTest($password1, $password2, $expected, $settings)
153
    {
154
        $this->initialize();
155
156
        $changePassword = new ChangePassword();
157
        $changePassword->setPassword1($password1);
158
        $changePassword->setPassword2($password2);
159
160
        $mockSettingsService = static::getMockBuilder(SettingsService::class)
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::getMockBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

160
        $mockSettingsService = static::/** @scrutinizer ignore-call */ getMockBuilder(SettingsService::class)
Loading history...
161
            ->disableOriginalConstructor()
162
            ->getMock();
163
        $mockSettingsService->expects(self::once())->method('getSettings')->willReturn($settings);
164
        $this->validator->_set('settingsService', $mockSettingsService);
165
166
        $mockLocalizationService = static::getMockBuilder(LocalizationService::class)
167
            ->disableOriginalConstructor()
168
            ->getMock();
169
        $mockLocalizationService->expects(self::any())->method('translate')->willReturn('');
170
        $this->validator->_set('localizationService', $mockLocalizationService);
171
172
        self::assertEquals($expected, $this->validator->validate($changePassword)->hasErrors());
173
    }
174
175
    /**
176
     * @test
177
     */
178
    public function noCurrentPasswordGivenTest()
179
    {
180
        $this->initialize();
181
182
        $changePassword = new ChangePassword();
183
        $changePassword->setCurrentPassword('');
184
185
        $settings = [
186
            'requireCurrentPassword' => [
187
                'enabled' => 1,
188
            ],
189
        ];
190
191
        $mockSettingsService = static::getMockBuilder(SettingsService::class)
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::getMockBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

191
        $mockSettingsService = static::/** @scrutinizer ignore-call */ getMockBuilder(SettingsService::class)
Loading history...
192
            ->disableOriginalConstructor()
193
            ->getMock();
194
        $mockSettingsService->expects(self::once())->method('getSettings')->willReturn($settings);
195
        $this->validator->_set('settingsService', $mockSettingsService);
196
197
        $mockLocalizationService = static::getMockBuilder(LocalizationService::class)
198
            ->disableOriginalConstructor()
199
            ->getMock();
200
        $mockLocalizationService->expects(self::any())->method('translate')->willReturn('');
201
        $this->validator->_set('localizationService', $mockLocalizationService);
202
203
        self::assertEquals(1570880411, $this->validator->validate($changePassword)->getErrors()[0]->getCode());
204
    }
205
206
    /**
207
     * @test
208
     */
209
    public function currentPasswordWrongTest()
210
    {
211
        $this->initialize();
212
213
        $changePassword = new ChangePassword();
214
        $changePassword->setCurrentPassword('invalid');
215
216
        $settings = [
217
            'requireCurrentPassword' => [
218
                'enabled' => 1,
219
            ],
220
        ];
221
222
        $mockSettingsService = static::getMockBuilder(SettingsService::class)
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::getMockBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

222
        $mockSettingsService = static::/** @scrutinizer ignore-call */ getMockBuilder(SettingsService::class)
Loading history...
223
            ->disableOriginalConstructor()
224
            ->getMock();
225
        $mockSettingsService->expects(self::once())->method('getSettings')->willReturn($settings);
226
        $this->validator->_set('settingsService', $mockSettingsService);
227
228
        $mockLocalizationService = static::getMockBuilder(LocalizationService::class)
229
            ->disableOriginalConstructor()
230
            ->getMock();
231
        $mockLocalizationService->expects(self::any())->method('translate')->willReturn('');
232
        $this->validator->_set('localizationService', $mockLocalizationService);
233
234
        $mockOldPasswordService = static::getMockBuilder(OldPasswordService::class)
235
            ->disableOriginalConstructor()
236
            ->getMock();
237
        $mockOldPasswordService->expects(self::any())->method('checkEqualsOldPassword')
238
            ->willReturn(false);
239
        $this->validator->_set('oldPasswordService', $mockOldPasswordService);
240
241
        self::assertEquals(1570880417, $this->validator->validate($changePassword)->getErrors()[0]->getCode());
242
    }
243
244
    /**
245
     * @test
246
     */
247
    public function currentPasswordValidationSkipped()
248
    {
249
        $this->initialize();
250
251
        $changePassword = new ChangePassword();
252
        $changePassword->setCurrentPassword('123456');
253
        $changePassword->setSkipCurrentPasswordCheck(true);
254
255
        $settings = [
256
            'requireCurrentPassword' => [
257
                'enabled' => 1,
258
            ],
259
        ];
260
261
        $mockSettingsService = static::getMockBuilder(SettingsService::class)
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::getMockBuilder() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

261
        $mockSettingsService = static::/** @scrutinizer ignore-call */ getMockBuilder(SettingsService::class)
Loading history...
262
            ->disableOriginalConstructor()
263
            ->getMock();
264
        $mockSettingsService->expects(self::once())->method('getSettings')->willReturn($settings);
265
        $this->validator->_set('settingsService', $mockSettingsService);
266
267
        $mockLocalizationService = static::getMockBuilder(LocalizationService::class)
268
            ->disableOriginalConstructor()
269
            ->getMock();
270
        $mockLocalizationService->expects(self::any())->method('translate')->willReturn('');
271
        $this->validator->_set('localizationService', $mockLocalizationService);
272
273
        self::assertEquals(1537701950, $this->validator->validate($changePassword)->getErrors()[0]->getCode());
274
    }
275
}
276