Skip to content

Commit 6fdb805

Browse files
authored
Fix #18469: Fixed Link::serialize(array $links) method in yii\web\Link
1 parent 01f3983 commit 6fdb805

File tree

3 files changed

+95
-5
lines changed

3 files changed

+95
-5
lines changed

framework/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Yii Framework 2 Change Log
33

44
2.0.50 under development
55
------------------------
6+
7+
- Bug #18469: Fixed `Link::serialize(array $links)` method in `yii\web\Link` (ggh2e3)
68
- Bug #20040: Fix type `boolean` in `MSSQL` (terabytesoftw)
79
- Bug #20005: Fix `yii\console\controllers\ServeController` to specify the router script (terabytesoftw)
810
- Bug #19060: Fix `yii\widgets\Menu` bug when using Closure for active item and adding additional tests in `tests\framework\widgets\MenuTest` (atrandafir)

framework/web/Link.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,10 @@ public static function serialize(array $links)
6262
{
6363
foreach ($links as $rel => $link) {
6464
if (is_array($link)) {
65-
foreach ($link as $i => $l) {
66-
$link[$i] = $l instanceof self ? array_filter((array) $l) : ['href' => $l];
67-
}
68-
$links[$rel] = $link;
69-
} elseif (!$link instanceof self) {
65+
$links[$rel] = self::serialize($link);
66+
} elseif ($link instanceof self) {
67+
$links[$rel] = array_filter((array)$link);
68+
} else {
7069
$links[$rel] = ['href' => $link];
7170
}
7271
}

tests/framework/web/LinkTest.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* @link https://www.yiiframework.com/
4+
* @copyright Copyright (c) 2008 Yii Software LLC
5+
* @license https://www.yiiframework.com/license/
6+
*/
7+
8+
namespace yiiunit\framework\web;
9+
10+
use yii\web\Link;
11+
use yiiunit\TestCase;
12+
13+
/**
14+
* @group web
15+
*/
16+
class LinkTest extends TestCase
17+
{
18+
public function testSerializeLinkInSimpleArrayWillRemoveNotSetValues()
19+
{
20+
$managerLink = new Link([
21+
'href' => 'https://example.com/users/4',
22+
'name' => 'User 4',
23+
'title' => 'My Manager',
24+
]);
25+
26+
$expected = [
27+
'self' => [
28+
'href' => 'https://example.com/users/1'
29+
],
30+
'manager' => [
31+
'href' => 'https://example.com/users/4',
32+
'name' => 'User 4',
33+
'title' => 'My Manager',
34+
],
35+
];
36+
37+
$this->assertEquals($expected, Link::serialize([
38+
'self' => 'https://example.com/users/1',
39+
'manager' => $managerLink,
40+
]));
41+
}
42+
43+
public function testSerializeNestedArrayWithLinkWillSerialize()
44+
{
45+
$linkData = [
46+
'self' => new Link([
47+
'href' => 'https://example.com/users/3',
48+
'name' => 'Daffy Duck',
49+
]),
50+
'fellows' => [
51+
[
52+
new Link([
53+
'href' => 'https://example.com/users/4',
54+
'name' => 'Bugs Bunny',
55+
]),
56+
],
57+
[
58+
new Link([
59+
'href' => 'https://example.com/users/5',
60+
'name' => 'Lola Bunny',
61+
]),
62+
]
63+
]
64+
];
65+
66+
$expected = [
67+
'self' => [
68+
'href' => 'https://example.com/users/3',
69+
'name' => 'Daffy Duck',
70+
],
71+
'fellows' => [
72+
[
73+
[
74+
'href' => 'https://example.com/users/4',
75+
'name' => 'Bugs Bunny',
76+
]
77+
],
78+
[
79+
[
80+
'href' => 'https://example.com/users/5',
81+
'name' => 'Lola Bunny',
82+
]
83+
]
84+
],
85+
];
86+
87+
$this->assertEquals($expected, Link::serialize($linkData));
88+
}
89+
}

0 commit comments

Comments
 (0)