RedirectRouteTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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\Routing\Router;
  17. use Cake\Routing\Route\RedirectRoute;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * test case for RedirectRoute
  21. */
  22. class RedirectRouteTest extends TestCase
  23. {
  24. /**
  25. * setUp method
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. Router::reload();
  33. Router::connect('/:controller', ['action' => 'index']);
  34. Router::connect('/:controller/:action/*');
  35. }
  36. /**
  37. * test match
  38. *
  39. * @return void
  40. */
  41. public function testMatch()
  42. {
  43. $route = new RedirectRoute('/home', ['controller' => 'posts']);
  44. $this->assertFalse($route->match(['controller' => 'posts', 'action' => 'index']));
  45. }
  46. /**
  47. * test parse failure
  48. *
  49. * @return void
  50. */
  51. public function testParseMiss()
  52. {
  53. $route = new RedirectRoute('/home', ['controller' => 'posts']);
  54. $this->assertFalse($route->parse('/nope'));
  55. $this->assertFalse($route->parse('/homes'));
  56. }
  57. /**
  58. * test the parsing of routes.
  59. *
  60. * @return void
  61. */
  62. public function testParseSimple()
  63. {
  64. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  65. $this->expectExceptionMessage('http://localhost/posts');
  66. $this->expectExceptionCode(301);
  67. $route = new RedirectRoute('/home', ['controller' => 'posts']);
  68. $route->parse('/home');
  69. }
  70. /**
  71. * test the parsing of routes.
  72. *
  73. * @return void
  74. */
  75. public function testParseRedirectOption()
  76. {
  77. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  78. $this->expectExceptionMessage('http://localhost/posts');
  79. $this->expectExceptionCode(301);
  80. $route = new RedirectRoute('/home', ['redirect' => ['controller' => 'posts']]);
  81. $route->parse('/home');
  82. }
  83. /**
  84. * test the parsing of routes.
  85. *
  86. * @return void
  87. */
  88. public function testParseArray()
  89. {
  90. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  91. $this->expectExceptionMessage('http://localhost/posts');
  92. $this->expectExceptionCode(301);
  93. $route = new RedirectRoute('/home', ['controller' => 'posts', 'action' => 'index']);
  94. $route->parse('/home');
  95. }
  96. /**
  97. * test redirecting to an external url
  98. *
  99. * @return void
  100. */
  101. public function testParseAbsolute()
  102. {
  103. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  104. $this->expectExceptionMessage('http://google.com');
  105. $this->expectExceptionCode(301);
  106. $route = new RedirectRoute('/google', 'http://google.com');
  107. $route->parse('/google');
  108. }
  109. /**
  110. * test redirecting with a status code
  111. *
  112. * @return void
  113. */
  114. public function testParseStatusCode()
  115. {
  116. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  117. $this->expectExceptionMessage('http://localhost/posts/view');
  118. $this->expectExceptionCode(302);
  119. $route = new RedirectRoute('/posts/*', ['controller' => 'posts', 'action' => 'view'], ['status' => 302]);
  120. $route->parse('/posts/2');
  121. }
  122. /**
  123. * test redirecting with the persist option
  124. *
  125. * @return void
  126. */
  127. public function testParsePersist()
  128. {
  129. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  130. $this->expectExceptionMessage('http://localhost/posts/view/2');
  131. $this->expectExceptionCode(301);
  132. $route = new RedirectRoute('/posts/*', ['controller' => 'posts', 'action' => 'view'], ['persist' => true]);
  133. $route->parse('/posts/2');
  134. }
  135. /**
  136. * test redirecting with persist and string target URLs
  137. *
  138. * @return void
  139. */
  140. public function testParsePersistStringUrl()
  141. {
  142. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  143. $this->expectExceptionMessage('http://localhost/test');
  144. $this->expectExceptionCode(301);
  145. $route = new RedirectRoute('/posts/*', '/test', ['persist' => true]);
  146. $route->parse('/posts/2');
  147. }
  148. /**
  149. * test redirecting with persist and passed args
  150. *
  151. * @return void
  152. */
  153. public function testParsePersistPassedArgs()
  154. {
  155. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  156. $this->expectExceptionMessage('http://localhost/tags/add/passme');
  157. $this->expectExceptionCode(301);
  158. $route = new RedirectRoute('/my_controllers/:action/*', ['controller' => 'tags', 'action' => 'add'], ['persist' => true]);
  159. $route->parse('/my_controllers/do_something/passme');
  160. }
  161. /**
  162. * test redirecting without persist and passed args
  163. *
  164. * @return void
  165. */
  166. public function testParseNoPersistPassedArgs()
  167. {
  168. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  169. $this->expectExceptionMessage('http://localhost/tags/add');
  170. $this->expectExceptionCode(301);
  171. $route = new RedirectRoute('/my_controllers/:action/*', ['controller' => 'tags', 'action' => 'add']);
  172. $route->parse('/my_controllers/do_something/passme');
  173. }
  174. /**
  175. * test redirecting with patterns
  176. *
  177. * @return void
  178. */
  179. public function testParsePersistPatterns()
  180. {
  181. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  182. $this->expectExceptionMessage('http://localhost/tags/add?lang=nl');
  183. $this->expectExceptionCode(301);
  184. $route = new RedirectRoute('/:lang/my_controllers', ['controller' => 'tags', 'action' => 'add'], ['lang' => '(nl|en)', 'persist' => ['lang']]);
  185. $route->parse('/nl/my_controllers/');
  186. }
  187. /**
  188. * test redirecting with patterns and a routed target
  189. *
  190. * @return void
  191. */
  192. public function testParsePersistMatchesAnotherRoute()
  193. {
  194. $this->expectException(\Cake\Routing\Exception\RedirectException::class);
  195. $this->expectExceptionMessage('http://localhost/nl/preferred_controllers');
  196. $this->expectExceptionCode(301);
  197. Router::connect('/:lang/preferred_controllers', ['controller' => 'tags', 'action' => 'add'], ['lang' => '(nl|en)', 'persist' => ['lang']]);
  198. $route = new RedirectRoute('/:lang/my_controllers', ['controller' => 'tags', 'action' => 'add'], ['lang' => '(nl|en)', 'persist' => ['lang']]);
  199. $route->parse('/nl/my_controllers/');
  200. }
  201. }