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 22 23
	public function testCreateUrl()
	{
		// default setting with '/' as base url
		$manager = new UrlManager(array(
			'baseUrl' => '/',
24
			'cache' => null,
Qiang Xue committed
25 26
		));
		$url = $manager->createUrl('post/view');
27
		$this->assertEquals('?r=post/view', $url);
Qiang Xue committed
28
		$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
29
		$this->assertEquals('?r=post/view&id=1&title=sample+post', $url);
Qiang Xue committed
30 31 32 33

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

		// pretty URL without rules
		$manager = new UrlManager(array(
			'enablePrettyUrl' => true,
			'baseUrl' => '/',
43
			'cache' => null,
Qiang Xue committed
44 45
		));
		$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
Qiang Xue committed
46
		$this->assertEquals('/post/view?id=1&title=sample+post', $url);
Qiang Xue committed
47 48 49
		$manager = new UrlManager(array(
			'enablePrettyUrl' => true,
			'baseUrl' => '/test/',
50
			'cache' => null,
Qiang Xue committed
51 52
		));
		$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
Qiang Xue committed
53
		$this->assertEquals('/test/post/view?id=1&title=sample+post', $url);
Qiang Xue committed
54 55 56
		$manager = new UrlManager(array(
			'enablePrettyUrl' => true,
			'baseUrl' => '/test/index.php',
57
			'cache' => null,
Qiang Xue committed
58 59
		));
		$url = $manager->createUrl('post/view', array('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 65 66

		// todo: test showScriptName

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

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

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

	public function testCreateAbsoluteUrl()
	{
Qiang Xue committed
120 121 122
		$manager = new UrlManager(array(
			'baseUrl' => '/',
			'hostInfo' => 'http://www.example.com',
123
			'cache' => null,
Qiang Xue committed
124 125
		));
		$url = $manager->createAbsoluteUrl('post/view', array('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()
	{
131 132 133
		$manager = new UrlManager(array(
			'cache' => null,
		));
Qiang Xue committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
		$request = new Request;

		// default setting without 'r' param
		unset($_GET['r']);
		$result = $manager->parseRequest($request);
		$this->assertEquals(array('', array()), $result);

		// default setting with 'r' param
		$_GET['r'] = 'site/index';
		$result = $manager->parseRequest($request);
		$this->assertEquals(array('site/index', array()), $result);

		// default setting with 'r' param as an array
		$_GET['r'] = array('site/index');
		$result = $manager->parseRequest($request);
		$this->assertEquals(array('', array()), $result);

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

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

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

		// strict parsing
		$manager = new UrlManager(array(
			'enablePrettyUrl' => true,
			'enableStrictParsing' => true,
			'suffix' => '.html',
			'cache' => null,
			'rules' => array(
				array(
					'pattern' => 'post/<id>/<title>',
					'route' => 'post/view',
				),
			),
		));
		// matching pathinfo
		$request->pathInfo = 'post/123/this+is+sample.html';
		$result = $manager->parseRequest($request);
		$this->assertEquals(array('post/view', array('id' => '123', 'title' => 'this+is+sample')), $result);
		// unmatching pathinfo
		$request->pathInfo = 'site/index.html';
		$result = $manager->parseRequest($request);
		$this->assertFalse($result);
Qiang Xue committed
259
	}
260

261 262 263 264 265 266 267
	public function testParseRESTRequest()
	{
		$request = new Request;

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

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

		// createUrl should ignore REST rules
		$this->mockApplication(array(
			'components' => array(
				'request' => array(
					'hostInfo' => 'http://localhost/',
					'baseUrl' => '/app'
				)
			)
		), \yii\web\Application::className());
		$this->assertEquals('/app/post/delete?id=123', $manager->createUrl('post/delete', array('id' => 123)));
		$this->destroyApplication();

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