RedirectRouteTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Routing\Route;
  16. use Cake\Core\App;
  17. use Cake\Core\Configure;
  18. use Cake\Network\Response;
  19. use Cake\Routing\Router;
  20. use Cake\Routing\Route\RedirectRoute;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * test case for RedirectRoute
  24. *
  25. */
  26. class RedirectRouteTest extends TestCase {
  27. /**
  28. * setUp method
  29. *
  30. * @return void
  31. */
  32. public function setUp() {
  33. parent::setUp();
  34. Configure::write('Routing', array('admin' => null, 'prefixes' => array()));
  35. Router::reload();
  36. }
  37. /**
  38. * test the parsing of routes.
  39. *
  40. * @return void
  41. */
  42. public function testParsing() {
  43. Router::connect('/:controller', array('action' => 'index'));
  44. Router::connect('/:controller/:action/*');
  45. $route = new RedirectRoute('/home', array('controller' => 'posts'));
  46. $route->response = $this->getMock('Cake\Network\Response', array('_sendHeader', 'stop'));
  47. $result = $route->parse('/home');
  48. $header = $route->response->header();
  49. $this->assertEquals(Router::url('/posts', true), $header['Location']);
  50. $route = new RedirectRoute('/home', array('controller' => 'posts', 'action' => 'index'));
  51. $route->response = $this->getMock('Cake\Network\Response', array('_sendHeader', 'stop'));
  52. $result = $route->parse('/home');
  53. $header = $route->response->header();
  54. $this->assertEquals(Router::url('/posts', true), $header['Location']);
  55. $this->assertEquals(301, $route->response->statusCode());
  56. $route = new RedirectRoute('/google', 'http://google.com');
  57. $route->response = $this->getMock('Cake\Network\Response', array('_sendHeader', 'stop'));
  58. $result = $route->parse('/google');
  59. $header = $route->response->header();
  60. $this->assertEquals('http://google.com', $header['Location']);
  61. $route = new RedirectRoute('/posts/*', array('controller' => 'posts', 'action' => 'view'), array('status' => 302));
  62. $route->response = $this->getMock('Cake\Network\Response', array('_sendHeader', 'stop'));
  63. $result = $route->parse('/posts/2');
  64. $header = $route->response->header();
  65. $this->assertEquals(Router::url('/posts/view', true), $header['Location']);
  66. $this->assertEquals(302, $route->response->statusCode());
  67. $route = new RedirectRoute('/posts/*', array('controller' => 'posts', 'action' => 'view'), array('persist' => true));
  68. $route->response = $this->getMock('Cake\Network\Response', array('_sendHeader', 'stop'));
  69. $result = $route->parse('/posts/2');
  70. $header = $route->response->header();
  71. $this->assertEquals(Router::url('/posts/view/2', true), $header['Location']);
  72. $route = new RedirectRoute('/posts/*', '/test', array('persist' => true));
  73. $route->response = $this->getMock('Cake\Network\Response', array('_sendHeader', 'stop'));
  74. $result = $route->parse('/posts/2');
  75. $header = $route->response->header();
  76. $this->assertEquals(Router::url('/test', true), $header['Location']);
  77. $route = new RedirectRoute('/my_controllers/:action/*', array('controller' => 'tags', 'action' => 'add'), array('persist' => true));
  78. $route->response = $this->getMock('Cake\Network\Response', array('_sendHeader', 'stop'));
  79. $result = $route->parse('/my_controllers/do_something/passme');
  80. $header = $route->response->header();
  81. $this->assertEquals(Router::url('/tags/add/passme', true), $header['Location']);
  82. $route = new RedirectRoute('/my_controllers/:action/*', array('controller' => 'tags', 'action' => 'add'));
  83. $route->response = $this->getMock('Cake\Network\Response', array('_sendHeader', 'stop'));
  84. $result = $route->parse('/my_controllers/do_something/passme');
  85. $header = $route->response->header();
  86. $this->assertEquals(Router::url('/tags/add', true), $header['Location']);
  87. $route = new RedirectRoute('/:lang/my_controllers', array('controller' => 'tags', 'action' => 'add'), array('lang' => '(nl|en)', 'persist' => array('lang')));
  88. $route->response = $this->getMock('Cake\Network\Response', array('_sendHeader', 'stop'));
  89. $result = $route->parse('/nl/my_controllers/');
  90. $header = $route->response->header();
  91. $this->assertEquals(Router::url('/tags/add?lang=nl', true), $header['Location']);
  92. Router::reload(); // reset default routes
  93. Router::connect('/:lang/preferred_controllers', array('controller' => 'tags', 'action' => 'add'), array('lang' => '(nl|en)', 'persist' => array('lang')));
  94. $route = new RedirectRoute('/:lang/my_controllers', array('controller' => 'tags', 'action' => 'add'), array('lang' => '(nl|en)', 'persist' => array('lang')));
  95. $route->response = $this->getMock('Cake\Network\Response', array('_sendHeader', 'stop'));
  96. $result = $route->parse('/nl/my_controllers/');
  97. $header = $route->response->header();
  98. $this->assertEquals(Router::url('/nl/preferred_controllers', true), $header['Location']);
  99. }
  100. }