Skip to content

Commit f3a4930

Browse files
Tests: Cover wp_rel_callback().
Verify: - Adding a `rel` attribute to an anchor that does not have one. - Merging a new `rel` value with an existing `rel` attribute without duplicating existing terms. - Stripping `nofollow` when the destination URL is an internal link. - Retaining other `rel` terms (such as `ugc`) for internal links. - Handling valueless HTML attributes (such as `download`) on the anchor element. Developed in #15788. Props devmuhib, huzaifaalmesbah. Fixes #66610. git-svn-id: https://develop.svn.wordpress.org/trunk@65820 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 43413fc commit f3a4930

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/**
4+
* Tests for the `wp_rel_callback()` function.
5+
*
6+
* @group formatting
7+
*
8+
* @covers ::wp_rel_callback
9+
*/
10+
class Tests_Formatting_WpRelCallback extends WP_UnitTestCase {
11+
12+
/**
13+
* Tests adding a `rel` attribute to an anchor that does not have one.
14+
*
15+
* @ticket 66610
16+
*/
17+
public function test_wp_rel_callback_adds_rel_to_link() {
18+
$matches = array( '<a href="https://example.com">', 'href="https://example.com"' );
19+
$actual = wp_rel_callback( $matches, 'nofollow' );
20+
21+
$this->assertSame( '<a href="https://example.com" rel="nofollow">', $actual );
22+
}
23+
24+
/**
25+
* Tests merging a new `rel` value with an existing `rel` attribute.
26+
*
27+
* @ticket 66610
28+
*/
29+
public function test_wp_rel_callback_merges_with_existing_rel() {
30+
$matches = array( '<a href="https://example.com" rel="external">', 'href="https://example.com" rel="external"' );
31+
$actual = wp_rel_callback( $matches, 'nofollow' );
32+
33+
$this->assertSame( '<a href="https://example.com" rel="external nofollow">', $actual );
34+
}
35+
36+
/**
37+
* Tests that existing `rel` values are not duplicated.
38+
*
39+
* @ticket 66610
40+
*/
41+
public function test_wp_rel_callback_does_not_duplicate_existing_rel() {
42+
$matches = array( '<a href="https://example.com" rel="nofollow ugc">', 'href="https://example.com" rel="nofollow ugc"' );
43+
$actual = wp_rel_callback( $matches, 'nofollow' );
44+
45+
$this->assertSame( '<a href="https://example.com" rel="nofollow ugc">', $actual );
46+
}
47+
48+
/**
49+
* Tests that `nofollow` is stripped when the link is internal.
50+
*
51+
* @ticket 66610
52+
*/
53+
public function test_wp_rel_callback_removes_nofollow_for_internal_links() {
54+
$home_url = home_url( '/test-page/' );
55+
$matches = array( '<a href="' . $home_url . '">', 'href="' . $home_url . '"' );
56+
$actual = wp_rel_callback( $matches, 'nofollow' );
57+
58+
$this->assertSame( '<a href="' . $home_url . '">', $actual );
59+
}
60+
61+
/**
62+
* Tests that internal links keep existing `rel` and can add other `rel` values like `ugc`.
63+
*
64+
* @ticket 66610
65+
*/
66+
public function test_wp_rel_callback_preserves_other_rel_for_internal_links() {
67+
$home_url = home_url( '/internal/' );
68+
$matches = array( '<a href="' . $home_url . '" rel="author">', 'href="' . $home_url . '" rel="author"' );
69+
$actual = wp_rel_callback( $matches, 'ugc' );
70+
71+
$this->assertSame( '<a href="' . $home_url . '" rel="author ugc">', $actual );
72+
}
73+
74+
/**
75+
* Tests handling of valueless attributes on the anchor.
76+
*
77+
* @ticket 66610
78+
*/
79+
public function test_wp_rel_callback_handles_valueless_attributes() {
80+
$matches = array( '<a href="https://example.com" download rel="opener">', 'href="https://example.com" download rel="opener"' );
81+
$actual = wp_rel_callback( $matches, 'noreferrer' );
82+
83+
$this->assertStringContainsString( 'download', $actual );
84+
$this->assertStringContainsString( 'rel="opener noreferrer"', $actual );
85+
}
86+
}

0 commit comments

Comments
 (0)