From 6fdb805da8a571a93fde26035d6b05dc8530d041 Mon Sep 17 00:00:00 2001 From: ggh2e3 <148446635+ggh2e3@users.noreply.github.com> Date: Thu, 23 Nov 2023 16:09:35 +0100 Subject: [PATCH] Fix #18469: Fixed `Link::serialize(array $links)` method in `yii\web\Link` --- framework/CHANGELOG.md | 2 + framework/web/Link.php | 9 ++-- tests/framework/web/LinkTest.php | 89 ++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 tests/framework/web/LinkTest.php diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 3867382321c..6645863bfd7 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -3,6 +3,8 @@ Yii Framework 2 Change Log 2.0.50 under development ------------------------ + +- Bug #18469: Fixed `Link::serialize(array $links)` method in `yii\web\Link` (ggh2e3) - Bug #20040: Fix type `boolean` in `MSSQL` (terabytesoftw) - Bug #20005: Fix `yii\console\controllers\ServeController` to specify the router script (terabytesoftw) - Bug #19060: Fix `yii\widgets\Menu` bug when using Closure for active item and adding additional tests in `tests\framework\widgets\MenuTest` (atrandafir) diff --git a/framework/web/Link.php b/framework/web/Link.php index c64c34f81c5..3f90fc66a28 100644 --- a/framework/web/Link.php +++ b/framework/web/Link.php @@ -62,11 +62,10 @@ public static function serialize(array $links) { foreach ($links as $rel => $link) { if (is_array($link)) { - foreach ($link as $i => $l) { - $link[$i] = $l instanceof self ? array_filter((array) $l) : ['href' => $l]; - } - $links[$rel] = $link; - } elseif (!$link instanceof self) { + $links[$rel] = self::serialize($link); + } elseif ($link instanceof self) { + $links[$rel] = array_filter((array)$link); + } else { $links[$rel] = ['href' => $link]; } } diff --git a/tests/framework/web/LinkTest.php b/tests/framework/web/LinkTest.php new file mode 100644 index 00000000000..6cc99b4d5a2 --- /dev/null +++ b/tests/framework/web/LinkTest.php @@ -0,0 +1,89 @@ + 'https://example.com/users/4', + 'name' => 'User 4', + 'title' => 'My Manager', + ]); + + $expected = [ + 'self' => [ + 'href' => 'https://example.com/users/1' + ], + 'manager' => [ + 'href' => 'https://example.com/users/4', + 'name' => 'User 4', + 'title' => 'My Manager', + ], + ]; + + $this->assertEquals($expected, Link::serialize([ + 'self' => 'https://example.com/users/1', + 'manager' => $managerLink, + ])); + } + + public function testSerializeNestedArrayWithLinkWillSerialize() + { + $linkData = [ + 'self' => new Link([ + 'href' => 'https://example.com/users/3', + 'name' => 'Daffy Duck', + ]), + 'fellows' => [ + [ + new Link([ + 'href' => 'https://example.com/users/4', + 'name' => 'Bugs Bunny', + ]), + ], + [ + new Link([ + 'href' => 'https://example.com/users/5', + 'name' => 'Lola Bunny', + ]), + ] + ] + ]; + + $expected = [ + 'self' => [ + 'href' => 'https://example.com/users/3', + 'name' => 'Daffy Duck', + ], + 'fellows' => [ + [ + [ + 'href' => 'https://example.com/users/4', + 'name' => 'Bugs Bunny', + ] + ], + [ + [ + 'href' => 'https://example.com/users/5', + 'name' => 'Lola Bunny', + ] + ] + ], + ]; + + $this->assertEquals($expected, Link::serialize($linkData)); + } +}