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
This commit is contained in:
Boone Gorges 2015-05-15 20:37:51 +00:00
parent 691656f2cf
commit 0027615ed1
2 changed files with 108 additions and 44 deletions

View File

@ -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
*/

View File

@ -0,0 +1,108 @@
<?php
/**
* @group taxonomy
*/
class Tests_Term_WpUniqueTermSlug extends WP_UnitTestCase {
public function setUp() {
parent::setUp();
register_taxonomy( 'wptests_tax1', 'post', array( 'hierarchical' => 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 );
}
}