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:
Sergey Biryukov
2022-09-02 01:14:29 +00:00
parent 8b106a9e54
commit 1f0289d890
3 changed files with 59 additions and 126 deletions
+35 -52
View File
@@ -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 );
}
}