UrlManagerTest.php 9.83 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

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

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

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

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

		// todo: test showScriptName

		// pretty URL with rules
		$manager = new UrlManager(array(
			'enablePrettyUrl' => true,
64
			'cache' => null,
Qiang Xue committed
65 66 67
			'rules' => array(
				array(
					'pattern' => 'post/<id>/<title>',
Qiang Xue committed
68
					'route' => 'post/view',
Qiang Xue committed
69 70 71 72 73
				),
			),
			'baseUrl' => '/',
		));
		$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
Qiang Xue committed
74
		$this->assertEquals('/post/1/sample+post', $url);
Qiang Xue committed
75 76 77 78 79 80
		$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,
81
			'cache' => null,
Qiang Xue committed
82 83 84
			'rules' => array(
				array(
					'pattern' => 'post/<id>/<title>',
Qiang Xue committed
85
					'route' => 'post/view',
Qiang Xue committed
86 87 88 89 90 91
				),
			),
			'baseUrl' => '/',
			'suffix' => '.html',
		));
		$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
Qiang Xue committed
92
		$this->assertEquals('/post/1/sample+post.html', $url);
Qiang Xue committed
93 94
		$url = $manager->createUrl('post/index', array('page' => 1));
		$this->assertEquals('/post/index.html?page=1', $url);
95 96 97 98 99 100 101

		// pretty URL with rules that have host info
		$manager = new UrlManager(array(
			'enablePrettyUrl' => true,
			'cache' => null,
			'rules' => array(
				array(
Qiang Xue committed
102
					'pattern' => 'post/<id>/<title>',
103
					'route' => 'post/view',
Qiang Xue committed
104
					'host' => 'http://<lang:en|fr>.example.com',
105 106 107 108 109 110 111 112
				),
			),
			'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
113 114 115 116
	}

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

	public function testParseRequest()
	{
128 129 130
		$manager = new UrlManager(array(
			'cache' => null,
		));
Qiang Xue committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
		$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,
151
			'cache' => null,
Qiang Xue committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165
		));
		// 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
166
		$request->pathInfo = '/module/site/index/';
Qiang Xue committed
167
		$result = $manager->parseRequest($request);
Qiang Xue committed
168
		$this->assertEquals(array('module/site/index/', array()), $result);
Qiang Xue committed
169

Qiang Xue committed
170 171 172
		// pretty URL rules
		$manager = new UrlManager(array(
			'enablePrettyUrl' => true,
173
			'cache' => null,
Qiang Xue committed
174 175 176 177 178 179 180 181 182 183 184
			'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
185
		// trailing slash is significant
Qiang Xue committed
186 187
		$request->pathInfo = 'post/123/this+is+sample/';
		$result = $manager->parseRequest($request);
Qiang Xue committed
188
		$this->assertEquals(array('post/123/this+is+sample/', array()), $result);
Qiang Xue committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
		// 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',
206
			'cache' => null,
Qiang Xue committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
			'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);
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

		// 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
256
	}
257

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

		// pretty URL rules
		$manager = new UrlManager(array(
			'enablePrettyUrl' => true,
Qiang Xue committed
265
			'showScriptName' => false,
266 267 268 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
			'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
309
}