Skip to content

Commit

Permalink
Fix #18469: Fixed Link::serialize(array $links) method in `yii\web\…
Browse files Browse the repository at this point in the history
…Link`
  • Loading branch information
ggh2e3 authored Nov 23, 2023
1 parent 01f3983 commit 6fdb805
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 5 deletions.
2 changes: 2 additions & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions framework/web/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
Expand Down
89 changes: 89 additions & 0 deletions tests/framework/web/LinkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?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\web\Link;
use yiiunit\TestCase;

/**
* @group web
*/
class LinkTest extends TestCase
{
public function testSerializeLinkInSimpleArrayWillRemoveNotSetValues()
{
$managerLink = new Link([
'href' => '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));
}
}

0 comments on commit 6fdb805

Please sign in to comment.