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
|
|
|
* Test the skip validator functionality |
93
|
|
|
*/ |
94
|
|
|
public function testSkipValidator(): void |
95
|
|
|
{ |
96
|
|
|
$antibot = new Antibot(md5(rand())); |
97
|
|
|
$validator = $this->createMock(ValidatorInterface::class); |
98
|
|
|
$validator->method('validate')->will($this->throwException(new SkippedValidationException('skipped'))); |
99
|
|
|
$antibot->addValidator($validator); |
|
|
|
|
100
|
|
|
$request = $this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4']); |
101
|
|
|
$validationResult = $antibot->validate($request); |
102
|
|
|
$this->assertInstanceOf(ValidationResult::class, $validationResult); |
103
|
|
|
$this->assertFalse($validationResult->isValid()); |
104
|
|
|
$this->assertFalse($validationResult->isFailed()); |
105
|
|
|
$this->assertTrue($validationResult->isSkipped()); |
106
|
|
|
$this->assertTrue($validationResult->hasSkips()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Test the blacklist validator functionality |
111
|
|
|
*/ |
112
|
|
|
public function testBlacklistValidator(): void |
113
|
|
|
{ |
114
|
|
|
$antibot = new Antibot(md5(rand())); |
115
|
|
|
$validator = $this->createMock(ValidatorInterface::class); |
116
|
|
|
$validator->method('validate')->will($this->throwException(new BlacklistValidationException('blacklist'))); |
117
|
|
|
$antibot->addValidator($validator); |
|
|
|
|
118
|
|
|
$request = $this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4']); |
119
|
|
|
$validationResult = $antibot->validate($request); |
120
|
|
|
$this->assertInstanceOf(ValidationResult::class, $validationResult); |
121
|
|
|
$this->assertFalse($validationResult->isValid()); |
122
|
|
|
$this->assertTrue($validationResult->isFailed()); |
123
|
|
|
$this->assertTrue($validationResult->isBlacklisted()); |
124
|
|
|
$this->assertEquals(['blacklist'], $validationResult->getBlacklists()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Test the whitelist validator functionality |
129
|
|
|
*/ |
130
|
|
|
public function testWhitelistValidator(): void |
131
|
|
|
{ |
132
|
|
|
$antibot = new Antibot(md5(rand())); |
133
|
|
|
$validator = $this->createMock(ValidatorInterface::class); |
134
|
|
|
$validator->method('validate')->will($this->throwException(new WhitelistValidationException('whitelist'))); |
135
|
|
|
$antibot->addValidator($validator); |
|
|
|
|
136
|
|
|
$request = $this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4']); |
137
|
|
|
$validationResult = $antibot->validate($request); |
138
|
|
|
$this->assertInstanceOf(ValidationResult::class, $validationResult); |
139
|
|
|
$this->assertTrue($validationResult->isValid()); |
140
|
|
|
$this->assertFalse($validationResult->isFailed()); |
141
|
|
|
$this->assertTrue($validationResult->isWhitelisted()); |
142
|
|
|
$this->assertEquals(['whitelist'], $validationResult->getWhitelists()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Test the erroring validator functionality |
147
|
|
|
*/ |
148
|
|
|
public function testErroringValidator(): void |
149
|
|
|
{ |
150
|
|
|
$error = time(); |
151
|
|
|
$antibot = new Antibot(md5(rand())); |
152
|
|
|
$validator = $this->createMock(ValidatorInterface::class); |
153
|
|
|
$validator->method('validate')->will($this->throwException(new ErrorException('error', $error))); |
154
|
|
|
$antibot->addValidator($validator); |
|
|
|
|
155
|
|
|
$request = $this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4']); |
156
|
|
|
$validationResult = $antibot->validate($request); |
157
|
|
|
$this->assertInstanceOf(ValidationResult::class, $validationResult); |
158
|
|
|
$this->assertFalse($validationResult->isValid()); |
159
|
|
|
$this->assertTrue($validationResult->isFailed()); |
160
|
|
|
$this->assertTrue($validationResult->hasErrors()); |
161
|
|
|
$errors = $validationResult->getErrors(); |
162
|
|
|
$this->assertTrue(is_array($errors)); |
163
|
|
|
$this->assertEquals(1, count($errors)); |
164
|
|
|
$this->assertInstanceOf(ErrorException::class, $errors[0]); |
165
|
|
|
$this->assertEquals($error, $errors[0]->getCode()); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Test the failing validator functionality |
170
|
|
|
*/ |
171
|
|
|
public function testFailingValidator(): void |
172
|
|
|
{ |
173
|
|
|
$error = time(); |
|
|
|
|
174
|
|
|
$antibot = new Antibot(md5(rand())); |
175
|
|
|
$validator = $this->createMock(ValidatorInterface::class); |
176
|
|
|
$validator->method('validate')->willReturn(false); |
177
|
|
|
$antibot->addValidator($validator); |
|
|
|
|
178
|
|
|
$request = $this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4']); |
179
|
|
|
$validationResult = $antibot->validate($request); |
180
|
|
|
$this->assertInstanceOf(ValidationResult::class, $validationResult); |
181
|
|
|
$this->assertFalse($validationResult->isValid()); |
182
|
|
|
$this->assertTrue($validationResult->isFailed()); |
183
|
|
|
$this->assertFalse($validationResult->hasErrors()); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Test the succeeding validator functionality |
188
|
|
|
*/ |
189
|
|
|
public function testSucceedingValidator(): void |
190
|
|
|
{ |
191
|
|
|
$error = time(); |
|
|
|
|
192
|
|
|
$antibot = new Antibot(md5(rand())); |
193
|
|
|
$validator = $this->createMock(ValidatorInterface::class); |
194
|
|
|
$validator->method('validate')->willReturn(true); |
195
|
|
|
$antibot->addValidator($validator); |
|
|
|
|
196
|
|
|
$request = $this->createRequest(['REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '1.2.3.4']); |
197
|
|
|
$validationResult = $antibot->validate($request); |
198
|
|
|
$this->assertInstanceOf(ValidationResult::class, $validationResult); |
199
|
|
|
$this->assertTrue($validationResult->isValid()); |
200
|
|
|
$this->assertFalse($validationResult->isFailed()); |
201
|
|
|
$this->assertFalse($validationResult->hasErrors()); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
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: