UrlManagerTest.php 6.75 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 17 18
		));
		$url = $manager->createUrl('post/view');
		$this->assertEquals('/?r=post/view', $url);
		$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
Qiang Xue committed
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'));
Qiang Xue committed
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 88 89 90 91
		$url = $manager->createUrl('post/index', array('page' => 1));
		$this->assertEquals('/post/index.html?page=1', $url);
	}

	public function testCreateAbsoluteUrl()
	{
Qiang Xue committed
92 93 94
		$manager = new UrlManager(array(
			'baseUrl' => '/',
			'hostInfo' => 'http://www.example.com',
95
			'cache' => null,
Qiang Xue committed
96 97 98
		));
		$url = $manager->createAbsoluteUrl('post/view', array('id' => 1, 'title' => 'sample post'));
		$this->assertEquals('http://www.example.com/?r=post/view&id=1&title=sample+post', $url);
Qiang Xue committed
99 100 101 102
	}

	public function testParseRequest()
	{
103 104 105
		$manager = new UrlManager(array(
			'cache' => null,
		));
Qiang Xue committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
		$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,
126
			'cache' => null,
Qiang Xue committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
		));
		// 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
144

Qiang Xue committed
145 146 147
		// pretty URL rules
		$manager = new UrlManager(array(
			'enablePrettyUrl' => true,
148
			'cache' => null,
Qiang Xue committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
			'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',
181
			'cache' => null,
Qiang Xue committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
			'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
209
	}
Qiang Xue committed
210
}