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

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

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

		// todo: test showScriptName

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

	public function testCreateAbsoluteUrl()
	{
Qiang Xue committed
87 88 89 90 91 92
		$manager = new UrlManager(array(
			'baseUrl' => '/',
			'hostInfo' => 'http://www.example.com',
		));
		$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
93 94 95 96
	}

	public function testParseRequest()
	{
Qiang Xue committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
		$manager = new UrlManager;
		$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,
		));
		// 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
135

Qiang Xue committed
136 137 138 139 140 141 142 143 144 145 146 147 148 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
		// pretty URL rules
		$manager = new UrlManager(array(
			'enablePrettyUrl' => true,
			'cacheID' => false,
			'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',
			'cacheID' => false,
			'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
200
	}
Qiang Xue committed
201
}