From 9fe8cdf1264c1ab87ef234219aa3633cd5e7d3b2 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Thu, 15 Jun 2023 03:23:25 +0000 Subject: [PATCH] Tests/Build tools: Various term related test improvements. Modifies the tests for `get_tag_link()`, `get_term()` and `get_term_field()` to: - use shared fixtures as possible - improves variable names - add `@covers` annotation as required Props peterwilsoncc, costdev. See #57841. git-svn-id: https://develop.svn.wordpress.org/trunk@55924 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/term/getTagLink.php | 76 ++++++++++------ tests/phpunit/tests/term/getTerm.php | 103 ++++++++++++++-------- tests/phpunit/tests/term/getTermField.php | 80 ++++++++++------- 3 files changed, 166 insertions(+), 93 deletions(-) diff --git a/tests/phpunit/tests/term/getTagLink.php b/tests/phpunit/tests/term/getTagLink.php index cd55353b25198..bca2f7b91d15c 100644 --- a/tests/phpunit/tests/term/getTagLink.php +++ b/tests/phpunit/tests/term/getTagLink.php @@ -5,16 +5,56 @@ * @covers ::get_tag_link */ class Tests_Term_GetTagLink extends WP_UnitTestCase { - public function test_success() { - $t = self::factory()->term->create( + /** + * Tag ID. + * + * @var int + */ + public static $tag_id; + + /** + * Test taxonomy term ID. + * + * @var int + */ + public static $term_id; + + /** + * Set up shared fixtures. + * + * @param WP_UnitTest_Factory $factory + */ + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { + self::$tag_id = $factory->term->create( array( 'taxonomy' => 'post_tag', - 'slug' => 'term-slug', + 'slug' => 'test-tag', ) ); - $found = get_tag_link( $t ); - $expected = home_url( '?tag=term-slug' ); + register_taxonomy( 'wptests_tax', 'post' ); + self::$term_id = self::factory()->term->create( + array( + 'taxonomy' => 'wptests_tax', + 'slug' => 'test-term', + ) + ); + } + + /** + * Set up the test fixture. + */ + public function set_up() { + parent::set_up(); + // Required as taxonomies are reset between tests. + register_taxonomy( 'wptests_tax', 'post' ); + } + + public function test_success() { + $tag_id = self::$tag_id; + + $found = get_tag_link( $tag_id ); + $expected = home_url( '?tag=test-tag' ); $this->assertSame( $expected, $found ); } @@ -23,18 +63,11 @@ public function test_success() { * @ticket 42771 */ public function test_should_return_link_for_term_from_another_taxonomy_on_primed_cache() { - register_taxonomy( 'wptests_tax', 'post' ); + $term_id = self::$term_id; - $t = self::factory()->term->create( - array( - 'taxonomy' => 'wptests_tax', - 'slug' => 'test-term', - ) - ); - - $term = get_term( $t ); + $term = get_term( $term_id ); - $found = get_tag_link( $t ); + $found = get_tag_link( $term_id ); $expected = home_url( '?wptests_tax=test-term' ); $this->assertSame( $expected, $found ); @@ -44,18 +77,11 @@ public function test_should_return_link_for_term_from_another_taxonomy_on_primed * @ticket 42771 */ public function test_should_return_link_for_term_from_another_taxonomy_on_empty_cache() { - register_taxonomy( 'wptests_tax', 'post' ); - - $t = self::factory()->term->create( - array( - 'taxonomy' => 'wptests_tax', - 'slug' => 'test-term', - ) - ); + $term_id = self::$term_id; - clean_term_cache( $t ); + clean_term_cache( $term_id ); - $found = get_tag_link( $t ); + $found = get_tag_link( $term_id ); $expected = home_url( '?wptests_tax=test-term' ); $this->assertSame( $expected, $found ); diff --git a/tests/phpunit/tests/term/getTerm.php b/tests/phpunit/tests/term/getTerm.php index 879690f985c8d..36567a7830884 100644 --- a/tests/phpunit/tests/term/getTerm.php +++ b/tests/phpunit/tests/term/getTerm.php @@ -2,11 +2,44 @@ /** * @group taxonomy + * + * @covers ::get_term */ class Tests_Term_GetTerm extends WP_UnitTestCase { + /** + * Shared terms. + * + * @var array[] + */ + public static $shared_terms = array(); + + /** + * Test taxonomy term object. + * + * @var WP_Term + */ + public static $term; + + /** + * Set up shared fixtures. + * + * @param WP_UnitTest_Factory $factory + */ + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { + register_taxonomy( 'wptests_tax', 'post' ); + self::$shared_terms = self::generate_shared_terms(); + + self::$term = $factory->term->create_and_get( array( 'taxonomy' => 'wptests_tax' ) ); + } + + /** + * Set up the test fixtures. + */ public function set_up() { parent::set_up(); + // Required as taxonomies are reset between tests. register_taxonomy( 'wptests_tax', 'post' ); + register_taxonomy( 'wptests_tax_2', 'post' ); } /** @@ -14,35 +47,35 @@ public function set_up() { * * @return array Array of term_id/old_term_id/term_taxonomy_id triplets. */ - protected function generate_shared_terms() { + protected static function generate_shared_terms() { global $wpdb; register_taxonomy( 'wptests_tax_2', 'post' ); - $t1 = wp_insert_term( 'Foo', 'wptests_tax' ); - $t2 = wp_insert_term( 'Foo', 'wptests_tax_2' ); + $term_1 = wp_insert_term( 'Foo', 'wptests_tax' ); + $term_2 = wp_insert_term( 'Foo', 'wptests_tax_2' ); // Manually modify because shared terms shouldn't naturally occur. $wpdb->update( $wpdb->term_taxonomy, - array( 'term_id' => $t1['term_id'] ), - array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ), + array( 'term_id' => $term_1['term_id'] ), + array( 'term_taxonomy_id' => $term_2['term_taxonomy_id'] ), array( '%d' ), array( '%d' ) ); - clean_term_cache( $t1['term_id'] ); + clean_term_cache( $term_1['term_id'] ); return array( array( - 'term_id' => $t1['term_id'], - 'old_term_id' => $t1['term_id'], - 'term_taxonomy_id' => $t1['term_taxonomy_id'], + 'term_id' => $term_1['term_id'], + 'old_term_id' => $term_1['term_id'], + 'term_taxonomy_id' => $term_1['term_taxonomy_id'], ), array( - 'term_id' => $t1['term_id'], - 'old_term_id' => $t2['term_id'], - 'term_taxonomy_id' => $t2['term_taxonomy_id'], + 'term_id' => $term_1['term_id'], + 'old_term_id' => $term_2['term_id'], + 'term_taxonomy_id' => $term_2['term_taxonomy_id'], ), ); } @@ -60,7 +93,7 @@ public function test_should_return_error_for_invalid_taxonomy() { } public function test_passing_term_object_should_skip_database_query_when_filter_property_is_empty() { - $term = self::factory()->term->create_and_get( array( 'taxonomy' => 'wptests_tax' ) ); + $term = self::$term; clean_term_cache( $term->term_id, 'wptests_tax' ); $num_queries = get_num_queries(); @@ -80,34 +113,34 @@ public function test_should_return_null_for_invalid_term_id() { } public function test_cache_should_be_populated_by_successful_fetch() { - $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); - clean_term_cache( $t, 'wptests_tax' ); + $term_id = self::$term->term_id; + clean_term_cache( $term_id, 'wptests_tax' ); // Prime cache. - $term_a = get_term( $t, 'wptests_tax' ); + $term_a = get_term( $term_id, 'wptests_tax' ); $num_queries = get_num_queries(); // Second call shouldn't require a database query. - $term_b = get_term( $t, 'wptests_tax' ); + $term_b = get_term( $term_id, 'wptests_tax' ); $this->assertSame( $num_queries, get_num_queries() ); $this->assertEquals( $term_a, $term_b ); } public function test_output_object() { - $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); - $this->assertIsObject( get_term( $t, 'wptests_tax', OBJECT ) ); + $term_id = self::$term->term_id; + $this->assertIsObject( get_term( $term_id, 'wptests_tax', OBJECT ) ); } public function test_output_array_a() { - $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); - $term = get_term( $t, 'wptests_tax', ARRAY_A ); + $term_id = self::$term->term_id; + $term = get_term( $term_id, 'wptests_tax', ARRAY_A ); $this->assertIsArray( $term ); $this->assertArrayHasKey( 'term_id', $term ); } public function test_output_array_n() { - $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); - $term = get_term( $t, 'wptests_tax', ARRAY_N ); + $term_id = self::$term->term_id; + $term = get_term( $term_id, 'wptests_tax', ARRAY_N ); $this->assertIsArray( $term ); $this->assertArrayNotHasKey( 'term_id', $term ); foreach ( $term as $k => $v ) { @@ -116,8 +149,8 @@ public function test_output_array_n() { } public function test_output_should_fall_back_to_object_for_invalid_input() { - $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); - $this->assertIsObject( get_term( $t, 'wptests_tax', 'foo' ) ); + $term_id = self::$term->term_id; + $this->assertIsObject( get_term( $term_id, 'wptests_tax', 'foo' ) ); } /** @@ -127,10 +160,10 @@ public function test_output_should_fall_back_to_object_for_invalid_input() { public function test_numeric_properties_should_be_cast_to_ints() { global $wpdb; - $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); + $term_id = self::$term->term_id; // Get raw data from the database. - $term_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms t JOIN $wpdb->term_taxonomy tt ON ( t.term_id = tt.term_id ) WHERE t.term_id = %d", $t ) ); + $term_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms t JOIN $wpdb->term_taxonomy tt ON ( t.term_id = tt.term_id ) WHERE t.term_id = %d", $term_id ) ); $contexts = array( 'raw', 'edit', 'db', 'display', 'rss', 'attribute', 'js' ); @@ -150,7 +183,7 @@ public function test_numeric_properties_should_be_cast_to_ints() { * @ticket 34332 */ public function test_should_return_null_when_provided_taxonomy_does_not_match_actual_term_taxonomy() { - $term_id = self::factory()->term->create( array( 'taxonomy' => 'post_tag' ) ); + $term_id = self::$term->term_id; $this->assertNull( get_term( $term_id, 'category' ) ); } @@ -158,7 +191,7 @@ public function test_should_return_null_when_provided_taxonomy_does_not_match_ac * @ticket 34533 */ public function test_should_return_wp_error_when_term_is_shared_and_no_taxonomy_is_specified() { - $terms = $this->generate_shared_terms(); + $terms = self::$shared_terms; $found = get_term( $terms[0]['term_id'] ); @@ -169,7 +202,7 @@ public function test_should_return_wp_error_when_term_is_shared_and_no_taxonomy_ * @ticket 34533 */ public function test_should_return_term_when_term_is_shared_and_correct_taxonomy_is_specified() { - $terms = $this->generate_shared_terms(); + $terms = self::$shared_terms; $found = get_term( $terms[0]['term_id'], 'wptests_tax' ); @@ -181,7 +214,7 @@ public function test_should_return_term_when_term_is_shared_and_correct_taxonomy * @ticket 34533 */ public function test_should_return_null_when_term_is_shared_and_incorrect_taxonomy_is_specified() { - $terms = $this->generate_shared_terms(); + $terms = self::$shared_terms; $found = get_term( $terms[0]['term_id'], 'post_tag' ); @@ -192,7 +225,7 @@ public function test_should_return_null_when_term_is_shared_and_incorrect_taxono * @ticket 34533 */ public function test_shared_term_in_cache_should_be_ignored_when_specifying_a_different_taxonomy() { - $terms = $this->generate_shared_terms(); + $terms = self::$shared_terms; // Prime cache for 'wptests_tax'. get_term( $terms[0]['term_id'], 'wptests_tax' ); @@ -211,11 +244,11 @@ public function test_shared_term_in_cache_should_be_ignored_when_specifying_a_di * @ticket 34533 */ public function test_should_return_error_when_only_matching_term_is_in_an_invalid_taxonomy() { - $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); + $term_id = self::$term->term_id; _unregister_taxonomy( 'wptests_tax' ); - $found = get_term( $t ); + $found = get_term( $term_id ); $this->assertWPError( $found ); $this->assertSame( 'invalid_taxonomy', $found->get_error_code() ); } @@ -224,7 +257,7 @@ public function test_should_return_error_when_only_matching_term_is_in_an_invali * @ticket 34533 */ public function test_term_should_be_returned_when_id_is_shared_only_with_invalid_taxonomies() { - $terms = $this->generate_shared_terms(); + $terms = self::$shared_terms; _unregister_taxonomy( 'wptests_tax' ); diff --git a/tests/phpunit/tests/term/getTermField.php b/tests/phpunit/tests/term/getTermField.php index c69044564c91e..fdcfbdc0285f1 100644 --- a/tests/phpunit/tests/term/getTermField.php +++ b/tests/phpunit/tests/term/getTermField.php @@ -2,53 +2,72 @@ /** * @group taxonomy + * + * @covers ::get_term_field */ class Tests_Term_getTermField extends WP_UnitTestCase { - public $taxonomy = 'wptests_tax'; + public static $taxonomy = 'wptests_tax'; + + public static $term; + + /** + * Set up shared fixtures. + * + * @param WP_UnitTest_Factory $factory + */ + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { + register_taxonomy( self::$taxonomy, 'post' ); + self::$term = $factory->term->create_and_get( + array( + 'taxonomy' => self::$taxonomy, + 'description' => wpautop( 'Test term description' ), + ) + ); + } public function set_up() { parent::set_up(); - - register_taxonomy( $this->taxonomy, 'post' ); + // Required as taxonomies are reset between tests. + register_taxonomy( self::$taxonomy, 'post' ); } /** * @ticket 34245 */ public function test_get_term_field_should_not_return_error_for_empty_taxonomy() { - $term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) ); + $term = self::$term; $found = get_term_field( 'taxonomy', $term->term_id, '' ); $this->assertNotWPError( $found ); - $this->assertSame( $this->taxonomy, $found ); + $this->assertSame( self::$taxonomy, $found ); } /** * @ticket 34245 */ public function test_get_term_field_supplying_a_taxonomy() { - $term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) ); + $term = self::$term; $found = get_term_field( 'taxonomy', $term->term_id, $term->taxonomy ); - $this->assertSame( $this->taxonomy, $found ); + $this->assertSame( self::$taxonomy, $found ); } /** * @ticket 34245 */ public function test_get_term_field_supplying_no_taxonomy() { - $term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) ); + $term = self::$term; $found = get_term_field( 'taxonomy', $term->term_id ); - $this->assertSame( $this->taxonomy, $found ); + $this->assertSame( self::$taxonomy, $found ); } /** * @ticket 34245 */ public function test_get_term_field_should_accept_a_WP_Term_id_or_object() { - $term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) ); + $term = self::$term; $this->assertInstanceOf( 'WP_Term', $term ); $this->assertSame( $term->term_id, get_term_field( 'term_id', $term ) ); @@ -60,7 +79,7 @@ public function test_get_term_field_should_accept_a_WP_Term_id_or_object() { * @ticket 34245 */ public function test_get_term_field_invalid_taxonomy_should_return_WP_Error() { - $term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) ); + $term = self::$term; $found = get_term_field( 'taxonomy', $term, 'foo-taxonomy' ); $this->assertWPError( $found ); @@ -71,7 +90,7 @@ public function test_get_term_field_invalid_taxonomy_should_return_WP_Error() { * @ticket 34245 */ public function test_get_term_field_invalid_term_should_return_WP_Error() { - $found = get_term_field( 'taxonomy', 0, $this->taxonomy ); + $found = get_term_field( 'taxonomy', 0, self::$taxonomy ); $this->assertWPError( $found ); $this->assertSame( 'invalid_term', $found->get_error_code() ); @@ -83,7 +102,7 @@ public function test_get_term_field_invalid_term_should_return_WP_Error() { } public function test_get_term_field_term_id() { - $term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) ); + $term = self::$term; $this->assertSame( $term->term_id, get_term_field( 'term_id', $term ) ); $this->assertSame( $term->term_id, get_term_field( 'term_id', $term->data ) ); @@ -96,7 +115,7 @@ public function test_get_term_field_name() { $term = self::factory()->term->create_and_get( array( 'name' => $name, - 'taxonomy' => $this->taxonomy, + 'taxonomy' => self::$taxonomy, ) ); @@ -110,7 +129,7 @@ public function test_get_term_field_slug_when_slug_is_set() { $term = self::factory()->term->create_and_get( array( - 'taxonomy' => $this->taxonomy, + 'taxonomy' => self::$taxonomy, 'slug' => $slug, ) ); @@ -125,7 +144,7 @@ public function test_get_term_field_slug_when_slug_falls_back_from_name() { $term = self::factory()->term->create_and_get( array( - 'taxonomy' => $this->taxonomy, + 'taxonomy' => self::$taxonomy, 'name' => $name, ) ); @@ -138,7 +157,7 @@ public function test_get_term_field_slug_when_slug_falls_back_from_name() { public function test_get_term_field_slug_when_slug_and_name_are_not_set() { $term = self::factory()->term->create_and_get( array( - 'taxonomy' => $this->taxonomy, + 'taxonomy' => self::$taxonomy, ) ); @@ -148,33 +167,28 @@ public function test_get_term_field_slug_when_slug_and_name_are_not_set() { } public function test_get_term_field_taxonomy() { - $term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) ); + $term = self::$term; - $this->assertSame( $this->taxonomy, get_term_field( 'taxonomy', $term ) ); - $this->assertSame( $this->taxonomy, get_term_field( 'taxonomy', $term->data ) ); - $this->assertSame( $this->taxonomy, get_term_field( 'taxonomy', $term->term_id ) ); + $this->assertSame( self::$taxonomy, get_term_field( 'taxonomy', $term ) ); + $this->assertSame( self::$taxonomy, get_term_field( 'taxonomy', $term->data ) ); + $this->assertSame( self::$taxonomy, get_term_field( 'taxonomy', $term->term_id ) ); } public function test_get_term_field_description() { - $desc = wpautop( 'baz' ); + $description = wpautop( 'Test term description' ); - $term = self::factory()->term->create_and_get( - array( - 'taxonomy' => $this->taxonomy, - 'description' => $desc, - ) - ); + $term = self::$term; - $this->assertSame( $desc, get_term_field( 'description', $term ) ); - $this->assertSame( $desc, get_term_field( 'description', $term->data ) ); - $this->assertSame( $desc, get_term_field( 'description', $term->term_id ) ); + $this->assertSame( $description, get_term_field( 'description', $term ) ); + $this->assertSame( $description, get_term_field( 'description', $term->data ) ); + $this->assertSame( $description, get_term_field( 'description', $term->term_id ) ); } public function test_get_term_field_parent() { - $parent = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) ); + $parent = self::$term; $term = self::factory()->term->create_and_get( array( - 'taxonomy' => $this->taxonomy, + 'taxonomy' => self::$taxonomy, 'parent' => $parent->term_id, ) );