Passed
Push — master ( 711cdb...bacb9d )
by Torben
04:41
created

ChangePasswordValidatorTest::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 0
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
/**
19
 * Class ChangePasswordValidatorTest
20
 */
21
class ChangePasswordValidatorTest extends UnitTestCase
22
{
23
    /**
24
     * @var ChangePasswordValidator
25
     */
26
    protected $validator;
27
28
    /**
29
     * Initialize validator
30
     */
31
    public function initialize()
32
    {
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
        $this->initialize();
154
155
        $changePassword = new ChangePassword();
156
        $changePassword->setPassword1($password1);
157
        $changePassword->setPassword2($password2);
158
159
        $mockSettingsService = $this->getMockBuilder(SettingsService::class)
160
            ->setMethods(['getSettings'])
161
            ->disableOriginalConstructor()
162
            ->getMock();
163
        $mockSettingsService->expects($this->once())->method('getSettings')->will($this->returnValue($settings));
164
        $this->inject($this->validator, 'settingsService', $mockSettingsService);
165
166
        $mockLocalizationService = $this->getMockBuilder(LocalizationService::class)
167
            ->setMethods(['translate'])
168
            ->disableOriginalConstructor()
169
            ->getMock();
170
        $mockLocalizationService->expects($this->any())->method('translate')->will($this->returnValue(''));
171
        $this->inject($this->validator, 'localizationService', $mockLocalizationService);
172
173
        $this->assertEquals($expected, $this->validator->validate($changePassword)->hasErrors());
174
    }
175
176
    /**
177
     * @test
178
     */
179
    public function noCurrentPasswordGivenTest()
180
    {
181
        $this->initialize();
182
183
        $changePassword = new ChangePassword();
184
        $changePassword->setCurrentPassword('');
185
186
        $settings = [
187
            'requireCurrentPassword' => [
188
                'enabled' => 1
189
            ]
190
        ];
191
192
        $mockSettingsService = $this->getMockBuilder(SettingsService::class)
193
            ->setMethods(['getSettings'])
194
            ->disableOriginalConstructor()
195
            ->getMock();
196
        $mockSettingsService->expects($this->once())->method('getSettings')->will($this->returnValue($settings));
197
        $this->inject($this->validator, 'settingsService', $mockSettingsService);
198
199
        $mockLocalizationService = $this->getMockBuilder(LocalizationService::class)
200
            ->setMethods(['translate'])
201
            ->disableOriginalConstructor()
202
            ->getMock();
203
        $mockLocalizationService->expects($this->any())->method('translate')->will($this->returnValue(''));
204
        $this->inject($this->validator, 'localizationService', $mockLocalizationService);
205
206
        $this->assertEquals(1570880411334, $this->validator->validate($changePassword)->getErrors()[0]->getCode());
207
    }
208
209
    /**
210
     * @test
211
     */
212
    public function currentPasswordWrongTest()
213
    {
214
        $this->initialize();
215
216
        $changePassword = new ChangePassword();
217
        $changePassword->setCurrentPassword('invalid');
218
219
        $settings = [
220
            'requireCurrentPassword' => [
221
                'enabled' => 1
222
            ]
223
        ];
224
225
        $mockSettingsService = $this->getMockBuilder(SettingsService::class)
226
            ->setMethods(['getSettings'])
227
            ->disableOriginalConstructor()
228
            ->getMock();
229
        $mockSettingsService->expects($this->once())->method('getSettings')->will($this->returnValue($settings));
230
        $this->inject($this->validator, 'settingsService', $mockSettingsService);
231
232
        $mockLocalizationService = $this->getMockBuilder(LocalizationService::class)
233
            ->setMethods(['translate'])
234
            ->disableOriginalConstructor()
235
            ->getMock();
236
        $mockLocalizationService->expects($this->any())->method('translate')->will($this->returnValue(''));
237
        $this->inject($this->validator, 'localizationService', $mockLocalizationService);
238
239
        $mockOldPasswordService = $this->getMockBuilder(OldPasswordService::class)
240
            ->setMethods(['checkEqualsOldPassword'])
241
            ->disableOriginalConstructor()
242
            ->getMock();
243
        $mockOldPasswordService->expects($this->any())->method('checkEqualsOldPassword')
244
            ->will($this->returnValue(false));
245
        $this->inject($this->validator, 'oldPasswordService', $mockOldPasswordService);
246
247
        $this->assertEquals(1570880417020, $this->validator->validate($changePassword)->getErrors()[0]->getCode());
248
    }
249
}
250