getTheTerms.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * @group taxonomy
  4. */
  5. class Tests_Term_GetTheTerms extends WP_UnitTestCase {
  6. protected $taxonomy = 'category';
  7. protected static $post_ids = array();
  8. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  9. self::$post_ids = $factory->post->create_many( 5 );
  10. }
  11. /**
  12. * @ticket 22560
  13. */
  14. function test_object_term_cache() {
  15. $post_id = self::$post_ids[0];
  16. $terms_1 = array( 'foo', 'bar', 'baz' );
  17. $terms_2 = array( 'bar', 'bing' );
  18. // Cache should be empty after a set.
  19. $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
  20. $this->assertSame( 3, count( $tt_1 ) );
  21. $this->assertFalse( wp_cache_get( $post_id, $this->taxonomy . '_relationships' ) );
  22. // wp_get_object_terms() does not prime the cache.
  23. wp_get_object_terms(
  24. $post_id,
  25. $this->taxonomy,
  26. array(
  27. 'fields' => 'names',
  28. 'orderby' => 't.term_id',
  29. )
  30. );
  31. $this->assertFalse( wp_cache_get( $post_id, $this->taxonomy . '_relationships' ) );
  32. // get_the_terms() does prime the cache.
  33. $terms = get_the_terms( $post_id, $this->taxonomy );
  34. $cache = wp_cache_get( $post_id, $this->taxonomy . '_relationships' );
  35. $this->assertInternalType( 'array', $cache );
  36. // Cache should be empty after a set.
  37. $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );
  38. $this->assertSame( 2, count( $tt_2 ) );
  39. $this->assertFalse( wp_cache_get( $post_id, $this->taxonomy . '_relationships' ) );
  40. }
  41. /**
  42. * @ticket 24189
  43. */
  44. function test_object_term_cache_when_term_changes() {
  45. $post_id = self::$post_ids[0];
  46. $tag_id = self::factory()->tag->create(
  47. array(
  48. 'name' => 'Amaze Tag',
  49. 'description' => 'My Amazing Tag',
  50. )
  51. );
  52. $tt_1 = wp_set_object_terms( $post_id, $tag_id, 'post_tag' );
  53. $terms = get_the_terms( $post_id, 'post_tag' );
  54. $this->assertSame( $tag_id, $terms[0]->term_id );
  55. $this->assertSame( 'My Amazing Tag', $terms[0]->description );
  56. $_updated = wp_update_term(
  57. $tag_id,
  58. 'post_tag',
  59. array(
  60. 'description' => 'This description is even more amazing!',
  61. )
  62. );
  63. $_new_term = get_term( $tag_id, 'post_tag' );
  64. $this->assertSame( $tag_id, $_new_term->term_id );
  65. $this->assertSame( 'This description is even more amazing!', $_new_term->description );
  66. $terms = get_the_terms( $post_id, 'post_tag' );
  67. $this->assertSame( $tag_id, $terms[0]->term_id );
  68. $this->assertSame( 'This description is even more amazing!', $terms[0]->description );
  69. }
  70. /**
  71. * @ticket 34262
  72. */
  73. public function test_get_the_terms_should_return_wp_term_objects_from_cache() {
  74. $p = self::$post_ids[0];
  75. register_taxonomy( 'wptests_tax', 'post' );
  76. $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
  77. wp_set_object_terms( $p, $t, 'wptests_tax' );
  78. // Prime the cache.
  79. get_the_terms( $p, 'wptests_tax' );
  80. $cached = get_the_terms( $p, 'wptests_tax' );
  81. $this->assertNotEmpty( $cached );
  82. $this->assertSame( $t, (int) $cached[0]->term_id );
  83. $this->assertInstanceOf( 'WP_Term', $cached[0] );
  84. }
  85. /**
  86. * @ticket 31086
  87. */
  88. public function test_get_the_terms_should_return_zero_indexed_array_when_cache_is_empty() {
  89. register_taxonomy( 'wptests_tax', 'post' );
  90. $p = self::$post_ids[0];
  91. wp_set_object_terms( $p, array( 'foo', 'bar' ), 'wptests_tax' );
  92. $found = get_the_terms( $p, 'wptests_tax' );
  93. $this->assertSameSets( array( 0, 1 ), array_keys( $found ) );
  94. }
  95. /**
  96. * @ticket 31086
  97. */
  98. public function test_get_the_terms_should_return_zero_indexed_array_when_cache_is_primed() {
  99. register_taxonomy( 'wptests_tax', 'post' );
  100. $p = self::$post_ids[0];
  101. wp_set_object_terms( $p, array( 'foo', 'bar' ), 'wptests_tax' );
  102. // Prime cache.
  103. update_object_term_cache( array( $p ), array( 'post' ) );
  104. $found = get_the_terms( $p, 'wptests_tax' );
  105. $this->assertSameSets( array( 0, 1 ), array_keys( $found ) );
  106. }
  107. /**
  108. * @ticket 35180
  109. * @ticket 28922
  110. */
  111. public function test_get_the_terms_should_return_results_ordered_by_name_when_pulling_from_cache() {
  112. register_taxonomy( 'wptests_tax', 'post' );
  113. $p = self::$post_ids[0];
  114. $t1 = self::factory()->term->create(
  115. array(
  116. 'taxonomy' => 'wptests_tax',
  117. 'name' => 'fff',
  118. )
  119. );
  120. $t2 = self::factory()->term->create(
  121. array(
  122. 'taxonomy' => 'wptests_tax',
  123. 'name' => 'aaa',
  124. )
  125. );
  126. $t3 = self::factory()->term->create(
  127. array(
  128. 'taxonomy' => 'wptests_tax',
  129. 'name' => 'zzz',
  130. )
  131. );
  132. wp_set_object_terms( $p, array( $t1, $t2, $t3 ), 'wptests_tax' );
  133. update_object_term_cache( $p, 'post' );
  134. $found = get_the_terms( $p, 'wptests_tax' );
  135. $this->assertSame( array( $t2, $t1, $t3 ), wp_list_pluck( $found, 'term_id' ) );
  136. }
  137. /**
  138. * @ticket 34723
  139. */
  140. function test_get_the_terms_should_return_wp_error_when_taxonomy_is_unregistered() {
  141. $p = self::$post_ids[0];
  142. $terms = get_the_terms( $p, 'this-taxonomy-does-not-exist' );
  143. $this->assertWPError( $terms );
  144. }
  145. /**
  146. * @ticket 36814
  147. */
  148. public function test_count_should_not_be_improperly_cached() {
  149. register_taxonomy( 'wptests_tax', 'post' );
  150. $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
  151. wp_set_object_terms( self::$post_ids[0], $t, 'wptests_tax' );
  152. $terms = get_the_terms( self::$post_ids[0], 'wptests_tax' );
  153. $this->assertSame( 1, $terms[0]->count );
  154. wp_set_object_terms( self::$post_ids[1], $t, 'wptests_tax' );
  155. $terms = get_the_terms( self::$post_ids[0], 'wptests_tax' );
  156. $this->assertSame( 2, $terms[0]->count );
  157. }
  158. /**
  159. * @ticket 36814
  160. */
  161. public function test_uncached_terms_should_be_primed_with_a_single_query() {
  162. global $wpdb;
  163. register_taxonomy( 'wptests_tax', 'post' );
  164. $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
  165. wp_set_object_terms( self::$post_ids[0], $terms, 'wptests_tax' );
  166. get_the_terms( self::$post_ids[0], 'wptests_tax' );
  167. // Clean cache for two of the terms.
  168. clean_term_cache( array( $terms[0], $terms[1] ), 'wptests_tax', false );
  169. $num_queries = $wpdb->num_queries;
  170. $found = get_the_terms( self::$post_ids[0], 'wptests_tax' );
  171. $this->assertSameSets( $terms, wp_list_pluck( $found, 'term_id' ) );
  172. $num_queries++;
  173. $this->assertSame( $num_queries, $wpdb->num_queries );
  174. }
  175. /**
  176. * @ticket 40306
  177. */
  178. public function test_term_cache_should_be_invalidated_on_set_object_terms() {
  179. register_taxonomy( 'wptests_tax', 'post' );
  180. // Temporarily disable term counting, which performs its own cache invalidation.
  181. wp_defer_term_counting( true );
  182. // Create Test Category.
  183. $term_id = self::factory()->term->create(
  184. array(
  185. 'taxonomy' => 'wptests_tax',
  186. )
  187. );
  188. $post_id = self::factory()->post->create();
  189. // Prime cache.
  190. get_the_terms( $post_id, 'wptests_tax' );
  191. wp_set_object_terms( $post_id, $term_id, 'wptests_tax' );
  192. $terms = get_the_terms( $post_id, 'wptests_tax' );
  193. // Re-activate term counting so this doesn't affect other tests.
  194. wp_defer_term_counting( false );
  195. $this->assertTrue( is_array( $terms ) );
  196. $this->assertSame( array( $term_id ), wp_list_pluck( $terms, 'term_id' ) );
  197. }
  198. /**
  199. * @ticket 40306
  200. */
  201. public function test_term_cache_should_be_invalidated_on_remove_object_terms() {
  202. register_taxonomy( 'wptests_tax', 'post' );
  203. // Create Test Category.
  204. $term_ids = self::factory()->term->create_many(
  205. 2,
  206. array(
  207. 'taxonomy' => 'wptests_tax',
  208. )
  209. );
  210. $post_id = self::factory()->post->create();
  211. wp_set_object_terms( $post_id, $term_ids, 'wptests_tax' );
  212. // Prime cache.
  213. get_the_terms( $post_id, 'wptests_tax' );
  214. // Temporarily disable term counting, which performs its own cache invalidation.
  215. wp_defer_term_counting( true );
  216. wp_remove_object_terms( $post_id, $term_ids[0], 'wptests_tax' );
  217. $terms = get_the_terms( $post_id, 'wptests_tax' );
  218. // Re-activate term counting so this doesn't affect other tests.
  219. wp_defer_term_counting( false );
  220. $this->assertTrue( is_array( $terms ) );
  221. $this->assertSame( array( $term_ids[1] ), wp_list_pluck( $terms, 'term_id' ) );
  222. }
  223. }