Browse Source

Merge branch 'rob006-url-rule-defaults'

tags/2.0.11
SilverFire - Dmitry Naumenko 8 years ago
parent
commit
97cad130f3
No known key found for this signature in database
GPG Key ID: 39DD917A92B270A
  1. 1
      framework/CHANGELOG.md
  2. 11
      framework/web/UrlRule.php
  3. 59
      tests/framework/web/UrlRuleTest.php

1
framework/CHANGELOG.md

@ -59,6 +59,7 @@ Yii Framework 2 Change Log
- Enh #9053: Added`yii\grid\RadioButtonColumn` (darwinisgod)
- Enh #9162: Added support of closures in `value` for attributes in `yii\widgets\DetailView` (arogachev)
- Enh #10896: Select only primary key when counting records in UniqueValidator (developeruz)
- Enh #10970: Allow omit specifying empty default params on URL creation (rob006)
- Enh #11037: `yii.js` and `yii.validation.js` use `Regexp.test()` instead of `String.match()` (arogachev, nkovacs)
- Enh #11163: Added separate method for client-side validation options `yii\validators\Validator::getClientOptions()` (arogachev)
- Enh #11697: Added `filterHaving()`, `andFilterHaving()` and `orFilterHaving()` to `yii\db\Query` (nicdnepr, samdark)

11
framework/web/UrlRule.php

@ -367,8 +367,15 @@ class UrlRule extends Object implements UrlRuleInterface
continue;
}
if (!isset($params[$name])) {
return false;
} elseif (strcmp($params[$name], $value) === 0) { // strcmp will do string conversion automatically
// allow omit empty optional params
// @see https://github.com/yiisoft/yii2/issues/10970
if (in_array($name, $this->placeholders) && strcmp($value, '') === 0) {
$params[$name] = '';
} else {
return false;
}
}
if (strcmp($params[$name], $value) === 0) { // strcmp will do string conversion automatically
unset($params[$name]);
if (isset($this->_paramRules[$name])) {
$tr["<$name>"] = '';

59
tests/framework/web/UrlRuleTest.php

@ -31,7 +31,7 @@ class UrlRuleTest extends TestCase
foreach ($tests as $j => $test) {
list ($route, $params, $expected) = $test;
$url = $rule->createUrl($manager, $route, $params);
$this->assertEquals($expected, $url, "Test#$i-$j: $name");
$this->assertSame($expected, $url, "Test#$i-$j: $name");
}
}
}
@ -484,6 +484,63 @@ class UrlRuleTest extends TestCase
],
],
[
'optional params - example from guide',
[
'pattern' => 'posts/<page:\d+>/<tag>',
'route' => 'post/index',
'defaults' => ['page' => 1, 'tag' => ''],
],
[
['post/index', ['page' => 1, 'tag' => ''], 'posts'],
['post/index', ['page' => 2, 'tag' => ''], 'posts/2'],
['post/index', ['page' => 2, 'tag' => 'news'], 'posts/2/news'],
['post/index', ['page' => 1, 'tag' => 'news'], 'posts/news'],
// allow skip empty params on URL creation
['post/index', [], false],
['post/index', ['tag' => ''], false],
['post/index', ['page' => 1], 'posts'],
['post/index', ['page' => 2], 'posts/2'],
],
],
[
'required params',
[
'pattern' => 'about-me',
'route' => 'site/page',
'defaults' => ['id' => 1],
],
[
['site/page', ['id' => 1], 'about-me'],
['site/page', ['id' => 2], false],
],
],
[
'required default param',
[
'pattern' => '',
'route' => 'site/home',
'defaults' => ['lang' => 'en'],
],
[
['site/home', ['lang' => 'en'], ''],
['site/home', ['lang' => ''], false],
['site/home', [], false],
],
],
[
'required default empty param',
[
'pattern' => '',
'route' => 'site/home',
'defaults' => ['lang' => ''],
],
[
['site/home', ['lang' => ''], ''],
['site/home', ['lang' => 'en'], false],
['site/home', [], false],
],
],
[
'route has parameters',
[
'pattern' => '<controller>/<action>',

Loading…
Cancel
Save