|
|
|
@ -1,14 +1,13 @@
|
|
|
|
|
<?php |
|
|
|
|
namespace yiiunit\framework\web; |
|
|
|
|
|
|
|
|
|
use yii\web\Application; |
|
|
|
|
use yii\web\Request; |
|
|
|
|
use yii\web\UrlManager; |
|
|
|
|
|
|
|
|
|
class UrlManagerTest extends \yiiunit\TestCase |
|
|
|
|
{ |
|
|
|
|
public function testCreateUrl() |
|
|
|
|
{ |
|
|
|
|
new Application('test', __DIR__ . '/../..'); |
|
|
|
|
// default setting with '/' as base url |
|
|
|
|
$manager = new UrlManager(array( |
|
|
|
|
'baseUrl' => '/', |
|
|
|
@ -50,6 +49,7 @@ class UrlManagerTest extends \yiiunit\TestCase
|
|
|
|
|
// pretty URL with rules |
|
|
|
|
$manager = new UrlManager(array( |
|
|
|
|
'enablePrettyUrl' => true, |
|
|
|
|
'cacheID' => false, |
|
|
|
|
'rules' => array( |
|
|
|
|
array( |
|
|
|
|
'pattern' => 'post/<id>/<title>', |
|
|
|
@ -66,6 +66,7 @@ class UrlManagerTest extends \yiiunit\TestCase
|
|
|
|
|
// pretty URL with rules and suffix |
|
|
|
|
$manager = new UrlManager(array( |
|
|
|
|
'enablePrettyUrl' => true, |
|
|
|
|
'cacheID' => false, |
|
|
|
|
'rules' => array( |
|
|
|
|
array( |
|
|
|
|
'pattern' => 'post/<id>/<title>', |
|
|
|
@ -83,11 +84,118 @@ class UrlManagerTest extends \yiiunit\TestCase
|
|
|
|
|
|
|
|
|
|
public function testCreateAbsoluteUrl() |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
$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); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function testParseRequest() |
|
|
|
|
{ |
|
|
|
|
$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); |
|
|
|
|
|
|
|
|
|
// 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); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|