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