Failed Conditions
Push — issue#702 ( d312bf...92afd8 )
by Guilherme
06:53
created

PasswordHintServiceTest::testNoPassword()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\CoreBundle\Tests\Service;
12
13
use LoginCidadao\CoreBundle\Service\PasswordHintService;
14
use PHPUnit\Framework\TestCase;
15
use Rollerworks\Component\PasswordStrength\Validator\Constraints\PasswordRequirements;
16
use Symfony\Component\Translation\TranslatorInterface;
17
use Symfony\Component\Validator\Constraints\Length;
18
use Symfony\Component\Validator\Constraints\NotBlank;
19
use Symfony\Component\Validator\Validator\ValidatorInterface;
20
21
class PasswordHintServiceTest extends TestCase
22
{
23
24
    public function testGetHintStringNoLimits()
25
    {
26
        $userClass = 'UserClass';
27
        $service = new PasswordHintService($this->getValidator(), $this->getTranslator(), $userClass);
28
        $service->getHintString();
29
    }
30
31
    public function testGetHintStringRange()
32
    {
33
        $messages = [
34
            ['password_hint.range.no_reqs'],
35
        ];
36
        $userClass = 'UserClass';
37
        $service = new PasswordHintService($this->getValidator(), $this->getTranslator($messages), $userClass);
38
        $service->getHintString(['min' => 8, 'max' => 160]);
39
    }
40
41
    public function testGetHintStringMin()
42
    {
43
        $messages = [
44
            ['password_hint.min.no_reqs'],
45
        ];
46
        $userClass = 'UserClass';
47
        $service = new PasswordHintService($this->getValidator(), $this->getTranslator($messages), $userClass);
48
        $service->getHintString(['min' => 8]);
49
    }
50
51
    public function testGetHintStringMax()
52
    {
53
        $messages = [
54
            ['password_hint.max.no_reqs'],
55
        ];
56
        $userClass = 'UserClass';
57
        $service = new PasswordHintService($this->getValidator(), $this->getTranslator($messages), $userClass);
58
        $service->getHintString(['max' => 200]);
59
    }
60
61
    public function testGetHintStringNoLimitsFullReqs()
62
    {
63
        $messages = [
64
            ['password_hint.no_limit.with_reqs'],
65
            ['password_hint.requirements.numbers'],
66
            ['password_hint.requirements.letters'],
67
            ['password_hint.requirements.special'],
68
            ['password_hint.and'],
69
        ];
70
        $userClass = 'UserClass';
71
        $service = new PasswordHintService($this->getValidator(), $this->getTranslator($messages), $userClass);
72
        $service->getHintString([
73
            'requireLetters' => true,
74
            'requireNumbers' => true,
75
            'requireSpecialCharacter' => true,
76
        ]);
77
    }
78
79
    public function testNoMetadata()
80
    {
81
        $expected = [
82
            'min' => 0,
83
            'max' => null,
84
            'requireLetters' => false,
85
            'requireNumbers' => false,
86
            'requireSpecialCharacter' => false,
87
        ];
88
89
        $userClass = 'UserClass';
90
        $service = new PasswordHintService($this->getValidator(), $this->getTranslator(), $userClass);
91
        $this->assertSame($expected, $service->getPasswordRequirements());
92
    }
93
94
    public function testNoPassword()
95
    {
96
        $expected = [
97
            'min' => 0,
98
            'max' => null,
99
            'requireLetters' => false,
100
            'requireNumbers' => false,
101
            'requireSpecialCharacter' => false,
102
        ];
103
104
        $userClass = 'UserClass';
105
106
        $this->runGetRequirementsTest(false, [], $userClass, $expected);
107
108
        $metadata = $this->createMock('Symfony\Component\Validator\Mapping\ClassMetadataInterface');
109
        $metadata->expects($this->once())
110
            ->method('hasPropertyMetadata')->with('plainPassword')->willReturn(false);
111
112
        $validator = $this->getValidator();
113
        $validator->expects($this->once())
114
            ->method('getMetadataFor')->with($userClass)
115
            ->willReturn($metadata);
116
117
        $service = new PasswordHintService($validator, $this->getTranslator(), $userClass);
118
        $this->assertSame($expected, $service->getPasswordRequirements());
119
    }
120
121
    public function testGetPasswordRequirements()
122
    {
123
        $expected = [
124
            'min' => 8,
125
            'max' => 256,
126
            'requireLetters' => true,
127
            'requireNumbers' => true,
128
            'requireSpecialCharacter' => true,
129
        ];
130
131
        $userClass = 'UserClass';
132
133
        $constraints = [
134
            new PasswordRequirements([
135
                'minLength' => 8,
136
                'requireLetters' => true,
137
                'requireCaseDiff' => true,
138
                'requireNumbers' => true,
139
                'requireSpecialCharacter' => true,
140
            ]),
141
            new Length(['min' => 6, 'max' => 256]), // 'min' should be overridden
142
        ];
143
144
        $this->runGetRequirementsTest(true, $constraints, $userClass, $expected);
145
    }
146
147
    public function testGetPasswordRequirementsNoMin()
148
    {
149
        $expected = [
150
            'min' => 0,
151
            'max' => null,
152
            'requireLetters' => false,
153
            'requireNumbers' => false,
154
            'requireSpecialCharacter' => false,
155
        ];
156
157
        $userClass = 'UserClass';
158
159
        $constraints = [new NotBlank()];
160
161
        $this->runGetRequirementsTest(true, $constraints, $userClass, $expected);
162
    }
163
164
    /**
165
     * @param array $expectedMessagesMap
166
     * @return TranslatorInterface|\PHPUnit_Framework_MockObject_MockObject
167
     */
168
    private function getTranslator($expectedMessagesMap = [])
169
    {
170
        $translator = $this->createMock('Symfony\Component\Translation\TranslatorInterface');
171
        $translator->expects($this->exactly(count($expectedMessagesMap)))
172
            ->method('trans')->willReturnMap($expectedMessagesMap);
173
174
        return $translator;
175
    }
176
177
    /**
178
     * @return ValidatorInterface|\PHPUnit_Framework_MockObject_MockObject
179
     */
180
    private function getValidator()
181
    {
182
        return $this->createMock('Symfony\Component\Validator\Validator\ValidatorInterface');
183
    }
184
185
    private function runGetRequirementsTest(
186
        $hasProp,
187
        $constraints,
188
        $userClass,
189
        $expected
190
    ) {
191
        $metadata = $this->createMock('Symfony\Component\Validator\Mapping\ClassMetadataInterface');
192
        $metadata->expects($this->once())
193
            ->method('hasPropertyMetadata')->with('plainPassword')->willReturn($hasProp);
194
195
        if ($hasProp) {
196
            $propMetadata = $this->createMock('Symfony\Component\Validator\Mapping\PropertyMetadataInterface');
197
            $propMetadata->expects($this->once())
198
                ->method('getConstraints')->willReturn($constraints);
199
200
            $metadata->expects($this->once())
201
                ->method('getPropertyMetadata')->with('plainPassword')->willReturn([$propMetadata]);
202
        }
203
204
        $validator = $this->getValidator();
205
        $validator->expects($this->once())
206
            ->method('getMetadataFor')->with($userClass)
207
            ->willReturn($metadata);
208
209
        $service = new PasswordHintService($validator, $this->getTranslator(), $userClass);
210
        $this->assertSame($expected, $service->getPasswordRequirements());
211
    }
212
}
213