RedirectRouteTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Routing\Route;
  16. use Cake\Http\ServerRequest;
  17. use Cake\Routing\Router;
  18. use Cake\Routing\Route\RedirectRoute;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * test case for RedirectRoute
  22. */
  23. class RedirectRouteTest extends TestCase
  24. {
  25. /**
  26. * setUp method
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. Router::reload();
  34. Router::connect('/:controller', ['action' => 'index']);
  35. Router::connect('/:controller/:action/*');
  36. }
  37. /**
  38. * test match
  39. *
  40. * @return void
  41. */
  42. public function testMatch()
  43. {
  44. $route = new RedirectRoute('/home', ['controller' => 'posts']);
  45. $this->assertFalse($route->match(['controller' => 'posts', 'action' => 'index']));
  46. }
  47. /**
  48. * test parse failure
  49. *
  50. * @return void
  51. */
  52. public function testParseMiss()
  53. {
  54. $route = new RedirectRoute('/home', ['controller' => 'posts']);
  55. $this->assertFalse($route->parse('/nope'));
  56. $this->assertFalse($route->parse('/homes'));
  57. }
  58. /**
  59. * test the parsing of routes.
  60. *
  61. * @return void
  62. */
  63. public function testParseSimple()
  64. {
  65. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  66. $this->expectExceptionMessage('http://localhost/posts');
  67. $this->expectExceptionCode(301);
  68. $route = new RedirectRoute('/home', ['controller' => 'posts']);
  69. $route->parse('/home');
  70. }
  71. /**
  72. * test the parsing of routes.
  73. *
  74. * @return void
  75. */
  76. public function testParseRedirectOption()
  77. {
  78. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  79. $this->expectExceptionMessage('http://localhost/posts');
  80. $this->expectExceptionCode(301);
  81. $route = new RedirectRoute('/home', ['redirect' => ['controller' => 'posts']]);
  82. $route->parse('/home');
  83. }
  84. /**
  85. * test the parsing of routes.
  86. *
  87. * @return void
  88. */
  89. public function testParseArray()
  90. {
  91. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  92. $this->expectExceptionMessage('http://localhost/posts');
  93. $this->expectExceptionCode(301);
  94. $route = new RedirectRoute('/home', ['controller' => 'posts', 'action' => 'index']);
  95. $route->parse('/home');
  96. }
  97. /**
  98. * test redirecting to an external url
  99. *
  100. * @return void
  101. */
  102. public function testParseAbsolute()
  103. {
  104. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  105. $this->expectExceptionMessage('http://google.com');
  106. $this->expectExceptionCode(301);
  107. $route = new RedirectRoute('/google', 'http://google.com');
  108. $route->parse('/google');
  109. }
  110. /**
  111. * test redirecting with a status code
  112. *
  113. * @return void
  114. */
  115. public function testParseStatusCode()
  116. {
  117. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  118. $this->expectExceptionMessage('http://localhost/posts/view');
  119. $this->expectExceptionCode(302);
  120. $route = new RedirectRoute('/posts/*', ['controller' => 'posts', 'action' => 'view'], ['status' => 302]);
  121. $route->parse('/posts/2');
  122. }
  123. /**
  124. * test redirecting with the persist option
  125. *
  126. * @return void
  127. */
  128. public function testParsePersist()
  129. {
  130. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  131. $this->expectExceptionMessage('http://localhost/posts/view/2');
  132. $this->expectExceptionCode(301);
  133. $route = new RedirectRoute('/posts/*', ['controller' => 'posts', 'action' => 'view'], ['persist' => true]);
  134. $route->parse('/posts/2');
  135. }
  136. /**
  137. * test redirecting with persist and a base directory
  138. *
  139. * @return void
  140. */
  141. public function testParsePersistBaseDirectory()
  142. {
  143. $request = new ServerRequest([
  144. 'base' => '/basedir',
  145. 'url' => '/posts/2'
  146. ]);
  147. Router::pushRequest($request);
  148. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  149. $this->expectExceptionMessage('http://localhost/basedir/posts/view/2');
  150. $this->expectExceptionCode(301);
  151. $route = new RedirectRoute('/posts/*', ['controller' => 'posts', 'action' => 'view'], ['persist' => true]);
  152. $route->parse('/posts/2');
  153. }
  154. /**
  155. * test redirecting with persist and string target URLs
  156. *
  157. * @return void
  158. */
  159. public function testParsePersistStringUrl()
  160. {
  161. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  162. $this->expectExceptionMessage('http://localhost/test');
  163. $this->expectExceptionCode(301);
  164. $route = new RedirectRoute('/posts/*', '/test', ['persist' => true]);
  165. $route->parse('/posts/2');
  166. }
  167. /**
  168. * test redirecting with persist and passed args
  169. *
  170. * @return void
  171. */
  172. public function testParsePersistPassedArgs()
  173. {
  174. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  175. $this->expectExceptionMessage('http://localhost/tags/add/passme');
  176. $this->expectExceptionCode(301);
  177. $route = new RedirectRoute('/my_controllers/:action/*', ['controller' => 'tags', 'action' => 'add'], ['persist' => true]);
  178. $route->parse('/my_controllers/do_something/passme');
  179. }
  180. /**
  181. * test redirecting without persist and passed args
  182. *
  183. * @return void
  184. */
  185. public function testParseNoPersistPassedArgs()
  186. {
  187. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  188. $this->expectExceptionMessage('http://localhost/tags/add');
  189. $this->expectExceptionCode(301);
  190. $route = new RedirectRoute('/my_controllers/:action/*', ['controller' => 'tags', 'action' => 'add']);
  191. $route->parse('/my_controllers/do_something/passme');
  192. }
  193. /**
  194. * test redirecting with patterns
  195. *
  196. * @return void
  197. */
  198. public function testParsePersistPatterns()
  199. {
  200. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  201. $this->expectExceptionMessage('http://localhost/tags/add?lang=nl');
  202. $this->expectExceptionCode(301);
  203. $route = new RedirectRoute('/:lang/my_controllers', ['controller' => 'tags', 'action' => 'add'], ['lang' => '(nl|en)', 'persist' => ['lang']]);
  204. $route->parse('/nl/my_controllers/');
  205. }
  206. /**
  207. * test redirecting with patterns and a routed target
  208. *
  209. * @return void
  210. */
  211. public function testParsePersistMatchesAnotherRoute()
  212. {
  213. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  214. $this->expectExceptionMessage('http://localhost/nl/preferred_controllers');
  215. $this->expectExceptionCode(301);
  216. Router::connect('/:lang/preferred_controllers', ['controller' => 'tags', 'action' => 'add'], ['lang' => '(nl|en)', 'persist' => ['lang']]);
  217. $route = new RedirectRoute('/:lang/my_controllers', ['controller' => 'tags', 'action' => 'add'], ['lang' => '(nl|en)', 'persist' => ['lang']]);
  218. $route->parse('/nl/my_controllers/');
  219. }
  220. }