Repository navigation
Expand file tree
/
Copy pathUserAgentTest.php
More file actions
146 lines (120 loc) · 5.29 KB
/
Copy pathUserAgentTest.php
File metadata and controls
146 lines (120 loc) · 5.29 KB
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
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\HTTP;
use CodeIgniter\Test\CIUnitTestCase;
use PHPUnit\Framework\Attributes\BackupGlobals;
use PHPUnit\Framework\Attributes\Group;
/**
* @internal
*/
#[BackupGlobals(true)]
#[Group('Others')]
final class UserAgentTest extends CIUnitTestCase
{
private string $_user_agent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-us) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27';
private string $_mobile_ua = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B117 Safari/6531.22.7';
private UserAgent $agent;
protected function setUp(): void
{
$this->resetServices();
parent::setUp();
// set a baseline user agent
service('superglobals')->setServer('HTTP_USER_AGENT', $this->_user_agent);
$this->agent = new UserAgent();
}
public function testMobile(): void
{
// Mobile Not Set
service('superglobals')->setServer('HTTP_USER_AGENT', $this->_mobile_ua);
$this->assertFalse($this->agent->isMobile());
}
public function testIsFunctions(): void
{
$this->assertTrue($this->agent->isBrowser());
$this->assertTrue($this->agent->isBrowser('Safari'));
$this->assertFalse($this->agent->isBrowser('Firefox'));
$this->assertFalse($this->agent->isRobot());
$this->assertFalse($this->agent->isMobile());
}
public function testReferrer(): void
{
service('superglobals')->setServer('HTTP_REFERER', 'http://codeigniter.com/user_guide/');
$this->assertTrue($this->agent->isReferral());
$this->assertSame('http://codeigniter.com/user_guide/', $this->agent->getReferrer());
$this->setPrivateProperty($this->agent, 'referrer', null);
service('superglobals')->setServer('HTTP_REFERER', '');
$this->assertFalse($this->agent->isReferral());
$this->assertSame('', $this->agent->getReferrer());
}
public function testAgentString(): void
{
$this->assertSame($this->_user_agent, $this->agent->getAgentString());
}
public function testBrowserInfo(): void
{
$this->assertSame('Mac OS X', $this->agent->getPlatform());
$this->assertSame('Safari', $this->agent->getBrowser());
$this->assertSame('5.0.4', $this->agent->getVersion());
$this->assertSame('', $this->agent->getRobot());
}
public function testParseModernSafariUsesVersionToken(): void
{
$this->agent->parse('Mozilla/5.0 (Macintosh; Intel Mac OS X 13_2) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Safari/605.1.15');
$this->assertSame('Mac OS X', $this->agent->getPlatform());
$this->assertSame('Safari', $this->agent->getBrowser());
$this->assertSame('16.3', $this->agent->getVersion());
}
public function testParseMobileSafariUsesVersionToken(): void
{
$this->agent->parse($this->_mobile_ua);
$this->assertSame('iOS', $this->agent->getPlatform());
$this->assertSame('Safari', $this->agent->getBrowser());
$this->assertSame('4.0.5', $this->agent->getVersion());
$this->assertSame('Apple iPhone', $this->agent->getMobile());
}
public function testParseChromeWithSafariTokenUsesChromeVersion(): void
{
$this->agent->parse('Mozilla/5.0 (Macintosh; Intel Mac OS X 13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36');
$this->assertSame('Mac OS X', $this->agent->getPlatform());
$this->assertSame('Chrome', $this->agent->getBrowser());
$this->assertSame('110.0.0.0', $this->agent->getVersion());
}
public function testParse(): void
{
$newAgent = 'Mozilla/5.0 (Android; Mobile; rv:13.0) Gecko/13.0 Firefox/13.0';
$this->agent->parse($newAgent);
$this->assertSame('Android', $this->agent->getPlatform());
$this->assertSame('Firefox', $this->agent->getBrowser());
$this->assertSame('13.0', $this->agent->getVersion());
$this->assertSame('', $this->agent->getRobot());
$this->assertSame('Android', $this->agent->getMobile());
$this->assertSame($newAgent, $this->agent->getAgentString());
$this->assertTrue($this->agent->isBrowser());
$this->assertFalse($this->agent->isRobot());
$this->assertTrue($this->agent->isMobile());
$this->assertTrue($this->agent->isMobile('android'));
}
public function testParseBot(): void
{
$newAgent = 'Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)';
$this->agent->parse($newAgent);
$this->assertFalse($this->agent->isBrowser());
$this->assertTrue($this->agent->isRobot());
$this->assertFalse($this->agent->isRobot('Bob'));
$this->assertFalse($this->agent->isMobile());
}
public function testEmptyUserAgentVariable(): void
{
service('superglobals')->setServer('HTTP_USER_AGENT', '');
$agent = new UserAgent();
$this->assertEmpty((string) $agent);
}
}