Completed
Push — master ( 504537...babb2f )
by Torben
04:05
created

ChangePasswordValidatorTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 136
c 1
b 0
f 0
dl 0
loc 224
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B validatePasswordComplexityDataProvider() 0 96 1
A currentPasswordWrongTest() 0 34 1
A validatePasswordComplexityTest() 0 21 1
A noCurrentPasswordGivenTest() 0 26 1
A setup() 0 9 1
1
<?php
2
namespace Derhansen\FeChangePwd\Tests\Unit\Validation\Validator;
3
4
/*
5
 * This file is part of the Extension "fe_change_pwd" for TYPO3 CMS.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.txt file that was distributed with this source code.
9
 */
10
11
use Derhansen\FeChangePwd\Domain\Model\Dto\ChangePassword;
12
use Derhansen\FeChangePwd\Service\LocalizationService;
13
use Derhansen\FeChangePwd\Service\OldPasswordService;
14
use Derhansen\FeChangePwd\Service\SettingsService;
15
use Derhansen\FeChangePwd\Validation\Validator\ChangePasswordValidator;
16
use Nimut\TestingFramework\TestCase\UnitTestCase;
17
18
class ChangePasswordValidatorTest extends UnitTestCase
19
{
20
    /**
21
     * @var ChangePasswordValidator
22
     */
23
    protected $validator;
24
25
    /**
26
     * Setup
27
     *
28
     * @return void
29
     */
30
    public function setup()
31
    {
32
        parent::setUp();
33
        $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...
34
            ChangePasswordValidator::class,
35
            ['translateErrorMessage', 'getValidator'],
36
            [],
37
            '',
38
            false
39
        );
40
    }
41
42
    /**
43
     * @return array
44
     */
45
    public function validatePasswordComplexityDataProvider()
46
    {
47
        return [
48
            'no password given' => [
49
                '',
50
                '',
51
                true,
52
                []
53
            ],
54
            'passwords not equal' => [
55
                'password1',
56
                'password2',
57
                true,
58
                []
59
            ],
60
            'length below min length' => [
61
                'password',
62
                'password',
63
                true,
64
                [
65
                    'passwordComplexity' => [
66
                        'minLength' => 9,
67
                        'capitalCharCheck' => 0,
68
                        'lowerCaseCharCheck' => 0,
69
                        'digitCheck' => 0,
70
                        'specialCharCheck' => 0,
71
                    ]
72
                ]
73
            ],
74
            'no capital char' => [
75
                'password',
76
                'password',
77
                true,
78
                [
79
                    'passwordComplexity' => [
80
                        'minLength' => 7,
81
                        'capitalCharCheck' => 1,
82
                        'lowerCaseCharCheck' => 0,
83
                        'digitCheck' => 0,
84
                        'specialCharCheck' => 0,
85
                    ]
86
                ]
87
            ],
88
            'no lower case char' => [
89
                'PASSWORD',
90
                'PASSWORD',
91
                true,
92
                [
93
                    'passwordComplexity' => [
94
                        'minLength' => 7,
95
                        'capitalCharCheck' => 0,
96
                        'lowerCaseCharCheck' => 1,
97
                        'digitCheck' => 0,
98
                        'specialCharCheck' => 0,
99
                    ]
100
                ]
101
            ],
102
            'no digit' => [
103
                'password',
104
                'password',
105
                true,
106
                [
107
                    'passwordComplexity' => [
108
                        'minLength' => 7,
109
                        'capitalCharCheck' => 0,
110
                        'lowerCaseCharCheck' => 0,
111
                        'digitCheck' => 1,
112
                        'specialCharCheck' => 0,
113
                    ]
114
                ]
115
            ],
116
            'no special char' => [
117
                'password',
118
                'password',
119
                true,
120
                [
121
                    'passwordComplexity' => [
122
                        'minLength' => 7,
123
                        'capitalCharCheck' => 0,
124
                        'lowerCaseCharCheck' => 0,
125
                        'digitCheck' => 0,
126
                        'specialCharCheck' => 1,
127
                    ]
128
                ]
129
            ],
130
            'strong password' => [
131
                'Th!s_i$_a_$+r0ng_passw0rd#',
132
                'Th!s_i$_a_$+r0ng_passw0rd#',
133
                false,
134
                [
135
                    'passwordComplexity' => [
136
                        'minLength' => 20,
137
                        'capitalCharCheck' => 1,
138
                        'lowerCaseCharCheck' => 1,
139
                        'digitCheck' => 1,
140
                        'specialCharCheck' => 1,
141
                    ]
142
                ]
143
            ],
144
        ];
145
    }
