You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

659 lines
16 KiB

12 years ago
<?php
namespace yiiunit\framework\web;
12 years ago
use yii\web\UrlManager;
12 years ago
use yii\web\UrlRule;
use yii\web\Request;
use yiiunit\TestCase;
12 years ago
/**
* @group web
*/
class UrlRuleTest extends TestCase
12 years ago
{
public function testCreateUrl()
{
$manager = new UrlManager(['cache' => null]);
12 years ago
$suites = $this->getTestsForCreateUrl();
foreach ($suites as $i => $suite) {
list ($name, $config, $tests) = $suite;
$rule = new UrlRule($config);
foreach ($tests as $j => $test) {
list ($route, $params, $expected) = $test;
12 years ago
$url = $rule->createUrl($manager, $route, $params);
12 years ago
$this->assertEquals($expected, $url, "Test#$i-$j: $name");
}
}
}
public function testParseRequest()
12 years ago
{
$manager = new UrlManager(['cache' => null]);
$request = new Request(['hostInfo' => 'http://en.example.com']);
$suites = $this->getTestsForParseRequest();
12 years ago
foreach ($suites as $i => $suite) {
list ($name, $config, $tests) = $suite;
$rule = new UrlRule($config);
foreach ($tests as $j => $test) {
$request->pathInfo = $test[0];
12 years ago
$route = $test[1];
$params = isset($test[2]) ? $test[2] : [];
$result = $rule->parseRequest($manager, $request);
12 years ago
if ($route === false) {
$this->assertFalse($result, "Test#$i-$j: $name");
} else {
$this->assertEquals([$route, $params], $result, "Test#$i-$j: $name");
12 years ago
}
}
}
}
protected function getTestsForCreateUrl()
{
// structure of each test
// message for the test
// config for the URL rule
// list of inputs and outputs
// route
// params
// expected output
return [
[
12 years ago
'empty pattern',
[
12 years ago
'pattern' => '',
'route' => 'post/index',
],
[
['post/index', [], ''],
['comment/index', [], false],
['post/index', ['page' => 1], '?page=1'],
],
],
[
12 years ago
'without param',
[
12 years ago
'pattern' => 'posts',
'route' => 'post/index',
],
[
['post/index', [], 'posts'],
['comment/index', [], false],
['post/index', ['page' => 1], 'posts?page=1'],
],
],
[
12 years ago
'parsing only',
[
12 years ago
'pattern' => 'posts',
'route' => 'post/index',
'mode' => UrlRule::PARSING_ONLY,
],
[
['post/index', [], false],
],
],
[
12 years ago
'with param',
[
12 years ago
'pattern' => 'post/<page>',
'route' => 'post/index',
],
[
['post/index', [], false],
['comment/index', [], false],
['post/index', ['page' => 1], 'post/1'],
['post/index', ['page' => 1, 'tag' => 'a'], 'post/1?tag=a'],
],
],
[
12 years ago
'with param requirement',
[
12 years ago
'pattern' => 'post/<page:\d+>',
'route' => 'post/index',
],
[
['post/index', ['page' => 'abc'], false],
['post/index', ['page' => 1], 'post/1'],
['post/index', ['page' => 1, 'tag' => 'a'], 'post/1?tag=a'],
],
],
[
12 years ago
'with multiple params',
[
12 years ago
'pattern' => 'post/<page:\d+>-<tag>',
'route' => 'post/index',
],
[
['post/index', ['page' => '1abc'], false],
['post/index', ['page' => 1], false],
['post/index', ['page' => 1, 'tag' => 'a'], 'post/1-a'],
],
],
[
12 years ago
'with optional param',
[
12 years ago
'pattern' => 'post/<page:\d+>/<tag>',
'route' => 'post/index',
'defaults' => ['page' => 1],
],
[
['post/index', ['page' => 1], false],
['post/index', ['page' => '1abc', 'tag' => 'a'], false],
['post/index', ['page' => 1, 'tag' => 'a'], 'post/a'],
['post/index', ['page' => 2, 'tag' => 'a'], 'post/2/a'],
],
],
[
12 years ago
'with optional param not in pattern',
[
12 years ago
'pattern' => 'post/<tag>',
'route' => 'post/index',
'defaults' => ['page' => 1],
],
[
['post/index', ['page' => 1], false],
['post/index', ['page' => '1abc', 'tag' => 'a'], false],
['post/index', ['page' => 2, 'tag' => 'a'], false],
['post/index', ['page' => 1, 'tag' => 'a'], 'post/a'],
],
],
[
12 years ago
'multiple optional params',
[
12 years ago
'pattern' => 'post/<page:\d+>/<tag>/<sort:yes|no>',
'route' => 'post/index',
'defaults' => ['page' => 1, 'sort' => 'yes'],
],
[
['post/index', ['page' => 1], false],
['post/index', ['page' => '1abc', 'tag' => 'a'], false],
['post/index', ['page' => 1, 'tag' => 'a', 'sort' => 'YES'], false],
['post/index', ['page' => 1, 'tag' => 'a', 'sort' => 'yes'], 'post/a'],
['post/index', ['page' => 2, 'tag' => 'a', 'sort' => 'yes'], 'post/2/a'],
['post/index', ['page' => 2, 'tag' => 'a', 'sort' => 'no'], 'post/2/a/no'],
['post/index', ['page' => 1, 'tag' => 'a', 'sort' => 'no'], 'post/a/no'],
],
],
[
12 years ago
'optional param and required param separated by dashes',
[
12 years ago
'pattern' => 'post/<page:\d+>-<tag>',
'route' => 'post/index',
'defaults' => ['page' => 1],
],
[
['post/index', ['page' => 1], false],
['post/index', ['page' => '1abc', 'tag' => 'a'], false],
['post/index', ['page' => 1, 'tag' => 'a'], 'post/-a'],
['post/index', ['page' => 2, 'tag' => 'a'], 'post/2-a'],
],
],
[
12 years ago
'optional param at the end',
[
12 years ago
'pattern' => 'post/<tag>/<page:\d+>',
'route' => 'post/index',
'defaults' => ['page' => 1],
],
[
['post/index', ['page' => 1], false],
['post/index', ['page' => '1abc', 'tag' => 'a'], false],
['post/index', ['page' => 1, 'tag' => 'a'], 'post/a'],
['post/index', ['page' => 2, 'tag' => 'a'], 'post/a/2'],
],
],
[
12 years ago
'consecutive optional params',
[
12 years ago
'pattern' => 'post/<page:\d+>/<tag>',
'route' => 'post/index',
'defaults' => ['page' => 1, 'tag' => 'a'],
],
[
['post/index', ['page' => 1], false],
['post/index', ['page' => '1abc', 'tag' => 'a'], false],
['post/index', ['page' => 1, 'tag' => 'a'], 'post'],
['post/index', ['page' => 2, 'tag' => 'a'], 'post/2'],
['post/index', ['page' => 1, 'tag' => 'b'], 'post/b'],
['post/index', ['page' => 2, 'tag' => 'b'], 'post/2/b'],
],
],
[
12 years ago
'consecutive optional params separated by dash',
[
12 years ago
'pattern' => 'post/<page:\d+>-<tag>',
'route' => 'post/index',
'defaults' => ['page' => 1, 'tag' => 'a'],
],
[
['post/index', ['page' => 1], false],
['post/index', ['page' => '1abc', 'tag' => 'a'], false],
['post/index', ['page' => 1, 'tag' => 'a'], 'post/-'],
['post/index', ['page' => 1, 'tag' => 'b'], 'post/-b'],
['post/index', ['page' => 2, 'tag' => 'a'], 'post/2-'],
['post/index', ['page' => 2, 'tag' => 'b'], 'post/2-b'],
],
],
[
12 years ago
'route has parameters',
[
12 years ago
'pattern' => '<controller>/<action>',
'route' => '<controller>/<action>',
'defaults' => [],
],
[
['post/index', ['page' => 1], 'post/index?page=1'],
['module/post/index', [], false],
],
],
[
12 years ago
'route has parameters with regex',
[
12 years ago
'pattern' => '<controller:post|comment>/<action>',
'route' => '<controller>/<action>',
'defaults' => [],
],
[
['post/index', ['page' => 1], 'post/index?page=1'],
['comment/index', ['page' => 1], 'comment/index?page=1'],
['test/index', ['page' => 1], false],
['post', [], false],
['module/post/index', [], false],
['post/index', ['controller' => 'comment'], 'post/index?controller=comment'],
],
],
[
12 years ago
'route has default parameter',
[
12 years ago
'pattern' => '<controller:post|comment>/<action>',
'route' => '<controller>/<action>',
'defaults' => ['action' => 'index'],
],
[
['post/view', ['page' => 1], 'post/view?page=1'],
['comment/view', ['page' => 1], 'comment/view?page=1'],
['test/view', ['page' => 1], false],
['test/index', ['page' => 1], false],
['post/index', ['page' => 1], 'post?page=1'],
],
],
[
12 years ago
'empty pattern with suffix',
[
12 years ago
'pattern' => '',
'route' => 'post/index',
'suffix' => '.html',
],
[
['post/index', [], ''],
['comment/index', [], false],
['post/index', ['page' => 1], '?page=1'],
],
],
[
12 years ago
'regular pattern with suffix',
[
12 years ago
'pattern' => 'posts',
'route' => 'post/index',
'suffix' => '.html',
],
[
['post/index', [], 'posts.html'],
['comment/index', [], false],
['post/index', ['page' => 1], 'posts.html?page=1'],
],
],
[
12 years ago
'empty pattern with slash suffix',
[
12 years ago
'pattern' => '',
'route' => 'post/index',
'suffix' => '/',
],
[
['post/index', [], ''],
['comment/index', [], false],
['post/index', ['page' => 1], '?page=1'],
],
],
[
12 years ago
'regular pattern with slash suffix',
[
12 years ago
'pattern' => 'posts',
'route' => 'post/index',
'suffix' => '/',
],
[
['post/index', [], 'posts/'],
['comment/index', [], false],
['post/index', ['page' => 1], 'posts/?page=1'],
],
],
[
'with host info',
[
'pattern' => 'post/<page:\d+>/<tag>',
'route' => 'post/index',
'defaults' => ['page' => 1],
'host' => 'http://<lang:en|fr>.example.com',
],
[
['post/index', ['page' => 1, 'tag' => 'a'], false],
['post/index', ['page' => 1, 'tag' => 'a', 'lang' => 'en'], 'http://en.example.com/post/a'],
],
],
];
12 years ago
}
protected function getTestsForParseRequest()
12 years ago
{
// structure of each test
// message for the test
// config for the URL rule
// list of inputs and outputs
// pathInfo
// expected route, or false if the rule doesn't apply
// expected params, or not set if empty
return [
[
12 years ago
'empty pattern',
[
12 years ago
'pattern' => '',
'route' => 'post/index',
],
[
['', 'post/index'],
['a', false],
],
],
[
12 years ago
'without param',
[
12 years ago
'pattern' => 'posts',
'route' => 'post/index',
],
[
['posts', 'post/index'],
['a', false],
],
],
[
'with dot', // https://github.com/yiisoft/yii/issues/2945
[
'pattern' => 'posts.html',
'route' => 'post/index',
],
[
['posts.html', 'post/index'],
['postsahtml', false],
],
],
[
12 years ago
'creation only',
[
12 years ago
'pattern' => 'posts',
'route' => 'post/index',
'mode' => UrlRule::CREATION_ONLY,
],
[
['posts', false],
],
],
[
12 years ago
'with param',
[
12 years ago
'pattern' => 'post/<page>',
'route' => 'post/index',
],
[
['post/1', 'post/index', ['page' => '1']],
['post/a', 'post/index', ['page' => 'a']],
['post', false],
['posts', false],
],
],
[
12 years ago
'with param requirement',
[
12 years ago
'pattern' => 'post/<page:\d+>',
'route' => 'post/index',
],
[
['post/1', 'post/index', ['page' => '1']],
['post/a', false],
['post/1/a', false],
],
],
[
12 years ago
'with multiple params',
[
12 years ago
'pattern' => 'post/<page:\d+>-<tag>',
'route' => 'post/index',
],
[
['post/1-a', 'post/index', ['page' => '1', 'tag' => 'a']],
['post/a', false],
['post/1', false],
['post/1/a', false],
],
],
[
12 years ago
'with optional param',
[
12 years ago
'pattern' => 'post/<page:\d+>/<tag>',
'route' => 'post/index',
'defaults' => ['page' => 1],
],
[
['post/1/a', 'post/index', ['page' => '1', 'tag' => 'a']],
['post/2/a', 'post/index', ['page' => '2', 'tag' => 'a']],
['post/a', 'post/index', ['page' => '1', 'tag' => 'a']],
['post/1', 'post/index', ['page' => '1', 'tag' => '1']],
],
],
[
12 years ago
'with optional param not in pattern',
[
12 years ago
'pattern' => 'post/<tag>',
'route' => 'post/index',
'defaults' => ['page' => 1],
],
[
['post/a', 'post/index', ['page' => '1', 'tag' => 'a']],
['post/1', 'post/index', ['page' => '1', 'tag' => '1']],
['post', false],
],
],
[
12 years ago
'multiple optional params',
[
12 years ago
'pattern' => 'post/<page:\d+>/<tag>/<sort:yes|no>',
'route' => 'post/index',
'defaults' => ['page' => 1, 'sort' => 'yes'],
],
[
['post/1/a/yes', 'post/index', ['page' => '1', 'tag' => 'a', 'sort' => 'yes']],
['post/1/a/no', 'post/index', ['page' => '1', 'tag' => 'a', 'sort' => 'no']],
['post/2/a/no', 'post/index', ['page' => '2', 'tag' => 'a', 'sort' => 'no']],
['post/2/a', 'post/index', ['page' => '2', 'tag' => 'a', 'sort' => 'yes']],
['post/a/no', 'post/index', ['page' => '1', 'tag' => 'a', 'sort' => 'no']],
['post/a', 'post/index', ['page' => '1', 'tag' => 'a', 'sort' => 'yes']],
['post', false],
],
],
[
12 years ago
'optional param and required param separated by dashes',
[
12 years ago
'pattern' => 'post/<page:\d+>-<tag>',
'route' => 'post/index',
'defaults' => ['page' => 1],
],
[
['post/1-a', 'post/index', ['page' => '1', 'tag' => 'a']],
['post/2-a', 'post/index', ['page' => '2', 'tag' => 'a']],
['post/-a', 'post/index', ['page' => '1', 'tag' => 'a']],
['post/a', false],
['post-a', false],
],
],
[
12 years ago
'optional param at the end',
[
12 years ago
'pattern' => 'post/<tag>/<page:\d+>',
'route' => 'post/index',
'defaults' => ['page' => 1],
],
[
['post/a/1', 'post/index', ['page' => '1', 'tag' => 'a']],
['post/a/2', 'post/index', ['page' => '2', 'tag' => 'a']],
['post/a', 'post/index', ['page' => '1', 'tag' => 'a']],
['post/2', 'post/index', ['page' => '1', 'tag' => '2']],
['post', false],
],
],
[
12 years ago
'consecutive optional params',
[
12 years ago
'pattern' => 'post/<page:\d+>/<tag>',
'route' => 'post/index',
'defaults' => ['page' => 1, 'tag' => 'a'],
],
[
['post/2/b', 'post/index', ['page' => '2', 'tag' => 'b']],
['post/2', 'post/index', ['page' => '2', 'tag' => 'a']],
['post', 'post/index', ['page' => '1', 'tag' => 'a']],
['post/b', 'post/index', ['page' => '1', 'tag' => 'b']],
['post//b', false],
],
],
[
12 years ago
'consecutive optional params separated by dash',
[
12 years ago
'pattern' => 'post/<page:\d+>-<tag>',
'route' => 'post/index',
'defaults' => ['page' => 1, 'tag' => 'a'],
],
[
['post/2-b', 'post/index', ['page' => '2', 'tag' => 'b']],
['post/2-', 'post/index', ['page' => '2', 'tag' => 'a']],
['post/-b', 'post/index', ['page' => '1', 'tag' => 'b']],
['post/-', 'post/index', ['page' => '1', 'tag' => 'a']],
['post', false],
],
],
[
12 years ago
'route has parameters',
[
12 years ago
'pattern' => '<controller>/<action>',
'route' => '<controller>/<action>',
'defaults' => [],
],
[
['post/index', 'post/index'],
['module/post/index', false],
],
],
[
12 years ago
'route has parameters with regex',
[
12 years ago
'pattern' => '<controller:post|comment>/<action>',
'route' => '<controller>/<action>',
'defaults' => [],
],
[
['post/index', 'post/index'],
['comment/index', 'comment/index'],
['test/index', false],
['post', false],
['module/post/index', false],
],
],
[
12 years ago
'route has default parameter',
[
12 years ago
'pattern' => '<controller:post|comment>/<action>',
'route' => '<controller>/<action>',
'defaults' => ['action' => 'index'],
],
[
['post/view', 'post/view'],
['comment/view', 'comment/view'],
['test/view', false],
['post', 'post/index'],
['posts', false],
['test', false],
['index', false],
],
],
[
12 years ago
'empty pattern with suffix',
[
12 years ago
'pattern' => '',
'route' => 'post/index',
'suffix' => '.html',
],
[
['', 'post/index'],
['.html', false],
['a.html', false],
],
],
[
12 years ago
'regular pattern with suffix',
[
12 years ago
'pattern' => 'posts',
'route' => 'post/index',
'suffix' => '.html',
],
[
['posts.html', 'post/index'],
['posts', false],
['posts.HTML', false],
['a.html', false],
['a', false],
],
],
[
12 years ago
'empty pattern with slash suffix',
[
12 years ago
'pattern' => '',
'route' => 'post/index',
'suffix' => '/',
],
[
['', 'post/index'],
['a', false],
],
],
[
12 years ago
'regular pattern with slash suffix',
[
12 years ago
'pattern' => 'posts',
'route' => 'post/index',
'suffix' => '/',
],
[
['posts/', 'post/index'],
['posts', false],
['a', false],
],
],
[
'with host info',
[
'pattern' => 'post/<page:\d+>',
'route' => 'post/index',
'host' => 'http://<lang:en|fr>.example.com',
],
[
['post/1', 'post/index', ['page' => '1', 'lang' => 'en']],
['post/a', false],
['post/1/a', false],
],
],
];
12 years ago
}
}