MobileComponentTest.php 7.7 KB

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