getTermField.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * @group taxonomy
  4. */
  5. class Tests_Term_getTermField extends WP_UnitTestCase {
  6. public $taxonomy = 'wptests_tax';
  7. function setUp() {
  8. parent::setUp();
  9. register_taxonomy( $this->taxonomy, 'post' );
  10. }
  11. /**
  12. * @ticket 34245
  13. */
  14. public function test_get_term_field_should_not_return_error_for_empty_taxonomy() {
  15. $term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
  16. $found = get_term_field( 'taxonomy', $term->term_id, '' );
  17. $this->assertNotWPError( $found );
  18. $this->assertSame( $this->taxonomy, $found );
  19. }
  20. /**
  21. * @ticket 34245
  22. */
  23. public function test_get_term_field_supplying_a_taxonomy() {
  24. $term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
  25. $found = get_term_field( 'taxonomy', $term->term_id, $term->taxonomy );
  26. $this->assertSame( $this->taxonomy, $found );
  27. }
  28. /**
  29. * @ticket 34245
  30. */
  31. public function test_get_term_field_supplying_no_taxonomy() {
  32. $term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
  33. $found = get_term_field( 'taxonomy', $term->term_id );
  34. $this->assertSame( $this->taxonomy, $found );
  35. }
  36. /**
  37. * @ticket 34245
  38. */
  39. public function test_get_term_field_should_accept_a_WP_Term_id_or_object() {
  40. $term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
  41. $this->assertInstanceOf( 'WP_Term', $term );
  42. $this->assertSame( $term->term_id, get_term_field( 'term_id', $term ) );
  43. $this->assertSame( $term->term_id, get_term_field( 'term_id', $term->data ) );
  44. $this->assertSame( $term->term_id, get_term_field( 'term_id', $term->term_id ) );
  45. }
  46. /**
  47. * @ticket 34245
  48. */
  49. public function test_get_term_field_invalid_taxonomy_should_return_WP_Error() {
  50. $term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
  51. $found = get_term_field( 'taxonomy', $term, 'foo-taxonomy' );
  52. $this->assertWPError( $found );
  53. $this->assertSame( 'invalid_taxonomy', $found->get_error_code() );
  54. }
  55. /**
  56. * @ticket 34245
  57. */
  58. public function test_get_term_field_invalid_term_should_return_WP_Error() {
  59. $found = get_term_field( 'taxonomy', 0, $this->taxonomy );
  60. $this->assertWPError( $found );
  61. $this->assertSame( 'invalid_term', $found->get_error_code() );
  62. $_found = get_term_field( 'taxonomy', 0 );
  63. $this->assertWPError( $_found );
  64. $this->assertSame( 'invalid_term', $_found->get_error_code() );
  65. }
  66. public function test_get_term_field_term_id() {
  67. $term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
  68. $this->assertSame( $term->term_id, get_term_field( 'term_id', $term ) );
  69. $this->assertSame( $term->term_id, get_term_field( 'term_id', $term->data ) );
  70. $this->assertSame( $term->term_id, get_term_field( 'term_id', $term->term_id ) );
  71. }
  72. public function test_get_term_field_name() {
  73. $name = rand_str( 15 );
  74. $term = self::factory()->term->create_and_get(
  75. array(
  76. 'name' => $name,
  77. 'taxonomy' => $this->taxonomy,
  78. )
  79. );
  80. $this->assertSame( $name, get_term_field( 'name', $term ) );
  81. $this->assertSame( $name, get_term_field( 'name', $term->data ) );
  82. $this->assertSame( $name, get_term_field( 'name', $term->term_id ) );
  83. }
  84. public function test_get_term_field_slug_when_slug_is_set() {
  85. $slug = rand_str( 15 );
  86. $term = self::factory()->term->create_and_get(
  87. array(
  88. 'taxonomy' => $this->taxonomy,
  89. 'slug' => $slug,
  90. )
  91. );
  92. $this->assertSame( $slug, get_term_field( 'slug', $term ) );
  93. $this->assertSame( $slug, get_term_field( 'slug', $term->data ) );
  94. $this->assertSame( $slug, get_term_field( 'slug', $term->term_id ) );
  95. }
  96. public function test_get_term_field_slug_when_slug_falls_back_from_name() {
  97. $name = rand_str( 15 );
  98. $term = self::factory()->term->create_and_get(
  99. array(
  100. 'taxonomy' => $this->taxonomy,
  101. 'name' => $name,
  102. )
  103. );
  104. $this->assertSame( $name, get_term_field( 'slug', $term ) );
  105. $this->assertSame( $name, get_term_field( 'slug', $term->data ) );
  106. $this->assertSame( $name, get_term_field( 'slug', $term->term_id ) );
  107. }
  108. public function test_get_term_field_slug_when_slug_and_name_are_not_set() {
  109. $term = self::factory()->term->create_and_get(
  110. array(
  111. 'taxonomy' => $this->taxonomy,
  112. )
  113. );
  114. $this->assertSame( $term->slug, get_term_field( 'slug', $term ) );
  115. $this->assertSame( $term->slug, get_term_field( 'slug', $term->data ) );
  116. $this->assertSame( $term->slug, get_term_field( 'slug', $term->term_id ) );
  117. }
  118. public function test_get_term_field_taxonomy() {
  119. $term = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
  120. $this->assertSame( $this->taxonomy, get_term_field( 'taxonomy', $term ) );
  121. $this->assertSame( $this->taxonomy, get_term_field( 'taxonomy', $term->data ) );
  122. $this->assertSame( $this->taxonomy, get_term_field( 'taxonomy', $term->term_id ) );
  123. }
  124. public function test_get_term_field_description() {
  125. $desc = wpautop( rand_str() );
  126. $term = self::factory()->term->create_and_get(
  127. array(
  128. 'taxonomy' => $this->taxonomy,
  129. 'description' => $desc,
  130. )
  131. );
  132. $this->assertSame( $desc, get_term_field( 'description', $term ) );
  133. $this->assertSame( $desc, get_term_field( 'description', $term->data ) );
  134. $this->assertSame( $desc, get_term_field( 'description', $term->term_id ) );
  135. }
  136. public function test_get_term_field_parent() {
  137. $parent = self::factory()->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
  138. $term = self::factory()->term->create_and_get(
  139. array(
  140. 'taxonomy' => $this->taxonomy,
  141. 'parent' => $parent->term_id,
  142. )
  143. );
  144. $this->assertSame( $parent->term_id, get_term_field( 'parent', $term ) );
  145. $this->assertSame( $parent->term_id, get_term_field( 'parent', $term->data ) );
  146. $this->assertSame( $parent->term_id, get_term_field( 'parent', $term->term_id ) );
  147. }
  148. }