-
Notifications
You must be signed in to change notification settings - Fork 6
/
AllowTest.php
192 lines (169 loc) · 7.11 KB
/
AllowTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* vipnytt/RobotsTxtParser
*
* @link https://github.com/VIPnytt/RobotsTxtParser
* @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT)
*/
namespace vipnytt\RobotsTxtParser\Tests;
use PHPUnit\Framework\TestCase;
use vipnytt\RobotsTxtParser;
/**
* Class AllowTest
*
* @package vipnytt\RobotsTxtParser\Tests
*/
class AllowTest extends TestCase
{
/**
* @dataProvider generateDataForTest
* @param string $robotsTxtContent
* @param string|false $rendered
*/
public function testDisAllow($robotsTxtContent, $rendered)
{
$parser = new RobotsTxtParser\TxtClient('http://example.com', 200, $robotsTxtContent);
$this->assertInstanceOf('vipnytt\RobotsTxtParser\TxtClient', $parser);
$this->assertTrue($parser->userAgent()->isAllowed("/"));
$this->assertTrue($parser->userAgent()->isAllowed("/article"));
$this->assertTrue($parser->userAgent()->isDisallowed("/temp"));
$this->assertTrue($parser->userAgent()->isDisallowed("/Admin"));
$this->assertTrue($parser->userAgent()->isDisallowed("/admin"));
$this->assertTrue($parser->userAgent()->isDisallowed("/admin/cp/test/"));
$this->assertFalse($parser->userAgent()->isDisallowed("/"));
$this->assertFalse($parser->userAgent()->isDisallowed("/article"));
$this->assertFalse($parser->userAgent()->isAllowed("/temp"));
$this->assertFalse($parser->userAgent()->isAllowed("/Admin"));
$this->assertFalse($parser->userAgent()->isAllowed("/admin"));
$this->assertFalse($parser->userAgent()->isAllowed("/admin/cp/test/"));
$this->assertTrue($parser->userAgent('*')->isAllowed("/"));
$this->assertTrue($parser->userAgent('*')->isAllowed("/article"));
$this->assertTrue($parser->userAgent('*')->isDisallowed("/temp"));
$this->assertFalse($parser->userAgent('*')->isDisallowed("/"));
$this->assertFalse($parser->userAgent('*')->isDisallowed("/article"));
$this->assertFalse($parser->userAgent('*')->isAllowed("/temp"));
$this->assertTrue($parser->userAgent('notListed')->isAllowed("/"));
$this->assertTrue($parser->userAgent('notListed')->isAllowed("/article"));
$this->assertTrue($parser->userAgent('notListed')->isDisallowed("/temp"));
$this->assertFalse($parser->userAgent('notListed')->isDisallowed("/"));
$this->assertFalse($parser->userAgent('notListed')->isDisallowed("/article"));
$this->assertFalse($parser->userAgent('notListed')->isAllowed("/temp"));
$this->assertTrue($parser->userAgent('agentV')->isDisallowed("/foo"));
$this->assertTrue($parser->userAgent('agentV')->isAllowed("/bar"));
$this->assertTrue($parser->userAgent('agentV')->isAllowed("/Foo"));
$this->assertTrue($parser->userAgent('agentW')->isDisallowed("/foo"));
$this->assertTrue($parser->userAgent('agentW')->isAllowed("/bar"));
$this->assertTrue($parser->userAgent('agentW')->isAllowed("/Foo"));
$this->assertTrue($parser->userAgent('spiderX/1.0')->isAllowed("/temp"));
$this->assertTrue($parser->userAgent('spiderX/1.0')->isDisallowed("/assets"));
$this->assertTrue($parser->userAgent('spiderX/1.0')->isAllowed("/forum"));
$this->assertFalse($parser->userAgent('spiderX/1.0')->isDisallowed("/temp"));
$this->assertFalse($parser->userAgent('spiderX/1.0')->isAllowed("/assets"));
$this->assertFalse($parser->userAgent('spiderX/1.0')->isDisallowed("/forum"));
$this->assertTrue($parser->userAgent('botY-test')->isDisallowed("/"));
$this->assertTrue($parser->userAgent('botY-test')->isDisallowed("/forum"));
$this->assertTrue($parser->userAgent('botY-test')->isAllowed("/forum/"));
$this->assertTrue($parser->userAgent('botY-test')->isDisallowed("/forum/topic"));
$this->assertTrue($parser->userAgent('botY-test')->isDisallowed("/public"));
$this->assertFalse($parser->userAgent('botY-test')->isAllowed("/"));
$this->assertFalse($parser->userAgent('botY-test')->isAllowed("/forum"));
$this->assertFalse($parser->userAgent('botY-test')->isDisallowed("/forum/"));
$this->assertFalse($parser->userAgent('botY-test')->isAllowed("/forum/topic"));
$this->assertFalse($parser->userAgent('botY-test')->isAllowed("/public"));
$this->assertTrue($parser->userAgent('crawlerZ')->isAllowed("/"));
$this->assertTrue($parser->userAgent('crawlerZ')->isDisallowed("/forum"));
$this->assertTrue($parser->userAgent('crawlerZ')->isDisallowed("/public"));
$this->assertFalse($parser->userAgent('crawlerZ')->isDisallowed("/"));
$this->assertFalse($parser->userAgent('crawlerZ')->isAllowed("/forum"));
$this->assertFalse($parser->userAgent('crawlerZ')->isAllowed("/public"));
if ($rendered !== false) {
if (version_compare(phpversion(), '7.0.0', '<')) {
$this->markTestIncomplete('Sort algorithm changed as of PHP 7');
}
$this->assertEquals($rendered, $parser->render()->normal("\n"));
$this->testDisAllow($rendered, false);
}
}
/**
* @dataProvider generateDataForTest
* @param string $robotsTxtContent
* @param string|false $rendered
*/
public function testDisAllowIsListed($robotsTxtContent, $rendered)
{
$parser = new RobotsTxtParser\TxtClient('http://example.com', 200, $robotsTxtContent);
$this->assertInstanceOf('vipnytt\RobotsTxtParser\TxtClient', $parser);
if (version_compare(phpversion(), '7.0.0', '<')) {
$this->markTestIncomplete('Sort algorithm changed as of PHP 7');
}
$this->assertEquals($rendered, $parser->render()->normal("\n"));
// Expected result: String length of matching rule
$this->assertEquals(6, $parser->userAgent('*')->disallow()->hasPath('/admin'));
$this->assertEquals(4, $parser->userAgent('agentV')->allow()->hasPath('/bar'));
$this->expectException(\InvalidArgumentException::class);
$parser->userAgent('*')->disallow()->hasPath('http;//www.example.com/invalid');
}
/**
* Generate test data
*
* @return array
*/
public function generateDataForTest()
{
return [
[
<<<ROBOTS
User-agent: anyone
User-agent: *
Disallow: /admin
Disallow: /admin
Disallow: /Admin*
Disallow: /temp#comment
Disallow: /forum**
Disallow: /admin/cp/test/
User-agent: agentV
User-agent: agentW
Disallow: /foo
Allow: /bar #comment
User-agent: spiderX
Disallow:
Disallow: /admin#
Disallow: /assets
User-agent: botY
Disallow: /
Allow: &&/1@| #invalid
Allow: /forum/$
Allow: /article
User-agent: crawlerZ
Disallow:
Disallow: /
Allow: /$
ROBOTS
,
<<<RENDERED
User-agent: *
User-agent: anyone
Disallow: /temp
Disallow: /forum
Disallow: /admin
Disallow: /Admin
Disallow: /admin/cp/test/
User-agent: agentv
User-agent: agentw
Disallow: /foo
Allow: /bar
User-agent: boty
Disallow: /
Allow: /forum/$
Allow: /article
User-agent: crawlerz
Disallow: /
Allow: /$
User-agent: spiderx
Disallow: /admin
Disallow: /assets
RENDERED
]
];
}
}