UrlManagerTest.php 9.81 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
	public function testCreateUrl()
	{
		// default setting with '/' as base url
		$manager = new UrlManager(array(
			'baseUrl' => '/',
15
			'cache' => null,
Qiang Xue committed
16 17
		));
		$url = $manager->createUrl('post/view');
18
		$this->assertEquals('?r=post/view', $url);
Qiang Xue committed
19
		$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
20
		$this->assertEquals('?r=post/view&id=1&title=sample+post', $url);
Qiang Xue committed
21 22 23 24

		// default setting with '/test/' as base url
		$manager = new UrlManager(array(
			'baseUrl' => '/test/',
25
			'cache' => null,
Qiang Xue committed
26 27
		));
		$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
28
		$this->assertEquals('/test?r=post/view&id=1&title=sample+post', $url);
Qiang Xue committed
29 30 31 32 33

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

		// todo: test showScriptName

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

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

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

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

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

		// 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
250
	}
251

252 253 254 255 256 257 258 259 260 261 262 263 264 265 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
	public function testParseRESTRequest()
	{
		$manager = new UrlManager(array(
			'cache' => null,
		));
		$request = new Request;

		// pretty URL rules
		$manager = new UrlManager(array(
			'enablePrettyUrl' => true,
			'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
305
}