146
147
    /**
148
     * @test
149
     * @dataProvider validatePasswordComplexityDataProvider
150
     */
151
    public function validatePasswordComplexityTest($password1, $password2, $expected, $settings)
152
    {
153
        $changePassword = new ChangePassword();
154
        $changePassword->setPassword1($password1);
155
        $changePassword->setPassword2($password2);
156
157
        $mockSettingsService = $this->getMockBuilder(SettingsService::class)
158
            ->setMethods(['getSettings'])
159
            ->disableOriginalConstructor()
160
            ->getMock();
161
        $mockSettingsService->expects($this->once())->method('getSettings')->will($this->returnValue($settings));
162
        $this->inject($this->validator, 'settingsService', $mockSettingsService);
163
164
        $mockLocalizationService = $this->getMockBuilder(LocalizationService::class)
165
            ->setMethods(['translate'])
166
            ->disableOriginalConstructor()
167
            ->getMock();
168
        $mockLocalizationService->expects($this->any())->method('translate')->will($this->returnValue(''));
169
        $this->inject($this->validator, 'localizationService', $mockLocalizationService);
170
171
        $this->assertEquals($expected, $this->validator->validate($changePassword)->hasErrors());
172
    }
173
174
    /**
175
     * @test
176
     */
177
    public function noCurrentPasswordGivenTest()
178
    {
179
        $changePassword = new ChangePassword();
180
        $changePassword->setCurrentPassword('');
181
182
        $settings = [
183
            'requireCurrentPassword' => [
184
                'enabled' => 1
185
            ]
186
        ];
187
188
        $mockSettingsService = $this->getMockBuilder(SettingsService::class)
189
            ->setMethods(['getSettings'])
190
            ->disableOriginalConstructor()
191
            ->getMock();
192
        $mockSettingsService->expects($this->once())->method('getSettings')->will($this->returnValue($settings));
193
        $this->inject($this->validator, 'settingsService', $mockSettingsService);
194
195
        $mockLocalizationService = $this->getMockBuilder(LocalizationService::class)
196
            ->setMethods(['translate'])
197
            ->disableOriginalConstructor()
198
            ->getMock();
199
        $mockLocalizationService->expects($this->any())->method('translate')->will($this->returnValue(''));
200
        $this->inject($this->validator, 'localizationService', $mockLocalizationService);
201
202
        $this->assertEquals(1570880411334, $this->validator->validate($changePassword)->getErrors()[0]->getCode());
203
    }
204
205
    /**
206
     * @test
207
     */
208
    public function currentPasswordWrongTest()
209
    {
210
        $changePassword = new ChangePassword();
211
        $changePassword->setCurrentPassword('invalid');
212
213
        $settings = [
214
            'requireCurrentPassword' => [
215
                'enabled' => 1
216
            ]
217
        ];
218
219
        $mockSettingsService = $this->getMockBuilder(SettingsService::class)
220
            ->setMethods(['getSettings'])
221
            ->disableOriginalConstructor()
222
            ->getMock();
223
        $mockSettingsService->expects($this->once())->method('getSettings')->will($this->returnValue($settings));
224
        $this->inject($this->validator, 'settingsService', $mockSettingsService);
225
226
        $mockLocalizationService = $this->getMockBuilder(LocalizationService::class)
227
            ->setMethods(['translate'])
228
            ->disableOriginalConstructor()
229
            ->getMock();
230
        $mockLocalizationService->expects($this->any())->method('translate')->will($this->returnValue(''));
231
        $this->inject($this->validator, 'localizationService', $mockLocalizationService);
232
233
        $mockOldPasswordService = $this->getMockBuilder(OldPasswordService::class)
234
            ->setMethods(['checkEqualsOldPassword'])
235
            ->disableOriginalConstructor()
236
            ->getMock();
237
        $mockOldPasswordService->expects($this->any())->method('checkEqualsOldPassword')
238
            ->will($this->returnValue(false));
239
        $this->inject($this->validator, 'oldPasswordService', $mockOldPasswordService);
240
241
        $this->assertEquals(1570880417020, $this->validator->validate($changePassword)->getErrors()[0]->getCode());
242
    }
243
}
244