Do not create shared taxonomy terms.

A "shared" term occurs when two entries in the `wp_term_taxonomy` table share a
single `term_id`, and thereby correspond to the same row in `wp_terms`. This
changeset stops the practice of creating shared terms: each new row in
`wp_term_taxonomy` will receive its own row in `wp_terms`. The new strategy
for term creation depends on whether the installation's database schema is up
to date for 4.1:

* If so, terms are allowed to be created with the same slug as an existing term, as long as they are in different taxonomies and do not share a parent. Thus, a new tag with the slug 'wordpress' can exist alongside a category with the slug 'wordpress'.
* If not, new terms will be forced to have unique slugs. Thus, on an installation containing a category with the slug 'wordpress', a new tag 'WordPress' will get the slug 'wordpress-2'.

Fixes #21950. See #5809.

git-svn-id: https://develop.svn.wordpress.org/trunk@30240 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2014-11-05 01:41:58 +00:00
parent 8096af102a
commit 88167ddc1a
2 changed files with 125 additions and 33 deletions
+100
View File
@@ -226,6 +226,93 @@ class Tests_Term extends WP_UnitTestCase {
$this->assertFalse( is_wp_error( $term20 ) );
}
/**
* @ticket 5809
*/
public function test_wp_insert_term_duplicate_slug_same_taxonomy() {
register_taxonomy( 'wptests_tax', 'post' );
$t = $this->factory->term->create( array(
'name' => 'Foo',
'slug' => 'foo',
'taxonomy' => 'wptests_tax',
) );
$term = get_term( $t, 'wptests_tax' );
$created = wp_insert_term( 'Foo 2', 'wptests_tax', array(
'slug' => 'foo',
) );
$created_term = get_term( $created['term_id'], 'wptests_tax' );
$this->assertSame( 'foo-2', $created_term->slug );
_unregister_taxonomy( 'wptests_tax', 'post' );
}
/**
* @ticket 5809
*/
public function test_wp_insert_term_duplicate_slug_different_taxonomy() {
register_taxonomy( 'wptests_tax', 'post' );
register_taxonomy( 'wptests_tax_2', 'post' );
$t = $this->factory->term->create( array(
'name' => 'Foo',
'slug' => 'foo',
'taxonomy' => 'wptests_tax',
) );
$term = get_term( $t, 'wptests_tax' );
$created = wp_insert_term( 'Foo 2', 'wptests_tax_2', array(
'slug' => 'foo',
) );
$this->assertFalse( is_wp_error( $created ) );
$new_term = get_term( $created['term_id'], 'wptests_tax_2' );
$this->assertSame( 'foo', $new_term->slug );
_unregister_taxonomy( 'wptests_tax', 'post' );
}
/**
* @ticket 5809
*/
public function test_wp_insert_term_duplicate_slug_different_taxonomy_before_410_schema_change() {
$db_version = get_option( 'db_version' );
update_option( 'db_version', 30055 );
register_taxonomy( 'wptests_tax', 'post' );
register_taxonomy( 'wptests_tax_2', 'post' );
$t = $this->factory->term->create( array(
'name' => 'Foo',
'slug' => 'foo',
'taxonomy' => 'wptests_tax',
) );
$term = get_term( $t, 'wptests_tax' );
$created = wp_insert_term( 'Foo 2', 'wptests_tax_2', array(
'slug' => 'foo',
) );
$this->assertFalse( is_wp_error( $created ) );
$new_term = get_term( $created['term_id'], 'wptests_tax_2' );
/*
* As of 4.1, we no longer create a shared term, but we also do not
* allow for duplicate slugs.
*/
$this->assertSame( 'foo-2', $new_term->slug );
$this->assertNotEquals( $new_term->term_id, $term->term_id );
_unregister_taxonomy( 'wptests_tax', 'post' );
update_option( 'db_version', $db_version );
}
public function test_wp_insert_term_alias_of_no_term_group() {
register_taxonomy( 'wptests_tax', 'post' );
$t1 = $this->factory->term->create( array(
@@ -353,6 +440,19 @@ class Tests_Term extends WP_UnitTestCase {
$this->assertEquals( $existing_term, $found->get_error_data() );
}
/**
* @ticket 5809
*/
public function test_wp_insert_term_should_not_create_shared_term() {
register_taxonomy( 'wptests_tax', 'post' );
register_taxonomy( 'wptests_tax_2', 'post' );
$t1 = wp_insert_term( 'Foo', 'wptests_tax' );
$t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );
$this->assertNotEquals( $t1['term_id'], $t2['term_id'] );
}
public function test_wp_insert_term_should_return_term_id_and_term_taxonomy_id() {
register_taxonomy( 'wptests_tax', 'post' );
$found = wp_insert_term( 'foo', 'wptests_tax' );