1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Crawler Detect - the web crawler detection library. |
5
|
|
|
* |
6
|
|
|
* (c) Mark Beech <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
use Jaybizzle\CrawlerDetect\CrawlerDetect; |
13
|
|
|
use Jaybizzle\CrawlerDetect\Fixtures\Crawlers; |
14
|
|
|
|
15
|
|
|
class UserAgentTest extends PHPUnit_Framework_TestCase |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
protected $CrawlerDetect; |
18
|
|
|
|
19
|
|
|
public function setUp() |
20
|
|
|
{ |
21
|
|
|
$this->CrawlerDetect = new CrawlerDetect(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
View Code Duplication |
public function testBots() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$lines = file(__DIR__.'/crawlers.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
27
|
|
|
|
28
|
|
|
foreach ($lines as $line) { |
29
|
|
|
$test = $this->CrawlerDetect->isCrawler($line); |
30
|
|
|
$this->assertEquals($test, true, $line); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
View Code Duplication |
public function testDevices() |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$lines = file(__DIR__.'/devices.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
37
|
|
|
|
38
|
|
|
foreach ($lines as $line) { |
39
|
|
|
$test = $this->CrawlerDetect->isCrawler($line); |
40
|
|
|
$this->assertEquals($test, false, $line); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testReturnsCorrectMatchedBotName() |
45
|
|
|
{ |
46
|
|
|
$test = $this->CrawlerDetect->isCrawler('Mozilla/5.0 (iPhone; CPU iPhone OS 7_1 like Mac OS X) AppleWebKit (KHTML, like Gecko) Mobile (compatible; Yahoo Ad monitoring; https://help.yahoo.com/kb/yahoo-ad-monitoring-SLN24857.html)'); |
|
|
|
|
47
|
|
|
|
48
|
|
|
$matches = $this->CrawlerDetect->getMatches(); |
49
|
|
|
|
50
|
|
|
$this->assertEquals($this->CrawlerDetect->getMatches(), 'Yahoo Ad monitoring', $matches); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testEmptyUserAgent() |
54
|
|
|
{ |
55
|
|
|
$test = $this->CrawlerDetect->isCrawler(' '); |
56
|
|
|
|
57
|
|
|
$this->assertEquals($test, false); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testForRegexCollision() |
61
|
|
|
{ |
62
|
|
|
$crawlers = new Crawlers(); |
63
|
|
|
|
64
|
|
|
foreach ($crawlers->getAll() as $key1 => $regex) { |
65
|
|
|
foreach ($crawlers->getAll() as $key2 => $compare) { |
66
|
|
|
// Dont check this regex against itself |
67
|
|
|
if ($key1 != $key2) { |
68
|
|
|
preg_match('/'.$regex.'/i', stripslashes($compare), $matches); |
69
|
|
|
|
70
|
|
|
$this->assertEmpty($matches, $regex.' collided with '.$compare); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.