From 0183e95e093e55b18f405c88bb7c9bba1e26aa8d Mon Sep 17 00:00:00 2001 From: Giorgio Costantino Date: Fri, 20 Oct 2023 12:28:08 +0200 Subject: [PATCH 1/5] Bug #18469: fix Link::serialize method --- framework/CHANGELOG.md | 1 + framework/web/Link.php | 6 +- tests/framework/web/LinkTest.php | 105 +++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 tests/framework/web/LinkTest.php diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index f039bec955b..3d5a7d1ac1f 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 Change Log 2.0.50 under development ------------------------ +- Bug #18469: Fixed `Link::serialize(array $links)` method in `yii\web\Link` - Bug #13920: Fixed erroneous validation for specific cases (tim-fischer-maschinensucher) - Bug #19927: Fixed `console\controllers\MessageController` when saving translations to database: fixed FK error when adding new string and language at the same time, checking/regenerating all missing messages and dropping messages for unused languages (atrandafir) - Bug #20002: Fixed superfluous query on HEAD request in serializer (xicond) diff --git a/framework/web/Link.php b/framework/web/Link.php index c64c34f81c5..09953cc8a62 100644 --- a/framework/web/Link.php +++ b/framework/web/Link.php @@ -66,7 +66,11 @@ public static function serialize(array $links) $link[$i] = $l instanceof self ? array_filter((array) $l) : ['href' => $l]; } $links[$rel] = $link; - } elseif (!$link instanceof self) { + } + if ($link instanceof self) { + $l = array_filter((array)$link); + $links[$rel] = $l; + } 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..dc975ebf6aa --- /dev/null +++ b/tests/framework/web/LinkTest.php @@ -0,0 +1,105 @@ +assertEquals([ + 'link' => [ + 'href' => 'http://example.com/users/4' + ], + 'title' => [ + 'href' => 'My user', + ] + ], Link::serialize([ + 'link' => 'http://example.com/users/4', + 'title' => 'My user', + ])); + } + + public function testSerializeArrayWithLinkSuccessfully() + { + $link = new UserLink([ + 'link' => 'http://example.com/users/4', + 'title' => 'User 4', + ]); + + $this->assertEquals([ + 'link serialized' => [ + 'title' => 'User 4', + 'link' => 'http://example.com/users/4' + ], + 'title' => ['href' => 'My user'], + ], Link::serialize([ + 'link serialized' => $link, + 'title' => 'My user', + ])); + } + + public function testSerializeNestedArrayWithLinkSuccessfully() + { + $link = new UserLink([ + 'link' => 'http://example.com/users/4', + 'title' => 'User 4', + ]); + + $this->assertEquals([ + 'link serialized' => [ + 'href' => [ + 'manager' => [ + 'title' => 'User 4', + 'link' => 'http://example.com/users/4' + ]] + ], + 'title' => ['href' => 'My user'], + ], + Link::serialize([ + 'link serialized' => [ + 'manager' => $link + ], + 'title' => 'My user', + ])); + } + + public function testSerializeArrayWithNotaLinkClassesSuccessfully() + { + $notALink = new NotALink([ + 'fakeName' => 'John', + ]); + + $this->assertEquals([ + 'this is not a link' => ['href' => $notALink], + ], Link::serialize([ + 'this is not a link' => $notALink, + ])); + } +} + +class UserLink extends Link +{ + /** @var string */ + public $link; + + /** @var string */ + public $title; +} + +class NotALink extends BaseObject +{ + /** @var string */ + public $fakeName; +} From 7cf5ebf50969e3be18d034e660a3d0e0d1f90d81 Mon Sep 17 00:00:00 2001 From: Giorgio Costantino Date: Fri, 20 Oct 2023 12:55:47 +0200 Subject: [PATCH 2/5] Bug #18469: fix changelog pattern --- framework/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 3d5a7d1ac1f..b3c8f67ee0d 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,7 +4,7 @@ Yii Framework 2 Change Log 2.0.50 under development ------------------------ -- Bug #18469: Fixed `Link::serialize(array $links)` method in `yii\web\Link` +- Bug #18469: Fixed `Link::serialize(array $links)` method in `yii\web\Link` (ggh2e3) - Bug #13920: Fixed erroneous validation for specific cases (tim-fischer-maschinensucher) - Bug #19927: Fixed `console\controllers\MessageController` when saving translations to database: fixed FK error when adding new string and language at the same time, checking/regenerating all missing messages and dropping messages for unused languages (atrandafir) - Bug #20002: Fixed superfluous query on HEAD request in serializer (xicond) From 9c430852b25085c90c0c5128b5c6e0476f0550ea Mon Sep 17 00:00:00 2001 From: Giorgio Costantino Date: Fri, 20 Oct 2023 13:00:37 +0200 Subject: [PATCH 3/5] Bug #18469: removed space at the end of changelog --- framework/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index b3c8f67ee0d..26ad29e15cf 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,7 +4,7 @@ Yii Framework 2 Change Log 2.0.50 under development ------------------------ -- Bug #18469: Fixed `Link::serialize(array $links)` method in `yii\web\Link` (ggh2e3) +- Bug #18469: Fixed `Link::serialize(array $links)` method in `yii\web\Link` (ggh2e3) - Bug #13920: Fixed erroneous validation for specific cases (tim-fischer-maschinensucher) - Bug #19927: Fixed `console\controllers\MessageController` when saving translations to database: fixed FK error when adding new string and language at the same time, checking/regenerating all missing messages and dropping messages for unused languages (atrandafir) - Bug #20002: Fixed superfluous query on HEAD request in serializer (xicond) From bf84616a37fcb0a5ac48c16e3cbd2a656cbe0841 Mon Sep 17 00:00:00 2001 From: Giorgio Costantino Date: Tue, 31 Oct 2023 16:44:55 +0100 Subject: [PATCH 4/5] Issue #18469: fix tests --- framework/web/Link.php | 6 +- tests/framework/web/LinkTest.php | 132 ++++++++++++++----------------- 2 files changed, 60 insertions(+), 78 deletions(-) diff --git a/framework/web/Link.php b/framework/web/Link.php index 09953cc8a62..d59dbe2b2ad 100644 --- a/framework/web/Link.php +++ b/framework/web/Link.php @@ -62,12 +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]; - } + $link = self::serialize($link); $links[$rel] = $link; } - if ($link instanceof self) { + else if ($link instanceof self) { $l = array_filter((array)$link); $links[$rel] = $l; } else { diff --git a/tests/framework/web/LinkTest.php b/tests/framework/web/LinkTest.php index dc975ebf6aa..6cc99b4d5a2 100644 --- a/tests/framework/web/LinkTest.php +++ b/tests/framework/web/LinkTest.php @@ -7,7 +7,6 @@ namespace yiiunit\framework\web; -use yii\base\BaseObject; use yii\web\Link; use yiiunit\TestCase; @@ -16,90 +15,75 @@ */ class LinkTest extends TestCase { - public function testSerializeSimpleArraySuccessfully() + public function testSerializeLinkInSimpleArrayWillRemoveNotSetValues() { - $this->assertEquals([ - 'link' => [ - 'href' => 'http://example.com/users/4' - ], - 'title' => [ - 'href' => 'My user', - ] - ], Link::serialize([ - 'link' => 'http://example.com/users/4', - 'title' => 'My user', - ])); - } - - public function testSerializeArrayWithLinkSuccessfully() - { - $link = new UserLink([ - 'link' => 'http://example.com/users/4', - 'title' => 'User 4', + $managerLink = new Link([ + 'href' => 'https://example.com/users/4', + 'name' => 'User 4', + 'title' => 'My Manager', ]); - $this->assertEquals([ - 'link serialized' => [ - 'title' => 'User 4', - 'link' => 'http://example.com/users/4' + $expected = [ + 'self' => [ + 'href' => 'https://example.com/users/1' ], - 'title' => ['href' => 'My user'], - ], Link::serialize([ - 'link serialized' => $link, - 'title' => 'My user', + '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 testSerializeNestedArrayWithLinkSuccessfully() + public function testSerializeNestedArrayWithLinkWillSerialize() { - $link = new UserLink([ - 'link' => 'http://example.com/users/4', - 'title' => 'User 4', - ]); + $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', + ]), + ] + ] + ]; - $this->assertEquals([ - 'link serialized' => [ - 'href' => [ - 'manager' => [ - 'title' => 'User 4', - 'link' => 'http://example.com/users/4' - ]] + $expected = [ + 'self' => [ + 'href' => 'https://example.com/users/3', + 'name' => 'Daffy Duck', ], - 'title' => ['href' => 'My user'], - ], - Link::serialize([ - 'link serialized' => [ - 'manager' => $link + 'fellows' => [ + [ + [ + 'href' => 'https://example.com/users/4', + 'name' => 'Bugs Bunny', + ] ], - 'title' => 'My user', - ])); - } - - public function testSerializeArrayWithNotaLinkClassesSuccessfully() - { - $notALink = new NotALink([ - 'fakeName' => 'John', - ]); + [ + [ + 'href' => 'https://example.com/users/5', + 'name' => 'Lola Bunny', + ] + ] + ], + ]; - $this->assertEquals([ - 'this is not a link' => ['href' => $notALink], - ], Link::serialize([ - 'this is not a link' => $notALink, - ])); + $this->assertEquals($expected, Link::serialize($linkData)); } } - -class UserLink extends Link -{ - /** @var string */ - public $link; - - /** @var string */ - public $title; -} - -class NotALink extends BaseObject -{ - /** @var string */ - public $fakeName; -} From 972683d02b6f7f775883d2d6b6fc4f8ef9bc2105 Mon Sep 17 00:00:00 2001 From: ggh2e3 <148446635+ggh2e3@users.noreply.github.com> Date: Fri, 17 Nov 2023 16:22:56 +0100 Subject: [PATCH 5/5] bug #18469: apply suggestions from code review Co-authored-by: Bizley --- framework/web/Link.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/framework/web/Link.php b/framework/web/Link.php index d59dbe2b2ad..3f90fc66a28 100644 --- a/framework/web/Link.php +++ b/framework/web/Link.php @@ -62,12 +62,9 @@ public static function serialize(array $links) { foreach ($links as $rel => $link) { if (is_array($link)) { - $link = self::serialize($link); - $links[$rel] = $link; - } - else if ($link instanceof self) { - $l = array_filter((array)$link); - $links[$rel] = $l; + $links[$rel] = self::serialize($link); + } elseif ($link instanceof self) { + $links[$rel] = array_filter((array)$link); } else { $links[$rel] = ['href' => $link]; }