2
0

UserAgentTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Http_UserAgent
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: JsonTest.php 12081 2008-10-22 19:07:55Z norm2782 $
  21. */
  22. require_once 'Zend/Config.php';
  23. require_once 'Zend/Http/UserAgent.php';
  24. require_once 'Zend/Http/UserAgent/Mobile.php';
  25. require_once 'Zend/Http/UserAgent/Storage/NonPersistent.php';
  26. require_once dirname(__FILE__) . '/TestAsset/TestPluginLoader.php';
  27. require_once dirname(__FILE__) . '/TestAsset/DesktopDevice.php';
  28. require_once dirname(__FILE__) . '/TestAsset/InvalidDevice.php';
  29. require_once dirname(__FILE__) . '/TestAsset/PopulatedStorage.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Http_UserAgent
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Http
  37. * @group Zend_Http_UserAgent
  38. */
  39. class Zend_Http_UserAgentTest extends PHPUnit_Framework_TestCase
  40. {
  41. public function setUp()
  42. {
  43. $this->server = array();
  44. $this->server['os'] = 'Windows_NT';
  45. $this->server['http_accept'] = '*/*';
  46. $this->server['http_accept_language'] = 'fr-FR';
  47. $this->server['http_accept_encoding'] = 'gzip, deflate';
  48. $this->server['http_user_agent'] = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)';
  49. $this->server['http_host'] = 'zfmobile';
  50. $this->server['http_connection'] = 'Keep-Alive';
  51. $this->server['http_cookie'] = 'ZDEDebuggerPresent=php,phtml,php3';
  52. $this->server['server_signature'] = '';
  53. $this->server['server_software'] = 'Apache/2.2.12 (Win32) mod_ssl/2.2.12 OpenSSL/0.9.8k';
  54. $this->server['server_name'] = 'zfmobile';
  55. $this->server['server_addr'] = '127.0.0.1';
  56. $this->server['server_port'] = '80';
  57. $this->server['remote_addr'] = '127.0.0.1';
  58. $this->server['server_protocol'] = 'HTTP/1.1';
  59. $this->config = array(
  60. 'server' => &$this->server,
  61. 'storage' => array(
  62. 'adapter' => 'NonPersistent',
  63. ),
  64. 'wurflapi' => array(
  65. 'wurfl_lib_dir' => constant('TESTS_ZEND_HTTP_USERAGENT_WURFL_LIB_DIR'),
  66. 'wurfl_config_file' => constant('TESTS_ZEND_HTTP_USERAGENT_WURFL_CONFIG_FILE'),
  67. ),
  68. );
  69. }
  70. public function testMatchUserAgentSimple()
  71. {
  72. $config = $this->config;
  73. $config['server']['server_software'] = 'Apache/2';
  74. $userAgent = new Zend_Http_UserAgent($config);
  75. $device = $userAgent->getDevice();
  76. $this->assertEquals('desktop', $userAgent->getBrowserType());
  77. $this->assertEquals('Internet Explorer', $device->getFeature('browser_name'));
  78. $this->assertEquals('7.0', $device->getFeature('browser_version'));
  79. $this->assertEquals('Internet Explorer', $device->getFeature('browser_compatibility'));
  80. $this->assertEquals('MSIE', $device->getFeature('browser_engine'));
  81. $this->assertEquals('Windows XP', $device->getFeature('device_os_name'));
  82. $this->assertEquals('Windows NT 5.1', $device->getFeature('device_os_token'));
  83. $this->assertEquals('apache', $device->getFeature('server_os'));
  84. $this->assertEquals('2', $device->getFeature('server_os_version'));
  85. }
  86. public function testMatchUserAgentServer()
  87. {
  88. $config = $this->config;
  89. $config['server']['os'] = 'Windows_NT';
  90. $config['server']['http_accept'] = '*/*';
  91. $config['server']['http_user_agent'] = 'Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1)';
  92. $config['server']['server_software'] = 'Apache/99';
  93. $config['user_agent'] = $config['server']["http_user_agent"];
  94. $userAgent = new Zend_Http_UserAgent($config);
  95. $device = $userAgent->getDevice();
  96. $this->assertEquals('desktop', $userAgent->getBrowserType());
  97. $this->assertEquals('Internet Explorer', $device->getFeature('browser_name'));
  98. $this->assertEquals('9.0', $device->getFeature('browser_version'));
  99. $this->assertEquals('Internet Explorer', $device->getFeature('browser_compatibility'));
  100. $this->assertEquals('MSIE', $device->getFeature('browser_engine'));
  101. $this->assertEquals('Windows XP', $device->getFeature('device_os_name'));
  102. $this->assertEquals('Windows NT 5.1', $device->getFeature('device_os_token'));
  103. $this->assertEquals('apache', $device->getFeature('server_os'));
  104. $this->assertEquals('99', $device->getFeature('server_os_version'));
  105. }
  106. public function testUserAgentDefineIdentificationSequence()
  107. {
  108. if (!constant('TESTS_ZEND_HTTP_USERAGENT_WURFL_LIB_DIR')) {
  109. $this->markTestSkipped('Depends on WURFL support');
  110. }
  111. $config = $this->config;
  112. $config['user_agent'] = 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleW1ebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A102 Safari/419.3';
  113. $userAgent = new Zend_Http_UserAgent($config);
  114. $device = $userAgent->getDevice();
  115. $this->assertType('Zend_Http_UserAgent_Mobile', $device);
  116. $this->assertEquals('mobile', $userAgent->getBrowserType());
  117. $this->assertEquals('Safari', $userAgent->getDevice()->getFeature('mobile_browser'));
  118. $this->assertEquals('iPhone OS', $userAgent->getDevice()->getFeature('device_os'));
  119. $this->assertEquals('true', $userAgent->getDevice()->getFeature('has_qwerty_keyboard'));
  120. $this->assertEquals('touchscreen', $userAgent->getDevice()->getFeature('pointing_method'));
  121. $this->assertEquals('false', $userAgent->getDevice()->getFeature('is_tablet'));
  122. $this->assertEquals('iPhone', $userAgent->getDevice()->getFeature('model_name'));
  123. $this->assertEquals('Apple', $userAgent->getDevice()->getFeature('brand_name'));
  124. }
  125. public function testUserAgentDefineStorage()
  126. {
  127. $config = array(
  128. 'storage' => array('adapter' => 'NonPersistent'),
  129. 'server' => $this->server,
  130. );
  131. $oUserAgent = new Zend_Http_UserAgent($config);
  132. $browser = $oUserAgent->getUserAgent();
  133. $this->assertType('Zend_Http_UserAgent_Storage_NonPersistent', $oUserAgent->getStorage($browser));
  134. }
  135. public function testUserAgentFeatureAdapter()
  136. {
  137. $config = $this->config;
  138. $config['mobile']['features']['path'] = dirname(__FILE__) . '/TestAsset/Device/Browser/Features/Adapter.php';
  139. $config['mobile']['features']['classname'] = 'Device_Browser_Features_Adapter';
  140. $config['user_agent'] = 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleW1ebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A102 Safari/419.3';
  141. $userAgent = new Zend_Http_UserAgent($config);
  142. $config = $userAgent->getConfig();
  143. $this->assertContains('Device/Browser/Features/Adapter.php', $config['mobile']['features']['path']);
  144. }
  145. public function testSetDefaultConfigAlone()
  146. {
  147. $config['server'] = $this->server;
  148. $userAgent = new Zend_Http_UserAgent($config);
  149. $config = $userAgent->getConfig();
  150. $this->assertEquals(Zend_Http_UserAgent::DEFAULT_IDENTIFICATION_SEQUENCE, $config['identification_sequence']);
  151. $this->assertEquals(Zend_Http_UserAgent::DEFAULT_PERSISTENT_STORAGE_ADAPTER, $config['storage']['adapter']);
  152. }
  153. public function testSetDefaultConfigStorage()
  154. {
  155. $config = array('identification_sequence' => 'Test');
  156. $oUserAgent = new Zend_Http_UserAgent($config);
  157. $test = $oUserAgent->getConfig();
  158. $this->assertEquals('Test', $test['identification_sequence']);
  159. $this->assertEquals(Zend_Http_UserAgent::DEFAULT_PERSISTENT_STORAGE_ADAPTER, $test['storage']['adapter']);
  160. }
  161. public function testSetDefaultConfigBoth()
  162. {
  163. $config = array(
  164. 'identification_sequence' => 'Test',
  165. 'storage' => array('adapter' => 'NonPersistent'),
  166. );
  167. $oUserAgent = new Zend_Http_UserAgent($config);
  168. $test = $oUserAgent->getConfig();
  169. $this->assertEquals('Test', $test['identification_sequence']);
  170. $this->assertEquals('NonPersistent', $test['storage']['adapter']);
  171. }
  172. public function testDeviceClassNameMatchesBrowserTypeIfUserAgentMatches()
  173. {
  174. if (!constant('TESTS_ZEND_HTTP_USERAGENT_WURFL_LIB_DIR')) {
  175. $this->markTestSkipped('Depends on WURFL support');
  176. }
  177. $this->config['browser_type'] = 'MoBiLe';
  178. $this->config['user_agent'] = 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleW1ebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A102 Safari/419.3';
  179. $userAgent = new Zend_Http_UserAgent($this->config);
  180. $className = get_class($userAgent->getDevice());
  181. $this->assertEquals('Zend_Http_UserAgent_Mobile', $className);
  182. }
  183. public function testDeviceClassNameMatchesDesktopTypeIfUserAgentDoesNotMatch()
  184. {
  185. $config = array(
  186. 'browser_type' => 'MoBiLe',
  187. );
  188. $userAgent = new Zend_Http_UserAgent($config);
  189. $className = get_class($userAgent->getDevice());
  190. $this->assertEquals('Zend_Http_UserAgent_Desktop', $className);
  191. }
  192. public function testUserAgentFromServerSuperglobalWhenNotProvided()
  193. {
  194. $_SERVER['HTTP_USER_AGENT'] = 'UserAgentTest2';
  195. $ua = new Zend_Http_UserAgent();
  196. $this->assertEquals('UserAgentTest2', $ua->getServerValue('http_user_agent'));
  197. $this->assertEquals('UserAgentTest2', $ua->getUserAgent());
  198. }
  199. public function testAllowsPassingUserAgentDirectly()
  200. {
  201. $this->config['user_agent'] = 'UserAgentTest2';
  202. $ua = new Zend_Http_UserAgent($this->config);
  203. $this->assertEquals('UserAgentTest2', $ua->getUserAgent());
  204. }
  205. public function testAllowsSettingUserAgentManually()
  206. {
  207. $ua = new Zend_Http_UserAgent();
  208. $ua->setUserAgent('userAgentTest');
  209. $this->assertEquals('userAgentTest', $ua->getServerValue('HTTP_USER_AGENT'));
  210. $this->assertEquals('userAgentTest', $ua->getUserAgent());
  211. }
  212. public function testUsesHttpAcceptConstantValueByDefault()
  213. {
  214. unset($this->server['http_accept']);
  215. $ua = new Zend_Http_UserAgent();
  216. $this->assertEquals(Zend_Http_UserAgent::DEFAULT_HTTP_ACCEPT, $ua->getHttpAccept());
  217. $this->assertEquals(Zend_Http_UserAgent::DEFAULT_HTTP_ACCEPT, $ua->getServerValue('HTTP_ACCEPT'));
  218. }
  219. public function testUsesServerHttpAcceptValueWhenPresent()
  220. {
  221. $_SERVER['HTTP_ACCEPT'] = 'HttpAcceptTest2';
  222. $ua = new Zend_Http_UserAgent();
  223. $this->assertEquals('HttpAcceptTest2', $ua->getHttpAccept());
  224. $this->assertEquals('HttpAcceptTest2', $ua->getServerValue('HTTP_ACCEPT'));
  225. }
  226. public function testAllowsPassingHttpAcceptValueViaConfiguration()
  227. {
  228. $this->config['http_accept'] = 'HttpAcceptTest';
  229. $ua = new Zend_Http_UserAgent($this->config);
  230. $this->assertEquals('HttpAcceptTest', $ua->getHttpAccept());
  231. $this->assertEquals('HttpAcceptTest', $ua->getServerValue('HTTP_ACCEPT'));
  232. }
  233. public function testAllowsSettingHttpAcceptManually()
  234. {
  235. $ua = new Zend_Http_UserAgent();
  236. $ua->setHttpAccept('httpAcceptTest');
  237. $this->assertEquals('httpAcceptTest', $ua->getHttpAccept());
  238. $this->assertEquals('httpAcceptTest', $ua->getServerValue('HTTP_ACCEPT'));
  239. }
  240. public function testCanSetConfigWithConfigObject()
  241. {
  242. $config = new Zend_Config($this->config);
  243. $ua = new Zend_Http_UserAgent($config);
  244. $test = $ua->getConfig();
  245. $this->assertEquals($config->storage->adapter, $test['storage']['adapter']);
  246. }
  247. public function testCanSetConfigWithTraversableObject()
  248. {
  249. $config = new ArrayObject($this->config);
  250. $ua = new Zend_Http_UserAgent($config);
  251. $test = $ua->getConfig();
  252. $this->assertEquals($config['storage'], $test['storage']);
  253. }
  254. public function invalidConfigs()
  255. {
  256. return array(
  257. array(true),
  258. array(1),
  259. array(1.0),
  260. array(new stdClass),
  261. );
  262. }
  263. /**
  264. * @dataProvider invalidConfigs
  265. */
  266. public function testSettingConfigWithInvalidTypeRaisesException($arg)
  267. {
  268. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'expected array');
  269. $ua = new Zend_Http_UserAgent($arg);
  270. }
  271. public function testAllowsSettingServerWithArrayObject()
  272. {
  273. $server = new ArrayObject($this->server);
  274. $ua = new Zend_Http_UserAgent(array('server' => $server));
  275. $this->assertEquals($server['os'], $ua->getServerValue('os'));
  276. }
  277. public function testAllowsSettingServerWithTraversableObject()
  278. {
  279. $server = new ArrayIterator($this->server);
  280. $ua = new Zend_Http_UserAgent(array('server' => $server));
  281. $this->assertEquals($this->server['os'], $ua->getServerValue('os'));
  282. }
  283. /**
  284. * @dataProvider invalidConfigs
  285. */
  286. public function testSettingServerWithInvalidTypeRaisesException($arg)
  287. {
  288. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'array or object implementing Traversable');
  289. $ua = new Zend_Http_UserAgent(array('server' => $arg));
  290. }
  291. public function testAllowsSettingPluginLoaderUsingClassname()
  292. {
  293. $ua = new Zend_Http_UserAgent();
  294. $ua->setPluginLoader('device', 'Zend_Http_TestAsset_TestPluginLoader');
  295. $loader = $ua->getPluginLoader('device');
  296. $this->assertType('Zend_Http_TestAsset_TestPluginLoader', $loader);
  297. }
  298. public function testSpecifyingInvalidPluginLoaderClassNameRaisesException()
  299. {
  300. $ua = new Zend_Http_UserAgent();
  301. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'extending Zend_Loader_PluginLoader');
  302. $ua->setPluginLoader('device', 'Zend_Http_TestAsset_InvalidPluginLoader');
  303. }
  304. public function invalidLoaders()
  305. {
  306. return array(
  307. array(true),
  308. array(1),
  309. array(1.0),
  310. array(array()),
  311. );
  312. }
  313. /**
  314. * @dataProvider invalidLoaders
  315. */
  316. public function testSpecifyingInvalidTypeToPluginLoaderRaisesException($arg)
  317. {
  318. $ua = new Zend_Http_UserAgent();
  319. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'class or object');
  320. $ua->setPluginLoader('device', $arg);
  321. }
  322. public function testSpecifyingNonPluginLoaderObjectRaisesException()
  323. {
  324. $ua = new Zend_Http_UserAgent();
  325. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'extending Zend_Loader_PluginLoader');
  326. $ua->setPluginLoader('device', $this);
  327. }
  328. public function testSpecifyingInvalidTypeWhenSettingPluginLoaderRaisesException()
  329. {
  330. $ua = new Zend_Http_UserAgent();
  331. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'plugin loader type');
  332. $ua->setPluginLoader('__bogus__', new Zend_Loader_PluginLoader());
  333. }
  334. public function testAllowsSpecifyingPluginLoadersViaConfiguration()
  335. {
  336. $this->config['plugin_loader'] = array(
  337. 'device' => 'Zend_Http_TestAsset_TestPluginLoader',
  338. 'storage' => 'Zend_Http_TestAsset_TestPluginLoader',
  339. );
  340. $ua = new Zend_Http_UserAgent($this->config);
  341. $deviceLoader = $ua->getPluginLoader('device');
  342. $this->assertType('Zend_Http_TestAsset_TestPluginLoader', $deviceLoader);
  343. $storageLoader = $ua->getPluginLoader('storage');
  344. $this->assertType('Zend_Http_TestAsset_TestPluginLoader', $storageLoader);
  345. $this->assertNotSame($deviceLoader, $storageLoader);
  346. }
  347. public function testAllowsSpecifyingCustomDeviceClassesViaConfiguration()
  348. {
  349. $this->config['desktop'] = array(
  350. 'device' => array(
  351. 'classname' => 'Zend_Http_TestAsset_DesktopDevice',
  352. ),
  353. );
  354. $this->config['user_agent'] = 'desktop';
  355. $ua = new Zend_Http_UserAgent($this->config);
  356. $device = $ua->getDevice();
  357. $this->assertType('Zend_Http_TestAsset_DesktopDevice', $device);
  358. }
  359. public function testAllowsSpecifyingCustomDeviceViaPrefixPath()
  360. {
  361. $this->config['desktop'] = array(
  362. 'device' => array(
  363. 'path' => dirname(__FILE__) . '/TestAsset/Device',
  364. 'prefix' => 'Zend_Http_TestAsset_Device',
  365. ),
  366. );
  367. $this->config['user_agent'] = 'desktop';
  368. $ua = new Zend_Http_UserAgent($this->config);
  369. $device = $ua->getDevice();
  370. $this->assertType('Zend_Http_TestAsset_Device_Desktop', $device);
  371. }
  372. public function testShouldRaiseExceptionOnInvalidDeviceClass()
  373. {
  374. $this->config['desktop'] = array(
  375. 'device' => array(
  376. 'classname' => 'Zend_Http_TestAsset_InvalidDevice',
  377. ),
  378. );
  379. $this->config['user_agent'] = 'desktop';
  380. $ua = new Zend_Http_UserAgent($this->config);
  381. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'Zend_Http_UserAgent_Device');
  382. $ua->getDevice();
  383. }
  384. public function testStorageContainsSerializedUserAgent()
  385. {
  386. $this->config['desktop'] = array(
  387. 'device' => array(
  388. 'classname' => 'Zend_Http_TestAsset_DesktopDevice',
  389. ),
  390. );
  391. $this->config['user_agent'] = 'desktop';
  392. $ua = new Zend_Http_UserAgent($this->config);
  393. // prime storage by retrieving device
  394. $device = $ua->getDevice();
  395. $storage = $ua->getStorage();
  396. $serialized = $storage->read();
  397. $test = unserialize($serialized);
  398. $this->assertEquals($ua->getBrowserType(), $test['browser_type']);
  399. $this->assertEquals($ua->getConfig(), $test['config']);
  400. $this->assertEquals('Zend_Http_TestAsset_DesktopDevice', $test['device_class']);
  401. $this->assertEquals($ua->getUserAgent(), $test['user_agent']);
  402. $this->assertEquals($ua->getHttpAccept(), $test['http_accept']);
  403. $test = unserialize($test['device']);
  404. $this->assertEquals($device->getAllFeatures(), $test['_aFeatures']);
  405. $this->assertEquals($device->getBrowser(), $test['_browser']);
  406. $this->assertEquals($device->getBrowserVersion(), $test['_browserVersion']);
  407. $this->assertEquals($device->getUserAgent(), $test['_userAgent']);
  408. $this->assertEquals($device->getImages(), $test['_images']);
  409. }
  410. public function testCanPopulateFromStorage()
  411. {
  412. $this->config['storage']['adapter'] = 'Zend_Http_TestAsset_PopulatedStorage';
  413. $this->config['user_agent'] = 'desktop';
  414. $ua = new Zend_Http_UserAgent($this->config);
  415. $storage = $ua->getStorage();
  416. $this->assertType('Zend_Http_TestAsset_PopulatedStorage', $storage);
  417. $device = $ua->getDevice();
  418. $this->assertType('Zend_Http_TestAsset_DesktopDevice', $device);
  419. }
  420. public function testCanClearStorage()
  421. {
  422. $this->config['desktop'] = array(
  423. 'device' => array(
  424. 'classname' => 'Zend_Http_TestAsset_DesktopDevice',
  425. ),
  426. );
  427. $this->config['user_agent'] = 'desktop';
  428. $ua = new Zend_Http_UserAgent($this->config);
  429. // Prime storage by retrieving device
  430. $device = $ua->getDevice();
  431. $storage = $ua->getStorage();
  432. $this->assertType('Zend_Http_UserAgent_Storage', $storage);
  433. $this->assertFalse($storage->isEmpty());
  434. $ua->clearStorage();
  435. $this->assertTrue($storage->isEmpty());
  436. }
  437. public function testServerIsImmutableOnceDeviceRetrieved()
  438. {
  439. $config = $this->config;
  440. $userAgent = new Zend_Http_UserAgent($config);
  441. $device = $userAgent->getDevice();
  442. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'immutable');
  443. $userAgent->setServerValue('HTTP_ACCEPT', 'application/json');
  444. }
  445. public function testBrowserTypeIsImmutableOnceDeviceRetrieved()
  446. {
  447. $config = $this->config;
  448. $userAgent = new Zend_Http_UserAgent($config);
  449. $device = $userAgent->getDevice();
  450. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'immutable');
  451. $userAgent->setBrowserType('mobile');
  452. }
  453. public function testHttpAcceptIsImmutableOnceDeviceRetrieved()
  454. {
  455. $config = $this->config;
  456. $userAgent = new Zend_Http_UserAgent($config);
  457. $device = $userAgent->getDevice();
  458. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'immutable');
  459. $userAgent->setHttpAccept('application/json');
  460. }
  461. public function testUserAgentIsImmutableOnceDeviceIsRetrieved()
  462. {
  463. $config = $this->config;
  464. $userAgent = new Zend_Http_UserAgent($config);
  465. $device = $userAgent->getDevice();
  466. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'immutable');
  467. $userAgent->setUserAgent('userAgentTest');
  468. }
  469. public function testStorageIsImmutableOnceDeviceIsRetrieved()
  470. {
  471. $config = $this->config;
  472. $userAgent = new Zend_Http_UserAgent($config);
  473. $device = $userAgent->getDevice();
  474. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'immutable');
  475. $userAgent->setStorage(new Zend_Http_UserAgent_Storage_NonPersistent());
  476. }
  477. public function testAllowsPassingStorageConfigurationOptions()
  478. {
  479. $config = $this->config;
  480. $config['storage']['adapter'] = 'Session';
  481. $config['storage']['options'] = array(
  482. 'browser_type' => 'foobar',
  483. 'member' => 'data',
  484. );
  485. $userAgent = new Zend_Http_UserAgent($config);
  486. $storage = $userAgent->getStorage();
  487. $this->assertEquals('.foobar', $storage->getNamespace());
  488. $this->assertEquals('data', $storage->getMember());
  489. }
  490. /**
  491. * @group ZF-10595
  492. */
  493. public function testAGroupDefinedAndSerialized()
  494. {
  495. $config = $this->config;
  496. $userAgent = new Zend_Http_UserAgent($config);
  497. $device = $userAgent->getDevice();
  498. $userAgent = unserialize(serialize($userAgent));
  499. $device = $userAgent->getDevice();
  500. $groups = $device->getAllGroups();
  501. }
  502. }