From 02c5d3a964a92041cc5c0434ab40f9728f0e34c1 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 4 Aug 2022 18:28:33 +0000 Subject: [PATCH] Tests: Combine test classes for `get_edit_term_link()` tests. There were two sets of tests for the function: * One in the `link` directory, based on the `link-template.php` file name. * One in the `term` directory, based on the component name. To avoid confusion and make it easier to decide where new tests should go in the future, the existing tests are now combined in the former location. Includes: * Setting the current user in `::set_up()` instead of each individual test method. * Changing the custom taxonomy name to `wptests_tax` for consistency with other tests. * Moving `::register_custom_taxonomy()` and `::get_term()` helpers to the beginning of the class. Follow-up to [32954], [36646], [52180], [52255], [53833]. See #55652. git-svn-id: https://develop.svn.wordpress.org/trunk@53836 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/link/editTermLink.php | 68 ++++---- tests/phpunit/tests/link/getEditTermLink.php | 154 ++++++++++++++----- tests/phpunit/tests/term/getEditTermLink.php | 103 ------------- tests/phpunit/tests/term/getTermLink.php | 54 +++---- 4 files changed, 179 insertions(+), 200 deletions(-) delete mode 100644 tests/phpunit/tests/term/getEditTermLink.php diff --git a/tests/phpunit/tests/link/editTermLink.php b/tests/phpunit/tests/link/editTermLink.php index b55cab5f75..2137bdd82a 100644 --- a/tests/phpunit/tests/link/editTermLink.php +++ b/tests/phpunit/tests/link/editTermLink.php @@ -12,7 +12,7 @@ class Tests_Link_EditTermLink extends WP_UnitTestCase { public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { self::register_custom_taxonomy(); - $taxonomies = array( 'category', 'post_tag', 'custom_taxonomy' ); + $taxonomies = array( 'category', 'post_tag', 'wptests_tax' ); foreach ( $taxonomies as $taxonomy ) { self::$terms[ $taxonomy ] = $factory->term->create_and_get( array( 'taxonomy' => $taxonomy ) ); } @@ -23,9 +23,37 @@ class Tests_Link_EditTermLink extends WP_UnitTestCase { public function set_up() { parent::set_up(); + wp_set_current_user( self::$user_ids['admin'] ); self::register_custom_taxonomy(); } + /** + * Helper to register a custom taxonomy for use in tests. + * + * @since 5.9.0 + */ + private static function register_custom_taxonomy() { + register_taxonomy( 'wptests_tax', 'post' ); + } + + /** + * Helper to get the term for the given taxonomy. + * + * @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. + */ + private function get_term( $taxonomy, $use_id ) { + $term = self::$terms[ $taxonomy ]; + if ( $use_id ) { + $term = $term->term_id; + } + + return $term; + } + /** * @dataProvider data_edit_term_link * @@ -36,7 +64,6 @@ class Tests_Link_EditTermLink extends WP_UnitTestCase { * @param string $expected Expected URL within admin of edit link. */ public function test_edit_term_link_for_permitted_user( $taxonomy, $use_id, $expected ) { - wp_set_current_user( self::$user_ids['admin'] ); $term = $this->get_term( $taxonomy, $use_id ); // Term IDs are not known by the data provider so need to be replaced. @@ -72,7 +99,6 @@ class Tests_Link_EditTermLink extends WP_UnitTestCase { * @param bool $use_id When true, pass term ID. Else, pass term object. */ public function test_edit_term_link_filter_is_int_by_term_id( $taxonomy, $use_id ) { - wp_set_current_user( self::$user_ids['admin'] ); $term = $this->get_term( $taxonomy, $use_id ); add_filter( @@ -96,7 +122,6 @@ class Tests_Link_EditTermLink extends WP_UnitTestCase { * @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 ) { - wp_set_current_user( self::$user_ids['admin'] ); $term = $this->get_term( $taxonomy, $use_id ); add_filter( @@ -139,42 +164,15 @@ class Tests_Link_EditTermLink extends WP_UnitTestCase { 'expected' => 'term.php?taxonomy=post_tag&tag_ID=%ID%&post_type=post', ), 'a custom taxonomy passing term_id' => array( - 'taxonomy' => 'custom_taxonomy', + 'taxonomy' => 'wptests_tax', 'use_id' => true, - 'expected' => 'term.php?taxonomy=custom_taxonomy&tag_ID=%ID%&post_type=post', + 'expected' => 'term.php?taxonomy=wptests_tax&tag_ID=%ID%&post_type=post', ), 'a custom taxonomy passing term object' => array( - 'taxonomy' => 'custom_taxonomy', + 'taxonomy' => 'wptests_tax', 'use_id' => false, - 'expected' => 'term.php?taxonomy=custom_taxonomy&tag_ID=%ID%&post_type=post', + 'expected' => 'term.php?taxonomy=wptests_tax&tag_ID=%ID%&post_type=post', ), ); } - - /** - * Helper to register a custom taxonomy for use in tests. - * - * @since 5.9.0 - */ - private static function register_custom_taxonomy() { - register_taxonomy( 'custom_taxonomy', 'post' ); - } - - /** - * Helper to get the term for the given taxonomy. - * - * @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. - */ - private function get_term( $taxonomy, $use_id ) { - $term = self::$terms[ $taxonomy ]; - if ( $use_id ) { - $term = $term->term_id; - } - - return $term; - } } diff --git a/tests/phpunit/tests/link/getEditTermLink.php b/tests/phpunit/tests/link/getEditTermLink.php index 3d1dd77da8..68f4ad7c02 100644 --- a/tests/phpunit/tests/link/getEditTermLink.php +++ b/tests/phpunit/tests/link/getEditTermLink.php @@ -12,7 +12,7 @@ class Tests_Link_GetEditTermLink extends WP_UnitTestCase { public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { self::register_custom_taxonomy(); - $taxonomies = array( 'category', 'post_tag', 'custom_taxonomy' ); + $taxonomies = array( 'category', 'post_tag', 'wptests_tax' ); foreach ( $taxonomies as $taxonomy ) { self::$terms[ $taxonomy ] = $factory->term->create_and_get( array( 'taxonomy' => $taxonomy ) ); } @@ -23,9 +23,123 @@ class Tests_Link_GetEditTermLink extends WP_UnitTestCase { public function set_up() { parent::set_up(); + wp_set_current_user( self::$user_ids['admin'] ); self::register_custom_taxonomy(); } + /** + * Helper to register a custom taxonomy for use in tests. + * + * @since 5.9.0 + */ + private static function register_custom_taxonomy() { + register_taxonomy( 'wptests_tax', 'post' ); + } + + /** + * Helper to get the term for the given taxonomy. + * + * @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. + */ + private function get_term( $taxonomy, $use_id ) { + $term = self::$terms[ $taxonomy ]; + if ( $use_id ) { + $term = $term->term_id; + } + + return $term; + } + + public function test_get_edit_term_link_default() { + $term1 = self::factory()->term->create( + array( + 'taxonomy' => 'wptests_tax', + 'name' => 'foo', + ) + ); + + $actual = get_edit_term_link( $term1, 'wptests_tax' ); + $expected = 'http://' . WP_TESTS_DOMAIN . '/wp-admin/term.php?taxonomy=wptests_tax&tag_ID=' . $term1 . '&post_type=post'; + $this->assertSame( $expected, $actual ); + } + + /** + * @ticket 32786 + */ + public function test_get_edit_term_link_invalid_id() { + $term1 = self::factory()->term->create( + array( + 'taxonomy' => 'wptests_tax', + 'name' => 'foo', + ) + ); + + $actual = get_edit_term_link( 12345, 'wptests_tax' ); + $this->assertNull( $actual ); + } + + /** + * @ticket 32786 + */ + public function test_get_edit_term_link_empty_id() { + $actual = get_edit_term_link( '', 'wptests_tax' ); + $this->assertNull( $actual ); + } + + /** + * @ticket 32786 + */ + public function test_get_edit_term_link_bad_tax() { + $actual = get_edit_term_link( '', 'bad_tax' ); + $this->assertNull( $actual ); + } + + /** + * @ticket 35922 + */ + public function test_taxonomy_should_not_be_required() { + $t = self::factory()->term->create( + array( + 'taxonomy' => 'wptests_tax', + 'name' => 'foo', + ) + ); + + $actual = get_edit_term_link( $t ); + $this->assertNotNull( $actual ); + } + + /** + * @ticket 35922 + */ + public function test_cap_check_should_use_correct_taxonomy_when_taxonomy_is_not_specified() { + register_taxonomy( + 'wptests_tax_subscriber', + 'post', + array( + 'capabilities' => array( + 'edit_terms' => 'read', + ), + ) + ); + + $t = self::factory()->term->create( + array( + 'taxonomy' => 'wptests_tax_subscriber', + 'name' => 'foo', + ) + ); + + wp_set_current_user( self::$user_ids['subscriber'] ); + + $actual = get_edit_term_link( $t ); + $this->assertNotNull( $actual ); + } + /** * @dataProvider data_get_edit_term_link * @@ -36,7 +150,6 @@ class Tests_Link_GetEditTermLink extends WP_UnitTestCase { * @param string $expected Expected URL within admin of edit link. */ public function test_get_edit_term_link_for_permitted_user( $taxonomy, $use_id, $expected ) { - wp_set_current_user( self::$user_ids['admin'] ); $term = $this->get_term( $taxonomy, $use_id ); // Term IDs are not known by the data provider so need to be replaced. @@ -72,7 +185,6 @@ class Tests_Link_GetEditTermLink extends WP_UnitTestCase { * @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_id( $taxonomy, $use_id ) { - wp_set_current_user( self::$user_ids['admin'] ); $term = $this->get_term( $taxonomy, $use_id ); add_filter( @@ -96,7 +208,6 @@ class Tests_Link_GetEditTermLink extends WP_UnitTestCase { * @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 ) { - wp_set_current_user( self::$user_ids['admin'] ); $term = $this->get_term( $taxonomy, $use_id ); add_filter( @@ -139,42 +250,15 @@ class Tests_Link_GetEditTermLink extends WP_UnitTestCase { 'expected' => 'term.php?taxonomy=post_tag&tag_ID=%ID%&post_type=post', ), 'a custom taxonomy passing term_id' => array( - 'taxonomy' => 'custom_taxonomy', + 'taxonomy' => 'wptests_tax', 'use_id' => true, - 'expected' => 'term.php?taxonomy=custom_taxonomy&tag_ID=%ID%&post_type=post', + 'expected' => 'term.php?taxonomy=wptests_tax&tag_ID=%ID%&post_type=post', ), 'a custom taxonomy passing term object' => array( - 'taxonomy' => 'custom_taxonomy', + 'taxonomy' => 'wptests_tax', 'use_id' => false, - 'expected' => 'term.php?taxonomy=custom_taxonomy&tag_ID=%ID%&post_type=post', + 'expected' => 'term.php?taxonomy=wptests_tax&tag_ID=%ID%&post_type=post', ), ); } - - /** - * Helper to register a custom taxonomy for use in tests. - * - * @since 5.9.0 - */ - private static function register_custom_taxonomy() { - register_taxonomy( 'custom_taxonomy', 'post' ); - } - - /** - * Helper to get the term for the given taxonomy. - * - * @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. - */ - private function get_term( $taxonomy, $use_id ) { - $term = self::$terms[ $taxonomy ]; - if ( $use_id ) { - $term = $term->term_id; - } - - return $term; - } } diff --git a/tests/phpunit/tests/term/getEditTermLink.php b/tests/phpunit/tests/term/getEditTermLink.php deleted file mode 100644 index 8d52cf3f88..0000000000 --- a/tests/phpunit/tests/term/getEditTermLink.php +++ /dev/null @@ -1,103 +0,0 @@ -user->create( array( 'role' => 'administrator' ) ) ); - register_taxonomy( 'wptests_tax', 'post' ); - } - - public function test_get_edit_term_link_default() { - $term1 = self::factory()->term->create( - array( - 'taxonomy' => 'wptests_tax', - 'name' => 'foo', - ) - ); - - $actual = get_edit_term_link( $term1, 'wptests_tax' ); - $expected = 'http://' . WP_TESTS_DOMAIN . '/wp-admin/term.php?taxonomy=wptests_tax&tag_ID=' . $term1 . '&post_type=post'; - $this->assertSame( $expected, $actual ); - } - - /** - * @ticket 32786 - */ - public function test_get_edit_term_link_invalid_id() { - $term1 = self::factory()->term->create( - array( - 'taxonomy' => 'wptests_tax', - 'name' => 'foo', - ) - ); - - $actual = get_edit_term_link( 12345, 'wptests_tax' ); - $this->assertNull( $actual ); - } - - /** - * @ticket 32786 - */ - public function test_get_edit_term_link_empty_id() { - $actual = get_edit_term_link( '', 'wptests_tax' ); - $this->assertNull( $actual ); - } - - /** - * @ticket 32786 - */ - public function test_get_edit_term_link_bad_tax() { - $actual = get_edit_term_link( '', 'bad_tax' ); - $this->assertNull( $actual ); - } - - /** - * @ticket 35922 - */ - public function test_taxonomy_should_not_be_required() { - $t = self::factory()->term->create( - array( - 'taxonomy' => 'wptests_tax', - 'name' => 'foo', - ) - ); - - $actual = get_edit_term_link( $t ); - $this->assertNotNull( $actual ); - } - - /** - * @ticket 35922 - */ - public function test_cap_check_should_use_correct_taxonomy_when_taxonomy_is_not_specified() { - register_taxonomy( - 'wptests_tax_subscriber', - 'post', - array( - 'capabilities' => array( - 'edit_terms' => 'read', - ), - ) - ); - - $t = self::factory()->term->create( - array( - 'taxonomy' => 'wptests_tax_subscriber', - 'name' => 'foo', - ) - ); - - $u = self::factory()->user->create( - array( - 'role' => 'subscriber', - ) - ); - wp_set_current_user( $u ); - - $actual = get_edit_term_link( $t ); - $this->assertNotNull( $actual ); - } -} diff --git a/tests/phpunit/tests/term/getTermLink.php b/tests/phpunit/tests/term/getTermLink.php index 61a4d08b7f..b3301c4f5f 100644 --- a/tests/phpunit/tests/term/getTermLink.php +++ b/tests/phpunit/tests/term/getTermLink.php @@ -22,6 +22,33 @@ class Tests_Term_GetTermLink extends WP_UnitTestCase { self::register_custom_taxonomy(); } + /** + * Helper to register a custom taxonomy for use in tests. + * + * @since 5.9.0 + */ + private static function register_custom_taxonomy() { + register_taxonomy( 'wptests_tax', 'post' ); + } + + /** + * Helper to get the term for the given taxonomy. + * + * @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. + */ + private function get_term( $taxonomy, $use_id ) { + $term = self::$terms[ $taxonomy ]; + if ( $use_id ) { + $term = $term->term_id; + } + + return $term; + } + public function test_integer_should_be_interpreted_as_term_id() { $t1 = self::factory()->term->create( array( @@ -320,31 +347,4 @@ class Tests_Term_GetTermLink extends WP_UnitTestCase { ), ); } - - /** - * Helper to register a custom taxonomy for use in tests. - * - * @since 5.9.0 - */ - private static function register_custom_taxonomy() { - register_taxonomy( 'wptests_tax', 'post' ); - } - - /** - * Helper to get the term for the given taxonomy. - * - * @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. - */ - private function get_term( $taxonomy, $use_id ) { - $term = self::$terms[ $taxonomy ]; - if ( $use_id ) { - $term = $term->term_id; - } - - return $term; - } }