| 222 | |
| 223 | /** |
| 224 | * @ticket 21760 |
| 225 | */ |
| 226 | function test_get_term_by_slug_cache() { |
| 227 | global $wpdb; |
| 228 | |
| 229 | $term_id = $this->factory->term->create( array( 'slug' => 'burrito', 'taxonomy' => 'post_tag' ) ); |
| 230 | |
| 231 | clean_term_cache( $term_id, 'post_tag' ); |
| 232 | $num_queries = $wpdb->num_queries; |
| 233 | |
| 234 | get_term_by( 'slug', 'burrito', 'post_tag' ); |
| 235 | $num_queries++; |
| 236 | $this->assertEquals( $num_queries, $wpdb->num_queries ); |
| 237 | |
| 238 | // This should now hit cache. |
| 239 | $term = get_term_by( 'slug', 'burrito', 'post_tag' ); |
| 240 | $this->assertEquals( $num_queries, $wpdb->num_queries ); |
| 241 | |
| 242 | $this->assertEquals( get_term( $term_id, 'post_tag' ), $term ); |
| 243 | $this->assertEquals( $num_queries, $wpdb->num_queries ); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * @ticket 21760 |
| 248 | */ |
| 249 | function test_get_term_by_slug_cache_update() { |
| 250 | global $wpdb; |
| 251 | |
| 252 | $term_id = $this->factory->term->create( array( 'slug' => 'burrito', 'taxonomy' => 'post_tag' ) ); |
| 253 | |
| 254 | clean_term_cache( $term_id, 'post_tag' ); |
| 255 | $num_queries = $wpdb->num_queries; |
| 256 | |
| 257 | get_term_by( 'slug', 'burrito', 'post_tag' ); |
| 258 | $num_queries++; |
| 259 | $this->assertEquals( $num_queries, $wpdb->num_queries ); |
| 260 | |
| 261 | // This should now hit cache. |
| 262 | get_term_by( 'slug', 'burrito', 'post_tag' ); |
| 263 | $this->assertEquals( $num_queries, $wpdb->num_queries ); |
| 264 | |
| 265 | // Update the tag which invalidates the cache. |
| 266 | wp_update_term( $term_id, 'post_tag', array( 'name' => 'Taco' ) ); |
| 267 | $num_queries = $wpdb->num_queries; |
| 268 | |
| 269 | // This should not hit cache. |
| 270 | get_term_by( 'slug', 'burrito', 'post_tag' ); |
| 271 | $num_queries++; |
| 272 | $this->assertEquals( $num_queries, $wpdb->num_queries ); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * @ticket 21760 |
| 277 | */ |
| 278 | function test_get_term_by_name_cache() { |
| 279 | global $wpdb; |
| 280 | |
| 281 | $term_id = $this->factory->term->create( array( 'name' => 'burrito', 'slug' => 'noburrito', 'taxonomy' => 'post_tag' ) ); |
| 282 | |
| 283 | clean_term_cache( $term_id, 'post_tag' ); |
| 284 | $num_queries = $wpdb->num_queries; |
| 285 | |
| 286 | get_term_by( 'name', 'burrito', 'post_tag' ); |
| 287 | $num_queries++; |
| 288 | $this->assertEquals( $num_queries, $wpdb->num_queries ); |
| 289 | |
| 290 | // This should now hit cache. |
| 291 | $term = get_term_by( 'name', 'burrito', 'post_tag' ); |
| 292 | $this->assertEquals( $num_queries, $wpdb->num_queries ); |
| 293 | |
| 294 | $this->assertEquals( get_term( $term_id, 'post_tag' ), $term ); |
| 295 | $this->assertEquals( $num_queries, $wpdb->num_queries ); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * @ticket 21760 |
| 300 | */ |
| 301 | function test_get_term_by_name_cache_update() { |
| 302 | global $wpdb; |
| 303 | |
| 304 | $term_id = $this->factory->term->create( array( 'name' => 'burrito', 'slug' => 'noburrito', 'taxonomy' => 'post_tag' ) ); |
| 305 | |
| 306 | clean_term_cache( $term_id, 'post_tag' ); |
| 307 | $num_queries = $wpdb->num_queries; |
| 308 | |
| 309 | get_term_by( 'name', 'burrito', 'post_tag' ); |
| 310 | $num_queries++; |
| 311 | $this->assertEquals( $num_queries, $wpdb->num_queries ); |
| 312 | |
| 313 | // This should now hit cache. |
| 314 | get_term_by( 'name', 'burrito', 'post_tag' ); |
| 315 | $this->assertEquals( $num_queries, $wpdb->num_queries ); |
| 316 | |
| 317 | // Update the tag which invalidates the cache. |
| 318 | wp_update_term( $term_id, 'post_tag', array( 'slug' => 'taco' ) ); |
| 319 | $num_queries = $wpdb->num_queries; |
| 320 | |
| 321 | // This should not hit cache. |
| 322 | get_term_by( 'name', 'burrito', 'post_tag' ); |
| 323 | $num_queries++; |
| 324 | $this->assertEquals( $num_queries, $wpdb->num_queries ); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * @ticket 21760 |
| 329 | */ |
| 330 | function test_invalidating_term_caches_should_fail_when_invalidation_is_suspended() { |
| 331 | global $wpdb; |
| 332 | |
| 333 | $term_id = $this->factory->term->create( array( 'name' => 'burrito', 'taxonomy' => 'post_tag' ) ); |
| 334 | |
| 335 | clean_term_cache( $term_id, 'post_tag' ); |
| 336 | $num_queries = $wpdb->num_queries; |
| 337 | $last_changed = wp_cache_get( 'last_changed', 'terms' ); |
| 338 | |
| 339 | $term1 = get_term_by( 'name', 'burrito', 'post_tag' ); |
| 340 | $num_queries++; |
| 341 | |
| 342 | // Verify the term is cached. |
| 343 | $term2 = get_term_by( 'name', 'burrito', 'post_tag' ); |
| 344 | $this->assertEquals( $num_queries, $wpdb->num_queries ); |
| 345 | $this->assertEquals( $term1, $term2 ); |
| 346 | |
| 347 | $suspend = wp_suspend_cache_invalidation(); |
| 348 | clean_term_cache( $term_id, 'post_tag' ); |
| 349 | |
| 350 | // Verify that the cached term still matches the initial term. |
| 351 | $term3 = get_term_by( 'name', 'burrito', 'post_tag' ); |
| 352 | $this->assertEquals( $num_queries, $wpdb->num_queries ); |
| 353 | $this->assertEquals( $term1, $term3 ); |
| 354 | |
| 355 | // Verify that last changed has not been updated as part of an invalidation routine. |
| 356 | $this->assertSame( $last_changed, wp_cache_get( 'last_changed', 'terms' ) ); |
| 357 | |
| 358 | // Clean up. |
| 359 | wp_suspend_cache_invalidation( $suspend ); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * @ticket 21760 |
| 364 | */ |
| 365 | public function test_get_term_by_does_not_prime_term_meta_cache() { |
| 366 | global $wpdb; |
| 367 | |
| 368 | $term_id = $this->factory->term->create( array( 'name' => 'burrito', 'taxonomy' => 'post_tag' ) ); |
| 369 | add_term_meta( $term_id, 'foo', 'bar' ); |
| 370 | |
| 371 | clean_term_cache( $term_id, 'post_tag' ); |
| 372 | $num_queries = $wpdb->num_queries; |
| 373 | |
| 374 | $term = get_term_by( 'name', 'burrito', 'post_tag' ); |
| 375 | $num_queries++; |
| 376 | $this->assertTrue( $term instanceof WP_Term ); |
| 377 | $this->assertSame( $term_id, $term->term_id ); |
| 378 | $this->assertEquals( $num_queries, $wpdb->num_queries ); |
| 379 | |
| 380 | $term_meta = get_term_meta( $term_id, 'foo', true ); |
| 381 | $num_queries++; |
| 382 | $this->assertSame( $term_meta, 'bar' ); |
| 383 | $this->assertEquals( $num_queries, $wpdb->num_queries ); |
| 384 | } |