Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | require BASEPATH.'libraries/User_agent.php'; |
| 4 | |
| 5 | // This class needs some work... |
| 6 | |
| 7 | class UserAgent_test extends CI_TestCase |
| 8 | { |
| 9 | 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'; |
| 10 | 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] | 11 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 12 | public function set_up() |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 13 | { |
| 14 | // set a baseline user agent |
| 15 | $_SERVER['HTTP_USER_AGENT'] = $this->_user_agent; |
Eric Barnes | 051a317 | 2011-11-27 00:49:25 -0500 | [diff] [blame] | 16 | |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 17 | $obj = new StdClass; |
| 18 | $obj->agent = new CI_User_agent(); |
Eric Barnes | 051a317 | 2011-11-27 00:49:25 -0500 | [diff] [blame] | 19 | |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 20 | $this->ci_instance($obj); |
Eric Barnes | 051a317 | 2011-11-27 00:49:25 -0500 | [diff] [blame] | 21 | |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 22 | $this->agent = $obj->agent; |
| 23 | } |
| 24 | |
| 25 | // -------------------------------------------------------------------- |
| 26 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 27 | public function test_accept_lang() |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 28 | { |
| 29 | $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en'; |
Eric Barnes | 051a317 | 2011-11-27 00:49:25 -0500 | [diff] [blame] | 30 | $this->assertTrue($this->agent->accept_lang()); |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 31 | unset($_SERVER['HTTP_ACCEPT_LANGUAGE']); |
Eric Barnes | 051a317 | 2011-11-27 00:49:25 -0500 | [diff] [blame] | 32 | $this->assertTrue($this->agent->accept_lang('en')); |
| 33 | $this->assertFalse($this->agent->accept_lang('fr')); |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // -------------------------------------------------------------------- |
| 37 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 38 | public function test_mobile() |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 39 | { |
| 40 | // Mobile Not Set |
| 41 | $_SERVER['HTTP_USER_AGENT'] = $this->_mobile_ua; |
| 42 | $this->assertEquals('', $this->agent->mobile()); |
| 43 | unset($_SERVER['HTTP_USER_AGENT']); |
| 44 | } |
| 45 | |
| 46 | // -------------------------------------------------------------------- |
| 47 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 48 | public function test_util_is_functions() |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 49 | { |
| 50 | $this->assertTrue($this->agent->is_browser()); |
| 51 | $this->assertFalse($this->agent->is_robot()); |
| 52 | $this->assertFalse($this->agent->is_mobile()); |
| 53 | $this->assertFalse($this->agent->is_referral()); |
| 54 | } |
| 55 | |
| 56 | // -------------------------------------------------------------------- |
| 57 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 58 | public function test_agent_string() |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 59 | { |
| 60 | $this->assertEquals($this->_user_agent, $this->agent->agent_string()); |
| 61 | } |
| 62 | |
| 63 | // -------------------------------------------------------------------- |
| 64 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 65 | public function test_browser_info() |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 66 | { |
| 67 | $this->assertEquals('Mac OS X', $this->agent->platform()); |
| 68 | $this->assertEquals('Safari', $this->agent->browser()); |
| 69 | $this->assertEquals('533.20.27', $this->agent->version()); |
| 70 | $this->assertEquals('', $this->agent->robot()); |
| 71 | $this->assertEquals('', $this->agent->referrer()); |
| 72 | } |
Eric Barnes | 051a317 | 2011-11-27 00:49:25 -0500 | [diff] [blame] | 73 | |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 74 | // -------------------------------------------------------------------- |
| 75 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 76 | public function test_charsets() |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 77 | { |
| 78 | $_SERVER['HTTP_ACCEPT_CHARSET'] = 'utf8'; |
Eric Barnes | 051a317 | 2011-11-27 00:49:25 -0500 | [diff] [blame] | 79 | |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 80 | $charsets = $this->agent->charsets(); |
Eric Barnes | 051a317 | 2011-11-27 00:49:25 -0500 | [diff] [blame] | 81 | |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 82 | $this->assertEquals('utf8', $charsets[0]); |
Eric Barnes | 051a317 | 2011-11-27 00:49:25 -0500 | [diff] [blame] | 83 | |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 84 | unset($_SERVER['HTTP_ACCEPT_CHARSET']); |
Eric Barnes | 051a317 | 2011-11-27 00:49:25 -0500 | [diff] [blame] | 85 | |
Greg Aker | 9512ab8 | 2011-04-21 15:10:48 -0500 | [diff] [blame] | 86 | $this->assertFalse($this->agent->accept_charset()); |
| 87 | } |
| 88 | |
| 89 | // -------------------------------------------------------------------- |
| 90 | |
| 91 | } |