1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ControlTests; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use ArrayAccess; |
7
|
|
|
use CommonTestClass; |
8
|
|
|
use kalanis\kw_forms\Controls; |
9
|
|
|
use kalanis\kw_forms\Exceptions\RenderException; |
10
|
|
|
use kalanis\kw_rules\Interfaces\IRules; |
11
|
|
|
use kalanis\kw_rules\Validate; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class CaptchaTest extends CommonTestClass |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @throws RenderException |
18
|
|
|
*/ |
19
|
|
|
public function testCaptcha(): void |
20
|
|
|
{ |
21
|
|
|
$captcha = new Captcha(); |
22
|
|
|
$never = new Controls\Security\Timeout\NoTime(); |
23
|
|
|
$always = new Controls\Security\Timeout\AnyTime(); |
24
|
|
|
|
25
|
|
|
// obligatory call for usually unused methods |
26
|
|
|
$captcha->addRules(); |
27
|
|
|
$captcha->removeRules(); |
28
|
|
|
$this->assertEmpty($captcha->renderInput()); |
29
|
|
|
$this->assertEmpty($captcha->renderErrors([])); |
30
|
|
|
|
31
|
|
|
// now real tests |
32
|
|
|
// render label - only when timeout says it |
33
|
|
|
$captcha->setLabel('uhbijnokm'); |
34
|
|
|
$this->assertNotEmpty($captcha->renderLabel()); |
35
|
|
|
$captcha->setTimeout($never); |
36
|
|
|
$this->assertNotEmpty($captcha->renderLabel()); |
37
|
|
|
$captcha->setTimeout($always); |
38
|
|
|
$this->assertEmpty($captcha->renderInput()); |
39
|
|
|
|
40
|
|
|
// rules - only when not need to pass |
41
|
|
|
$captcha->addRule(IRules::IS_FILLED, 'empty'); |
42
|
|
|
$captcha->setTimeout($never); |
43
|
|
|
$this->assertNotEmpty($captcha->getRules()); |
44
|
|
|
$captcha->setTimeout($always); |
45
|
|
|
$this->assertEmpty($captcha->getRules()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @throws RenderException |
50
|
|
|
* @requires function imagettftext |
51
|
|
|
*/ |
52
|
|
|
public function testGraphical(): void |
53
|
|
|
{ |
54
|
|
|
$session = new \MockArray(); |
55
|
|
|
$captcha = new Graphical(); |
56
|
|
|
$captcha->set('cpt', $session, 'die', $this->getFontPath()); |
57
|
|
|
|
58
|
|
|
// obligatory call for usually unused methods |
59
|
|
|
$this->assertEmpty($captcha->renderErrors([])); |
60
|
|
|
$this->assertNotEmpty($captcha->renderInput()); |
61
|
|
|
|
62
|
|
|
// now timeout render |
63
|
|
|
$captcha->setTimeout(new Controls\Security\Timeout\AnyTime()); |
64
|
|
|
$this->assertEmpty($captcha->renderLabel()); |
65
|
|
|
$captcha->setTimeout(new Controls\Security\Timeout\NoTime()); |
66
|
|
|
$this->assertNotEmpty($captcha->renderLabel()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @throws RenderException |
71
|
|
|
*/ |
72
|
|
|
public function testDisabled(): void |
73
|
|
|
{ |
74
|
|
|
$captcha = new Controls\Security\Captcha\Disabled(); |
75
|
|
|
|
76
|
|
|
// obligatory call for usually unused methods |
77
|
|
|
$captcha->addRule('none', 'none'); |
78
|
|
|
$this->assertEmpty($captcha->getRules()); |
79
|
|
|
$this->assertEmpty($captcha->renderInput()); |
80
|
|
|
$this->assertEmpty($captcha->renderLabel()); |
81
|
|
|
$this->assertEmpty($captcha->renderErrors([])); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testText(): void |
85
|
|
|
{ |
86
|
|
|
$session = new \MockArray(); |
87
|
|
|
$captcha = new Controls\Security\Captcha\Text(); |
88
|
|
|
$captcha->set('cpt', $session, 'die', $this->getFontPath()); |
89
|
|
|
|
90
|
|
|
// obligatory call for usually unused methods |
91
|
|
|
$captcha->addRule('none', 'none'); |
92
|
|
|
|
93
|
|
|
$validate = new Validate(); |
94
|
|
|
// correct one |
95
|
|
|
$session->offsetSet('cpt_last', $session->offsetGet('cpt_now')); // copy captcha data |
96
|
|
|
$captcha->setValue($session->offsetGet('cpt_last')); |
97
|
|
|
$this->assertTrue($validate->validate($captcha)); |
98
|
|
|
$this->assertEmpty($validate->getErrors()); |
99
|
|
|
// failed one |
100
|
|
|
$captcha->setValue('plkk'); |
101
|
|
|
$this->assertFalse($validate->validate($captcha)); |
102
|
|
|
$this->assertNotEmpty($validate->getErrors()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @throws RenderException |
107
|
|
|
* @requires function imagettftext |
108
|
|
|
*/ |
109
|
|
|
public function testNumerical(): void |
110
|
|
|
{ |
111
|
|
|
$session = new \MockArray(); |
112
|
|
|
$captcha = new Controls\Security\Captcha\Numerical(); |
113
|
|
|
$captcha->set('cpt', $session, 'die', $this->getFontPath()); |
114
|
|
|
|
115
|
|
|
// obligatory call for usually unused methods |
116
|
|
|
$captcha->addRule('none', 'none'); |
117
|
|
|
$this->assertEmpty($captcha->renderLabel()); |
118
|
|
|
$this->assertNotEmpty($captcha->renderInput()); |
119
|
|
|
|
120
|
|
|
$validate = new Validate(); |
121
|
|
|
// correct one |
122
|
|
|
$session->offsetSet('cpt_last', $session->offsetGet('cpt_now')); // copy captcha data |
123
|
|
|
$captcha->setValue($session->offsetGet('cpt_last')); |
124
|
|
|
$this->assertTrue($validate->validate($captcha)); |
125
|
|
|
$this->assertEmpty($validate->getErrors()); |
126
|
|
|
// failed one |
127
|
|
|
$captcha->setValue('plkk'); |
128
|
|
|
$this->assertFalse($validate->validate($captcha)); |
129
|
|
|
$this->assertNotEmpty($validate->getErrors()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @throws RenderException |
134
|
|
|
* @requires function imagettfbbox |
135
|
|
|
*/ |
136
|
|
|
public function testColourful(): void |
137
|
|
|
{ |
138
|
|
|
$session = new \MockArray(); |
139
|
|
|
$captcha = new Controls\Security\Captcha\ColourfulText(); |
140
|
|
|
$captcha->set('cpt', $session, 'die', $this->getFontPath()); |
141
|
|
|
|
142
|
|
|
// obligatory call for usually unused methods |
143
|
|
|
$captcha->addRule('none', 'none'); |
144
|
|
|
$this->assertNotEmpty($captcha->renderLabel()); |
145
|
|
|
|
146
|
|
|
$validate = new Validate(); |
147
|
|
|
// correct one |
148
|
|
|
$session->offsetSet('cpt_last', $session->offsetGet('cpt_now')); // copy captcha data |
149
|
|
|
$captcha->setValue($session->offsetGet('cpt_last')); |
150
|
|
|
$this->assertTrue($validate->validate($captcha)); |
151
|
|
|
$this->assertEmpty($validate->getErrors()); |
152
|
|
|
// failed one |
153
|
|
|
$captcha->setValue('plkk'); |
154
|
|
|
$this->assertFalse($validate->validate($captcha)); |
155
|
|
|
$this->assertNotEmpty($validate->getErrors()); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
protected function getFontPath(): ?string |
159
|
|
|
{ |
160
|
|
|
$path = realpath(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', 'data', 'freesans.ttf'])); |
161
|
|
|
if (!$path) { |
162
|
|
|
$this->assertFalse(true, 'Need to find font for generating test images!'); |
163
|
|
|
} |
164
|
|
|
return $path; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param int $id |
169
|
|
|
* @param string $instance |
170
|
|
|
* @dataProvider factoryProvider |
171
|
|
|
*/ |
172
|
|
|
public function testFactory(int $id, string $instance): void |
173
|
|
|
{ |
174
|
|
|
$session = new \MockArray(); |
175
|
|
|
$factory = new Controls\Security\Captcha\Factory(); |
176
|
|
|
$this->assertInstanceOf($instance, $factory->getCaptcha($id, $session)); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function factoryProvider(): array |
180
|
|
|
{ |
181
|
|
|
return [ |
182
|
|
|
[Controls\Security\Captcha\Factory::TYPE_DISABLED, Controls\Security\Captcha\Disabled::class], |
183
|
|
|
[Controls\Security\Captcha\Factory::TYPE_TEXT, Controls\Security\Captcha\Text::class], |
184
|
|
|
[Controls\Security\Captcha\Factory::TYPE_MATH, Controls\Security\Captcha\Numerical::class], |
185
|
|
|
[Controls\Security\Captcha\Factory::TYPE_COLOUR, Controls\Security\Captcha\ColourfulText::class], |
186
|
|
|
[Controls\Security\Captcha\Factory::TYPE_NOCAPTCHA, Controls\Security\Captcha\Nocaptcha::class], |
187
|
|
|
[123, Controls\Security\Captcha\Text::class], |
188
|
|
|
]; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
class Captcha extends Controls\Security\Captcha\ACaptcha |
194
|
|
|
{ |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
class Graphical extends Controls\Security\Captcha\AGraphical |
199
|
|
|
{ |
200
|
|
|
public function set(string $alias, ArrayAccess &$session, string $errorMessage, string $font = ''): self |
201
|
|
|
{ |
202
|
|
|
$this->font = $font; |
203
|
|
|
$text = strtolower($this->generateRandomString(8)); |
204
|
|
|
$this->setEntry($alias, null, $text); |
205
|
|
|
$this->fillSession($alias, $session, $text); |
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|