UrlManagerTest.php 9.85 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
		]);
26
		$url = $manager->createUrl(['post/view']);
27
		$this->assertEquals('?r=post/view', $url);
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);
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);
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);
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);
127

128
		$url = $manager->createAbsoluteUrl(['post/view', 'id' => 1, 'title' => 'sample post'], 'https');
129 130 131
		$this->assertEquals('https://www.example.com?r=post/view&id=1&title=sample+post', $url);

		$manager->hostInfo = 'https://www.example.com';
132
		$url = $manager->createAbsoluteUrl(['post/view', 'id' => 1, 'title' => 'sample post'], 'http');
133
		$this->assertEquals('http://www.example.com?r=post/view&id=1&title=sample+post', $url);
Qiang Xue committed
134 135 136 137
	}

	public function testParseRequest()
	{
Alexander Makarov committed
138
		$manager = new UrlManager(['cache' => null]);
Qiang Xue committed
139 140 141 142 143
		$request = new Request;

		// default setting without 'r' param
		unset($_GET['r']);
		$result = $manager->parseRequest($request);
Alexander Makarov committed
144
		$this->assertEquals(['', []], $result);
Qiang Xue committed
145 146 147 148

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

		// default setting with 'r' param as an array
Alexander Makarov committed
152
		$_GET['r'] = ['site/index'];
Qiang Xue committed
153
		$result = $manager->parseRequest($request);
Alexander Makarov committed
154
		$this->assertEquals(['', []], $result);
Qiang Xue committed
155 156

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

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

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

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

266 267 268 269 270
	public function testParseRESTRequest()
	{
		$request = new Request;

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

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

		// createUrl should ignore REST rules
Alexander Makarov committed
304 305 306
		$this->mockApplication([
			'components' => [
				'request' => [
307 308
					'hostInfo' => 'http://localhost/',
					'baseUrl' => '/app'
Alexander Makarov committed
309 310 311
				]
			]
		], \yii\web\Application::className());
312
		$this->assertEquals('/app/post/delete?id=123', $manager->createUrl(['post/delete', 'id' => 123]));
313 314 315 316
		$this->destroyApplication();

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