Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 1 | <?php |
| 2 | |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 3 | class UserAgent_test extends CI_TestCase { |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 4 | |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 5 | 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 Barnes | 051a317 | 2011-11-27 00:49:25 -0500 | [diff] [blame] | 7 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 8 | public function set_up() |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 9 | { |
| 10 | // set a baseline user agent |
| 11 | $_SERVER['HTTP_USER_AGENT'] = $this->_user_agent; |
Eric Barnes | 051a317 | 2011-11-27 00:49:25 -0500 | [diff] [blame] | 12 | |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 13 | $this->ci_vfs_clone('application/config/user_agents.php'); |
Andrey Andreev | 10e7a32 | 2014-02-20 16:42:16 +0200 | [diff] [blame] | 14 | $this->agent = new CI_User_agent(); |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 15 | $this->ci_instance_var('agent', $this->agent); |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | // -------------------------------------------------------------------- |
| 19 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 20 | public function test_accept_lang() |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 21 | { |
| 22 | $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en'; |
Eric Barnes | 051a317 | 2011-11-27 00:49:25 -0500 | [diff] [blame] | 23 | $this->assertTrue($this->agent->accept_lang()); |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 24 | unset($_SERVER['HTTP_ACCEPT_LANGUAGE']); |
Eric Barnes | 051a317 | 2011-11-27 00:49:25 -0500 | [diff] [blame] | 25 | $this->assertTrue($this->agent->accept_lang('en')); |
| 26 | $this->assertFalse($this->agent->accept_lang('fr')); |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | // -------------------------------------------------------------------- |
| 30 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 31 | public function test_mobile() |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 32 | { |
| 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 Andreev | a9938a0 | 2014-01-17 14:55:56 +0200 | [diff] [blame] | 41 | public function test_is_functions() |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 42 | { |
| 43 | $this->assertTrue($this->agent->is_browser()); |
Andrey Andreev | a9938a0 | 2014-01-17 14:55:56 +0200 | [diff] [blame] | 44 | $this->assertTrue($this->agent->is_browser('Safari')); |
| 45 | $this->assertFalse($this->agent->is_browser('Firefox')); |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 46 | $this->assertFalse($this->agent->is_robot()); |
| 47 | $this->assertFalse($this->agent->is_mobile()); |
Andrey Andreev | a9938a0 | 2014-01-17 14:55:56 +0200 | [diff] [blame] | 48 | } |
| 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 Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 60 | $this->assertFalse($this->agent->is_referral()); |
Andrey Andreev | a9938a0 | 2014-01-17 14:55:56 +0200 | [diff] [blame] | 61 | $this->assertEquals('', $this->agent->referrer()); |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // -------------------------------------------------------------------- |
| 65 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 66 | public function test_agent_string() |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 67 | { |
| 68 | $this->assertEquals($this->_user_agent, $this->agent->agent_string()); |
| 69 | } |
| 70 | |
| 71 | // -------------------------------------------------------------------- |
| 72 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 73 | public function test_browser_info() |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 74 | { |
| 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 Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 79 | } |
Eric Barnes | 051a317 | 2011-11-27 00:49:25 -0500 | [diff] [blame] | 80 | |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 81 | // -------------------------------------------------------------------- |
| 82 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 83 | public function test_charsets() |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 84 | { |
| 85 | $_SERVER['HTTP_ACCEPT_CHARSET'] = 'utf8'; |
Andrey Andreev | a9938a0 | 2014-01-17 14:55:56 +0200 | [diff] [blame] | 86 | $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 Barnes | 051a317 | 2011-11-27 00:49:25 -0500 | [diff] [blame] | 91 | |
Andrey Andreev | a9938a0 | 2014-01-17 14:55:56 +0200 | [diff] [blame] | 92 | $_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 Barnes | 051a317 | 2011-11-27 00:49:25 -0500 | [diff] [blame] | 96 | |
Andrey Andreev | a9938a0 | 2014-01-17 14:55:56 +0200 | [diff] [blame] | 97 | $_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 Barnes | 051a317 | 2011-11-27 00:49:25 -0500 | [diff] [blame] | 104 | |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 105 | unset($_SERVER['HTTP_ACCEPT_CHARSET']); |
Andrey Andreev | a9938a0 | 2014-01-17 14:55:56 +0200 | [diff] [blame] | 106 | } |
Eric Barnes | 051a317 | 2011-11-27 00:49:25 -0500 | [diff] [blame] | 107 | |
Andrey Andreev | a9938a0 | 2014-01-17 14:55:56 +0200 | [diff] [blame] | 108 | 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 Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 123 | } |
| 124 | |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 125 | } |