General: Add "noopener" to wp_list_bookmarks() output.

In the bookmarks walker `_walk_bookmarks()`, add a `'noopener'` to the bookmark's `rel` attribute when there's `target` attribute.

Adds a new test class for `wp_list_bookmarks()` and tests for this change.

Follow-up to [3880], [10712].

Props birgire, costdev, hellofromTonya, mukesh27 , sergeybiryukov, tw2113.
Fixes #53839.

git-svn-id: https://develop.svn.wordpress.org/trunk@52061 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Tonya Mork
2021-11-08 23:05:50 +00:00
parent 4a77621488
commit 81b29a2865
2 changed files with 118 additions and 4 deletions

View File

@@ -0,0 +1,103 @@
<?php
/**
* Test wp_list_bookmarks().
*
* @group bookmark
* @covers ::wp_list_bookmarks
*/
class Tests_Functions_wpListBookmarks extends WP_UnitTestCase {
/**
* Test that wp_list_bookmarks adds "noopener" to the "rel" attribute.
*
* @dataProvider data_wp_list_bookmarks_adds_noopener
*
* @ticket 53839
*
* @param array $args The arguments to create the bookmark.
*/
public function test_wp_list_bookmarks_adds_noopener( $args ) {
$bookmark = self::factory()->bookmark->create( $args );
$this->assertStringContainsString( 'noopener', wp_list_bookmarks( 'echo=0' ) );
}
/**
* Data provider.
*
* @return array
*/
public function data_wp_list_bookmarks_adds_noopener() {
return array(
'target as "_blank"' => array(
'args' => array(
'link_name' => 'With _blank',
'link_url' => 'https://www.wordpress.org',
'link_target' => '_blank',
),
),
'target as "_blank" and a link relationship' => array(
'args' => array(
'link_name' => 'With _blank and a link relationship',
'link_url' => 'https://www.wordpress.org',
'link_target' => '_blank',
'rel' => 'me',
),
),
'target as "_top"' => array(
'args' => array(
'link_name' => 'With _top',
'link_url' => 'https://www.wordpress.org',
'link_target' => '_top',
),
),
'target as "_top" and a link relationship' => array(
'args' => array(
'link_name' => 'With _top and a link relationship',
'link_url' => 'https://www.wordpress.org',
'link_target' => '_top',
'rel' => 'me',
),
),
);
}
/**
* Test that wp_list_bookmarks does not add "noopener" to the "rel" attribute.
*
* @dataProvider data_wp_list_bookmarks_does_not_add_noopener
*
* @ticket 53839
*
* @param array $args The arguments to create the bookmark.
*/
public function test_wp_list_bookmarks_does_not_add_noopener( $args ) {
$bookmark = self::factory()->bookmark->create( $args );
$this->assertStringNotContainsString( 'noopener', wp_list_bookmarks( 'echo=0' ) );
}
/**
* Data provider.
*
* @return array
*/
public function data_wp_list_bookmarks_does_not_add_noopener() {
return array(
'target as "_none"' => array(
'args' => array(
'link_name' => 'With _blank',
'link_url' => 'https://www.wordpress.org',
'link_target' => '_none',
),
),
'target as "_none" and a link relationship' => array(
'args' => array(
'link_name' => 'With _blank and a link relationship',
'link_url' => 'https://www.wordpress.org',
'link_target' => '_none',
'rel' => 'me',
),
),
);
}
}