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 all 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
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 @@
{
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 {

Check warning on line 68 in framework/web/Link.php

View check run for this annotation

Codecov / codecov/patch

framework/web/Link.php#L68

Added line #L68 was not covered by tests
$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));
}
}
Loading