Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug #18469: fix Link::serialize method #20020

Merged
merged 8 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` (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)
Expand Down
6 changes: 5 additions & 1 deletion framework/web/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's refactor this method to a recursive call instead.

Copy link
Contributor Author

@ggh2e3 ggh2e3 Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you referring to line 66(or maybe the whole method)?
I'd replace the second loop with this recursive call $link = self::serialize($link); in order to remove it

$l = array_filter((array)$link);
$links[$rel] = $l;
} else {
$links[$rel] = ['href' => $link];
}
}
Expand Down
105 changes: 105 additions & 0 deletions tests/framework/web/LinkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/

namespace yiiunit\framework\web;

use yii\base\BaseObject;
use yii\web\Link;
use yiiunit\TestCase;

/**
* @group web
*/
class LinkTest extends TestCase
{
public function testSerializeSimpleArraySuccessfully()
{
$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',
]);

$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;
}
Loading