UrlManagerTest.php 7.36 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 6
use yii\web\UrlManager;

Qiang Xue committed
7 8
class UrlManagerTest extends \yiiunit\TestCase
{
Qiang Xue committed
9 10 11 12 13
	public function testCreateUrl()
	{
		// default setting with '/' as base url
		$manager = new UrlManager(array(
			'baseUrl' => '/',
14
			'cache' => null,
Qiang Xue committed
15 16
		));
		$url = $manager->createUrl('post/view');
17
		$this->assertEquals('?r=post/view', $url);
Qiang Xue committed
18
		$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
19
		$this->assertEquals('?r=post/view&id=1&title=sample+post', $url);
Qiang Xue committed
20 21 22 23

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

		// pretty URL without rules
		$manager = new UrlManager(array(
			'enablePrettyUrl' => true,
			'baseUrl' => '/',
33
			'cache' => null,
Qiang Xue committed
34 35
		));
		$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
Qiang Xue committed
36
		$this->assertEquals('/post/view?id=1&title=sample+post', $url);
Qiang Xue committed
37 38 39
		$manager = new UrlManager(array(
			'enablePrettyUrl' => true,
			'baseUrl' => '/test/',
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('/test/post/view?id=1&title=sample+post', $url);
Qiang Xue committed
44 45 46
		$manager = new UrlManager(array(
			'enablePrettyUrl' => true,
			'baseUrl' => '/test/index.php',
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/index.php/post/view?id=1&title=sample+post', $url);
Qiang Xue committed
51 52 53 54 55 56

		// todo: test showScriptName

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

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

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

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

Qiang Xue committed
163 164 165
		// pretty URL rules
		$manager = new UrlManager(array(
			'enablePrettyUrl' => true,
166
			'cache' => null,
Qiang Xue committed
167 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
			'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',
199
			'cache' => null,
Qiang Xue committed
200 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
			'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);
Qiang Xue committed
227
	}
Qiang Xue committed
228
}