From 0027615ed1d6afc53754249e66213ab20d776ba0 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Fri, 15 May 2015 20:37:51 +0000 Subject: [PATCH] Improve unit tests for `wp_unique_term_slug()`. See #20783. git-svn-id: https://develop.svn.wordpress.org/trunk@32507 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/term.php | 44 ------- tests/phpunit/tests/term/wpUniqueTermSlug.php | 108 ++++++++++++++++++ 2 files changed, 108 insertions(+), 44 deletions(-) create mode 100644 tests/phpunit/tests/term/wpUniqueTermSlug.php diff --git a/tests/phpunit/tests/term.php b/tests/phpunit/tests/term.php index 6f5b8a46a1..60409dda15 100644 --- a/tests/phpunit/tests/term.php +++ b/tests/phpunit/tests/term.php @@ -471,50 +471,6 @@ class Tests_Term extends WP_UnitTestCase { $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] ); } - function test_wp_unique_term_slug() { - // set up test data - $a = wp_insert_term( 'parent', $this->taxonomy ); - $this->assertInternalType( 'array', $a ); - $b = wp_insert_term( 'child', $this->taxonomy, array( 'parent' => $a['term_id'] ) ); - $this->assertInternalType( 'array', $b ); - $c = wp_insert_term( 'neighbor', $this->taxonomy ); - $this->assertInternalType( 'array', $c ); - $d = wp_insert_term( 'pet', $this->taxonomy, array( 'parent' => $c['term_id'] ) ); - $this->assertInternalType( 'array', $c ); - - $a_term = get_term( $a['term_id'], $this->taxonomy ); - $b_term = get_term( $b['term_id'], $this->taxonomy ); - $c_term = get_term( $c['term_id'], $this->taxonomy ); - $d_term = get_term( $d['term_id'], $this->taxonomy ); - - // a unique slug gets unchanged - $this->assertEquals( 'unique-term', wp_unique_term_slug( 'unique-term', $c_term ) ); - - // a non-hierarchicial dupe gets suffixed with "-#" - $this->assertEquals( 'parent-2', wp_unique_term_slug( 'parent', $c_term ) ); - - // a hierarchical dupe initially gets suffixed with its parent term - $this->assertEquals( 'child-neighbor', wp_unique_term_slug( 'child', $d_term ) ); - - // a hierarchical dupe whose parent already contains the {term}-{parent term} - // term gets suffixed with parent term name and then '-#' - $e = wp_insert_term( 'child-neighbor', $this->taxonomy, array( 'parent' => $c['term_id'] ) ); - $this->assertEquals( 'child-neighbor-2', wp_unique_term_slug( 'child', $d_term ) ); - - $f = wp_insert_term( 'foo', $this->taxonomy ); - $this->assertInternalType( 'array', $f ); - $f_term = get_term( $f['term_id'], $this->taxonomy ); - $this->assertEquals( 'foo', $f_term->slug ); - $this->assertEquals( 'foo', wp_unique_term_slug( 'foo', $f_term ) ); - - $g = wp_insert_term( 'foo', $this->taxonomy ); - $this->assertInstanceOf( 'WP_Error', $g ); - - // clean up - foreach ( array( $a, $b, $c, $d, $e, $f ) as $t ) - $this->assertTrue( wp_delete_term( $t['term_id'], $this->taxonomy ) ); - } - /** * @ticket 25852 */ diff --git a/tests/phpunit/tests/term/wpUniqueTermSlug.php b/tests/phpunit/tests/term/wpUniqueTermSlug.php new file mode 100644 index 0000000000..c71523f4da --- /dev/null +++ b/tests/phpunit/tests/term/wpUniqueTermSlug.php @@ -0,0 +1,108 @@ + false ) ); + register_taxonomy( 'wptests_tax2', 'post', array( 'hierarchical' => true ) ); + } + + public function test_unique_slug_should_be_unchanged() { + $term = $this->factory->term->create_and_get( array( + 'taxonomy' => 'wptests_tax1', + 'name' => 'foo', + 'slug' => 'foo', + ) ); + + $actual = wp_unique_term_slug( 'bar', $term ); + $this->assertEquals( 'bar', $actual ); + } + + public function test_nonunique_slug_in_different_taxonomy_should_be_unchanged() { + $term1 = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax2', + 'name' => 'bar', + 'slug' => 'bar', + ) ); + + $term2 = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax1', + 'name' => 'foo', + 'slug' => 'foo', + ) ); + $term2_object = get_term( $term2, 'wptests_tax1' ); + + $actual = wp_unique_term_slug( 'bar', $term2_object ); + $this->assertEquals( 'bar', $actual ); + } + + public function test_nonunique_slug_in_same_nonhierarchical_taxonomy_should_be_changed() { + $term1 = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax1', + 'name' => 'bar', + 'slug' => 'bar', + ) ); + + $term2 = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax1', + 'name' => 'foo', + 'slug' => 'foo', + ) ); + $term2_object = get_term( $term2, 'wptests_tax1' ); + + $actual = wp_unique_term_slug( 'bar', $term2_object ); + $this->assertEquals( 'bar-2', $actual ); + } + + public function test_nonunique_slug_in_same_hierarchical_taxonomy_with_same_parent_should_be_suffixed_with_parent_slug() { + $parent = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax2', + 'slug' => 'parent-term', + ) ); + + $term1 = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax2', + 'name' => 'bar', + 'slug' => 'bar', + 'parent' => $parent, + ) ); + + $term2 = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax2', + 'name' => 'foo', + 'slug' => 'foo', + 'parent' => $parent, + ) ); + $term2_object = get_term( $term2, 'wptests_tax2' ); + + $actual = wp_unique_term_slug( 'bar', $term2_object ); + $this->assertEquals( 'bar-parent-term', $actual ); + } + + public function test_nonunique_slug_in_same_hierarchical_taxonomy_at_different_level_of_hierarchy_should_be_suffixed_with_number() { + $parent = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax2', + 'slug' => 'parent-term', + ) ); + + $term1 = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax2', + 'name' => 'bar', + 'slug' => 'bar', + 'parent' => $parent, + ) ); + + $term2 = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax2', + 'name' => 'foo', + 'slug' => 'foo', + ) ); + $term2_object = get_term( $term2, 'wptests_tax2' ); + + $actual = wp_unique_term_slug( 'bar', $term2_object ); + $this->assertEquals( 'bar-2', $actual ); + } +}