123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761 |
- <?php
- /**
- * @group query
- * @group post
- */
- class Tests_Post_Query extends WP_UnitTestCase {
- /**
- * @group taxonomy
- */
- function test_category__and_var() {
- $q = new WP_Query();
- $term_id = self::factory()->category->create(
- array(
- 'slug' => 'woo',
- 'name' => 'WOO!',
- )
- );
- $term_id2 = self::factory()->category->create(
- array(
- 'slug' => 'hoo',
- 'name' => 'HOO!',
- )
- );
- $post_id = self::factory()->post->create();
- wp_set_post_categories( $post_id, $term_id );
- $posts = $q->query( array( 'category__and' => array( $term_id ) ) );
- $this->assertEmpty( $q->get( 'category__and' ) );
- $this->assertCount( 0, $q->get( 'category__and' ) );
- $this->assertNotEmpty( $q->get( 'category__in' ) );
- $this->assertCount( 1, $q->get( 'category__in' ) );
- $this->assertNotEmpty( $posts );
- $this->assertSame( array( $post_id ), wp_list_pluck( $posts, 'ID' ) );
- $posts2 = $q->query( array( 'category__and' => array( $term_id, $term_id2 ) ) );
- $this->assertNotEmpty( $q->get( 'category__and' ) );
- $this->assertCount( 2, $q->get( 'category__and' ) );
- $this->assertEmpty( $q->get( 'category__in' ) );
- $this->assertCount( 0, $q->get( 'category__in' ) );
- $this->assertEmpty( $posts2 );
- }
- /**
- * @ticket 28099
- * @group taxonomy
- */
- function test_empty_category__in() {
- $cat_id = self::factory()->category->create();
- $post_id = self::factory()->post->create();
- wp_set_post_categories( $post_id, $cat_id );
- $q1 = get_posts( array( 'category__in' => array( $cat_id ) ) );
- $this->assertNotEmpty( $q1 );
- $q2 = get_posts( array( 'category__in' => array() ) );
- $this->assertNotEmpty( $q2 );
- $tag = wp_insert_term( 'woo', 'post_tag' );
- $tag_id = $tag['term_id'];
- $slug = get_tag( $tag_id )->slug;
- wp_set_post_tags( $post_id, $slug );
- $q3 = get_posts( array( 'tag__in' => array( $tag_id ) ) );
- $this->assertNotEmpty( $q3 );
- $q4 = get_posts( array( 'tag__in' => array() ) );
- $this->assertNotEmpty( $q4 );
- $q5 = get_posts( array( 'tag_slug__in' => array( $slug ) ) );
- $this->assertNotEmpty( $q5 );
- $q6 = get_posts( array( 'tag_slug__in' => array() ) );
- $this->assertNotEmpty( $q6 );
- }
- /**
- * @ticket 22448
- */
- function test_the_posts_filter() {
- // Create posts and clear their caches.
- $post_ids = self::factory()->post->create_many( 4 );
- foreach ( $post_ids as $post_id ) {
- clean_post_cache( $post_id );
- }
- add_filter( 'the_posts', array( $this, 'the_posts_filter' ) );
- $query = new WP_Query(
- array(
- 'post_type' => 'post',
- 'posts_per_page' => 3,
- )
- );
- // Fourth post added in filter.
- $this->assertSame( 4, count( $query->posts ) );
- $this->assertSame( 4, $query->post_count );
- foreach ( $query->posts as $post ) {
- // Posts are WP_Post objects.
- $this->assertTrue( is_a( $post, 'WP_Post' ) );
- // Filters are raw.
- $this->assertSame( 'raw', $post->filter );
- // Custom data added in the_posts filter is preserved.
- $this->assertSame( array( $post->ID, 'custom data' ), $post->custom_data );
- }
- remove_filter( 'the_posts', array( $this, 'the_posts_filter' ) );
- }
- /**
- * Use with the_posts filter, appends a post and adds some custom data.
- */
- function the_posts_filter( $posts ) {
- $posts[] = clone $posts[0];
- // Add some custom data to each post.
- foreach ( $posts as $key => $post ) {
- $posts[ $key ]->custom_data = array( $post->ID, 'custom data' );
- }
- return $posts;
- }
- function test_post__in_ordering() {
- $post_id1 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'menu_order' => rand( 1, 100 ),
- )
- );
- $post_id2 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'menu_order' => rand( 1, 100 ),
- )
- );
- $post_id3 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'post_parent' => $post_id2,
- 'menu_order' => rand( 1, 100 ),
- )
- );
- $post_id4 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'post_parent' => $post_id2,
- 'menu_order' => rand( 1, 100 ),
- )
- );
- $post_id5 = self::factory()->post->create(
- array(
- 'post_type' => 'page',
- 'menu_order' => rand( 1, 100 ),
- )
- );
- $ordered = array( $post_id2, $post_id4, $post_id3, $post_id1, $post_id5 );
- $q = new WP_Query(
- array(
- 'post_type' => 'any',
- 'post__in' => $ordered,
- 'orderby' => 'post__in',
- )
- );
- $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'ID' ) );
- }
- /**
- * @ticket 38034
- */
- public function test_orderby_post__in_array() {
- $posts = self::factory()->post->create_many( 4 );
- $ordered = array( $posts[2], $posts[0], $posts[3] );
- $q = new WP_Query(
- array(
- 'post_type' => 'any',
- 'post__in' => $ordered,
- 'orderby' => array( 'post__in' => 'ASC' ),
- )
- );
- $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'ID' ) );
- }
- /**
- * @ticket 38034
- */
- public function test_orderby_post__in_array_with_implied_order() {
- $posts = self::factory()->post->create_many( 4 );
- $ordered = array( $posts[2], $posts[0], $posts[3] );
- $q = new WP_Query(
- array(
- 'post_type' => 'any',
- 'post__in' => $ordered,
- 'orderby' => 'post__in',
- )
- );
- $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'ID' ) );
- }
- function test_post__in_attachment_ordering() {
- $post_id = self::factory()->post->create();
- $att_ids = array();
- $file = DIR_TESTDATA . '/images/canola.jpg';
- $att_ids[1] = self::factory()->attachment->create_object(
- $file,
- $post_id,
- array(
- 'post_mime_type' => 'image/jpeg',
- 'menu_order' => rand( 1, 100 ),
- )
- );
- $att_ids[2] = self::factory()->attachment->create_object(
- $file,
- $post_id,
- array(
- 'post_mime_type' => 'image/jpeg',
- 'menu_order' => rand( 1, 100 ),
- )
- );
- $att_ids[3] = self::factory()->attachment->create_object(
- $file,
- $post_id,
- array(
- 'post_mime_type' => 'image/jpeg',
- 'menu_order' => rand( 1, 100 ),
- )
- );
- $att_ids[4] = self::factory()->attachment->create_object(
- $file,
- $post_id,
- array(
- 'post_mime_type' => 'image/jpeg',
- 'menu_order' => rand( 1, 100 ),
- )
- );
- $att_ids[5] = self::factory()->attachment->create_object(
- $file,
- $post_id,
- array(
- 'post_mime_type' => 'image/jpeg',
- 'menu_order' => rand( 1, 100 ),
- )
- );
- $ordered = array( $att_ids[5], $att_ids[1], $att_ids[4], $att_ids[3], $att_ids[2] );
- $attached = new WP_Query(
- array(
- 'post__in' => $ordered,
- 'post_type' => 'attachment',
- 'post_parent' => $post_id,
- 'post_mime_type' => 'image',
- 'post_status' => 'inherit',
- 'posts_per_page' => '-1',
- 'orderby' => 'post__in',
- )
- );
- $this->assertSame( $ordered, wp_list_pluck( $attached->posts, 'ID' ) );
- }
- /**
- * @ticket 36515
- */
- public function test_post_name__in_ordering() {
- $post_id1 = self::factory()->post->create(
- array(
- 'post_name' => 'id-1',
- 'post_type' => 'page',
- )
- );
- $post_id2 = self::factory()->post->create(
- array(
- 'post_name' => 'id-2',
- 'post_type' => 'page',
- )
- );
- $post_id3 = self::factory()->post->create(
- array(
- 'post_name' => 'id-3',
- 'post_type' => 'page',
- 'post_parent' => $post_id2,
- )
- );
- $ordered = array( 'id-2', 'id-3', 'id-1' );
- $q = new WP_Query(
- array(
- 'post_type' => 'any',
- 'post_name__in' => $ordered,
- 'orderby' => 'post_name__in',
- )
- );
- $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'post_name' ) );
- }
- function test_post_status() {
- $statuses1 = get_post_stati();
- $this->assertContains( 'auto-draft', $statuses1 );
- $statuses2 = get_post_stati( array( 'exclude_from_search' => true ) );
- $this->assertContains( 'auto-draft', $statuses2 );
- $statuses3 = get_post_stati( array( 'exclude_from_search' => false ) );
- $this->assertNotContains( 'auto-draft', $statuses3 );
- $q1 = new WP_Query( array( 'post_status' => 'any' ) );
- $this->assertContains( "post_status <> 'auto-draft'", $q1->request );
- $q2 = new WP_Query( array( 'post_status' => 'any, auto-draft' ) );
- $this->assertNotContains( "post_status <> 'auto-draft'", $q2->request );
- $q3 = new WP_Query( array( 'post_status' => array( 'any', 'auto-draft' ) ) );
- $this->assertNotContains( "post_status <> 'auto-draft'", $q3->request );
- }
- /**
- * @ticket 17065
- */
- function test_orderby_array() {
- global $wpdb;
- $q1 = new WP_Query(
- array(
- 'orderby' => array(
- 'type' => 'DESC',
- 'name' => 'ASC',
- ),
- )
- );
- $this->assertContains(
- "ORDER BY $wpdb->posts.post_type DESC, $wpdb->posts.post_name ASC",
- $q1->request
- );
- $q2 = new WP_Query( array( 'orderby' => array() ) );
- $this->assertNotContains( 'ORDER BY', $q2->request );
- $this->assertNotContains( 'ORDER', $q2->request );
- $q3 = new WP_Query( array( 'post_type' => 'post' ) );
- $this->assertContains(
- "ORDER BY $wpdb->posts.post_date DESC",
- $q3->request
- );
- $q4 = new WP_Query( array( 'post_type' => 'post' ) );
- $this->assertContains(
- "ORDER BY $wpdb->posts.post_date DESC",
- $q4->request
- );
- }
- /**
- * @ticket 17065
- */
- function test_order() {
- global $wpdb;
- $q1 = new WP_Query(
- array(
- 'orderby' => array(
- 'post_type' => 'foo',
- ),
- )
- );
- $this->assertContains(
- "ORDER BY $wpdb->posts.post_type DESC",
- $q1->request
- );
- $q2 = new WP_Query(
- array(
- 'orderby' => 'title',
- 'order' => 'foo',
- )
- );
- $this->assertContains(
- "ORDER BY $wpdb->posts.post_title DESC",
- $q2->request
- );
- $q3 = new WP_Query(
- array(
- 'order' => 'asc',
- )
- );
- $this->assertContains(
- "ORDER BY $wpdb->posts.post_date ASC",
- $q3->request
- );
- }
- /**
- * @ticket 29629
- */
- function test_orderby() {
- // 'rand' is a valid value.
- $q = new WP_Query( array( 'orderby' => 'rand' ) );
- $this->assertContains( 'ORDER BY RAND()', $q->request );
- $this->assertNotContains( 'ASC', $q->request );
- $this->assertNotContains( 'DESC', $q->request );
- // This isn't allowed.
- $q2 = new WP_Query( array( 'order' => 'rand' ) );
- $this->assertContains( 'ORDER BY', $q2->request );
- $this->assertNotContains( 'RAND()', $q2->request );
- $this->assertContains( 'DESC', $q2->request );
- // 'none' is a valid value.
- $q3 = new WP_Query( array( 'orderby' => 'none' ) );
- $this->assertNotContains( 'ORDER BY', $q3->request );
- $this->assertNotContains( 'DESC', $q3->request );
- $this->assertNotContains( 'ASC', $q3->request );
- // False is a valid value.
- $q4 = new WP_Query( array( 'orderby' => false ) );
- $this->assertNotContains( 'ORDER BY', $q4->request );
- $this->assertNotContains( 'DESC', $q4->request );
- $this->assertNotContains( 'ASC', $q4->request );
- // Empty array() is a valid value.
- $q5 = new WP_Query( array( 'orderby' => array() ) );
- $this->assertNotContains( 'ORDER BY', $q5->request );
- $this->assertNotContains( 'DESC', $q5->request );
- $this->assertNotContains( 'ASC', $q5->request );
- }
- /**
- * @ticket 35692
- */
- public function test_orderby_rand_with_seed() {
- $q = new WP_Query(
- array(
- 'orderby' => 'RAND(5)',
- )
- );
- $this->assertContains( 'ORDER BY RAND(5)', $q->request );
- }
- /**
- * @ticket 35692
- */
- public function test_orderby_rand_should_ignore_invalid_seed() {
- $q = new WP_Query(
- array(
- 'orderby' => 'RAND(foo)',
- )
- );
- $this->assertNotContains( 'ORDER BY RAND', $q->request );
- }
- /**
- * @ticket 35692
- */
- public function test_orderby_rand_with_seed_should_be_case_insensitive() {
- $q = new WP_Query(
- array(
- 'orderby' => 'rand(5)',
- )
- );
- $this->assertContains( 'ORDER BY RAND(5)', $q->request );
- }
- /**
- * Tests the post_name__in attribute of WP_Query.
- *
- * @ticket 33065
- */
- public function test_post_name__in() {
- $q = new WP_Query();
- $post_ids[0] = self::factory()->post->create(
- array(
- 'post_title' => 'woo',
- 'post_date' => '2015-07-23 00:00:00',
- )
- );
- $post_ids[1] = self::factory()->post->create(
- array(
- 'post_title' => 'hoo',
- 'post_date' => '2015-07-23 00:00:00',
- )
- );
- $post_ids[2] = self::factory()->post->create(
- array(
- 'post_title' => 'test',
- 'post_date' => '2015-07-23 00:00:00',
- )
- );
- $post_ids[3] = self::factory()->post->create(
- array(
- 'post_title' => 'me',
- 'post_date' => '2015-07-23 00:00:00',
- )
- );
- $requested = array( $post_ids[0], $post_ids[3] );
- $q->query(
- array(
- 'post_name__in' => array( 'woo', 'me' ),
- 'fields' => 'ids',
- )
- );
- $actual_posts = $q->get_posts();
- $this->assertSameSets( $requested, $actual_posts );
- $requested = array( $post_ids[1], $post_ids[2] );
- $q->query(
- array(
- 'post_name__in' => array( 'hoo', 'test' ),
- 'fields' => 'ids',
- )
- );
- $actual_posts = $q->get_posts();
- $this->assertSameSets( $requested, $actual_posts );
- }
- /**
- * @ticket 36687
- */
- public function test_posts_pre_query_filter_should_bypass_database_query() {
- global $wpdb;
- add_filter( 'posts_pre_query', array( __CLASS__, 'filter_posts_pre_query' ) );
- $num_queries = $wpdb->num_queries;
- $q = new WP_Query(
- array(
- 'fields' => 'ids',
- 'no_found_rows' => true,
- )
- );
- remove_filter( 'posts_pre_query', array( __CLASS__, 'filter_posts_pre_query' ) );
- $this->assertSame( $num_queries, $wpdb->num_queries );
- $this->assertSame( array( 12345 ), $q->posts );
- }
- public static function filter_posts_pre_query( $posts ) {
- return array( 12345 );
- }
- /**
- * @ticket 36687
- */
- public function test_posts_pre_query_filter_should_respect_set_found_posts() {
- global $wpdb;
- $this->post_id = self::factory()->post->create();
- // Prevent the DB query.
- add_filter( 'posts_request', '__return_empty_string' );
- add_filter( 'found_posts_query', '__return_empty_string' );
- // Add the post and found_posts.
- add_filter( 'the_posts', array( $this, 'filter_the_posts' ) );
- add_filter( 'found_posts', array( $this, 'filter_found_posts' ) );
- $q = new WP_Query( array( 'suppress_filters' => false ) );
- remove_filter( 'posts_request', '__return_empty_string' );
- remove_filter( 'found_posts_query', '__return_empty_string' );
- remove_filter( 'the_posts', array( $this, 'filter_the_posts' ) );
- remove_filter( 'found_posts', array( $this, 'filter_found_posts' ) );
- $this->assertSame( array( $this->post_id ), wp_list_pluck( $q->posts, 'ID' ) );
- $this->assertSame( 1, $q->found_posts );
- }
- public function filter_the_posts() {
- return array( get_post( $this->post_id ) );
- }
- public function filter_found_posts( $posts ) {
- return 1;
- }
- /**
- * @ticket 36687
- */
- public function test_set_found_posts_fields_ids() {
- register_post_type( 'wptests_pt' );
- $posts = self::factory()->post->create_many( 2, array( 'post_type' => 'wptests_pt' ) );
- foreach ( $posts as $p ) {
- clean_post_cache( $p );
- }
- $q = new WP_Query(
- array(
- 'post_type' => 'wptests_pt',
- 'posts_per_page' => 1,
- 'fields' => 'ids',
- )
- );
- $this->assertSame( 2, $q->found_posts );
- $this->assertEquals( 2, $q->max_num_pages );
- }
- /**
- * @ticket 36687
- */
- public function test_set_found_posts_fields_idparent() {
- register_post_type( 'wptests_pt' );
- $posts = self::factory()->post->create_many( 2, array( 'post_type' => 'wptests_pt' ) );
- foreach ( $posts as $p ) {
- clean_post_cache( $p );
- }
- $q = new WP_Query(
- array(
- 'post_type' => 'wptests_pt',
- 'posts_per_page' => 1,
- 'fields' => 'id=>parent',
- )
- );
- $this->assertSame( 2, $q->found_posts );
- $this->assertEquals( 2, $q->max_num_pages );
- }
- /**
- * @ticket 36687
- */
- public function test_set_found_posts_fields_split_the_query() {
- register_post_type( 'wptests_pt' );
- $posts = self::factory()->post->create_many( 2, array( 'post_type' => 'wptests_pt' ) );
- foreach ( $posts as $p ) {
- clean_post_cache( $p );
- }
- add_filter( 'split_the_query', '__return_true' );
- $q = new WP_Query(
- array(
- 'post_type' => 'wptests_pt',
- 'posts_per_page' => 1,
- )
- );
- remove_filter( 'split_the_query', '__return_true' );
- $this->assertSame( 2, $q->found_posts );
- $this->assertEquals( 2, $q->max_num_pages );
- }
- /**
- * @ticket 36687
- */
- public function test_set_found_posts_fields_not_split_the_query() {
- register_post_type( 'wptests_pt' );
- $posts = self::factory()->post->create_many( 2, array( 'post_type' => 'wptests_pt' ) );
- foreach ( $posts as $p ) {
- clean_post_cache( $p );
- }
- // ! $split_the_query
- add_filter( 'split_the_query', '__return_false' );
- $q = new WP_Query(
- array(
- 'post_type' => 'wptests_pt',
- 'posts_per_page' => 1,
- )
- );
- remove_filter( 'split_the_query', '__return_false' );
- $this->assertSame( 2, $q->found_posts );
- $this->assertEquals( 2, $q->max_num_pages );
- }
- public function set_found_posts_provider() {
- // Count return 0 for null, but 1 for other data you may not expect.
- return array(
- array( null, 0 ),
- array( '', 1 ),
- array( "To life, to life, l'chaim", 1 ),
- array( false, 1 ),
- );
- }
- /**
- * @ticket 42860
- *
- * @dataProvider set_found_posts_provider
- */
- public function test_set_found_posts_not_posts_as_an_array( $posts, $expected ) {
- $q = new WP_Query(
- array(
- 'post_type' => 'wptests_pt',
- 'posts_per_page' => 1,
- )
- );
- $q->posts = $posts;
- $methd = new ReflectionMethod( 'WP_Query', 'set_found_posts' );
- $methd->setAccessible( true );
- $methd->invoke( $q, array( 'no_found_rows' => false ), array() );
- $this->assertSame( $expected, $q->found_posts );
- }
- /**
- * @ticket 42469
- */
- public function test_found_posts_should_be_integer_not_string() {
- $this->post_id = self::factory()->post->create();
- $q = new WP_Query(
- array(
- 'posts_per_page' => 1,
- )
- );
- $this->assertInternalType( 'int', $q->found_posts );
- }
- /**
- * @ticket 42469
- */
- public function test_found_posts_should_be_integer_even_if_found_posts_filter_returns_string_value() {
- $this->post_id = self::factory()->post->create();
- add_filter( 'found_posts', '__return_empty_string' );
- $q = new WP_Query(
- array(
- 'posts_per_page' => 1,
- )
- );
- remove_filter( 'found_posts', '__return_empty_string' );
- $this->assertInternalType( 'int', $q->found_posts );
- }
- }
|