UrlManagerTest.php 9.42 KB
Newer Older
Qiang Xue committed
1 2 3
<?php
namespace yiiunit\framework\web;

Qiang Xue committed
4
use yii\web\Request;
Qiang Xue committed
5
use yii\web\UrlManager;
Alexander Makarov committed
6
use yiiunit\TestCase;
Qiang Xue committed
7

8 9 10
/**
 * @group web
 */
Alexander Makarov committed
11
class UrlManagerTest extends TestCase
Qiang Xue committed
12
{
Qiang Xue committed
13 14 15 16 17 18
	protected function setUp()
	{
		parent::setUp();
		$this->mockApplication();
	}

Qiang Xue committed
19 20 21
	public function testCreateUrl()
	{
		// default setting with '/' as base url
Alexander Makarov committed
22
		$manager = new UrlManager([
Qiang Xue committed
23
			'baseUrl' => '/',
24
			'cache' => null,
Alexander Makarov committed
25
		]);
Qiang Xue committed
26
		$url = $manager->createUrl('post/view');
27
		$this->assertEquals('?r=post/view', $url);
Alexander Makarov committed
28
		$url = $manager->createUrl('post/view', ['id' => 1, 'title' => 'sample post']);
29
		$this->assertEquals('?r=post/view&id=1&title=sample+post', $url);
Qiang Xue committed
30 31

		// default setting with '/test/' as base url
Alexander Makarov committed
32
		$manager = new UrlManager([
Qiang Xue committed
33
			'baseUrl' => '/test/',
34
			'cache' => null,
Alexander Makarov committed
35 36
		]);
		$url = $manager->createUrl('post/view', ['id' => 1, 'title' => 'sample post']);
37
		$this->assertEquals('/test?r=post/view&id=1&title=sample+post', $url);
Qiang Xue committed
38 39

		// pretty URL without rules
Alexander Makarov committed
40
		$manager = new UrlManager([
Qiang Xue committed
41 42
			'enablePrettyUrl' => true,
			'baseUrl' => '/',
43
			'cache' => null,
Alexander Makarov committed
44 45
		]);
		$url = $manager->createUrl('post/view', ['id' => 1, 'title' => 'sample post']);
Qiang Xue committed
46
		$this->assertEquals('/post/view?id=1&title=sample+post', $url);
Alexander Makarov committed
47
		$manager = new UrlManager([
Qiang Xue committed
48 49
			'enablePrettyUrl' => true,
			'baseUrl' => '/test/',
50
			'cache' => null,
Alexander Makarov committed
51 52
		]);
		$url = $manager->createUrl('post/view', ['id' => 1, 'title' => 'sample post']);
Qiang Xue committed
53
		$this->assertEquals('/test/post/view?id=1&title=sample+post', $url);
Alexander Makarov committed
54
		$manager = new UrlManager([
Qiang Xue committed
55 56
			'enablePrettyUrl' => true,
			'baseUrl' => '/test/index.php',
57
			'cache' => null,
Alexander Makarov committed
58 59
		]);
		$url = $manager->createUrl('post/view', ['id' => 1, 'title' => 'sample post']);
Qiang Xue committed
60
		$this->assertEquals('/test/index.php/post/view?id=1&title=sample+post', $url);
Qiang Xue committed
61 62 63 64

		// todo: test showScriptName

		// pretty URL with rules
Alexander Makarov committed
65
		$manager = new UrlManager([
Qiang Xue committed
66
			'enablePrettyUrl' => true,
67
			'cache' => null,
Alexander Makarov committed
68 69
			'rules' => [
				[
Qiang Xue committed
70
					'pattern' => 'post/<id>/<title>',
Qiang Xue committed
71
					'route' => 'post/view',
Alexander Makarov committed
72 73
				],
			],
Qiang Xue committed
74
			'baseUrl' => '/',
Alexander Makarov committed
75 76
		]);
		$url = $manager->createUrl('post/view', ['id' => 1, 'title' => 'sample post']);
Qiang Xue committed
77
		$this->assertEquals('/post/1/sample+post', $url);
Alexander Makarov committed
78
		$url = $manager->createUrl('post/index', ['page' => 1]);
Qiang Xue committed
79 80 81
		$this->assertEquals('/post/index?page=1', $url);

		// pretty URL with rules and suffix
Alexander Makarov committed
82
		$manager = new UrlManager([
Qiang Xue committed
83
			'enablePrettyUrl' => true,
84
			'cache' => null,
Alexander Makarov committed
85 86
			'rules' => [
				[
Qiang Xue committed
87
					'pattern' => 'post/<id>/<title>',
Qiang Xue committed
88
					'route' => 'post/view',
Alexander Makarov committed
89 90
				],
			],
Qiang Xue committed
91 92
			'baseUrl' => '/',
			'suffix' => '.html',
Alexander Makarov committed
93 94
		]);
		$url = $manager->createUrl('post/view', ['id' => 1, 'title' => 'sample post']);
Qiang Xue committed
95
		$this->assertEquals('/post/1/sample+post.html', $url);
Alexander Makarov committed
96
		$url = $manager->createUrl('post/index', ['page' => 1]);
Qiang Xue committed
97
		$this->assertEquals('/post/index.html?page=1', $url);
98 99

		// pretty URL with rules that have host info
Alexander Makarov committed
100
		$manager = new UrlManager([
101 102
			'enablePrettyUrl' => true,
			'cache' => null,
Alexander Makarov committed
103 104
			'rules' => [
				[
Qiang Xue committed
105
					'pattern' => 'post/<id>/<title>',
106
					'route' => 'post/view',
Qiang Xue committed
107
					'host' => 'http://<lang:en|fr>.example.com',
Alexander Makarov committed
108 109
				],
			],
110
			'baseUrl' => '/test',
Alexander Makarov committed
111 112
		]);
		$url = $manager->createUrl('post/view', ['id' => 1, 'title' => 'sample post', 'lang' => 'en']);
113
		$this->assertEquals('http://en.example.com/test/post/1/sample+post', $url);
Alexander Makarov committed
114
		$url = $manager->createUrl('post/index', ['page' => 1]);
115
		$this->assertEquals('/test/post/index?page=1', $url);
Qiang Xue committed
116 117 118 119
	}

	public function testCreateAbsoluteUrl()
	{
Alexander Makarov committed
120
		$manager = new UrlManager([
Qiang Xue committed
121 122
			'baseUrl' => '/',
			'hostInfo' => 'http://www.example.com',
123
			'cache' => null,
Alexander Makarov committed
124 125
		]);
		$url = $manager->createAbsoluteUrl('post/view', ['id' => 1, 'title' => 'sample post']);
126
		$this->assertEquals('http://www.example.com?r=post/view&id=1&title=sample+post', $url);
Qiang Xue committed
127 128 129 130
	}

	public function testParseRequest()
	{
Alexander Makarov committed
131
		$manager = new UrlManager(['cache' => null]);
Qiang Xue committed
132 133 134 135 136
		$request = new Request;

		// default setting without 'r' param
		unset($_GET['r']);
		$result = $manager->parseRequest($request);
Alexander Makarov committed
137
		$this->assertEquals(['', []], $result);
Qiang Xue committed
138 139 140 141

		// default setting with 'r' param
		$_GET['r'] = 'site/index';
		$result = $manager->parseRequest($request);
Alexander Makarov committed
142
		$this->assertEquals(['site/index', []], $result);
Qiang Xue committed
143 144

		// default setting with 'r' param as an array
Alexander Makarov committed
145
		$_GET['r'] = ['site/index'];
Qiang Xue committed
146
		$result = $manager->parseRequest($request);
Alexander Makarov committed
147
		$this->assertEquals(['', []], $result);
Qiang Xue committed
148 149

		// pretty URL without rules
Alexander Makarov committed
150
		$manager = new UrlManager([
Qiang Xue committed
151
			'enablePrettyUrl' => true,
152
			'cache' => null,
Alexander Makarov committed
153
		]);
Qiang Xue committed
154 155 156
		// empty pathinfo
		$request->pathInfo = '';
		$result = $manager->parseRequest($request);
Alexander Makarov committed
157
		$this->assertEquals(['', []], $result);
Qiang Xue committed
158 159 160
		// normal pathinfo
		$request->pathInfo = 'site/index';
		$result = $manager->parseRequest($request);
Alexander Makarov committed
161
		$this->assertEquals(['site/index', []], $result);
Qiang Xue committed
162 163 164
		// pathinfo with module
		$request->pathInfo = 'module/site/index';
		$result = $manager->parseRequest($request);
Alexander Makarov committed
165
		$this->assertEquals(['module/site/index', []], $result);
Qiang Xue committed
166
		// pathinfo with trailing slashes
Qiang Xue committed
167
		$request->pathInfo = '/module/site/index/';
Qiang Xue committed
168
		$result = $manager->parseRequest($request);
Alexander Makarov committed
169
		$this->assertEquals(['module/site/index/', []], $result);
Qiang Xue committed
170

Qiang Xue committed
171
		// pretty URL rules
Alexander Makarov committed
172
		$manager = new UrlManager([
Qiang Xue committed
173
			'enablePrettyUrl' => true,
174
			'cache' => null,
Alexander Makarov committed
175 176
			'rules' => [
				[
Qiang Xue committed
177 178
					'pattern' => 'post/<id>/<title>',
					'route' => 'post/view',
Alexander Makarov committed
179 180 181
				],
			],
		]);
Qiang Xue committed
182 183 184
		// matching pathinfo
		$request->pathInfo = 'post/123/this+is+sample';
		$result = $manager->parseRequest($request);
Alexander Makarov committed
185
		$this->assertEquals(['post/view', ['id' => '123', 'title' => 'this+is+sample']], $result);
Qiang Xue committed
186
		// trailing slash is significant
Qiang Xue committed
187 188
		$request->pathInfo = 'post/123/this+is+sample/';
		$result = $manager->parseRequest($request);
Alexander Makarov committed
189
		$this->assertEquals(['post/123/this+is+sample/', []], $result);
Qiang Xue committed
190 191 192
		// empty pathinfo
		$request->pathInfo = '';
		$result = $manager->parseRequest($request);
Alexander Makarov committed
193
		$this->assertEquals(['', []], $result);
Qiang Xue committed
194 195 196
		// normal pathinfo
		$request->pathInfo = 'site/index';
		$result = $manager->parseRequest($request);
Alexander Makarov committed
197
		$this->assertEquals(['site/index', []], $result);
Qiang Xue committed
198 199 200
		// pathinfo with module
		$request->pathInfo = 'module/site/index';
		$result = $manager->parseRequest($request);
Alexander Makarov committed
201
		$this->assertEquals(['module/site/index', []], $result);
Qiang Xue committed
202 203

		// pretty URL rules
Alexander Makarov committed
204
		$manager = new UrlManager([
Qiang Xue committed
205 206
			'enablePrettyUrl' => true,
			'suffix' => '.html',
207
			'cache' => null,
Alexander Makarov committed
208 209
			'rules' => [
				[
Qiang Xue committed
210 211
					'pattern' => 'post/<id>/<title>',
					'route' => 'post/view',
Alexander Makarov committed
212 213 214
				],
			],
		]);
Qiang Xue committed
215 216 217
		// matching pathinfo
		$request->pathInfo = 'post/123/this+is+sample.html';
		$result = $manager->parseRequest($request);
Alexander Makarov committed
218
		$this->assertEquals(['post/view', ['id' => '123', 'title' => 'this+is+sample']], $result);
Qiang Xue committed
219 220 221 222 223 224 225
		// matching pathinfo without suffix
		$request->pathInfo = 'post/123/this+is+sample';
		$result = $manager->parseRequest($request);
		$this->assertFalse($result);
		// empty pathinfo
		$request->pathInfo = '';
		$result = $manager->parseRequest($request);
Alexander Makarov committed
226
		$this->assertEquals(['', []], $result);
Qiang Xue committed
227 228 229
		// normal pathinfo
		$request->pathInfo = 'site/index.html';
		$result = $manager->parseRequest($request);
Alexander Makarov committed
230
		$this->assertEquals(['site/index', []], $result);
Qiang Xue committed
231 232 233 234
		// pathinfo without suffix
		$request->pathInfo = 'site/index';
		$result = $manager->parseRequest($request);
		$this->assertFalse($result);
235 236

		// strict parsing
Alexander Makarov committed
237
		$manager = new UrlManager([
238 239 240 241
			'enablePrettyUrl' => true,
			'enableStrictParsing' => true,
			'suffix' => '.html',
			'cache' => null,
Alexander Makarov committed
242 243
			'rules' => [
				[
244 245
					'pattern' => 'post/<id>/<title>',
					'route' => 'post/view',
Alexander Makarov committed
246 247 248
				],
			],
		]);
249 250 251
		// matching pathinfo
		$request->pathInfo = 'post/123/this+is+sample.html';
		$result = $manager->parseRequest($request);
Alexander Makarov committed
252
		$this->assertEquals(['post/view', ['id' => '123', 'title' => 'this+is+sample']], $result);
253 254 255 256
		// unmatching pathinfo
		$request->pathInfo = 'site/index.html';
		$result = $manager->parseRequest($request);
		$this->assertFalse($result);
Qiang Xue committed
257
	}
258

259 260 261 262 263
	public function testParseRESTRequest()
	{
		$request = new Request;

		// pretty URL rules
Alexander Makarov committed
264
		$manager = new UrlManager([
265
			'enablePrettyUrl' => true,
Qiang Xue committed
266
			'showScriptName' => false,
267
			'cache' => null,
Alexander Makarov committed
268
			'rules' => [
269 270 271 272
				'PUT,POST post/<id>/<title>' => 'post/create',
				'DELETE post/<id>' => 'post/delete',
				'post/<id>/<title>' => 'post/view',
				'POST/GET' => 'post/get',
Alexander Makarov committed
273 274
			],
		]);
275 276 277 278
		// matching pathinfo GET request
		$_SERVER['REQUEST_METHOD'] = 'GET';
		$request->pathInfo = 'post/123/this+is+sample';
		$result = $manager->parseRequest($request);
Alexander Makarov committed
279
		$this->assertEquals(['post/view', ['id' => '123', 'title' => 'this+is+sample']], $result);
280 281 282 283
		// matching pathinfo PUT/POST request
		$_SERVER['REQUEST_METHOD'] = 'PUT';
		$request->pathInfo = 'post/123/this+is+sample';
		$result = $manager->parseRequest($request);
Alexander Makarov committed
284
		$this->assertEquals(['post/create', ['id' => '123', 'title' => 'this+is+sample']], $result);
285 286 287
		$_SERVER['REQUEST_METHOD'] = 'POST';
		$request->pathInfo = 'post/123/this+is+sample';
		$result = $manager->parseRequest($request);
Alexander Makarov committed
288
		$this->assertEquals(['post/create', ['id' => '123', 'title' => 'this+is+sample']], $result);
289 290 291 292 293

		// no wrong matching
		$_SERVER['REQUEST_METHOD'] = 'POST';
		$request->pathInfo = 'POST/GET';
		$result = $manager->parseRequest($request);
Alexander Makarov committed
294
		$this->assertEquals(['post/get', []], $result);
295 296

		// createUrl should ignore REST rules
Alexander Makarov committed
297 298 299
		$this->mockApplication([
			'components' => [
				'request' => [
300 301
					'hostInfo' => 'http://localhost/',
					'baseUrl' => '/app'
Alexander Makarov committed
302 303 304 305
				]
			]
		], \yii\web\Application::className());
		$this->assertEquals('/app/post/delete?id=123', $manager->createUrl('post/delete', ['id' => 123]));
306 307 308 309
		$this->destroyApplication();

		unset($_SERVER['REQUEST_METHOD']);
	}
Qiang Xue committed
310
}