| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 2.0.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Routing\Route;
- use Cake\Http\ServerRequest;
- use Cake\Routing\Router;
- use Cake\Routing\Route\RedirectRoute;
- use Cake\TestSuite\TestCase;
- /**
- * test case for RedirectRoute
- */
- class RedirectRouteTest extends TestCase
- {
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- Router::reload();
- Router::connect('/:controller', ['action' => 'index']);
- Router::connect('/:controller/:action/*');
- }
- /**
- * test match
- *
- * @return void
- */
- public function testMatch()
- {
- $route = new RedirectRoute('/home', ['controller' => 'posts']);
- $this->assertFalse($route->match(['controller' => 'posts', 'action' => 'index']));
- }
- /**
- * test parse failure
- *
- * @return void
- */
- public function testParseMiss()
- {
- $route = new RedirectRoute('/home', ['controller' => 'posts']);
- $this->assertFalse($route->parse('/nope'));
- $this->assertFalse($route->parse('/homes'));
- }
- /**
- * test the parsing of routes.
- *
- * @return void
- */
- public function testParseSimple()
- {
- $this->expectException(\Cake\Routing\Exception\RedirectException::class);
- $this->expectExceptionMessage('http://localhost/posts');
- $this->expectExceptionCode(301);
- $route = new RedirectRoute('/home', ['controller' => 'posts']);
- $route->parse('/home');
- }
- /**
- * test the parsing of routes.
- *
- * @return void
- */
- public function testParseRedirectOption()
- {
- $this->expectException(\Cake\Routing\Exception\RedirectException::class);
- $this->expectExceptionMessage('http://localhost/posts');
- $this->expectExceptionCode(301);
- $route = new RedirectRoute('/home', ['redirect' => ['controller' => 'posts']]);
- $route->parse('/home');
- }
- /**
- * test the parsing of routes.
- *
- * @return void
- */
- public function testParseArray()
- {
- $this->expectException(\Cake\Routing\Exception\RedirectException::class);
- $this->expectExceptionMessage('http://localhost/posts');
- $this->expectExceptionCode(301);
- $route = new RedirectRoute('/home', ['controller' => 'posts', 'action' => 'index']);
- $route->parse('/home');
- }
- /**
- * test redirecting to an external url
- *
- * @return void
- */
- public function testParseAbsolute()
- {
- $this->expectException(\Cake\Routing\Exception\RedirectException::class);
- $this->expectExceptionMessage('http://google.com');
- $this->expectExceptionCode(301);
- $route = new RedirectRoute('/google', 'http://google.com');
- $route->parse('/google');
- }
- /**
- * test redirecting with a status code
- *
- * @return void
- */
- public function testParseStatusCode()
- {
- $this->expectException(\Cake\Routing\Exception\RedirectException::class);
- $this->expectExceptionMessage('http://localhost/posts/view');
- $this->expectExceptionCode(302);
- $route = new RedirectRoute('/posts/*', ['controller' => 'posts', 'action' => 'view'], ['status' => 302]);
- $route->parse('/posts/2');
- }
- /**
- * test redirecting with the persist option
- *
- * @return void
- */
- public function testParsePersist()
- {
- $this->expectException(\Cake\Routing\Exception\RedirectException::class);
- $this->expectExceptionMessage('http://localhost/posts/view/2');
- $this->expectExceptionCode(301);
- $route = new RedirectRoute('/posts/*', ['controller' => 'posts', 'action' => 'view'], ['persist' => true]);
- $route->parse('/posts/2');
- }
- /**
- * test redirecting with persist and a base directory
- *
- * @return void
- */
- public function testParsePersistBaseDirectory()
- {
- $request = new ServerRequest([
- 'base' => '/basedir',
- 'url' => '/posts/2',
- ]);
- Router::pushRequest($request);
- $this->expectException(\Cake\Routing\Exception\RedirectException::class);
- $this->expectExceptionMessage('http://localhost/basedir/posts/view/2');
- $this->expectExceptionCode(301);
- $route = new RedirectRoute('/posts/*', ['controller' => 'posts', 'action' => 'view'], ['persist' => true]);
- $route->parse('/posts/2');
- }
- /**
- * test redirecting with persist and string target URLs
- *
- * @return void
- */
- public function testParsePersistStringUrl()
- {
- $this->expectException(\Cake\Routing\Exception\RedirectException::class);
- $this->expectExceptionMessage('http://localhost/test');
- $this->expectExceptionCode(301);
- $route = new RedirectRoute('/posts/*', '/test', ['persist' => true]);
- $route->parse('/posts/2');
- }
- /**
- * test redirecting with persist and passed args
- *
- * @return void
- */
- public function testParsePersistPassedArgs()
- {
- $this->expectException(\Cake\Routing\Exception\RedirectException::class);
- $this->expectExceptionMessage('http://localhost/tags/add/passme');
- $this->expectExceptionCode(301);
- $route = new RedirectRoute('/my_controllers/:action/*', ['controller' => 'tags', 'action' => 'add'], ['persist' => true]);
- $route->parse('/my_controllers/do_something/passme');
- }
- /**
- * test redirecting without persist and passed args
- *
- * @return void
- */
- public function testParseNoPersistPassedArgs()
- {
- $this->expectException(\Cake\Routing\Exception\RedirectException::class);
- $this->expectExceptionMessage('http://localhost/tags/add');
- $this->expectExceptionCode(301);
- $route = new RedirectRoute('/my_controllers/:action/*', ['controller' => 'tags', 'action' => 'add']);
- $route->parse('/my_controllers/do_something/passme');
- }
- /**
- * test redirecting with patterns
- *
- * @return void
- */
- public function testParsePersistPatterns()
- {
- $this->expectException(\Cake\Routing\Exception\RedirectException::class);
- $this->expectExceptionMessage('http://localhost/tags/add?lang=nl');
- $this->expectExceptionCode(301);
- $route = new RedirectRoute('/:lang/my_controllers', ['controller' => 'tags', 'action' => 'add'], ['lang' => '(nl|en)', 'persist' => ['lang']]);
- $route->parse('/nl/my_controllers/');
- }
- /**
- * test redirecting with patterns and a routed target
- *
- * @return void
- */
- public function testParsePersistMatchesAnotherRoute()
- {
- $this->expectException(\Cake\Routing\Exception\RedirectException::class);
- $this->expectExceptionMessage('http://localhost/nl/preferred_controllers');
- $this->expectExceptionCode(301);
- Router::connect('/:lang/preferred_controllers', ['controller' => 'tags', 'action' => 'add'], ['lang' => '(nl|en)', 'persist' => ['lang']]);
- $route = new RedirectRoute('/:lang/my_controllers', ['controller' => 'tags', 'action' => 'add'], ['lang' => '(nl|en)', 'persist' => ['lang']]);
- $route->parse('/nl/my_controllers/');
- }
- /**
- * Test setting HTTP status
- *
- * @return void
- */
- public function testSetStatus()
- {
- $route = new RedirectRoute('/home', ['controller' => 'posts']);
- $result = $route->setStatus(302);
- $this->assertSame($result, $route);
- $this->assertEquals(302, $route->options['status']);
- }
- }
|