Make WordPress Core

Changeset 29798


Ignore:
Timestamp:
09/30/2014 02:59:10 PM (12 years ago)
Author:
boonebgorges
Message:

Improve unit test coverage for duplicate term creation.

These include an exhaustive set of tests for term_exists(), as well as tests
for wp_insert_term() that demonstrate failure when attempting to create a
duplicate term.

Props simonwheatley for an initial patch.

See #22023.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/term.php

    r29196 r29798  
    5454        }
    5555
     56        public function test_term_exists_term_0() {
     57                $this->assertSame( 0, term_exists( 0 ) );
     58        }
     59
     60        public function test_term_exists_term_int_taxonomy_nonempty_term_exists() {
     61                $t = $this->factory->term->create( array(
     62                        'taxonomy' => 'post_tag',
     63                ) );
     64
     65                $found = term_exists( intval( $t ), 'post_tag' );
     66                $this->assertEquals( $t, $found['term_id'] );
     67        }
     68
     69        public function test_term_exists_term_int_taxonomy_nonempty_term_does_not_exist() {
     70                $this->assertNull( term_exists( 54321, 'post_tag' ) );
     71        }
     72
     73        public function test_term_exists_term_int_taxonomy_nonempty_wrong_taxonomy() {
     74                $t = $this->factory->term->create( array(
     75                        'taxonomy' => 'post_tag',
     76                ) );
     77
     78                $this->assertNull( term_exists( intval( $t ), 'foo' ) );
     79        }
     80
     81        public function test_term_exists_term_int_taxonomy_empty_term_exists() {
     82                $t = $this->factory->term->create( array(
     83                        'taxonomy' => 'post_tag',
     84                ) );
     85
     86                $found = term_exists( intval( $t ), 'post_tag' );
     87                $this->assertEquals( $t, $found['term_id'] );
     88        }
     89
     90        public function test_term_exists_term_int_taxonomy_empty_term_does_not_exist() {
     91                $this->assertNull( term_exists( 54321 ) );
     92        }
     93
     94        public function test_term_exists_unslash_term() {
     95                $t = $this->factory->term->create( array(
     96                        'taxonomy' => 'post_tag',
     97                        'name' => 'I "love" WordPress\'s taxonomy system',
     98                ) );
     99
     100                $found = term_exists( 'I \"love\" WordPress\\\'s taxonomy system' );
     101                $this->assertEquals( $t, $found );
     102        }
     103
     104        public function test_term_exists_trim_term() {
     105                $t = $this->factory->term->create( array(
     106                        'taxonomy' => 'post_tag',
     107                        'slug' => 'foo',
     108                ) );
     109
     110                $found = term_exists( '  foo  ' );
     111                $this->assertEquals( $t, $found );
     112        }
     113
     114        public function test_term_exists_term_trimmed_to_empty_string() {
     115                $this->assertSame( 0, term_exists( '   ' ) );
     116        }
     117
     118        public function test_term_exists_taxonomy_nonempty_parent_nonempty_match_slug() {
     119                register_taxonomy( 'foo', 'post', array(
     120                        'hierarchical' => true,
     121                ) );
     122
     123                $parent_term = $this->factory->term->create( array(
     124                        'taxonomy' => 'foo',
     125                ) );
     126
     127                $t = $this->factory->term->create( array(
     128                        'taxonomy' => 'foo',
     129                        'parent' => $parent_term,
     130                        'slug' => 'child-term',
     131                ) );
     132
     133                $found = term_exists( 'child-term', 'foo', $parent_term );
     134
     135                _unregister_taxonomy( 'foo' );
     136
     137                $this->assertInternalType( 'array', $found );
     138                $this->assertEquals( $t, $found['term_id'] );
     139        }
     140
     141        public function test_term_exists_taxonomy_nonempty_parent_nonempty_match_name() {
     142                register_taxonomy( 'foo', 'post', array(
     143                        'hierarchical' => true,
     144                ) );
     145
     146                $parent_term = $this->factory->term->create( array(
     147                        'taxonomy' => 'foo',
     148                ) );
     149
     150                $t = $this->factory->term->create( array(
     151                        'taxonomy' => 'foo',
     152                        'parent' => $parent_term,
     153                        'name' => 'Child Term',
     154                ) );
     155
     156                $found = term_exists( 'Child Term', 'foo', $parent_term );
     157
     158                _unregister_taxonomy( 'foo' );
     159
     160                $this->assertInternalType( 'array', $found );
     161                $this->assertEquals( $t, $found['term_id'] );
     162        }
     163
     164        public function test_term_exists_taxonomy_nonempty_parent_empty_match_slug() {
     165                register_taxonomy( 'foo', 'post', array() );
     166
     167                $t = $this->factory->term->create( array(
     168                        'taxonomy' => 'foo',
     169                        'slug' => 'kewl-dudez',
     170                ) );
     171
     172                $found = term_exists( 'kewl-dudez', 'foo' );
     173
     174                _unregister_taxonomy( 'foo' );
     175
     176                $this->assertInternalType( 'array', $found );
     177                $this->assertEquals( $t, $found['term_id'] );
     178        }
     179
     180        public function test_term_exists_taxonomy_nonempty_parent_empty_match_name() {
     181                register_taxonomy( 'foo', 'post', array() );
     182
     183                $t = $this->factory->term->create( array(
     184                        'taxonomy' => 'foo',
     185                        'name' => 'Kewl Dudez',
     186                ) );
     187
     188                $found = term_exists( 'Kewl Dudez', 'foo' );
     189
     190                _unregister_taxonomy( 'foo' );
     191
     192                $this->assertInternalType( 'array', $found );
     193                $this->assertEquals( $t, $found['term_id'] );
     194        }
     195
     196        public function test_term_exists_taxonomy_empty_parent_empty_match_slug() {
     197                register_taxonomy( 'foo', 'post', array() );
     198
     199                $t = $this->factory->term->create( array(
     200                        'taxonomy' => 'foo',
     201                        'name' => 'juicy-fruit',
     202                ) );
     203
     204                $found = term_exists( 'juicy-fruit' );
     205
     206                _unregister_taxonomy( 'foo' );
     207
     208                $this->assertInternalType( 'string', $found );
     209                $this->assertEquals( $t, $found );
     210        }
     211
     212        public function test_term_exists_taxonomy_empty_parent_empty_match_name() {
     213                register_taxonomy( 'foo', 'post', array() );
     214
     215                $t = $this->factory->term->create( array(
     216                        'taxonomy' => 'foo',
     217                        'name' => 'Juicy Fruit',
     218                ) );
     219
     220                $found = term_exists( 'Juicy Fruit' );
     221
     222                _unregister_taxonomy( 'foo' );
     223
     224                $this->assertInternalType( 'string', $found );
     225                $this->assertEquals( $t, $found );
     226        }
     227
    56228        function test_term_exists_known() {
    57229                // insert a term
     
    71243                $this->assertEquals( 0, term_exists('') );
    72244                $this->assertEquals( 0, term_exists(NULL) );
     245        }
     246
     247        public function test_wp_insert_term_duplicate_name_slug_non_hierarchical() {
     248                register_taxonomy( 'foo', 'post', array() );
     249
     250                $existing_term = $this->factory->term->create( array(
     251                        'slug' => 'new-term',
     252                        'name' => 'New Term',
     253                        'taxonomy' => 'foo',
     254                ) );
     255
     256                $found = wp_insert_term( 'New Term', 'foo', array(
     257                        'slug' => 'new-term',
     258                ) );
     259
     260                _unregister_taxonomy( 'foo' );
     261
     262                $this->assertTrue( is_wp_error( $found ) );
     263                $this->assertEquals( $existing_term, $found->get_error_data() );
     264        }
     265
     266        public function test_wp_insert_term_duplicate_name_hierarchical() {
     267                register_taxonomy( 'foo', 'post', array(
     268                        'hierarchical' => true,
     269                ) );
     270
     271                $parent_term = $this->factory->term->create( array(
     272                        'taxonomy' => 'foo',
     273                ) );
     274
     275                $existing_term = $this->factory->term->create( array(
     276                        'name' => 'New Term',
     277                        'taxonomy' => 'foo',
     278                        'parent' => $parent_term,
     279                ) );
     280
     281                $found = wp_insert_term( 'New Term', 'foo', array(
     282                        'parent' => $parent_term,
     283                ) );
     284
     285                _unregister_taxonomy( 'foo' );
     286
     287                $this->assertTrue( is_wp_error( $found ) );
     288                $this->assertEquals( $existing_term, $found->get_error_data() );
     289        }
     290
     291        public function test_wp_insert_term_duplicate_name_slug_hierarchical() {
     292                register_taxonomy( 'foo', 'post', array(
     293                        'hierarchical' => true,
     294                ) );
     295
     296                $parent_term = $this->factory->term->create( array(
     297                        'taxonomy' => 'foo',
     298                ) );
     299
     300                $existing_term = $this->factory->term->create( array(
     301                        'name' => 'New Term',
     302                        'slug' => 'new-term-slug',
     303                        'taxonomy' => 'foo',
     304                        'parent' => $parent_term,
     305                ) );
     306
     307                $found = wp_insert_term( 'New Term', 'foo', array(
     308                        'parent' => $parent_term,
     309                        'slug' => 'new-term-slug',
     310                ) );
     311
     312                _unregister_taxonomy( 'foo' );
     313
     314                $this->assertTrue( is_wp_error( $found ) );
     315                $this->assertEquals( $existing_term, $found->get_error_data() );
    73316        }
    74317
Note: See TracChangeset for help on using the changeset viewer.