getTerms.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * @group taxonomy
  4. */
  5. class Tests_Term_getTerms extends WP_UnitTestCase {
  6. function setUp() {
  7. parent::setUp();
  8. _clean_term_filters();
  9. wp_cache_delete( 'last_changed', 'terms' );
  10. }
  11. /**
  12. * @ticket 23326
  13. */
  14. function test_get_terms_cache() {
  15. global $wpdb;
  16. $posts = $this->factory->post->create_many( 15, array( 'post_type' => 'post' ) );
  17. foreach ( $posts as $post )
  18. wp_set_object_terms( $post, rand_str(), 'post_tag' );
  19. wp_cache_delete( 'last_changed', 'terms' );
  20. $this->assertFalse( wp_cache_get( 'last_changed', 'terms' ) );
  21. $num_queries = $wpdb->num_queries;
  22. // last_changed and num_queries should bump
  23. $terms = get_terms( 'post_tag' );
  24. $this->assertEquals( 15, count( $terms ) );
  25. $time1 = wp_cache_get( 'last_changed', 'terms' );
  26. $this->assertNotEmpty( $time1 );
  27. $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
  28. $num_queries = $wpdb->num_queries;
  29. // Again. last_changed and num_queries should remain the same.
  30. $terms = get_terms( 'post_tag' );
  31. $this->assertEquals( 15, count( $terms ) );
  32. $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'terms' ) );
  33. $this->assertEquals( $num_queries, $wpdb->num_queries );
  34. $num_queries = $wpdb->num_queries;
  35. // Different query. num_queries should bump, last_changed should remain the same.
  36. $terms = get_terms( 'post_tag', array( 'number' => 10 ) );
  37. $this->assertEquals( 10, count( $terms ) );
  38. $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'terms' ) );
  39. $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
  40. $num_queries = $wpdb->num_queries;
  41. // Again. last_changed and num_queries should remain the same.
  42. $terms = get_terms( 'post_tag', array( 'number' => 10 ) );
  43. $this->assertEquals( 10, count( $terms ) );
  44. $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'terms' ) );
  45. $this->assertEquals( $num_queries, $wpdb->num_queries );
  46. // Force last_changed to bump
  47. wp_delete_term( $terms[0]->term_id, 'post_tag' );
  48. $num_queries = $wpdb->num_queries;
  49. $this->assertNotEquals( $time1, $time2 = wp_cache_get( 'last_changed', 'terms' ) );
  50. // last_changed and num_queries should bump after a term is deleted
  51. $terms = get_terms( 'post_tag' );
  52. $this->assertEquals( 14, count( $terms ) );
  53. $this->assertEquals( $time2, wp_cache_get( 'last_changed', 'terms' ) );
  54. $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
  55. $num_queries = $wpdb->num_queries;
  56. // Again. last_changed and num_queries should remain the same.
  57. $terms = get_terms( 'post_tag' );
  58. $this->assertEquals( 14, count( $terms ) );
  59. $this->assertEquals( $time2, wp_cache_get( 'last_changed', 'terms' ) );
  60. $this->assertEquals( $num_queries, $wpdb->num_queries );
  61. // @todo Repeat with term insert and update.
  62. }
  63. /**
  64. * @ticket 23506
  65. */
  66. function test_get_terms_should_allow_arbitrary_indexed_taxonomies_array() {
  67. $term_id = $this->factory->tag->create();
  68. $terms = get_terms( array( '111' => 'post_tag' ), array( 'hide_empty' => false ) );
  69. $this->assertEquals( $term_id, reset( $terms )->term_id );
  70. }
  71. /**
  72. * @ticket 13661
  73. */
  74. function test_get_terms_fields() {
  75. $term_id1 = $this->factory->tag->create( array( 'slug' => 'woo', 'name' => 'WOO!' ) );
  76. $term_id2 = $this->factory->tag->create( array( 'slug' => 'hoo', 'name' => 'HOO!', 'parent' => $term_id1 ) );
  77. $terms_id_parent = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'id=>parent' ) );
  78. $this->assertEquals( array(
  79. $term_id1 => 0,
  80. $term_id2 => $term_id1
  81. ), $terms_id_parent );
  82. $terms_ids = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'ids' ) );
  83. $this->assertEqualSets( array( $term_id1, $term_id2 ), $terms_ids );
  84. $terms_name = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'names' ) );
  85. $this->assertEqualSets( array( 'WOO!', 'HOO!' ), $terms_name );
  86. $terms_id_name = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'id=>name' ) );
  87. $this->assertEquals( array(
  88. $term_id1 => 'WOO!',
  89. $term_id2 => 'HOO!',
  90. ), $terms_id_name );
  91. $terms_id_slug = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'id=>slug' ) );
  92. $this->assertEquals( array(
  93. $term_id1 => 'woo',
  94. $term_id2 => 'hoo'
  95. ), $terms_id_slug );
  96. }
  97. /**
  98. * @ticket 11823
  99. */
  100. function test_get_terms_include_exclude() {
  101. global $wpdb;
  102. $term_id1 = $this->factory->tag->create();
  103. $term_id2 = $this->factory->tag->create();
  104. $inc_terms = get_terms( 'post_tag', array(
  105. 'include' => array( $term_id1, $term_id2 ),
  106. 'hide_empty' => false
  107. ) );
  108. $this->assertEquals( array( $term_id1, $term_id2 ), wp_list_pluck( $inc_terms, 'term_id' ) );
  109. $exc_terms = get_terms( 'post_tag', array(
  110. 'exclude' => array( $term_id1, $term_id2 ),
  111. 'hide_empty' => false
  112. ) );
  113. $this->assertEquals( array(), wp_list_pluck( $exc_terms, 'term_id' ) );
  114. // These should not generate query errors.
  115. get_terms( 'post_tag', array( 'exclude' => array( 0 ), 'hide_empty' => false ) );
  116. $this->assertEmpty( $wpdb->last_error );
  117. get_terms( 'post_tag', array( 'exclude' => array( 'unexpected-string' ), 'hide_empty' => false ) );
  118. $this->assertEmpty( $wpdb->last_error );
  119. get_terms( 'post_tag', array( 'include' => array( 'unexpected-string' ), 'hide_empty' => false ) );
  120. $this->assertEmpty( $wpdb->last_error );
  121. }
  122. /**
  123. * @ticket 25710
  124. */
  125. function test_get_terms_exclude_tree() {
  126. $term_id_uncategorized = get_option( 'default_category' );
  127. $term_id1 = $this->factory->category->create();
  128. $term_id11 = $this->factory->category->create( array( 'parent' => $term_id1 ) );
  129. $term_id2 = $this->factory->category->create();
  130. $term_id22 = $this->factory->category->create( array( 'parent' => $term_id2 ) );
  131. // There's something else broken in the cache cleaning routines that leads to this having to be done manually
  132. delete_option( 'category_children' );
  133. $terms = get_terms( 'category', array(
  134. 'exclude' => $term_id_uncategorized,
  135. 'fields' => 'ids',
  136. 'hide_empty' => false,
  137. ) );
  138. $this->assertEquals( array( $term_id1, $term_id11, $term_id2, $term_id22 ), $terms );
  139. $terms = get_terms( 'category', array(
  140. 'fields' => 'ids',
  141. 'exclude_tree' => "$term_id1,$term_id_uncategorized",
  142. 'hide_empty' => false,
  143. ) );
  144. $this->assertEquals( array( $term_id2, $term_id22 ), $terms );
  145. }
  146. /**
  147. * @ticket 13992
  148. */
  149. function test_get_terms_search() {
  150. $term_id1 = $this->factory->tag->create( array( 'slug' => 'burrito' ) );
  151. $term_id2 = $this->factory->tag->create( array( 'name' => 'Wilbur' ) );
  152. $terms = get_terms( 'post_tag', array( 'hide_empty' => false, 'search' => 'bur', 'fields' => 'ids' ) );
  153. $this->assertEqualSets( array( $term_id1, $term_id2 ), $terms );
  154. }
  155. function test_get_terms_like() {
  156. $term_id1 = $this->factory->tag->create( array( 'name' => 'burrito', 'description' => 'This is a burrito.' ) );
  157. $term_id2 = $this->factory->tag->create( array( 'name' => 'taco', 'description' => 'Burning man.' ) );
  158. $terms = get_terms( 'post_tag', array( 'hide_empty' => false, 'name__like' => 'bur', 'fields' => 'ids' ) );
  159. $this->assertEqualSets( array( $term_id1 ), $terms );
  160. $terms2 = get_terms( 'post_tag', array( 'hide_empty' => false, 'description__like' => 'bur', 'fields' => 'ids' ) );
  161. $this->assertEqualSets( array( $term_id1, $term_id2 ), $terms2 );
  162. $terms3 = get_terms( 'post_tag', array( 'hide_empty' => false, 'name__like' => 'Bur', 'fields' => 'ids' ) );
  163. $this->assertEqualSets( array( $term_id1 ), $terms3 );
  164. $terms4 = get_terms( 'post_tag', array( 'hide_empty' => false, 'description__like' => 'Bur', 'fields' => 'ids' ) );
  165. $this->assertEqualSets( array( $term_id1, $term_id2 ), $terms4 );
  166. $terms5 = get_terms( 'post_tag', array( 'hide_empty' => false, 'name__like' => 'ENCHILADA', 'fields' => 'ids' ) );
  167. $this->assertEmpty( $terms5 );
  168. $terms6 = get_terms( 'post_tag', array( 'hide_empty' => false, 'description__like' => 'ENCHILADA', 'fields' => 'ids' ) );
  169. $this->assertEmpty( $terms6 );
  170. $terms7 = get_terms( 'post_tag', array( 'hide_empty' => false, 'name__like' => 'o', 'fields' => 'ids' ) );
  171. $this->assertEqualSets( array( $term_id1, $term_id2 ), $terms7 );
  172. $terms8 = get_terms( 'post_tag', array( 'hide_empty' => false, 'description__like' => '.', 'fields' => 'ids' ) );
  173. $this->assertEqualSets( array( $term_id1, $term_id2 ), $terms8 );
  174. }
  175. }