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 6 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 #20045: Fix type `boolean` in `MySQL` (terabytesoftw)
- Bug #20040: Fix type `boolean` in `MSSQL` (terabytesoftw)
- Bug #20005: Fix `yii\console\controllers\ServeController` to specify the router script (terabytesoftw)
Expand Down
10 changes: 6 additions & 4 deletions framework/web/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@
{
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;
ggh2e3 marked this conversation as resolved.
Show resolved Hide resolved
} elseif (!$link instanceof self) {
}

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

View check run for this annotation

Codecov / codecov/patch

framework/web/Link.php#L67

Added line #L67 was not covered by tests
else if ($link instanceof self) {
$l = array_filter((array)$link);
$links[$rel] = $l;
ggh2e3 marked this conversation as resolved.
Show resolved Hide resolved
} else {

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

View check run for this annotation

Codecov / codecov/patch

framework/web/Link.php#L71

Added line #L71 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