1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* antibot |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Antibot |
8
|
|
|
* @subpackage Jkphl\Antibot\Tests\Domain |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2020 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2020 Joschi Kuphal <[email protected]> |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Jkphl\Antibot\Tests\Domain; |
38
|
|
|
|
39
|
|
|
use Jkphl\Antibot\Domain\Antibot; |
40
|
|
|
use Jkphl\Antibot\Domain\Contract\ValidationResultInterface; |
41
|
|
|
use Jkphl\Antibot\Domain\Contract\ValidatorInterface; |
42
|
|
|
use Jkphl\Antibot\Domain\Exceptions\BlacklistValidationException; |
43
|
|
|
use Jkphl\Antibot\Domain\Exceptions\ErrorException; |
44
|
|
|
use Jkphl\Antibot\Domain\Exceptions\InvalidArgumentException; |
45
|
|
|
use Jkphl\Antibot\Domain\Exceptions\RuntimeException; |
46
|
|
|
use Jkphl\Antibot\Domain\Exceptions\SkippedValidationException; |
47
|
|
|
use Jkphl\Antibot\Domain\Exceptions\WhitelistValidationException; |
48
|
|
|
use Jkphl\Antibot\Ports\ValidationResult; |
49
|
|
|
use Jkphl\Antibot\Tests\AbstractTestBase; |
50
|
|
|
use Psr\Log\NullLogger; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Antibot Tests |
54
|
|
|
* |
55
|
|
|
* @package Jkphl\Antibot |
56
|
|
|
* @subpackage Jkphl\Antibot\Tests\Domain |
57
|
|
|
*/ |
58
|
|
|
class AntibotTest extends AbstractTestBase |
59
|
|
|
{ |
60
|
|
|
/** |
61
|
|
|
* General test |
62
|
|
|
*/ |
63
|
|
|
public function testAntibot(): void |
64
|
|
|
{ |
65
|
|
|
$this->expectExceptionCode(1544191654); |
66
|
|
|
$this->expectException(RuntimeException::class); |
67
|
|
|
$session = md5(rand()); |
68
|
|
|
$antibot = new Antibot($session, 'customPrefix'); |
69
|
|
|
$this->assertInstanceOf(Antibot::class, $antibot); |
70
|
|
|
$this->assertEquals($antibot->getUnique(), $session); |
71
|
|
|
$this->assertEquals($antibot->getPrefix(), 'customPrefix'); |
72
|
|
|
|
73
|
|
|
$logger = new NullLogger(); |
74
|
|
|
$antibot->setLogger($logger); |
75
|
|
|
$this->assertInstanceOf(NullLogger::class, $antibot->getLogger()); |
76
|
|
|
|
77
|
|
|
$antibot->getParameterPrefix(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Test uninitialized data |
82
|
|
|
*/ |
83
|
|
|
public function testUnitializedData(): void |
84
|
|
|
{ |
85
|
|
|
$this->expectExceptionCode(1544191654); |
86
|
|
|
$this->expectException(RuntimeException::class); |
87
|
|
|
$antibot = new Antibot(md5(rand())); |
88
|
|
|
|
89
|
|
|
$antibot->getData(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Parameter scope test |
94
|
|
|
*/ |
95
|
|
|
public function testParameterScope(): void |
96
|
|
|
{ |
97
|
|
|
$this->expectExceptionCode(1546963356); |
98
|
|
|
$this->expectException(InvalidArgumentException::class); |
99
|
|
|
$antibot = new Antibot(md5(rand())); |
100
|
|
|
$this->assertInstanceOf(Antibot::class, $antibot); |
101
|
|
|
$antibot->validateRequest( |
102
|
|
|
$this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4']), |
103
|
|
|
$this->createMock(ValidationResultInterface::class) |
|
|
|
|
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$antibot->setParameterScope('one', 'two'); |
107
|
|
|
$scopedParameters = $antibot->getScopedParameters(['three' => 4]); |
108
|
|
|
$this->assertTrue(isset($scopedParameters['one']['two'][$antibot->getParameterPrefix()]['three'])); |
109
|
|
|
$this->assertEquals(4, $scopedParameters['one']['two'][$antibot->getParameterPrefix()]['three']); |
110
|
|
|
|
111
|
|
|
$antibot->setParameterScope(false); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Test the skip validator functionality |
116
|
|
|
*/ |
117
|
|
|
public function testSkipValidator(): void |
118
|
|
|
{ |
119
|
|
|
$antibot = new Antibot(md5(rand())); |
120
|
|
|
$validator = $this->createMock(ValidatorInterface::class); |
121
|
|
|
$validator->method('validate')->will($this->throwException(new SkippedValidationException('skipped'))); |
|
|
|
|
122
|
|
|
$antibot->addValidator($validator); |
|
|
|
|
123
|
|
|
$request = $this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4']); |
124
|
|
|
$validationResult = $antibot->validateRequest($request, new ValidationResult()); |
125
|
|
|
$this->assertInstanceOf(ValidationResult::class, $validationResult); |
126
|
|
|
$this->assertFalse($validationResult->isValid()); |
127
|
|
|
$this->assertFalse($validationResult->isFailed()); |
128
|
|
|
$this->assertTrue($validationResult->isSkipped()); |
129
|
|
|
$this->assertTrue($validationResult->hasSkips()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Test the blacklist validator functionality |
134
|
|
|
*/ |
135
|
|
|
public function testBlacklistValidator(): void |
136
|
|
|
{ |
137
|
|
|
$antibot = new Antibot(md5(rand())); |
138
|
|
|
$validator = $this->createMock(ValidatorInterface::class); |
139
|
|
|
$validator->method('validate')->will($this->throwException(new BlacklistValidationException('blacklist'))); |
|
|
|
|
140
|
|
|
$antibot->addValidator($validator); |
|
|
|
|
141
|
|
|
$request = $this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4']); |
142
|
|
|
$validationResult = $antibot->validateRequest($request, new ValidationResult()); |
143
|
|
|
$this->assertInstanceOf(ValidationResult::class, $validationResult); |
144
|
|
|
$this->assertFalse($validationResult->isValid()); |
145
|
|
|
$this->assertTrue($validationResult->isFailed()); |
146
|
|
|
$this->assertTrue($validationResult->isBlacklisted()); |
147
|
|
|
$this->assertEquals(['blacklist'], $validationResult->getBlacklists()); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Test the whitelist validator functionality |
152
|
|
|
*/ |
153
|
|
|
public function testWhitelistValidator(): void |
154
|
|
|
{ |
155
|
|
|
$antibot = new Antibot(md5(rand())); |
156
|
|
|
$validator = $this->createMock(ValidatorInterface::class); |
157
|
|
|
$validator->method('validate')->will($this->throwException(new WhitelistValidationException('whitelist'))); |
|
|
|
|
158
|
|
|
$antibot->addValidator($validator); |
|
|
|
|
159
|
|
|
$request = $this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4']); |
160
|
|
|
$validationResult = $antibot->validateRequest($request, new ValidationResult()); |
161
|
|
|
$this->assertInstanceOf(ValidationResult::class, $validationResult); |
162
|
|
|
$this->assertTrue($validationResult->isValid()); |
163
|
|
|
$this->assertFalse($validationResult->isFailed()); |
164
|
|
|
$this->assertTrue($validationResult->isWhitelisted()); |
165
|
|
|
$this->assertEquals(['whitelist'], $validationResult->getWhitelists()); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Test the erroring validator functionality |
170
|
|
|
*/ |
171
|
|
|
public function testErroringValidator(): void |
172
|
|
|
{ |
173
|
|
|
$error = time(); |
174
|
|
|
$antibot = new Antibot(md5(rand())); |
175
|
|
|
$validator = $this->createMock(ValidatorInterface::class); |
176
|
|
|
$validator->method('validate')->will($this->throwException(new ErrorException('error', $error))); |
|
|
|
|
177
|
|
|
$antibot->addValidator($validator); |
|
|
|
|
178
|
|
|
$request = $this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4']); |
179
|
|
|
$validationResult = $antibot->validateRequest($request, new ValidationResult()); |
180
|
|
|
$this->assertInstanceOf(ValidationResult::class, $validationResult); |
181
|
|
|
$this->assertFalse($validationResult->isValid()); |
182
|
|
|
$this->assertTrue($validationResult->isFailed()); |
183
|
|
|
$this->assertTrue($validationResult->hasErrors()); |
184
|
|
|
$errors = $validationResult->getErrors(); |
185
|
|
|
$this->assertTrue(is_array($errors)); |
186
|
|
|
$this->assertEquals(1, count($errors)); |
187
|
|
|
$this->assertInstanceOf(ErrorException::class, $errors[0]); |
188
|
|
|
$this->assertEquals($error, $errors[0]->getCode()); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Test the failing validator functionality |
193
|
|
|
*/ |
194
|
|
|
public function testFailingValidator(): void |
195
|
|
|
{ |
196
|
|
|
$error = time(); |
|
|
|
|
197
|
|
|
$antibot = new Antibot(md5(rand())); |
198
|
|
|
$validator = $this->createMock(ValidatorInterface::class); |
199
|
|
|
$validator->method('validate')->willReturn(false); |
|
|
|
|
200
|
|
|
$antibot->addValidator($validator); |
|
|
|
|
201
|
|
|
$request = $this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4']); |
202
|
|
|
$validationResult = $antibot->validateRequest($request, new ValidationResult()); |
203
|
|
|
$this->assertInstanceOf(ValidationResult::class, $validationResult); |
204
|
|
|
$this->assertFalse($validationResult->isValid()); |
205
|
|
|
$this->assertTrue($validationResult->isFailed()); |
206
|
|
|
$this->assertFalse($validationResult->hasErrors()); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Test the succeeding validator functionality |
211
|
|
|
*/ |
212
|
|
|
public function testSucceedingValidator(): void |
213
|
|
|
{ |
214
|
|
|
$error = time(); |
|
|
|
|
215
|
|
|
$antibot = new Antibot(md5(rand())); |
216
|
|
|
$validator = $this->createMock(ValidatorInterface::class); |
217
|
|
|
$validator->method('validate')->willReturn(true); |
|
|
|
|
218
|
|
|
$antibot->addValidator($validator); |
|
|
|
|
219
|
|
|
$request = $this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4']); |
220
|
|
|
$validationResult = $antibot->validateRequest($request, new ValidationResult()); |
221
|
|
|
$this->assertInstanceOf(ValidationResult::class, $validationResult); |
222
|
|
|
$this->assertTrue($validationResult->isValid()); |
223
|
|
|
$this->assertFalse($validationResult->isFailed()); |
224
|
|
|
$this->assertFalse($validationResult->hasErrors()); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: