From 01ff1f65936f005fa43de22d36284d538941b071 Mon Sep 17 00:00:00 2001 From: IceJOKER Date: Sat, 2 Dec 2017 08:26:55 +0300 Subject: [PATCH] Fixes #15142: Fixed array params replacing in `yii\helpers\BaseUrl::current()` --- framework/CHANGELOG.md | 1 + framework/helpers/BaseUrl.php | 2 +- tests/framework/helpers/UrlTest.php | 24 ++++++++++++++++-------- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index db405e2817..e3d3a290b1 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 Change Log 2.0.14 under development ------------------------ +- Bug #15142: Fixed array params replacing in `yii\helpers\BaseUrl::current()` (IceJOKER) - Bug #15249: Controllers in subdirectories were not visible in commands list (IceJOKER) - Enh #5515: Added default value for `yii\behaviors\BlameableBehavior` for cases when the user is guest (dmirogin) - Bug #14276: Fixed I18N format with dotted parameters (developeruz) diff --git a/framework/helpers/BaseUrl.php b/framework/helpers/BaseUrl.php index 13789c3cd3..d26951b6b6 100644 --- a/framework/helpers/BaseUrl.php +++ b/framework/helpers/BaseUrl.php @@ -429,7 +429,7 @@ class BaseUrl { $currentParams = Yii::$app->getRequest()->getQueryParams(); $currentParams[0] = '/' . Yii::$app->controller->getRoute(); - $route = array_replace($currentParams, $params); + $route = array_replace_recursive($currentParams, $params); return static::toRoute($route, $scheme); } diff --git a/tests/framework/helpers/UrlTest.php b/tests/framework/helpers/UrlTest.php index 97c5bf8e7b..a9e6e91e33 100644 --- a/tests/framework/helpers/UrlTest.php +++ b/tests/framework/helpers/UrlTest.php @@ -12,7 +12,6 @@ use yii\base\Action; use yii\base\Module; use yii\helpers\Url; use yii\web\Controller; -use yii\web\UrlManager; use yii\widgets\Menu; use yiiunit\framework\filters\stubs\UserIdentity; use yiiunit\TestCase; @@ -122,14 +121,23 @@ class UrlTest extends TestCase { $this->mockAction('page', 'view', null, []); Yii::$app->request->setQueryParams(['id' => 10, 'name' => 'test', 10 => 0]); + $uri = '/base/index.php?r=page%2Fview'; - $this->assertEquals('/base/index.php?r=page%2Fview&id=10&name=test&10=0', Url::current()); - $this->assertEquals('/base/index.php?r=page%2Fview&id=20&name=test&10=0', Url::current(['id' => 20])); - $this->assertEquals('/base/index.php?r=page%2Fview&name=test&10=0', Url::current(['id' => null])); - $this->assertEquals('/base/index.php?r=page%2Fview&name=test&10=0&1=yes', Url::current(['id' => [], 1 => 'yes'])); - $this->assertEquals('/base/index.php?r=page%2Fview&name=test&10=0', Url::current(['id' => []])); - $this->assertEquals('/base/index.php?r=page%2Fview&name=test', Url::current(['id' => null, 10 => null])); - $this->assertEquals('/base/index.php?r=page%2Fview&name=test&1=yes', Url::current(['id' => null, 10 => null, 1 => 'yes'])); + $this->assertEquals($uri . '&id=10&name=test&10=0', Url::current()); + $this->assertEquals($uri . '&id=20&name=test&10=0', Url::current(['id' => 20])); + $this->assertEquals($uri . '&name=test&10=0', Url::current(['id' => null])); + $this->assertEquals($uri . '&name=test&10=0&1=yes', Url::current(['id' => [], 1 => 'yes'])); + $this->assertEquals($uri . '&name=test&10=0', Url::current(['id' => []])); + $this->assertEquals($uri . '&name=test', Url::current(['id' => null, 10 => null])); + $this->assertEquals($uri . '&name=test&1=yes', Url::current(['id' => null, 10 => null, 1 => 'yes'])); + + $params = ['arr' => ['attr_one' => 1, 'attr_two' => 2]]; + Yii::$app->request->setQueryParams($params); + + $this->assertEquals($uri . '&arr%5Battr_one%5D=1&arr%5Battr_two%5D=2', Url::current()); + $this->assertEquals($uri, Url::current(['arr' => null])); + $this->assertEquals($uri . '&arr%5Battr_two%5D=2', Url::current(['arr' => ['attr_one' => null]])); + $this->assertEquals($uri . '&arr%5Battr_one%5D=1&arr%5Battr_two%5D=two', Url::current(['arr' => ['attr_two' => 'two']])); } public function testPrevious()