mirror of
git://develop.git.wordpress.org/
synced 2025-02-07 08:04:27 +01:00
Tests: Simplify and correct get_term_link()
and get_edit_term_link()
tests:
* Some assertions were unnecessarily duplicated. They aim to test the function behavior both when passing a term ID and term object, however that is already handled via the `$use_id` parameter of the `get_term()` helper in the same test class. The data providers already supply test cases both with a term ID and term object, so there is no need for a second assertion or a whole second test method with a term object. * One `get_term_feed_link()` test was unnecessarily skipped half of the time, when term object was passed instead of term ID. Instead, it can use a dedicated data provider and avoid skipping. Includes: * Using more descriptive test method names to clarify the intention of the tests. * Some documentation updates for clarity. Follow-up to [52180], [52255], [52258], [52305], [53833], [53836]. See #55652. git-svn-id: https://develop.svn.wordpress.org/trunk@54061 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
8b106a9e54
commit
1f0289d890
@ -42,8 +42,8 @@ class Tests_Link_EditTermLink extends WP_UnitTestCase {
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param string $taxonomy Taxonomy being tested (used for index of term keys).
|
||||
* @param bool $use_id When true, pass term ID. Else, pass term object.
|
||||
* @return WP_Term|int If $use_id is true, term ID is returned; else instance of WP_Term.
|
||||
* @param bool $use_id Whether to return term ID or term object.
|
||||
* @return WP_Term|int Term ID if `$use_id` is true, WP_Term instance otherwise.
|
||||
*/
|
||||
private function get_term( $taxonomy, $use_id ) {
|
||||
$term = self::$terms[ $taxonomy ];
|
||||
@ -59,11 +59,11 @@ class Tests_Link_EditTermLink extends WP_UnitTestCase {
|
||||
*
|
||||
* @ticket 50225
|
||||
*
|
||||
* @param string $taxonomy Taxonomy being tested (used for index of term keys).
|
||||
* @param bool $use_id When true, pass term ID. Else, pass term object.
|
||||
* @param string $expected Expected URL within admin of edit link.
|
||||
* @param string $taxonomy Taxonomy being tested.
|
||||
* @param bool $use_id Whether to pass term ID or term object to `edit_term_link()`.
|
||||
* @param string $expected Expected part of admin URL for the edit link.
|
||||
*/
|
||||
public function test_edit_term_link_for_permitted_user( $taxonomy, $use_id, $expected ) {
|
||||
public function test_edit_term_link_should_return_the_link_for_permitted_user( $taxonomy, $use_id, $expected ) {
|
||||
$term = $this->get_term( $taxonomy, $use_id );
|
||||
|
||||
// Term IDs are not known by the data provider so need to be replaced.
|
||||
@ -71,7 +71,6 @@ class Tests_Link_EditTermLink extends WP_UnitTestCase {
|
||||
$expected = '"' . admin_url( $expected ) . '"';
|
||||
|
||||
$this->assertStringContainsString( $expected, edit_term_link( '', '', '', $term, false ) );
|
||||
$this->assertStringContainsString( $expected, edit_term_link( '', '', '', get_term( $term, $taxonomy ), false ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -79,15 +78,14 @@ class Tests_Link_EditTermLink extends WP_UnitTestCase {
|
||||
*
|
||||
* @ticket 50225
|
||||
*
|
||||
* @param string $taxonomy Taxonomy being tested (used for index of term keys).
|
||||
* @param bool $use_id When true, pass term ID. Else, pass term object.
|
||||
* @param string $taxonomy Taxonomy being tested.
|
||||
* @param bool $use_id Whether to pass term ID or term object to `edit_term_link()`.
|
||||
*/
|
||||
public function test_edit_term_link_for_denied_user( $taxonomy, $use_id ) {
|
||||
public function test_edit_term_link_should_return_null_for_denied_user( $taxonomy, $use_id ) {
|
||||
wp_set_current_user( self::$user_ids['subscriber'] );
|
||||
$term = $this->get_term( $taxonomy, $use_id );
|
||||
|
||||
$this->assertNull( edit_term_link( '', '', '', $term, false ) );
|
||||
$this->assertNull( edit_term_link( '', '', '', get_term( $term, $taxonomy ), false ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -95,10 +93,10 @@ class Tests_Link_EditTermLink extends WP_UnitTestCase {
|
||||
*
|
||||
* @ticket 50225
|
||||
*
|
||||
* @param string $taxonomy Taxonomy being tested (used for index of term keys).
|
||||
* @param bool $use_id When true, pass term ID. Else, pass term object.
|
||||
* @param string $taxonomy Taxonomy being tested.
|
||||
* @param bool $use_id Whether to pass term ID or term object to `edit_term_link()`.
|
||||
*/
|
||||
public function test_edit_term_link_filter_is_int_by_term_id( $taxonomy, $use_id ) {
|
||||
public function test_edit_term_link_filter_should_receive_term_id( $taxonomy, $use_id ) {
|
||||
$term = $this->get_term( $taxonomy, $use_id );
|
||||
|
||||
add_filter(
|
||||
@ -113,29 +111,6 @@ class Tests_Link_EditTermLink extends WP_UnitTestCase {
|
||||
edit_term_link( '', '', '', $term, false );
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider data_edit_term_link
|
||||
*
|
||||
* @ticket 50225
|
||||
*
|
||||
* @param string $taxonomy Taxonomy being tested (used for index of term keys).
|
||||
* @param bool $use_id When true, pass term ID. Else, pass term object.
|
||||
*/
|
||||
public function test_edit_term_link_filter_is_int_by_term_object( $taxonomy, $use_id ) {
|
||||
$term = $this->get_term( $taxonomy, $use_id );
|
||||
|
||||
add_filter(
|
||||
'edit_term_link',
|
||||
function( $location, $term ) {
|
||||
$this->assertIsInt( $term );
|
||||
},
|
||||
10,
|
||||
2
|
||||
);
|
||||
|
||||
edit_term_link( '', '', '', get_term( $term, $taxonomy ), false );
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
|
@ -42,8 +42,8 @@ class Tests_Link_GetEditTermLink extends WP_UnitTestCase {
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param string $taxonomy Taxonomy being tested (used for index of term keys).
|
||||
* @param bool $use_id When true, pass term ID. Else, pass term object.
|
||||
* @return WP_Term|int If $use_id is true, term ID is returned; else instance of WP_Term.
|
||||
* @param bool $use_id Whether to return term ID or term object.
|
||||
* @return WP_Term|int Term ID if `$use_id` is true, WP_Term instance otherwise.
|
||||
*/
|
||||
private function get_term( $taxonomy, $use_id ) {
|
||||
$term = self::$terms[ $taxonomy ];
|
||||
@ -145,11 +145,11 @@ class Tests_Link_GetEditTermLink extends WP_UnitTestCase {
|
||||
*
|
||||
* @ticket 50225
|
||||
*
|
||||
* @param string $taxonomy Taxonomy being tested (used for index of term keys).
|
||||
* @param bool $use_id When true, pass term ID. Else, pass term object.
|
||||
* @param string $expected Expected URL within admin of edit link.
|
||||
* @param string $taxonomy Taxonomy being tested.
|
||||
* @param bool $use_id Whether to pass term ID or term object to `get_edit_term_link()`.
|
||||
* @param string $expected Expected part of admin URL for the edit link.
|
||||
*/
|
||||
public function test_get_edit_term_link_for_permitted_user( $taxonomy, $use_id, $expected ) {
|
||||
public function test_get_edit_term_link_should_return_the_link_for_permitted_user( $taxonomy, $use_id, $expected ) {
|
||||
$term = $this->get_term( $taxonomy, $use_id );
|
||||
|
||||
// Term IDs are not known by the data provider so need to be replaced.
|
||||
@ -157,7 +157,6 @@ class Tests_Link_GetEditTermLink extends WP_UnitTestCase {
|
||||
$expected = admin_url( $expected );
|
||||
|
||||
$this->assertSame( $expected, get_edit_term_link( $term, $taxonomy ) );
|
||||
$this->assertSame( $expected, get_edit_term_link( get_term( $term, $taxonomy ), $taxonomy ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -165,15 +164,14 @@ class Tests_Link_GetEditTermLink extends WP_UnitTestCase {
|
||||
*
|
||||
* @ticket 50225
|
||||
*
|
||||
* @param string $taxonomy Taxonomy being tested (used for index of term keys).
|
||||
* @param bool $use_id When true, pass term ID. Else, pass term object.
|
||||
* @param string $taxonomy Taxonomy being tested.
|
||||
* @param bool $use_id Whether to pass term ID or term object to `get_edit_term_link()`.
|
||||
*/
|
||||
public function test_get_edit_term_link_for_denied_user( $taxonomy, $use_id ) {
|
||||
public function test_get_edit_term_link_should_return_null_for_denied_user( $taxonomy, $use_id ) {
|
||||
wp_set_current_user( self::$user_ids['subscriber'] );
|
||||
$term = $this->get_term( $taxonomy, $use_id );
|
||||
|
||||
$this->assertNull( get_edit_term_link( $term, $taxonomy ) );
|
||||
$this->assertNull( get_edit_term_link( get_term( $term, $taxonomy ), $taxonomy ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -181,10 +179,10 @@ class Tests_Link_GetEditTermLink extends WP_UnitTestCase {
|
||||
*
|
||||
* @ticket 50225
|
||||
*
|
||||
* @param string $taxonomy Taxonomy being tested (used for index of term keys).
|
||||
* @param bool $use_id When true, pass term ID. Else, pass term object.
|
||||
* @param string $taxonomy Taxonomy being tested.
|
||||
* @param bool $use_id Whether to pass term ID or term object to `get_edit_term_link()`.
|
||||
*/
|
||||
public function test_get_edit_term_link_filter_is_int_by_term_id( $taxonomy, $use_id ) {
|
||||
public function test_get_edit_term_link_filter_should_receive_term_id( $taxonomy, $use_id ) {
|
||||
$term = $this->get_term( $taxonomy, $use_id );
|
||||
|
||||
add_filter(
|
||||
@ -199,29 +197,6 @@ class Tests_Link_GetEditTermLink extends WP_UnitTestCase {
|
||||
get_edit_term_link( $term, $taxonomy );
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider data_get_edit_term_link
|
||||
*
|
||||
* @ticket 50225
|
||||
*
|
||||
* @param string $taxonomy Taxonomy being tested (used for index of term keys).
|
||||
* @param bool $use_id When true, pass term ID. Else, pass term object.
|
||||
*/
|
||||
public function test_get_edit_term_link_filter_is_int_by_term_object( $taxonomy, $use_id ) {
|
||||
$term = $this->get_term( $taxonomy, $use_id );
|
||||
|
||||
add_filter(
|
||||
'get_edit_term_link',
|
||||
function( $location, $term ) {
|
||||
$this->assertIsInt( $term );
|
||||
},
|
||||
10,
|
||||
2
|
||||
);
|
||||
|
||||
get_edit_term_link( get_term( $term, $taxonomy ), $taxonomy );
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
|
@ -37,8 +37,8 @@ class Tests_Term_GetTermLink extends WP_UnitTestCase {
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param string $taxonomy Taxonomy being tested (used for index of term keys).
|
||||
* @param bool $use_id When true, pass term ID. Else, pass term object.
|
||||
* @return WP_Term|int If $use_id is true, term ID is returned; else instance of WP_Term.
|
||||
* @param bool $use_id Whether to return term ID or term object.
|
||||
* @return WP_Term|int Term ID if `$use_id` is true, WP_Term instance otherwise.
|
||||
*/
|
||||
private function get_term( $taxonomy, $use_id ) {
|
||||
$term = self::$terms[ $taxonomy ];
|
||||
@ -247,14 +247,14 @@ class Tests_Term_GetTermLink extends WP_UnitTestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider data_get_term_link
|
||||
* @dataProvider data_term_link_filter_should_receive_term_object
|
||||
*
|
||||
* @ticket 50225
|
||||
*
|
||||
* @param string $taxonomy Taxonomy being tested (used for index of term keys).
|
||||
* @param bool $use_id When true, pass term ID. Else, pass term object.
|
||||
* @param string $taxonomy Taxonomy being tested.
|
||||
* @param bool $use_id Whether to pass term ID or term object to `get_term_link()`.
|
||||
*/
|
||||
public function test_get_term_link_filter_is_object_by_term_id( $taxonomy, $use_id ) {
|
||||
public function test_term_link_filter_should_receive_term_object( $taxonomy, $use_id ) {
|
||||
$term = $this->get_term( $taxonomy, $use_id );
|
||||
|
||||
add_filter(
|
||||
@ -269,57 +269,12 @@ class Tests_Term_GetTermLink extends WP_UnitTestCase {
|
||||
get_term_link( $term, $taxonomy );
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider data_get_term_link
|
||||
*
|
||||
* @ticket 50225
|
||||
*
|
||||
* @param string $taxonomy Taxonomy being tested (used for index of term keys).
|
||||
* @param bool $use_id When true, pass term ID. Else, pass term object.
|
||||
*/
|
||||
public function test_get_term_link_filter_is_object_by_term_object( $taxonomy, $use_id ) {
|
||||
$term = $this->get_term( $taxonomy, $use_id );
|
||||
|
||||
add_filter(
|
||||
'term_link',
|
||||
function( $location, $term ) {
|
||||
$this->assertInstanceOf( 'WP_Term', $term );
|
||||
},
|
||||
10,
|
||||
2
|
||||
);
|
||||
|
||||
get_term_link( get_term( $term, $taxonomy ), $taxonomy );
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider data_get_term_link
|
||||
*
|
||||
* @ticket 50225
|
||||
*
|
||||
* @param string $taxonomy Taxonomy being tested (used for index of term keys).
|
||||
* @param bool $use_id When true, pass term ID. Else, skip the test.
|
||||
*/
|
||||
public function test_get_term_feed_link_backward_compatibility( $taxonomy, $use_id ) {
|
||||
if ( $use_id ) {
|
||||
$term = $this->get_term( $taxonomy, $use_id );
|
||||
|
||||
$term_feed_link = get_term_feed_link( $term, $taxonomy );
|
||||
$this->assertIsString( $term_feed_link );
|
||||
|
||||
$term_feed_link = get_term_feed_link( $term, '' );
|
||||
$this->assertIsString( $term_feed_link );
|
||||
} else {
|
||||
$this->markTestSkipped( 'This test requires to pass an ID to get_term_feed_link()' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function data_get_term_link() {
|
||||
public function data_term_link_filter_should_receive_term_object() {
|
||||
return array(
|
||||
'category passing term_id' => array(
|
||||
'taxonomy' => 'category',
|
||||
@ -347,4 +302,32 @@ class Tests_Term_GetTermLink extends WP_UnitTestCase {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider data_get_term_feed_link_should_use_term_taxonomy_when_term_id_is_passed
|
||||
*
|
||||
* @ticket 50225
|
||||
*
|
||||
* @param string $taxonomy Taxonomy being tested.
|
||||
*/
|
||||
public function test_get_term_feed_link_should_use_term_taxonomy_when_term_id_is_passed( $taxonomy ) {
|
||||
$term = $this->get_term( $taxonomy, true );
|
||||
|
||||
$term_feed_link = get_term_feed_link( $term, $taxonomy );
|
||||
$this->assertIsString( $term_feed_link );
|
||||
|
||||
$term_feed_link = get_term_feed_link( $term, '' );
|
||||
$this->assertIsString( $term_feed_link );
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function data_get_term_feed_link_should_use_term_taxonomy_when_term_id_is_passed() {
|
||||
$taxonomies = array( 'category', 'post_tag', 'wptests_tax' );
|
||||
|
||||
return $this->text_array_to_dataprovider( $taxonomies );
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user