cache.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php
  2. /**
  3. * @group taxonomy
  4. */
  5. class Tests_Term_Cache extends WP_UnitTestCase {
  6. function setUp() {
  7. parent::setUp();
  8. wp_cache_delete( 'last_changed', 'terms' );
  9. }
  10. /**
  11. * @ticket 25711
  12. */
  13. function test_category_children_cache() {
  14. // Test with only one Parent => Child.
  15. $term_id1 = self::factory()->category->create();
  16. $term_id1_child = self::factory()->category->create( array( 'parent' => $term_id1 ) );
  17. $hierarchy = _get_term_hierarchy( 'category' );
  18. $this->assertSame( array( $term_id1 => array( $term_id1_child ) ), $hierarchy );
  19. // Add another Parent => Child.
  20. $term_id2 = self::factory()->category->create();
  21. $term_id2_child = self::factory()->category->create( array( 'parent' => $term_id2 ) );
  22. $hierarchy = _get_term_hierarchy( 'category' );
  23. $this->assertSame(
  24. array(
  25. $term_id1 => array( $term_id1_child ),
  26. $term_id2 => array( $term_id2_child ),
  27. ),
  28. $hierarchy
  29. );
  30. }
  31. /**
  32. * @ticket 22526
  33. */
  34. function test_category_name_change() {
  35. $term = self::factory()->category->create_and_get( array( 'name' => 'Foo' ) );
  36. $post_id = self::factory()->post->create();
  37. wp_set_post_categories( $post_id, $term->term_id );
  38. $post = get_post( $post_id );
  39. $cats1 = get_the_category( $post->ID );
  40. $this->assertSame( $term->name, reset( $cats1 )->name );
  41. wp_update_term( $term->term_id, 'category', array( 'name' => 'Bar' ) );
  42. $cats2 = get_the_category( $post->ID );
  43. $this->assertNotEquals( $term->name, reset( $cats2 )->name );
  44. }
  45. /**
  46. * @ticket 14485
  47. */
  48. function test_hierachy_invalidation() {
  49. $tax = 'burrito';
  50. register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
  51. $this->assertTrue( get_taxonomy( $tax )->hierarchical );
  52. $step = 1;
  53. $parent_id = 0;
  54. $children = 0;
  55. foreach ( range( 1, 9 ) as $i ) {
  56. switch ( $step ) {
  57. case 1:
  58. $parent = wp_insert_term( 'Parent' . $i, $tax );
  59. $parent_id = $parent['term_id'];
  60. break;
  61. case 2:
  62. $parent = wp_insert_term( 'Child' . $i, $tax, array( 'parent' => $parent_id ) );
  63. $parent_id = $parent['term_id'];
  64. $children++;
  65. break;
  66. case 3:
  67. wp_insert_term( 'Grandchild' . $i, $tax, array( 'parent' => $parent_id ) );
  68. $parent_id = 0;
  69. $children++;
  70. break;
  71. }
  72. $terms = get_terms( $tax, array( 'hide_empty' => false ) );
  73. $this->assertSame( $i, count( $terms ) );
  74. if ( $i > 1 ) {
  75. $hierarchy = _get_term_hierarchy( $tax );
  76. $this->assertNotEmpty( $hierarchy );
  77. $this->assertSame( $children, count( $hierarchy, COUNT_RECURSIVE ) - count( $hierarchy ) );
  78. }
  79. if ( 0 === ( $i % 3 ) ) {
  80. $step = 1;
  81. } else {
  82. $step++;
  83. }
  84. }
  85. _unregister_taxonomy( $tax );
  86. }
  87. public function test_get_term_should_update_term_cache_when_passed_an_object() {
  88. global $wpdb;
  89. register_taxonomy( 'wptests_tax', 'post' );
  90. $term = self::factory()->term->create(
  91. array(
  92. 'taxonomy' => 'wptests_tax',
  93. )
  94. );
  95. $term_object = get_term( $term, 'wptests_tax' );
  96. wp_cache_delete( $term, 'terms' );
  97. // Affirm that the cache is empty.
  98. $this->assertEmpty( wp_cache_get( $term, 'terms' ) );
  99. $num_queries = $wpdb->num_queries;
  100. // get_term() will only be update the cache if the 'filter' prop is unset.
  101. unset( $term_object->filter );
  102. $term_object_2 = get_term( $term_object, 'wptests_tax' );
  103. // No new queries should have fired.
  104. $this->assertSame( $num_queries, $wpdb->num_queries );
  105. $this->assertSame( $term_object, $term_object_2 );
  106. }
  107. public function test_get_term_should_update_term_cache_when_passed_a_valid_term_identifier() {
  108. global $wpdb;
  109. register_taxonomy( 'wptests_tax', 'post' );
  110. $term = self::factory()->term->create(
  111. array(
  112. 'taxonomy' => 'wptests_tax',
  113. )
  114. );
  115. wp_cache_delete( $term, 'terms' );
  116. // Affirm that the cache is empty.
  117. $this->assertEmpty( wp_cache_get( $term, 'terms' ) );
  118. $num_queries = $wpdb->num_queries;
  119. // Prime cache.
  120. $term_object = get_term( $term, 'wptests_tax' );
  121. $this->assertNotEmpty( wp_cache_get( $term, 'terms' ) );
  122. $this->assertSame( $num_queries + 1, $wpdb->num_queries );
  123. $term_object_2 = get_term( $term, 'wptests_tax' );
  124. // No new queries should have fired.
  125. $this->assertSame( $num_queries + 1, $wpdb->num_queries );
  126. $this->assertEquals( $term_object, $term_object_2 );
  127. }
  128. public function test_get_term_by_should_update_term_cache_when_passed_a_valid_term_identifier() {
  129. global $wpdb;
  130. register_taxonomy( 'wptests_tax', 'post' );
  131. $term = self::factory()->term->create(
  132. array(
  133. 'taxonomy' => 'wptests_tax',
  134. )
  135. );
  136. wp_cache_delete( $term, 'terms' );
  137. // Affirm that the cache is empty.
  138. $this->assertEmpty( wp_cache_get( $term, 'terms' ) );
  139. $num_queries = $wpdb->num_queries;
  140. // Prime cache.
  141. $term_object = get_term_by( 'id', $term, 'wptests_tax' );
  142. $this->assertNotEmpty( wp_cache_get( $term, 'terms' ) );
  143. $this->assertSame( $num_queries + 1, $wpdb->num_queries );
  144. $term_object_2 = get_term( $term, 'wptests_tax' );
  145. // No new queries should have fired.
  146. $this->assertSame( $num_queries + 1, $wpdb->num_queries );
  147. $this->assertEquals( $term_object, $term_object_2 );
  148. }
  149. /**
  150. * @ticket 30749
  151. */
  152. public function test_get_terms_should_update_cache_for_located_terms() {
  153. global $wpdb;
  154. register_taxonomy( 'wptests_tax', 'post' );
  155. $terms = self::factory()->term->create_many(
  156. 5,
  157. array(
  158. 'taxonomy' => 'wptests_tax',
  159. )
  160. );
  161. $term_objects = get_terms(
  162. 'wptests_tax',
  163. array(
  164. 'hide_empty' => false,
  165. )
  166. );
  167. $num_queries = $wpdb->num_queries;
  168. foreach ( $terms as $term_id ) {
  169. get_term( $term_id, 'wptests_tax' );
  170. }
  171. $this->assertSame( $num_queries, $wpdb->num_queries );
  172. _unregister_taxonomy( 'wptests_tax' );
  173. }
  174. /**
  175. * @ticket 35462
  176. */
  177. public function test_term_objects_should_not_be_modified_by_update_term_cache() {
  178. register_taxonomy( 'wptests_tax', 'post' );
  179. $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
  180. $p = self::factory()->post->create();
  181. wp_set_object_terms( $p, $t, 'wptests_tax' );
  182. $terms = wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'all_with_object_id' ) );
  183. update_term_cache( $terms );
  184. foreach ( $terms as $term ) {
  185. $this->assertSame( $p, $term->object_id );
  186. }
  187. }
  188. /**
  189. * @ticket 21760
  190. */
  191. function test_get_term_by_slug_cache() {
  192. global $wpdb;
  193. $term_id = $this->factory->term->create(
  194. array(
  195. 'slug' => 'burrito',
  196. 'name' => 'Taco',
  197. 'taxonomy' => 'post_tag',
  198. )
  199. );
  200. clean_term_cache( $term_id, 'post_tag' );
  201. $num_queries = $wpdb->num_queries;
  202. $term = get_term_by( 'slug', 'burrito', 'post_tag' );
  203. $num_queries++;
  204. $this->assertSame( 'Taco', $term->name );
  205. $this->assertSame( $num_queries, $wpdb->num_queries );
  206. // This should now hit cache.
  207. $term = get_term_by( 'slug', 'burrito', 'post_tag' );
  208. $this->assertSame( 'Taco', $term->name );
  209. $this->assertSame( $num_queries, $wpdb->num_queries );
  210. $this->assertEquals( get_term( $term_id, 'post_tag' ), $term );
  211. $this->assertSame( $num_queries, $wpdb->num_queries );
  212. }
  213. /**
  214. * @ticket 21760
  215. */
  216. function test_get_term_by_slug_cache_update() {
  217. global $wpdb;
  218. $term_id = $this->factory->term->create(
  219. array(
  220. 'slug' => 'burrito',
  221. 'name' => 'Taco',
  222. 'taxonomy' => 'post_tag',
  223. )
  224. );
  225. clean_term_cache( $term_id, 'post_tag' );
  226. $num_queries = $wpdb->num_queries;
  227. $term = get_term_by( 'slug', 'burrito', 'post_tag' );
  228. $num_queries++;
  229. $this->assertSame( 'Taco', $term->name );
  230. $this->assertSame( $num_queries, $wpdb->num_queries );
  231. // This should now hit cache.
  232. $term = get_term_by( 'slug', 'burrito', 'post_tag' );
  233. $this->assertSame( 'Taco', $term->name );
  234. $this->assertSame( $num_queries, $wpdb->num_queries );
  235. // Update the tag which invalidates the cache.
  236. wp_update_term( $term_id, 'post_tag', array( 'name' => 'No Taco' ) );
  237. $num_queries = $wpdb->num_queries;
  238. // This should not hit cache.
  239. $term = get_term_by( 'slug', 'burrito', 'post_tag' );
  240. $num_queries++;
  241. $this->assertSame( 'No Taco', $term->name );
  242. $this->assertSame( $num_queries, $wpdb->num_queries );
  243. }
  244. /**
  245. * @ticket 21760
  246. */
  247. function test_get_term_by_name_cache() {
  248. global $wpdb;
  249. $term_id = $this->factory->term->create(
  250. array(
  251. 'name' => 'Burrito',
  252. 'slug' => 'noburrito',
  253. 'taxonomy' => 'post_tag',
  254. )
  255. );
  256. clean_term_cache( $term_id, 'post_tag' );
  257. $num_queries = $wpdb->num_queries;
  258. get_term_by( 'name', 'Burrito', 'post_tag' );
  259. $num_queries++;
  260. $this->assertSame( $num_queries, $wpdb->num_queries );
  261. // This should now hit cache.
  262. $term = get_term_by( 'name', 'Burrito', 'post_tag' );
  263. $this->assertSame( $num_queries, $wpdb->num_queries );
  264. $this->assertEquals( get_term( $term_id, 'post_tag' ), $term );
  265. $this->assertSame( $num_queries, $wpdb->num_queries );
  266. }
  267. /**
  268. * @ticket 21760
  269. */
  270. function test_get_term_by_name_cache_update() {
  271. global $wpdb;
  272. $term_id = $this->factory->term->create(
  273. array(
  274. 'name' => 'Burrito',
  275. 'slug' => 'noburrito',
  276. 'taxonomy' => 'post_tag',
  277. )
  278. );
  279. clean_term_cache( $term_id, 'post_tag' );
  280. $num_queries = $wpdb->num_queries;
  281. get_term_by( 'name', 'Burrito', 'post_tag' );
  282. $num_queries++;
  283. $this->assertSame( $num_queries, $wpdb->num_queries );
  284. // This should now hit cache.
  285. get_term_by( 'name', 'Burrito', 'post_tag' );
  286. $this->assertSame( $num_queries, $wpdb->num_queries );
  287. // Update the tag which invalidates the cache.
  288. wp_update_term( $term_id, 'post_tag', array( 'slug' => 'taco' ) );
  289. $num_queries = $wpdb->num_queries;
  290. // This should not hit cache.
  291. get_term_by( 'name', 'burrito', 'post_tag' );
  292. $num_queries++;
  293. $this->assertSame( $num_queries, $wpdb->num_queries );
  294. }
  295. /**
  296. * @ticket 21760
  297. */
  298. function test_invalidating_term_caches_should_fail_when_invalidation_is_suspended() {
  299. global $wpdb;
  300. $term_id = $this->factory->term->create(
  301. array(
  302. 'name' => 'Burrito',
  303. 'taxonomy' => 'post_tag',
  304. )
  305. );
  306. clean_term_cache( $term_id, 'post_tag' );
  307. $num_queries = $wpdb->num_queries;
  308. $last_changed = wp_cache_get( 'last_changed', 'terms' );
  309. $term1 = get_term_by( 'name', 'Burrito', 'post_tag' );
  310. $num_queries++;
  311. // Verify the term is cached.
  312. $term2 = get_term_by( 'name', 'Burrito', 'post_tag' );
  313. $this->assertSame( $num_queries, $wpdb->num_queries );
  314. $this->assertEquals( $term1, $term2 );
  315. $suspend = wp_suspend_cache_invalidation();
  316. // Update the tag.
  317. wp_update_term( $term_id, 'post_tag', array( 'name' => 'Taco' ) );
  318. $num_queries = $wpdb->num_queries;
  319. // Verify that the cached term still matches the initial cached term.
  320. $term3 = get_term_by( 'name', 'Burrito', 'post_tag' );
  321. $this->assertSame( $num_queries, $wpdb->num_queries );
  322. $this->assertEquals( $term1, $term3 );
  323. // Verify that last changed has not been updated as part of an invalidation routine.
  324. $this->assertSame( $last_changed, wp_cache_get( 'last_changed', 'terms' ) );
  325. // Clean up.
  326. wp_suspend_cache_invalidation( $suspend );
  327. }
  328. /**
  329. * @ticket 21760
  330. */
  331. public function test_get_term_by_does_not_prime_term_meta_cache() {
  332. global $wpdb;
  333. $term_id = $this->factory->term->create(
  334. array(
  335. 'name' => 'Burrito',
  336. 'taxonomy' => 'post_tag',
  337. )
  338. );
  339. add_term_meta( $term_id, 'foo', 'bar' );
  340. clean_term_cache( $term_id, 'post_tag' );
  341. $num_queries = $wpdb->num_queries;
  342. $term = get_term_by( 'name', 'Burrito', 'post_tag' );
  343. $num_queries++;
  344. $this->assertTrue( $term instanceof WP_Term );
  345. $this->assertSame( $term_id, $term->term_id );
  346. $this->assertSame( $num_queries, $wpdb->num_queries );
  347. $term_meta = get_term_meta( $term_id, 'foo', true );
  348. $num_queries++;
  349. $this->assertSame( $term_meta, 'bar' );
  350. $this->assertSame( $num_queries, $wpdb->num_queries );
  351. }
  352. /**
  353. * @ticket 37291
  354. */
  355. public function test_get_object_term_cache_should_return_error_if_any_term_is_an_error() {
  356. register_taxonomy( 'wptests_tax', 'post' );
  357. $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
  358. $p = self::factory()->post->create();
  359. wp_set_object_terms( $p, $t, 'wptests_tax' );
  360. // Prime cache.
  361. $terms = get_the_terms( $p, 'wptests_tax' );
  362. $this->assertSameSets( array( $t ), wp_list_pluck( $terms, 'term_id' ) );
  363. /*
  364. * Modify cached array to insert an empty term ID,
  365. * which will trigger an error in get_term().
  366. */
  367. $cached_ids = wp_cache_get( $p, 'wptests_tax_relationships' );
  368. $cached_ids[] = 0;
  369. wp_cache_set( $p, $cached_ids, 'wptests_tax_relationships' );
  370. $terms = get_the_terms( $p, 'wptests_tax' );
  371. $this->assertWPError( $terms );
  372. }
  373. }