RedirectRouteTest.php 7.7 KB

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