wpUniqueTermSlug.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * @group taxonomy
  4. */
  5. class Tests_Term_WpUniqueTermSlug extends WP_UnitTestCase {
  6. public function setUp() {
  7. parent::setUp();
  8. register_taxonomy( 'wptests_tax1', 'post', array( 'hierarchical' => false ) );
  9. register_taxonomy( 'wptests_tax2', 'post', array( 'hierarchical' => true ) );
  10. }
  11. public function test_unique_slug_should_be_unchanged() {
  12. $term = self::factory()->term->create_and_get(
  13. array(
  14. 'taxonomy' => 'wptests_tax1',
  15. 'name' => 'foo',
  16. 'slug' => 'foo',
  17. )
  18. );
  19. $actual = wp_unique_term_slug( 'bar', $term );
  20. $this->assertSame( 'bar', $actual );
  21. }
  22. public function test_nonunique_slug_in_different_taxonomy_should_be_unchanged() {
  23. $term1 = self::factory()->term->create(
  24. array(
  25. 'taxonomy' => 'wptests_tax2',
  26. 'name' => 'bar',
  27. 'slug' => 'bar',
  28. )
  29. );
  30. $term2 = self::factory()->term->create(
  31. array(
  32. 'taxonomy' => 'wptests_tax1',
  33. 'name' => 'foo',
  34. 'slug' => 'foo',
  35. )
  36. );
  37. $term2_object = get_term( $term2, 'wptests_tax1' );
  38. $actual = wp_unique_term_slug( 'bar', $term2_object );
  39. $this->assertSame( 'bar', $actual );
  40. }
  41. public function test_nonunique_slug_in_same_nonhierarchical_taxonomy_should_be_changed() {
  42. $term1 = self::factory()->term->create(
  43. array(
  44. 'taxonomy' => 'wptests_tax1',
  45. 'name' => 'bar',
  46. 'slug' => 'bar',
  47. )
  48. );
  49. $term2 = self::factory()->term->create(
  50. array(
  51. 'taxonomy' => 'wptests_tax1',
  52. 'name' => 'foo',
  53. 'slug' => 'foo',
  54. )
  55. );
  56. $term2_object = get_term( $term2, 'wptests_tax1' );
  57. $actual = wp_unique_term_slug( 'bar', $term2_object );
  58. $this->assertSame( 'bar-2', $actual );
  59. }
  60. public function test_nonunique_slug_in_same_hierarchical_taxonomy_with_same_parent_should_be_suffixed_with_parent_slug() {
  61. $parent = self::factory()->term->create(
  62. array(
  63. 'taxonomy' => 'wptests_tax2',
  64. 'slug' => 'parent-term',
  65. )
  66. );
  67. $term1 = self::factory()->term->create(
  68. array(
  69. 'taxonomy' => 'wptests_tax2',
  70. 'name' => 'bar',
  71. 'slug' => 'bar',
  72. 'parent' => $parent,
  73. )
  74. );
  75. $term2 = self::factory()->term->create(
  76. array(
  77. 'taxonomy' => 'wptests_tax2',
  78. 'name' => 'foo',
  79. 'slug' => 'foo',
  80. 'parent' => $parent,
  81. )
  82. );
  83. $term2_object = get_term( $term2, 'wptests_tax2' );
  84. $actual = wp_unique_term_slug( 'bar', $term2_object );
  85. $this->assertSame( 'bar-parent-term', $actual );
  86. }
  87. public function test_nonunique_slug_in_same_hierarchical_taxonomy_at_different_level_of_hierarchy_should_be_suffixed_with_number() {
  88. $parent = self::factory()->term->create(
  89. array(
  90. 'taxonomy' => 'wptests_tax2',
  91. 'slug' => 'parent-term',
  92. )
  93. );
  94. $term1 = self::factory()->term->create(
  95. array(
  96. 'taxonomy' => 'wptests_tax2',
  97. 'name' => 'bar',
  98. 'slug' => 'bar',
  99. 'parent' => $parent,
  100. )
  101. );
  102. $term2 = self::factory()->term->create(
  103. array(
  104. 'taxonomy' => 'wptests_tax2',
  105. 'name' => 'foo',
  106. 'slug' => 'foo',
  107. )
  108. );
  109. $term2_object = get_term( $term2, 'wptests_tax2' );
  110. $actual = wp_unique_term_slug( 'bar', $term2_object );
  111. $this->assertSame( 'bar-2', $actual );
  112. }
  113. /**
  114. * @ticket 46431
  115. */
  116. public function test_duplicate_parent_suffixed_slug_should_get_numeric_suffix() {
  117. $t1 = self::factory()->term->create(
  118. array(
  119. 'taxonomy' => 'wptests_tax2',
  120. 'name' => 'Animal',
  121. 'slug' => 'animal',
  122. )
  123. );
  124. $t2 = self::factory()->term->create(
  125. array(
  126. 'taxonomy' => 'wptests_tax2',
  127. 'name' => 'Dog',
  128. 'slug' => 'dog',
  129. )
  130. );
  131. $t3 = self::factory()->term->create(
  132. array(
  133. 'taxonomy' => 'wptests_tax2',
  134. 'name' => 'Cat',
  135. 'slug' => 'dog-animal',
  136. 'parent' => $t1,
  137. )
  138. );
  139. $t4 = self::factory()->term->create(
  140. array(
  141. 'taxonomy' => 'wptests_tax2',
  142. 'name' => 'Giraffe',
  143. 'slug' => 'giraffe',
  144. 'parent' => $t1,
  145. )
  146. );
  147. $term = get_term( $t4 );
  148. $slug = wp_unique_term_slug( 'dog', $term );
  149. $this->assertSame( 'dog-animal-2', $slug );
  150. }
  151. }