Completed
Push — master ( 3efa28...274e52 )
by Mark
01:12
created

UserAgentTest::it_returns_full_matched_bot_name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 8
loc 8
rs 10
c 0
b 0
f 0
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
use PHPUnit\Framework\TestCase;
15
16
final class UserAgentTest extends TestCase
17
{
18
    private $CrawlerDetect;
19
20
    protected function setUp()
21
    {
22
        $this->CrawlerDetect = new CrawlerDetect();
23
    }
24
25
    /** @test */
26 View Code Duplication
    public function user_agents_are_bots()
27
    {
28
        $lines = file(__DIR__.'/crawlers.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
29
30
        foreach ($lines as $line) {
31
            $test = $this->CrawlerDetect->isCrawler($line);
32
            $this->assertTrue($test, $line);
33
        }
34
    }
35
36
    /** @test */
37 View Code Duplication
    public function user_agents_are_devices()
38
    {
39
        $lines = file(__DIR__.'/devices.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
40
41
        foreach ($lines as $line) {
42
            $test = $this->CrawlerDetect->isCrawler($line);
43
            $this->assertFalse($test, $line);
44
        }
45
    }
46
47
    /** @test */
48 View Code Duplication
    public function it_returns_correct_matched_bot_name()
49
    {
50
        $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)');
0 ignored issues
show
Unused Code introduced by
$test is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
52
        $matches = $this->CrawlerDetect->getMatches();
53
54
        $this->assertEquals($this->CrawlerDetect->getMatches(), 'monitoring', $matches);
55
    }
56
57
    /** @test */
58 View Code Duplication
    public function it_returns_full_matched_bot_name()
59
    {
60
        $test = $this->CrawlerDetect->isCrawler('somenaughtybot');
0 ignored issues
show
Unused Code introduced by
$test is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
61
62
        $matches = $this->CrawlerDetect->getMatches();
63
64
        $this->assertEquals($this->CrawlerDetect->getMatches(), 'somenaughtybot', $matches);
65
    }
66
67
    /** @test */
68 View Code Duplication
    public function it_returns_null_when_no_bot_detected()
69
    {
70
        $test = $this->CrawlerDetect->isCrawler('nothing to see here');
0 ignored issues
show
Unused Code introduced by
$test is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
71
72
        $matches = $this->CrawlerDetect->getMatches();
73
74
        $this->assertEquals($this->CrawlerDetect->getMatches(), null, $matches);
75
    }
76
77
    /** @test */
78
    public function empty_user_agent()
79
    {
80
        $test = $this->CrawlerDetect->isCrawler('      ');
81
82
        $this->assertFalse($test);
83
    }
84
85
    /** @test */
86
    public function current_visitor()
87
    {
88
        $headers = (array) json_decode('{"DOCUMENT_ROOT":"\/home\/test\/public_html","GATEWAY_INTERFACE":"CGI\/1.1","HTTP_ACCEPT":"*\/*","HTTP_ACCEPT_ENCODING":"gzip, deflate","HTTP_CACHE_CONTROL":"no-cache","HTTP_CONNECTION":"Keep-Alive","HTTP_FROM":"bingbot(at)microsoft.com","HTTP_HOST":"www.test.com","HTTP_PRAGMA":"no-cache","HTTP_USER_AGENT":"Mozilla\/5.0 (compatible; bingbot\/2.0; +http:\/\/www.bing.com\/bingbot.htm)","PATH":"\/bin:\/usr\/bin","QUERY_STRING":"order=closingDate","REDIRECT_STATUS":"200","REMOTE_ADDR":"127.0.0.1","REMOTE_PORT":"3360","REQUEST_METHOD":"GET","REQUEST_URI":"\/?test=testing","SCRIPT_FILENAME":"\/home\/test\/public_html\/index.php","SCRIPT_NAME":"\/index.php","SERVER_ADDR":"127.0.0.1","SERVER_ADMIN":"[email protected]","SERVER_NAME":"www.test.com","SERVER_PORT":"80","SERVER_PROTOCOL":"HTTP\/1.1","SERVER_SIGNATURE":"","SERVER_SOFTWARE":"Apache","UNIQUE_ID":"Vx6MENRxerBUSDEQgFLAAAAAS","PHP_SELF":"\/index.php","REQUEST_TIME_FLOAT":1461619728.0705,"REQUEST_TIME":1461619728}');
89
90
        $cd = new CrawlerDetect($headers);
91
92
        $this->assertTrue($cd->isCrawler());
93
    }
94
95
    /** @test */
96
    public function user_agent_passed_via_contructor()
97
    {
98
        $cd = new CrawlerDetect(null, '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)');
99
100
        $this->assertTrue($cd->isCrawler());
101
    }
102
103
    /** @test */
104
    public function http_from_header()
105
    {
106
        $headers = (array) json_decode('{"DOCUMENT_ROOT":"\/home\/test\/public_html","GATEWAY_INTERFACE":"CGI\/1.1","HTTP_ACCEPT":"*\/*","HTTP_ACCEPT_ENCODING":"gzip, deflate","HTTP_CACHE_CONTROL":"no-cache","HTTP_CONNECTION":"Keep-Alive","HTTP_FROM":"googlebot(at)googlebot.com","HTTP_HOST":"www.test.com","HTTP_PRAGMA":"no-cache","HTTP_USER_AGENT":"Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/28.0.1500.71 Safari\/537.36","PATH":"\/bin:\/usr\/bin","QUERY_STRING":"order=closingDate","REDIRECT_STATUS":"200","REMOTE_ADDR":"127.0.0.1","REMOTE_PORT":"3360","REQUEST_METHOD":"GET","REQUEST_URI":"\/?test=testing","SCRIPT_FILENAME":"\/home\/test\/public_html\/index.php","SCRIPT_NAME":"\/index.php","SERVER_ADDR":"127.0.0.1","SERVER_ADMIN":"[email protected]","SERVER_NAME":"www.test.com","SERVER_PORT":"80","SERVER_PROTOCOL":"HTTP\/1.1","SERVER_SIGNATURE":"","SERVER_SOFTWARE":"Apache","UNIQUE_ID":"Vx6MENRxerBUSDEQgFLAAAAAS","PHP_SELF":"\/index.php","REQUEST_TIME_FLOAT":1461619728.0705,"REQUEST_TIME":1461619728}');
107
108
        $cd = new CrawlerDetect($headers);
109
110
        $this->assertTrue($cd->isCrawler());
111
    }
112
113
    /** @test */
114
    public function matches_does_not_persit_across_multiple_calls()
115
    {
116
        $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)');
117
        $matches = $this->CrawlerDetect->getMatches();
118
        $this->assertEquals($this->CrawlerDetect->getMatches(), 'monitoring', $matches);
119
120
        $this->CrawlerDetect->isCrawler('This should not match');
121
        $matches = $this->CrawlerDetect->getMatches();
0 ignored issues
show
Unused Code introduced by
$matches is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
122
        $this->assertNull($this->CrawlerDetect->getMatches());
123
    }
124
125
    /** @test */
126
    public function the_regex_patterns_are_unique()
127
    {
128
        $crawlers = new Crawlers();
129
130
        $this->assertEquals(count($crawlers->getAll()), count(array_unique($crawlers->getAll())));
131
    }
132
133
    /** @test */
134
    public function there_are_no_regex_collisions()
135
    {
136
        $crawlers = new Crawlers();
137
138
        foreach ($crawlers->getAll() as $key1 => $regex) {
139
            foreach ($crawlers->getAll() as $key2 => $compare) {
140
                // Dont check this regex against itself
141
                if ($key1 != $key2) {
142
                    preg_match('/'.$regex.'/i', stripslashes($compare), $matches);
143
144
                    $this->assertEmpty($matches, $regex.' collided with '.$compare);
145
                }
146
            }
147
        }
148
    }
149
}
150