Skip to content

Commit a9c8ccd

Browse files
committed
Tests: Add bookmark rendering coverage.
Cover bookmark markup and display options in `_walk_bookmarks()`, plus categorized and uncategorized output, filtering, visibility, and echo behavior in `wp_list_bookmarks()`. Developed in: #13256 Props pbearne. See #65957. git-svn-id: https://develop.svn.wordpress.org/trunk@65147 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 62884f0 commit a9c8ccd

2 files changed

Lines changed: 669 additions & 0 deletions

File tree

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
<?php
2+
/**
3+
* Test cases for _walk_bookmarks().
4+
*
5+
* @package WordPress
6+
* @subpackage UnitTests
7+
* @since 2.1.0
8+
*
9+
* @group bookmark
10+
* @group template
11+
*
12+
* @covers ::_walk_bookmarks
13+
*/
14+
class Tests_walk_bookmarks extends WP_UnitTestCase {
15+
16+
/**
17+
* Tests formatted output of a list of bookmarks with various arguments and configurations.
18+
*
19+
* @ticket 65957
20+
*
21+
* @dataProvider data_walk_bookmarks
22+
*
23+
* @param array $bookmarks_data List of bookmark field arrays to create objects for.
24+
* @param array|string $args Arguments passed to _walk_bookmarks.
25+
* @param string $expected Expected HTML output.
26+
*/
27+
public function test_walk_bookmarks( array $bookmarks_data, $args, string $expected ): void {
28+
$bookmarks = array();
29+
foreach ( $bookmarks_data as $data ) {
30+
$bookmarks[] = (object) $data;
31+
}
32+
33+
$this->assertSame( $expected, _walk_bookmarks( $bookmarks, $args ) );
34+
}
35+
36+
/**
37+
* Data provider for test_walk_bookmarks.
38+
*
39+
* @return array<string, array{
40+
* bookmarks_data: array<int, array<string, mixed>>,
41+
* args: array<string, mixed>|string,
42+
* expected: string,
43+
* }>
44+
*/
45+
public function data_walk_bookmarks(): array {
46+
return array(
47+
'empty bookmarks array' => array(
48+
'bookmarks_data' => array(),
49+
'args' => array(),
50+
'expected' => '',
51+
),
52+
'default arguments with standard bookmark' => array(
53+
'bookmarks_data' => array(
54+
array(
55+
'link_id' => 1,
56+
'link_name' => 'Example Site',
57+
'link_url' => 'https://example.org',
58+
'link_description' => 'A great website',
59+
'link_image' => '',
60+
'link_target' => '',
61+
'link_rel' => '',
62+
'link_rating' => 0,
63+
),
64+
),
65+
'args' => array(),
66+
'expected' => "<li><a href=\"https://example.org\" title=\"A great website\">Example Site</a></li>\n",
67+
),
68+
'empty link_url falls back to hash' => array(
69+
'bookmarks_data' => array(
70+
array(
71+
'link_id' => 2,
72+
'link_name' => 'No URL Link',
73+
'link_url' => '',
74+
'link_description' => '',
75+
'link_image' => '',
76+
'link_target' => '',
77+
'link_rel' => '',
78+
'link_rating' => 0,
79+
),
80+
),
81+
'args' => array(),
82+
'expected' => "<li><a href=\"#\">No URL Link</a></li>\n",
83+
),
84+
'with link_rel and link_target' => array(
85+
'bookmarks_data' => array(
86+
array(
87+
'link_id' => 3,
88+
'link_name' => 'External Link',
89+
'link_url' => 'https://example.com',
90+
'link_description' => 'External resource',
91+
'link_image' => '',
92+
'link_target' => '_blank',
93+
'link_rel' => 'nofollow noopener',
94+
'link_rating' => 0,
95+
),
96+
),
97+
'args' => array(),
98+
'expected' => "<li><a href=\"https://example.com\" rel=\"nofollow noopener\" title=\"External resource\" target=\"_blank\">External Link</a></li>\n",
99+
),
100+
'show_description and show_rating with custom between' => array(
101+
'bookmarks_data' => array(
102+
array(
103+
'link_id' => 4,
104+
'link_name' => 'Rated Link',
105+
'link_url' => 'https://example.org/rated',
106+
'link_description' => 'High quality site',
107+
'link_image' => '',
108+
'link_target' => '',
109+
'link_rel' => '',
110+
'link_rating' => 5,
111+
),
112+
),
113+
'args' => array(
114+
'show_description' => 1,
115+
'show_rating' => 1,
116+
'between' => ' - ',
117+
),
118+
'expected' => "<li><a href=\"https://example.org/rated\" title=\"High quality site\">Rated Link</a> - High quality site - 5</li>\n",
119+
),
120+
'show_images with absolute http url and show_name disabled' => array(
121+
'bookmarks_data' => array(
122+
array(
123+
'link_id' => 5,
124+
'link_name' => 'Image Link',
125+
'link_url' => 'https://example.org/img',
126+
'link_description' => 'Site with logo',
127+
'link_image' => 'https://example.org/logo.png',
128+
'link_target' => '',
129+
'link_rel' => '',
130+
'link_rating' => 0,
131+
),
132+
),
133+
'args' => array(
134+
'show_images' => 1,
135+
'show_name' => 0,
136+
),
137+
'expected' => "<li><a href=\"https://example.org/img\" title=\"Site with logo\"><img src=\"https://example.org/logo.png\" alt=\"Image Link\" title=\"Site with logo\" /></a></li>\n",
138+
),
139+
'show_images with absolute http url and show_name enabled' => array(
140+
'bookmarks_data' => array(
141+
array(
142+
'link_id' => 6,
143+
'link_name' => 'Image Link With Name',
144+
'link_url' => 'https://example.org/img2',
145+
'link_description' => '',
146+
'link_image' => 'https://example.org/logo2.png',
147+
'link_target' => '',
148+
'link_rel' => '',
149+
'link_rating' => 0,
150+
),
151+
),
152+
'args' => array(
153+
'show_images' => 1,
154+
'show_name' => 1,
155+
),
156+
'expected' => "<li><a href=\"https://example.org/img2\"><img src=\"https://example.org/logo2.png\" alt=\"Image Link With Name\" /> Image Link With Name</a></li>\n",
157+
),
158+
'show_images disabled when image exists' => array(
159+
'bookmarks_data' => array(
160+
array(
161+
'link_id' => 7,
162+
'link_name' => 'Text Only Link',
163+
'link_url' => 'https://example.org/text',
164+
'link_description' => '',
165+
'link_image' => 'https://example.org/logo.png',
166+
'link_target' => '',
167+
'link_rel' => '',
168+
'link_rating' => 0,
169+
),
170+
),
171+
'args' => array(
172+
'show_images' => 0,
173+
),
174+
'expected' => "<li><a href=\"https://example.org/text\">Text Only Link</a></li>\n",
175+
),
176+
'custom before, after, link_before, and link_after wrappers' => array(
177+
'bookmarks_data' => array(
178+
array(
179+
'link_id' => 8,
180+
'link_name' => 'Wrapped Link',
181+
'link_url' => 'https://example.org/wrapped',
182+
'link_description' => '',
183+
'link_image' => '',
184+
'link_target' => '',
185+
'link_rel' => '',
186+
'link_rating' => 0,
187+
),
188+
),
189+
'args' => array(
190+
'before' => '<div class="bookmark-item">',
191+
'after' => '</div>',
192+
'link_before' => '<span class="link-text">',
193+
'link_after' => '</span>',
194+
),
195+
'expected' => "<div class=\"bookmark-item\"><a href=\"https://example.org/wrapped\"><span class=\"link-text\">Wrapped Link</span></a></div>\n",
196+
),
197+
);
198+
}
199+
200+
/**
201+
* Tests show_images with a relative image URL.
202+
*
203+
* @ticket 65957
204+
*/
205+
public function test_walk_bookmarks_relative_image_url(): void {
206+
$bookmark = (object) array(
207+
'link_id' => 10,
208+
'link_name' => 'Relative Image Link',
209+
'link_url' => 'https://example.org',
210+
'link_description' => '',
211+
'link_image' => '/images/logo.png',
212+
'link_target' => '',
213+
'link_rel' => '',
214+
'link_rating' => 0,
215+
);
216+
217+
$expected = '<li><a href="https://example.org"><img src="' . get_option( 'siteurl' ) . '/images/logo.png" alt="Relative Image Link" /></a></li>' . "\n";
218+
219+
$this->assertSame( $expected, _walk_bookmarks( array( $bookmark ), array( 'show_images' => 1 ) ) );
220+
}
221+
222+
/**
223+
* Tests show_updated functionality with recently_updated bookmarks.
224+
*
225+
* @ticket 65957
226+
*/
227+
public function test_walk_bookmarks_show_updated(): void {
228+
$timestamp = strtotime( '2026-08-24 12:00:00' );
229+
230+
$bookmark_recent = (object) array(
231+
'link_id' => 11,
232+
'link_name' => 'Recent Link',
233+
'link_url' => 'https://example.org/recent',
234+
'link_description' => 'Description',
235+
'link_image' => '',
236+
'link_target' => '',
237+
'link_rel' => '',
238+
'link_rating' => 0,
239+
'recently_updated' => true,
240+
'link_updated_f' => (string) $timestamp,
241+
);
242+
243+
$bookmark_not_recent = (object) array(
244+
'link_id' => 12,
245+
'link_name' => 'Old Link',
246+
'link_url' => 'https://example.org/old',
247+
'link_description' => '',
248+
'link_image' => '',
249+
'link_target' => '',
250+
'link_rel' => '',
251+
'link_rating' => 0,
252+
'recently_updated' => false,
253+
'link_updated_f' => '0000-00-00 00:00:00',
254+
);
255+
256+
$date_format = get_option( 'links_updated_date_format' );
257+
$gmt_offset = (int) ( (float) get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
258+
$expected_date = gmdate( $date_format, $timestamp + $gmt_offset );
259+
260+
$output = _walk_bookmarks(
261+
array( $bookmark_recent, $bookmark_not_recent ),
262+
array(
263+
'show_updated' => 1,
264+
)
265+
);
266+
267+
$this->assertStringContainsString( '<em><a href="https://example.org/recent"', $output );
268+
$this->assertStringContainsString( 'Last updated: ' . $expected_date, $output );
269+
$this->assertStringContainsString( '</a></em>', $output );
270+
$this->assertStringNotContainsString( '<em><a href="https://example.org/old"', $output );
271+
}
272+
}

0 commit comments

Comments
 (0)