WordPress.org

Make WordPress Core

Ticket #17689: 17689.7.diff

File 17689.7.diff, 3.1 KB (added by aaroncampbell, 6 years ago)
  • src/wp-includes/taxonomy.php

     
    23912391
    23922392        $slug_provided = ! empty( $slug );
    23932393        if ( ! $slug_provided ) {
    2394                 $slug = sanitize_title($name);
     2394                $_name = trim( $name );
     2395                $existing_term = get_term_by( 'name', $_name, $taxonomy );
     2396                if ( $existing_term ) {
     2397                        $slug = $existing_term->slug;
     2398                } else {
     2399                        $slug = sanitize_title( $name );
     2400                }
    23952401        }
    23962402
    23972403        $term_group = 0;
     
    39313937                wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) );
    39323938
    39333939        return $parent;
    3934 }
    3935  No newline at end of file
     3940}
  • tests/phpunit/tests/taxonomy.php

     
    168168                wp_set_object_terms( $post->ID, $term['term_id'], 'category' );
    169169                $this->assertTrue( in_category( $term['term_id'], $post ) );
    170170        }
     171
     172        /**
     173         * @ticket 17689
     174         */
     175        function test_duplicate_name() {
     176                $term = $this->factory->tag->create_and_get( array( 'name' => 'Bozo' ) );
     177                $this->assertFalse( is_wp_error( $term ) );
     178                $this->assertTrue( empty( $term->errors ) );
     179
     180                // Test existing term name with unique slug
     181                $term1 = $this->factory->tag->create( array( 'name' => 'Bozo', 'slug' => 'bozo1' ) );
     182                $this->assertFalse( is_wp_error( $term1 ) );
     183                $this->assertTrue( empty($term1->errors ) );
     184
     185                // Test an existing term name
     186                $term2 = $this->factory->tag->create( array( 'name' => 'Bozo' ) );
     187                $this->assertTrue( is_wp_error( $term2 ) );
     188                $this->assertNotEmpty( $term2->errors );
     189
     190                // Test named terms ending in special characters
     191                $term3 = $this->factory->tag->create( array( 'name' => 'T$' ) );
     192                $term4 = $this->factory->tag->create( array( 'name' => 'T$$' ) );
     193                $term5 = $this->factory->tag->create( array( 'name' => 'T$$$' ) );
     194                $term6 = $this->factory->tag->create( array( 'name' => 'T$$$$' ) );
     195                $term7 = $this->factory->tag->create( array( 'name' => 'T$$$$' ) );
     196                $this->assertTrue( is_wp_error( $term7 ) );
     197                $this->assertNotEmpty( $term7->errors );
     198                $this->assertEquals( $term6, $term7->error_data['term_exists'] );
     199
     200                $terms = array_map( 'get_tag', array( $term3, $term4, $term5, $term6 ) );
     201                $this->assertCount( 4, array_unique( wp_list_pluck( $terms, 'slug' ) ) );
     202
     203                // Test named terms with only special characters
     204                $term8 = $this->factory->tag->create( array( 'name' => '$' ) );
     205                $term9 = $this->factory->tag->create( array( 'name' => '$$' ) );
     206                $term10 = $this->factory->tag->create( array( 'name' => '$$$' ) );
     207                $term11 = $this->factory->tag->create( array( 'name' => '$$$$' ) );
     208                $term12 = $this->factory->tag->create( array( 'name' => '$$$$' ) );
     209                $this->assertTrue( is_wp_error( $term12 ) );
     210                $this->assertNotEmpty( $term12->errors );
     211                $this->assertEquals( $term11, $term12->error_data['term_exists'] );
     212
     213                $terms = array_map( 'get_tag', array( $term8, $term9, $term10, $term11 ) );
     214                $this->assertCount( 4, array_unique( wp_list_pluck( $terms, 'slug' ) ) );
     215        }
    171216}