forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserAgentTest.php
More file actions
115 lines (95 loc) · 3.97 KB
/
UserAgentTest.php
File metadata and controls
115 lines (95 loc) · 3.97 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
<?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\Group;
/**
* @internal
*/
#[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
{
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('533.20.27', $this->agent->getVersion());
$this->assertSame('', $this->agent->getRobot());
}
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);
}
}