blob: c02f2bd9d2d31c80a52013c421e245f5d118fd30 [file] [log] [blame]
Greg Aker9512ab82011-04-21 15:10:48 -05001<?php
2
Taufan Adityaac5373a2012-03-28 16:03:38 +07003class UserAgent_test extends CI_TestCase {
Andrey Andreevc1862882012-06-09 23:16:58 +03004
Greg Aker9512ab82011-04-21 15:10:48 -05005 protected $_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';
6 protected $_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';
Eric Barnes051a3172011-11-27 00:49:25 -05007
Eric Barnes68286a42011-04-21 22:00:33 -04008 public function set_up()
Greg Aker9512ab82011-04-21 15:10:48 -05009 {
10 // set a baseline user agent
11 $_SERVER['HTTP_USER_AGENT'] = $this->_user_agent;
Eric Barnes051a3172011-11-27 00:49:25 -050012
dchill427ecc5cd2012-10-12 16:25:51 -040013 $this->ci_vfs_clone('application/config/user_agents.php');
Andrey Andreev10e7a322014-02-20 16:42:16 +020014 $this->agent = new CI_User_agent();
dchill427ecc5cd2012-10-12 16:25:51 -040015 $this->ci_instance_var('agent', $this->agent);
Greg Aker9512ab82011-04-21 15:10:48 -050016 }
17
18 // --------------------------------------------------------------------
19
Eric Barnes68286a42011-04-21 22:00:33 -040020 public function test_accept_lang()
Greg Aker9512ab82011-04-21 15:10:48 -050021 {
22 $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en';
Eric Barnes051a3172011-11-27 00:49:25 -050023 $this->assertTrue($this->agent->accept_lang());
Greg Aker9512ab82011-04-21 15:10:48 -050024 unset($_SERVER['HTTP_ACCEPT_LANGUAGE']);
Eric Barnes051a3172011-11-27 00:49:25 -050025 $this->assertTrue($this->agent->accept_lang('en'));
26 $this->assertFalse($this->agent->accept_lang('fr'));
Greg Aker9512ab82011-04-21 15:10:48 -050027 }
28
29 // --------------------------------------------------------------------
30
Eric Barnes68286a42011-04-21 22:00:33 -040031 public function test_mobile()
Greg Aker9512ab82011-04-21 15:10:48 -050032 {
33 // Mobile Not Set
34 $_SERVER['HTTP_USER_AGENT'] = $this->_mobile_ua;
35 $this->assertEquals('', $this->agent->mobile());
36 unset($_SERVER['HTTP_USER_AGENT']);
37 }
38
39 // --------------------------------------------------------------------
40
Andrey Andreeva9938a02014-01-17 14:55:56 +020041 public function test_is_functions()
Greg Aker9512ab82011-04-21 15:10:48 -050042 {
43 $this->assertTrue($this->agent->is_browser());
Andrey Andreeva9938a02014-01-17 14:55:56 +020044 $this->assertTrue($this->agent->is_browser('Safari'));
45 $this->assertFalse($this->agent->is_browser('Firefox'));
Greg Aker9512ab82011-04-21 15:10:48 -050046 $this->assertFalse($this->agent->is_robot());
47 $this->assertFalse($this->agent->is_mobile());
Andrey Andreeva9938a02014-01-17 14:55:56 +020048 }
49
50 // --------------------------------------------------------------------
51
52 public function test_referrer()
53 {
54 $_SERVER['HTTP_REFERER'] = 'http://codeigniter.com/user_guide/';
55 $this->assertTrue($this->agent->is_referral());
56 $this->assertEquals('http://codeigniter.com/user_guide/', $this->agent->referrer());
57
58 $this->agent->referer = NULL;
59 unset($_SERVER['HTTP_REFERER']);
Greg Aker9512ab82011-04-21 15:10:48 -050060 $this->assertFalse($this->agent->is_referral());
Andrey Andreeva9938a02014-01-17 14:55:56 +020061 $this->assertEquals('', $this->agent->referrer());
Greg Aker9512ab82011-04-21 15:10:48 -050062 }
63
64 // --------------------------------------------------------------------
65
Eric Barnes68286a42011-04-21 22:00:33 -040066 public function test_agent_string()
Greg Aker9512ab82011-04-21 15:10:48 -050067 {
68 $this->assertEquals($this->_user_agent, $this->agent->agent_string());
69 }
70
71 // --------------------------------------------------------------------
72
Eric Barnes68286a42011-04-21 22:00:33 -040073 public function test_browser_info()
Greg Aker9512ab82011-04-21 15:10:48 -050074 {
75 $this->assertEquals('Mac OS X', $this->agent->platform());
76 $this->assertEquals('Safari', $this->agent->browser());
77 $this->assertEquals('533.20.27', $this->agent->version());
78 $this->assertEquals('', $this->agent->robot());
Greg Aker9512ab82011-04-21 15:10:48 -050079 }
Eric Barnes051a3172011-11-27 00:49:25 -050080
Greg Aker9512ab82011-04-21 15:10:48 -050081 // --------------------------------------------------------------------
82
Eric Barnes68286a42011-04-21 22:00:33 -040083 public function test_charsets()
Greg Aker9512ab82011-04-21 15:10:48 -050084 {
85 $_SERVER['HTTP_ACCEPT_CHARSET'] = 'utf8';
Andrey Andreeva9938a02014-01-17 14:55:56 +020086 $this->agent->charsets = array();
87 $this->agent->charsets();
88 $this->assertTrue($this->agent->accept_charset('utf8'));
89 $this->assertFalse($this->agent->accept_charset('foo'));
90 $this->assertEquals('utf8', $this->agent->charsets[0]);
Eric Barnes051a3172011-11-27 00:49:25 -050091
Andrey Andreeva9938a02014-01-17 14:55:56 +020092 $_SERVER['HTTP_ACCEPT_CHARSET'] = '';
93 $this->agent->charsets = array();
94 $this->assertFalse($this->agent->accept_charset());
95 $this->assertEquals('Undefined', $this->agent->charsets[0]);
Eric Barnes051a3172011-11-27 00:49:25 -050096
Andrey Andreeva9938a02014-01-17 14:55:56 +020097 $_SERVER['HTTP_ACCEPT_CHARSET'] = 'iso-8859-5, unicode-1-1; q=0.8';
98 $this->agent->charsets = array();
99 $this->assertTrue($this->agent->accept_charset('iso-8859-5'));
100 $this->assertTrue($this->agent->accept_charset('unicode-1-1'));
101 $this->assertFalse($this->agent->accept_charset('foo'));
102 $this->assertEquals('iso-8859-5', $this->agent->charsets[0]);
103 $this->assertEquals('unicode-1-1', $this->agent->charsets[1]);
Eric Barnes051a3172011-11-27 00:49:25 -0500104
Greg Aker9512ab82011-04-21 15:10:48 -0500105 unset($_SERVER['HTTP_ACCEPT_CHARSET']);
Andrey Andreeva9938a02014-01-17 14:55:56 +0200106 }
Eric Barnes051a3172011-11-27 00:49:25 -0500107
Andrey Andreeva9938a02014-01-17 14:55:56 +0200108 public function test_parse()
109 {
110 $new_agent = 'Mozilla/5.0 (Android; Mobile; rv:13.0) Gecko/13.0 Firefox/13.0';
111 $this->agent->parse($new_agent);
112
113 $this->assertEquals('Android', $this->agent->platform());
114 $this->assertEquals('Firefox', $this->agent->browser());
115 $this->assertEquals('13.0', $this->agent->version());
116 $this->assertEquals('', $this->agent->robot());
117 $this->assertEquals('Android', $this->agent->mobile());
118 $this->assertEquals($new_agent, $this->agent->agent_string());
119 $this->assertTrue($this->agent->is_browser());
120 $this->assertFalse($this->agent->is_robot());
121 $this->assertTrue($this->agent->is_mobile());
122 $this->assertTrue($this->agent->is_mobile('android'));
Greg Aker9512ab82011-04-21 15:10:48 -0500123 }
124
Greg Aker9512ab82011-04-21 15:10:48 -0500125}