MobileComponentTest.php 7.8 KB

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