mirror of
https://github.com/yiisoft/yii.git
synced 2026-03-03 23:04:04 +01:00
415 lines
10 KiB
PHP
415 lines
10 KiB
PHP
<?php
|
|
|
|
Yii::import('system.web.CUrlManager');
|
|
Yii::import('system.web.CHttpRequest');
|
|
|
|
class TestHttpRequest extends CHttpRequest
|
|
{
|
|
private $myPathInfo;
|
|
private $myScriptUrl;
|
|
|
|
public function getScriptUrl()
|
|
{
|
|
return $this->myScriptUrl;
|
|
}
|
|
|
|
public function setScriptUrl($value)
|
|
{
|
|
$this->myScriptUrl=$value;
|
|
}
|
|
|
|
public function getPathInfo()
|
|
{
|
|
return $this->myPathInfo;
|
|
}
|
|
|
|
public function setPathInfo($value)
|
|
{
|
|
$this->myPathInfo=$value;
|
|
}
|
|
}
|
|
|
|
|
|
class CUrlManagerTest extends CTestCase
|
|
{
|
|
public function testParseUrlWithPathFormat()
|
|
{
|
|
$rules=array(
|
|
'article/<id:\d+>'=>'article/read',
|
|
'article/<year:\d{4}>/<title>/*'=>'article/read',
|
|
'a/<_a>/*'=>'article',
|
|
'register/*'=>'user',
|
|
'home/*'=>'',
|
|
'ad/*'=>'admin/index/list',
|
|
'<c:(post|comment)>/<id:\d+>/<a:(create|update|delete)>'=>'<c>/<a>',
|
|
'<c:(post|comment)>/<id:\d+>'=>'<c>/view',
|
|
'<c:(post|comment)>s/*'=>'<c>/list',
|
|
'http://<user:\w+>.example.com/<lang:\w+>/profile'=>'user/profile',
|
|
);
|
|
$entries=array(
|
|
array(
|
|
'pathInfo'=>'article/123',
|
|
'route'=>'article/read',
|
|
'params'=>array('id'=>'123'),
|
|
),
|
|
array(
|
|
'pathInfo'=>'article/123/name/value',
|
|
'route'=>'article/123/name/value',
|
|
'params'=>array(),
|
|
),
|
|
array(
|
|
'pathInfo'=>'article/2000/title goes here',
|
|
'route'=>'article/read',
|
|
'params'=>array('year'=>'2000','title'=>'title goes here'),
|
|
),
|
|
array(
|
|
'pathInfo'=>'article/2000/title goes here/name/value',
|
|
'route'=>'article/read',
|
|
'params'=>array('year'=>'2000','title'=>'title goes here','name'=>'value'),
|
|
),
|
|
array(
|
|
'pathInfo'=>'register/username/admin',
|
|
'route'=>'user',
|
|
'params'=>array('username'=>'admin'),
|
|
),
|
|
array(
|
|
'pathInfo'=>'home/name/value/name1/value1',
|
|
'route'=>'',
|
|
'params'=>array('name'=>'value','name1'=>'value1'),
|
|
),
|
|
array(
|
|
'pathInfo'=>'home2/name/value/name1/value1',
|
|
'route'=>'home2/name/value/name1/value1',
|
|
'params'=>array(),
|
|
),
|
|
array(
|
|
'pathInfo'=>'post',
|
|
'route'=>'post',
|
|
'params'=>array(),
|
|
),
|
|
array(
|
|
'pathInfo'=>'post/read',
|
|
'route'=>'post/read',
|
|
'params'=>array(),
|
|
),
|
|
array(
|
|
'pathInfo'=>'post/read/id/100',
|
|
'route'=>'post/read/id/100',
|
|
'params'=>array(),
|
|
),
|
|
array(
|
|
'pathInfo'=>'',
|
|
'route'=>'',
|
|
'params'=>array(),
|
|
),
|
|
array(
|
|
'pathInfo'=>'ad/name/value',
|
|
'route'=>'admin/index/list',
|
|
'params'=>array('name'=>'value'),
|
|
),
|
|
array(
|
|
'pathInfo'=>'admin/name/value',
|
|
'route'=>'admin/name/value',
|
|
'params'=>array(),
|
|
),
|
|
array(
|
|
'pathInfo'=>'posts',
|
|
'route'=>'post/list',
|
|
'params'=>array(),
|
|
),
|
|
array(
|
|
'pathInfo'=>'posts/page/3',
|
|
'route'=>'post/list',
|
|
'params'=>array('page'=>3),
|
|
),
|
|
array(
|
|
'pathInfo'=>'post/3',
|
|
'route'=>'post/view',
|
|
'params'=>array('id'=>3),
|
|
),
|
|
array(
|
|
'pathInfo'=>'post/3/delete',
|
|
'route'=>'post/delete',
|
|
'params'=>array('id'=>3),
|
|
),
|
|
array(
|
|
'pathInfo'=>'post/3/delete/a',
|
|
'route'=>'post/3/delete/a',
|
|
'params'=>array(),
|
|
),
|
|
array(
|
|
'pathInfo'=>'en/profile',
|
|
'route'=>'user/profile',
|
|
'params'=>array('user'=>'admin','lang'=>'en'),
|
|
),
|
|
);
|
|
$config=array(
|
|
'basePath'=>dirname(__FILE__),
|
|
'components'=>array(
|
|
'request'=>array(
|
|
'class'=>'TestHttpRequest',
|
|
'scriptUrl'=>'/app/index.php',
|
|
),
|
|
),
|
|
);
|
|
$app=new TestApplication($config);
|
|
$app->controllerPath=dirname(__FILE__).DIRECTORY_SEPARATOR.'controllers';
|
|
$request=$app->request;
|
|
$_SERVER['HTTP_HOST']='admin.example.com';
|
|
$um=new CUrlManager;
|
|
$um->urlSuffix='.html';
|
|
$um->urlFormat='path';
|
|
$um->rules=$rules;
|
|
$um->init($app);
|
|
foreach($entries as $entry)
|
|
{
|
|
$request->pathInfo=$entry['pathInfo'];
|
|
$_GET=array();
|
|
$route=$um->parseUrl($request);
|
|
$this->assertEquals($entry['route'],$route);
|
|
$this->assertEquals($entry['params'],$_GET);
|
|
// test the .html version
|
|
$request->pathInfo=$entry['pathInfo'].'.html';
|
|
$_GET=array();
|
|
$route=$um->parseUrl($request);
|
|
$this->assertEquals($entry['route'],$route);
|
|
$this->assertEquals($entry['params'],$_GET);
|
|
}
|
|
}
|
|
|
|
public function testcreateUrlWithPathFormat()
|
|
{
|
|
$rules=array(
|
|
'article/<id:\d+>'=>'article/read',
|
|
'article/<year:\d{4}>/<title>/*'=>'article/read',
|
|
'a/<_a>/*'=>'article',
|
|
'register/*'=>'user',
|
|
'home/*'=>'',
|
|
'<c:(post|comment)>/<id:\d+>/<a:(create|update|delete)>'=>'<c>/<a>',
|
|
'<c:(post|comment)>/<id:\d+>'=>'<c>/view',
|
|
'<c:(post|comment)>s/*'=>'<c>/list',
|
|
'http://<user:\w+>.example.com/<lang:\w+>/profile'=>'user/profile',
|
|
);
|
|
$config=array(
|
|
'basePath'=>dirname(__FILE__),
|
|
'components'=>array(
|
|
'request'=>array(
|
|
'class'=>'TestHttpRequest',
|
|
),
|
|
),
|
|
);
|
|
$app=new TestApplication($config);
|
|
$entries=array(
|
|
array(
|
|
'scriptUrl'=>'/apps/index.php',
|
|
'url'=>'/apps/index.php/post/123?name1=value1',
|
|
'url2'=>'/apps/post/123?name1=value1',
|
|
'url3'=>'/apps/post/123.html?name1=value1',
|
|
'route'=>'post/view',
|
|
'params'=>array(
|
|
'id'=>'123',
|
|
'name1'=>'value1',
|
|
),
|
|
),
|
|
array(
|
|
'scriptUrl'=>'/apps/index.php',
|
|
'url'=>'/apps/index.php/post/123/update?name1=value1',
|
|
'url2'=>'/apps/post/123/update?name1=value1',
|
|
'url3'=>'/apps/post/123/update.html?name1=value1',
|
|
'route'=>'post/update',
|
|
'params'=>array(
|
|
'id'=>'123',
|
|
'name1'=>'value1',
|
|
),
|
|
),
|
|
array(
|
|
'scriptUrl'=>'/apps/index.php',
|
|
'url'=>'/apps/index.php/posts/page/123',
|
|
'url2'=>'/apps/posts/page/123',
|
|
'url3'=>'/apps/posts/page/123.html',
|
|
'route'=>'post/list',
|
|
'params'=>array(
|
|
'page'=>'123',
|
|
),
|
|
),
|
|
array(
|
|
'scriptUrl'=>'/apps/index.php',
|
|
'url'=>'/apps/index.php/article/123?name1=value1',
|
|
'url2'=>'/apps/article/123?name1=value1',
|
|
'url3'=>'/apps/article/123.html?name1=value1',
|
|
'route'=>'article/read',
|
|
'params'=>array(
|
|
'id'=>'123',
|
|
'name1'=>'value1',
|
|
),
|
|
),
|
|
array(
|
|
'scriptUrl'=>'/index.php',
|
|
'url'=>'/index.php/article/123?name1=value1',
|
|
'url2'=>'/article/123?name1=value1',
|
|
'url3'=>'/article/123.html?name1=value1',
|
|
'route'=>'article/read',
|
|
'params'=>array(
|
|
'id'=>'123',
|
|
'name1'=>'value1',
|
|
),
|
|
),
|
|
array(
|
|
'scriptUrl'=>'/apps/index.php',
|
|
'url'=>'/apps/index.php/article/2000/the_title/name1/value1',
|
|
'url2'=>'/apps/article/2000/the_title/name1/value1',
|
|
'url3'=>'/apps/article/2000/the_title/name1/value1.html',
|
|
'route'=>'article/read',
|
|
'params'=>array(
|
|
'year'=>'2000',
|
|
'title'=>'the_title',
|
|
'name1'=>'value1',
|
|
),
|
|
),
|
|
array(
|
|
'scriptUrl'=>'/index.php',
|
|
'url'=>'/index.php/article/2000/the_title/name1/value1',
|
|
'url2'=>'/article/2000/the_title/name1/value1',
|
|
'url3'=>'/article/2000/the_title/name1/value1.html',
|
|
'route'=>'article/read',
|
|
'params'=>array(
|
|
'year'=>'2000',
|
|
'title'=>'the_title',
|
|
'name1'=>'value1',
|
|
),
|
|
),
|
|
array(
|
|
'scriptUrl'=>'/apps/index.php',
|
|
'url'=>'/apps/index.php/post/edit/id/123/name1/value1',
|
|
'url2'=>'/apps/post/edit/id/123/name1/value1',
|
|
'url3'=>'/apps/post/edit/id/123/name1/value1.html',
|
|
'route'=>'post/edit',
|
|
'params'=>array(
|
|
'id'=>'123',
|
|
'name1'=>'value1',
|
|
),
|
|
),
|
|
array(
|
|
'scriptUrl'=>'/index.php',
|
|
'url'=>'http://admin.example.com/en/profile',
|
|
'url2'=>'http://admin.example.com/en/profile',
|
|
'url3'=>'http://admin.example.com/en/profile.html',
|
|
'route'=>'user/profile',
|
|
'params'=>array(
|
|
'user'=>'admin',
|
|
'lang'=>'en',
|
|
),
|
|
),
|
|
);
|
|
foreach($entries as $entry)
|
|
{
|
|
$app->request->baseUrl=null; // reset so that it can be determined based on scriptUrl
|
|
$app->request->scriptUrl=$entry['scriptUrl'];
|
|
$um=new CUrlManager;
|
|
$um->urlFormat='path';
|
|
$um->rules=$rules;
|
|
$um->init($app);
|
|
$url=$um->createUrl($entry['route'],$entry['params']);
|
|
$this->assertEquals($entry['url'],$url);
|
|
|
|
$um=new CUrlManager;
|
|
$um->urlFormat='path';
|
|
$um->rules=$rules;
|
|
$um->init($app);
|
|
$um->showScriptName=false;
|
|
$url=$um->createUrl($entry['route'],$entry['params']);
|
|
$this->assertEquals($entry['url2'],$url);
|
|
|
|
$um->urlSuffix='.html';
|
|
$url=$um->createUrl($entry['route'],$entry['params']);
|
|
$this->assertEquals($entry['url3'],$url);
|
|
}
|
|
}
|
|
|
|
public function testParseUrlWithGetFormat()
|
|
{
|
|
$config=array(
|
|
'basePath'=>dirname(__FILE__),
|
|
'components'=>array(
|
|
'request'=>array(
|
|
'class'=>'TestHttpRequest',
|
|
'scriptUrl'=>'/app/index.php',
|
|
),
|
|
),
|
|
);
|
|
$entries=array(
|
|
array(
|
|
'route'=>'article/read',
|
|
'name'=>'value',
|
|
),
|
|
);
|
|
$app=new TestApplication($config);
|
|
$request=$app->request;
|
|
$um=new CUrlManager;
|
|
$um->urlFormat='get';
|
|
$um->routeVar='route';
|
|
$um->init($app);
|
|
foreach($entries as $entry)
|
|
{
|
|
$_GET=$entry;
|
|
$route=$um->parseUrl($request);
|
|
$this->assertEquals($entry['route'],$route);
|
|
$this->assertEquals($_GET,$entry);
|
|
}
|
|
}
|
|
|
|
public function testCreateUrlWithGetFormat()
|
|
{
|
|
$config=array(
|
|
'basePath'=>dirname(__FILE__),
|
|
'components'=>array(
|
|
'request'=>array(
|
|
'class'=>'TestHttpRequest',
|
|
),
|
|
),
|
|
);
|
|
$app=new TestApplication($config);
|
|
$entries=array(
|
|
array(
|
|
'scriptUrl'=>'/apps/index.php',
|
|
'url'=>'/apps/index.php?route=article/read&name=value&name1=value1',
|
|
'url2'=>'/apps/?route=article/read&name=value&name1=value1',
|
|
'route'=>'article/read',
|
|
'params'=>array(
|
|
'name'=>'value',
|
|
'name1'=>'value1',
|
|
),
|
|
),
|
|
array(
|
|
'scriptUrl'=>'/index.php',
|
|
'url'=>'/index.php?route=article/read&name=value&name1=value1',
|
|
'url2'=>'/?route=article/read&name=value&name1=value1',
|
|
'route'=>'article/read',
|
|
'params'=>array(
|
|
'name'=>'value',
|
|
'name1'=>'value1',
|
|
),
|
|
),
|
|
);
|
|
foreach($entries as $entry)
|
|
{
|
|
$app->request->baseUrl=null;
|
|
$app->request->scriptUrl=$entry['scriptUrl'];
|
|
$um=new CUrlManager;
|
|
$um->urlFormat='get';
|
|
$um->routeVar='route';
|
|
$um->init($app);
|
|
$url=$um->createUrl($entry['route'],$entry['params'],'&');
|
|
$this->assertEquals($url,$entry['url']);
|
|
|
|
$um=new CUrlManager;
|
|
$um->urlFormat='get';
|
|
$um->routeVar='route';
|
|
$um->showScriptName=false;
|
|
$um->init($app);
|
|
$url=$um->createUrl($entry['route'],$entry['params'],'&');
|
|
$this->assertEquals($url,$entry['url2']);
|
|
}
|
|
}
|
|
}
|