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 © 2018 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 © 2018 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\ValidatorInterface; |
41
|
|
|
use Jkphl\Antibot\Domain\Exceptions\BlacklistValidationException; |
42
|
|
|
use Jkphl\Antibot\Domain\Exceptions\ErrorException; |
43
|
|
|
use Jkphl\Antibot\Domain\Exceptions\SkippedValidationException; |
44
|
|
|
use Jkphl\Antibot\Domain\Exceptions\WhitelistValidationException; |
45
|
|
|
use Jkphl\Antibot\Domain\Model\ValidationResult; |
46
|
|
|
use Jkphl\Antibot\Tests\AbstractTestBase; |
47
|
|
|
use Psr\Log\NullLogger; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Antibot Tests |
51
|
|
|
* |
52
|
|
|
* @package Jkphl\Antibot |
53
|
|
|
* @subpackage Jkphl\Antibot\Tests\Domain |
54
|
|
|
*/ |
55
|
|
|
class AntibotTest extends AbstractTestBase |
56
|
|
|
{ |
57
|
|
|
/** |
58
|
|
|
* General test |
59
|
|
|
* |
60
|
|
|
* @expectedException \Jkphl\Antibot\Domain\Exceptions\RuntimeException |
61
|
|
|
* @expectedExceptionCode 1544191654 |
62
|
|
|
*/ |
63
|
|
|
public function testAntibot(): void |
64
|
|
|
{ |
65
|
|
|
$session = md5(rand()); |
66
|
|
|
$antibot = new Antibot($session, 'customPrefix'); |
67
|
|
|
$this->assertInstanceOf(Antibot::class, $antibot); |
68
|
|
|
$this->assertEquals($antibot->getUnique(), $session); |
69
|
|
|
$this->assertEquals($antibot->getPrefix(), 'customPrefix'); |
70
|
|
|
|
71
|
|
|
$logger = new NullLogger(); |
72
|
|
|
$antibot->setLogger($logger); |
73
|
|
|
$this->assertInstanceOf(NullLogger::class, $antibot->getLogger()); |
74
|
|
|
|
75
|
|
|
$antibot->getParameterPrefix(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Test uninitialized data |
80
|
|
|
* |
81
|
|
|
* @expectedException \Jkphl\Antibot\Domain\Exceptions\RuntimeException |
82
|
|
|
* @expectedExceptionCode 1544191654 |
83
|
|
|
*/ |
84
|
|
|
public function testUnitializedData(): void |
85
|
|
|
{ |
86
|
|
|
$antibot = new Antibot(md5(rand())); |
87
|
|
|
|
88
|
|
|
$antibot->getData(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Parameter scope test |
93
|
|
|
* |
94
|
|
|
* @expectedException \Jkphl\Antibot\Domain\Exceptions\InvalidArgumentException |
95
|
|
|
* @expectedExceptionCode 1546963356 |
96
|
|
|
*/ |
97
|
|
|
public function testParameterScope(): void |
98
|
|
|
{ |
99
|
|
|
$antibot = new Antibot(md5(rand())); |
100
|
|
|
$this->assertInstanceOf(Antibot::class, $antibot); |
101
|
|
|
$antibot->validate($this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4'])); |
102
|
|
|
|
103
|
|
|
$antibot->setParameterScope('one', 'two'); |
104
|
|
|
$scopedParameters = $antibot->getScopedParameters(['three' => 4]); |
105
|
|
|
$this->assertTrue(isset($scopedParameters['one']['two'][$antibot->getParameterPrefix()]['three'])); |
106
|
|
|
$this->assertEquals(4, $scopedParameters['one']['two'][$antibot->getParameterPrefix()]['three']); |
107
|
|
|
|
108
|
|
|
$antibot->setParameterScope(false); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Test the skip validator functionality |
113
|
|
|
*/ |
114
|
|
|
public function testSkipValidator(): void |
115
|
|
|
{ |
116
|
|
|
$antibot = new Antibot(md5(rand())); |
117
|
|
|
$validator = $this->createMock(ValidatorInterface::class); |
118
|
|
|
$validator->method('validate')->will($this->throwException(new SkippedValidationException('skipped'))); |
119
|
|
|
$antibot->addValidator($validator); |
|
|
|
|
120
|
|
|
$request = $this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4']); |
121
|
|
|
$validationResult = $antibot->validate($request); |
122
|
|
|
$this->assertInstanceOf(ValidationResult::class, $validationResult); |
123
|
|
|
$this->assertFalse($validationResult->isValid()); |
124
|
|
|
$this->assertFalse($validationResult->isFailed()); |
125
|
|
|
$this->assertTrue($validationResult->isSkipped()); |
126
|
|
|
$this->assertTrue($validationResult->hasSkips()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Test the blacklist validator functionality |
131
|
|
|
*/ |
132
|
|
|
public function testBlacklistValidator(): void |
133
|
|
|
{ |
134
|
|
|
$antibot = new Antibot(md5(rand())); |
135
|
|
|
$validator = $this->createMock(ValidatorInterface::class); |
136
|
|
|
$validator->method('validate')->will($this->throwException(new BlacklistValidationException('blacklist'))); |
137
|
|
|
$antibot->addValidator($validator); |
|
|
|
|
138
|
|
|
$request = $this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4']); |
139
|
|
|
$validationResult = $antibot->validate($request); |
140
|
|
|
$this->assertInstanceOf(ValidationResult::class, $validationResult); |
141
|
|
|
$this->assertFalse($validationResult->isValid()); |
142
|
|
|
$this->assertTrue($validationResult->isFailed()); |
143
|
|
|
$this->assertTrue($validationResult->isBlacklisted()); |
144
|
|
|
$this->assertEquals(['blacklist'], $validationResult->getBlacklists()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Test the whitelist validator functionality |
149
|
|
|
*/ |
150
|
|
|
public function testWhitelistValidator(): void |
151
|
|
|
{ |
152
|
|
|
$antibot = new Antibot(md5(rand())); |
153
|
|
|
$validator = $this->createMock(ValidatorInterface::class); |
154
|
|
|
$validator->method('validate')->will($this->throwException(new WhitelistValidationException('whitelist'))); |
155
|
|
|
$antibot->addValidator($validator); |
|
|
|
|
156
|
|
|
$request = $this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4']); |
157
|
|
|
$validationResult = $antibot->validate($request); |
158
|
|
|
$this->assertInstanceOf(ValidationResult::class, $validationResult); |
159
|
|
|
$this->assertTrue($validationResult->isValid()); |
160
|
|
|
$this->assertFalse($validationResult->isFailed()); |
161
|
|
|
$this->assertTrue($validationResult->isWhitelisted()); |
162
|
|
|
$this->assertEquals(['whitelist'], $validationResult->getWhitelists()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Test the erroring validator functionality |
167
|
|
|
*/ |
168
|
|
|
public function testErroringValidator(): void |
169
|
|
|
{ |
170
|
|
|
$error = time(); |
171
|
|
|
$antibot = new Antibot(md5(rand())); |
172
|
|
|
$validator = $this->createMock(ValidatorInterface::class); |
173
|
|
|
$validator->method('validate')->will($this->throwException(new ErrorException('error', $error))); |
174
|
|
|
$antibot->addValidator($validator); |
|
|
|
|
175
|
|
|
$request = $this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4']); |
176
|
|
|
$validationResult = $antibot->validate($request); |
177
|
|
|
$this->assertInstanceOf(ValidationResult::class, $validationResult); |
178
|
|
|
$this->assertFalse($validationResult->isValid()); |
179
|
|
|
$this->assertTrue($validationResult->isFailed()); |
180
|
|
|
$this->assertTrue($validationResult->hasErrors()); |
181
|
|
|
$errors = $validationResult->getErrors(); |
182
|
|
|
$this->assertTrue(is_array($errors)); |
183
|
|
|
$this->assertEquals(1, count($errors)); |
184
|
|
|
$this->assertInstanceOf(ErrorException::class, $errors[0]); |
185
|
|
|
$this->assertEquals($error, $errors[0]->getCode()); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Test the failing validator functionality |
190
|
|
|
*/ |
191
|
|
|
public function testFailingValidator(): void |
192
|
|
|
{ |
193
|
|
|
$error = time(); |
|
|
|
|
194
|
|
|
$antibot = new Antibot(md5(rand())); |
195
|
|
|
$validator = $this->createMock(ValidatorInterface::class); |
196
|
|
|
$validator->method('validate')->willReturn(false); |
197
|
|
|
$antibot->addValidator($validator); |
|
|
|
|
198
|
|
|
$request = $this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4']); |
199
|
|
|
$validationResult = $antibot->validate($request); |
200
|
|
|
$this->assertInstanceOf(ValidationResult::class, $validationResult); |
201
|
|
|
$this->assertFalse($validationResult->isValid()); |
202
|
|
|
$this->assertTrue($validationResult->isFailed()); |
203
|
|
|
$this->assertFalse($validationResult->hasErrors()); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Test the succeeding validator functionality |
208
|
|
|
*/ |
209
|
|
|
public function testSucceedingValidator(): void |
210
|
|
|
{ |
211
|
|
|
$error = time(); |
|
|
|
|
212
|
|
|
$antibot = new Antibot(md5(rand())); |
213
|
|
|
$validator = $this->createMock(ValidatorInterface::class); |
214
|
|
|
$validator->method('validate')->willReturn(true); |
215
|
|
|
$antibot->addValidator($validator); |
|
|
|
|
216
|
|
|
$request = $this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4']); |
217
|
|
|
$validationResult = $antibot->validate($request); |
218
|
|
|
$this->assertInstanceOf(ValidationResult::class, $validationResult); |
219
|
|
|
$this->assertTrue($validationResult->isValid()); |
220
|
|
|
$this->assertFalse($validationResult->isFailed()); |
221
|
|
|
$this->assertFalse($validationResult->hasErrors()); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
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: