123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- /**
- * @group post
- */
- class Tests_Post_getPages extends WP_UnitTestCase {
- function setUp() {
- parent::setUp();
- }
- /**
- * @ticket 23167
- */
- function test_get_pages_cache() {
- global $wpdb;
- $this->factory->post->create_many( 15, array( 'post_type' => 'page' ) );
- wp_cache_delete( 'last_changed', 'posts' );
- $this->assertFalse( wp_cache_get( 'last_changed', 'posts' ) );
- $pages = get_pages();
- $this->assertEquals( 15, count( $pages ) );
- $this->assertNotEmpty( $time1 = wp_cache_get( 'last_changed', 'posts' ) );
- $num_queries = $wpdb->num_queries;
- foreach ( $pages as $page )
- $this->assertInstanceOf( 'WP_Post', $page );
- // Again. num_queries and last_changed should remain the same.
- $pages = get_pages();
- $this->assertEquals( 15, count( $pages ) );
- $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
- $this->assertEquals( $num_queries, $wpdb->num_queries );
- foreach ( $pages as $page )
- $this->assertInstanceOf( 'WP_Post', $page );
- // Again with different args. last_changed should not increment because of
- // different args to get_pages(). num_queries should bump by 1.
- $pages = get_pages( array( 'number' => 10 ) );
- $this->assertEquals( 10, count( $pages ) );
- $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
- $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
- foreach ( $pages as $page )
- $this->assertInstanceOf( 'WP_Post', $page );
- $num_queries = $wpdb->num_queries;
- // Again. num_queries and last_changed should remain the same.
- $pages = get_pages( array( 'number' => 10 ) );
- $this->assertEquals( 10, count( $pages ) );
- $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
- $this->assertEquals( $num_queries, $wpdb->num_queries );
- foreach ( $pages as $page )
- $this->assertInstanceOf( 'WP_Post', $page );
- // Do the first query again. The interim queries should not affect it.
- $pages = get_pages();
- $this->assertEquals( 15, count( $pages ) );
- $this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
- $this->assertEquals( $num_queries, $wpdb->num_queries );
- foreach ( $pages as $page )
- $this->assertInstanceOf( 'WP_Post', $page );
- // Force last_changed to increment.
- clean_post_cache( $pages[0]->ID );
- $this->assertNotEquals( $time1, $time2 = wp_cache_get( 'last_changed', 'posts' ) );
- $num_queries = $wpdb->num_queries;
- // last_changed bumped so num_queries should increment.
- $pages = get_pages( array( 'number' => 10 ) );
- $this->assertEquals( 10, count( $pages ) );
- $this->assertEquals( $time2, wp_cache_get( 'last_changed', 'posts' ) );
- $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
- foreach ( $pages as $page )
- $this->assertInstanceOf( 'WP_Post', $page );
- $last_changed = wp_cache_get( 'last_changed', 'posts' );
- // This should bump last_changed.
- wp_delete_post( $pages[0]->ID );
- $this->assertGreaterThan( $last_changed, wp_cache_get( 'last_changed', 'posts' ) );
- $num_queries = $wpdb->num_queries;
- $last_changed = wp_cache_get( 'last_changed', 'posts' );
- // num_queries should bump after wp_delete_post() bumps last_changed.
- $pages = get_pages();
- $this->assertEquals( 14, count( $pages ) );
- $this->assertEquals( $last_changed, wp_cache_get( 'last_changed', 'posts' ) );
- $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
- foreach ( $pages as $page )
- $this->assertInstanceOf( 'WP_Post', $page );
- }
- /**
- * @ticket 20376
- */
- function test_get_pages_meta() {
- $posts = $this->factory->post->create_many( 3, array( 'post_type' => 'page' ) );
- add_post_meta( $posts[0], 'some-meta-key', '0' );
- add_post_meta( $posts[1], 'some-meta-key', '' );
- add_post_meta( $posts[2], 'some-meta-key', '1' );
- $this->assertEquals( 1, count( get_pages( array( 'meta_key' => 'some-meta-key', 'meta_value' => '0' ) ) ) );
- $this->assertEquals( 1, count( get_pages( array( 'meta_key' => 'some-meta-key', 'meta_value' => '1' ) ) ) );
- $this->assertEquals( 3, count( get_pages( array( 'meta_key' => 'some-meta-key' ) ) ) );
- }
- /**
- * @ticket 22074
- */
- function test_get_pages_include_exclude() {
- $page_ids = array();
- foreach ( range( 1, 20 ) as $i )
- $page_ids[] = $this->factory->post->create( array( 'post_type' => 'page' ) );
- $inc = array_slice( $page_ids, 0, 10 );
- sort( $inc );
- $exc = array_slice( $page_ids, 10 );
- sort( $exc );
- $include = get_pages( array( 'include' => $inc ) );
- $inc_result = wp_list_pluck( $include, 'ID' );
- sort( $inc_result );
- $this->assertEquals( $inc, $inc_result );
- $exclude = get_pages( array( 'exclude' => $exc ) );
- $exc_result = wp_list_pluck( $exclude, 'ID' );
- sort( $exc_result );
- $this->assertEquals( $inc, $exc_result );
- }
- /**
- * @ticket 9470
- */
- function test_get_pages_parent() {
- $page_id1 = $this->factory->post->create( array( 'post_type' => 'page' ) );
- $page_id2 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id1 ) );
- $page_id3 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id2 ) );
- $page_id4 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id1 ) );
- $pages = get_pages( array( 'parent' => 0, 'hierarchical' => false ) );
- $this->assertEqualSets( array( $page_id1 ), wp_list_pluck( $pages, 'ID' ) );
- $pages = get_pages( array( 'parent' => $page_id1, 'hierarchical' => false ) );
- $this->assertEqualSets( array( $page_id2, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
- $pages = get_pages( array( 'parent' => array( $page_id1, $page_id2 ), 'hierarchical' => false ) );
- $this->assertEqualSets( array( $page_id2, $page_id3, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
- $pages = get_pages( array( 'parent' => 0 ) );
- $this->assertEqualSets( array( $page_id1 ), wp_list_pluck( $pages, 'ID' ) );
- $pages = get_pages( array( 'parent' => $page_id1 ) );
- $this->assertEqualSets( array( $page_id2, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
- $pages = get_pages( array( 'parent' => array( $page_id1, $page_id2 ) ) );
- $this->assertEqualSets( array( $page_id2, $page_id3, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
- }
- /**
- * @ticket 22389
- */
- function test_wp_dropdown_pages() {
- $posts = $this->factory->post->create_many( 5, array( 'post_type' => 'page' ) );
- preg_match_all( '#<option#', wp_dropdown_pages( 'echo=0' ), $matches );
- $this->assertEquals( 5, count( $matches[0] ) );
- }
- /**
- * @ticket 22208
- */
- function test_get_chidren_fields_ids() {
- $post_id = $this->factory->post->create();
- $child_ids = $this->factory->post->create_many( 5, array( 'post_parent' => $post_id ) );
- $post_ids = get_children( array( 'fields' => 'ids', 'post_parent' => $post_id ) );
- $this->assertEqualSets( $child_ids, $post_ids );
- }
- /**
- * @ticket 25750
- */
- function test_get_pages_hierarchical_and_no_parent() {
- global $wpdb;
- $page_1 = $this->factory->post->create( array( 'post_type' => 'page' ) );
- $page_2 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
- $page_3 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
- $page_4 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_2 ) );
- $pages = get_pages(); // Defaults: hierarchical = true, parent = -1
- $pages_default_args = get_pages( array( 'hierarchical' => true, 'parent' => -1 ) );
- // Confirm the defaults.
- $this->assertEquals( $pages, $pages_default_args );
- /*
- * Here's the tree we are testing:
- *
- * page 1
- * - page 2
- * -- page 4
- * - page 3
- *
- * If hierarchical => true works, the order will be 1,2,4,3.
- * If it doesn't, they will be in the creation order, 1,2,3,4.
- */
- $this->assertEqualSets( array( $page_1, $page_2, $page_4, $page_3 ), wp_list_pluck( $pages, 'ID' ) );
- }
- }
|