MobileComponentTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. App::uses('MobileComponent', 'Tools.Controller/Component');
  3. App::uses('CakeSession', 'Model/Datasource');
  4. App::uses('Component', 'Controller');
  5. App::uses('AppController', 'Controller');
  6. /**
  7. * Test MobileComponent
  8. */
  9. class MobileComponentTest extends CakeTestCase {
  10. public $fixtures = array('core.cake_session');
  11. /**
  12. * SetUp method
  13. *
  14. * @return void
  15. */
  16. public function setUp() {
  17. parent::setUp();
  18. $this->Controller = new MobileComponentTestController(new CakeRequest(null, false), new CakeResponse());
  19. $this->Controller->constructClasses();
  20. $this->Controller->Mobile->Controller = $this->Controller;
  21. CakeSession::write('User', '');
  22. CakeSession::delete('User');
  23. CakeSession::write('Session', '');
  24. CakeSession::delete('Session');
  25. Configure::delete('User');
  26. }
  27. /**
  28. * Tear-down method. Resets environment state.
  29. *
  30. * @return void
  31. */
  32. public function tearDown() {
  33. parent::tearDown();
  34. unset($this->Controller->Mobile);
  35. unset($this->Controller);
  36. }
  37. public function testDetect() {
  38. $is = $this->Controller->Mobile->detect();
  39. $this->assertFalse($is);
  40. }
  41. public function testMobileNotMobile() {
  42. $this->Controller->Mobile->initialize($this->Controller);
  43. $this->Controller->Mobile->startup($this->Controller);
  44. $this->assertFalse($this->Controller->Mobile->isMobile);
  45. $session = CakeSession::read('User');
  46. $this->assertSame(array('mobile' => 0), $session);
  47. }
  48. public function testMobileForceActivated() {
  49. $this->Controller->request->query['mobile'] = 1;
  50. $this->Controller->Mobile->initialize($this->Controller);
  51. $this->Controller->Mobile->startup($this->Controller);
  52. $session = CakeSession::read('User');
  53. $this->assertSame(array('nomobile' => 0, 'mobile' => 0), $session);
  54. $this->Controller->Mobile->setMobile();
  55. $configure = Configure::read('User');
  56. $this->assertSame(array('isMobile' => 0, 'setMobile' => 1), $configure);
  57. $this->assertEquals(array('desktopUrl' => '/?mobile=0'), $this->Controller->viewVars);
  58. }
  59. public function testMobileForceDeactivated() {
  60. $this->Controller->request->query['mobile'] = 0;
  61. $this->Controller->Mobile->initialize($this->Controller);
  62. $this->Controller->Mobile->startup($this->Controller);
  63. $session = CakeSession::read('User');
  64. $this->assertSame(array('nomobile' => 1, 'mobile' => 0), $session);
  65. $this->Controller->Mobile->setMobile();
  66. $configure = Configure::read('User');
  67. $this->assertSame(array('isMobile' => 0, 'setMobile' => 0), $configure);
  68. $this->assertEquals(array('mobileUrl' => '/?mobile=1'), $this->Controller->viewVars);
  69. }
  70. public function testMobileFakeMobile() {
  71. $_SERVER['HTTP_USER_AGENT'] = 'Some Android device';
  72. $this->Controller->Mobile->initialize($this->Controller);
  73. $this->Controller->Mobile->startup($this->Controller);
  74. $session = CakeSession::read('User');
  75. $this->assertSame(array('mobile' => 1), $session);
  76. $this->assertTrue($this->Controller->Mobile->isMobile);
  77. $this->Controller->Mobile->setMobile();
  78. $configure = Configure::read('User');
  79. $this->assertSame(array('isMobile' => 1, 'setMobile' => 1), $configure);
  80. }
  81. public function testMobileFakeMobileForceDeactivated() {
  82. $this->Controller->request->query['mobile'] = 0;
  83. $_SERVER['HTTP_USER_AGENT'] = 'Some Android device';
  84. $this->Controller->Mobile->initialize($this->Controller);
  85. $this->Controller->Mobile->startup($this->Controller);
  86. $session = CakeSession::read('User');
  87. $this->assertSame(array('nomobile' => 1, 'mobile' => 1), $session);
  88. $this->assertTrue($this->Controller->Mobile->isMobile);
  89. $this->Controller->Mobile->setMobile();
  90. $configure = Configure::read('User');
  91. $this->assertSame(array('isMobile' => 1, 'setMobile' => 0), $configure);
  92. }
  93. public function testMobileFakeMobileAuto() {
  94. $this->Controller->Mobile->settings['auto'] = true;
  95. $_SERVER['HTTP_USER_AGENT'] = 'Some Android device';
  96. $this->Controller->Mobile->initialize($this->Controller);
  97. $this->Controller->Mobile->startup($this->Controller);
  98. $session = CakeSession::read('User');
  99. $this->assertSame(array('mobile' => 1), $session);
  100. $this->assertTrue($this->Controller->Mobile->isMobile);
  101. $configure = Configure::read('User');
  102. $this->assertSame(array('isMobile' => 1, 'setMobile' => 1), $configure);
  103. $this->assertTrue($this->Controller->Mobile->setMobile);
  104. }
  105. public function testMobileVendorEngineCake() {
  106. $this->Controller->Mobile->settings['engine'] = 'cake';
  107. $_SERVER['HTTP_USER_AGENT'] = 'Some Android device';
  108. $this->Controller->Mobile->initialize($this->Controller);
  109. $this->Controller->Mobile->startup($this->Controller);
  110. $session = CakeSession::read('User');
  111. $this->assertSame(array('mobile' => 1), $session);
  112. $this->assertTrue($this->Controller->Mobile->isMobile);
  113. }
  114. public function testMobileVendorEngineTools() {
  115. $this->Controller->Mobile->settings['engine'] = 'tools';
  116. $_SERVER['HTTP_USER_AGENT'] = 'Some Android device';
  117. $this->Controller->Mobile->initialize($this->Controller);
  118. $this->Controller->Mobile->startup($this->Controller);
  119. $session = CakeSession::read('User');
  120. $this->assertSame(array('mobile' => 1), $session);
  121. $this->assertTrue($this->Controller->Mobile->isMobile);
  122. }
  123. public function testMobileCustomMobile() {
  124. $this->Controller->Mobile->settings['mobile'] = array();
  125. $_SERVER['HTTP_USER_AGENT'] = 'Some Ipad device';
  126. $this->Controller->Mobile->initialize($this->Controller);
  127. $this->Controller->Mobile->startup($this->Controller);
  128. $session = CakeSession::read('User');
  129. $this->assertSame(array('mobile' => 0), $session);
  130. }
  131. public function testMobileCustomMobileMobile() {
  132. $this->Controller->Mobile->settings['mobile'] = array('mobile');
  133. $_SERVER['HTTP_USER_AGENT'] = 'Some Android device';
  134. $this->Controller->Mobile->initialize($this->Controller);
  135. $this->Controller->Mobile->startup($this->Controller);
  136. $session = CakeSession::read('User');
  137. $this->assertSame(array('mobile' => 1), $session);
  138. }
  139. public function testMobileCustomMobileTablet() {
  140. $this->Controller->Mobile->settings['mobile'] = array('tablet');
  141. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A403 Safari/8536.25';
  142. $this->Controller->Mobile->initialize($this->Controller);
  143. $this->Controller->Mobile->startup($this->Controller);
  144. $session = CakeSession::read('User');
  145. $this->assertSame(array('mobile' => 1), $session);
  146. }
  147. public function testMobileCustomMobileTablet2() {
  148. $this->Controller->Mobile->settings['mobile'] = array('mobile');
  149. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A403 Safari/8536.25';
  150. $this->Controller->Mobile->initialize($this->Controller);
  151. $this->Controller->Mobile->startup($this->Controller);
  152. $session = CakeSession::read('User');
  153. $this->assertSame(array('mobile' => 0), $session);
  154. }
  155. public function testMobileEngineClosure() {
  156. $closure = function() {
  157. return $_SERVER['HTTP_USER_AGENT'] === 'Foo';
  158. };
  159. $this->Controller->Mobile->settings['engine'] = $closure;
  160. $_SERVER['HTTP_USER_AGENT'] = 'Foo';
  161. $this->Controller->Mobile->initialize($this->Controller);
  162. $this->Controller->Mobile->startup($this->Controller);
  163. $session = CakeSession::read('User');
  164. $this->assertSame(array('mobile' => 1), $session);
  165. }
  166. }
  167. class MobileComponentTestController extends AppController {
  168. /**
  169. * Components property
  170. *
  171. * @var array
  172. */
  173. public $components = array('Tools.Mobile');
  174. /**
  175. * Failed property
  176. *
  177. * @var boolean
  178. */
  179. public $failed = false;
  180. /**
  181. * Used for keeping track of headers in test
  182. *
  183. * @var array
  184. */
  185. public $testHeaders = array();
  186. /**
  187. * Fail method
  188. *
  189. * @return void
  190. */
  191. public function fail() {
  192. $this->failed = true;
  193. }
  194. /**
  195. * Redirect method
  196. *
  197. * @param mixed $option
  198. * @param mixed $code
  199. * @param mixed $exit
  200. * @return void
  201. */
  202. public function redirect($url, $status = null, $exit = true) {
  203. return $status;
  204. }
  205. }