| 16591 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CakeRequest Test case file.
|
|
|
4 |
*
|
|
|
5 |
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
7 |
*
|
|
|
8 |
* Licensed under The MIT License
|
|
|
9 |
* For full copyright and license information, please see the LICENSE.txt
|
|
|
10 |
* Redistributions of files must retain the above copyright notice.
|
|
|
11 |
*
|
|
|
12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
13 |
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
14 |
* @package Cake.Test.Case.Routing.Route
|
|
|
15 |
* @since CakePHP(tm) v 2.0
|
|
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
App::uses('RedirectRoute', 'Routing/Route');
|
|
|
20 |
App::uses('CakeResponse', 'Network');
|
|
|
21 |
App::uses('Router', 'Routing');
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* test case for RedirectRoute
|
|
|
25 |
*
|
|
|
26 |
* @package Cake.Test.Case.Routing.Route
|
|
|
27 |
*/
|
|
|
28 |
class RedirectRouteTest extends CakeTestCase {
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* setUp method
|
|
|
32 |
*
|
|
|
33 |
* @return void
|
|
|
34 |
*/
|
|
|
35 |
public function setUp() {
|
|
|
36 |
parent::setUp();
|
|
|
37 |
Configure::write('Routing', array('admin' => null, 'prefixes' => array()));
|
|
|
38 |
Router::reload();
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* test the parsing of routes.
|
|
|
43 |
*
|
|
|
44 |
* @return void
|
|
|
45 |
*/
|
|
|
46 |
public function testParsing() {
|
|
|
47 |
$route = new RedirectRoute('/home', array('controller' => 'posts'));
|
|
|
48 |
$route->stop = false;
|
|
|
49 |
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
|
|
50 |
$result = $route->parse('/home');
|
|
|
51 |
$header = $route->response->header();
|
|
|
52 |
$this->assertEquals(Router::url('/posts', true), $header['Location']);
|
|
|
53 |
|
|
|
54 |
$route = new RedirectRoute('/home', array('controller' => 'posts', 'action' => 'index'));
|
|
|
55 |
$route->stop = false;
|
|
|
56 |
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
|
|
57 |
$result = $route->parse('/home');
|
|
|
58 |
$header = $route->response->header();
|
|
|
59 |
$this->assertEquals(Router::url('/posts', true), $header['Location']);
|
|
|
60 |
$this->assertEquals(301, $route->response->statusCode());
|
|
|
61 |
|
|
|
62 |
$route = new RedirectRoute('/google', 'http://google.com');
|
|
|
63 |
$route->stop = false;
|
|
|
64 |
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
|
|
65 |
$result = $route->parse('/google');
|
|
|
66 |
$header = $route->response->header();
|
|
|
67 |
$this->assertEquals('http://google.com', $header['Location']);
|
|
|
68 |
|
|
|
69 |
$route = new RedirectRoute('/posts/*', array('controller' => 'posts', 'action' => 'view'), array('status' => 302));
|
|
|
70 |
$route->stop = false;
|
|
|
71 |
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
|
|
72 |
$result = $route->parse('/posts/2');
|
|
|
73 |
$header = $route->response->header();
|
|
|
74 |
$this->assertEquals(Router::url('/posts/view', true), $header['Location']);
|
|
|
75 |
$this->assertEquals(302, $route->response->statusCode());
|
|
|
76 |
|
|
|
77 |
$route = new RedirectRoute('/posts/*', array('controller' => 'posts', 'action' => 'view'), array('persist' => true));
|
|
|
78 |
$route->stop = false;
|
|
|
79 |
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
|
|
80 |
$result = $route->parse('/posts/2');
|
|
|
81 |
$header = $route->response->header();
|
|
|
82 |
$this->assertEquals(Router::url('/posts/view/2', true), $header['Location']);
|
|
|
83 |
|
|
|
84 |
$route = new RedirectRoute('/posts/*', '/test', array('persist' => true));
|
|
|
85 |
$route->stop = false;
|
|
|
86 |
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
|
|
87 |
$result = $route->parse('/posts/2');
|
|
|
88 |
$header = $route->response->header();
|
|
|
89 |
$this->assertEquals(Router::url('/test', true), $header['Location']);
|
|
|
90 |
|
|
|
91 |
$route = new RedirectRoute('/my_controllers/:action/*', array('controller' => 'tags', 'action' => 'add'), array('persist' => true));
|
|
|
92 |
$route->stop = false;
|
|
|
93 |
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
|
|
94 |
$result = $route->parse('/my_controllers/do_something/passme/named:param');
|
|
|
95 |
$header = $route->response->header();
|
|
|
96 |
$this->assertEquals(Router::url('/tags/add/passme/named:param', true), $header['Location']);
|
|
|
97 |
|
|
|
98 |
$route = new RedirectRoute('/my_controllers/:action/*', array('controller' => 'tags', 'action' => 'add'));
|
|
|
99 |
$route->stop = false;
|
|
|
100 |
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
|
|
101 |
$result = $route->parse('/my_controllers/do_something/passme/named:param');
|
|
|
102 |
$header = $route->response->header();
|
|
|
103 |
$this->assertEquals(Router::url('/tags/add', true), $header['Location']);
|
|
|
104 |
|
|
|
105 |
$route = new RedirectRoute('/:lang/my_controllers', array('controller' => 'tags', 'action' => 'add'), array('lang' => '(nl|en)', 'persist' => array('lang')));
|
|
|
106 |
$route->stop = false;
|
|
|
107 |
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
|
|
108 |
$result = $route->parse('/nl/my_controllers/');
|
|
|
109 |
$header = $route->response->header();
|
|
|
110 |
$this->assertEquals(Router::url('/tags/add/lang:nl', true), $header['Location']);
|
|
|
111 |
|
|
|
112 |
Router::$routes = array(); // reset default routes
|
|
|
113 |
Router::connect('/:lang/preferred_controllers', array('controller' => 'tags', 'action' => 'add'), array('lang' => '(nl|en)', 'persist' => array('lang')));
|
|
|
114 |
$route = new RedirectRoute('/:lang/my_controllers', array('controller' => 'tags', 'action' => 'add'), array('lang' => '(nl|en)', 'persist' => array('lang')));
|
|
|
115 |
$route->stop = false;
|
|
|
116 |
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
|
|
117 |
$result = $route->parse('/nl/my_controllers/');
|
|
|
118 |
$header = $route->response->header();
|
|
|
119 |
$this->assertEquals(Router::url('/nl/preferred_controllers', true), $header['Location']);
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
}
|