WordPress.org

Make WordPress Core

Ticket #23167: 23167-ut.diff

File 23167-ut.diff, 2.3 KB (added by ryan, 7 years 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                //debug_log( $wpdb );
     33                $this->assertEquals( 10, count( $pages ) );
     34                $this->assertEquals( 1, wp_cache_get( 'last_changed', 'posts' ) );
     35                $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
     36
     37                // Again. num_queries and last_changed should remain the same.
     38                $pages = get_pages( array( 'number' => 10 ) );
     39                $this->assertEquals( 10, count( $pages ) );
     40                $this->assertEquals( 1, wp_cache_get( 'last_changed', 'posts' ) );
     41                $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
     42
     43                // Force last_changed to increment.
     44                clean_post_cache( $pages[0]->ID );
     45                $this->assertEquals( 2, wp_cache_get( 'last_changed', 'posts' ) );
     46
     47                // Last changed bumped so num_queries should increment.
     48                $pages = get_pages( array( 'number' => 10 ) );
     49                $this->assertEquals( 10, count( $pages ) );
     50                $this->assertEquals( 2, wp_cache_get( 'last_changed', 'posts' ) );
     51                $this->assertEquals( $num_queries + 2, $wpdb->num_queries );
     52        }
     53}
     54 No newline at end of file