Make WordPress Core

Ticket #31328: 31328.6.diff

File 31328.6.diff, 7.8 KB (added by boonebgorges, 10 years ago)
  • src/wp-includes/taxonomy.php

    diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
    index c7a923e..fd74fbc 100644
    function wp_insert_term( $term, $taxonomy, $args = array() ) { 
    29032903                }
    29042904        }
    29052905
    2906         // Terms with duplicate names are not allowed at the same level of a taxonomy hierarchy.
     2906        /*
     2907         * Prevent the creation of terms with duplicate names at the same level of a taxonomy hierarchy,
     2908         * unless a unique slug has been explicitly provided.
     2909         */
    29072910        if ( $existing_term = get_term_by( 'name', $name, $taxonomy ) ) {
    2908                 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
    2909                         $siblings = get_terms( $taxonomy, array( 'fields' => 'names', 'get' => 'all', 'parent' => $parent ) );
    2910                         if ( in_array( $name, $siblings ) ) {
    2911                                 return new WP_Error( 'term_exists', __( 'A term with the name already exists with this parent.' ), $existing_term->term_id );
     2911                if ( ! $slug_provided || $existing_term->slug === $slug ) {
     2912                        if ( is_taxonomy_hierarchical( $taxonomy ) ) {
     2913                                $siblings = get_terms( $taxonomy, array( 'fields' => 'names', 'get' => 'all', 'parent' => $parent ) );
     2914                                if ( in_array( $name, $siblings ) ) {
     2915                                        return new WP_Error( 'term_exists', __( 'A term with the name already exists with this parent.' ), $existing_term->term_id );
     2916                                }
     2917                        } else {
     2918                                return new WP_Error( 'term_exists', __( 'A term with the name already exists in this taxonomy.' ), $existing_term->term_id );
    29122919                        }
    2913                 } else {
    2914                         return new WP_Error( 'term_exists', __( 'A term with the name already exists in this taxonomy.' ), $existing_term->term_id );
    29152920                }
    29162921        }
    29172922
  • tests/phpunit/tests/term.php

    diff --git tests/phpunit/tests/term.php tests/phpunit/tests/term.php
    index 712663e..0d65dda 100644
    class Tests_Term extends WP_UnitTestCase { 
    174174
    175175                // Test existing term name with unique slug
    176176                $term1 = $this->factory->tag->create( array( 'name' => 'Bozo', 'slug' => 'bozo1' ) );
    177                 $this->assertTrue( is_wp_error( $term1 ) );
    178                 $this->assertSame( 'term_exists', $term1->get_error_code() );
    179                 $this->assertEquals( $term->term_id, $term1->get_error_data() );
     177                $this->assertFalse( is_wp_error( $term1 ) );
    180178
    181179                // Test an existing term name
    182180                $term2 = $this->factory->tag->create( array( 'name' => 'Bozo' ) );
    class Tests_Term extends WP_UnitTestCase { 
    228226        }
    229227
    230228        /**
     229         * @ticket 31328
     230         */
     231        public function test_wp_insert_term_should_allow_duplicate_names_when_a_matching_slug_has_been_provided_in_non_hierarchical_taxonomy() {
     232                register_taxonomy( 'wptests_tax', 'post' );
     233                $t1 = $this->factory->term->create( array(
     234                        'name' => 'Foo',
     235                        'slug' => 'foo',
     236                        'taxonomy' => 'wptests_tax',
     237                ) );
     238
     239                $t2 = wp_insert_term( 'Foo', 'wptests_tax', array(
     240                        'slug' => 'foo',
     241                ) );
     242
     243                $this->assertWPError( $t2 );
     244                $this->assertSame( 'term_exists', $t2->get_error_code() );
     245        }
     246
     247        /**
     248         * @ticket 31328
     249         */
     250        public function test_wp_insert_term_should_allow_duplicate_names_when_a_unique_slug_has_been_provided_in_non_hierarchical_taxonomy() {
     251                register_taxonomy( 'wptests_tax', 'post' );
     252                $t1 = $this->factory->term->create( array(
     253                        'name' => 'Foo',
     254                        'slug' => 'foo',
     255                        'taxonomy' => 'wptests_tax',
     256                ) );
     257
     258                $t2 = wp_insert_term( 'Foo', 'wptests_tax', array(
     259                        'slug' => 'foo-unique',
     260                ) );
     261
     262                $this->assertFalse( is_wp_error( $t2 ) );
     263
     264                $t2_term = get_term( $t2['term_id'], 'wptests_tax' );
     265                $this->assertSame( 'foo-unique', $t2_term->slug );
     266                $this->assertSame( 'Foo', $t2_term->name );
     267        }
     268
     269        /**
     270         * @ticket 31328
     271         */
     272        public function test_wp_insert_term_should_not_allow_duplicate_names_when_the_slug_is_not_provided_in_non_hierarchical_taxonomy() {
     273                register_taxonomy( 'wptests_tax', 'post' );
     274                $t1 = $this->factory->term->create( array(
     275                        'name' => 'Foo',
     276                        'slug' => 'foo',
     277                        'taxonomy' => 'wptests_tax',
     278                ) );
     279
     280                $t2 = wp_insert_term( 'Foo', 'wptests_tax' );
     281
     282                $this->assertWPError( $t2 );
     283                $this->assertSame( 'term_exists', $t2->get_error_code() );
     284        }
     285
     286        /**
     287         * @ticket 31328
     288         */
     289        public function test_wp_insert_term_should_allow_duplicate_names_when_a_matching_slug_has_been_provided_in_hierarchical_taxonomy() {
     290                register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
     291                $t1 = $this->factory->term->create( array(
     292                        'name' => 'Foo',
     293                        'slug' => 'foo',
     294                        'taxonomy' => 'wptests_tax',
     295                ) );
     296
     297                $t2 = wp_insert_term( 'Foo', 'wptests_tax', array(
     298                        'slug' => 'foo',
     299                ) );
     300
     301                $this->assertWPError( $t2 );
     302                $this->assertSame( 'term_exists', $t2->get_error_code() );
     303        }
     304
     305        /**
     306         * @ticket 31328
     307         */
     308        public function test_wp_insert_term_should_allow_duplicate_names_when_a_unique_slug_has_been_provided_in_hierarchical_taxonomy() {
     309                register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
     310                $t1 = $this->factory->term->create( array(
     311                        'name' => 'Foo',
     312                        'slug' => 'foo',
     313                        'taxonomy' => 'wptests_tax',
     314                ) );
     315
     316                $t2 = wp_insert_term( 'Foo', 'wptests_tax', array(
     317                        'slug' => 'foo-unique',
     318                ) );
     319
     320                $this->assertFalse( is_wp_error( $t2 ) );
     321
     322                $t2_term = get_term( $t2['term_id'], 'wptests_tax' );
     323                $this->assertSame( 'foo-unique', $t2_term->slug );
     324                $this->assertSame( 'Foo', $t2_term->name );
     325        }
     326
     327        /**
     328         * @ticket 31328
     329         */
     330        public function test_wp_insert_term_should_not_allow_duplicate_names_when_the_slug_is_not_provided_in_hierarchical_taxonomy() {
     331                register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
     332                $t1 = $this->factory->term->create( array(
     333                        'name' => 'Foo',
     334                        'slug' => 'foo',
     335                        'taxonomy' => 'wptests_tax',
     336                ) );
     337
     338                $t2 = wp_insert_term( 'Foo', 'wptests_tax' );
     339
     340                $this->assertWPError( $t2 );
     341                $this->assertSame( 'term_exists', $t2->get_error_code() );
     342        }
     343        /**
    231344         * @ticket 5809
    232345         */
    233346        public function test_wp_insert_term_duplicate_slug_same_taxonomy() {
    class Tests_Term extends WP_UnitTestCase { 
    370483                $this->assertSame( 0, $created_term->term_group );
    371484        }
    372485
    373         public function test_wp_insert_term_duplicate_name_slug_non_hierarchical() {
    374                 register_taxonomy( 'foo', 'post', array() );
    375 
    376                 $existing_term = $this->factory->term->create( array(
    377                         'slug' => 'new-term',
    378                         'name' => 'New Term',
    379                         'taxonomy' => 'foo',
    380                 ) );
    381 
    382                 $found = wp_insert_term( 'New Term', 'foo', array(
    383                         'slug' => 'new-term',
    384                 ) );
    385 
    386                 _unregister_taxonomy( 'foo' );
    387 
    388                 $this->assertTrue( is_wp_error( $found ) );
    389                 $this->assertEquals( $existing_term, $found->get_error_data() );
    390         }
    391 
    392         public function test_wp_insert_term_duplicate_name_hierarchical() {
    393                 register_taxonomy( 'foo', 'post', array(
    394                         'hierarchical' => true,
    395                 ) );
    396 
    397                 $parent_term = $this->factory->term->create( array(
    398                         'taxonomy' => 'foo',
    399                 ) );
    400 
    401                 $existing_term = $this->factory->term->create( array(
    402                         'name' => 'New Term',
    403                         'taxonomy' => 'foo',
    404                         'parent' => $parent_term,
    405                 ) );
    406 
    407                 $found = wp_insert_term( 'New Term', 'foo', array(
    408                         'parent' => $parent_term,
    409                 ) );
    410 
    411                 _unregister_taxonomy( 'foo' );
    412 
    413                 $this->assertTrue( is_wp_error( $found ) );
    414                 $this->assertEquals( $existing_term, $found->get_error_data() );
    415         }
    416 
    417         public function test_wp_insert_term_duplicate_name_slug_hierarchical() {
    418                 register_taxonomy( 'foo', 'post', array(
    419                         'hierarchical' => true,
    420                 ) );
    421 
    422                 $parent_term = $this->factory->term->create( array(
    423                         'taxonomy' => 'foo',
    424                 ) );
    425 
    426                 $existing_term = $this->factory->term->create( array(
    427                         'name' => 'New Term',
    428                         'slug' => 'new-term-slug',
    429                         'taxonomy' => 'foo',
    430                         'parent' => $parent_term,
    431                 ) );
    432 
    433                 $found = wp_insert_term( 'New Term', 'foo', array(
    434                         'parent' => $parent_term,
    435                         'slug' => 'new-term-slug',
    436                 ) );
    437 
    438                 _unregister_taxonomy( 'foo' );
    439 
    440                 $this->assertTrue( is_wp_error( $found ) );
    441                 $this->assertEquals( $existing_term, $found->get_error_data() );
    442         }
    443 
    444486        /**
    445487         * @ticket 5809
    446488         */