123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722 |
- <?php
- /**
- * @group post
- */
- class Tests_Post_GetPages extends WP_UnitTestCase {
- /**
- * @ticket 23167
- */
- function test_get_pages_cache() {
- global $wpdb;
- self::factory()->post->create_many( 3, array( 'post_type' => 'page' ) );
- wp_cache_delete( 'last_changed', 'posts' );
- $this->assertFalse( wp_cache_get( 'last_changed', 'posts' ) );
- $pages = get_pages();
- $this->assertSame( 3, count( $pages ) );
- $time1 = wp_cache_get( 'last_changed', 'posts' );
- $this->assertNotEmpty( $time1 );
- $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->assertSame( 3, count( $pages ) );
- $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) );
- $this->assertSame( $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' => 2 ) );
- $this->assertSame( 2, count( $pages ) );
- $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) );
- $this->assertSame( $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' => 2 ) );
- $this->assertSame( 2, count( $pages ) );
- $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) );
- $this->assertSame( $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->assertSame( 3, count( $pages ) );
- $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) );
- $this->assertSame( $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' => 2 ) );
- $this->assertSame( 2, count( $pages ) );
- $this->assertSame( $time2, wp_cache_get( 'last_changed', 'posts' ) );
- $this->assertSame( $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 );
- $old_changed_float = $this->_microtime_to_float( $last_changed );
- $new_changed_float = $this->_microtime_to_float( wp_cache_get( 'last_changed', 'posts' ) );
- $this->assertGreaterThan( $old_changed_float, $new_changed_float );
- $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->assertSame( 2, count( $pages ) );
- $this->assertSame( $last_changed, wp_cache_get( 'last_changed', 'posts' ) );
- $this->assertSame( $num_queries + 1, $wpdb->num_queries );
- foreach ( $pages as $page ) {
- $this->assertInstanceOf( 'WP_Post', $page );
- }
- }
- /**
- * @ticket 40669
- */
- public function test_cache_should_be_invalidated_by_add_post_meta() {
- $posts = self::factory()->post->create_many(
- 2,
- array(
- 'post_type' => 'page',
- )
- );
- add_post_meta( $posts[0], 'foo', 'bar' );
- $cached = get_pages(
- array(
- 'meta_key' => 'foo',
- 'meta_value' => 'bar',
- )
- );
- $cached_ids = wp_list_pluck( $cached, 'ID' );
- $this->assertSameSets( array( $posts[0] ), $cached_ids );
- add_post_meta( $posts[1], 'foo', 'bar' );
- $found = get_pages(
- array(
- 'meta_key' => 'foo',
- 'meta_value' => 'bar',
- )
- );
- $found_ids = wp_list_pluck( $found, 'ID' );
- $this->assertSameSets( $posts, $found_ids );
- }
- /**
- * @ticket 40669
- */
- public function test_cache_should_be_invalidated_by_update_post_meta() {
- $posts = self::factory()->post->create_many(
- 2,
- array(
- 'post_type' => 'page',
- )
- );
- add_post_meta( $posts[0], 'foo', 'bar' );
- add_post_meta( $posts[1], 'foo', 'bar' );
- $cached = get_pages(
- array(
- 'meta_key' => 'foo',
- 'meta_value' => 'bar',
- )
- );
- $cached_ids = wp_list_pluck( $cached, 'ID' );
- $this->assertSameSets( $posts, $cached_ids );
- update_post_meta( $posts[1], 'foo', 'baz' );
- $found = get_pages(
- array(
- 'meta_key' => 'foo',
- 'meta_value' => 'bar',
- )
- );
- $found_ids = wp_list_pluck( $found, 'ID' );
- $this->assertSameSets( array( $posts[0] ), $found_ids );
- }
- /**
- * @ticket 40669
- */
- public function test_cache_should_be_invalidated_by_delete_post_meta() {
- $posts = self::factory()->post->create_many(
- 2,
- array(
- 'post_type' => 'page',
- )
- );
- add_post_meta( $posts[0], 'foo', 'bar' );
- add_post_meta( $posts[1], 'foo', 'bar' );
- $cached = get_pages(
- array(
- 'meta_key' => 'foo',
- 'meta_value' => 'bar',
- )
- );
- $cached_ids = wp_list_pluck( $cached, 'ID' );
- $this->assertSameSets( $posts, $cached_ids );
- delete_post_meta( $posts[1], 'foo' );
- $found = get_pages(
- array(
- 'meta_key' => 'foo',
- 'meta_value' => 'bar',
- )
- );
- $found_ids = wp_list_pluck( $found, 'ID' );
- $this->assertSameSets( array( $posts[0] ), $found_ids );
- }
- /**
- * @ticket 40669
- */
- public function test_cache_should_be_invalidated_by_delete_post_meta_by_key() {
- $posts = self::factory()->post->create_many(
- 2,
- array(
- 'post_type' => 'page',
- )
- );
- add_post_meta( $posts[0], 'foo', 'bar' );
- add_post_meta( $posts[1], 'foo', 'bar' );
- $cached = get_pages(
- array(
- 'meta_key' => 'foo',
- 'meta_value' => 'bar',
- )
- );
- $cached_ids = wp_list_pluck( $cached, 'ID' );
- $this->assertSameSets( $posts, $cached_ids );
- delete_post_meta_by_key( 'foo' );
- $found = get_pages(
- array(
- 'meta_key' => 'foo',
- 'meta_value' => 'bar',
- )
- );
- $found_ids = wp_list_pluck( $found, 'ID' );
- $this->assertSameSets( array(), $found_ids );
- }
- /**
- * @ticket 20376
- */
- function test_get_pages_meta() {
- $posts = self::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->assertSame(
- 1,
- count(
- get_pages(
- array(
- 'meta_key' => 'some-meta-key',
- 'meta_value' => '0',
- )
- )
- )
- );
- $this->assertSame(
- 1,
- count(
- get_pages(
- array(
- 'meta_key' => 'some-meta-key',
- 'meta_value' => '1',
- )
- )
- )
- );
- $this->assertSame( 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[] = self::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->assertSame( $inc, $inc_result );
- $exclude = get_pages( array( 'exclude' => $exc ) );
- $exc_result = wp_list_pluck( $exclude, 'ID' );
- sort( $exc_result );
- $this->assertSame( $inc, $exc_result );
- }
- /**
- * @ticket 9470
- */
- function test_get_pages_parent() {
- $page_id1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
- $page_id2 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'post_parent' => $page_id1,
- )
- );
- $page_id3 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'post_parent' => $page_id2,
- )
- );
- $page_id4 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'post_parent' => $page_id1,
- )
- );
- $pages = get_pages(
- array(
- 'parent' => 0,
- 'hierarchical' => false,
- )
- );
- $this->assertSameSets( array( $page_id1 ), wp_list_pluck( $pages, 'ID' ) );
- $pages = get_pages(
- array(
- 'parent' => $page_id1,
- 'hierarchical' => false,
- )
- );
- $this->assertSameSets( array( $page_id2, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
- $pages = get_pages(
- array(
- 'parent' => array( $page_id1, $page_id2 ),
- 'hierarchical' => false,
- )
- );
- $this->assertSameSets( array( $page_id2, $page_id3, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
- $pages = get_pages( array( 'parent' => 0 ) );
- $this->assertSameSets( array( $page_id1 ), wp_list_pluck( $pages, 'ID' ) );
- $pages = get_pages( array( 'parent' => $page_id1 ) );
- $this->assertSameSets( array( $page_id2, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
- $pages = get_pages( array( 'parent' => array( $page_id1, $page_id2 ) ) );
- $this->assertSameSets( array( $page_id2, $page_id3, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
- }
- /**
- * @ticket 22389
- */
- function test_wp_dropdown_pages() {
- self::factory()->post->create_many( 5, array( 'post_type' => 'page' ) );
- preg_match_all( '#<option#', wp_dropdown_pages( 'echo=0' ), $matches );
- $this->assertSame( 5, count( $matches[0] ) );
- }
- /**
- * @ticket 22208
- */
- function test_get_chidren_fields_ids() {
- $post_id = self::factory()->post->create();
- $child_ids = self::factory()->post->create_many( 5, array( 'post_parent' => $post_id ) );
- $post_ids = get_children(
- array(
- 'fields' => 'ids',
- 'post_parent' => $post_id,
- )
- );
- $this->assertSameSets( $child_ids, $post_ids );
- }
- /**
- * @ticket 25750
- */
- function test_get_pages_hierarchical_and_no_parent() {
- global $wpdb;
- $page_1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
- $page_2 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'post_parent' => $page_1,
- )
- );
- $page_3 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'post_parent' => $page_1,
- )
- );
- $page_4 = self::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->assertSameSets( array( $page_1, $page_2, $page_4, $page_3 ), wp_list_pluck( $pages, 'ID' ) );
- }
- /**
- * @ticket 18701
- */
- public function test_get_pages_hierarchical_empty_child_of() {
- $page_1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
- $page_2 = self::factory()->post->create( array( 'post_type' => 'page' ) );
- $page_3 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'post_parent' => $page_1,
- )
- );
- $page_4 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'post_parent' => $page_1,
- )
- );
- $pages = get_pages(); // Defaults: hierarchical = true, child_of = '', parent = -1.
- $default_args = get_pages(
- array(
- 'hierarchical' => true,
- 'child_of' => '',
- )
- );
- $this->assertEquals( $pages, $default_args );
- /*
- * Page tree:
- *
- * page 1 (parent 0)
- * – page 3 (parent 1)
- * – page 4 (parent 1)
- * page 2 (parent 0)
- *
- * With default arguments, if child_of is empty (normalized to 0), only pages with a matching
- * post_parent will be returned, in the order they were created: 1, 2.
- */
- $found_pages = wp_list_filter( $pages, array( 'post_parent' => 0 ) );
- $this->assertSameSets( array( $page_1, $page_2 ), wp_list_pluck( $found_pages, 'ID' ) );
- }
- /**
- * @ticket 18701
- */
- public function test_get_pages_non_hierarchical_empty_child_of() {
- $page_1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
- $page_2 = self::factory()->post->create( array( 'post_type' => 'page' ) );
- $page_3 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'post_parent' => $page_1,
- )
- );
- $page_4 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'post_parent' => $page_1,
- )
- );
- $pages = get_pages( array( 'hierarchical' => false ) ); // child_of = '', parent = -1.
- /*
- * Page tree:
- *
- * page 1 (parent 0)
- * – page 3 (parent 1)
- * – page 4 (parent 1)
- * page 2 (parent 0)
- *
- * If hierarchical is false and child_of is empty (normalized to 0), pages will be returned
- * in order of creation: 1, 2, 3, 4, regardless of parent.
- */
- $this->assertSameSets( array( $page_1, $page_2, $page_3, $page_4 ), wp_list_pluck( $pages, 'ID' ) );
- }
- /**
- * @ticket 18701
- */
- public function test_get_pages_hierarchical_non_empty_child_of() {
- $page_1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
- $page_2 = self::factory()->post->create( array( 'post_type' => 'page' ) );
- $page_3 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'post_parent' => $page_1,
- )
- );
- $page_4 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'post_parent' => $page_3,
- )
- );
- $page_5 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'post_parent' => $page_1,
- )
- );
- $pages = get_pages( array( 'child_of' => $page_1 ) ); // Defaults: hierarchical = true, parent = -1.
- /*
- * Page tree:
- *
- * page 1 (parent 0)
- * – page 3 (parent 1)
- * –– page 4 (parent 3)
- * – page 5 (parent 1)
- * page 2 (parent 0)
- *
- * If hierarchical is true (default), and child_of is not empty, pages will be returned
- * hierarchically in order of creation: 3, 4, 5.
- */
- $this->assertSameSets( array( $page_3, $page_4, $page_5 ), wp_list_pluck( $pages, 'ID' ) );
- }
- /**
- * @ticket 18701
- */
- public function test_get_pages_non_hierarchical_non_empty_child_of() {
- $page_1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
- $page_2 = self::factory()->post->create( array( 'post_type' => 'page' ) );
- $page_3 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'post_parent' => $page_1,
- )
- );
- $page_4 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'post_parent' => $page_3,
- )
- );
- $page_5 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'post_parent' => $page_1,
- )
- );
- $pages = get_pages(
- array(
- 'hierarchical' => false,
- 'child_of' => $page_1,
- )
- );
- /*
- * Page tree:
- *
- * page 1 (parent 0)
- * – page 3 (parent 1)
- * –– page 4 (parent 3)
- * – page 5 (parent 1)
- * page 2 (parent 0)
- *
- * If hierarchical is false, and child_of is not empty, pages will (apparently) be returned
- * hierarchically anyway in order of creation: 3, 4, 5.
- */
- $this->assertSameSets( array( $page_3, $page_4, $page_5 ), wp_list_pluck( $pages, 'ID' ) );
- // How it should work.
- $found_pages = wp_list_filter( $pages, array( 'post_parent' => $page_1 ) );
- $this->assertSameSets( array( $page_3, $page_5 ), wp_list_pluck( $found_pages, 'ID' ) );
- }
- function test_wp_list_pages_classes() {
- $type = 'taco';
- register_post_type(
- $type,
- array(
- 'hierarchical' => true,
- 'public' => true,
- )
- );
- $posts = self::factory()->post->create_many( 2, array( 'post_type' => $type ) );
- $post_id = reset( $posts );
- $this->go_to( "/?p=$post_id&post_type=$type" );
- $this->assertSame( $post_id, get_queried_object_id() );
- $output = wp_list_pages(
- array(
- 'echo' => false,
- 'title_li' => '',
- 'post_type' => $type,
- )
- );
- $this->assertNotEmpty( $output );
- $this->assertSame( 2, substr_count( $output, 'class="page_item ' ) );
- $this->assertContains( 'current_page_item', $output );
- $this->assertSame( 1, substr_count( $output, 'current_page_item' ) );
- _unregister_post_type( $type );
- }
- function test_exclude_tree() {
- $post_id1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
- $post_id2 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'post_parent' => $post_id1,
- )
- );
- $post_id3 = self::factory()->post->create( array( 'post_type' => 'page' ) );
- $post_id4 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'post_parent' => $post_id3,
- )
- );
- $all = get_pages();
- $this->assertCount( 4, $all );
- $exclude1 = get_pages( "exclude_tree=$post_id1" );
- $this->assertCount( 2, $exclude1 );
- $exclude2 = get_pages( array( 'exclude_tree' => $post_id1 ) );
- $this->assertCount( 2, $exclude2 );
- $exclude3 = get_pages( array( 'exclude_tree' => array( $post_id1 ) ) );
- $this->assertCount( 2, $exclude3 );
- $exclude4 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id2 ) ) );
- $this->assertCount( 2, $exclude4 );
- $exclude5 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id3 ) ) );
- $this->assertCount( 0, $exclude5 );
- $post_id5 = self::factory()->post->create( array( 'post_type' => 'page' ) );
- $post_id6 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'post_parent' => $post_id5,
- )
- );
- $exclude6 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id3 ) ) );
- $this->assertCount( 2, $exclude6 );
- }
- /**
- * @ticket 43514
- */
- function test_get_pages_cache_empty() {
- global $wpdb;
- wp_cache_delete( 'last_changed', 'posts' );
- $this->assertFalse( wp_cache_get( 'last_changed', 'posts' ) );
- $num_queries = $wpdb->num_queries;
- $pages = get_pages(); // Database gets queried.
- $this->assertSame( $num_queries + 1, $wpdb->num_queries );
- $num_queries = $wpdb->num_queries;
- $pages = get_pages(); // Database should not get queried.
- $this->assertSame( $num_queries, $wpdb->num_queries );
- }
- }
|