WordPress.org

Make WordPress Core

Ticket #23167: 23167-ut.2.diff

File 23167-ut.2.diff, 3.2 KB (added by ryan, 15 months ago)
  • tests/post/getPages.php

     
     1<?php 
     2 
     3/** 
     4 * @group post 
     5 */ 
     6 
     7class Tests_Post_getPages extends WP_UnitTestCase { 
     8        function setUp() { 
     9                parent::setUp(); 
     10        } 
     11 
     12        function test_get_pages_cache() { 
     13                global $wpdb; 
     14 
     15                $this->factory->post->create_many( 15, array( 'post_type' => 'page' ) ); 
     16                $this->assertFalse( wp_cache_get( 'last_changed', 'posts' ) ); 
     17 
     18                $pages = get_pages(); 
     19                $this->assertEquals( 15, count( $pages ) ); 
     20                $this->assertEquals( 1, wp_cache_get( 'last_changed', 'posts' ) ); 
     21                $num_queries = $wpdb->num_queries; 
     22 
     23                // Again. num_queries and last_changed should remain the same. 
     24                $pages = get_pages(); 
     25                $this->assertEquals( 15, count( $pages ) ); 
     26                $this->assertEquals( 1, wp_cache_get( 'last_changed', 'posts' ) ); 
     27                $this->assertEquals( $num_queries, $wpdb->num_queries ); 
     28 
     29                // Again with different args. last_changed should not increment because of 
     30                // different args to get_pages(). num_queries should bump by 1. 
     31                $pages = get_pages( array( 'number' => 10 ) ); 
     32                $this->assertEquals( 10, count( $pages ) ); 
     33                $this->assertEquals( 1, wp_cache_get( 'last_changed', 'posts' ) ); 
     34                $this->assertEquals( $num_queries + 1, $wpdb->num_queries ); 
     35 
     36                $num_queries = $wpdb->num_queries; 
     37 
     38                // Again. num_queries and last_changed should remain the same. 
     39                $pages = get_pages( array( 'number' => 10 ) ); 
     40                $this->assertEquals( 10, count( $pages ) ); 
     41                $this->assertEquals( 1, wp_cache_get( 'last_changed', 'posts' ) ); 
     42                $this->assertEquals( $num_queries, $wpdb->num_queries ); 
     43 
     44                // Do the first query again. The interim queries should not affect it. 
     45                $pages = get_pages(); 
     46                $this->assertEquals( 15, count( $pages ) ); 
     47                $this->assertEquals( 1, wp_cache_get( 'last_changed', 'posts' ) ); 
     48                $this->assertEquals( $num_queries, $wpdb->num_queries ); 
     49 
     50                // Force last_changed to increment. 
     51                clean_post_cache( $pages[0]->ID ); 
     52                $this->assertEquals( 2, wp_cache_get( 'last_changed', 'posts' ) ); 
     53 
     54                $num_queries = $wpdb->num_queries; 
     55 
     56                // last_changed bumped so num_queries should increment. 
     57                $pages = get_pages( array( 'number' => 10 ) ); 
     58                $this->assertEquals( 10, count( $pages ) ); 
     59                $this->assertEquals( 2, wp_cache_get( 'last_changed', 'posts' ) ); 
     60                $this->assertEquals( $num_queries + 1, $wpdb->num_queries ); 
     61 
     62                $last_changed = wp_cache_get( 'last_changed', 'posts' ); 
     63 
     64                // This should bump last_changed. 
     65                wp_delete_post( $pages[0]->ID ); 
     66                $this->assertGreaterThan( $last_changed, wp_cache_get( 'last_changed', 'posts' ) ); 
     67 
     68                $num_queries = $wpdb->num_queries; 
     69                $last_changed = wp_cache_get( 'last_changed', 'posts' ); 
     70 
     71                // num_queries should bump after wp_delete_post() bumps last_changed. 
     72                $pages = get_pages(); 
     73                $this->assertEquals( 14, count( $pages ) ); 
     74                $this->assertEquals( $last_changed, wp_cache_get( 'last_changed', 'posts' ) ); 
     75                $this->assertEquals( $num_queries + 1, $wpdb->num_queries ); 
     76        } 
     77} 
     78 No newline at end of file