1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
135
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
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
227
228
<?php
namespace yiiunit\framework\web;
use yii\web\Request;
use yii\web\UrlManager;
class UrlManagerTest extends \yiiunit\TestCase
{
public function testCreateUrl()
{
// default setting with '/' as base url
$manager = new UrlManager(array(
'baseUrl' => '/',
'cache' => null,
));
$url = $manager->createUrl('post/view');
$this->assertEquals('?r=post/view', $url);
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('?r=post/view&id=1&title=sample+post', $url);
// default setting with '/test/' as base url
$manager = new UrlManager(array(
'baseUrl' => '/test/',
'cache' => null,
));
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('/test?r=post/view&id=1&title=sample+post', $url);
// pretty URL without rules
$manager = new UrlManager(array(
'enablePrettyUrl' => true,
'baseUrl' => '/',
'cache' => null,
));
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('/post/view?id=1&title=sample+post', $url);
$manager = new UrlManager(array(
'enablePrettyUrl' => true,
'baseUrl' => '/test/',
'cache' => null,
));
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('/test/post/view?id=1&title=sample+post', $url);
$manager = new UrlManager(array(
'enablePrettyUrl' => true,
'baseUrl' => '/test/index.php',
'cache' => null,
));
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('/test/index.php/post/view?id=1&title=sample+post', $url);
// todo: test showScriptName
// pretty URL with rules
$manager = new UrlManager(array(
'enablePrettyUrl' => true,
'cache' => null,
'rules' => array(
array(
'pattern' => 'post/<id>/<title>',
'route' => 'post/view',
),
),
'baseUrl' => '/',
));
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('/post/1/sample+post', $url);
$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,
'cache' => null,
'rules' => array(
array(
'pattern' => 'post/<id>/<title>',
'route' => 'post/view',
),
),
'baseUrl' => '/',
'suffix' => '.html',
));
$url = $manager->createUrl('post/view', array('id' => 1, 'title' => 'sample post'));
$this->assertEquals('/post/1/sample+post.html', $url);
$url = $manager->createUrl('post/index', array('page' => 1));
$this->assertEquals('/post/index.html?page=1', $url);
// pretty URL with rules that have host info
$manager = new UrlManager(array(
'enablePrettyUrl' => true,
'cache' => null,
'rules' => array(
array(
'pattern' => 'post/<id>/<title>',
'route' => 'post/view',
'host' => 'http://<lang:en|fr>.example.com',
),
),
'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);
}
public function testCreateAbsoluteUrl()
{
$manager = new UrlManager(array(
'baseUrl' => '/',
'hostInfo' => 'http://www.example.com',
'cache' => null,
));
$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);
}
public function testParseRequest()
{
$manager = new UrlManager(array(
'cache' => null,
));
$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,
'cache' => null,
));
// 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);
// pretty URL rules
$manager = new UrlManager(array(
'enablePrettyUrl' => true,
'cache' => null,
'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',
'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);
// 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);
}
}