WordPress.org

Make WordPress Core

Ticket #23167: 23167-ut.3.diff

File 23167-ut.3.diff, 3.7 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                foreach ( $pages as $page )
     23                        $this->assertInstanceOf( 'WP_Post', $page );
     24
     25                // Again. num_queries and last_changed should remain the same.
     26                $pages = get_pages();
     27                $this->assertEquals( 15, count( $pages ) );
     28                $this->assertEquals( 1, wp_cache_get( 'last_changed', 'posts' ) );
     29                $this->assertEquals( $num_queries, $wpdb->num_queries );
     30                foreach ( $pages as $page )
     31                        $this->assertInstanceOf( 'WP_Post', $page );
     32
     33                // Again with different args. last_changed should not increment because of
     34                // different args to get_pages(). num_queries should bump by 1.
     35                $pages = get_pages( array( 'number' => 10 ) );
     36                $this->assertEquals( 10, count( $pages ) );
     37                $this->assertEquals( 1, wp_cache_get( 'last_changed', 'posts' ) );
     38                $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
     39                foreach ( $pages as $page )
     40                        $this->assertInstanceOf( 'WP_Post', $page );
     41
     42                $num_queries = $wpdb->num_queries;
     43
     44                // Again. num_queries and last_changed should remain the same.
     45                $pages = get_pages( array( 'number' => 10 ) );
     46                $this->assertEquals( 10, count( $pages ) );
     47                $this->assertEquals( 1, wp_cache_get( 'last_changed', 'posts' ) );
     48                $this->assertEquals( $num_queries, $wpdb->num_queries );
     49                foreach ( $pages as $page )
     50                        $this->assertInstanceOf( 'WP_Post', $page );
     51
     52                // Do the first query again. The interim queries should not affect it.
     53                $pages = get_pages();
     54                $this->assertEquals( 15, count( $pages ) );
     55                $this->assertEquals( 1, wp_cache_get( 'last_changed', 'posts' ) );
     56                $this->assertEquals( $num_queries, $wpdb->num_queries );
     57                foreach ( $pages as $page )
     58                        $this->assertInstanceOf( 'WP_Post', $page );
     59
     60                // Force last_changed to increment.
     61                clean_post_cache( $pages[0]->ID );
     62                $this->assertEquals( 2, wp_cache_get( 'last_changed', 'posts' ) );
     63
     64                $num_queries = $wpdb->num_queries;
     65
     66                // last_changed bumped so num_queries should increment.
     67                $pages = get_pages( array( 'number' => 10 ) );
     68                $this->assertEquals( 10, count( $pages ) );
     69                $this->assertEquals( 2, wp_cache_get( 'last_changed', 'posts' ) );
     70                $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
     71                foreach ( $pages as $page )
     72                        $this->assertInstanceOf( 'WP_Post', $page );
     73
     74                $last_changed = wp_cache_get( 'last_changed', 'posts' );
     75
     76                // This should bump last_changed.
     77                wp_delete_post( $pages[0]->ID );
     78                $this->assertGreaterThan( $last_changed, wp_cache_get( 'last_changed', 'posts' ) );
     79
     80                $num_queries = $wpdb->num_queries;
     81                $last_changed = wp_cache_get( 'last_changed', 'posts' );
     82
     83                // num_queries should bump after wp_delete_post() bumps last_changed.
     84                $pages = get_pages();
     85                $this->assertEquals( 14, count( $pages ) );
     86                $this->assertEquals( $last_changed, wp_cache_get( 'last_changed', 'posts' ) );
     87                $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
     88                foreach ( $pages as $page )
     89                        $this->assertInstanceOf( 'WP_Post', $page );
     90        }
     91}
     92 No newline at end of file