wordpress-develop/tests/phpunit/tests/query.php
Boone Gorges b7c8dbf4a2 Better default values in WP_Query::get_queried_object().
Setting the default value of the `queried_object_id` property to `0` meant
that, when called early enough in the WP bootstrap, `get_queried_object()`
could short-circuit the normal query by fooling it into thinking that the
request was for a page with id 0. Setting the default value to `null` instead
avoids this problem.

Props gradyetc, jazbek.
Fixes #31355.

git-svn-id: https://develop.svn.wordpress.org/trunk@34073 602fd350-edb4-49c9-b593-d223f7449a82
2015-09-12 03:36:12 +00:00

463 lines
14 KiB
PHP

<?php
class Tests_Query extends WP_UnitTestCase {
function setUp() {
global $wp_rewrite;
parent::setUp();
$wp_rewrite->init();
$wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
create_initial_taxonomies();
$wp_rewrite->flush_rules();
}
/**
* @ticket 24785
*
*/
function test_nested_loop_reset_postdata() {
$post_id = $this->factory->post->create();
$nested_post_id = $this->factory->post->create();
$first_query = new WP_Query( array( 'post__in' => array( $post_id ) ) );
while ( $first_query->have_posts() ) { $first_query->the_post();
$second_query = new WP_Query( array( 'post__in' => array( $nested_post_id ) ) );
while ( $second_query->have_posts() ) {
$second_query->the_post();
$this->assertEquals( get_the_ID(), $nested_post_id );
}
$first_query->reset_postdata();
$this->assertEquals( get_the_ID(), $post_id );
}
}
/**
* @ticket 16471
*/
function test_default_query_var() {
$query = new WP_Query;
$this->assertEquals( '', $query->get( 'nonexistent' ) );
$this->assertFalse( $query->get( 'nonexistent', false ) );
$this->assertTrue( $query->get( 'nonexistent', true ) );
}
/**
* @ticket 25380
*/
function test_pre_posts_per_page() {
$this->factory->post->create_many( 10 );
add_action( 'pre_get_posts', array( $this, 'filter_posts_per_page' ) );
$this->go_to( get_feed_link() );
$this->assertEquals( 30, get_query_var( 'posts_per_page' ) );
}
function filter_posts_per_page( &$query ) {
$query->set( 'posts_per_rss', 30 );
}
/**
* @ticket 26627
*/
function test_tag_queried_object() {
$slug = 'tag-slug-26627';
$this->factory->tag->create( array( 'slug' => $slug ) );
$tag = get_term_by( 'slug', $slug, 'post_tag' );
add_action( 'pre_get_posts', array( $this, '_tag_queried_object' ), 11 );
$this->go_to( get_term_link( $tag ) );
$this->assertQueryTrue( 'is_tag', 'is_archive' );
$this->assertNotEmpty( get_query_var( 'tag_id' ) );
$this->assertNotEmpty( get_query_var( 'tag' ) );
$this->assertEmpty( get_query_var( 'tax_query' ) );
$this->assertCount( 1, get_query_var( 'tag_slug__in' ) );
$this->assertEquals( get_queried_object(), $tag );
remove_action( 'pre_get_posts', array( $this, '_tag_queried_object' ), 11 );
}
function _tag_queried_object( &$query ) {
$tag = get_term_by( 'slug', 'tag-slug-26627', 'post_tag' );
$this->assertTrue( $query->is_tag() );
$this->assertTrue( $query->is_archive() );
$this->assertNotEmpty( $query->get( 'tag' ) );
$this->assertCount( 1, $query->get( 'tag_slug__in' ) );
$this->assertEquals( $query->get_queried_object(), $tag );
}
/**
* @ticket 31246
*/
public function test_get_queried_object_should_return_null_when_is_tax_is_true_but_the_taxonomy_args_have_been_removed_in_a_parse_query_callback() {
// Don't override the args provided below.
remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) );
register_taxonomy( 'wptests_tax', 'post' );
$terms = $this->factory->term->create_many( 2, array(
'taxonomy' => 'wptests_tax',
) );
$posts = $this->factory->post->create_many( 2 );
wp_set_object_terms( $posts[0], array( $terms[0] ), 'wptests_tax' );
wp_set_object_terms( $posts[1], array( $terms[1] ), 'wptests_tax' );
add_action( 'parse_query', array( $this, 'filter_parse_query_to_remove_tax' ) );
$q = new WP_Query( array(
'fields' => 'ids',
'wptests_tax' => $terms[1],
) );
remove_action( 'parse_query', array( $this, 'filter_parse_query_to_remove_tax' ) );
$this->assertNull( $q->get_queried_object() );
}
public function filter_parse_query_to_remove_tax( $q ) {
unset( $q->query_vars['wptests_tax'] );
}
public function test_orderby_space_separated() {
global $wpdb;
$q = new WP_Query( array(
'orderby' => 'title date',
'order' => 'DESC',
) );
$this->assertContains( "ORDER BY $wpdb->posts.post_title DESC, $wpdb->posts.post_date DESC", $q->request );
}
public function test_cat_querystring_single_term() {
$c1 = $this->factory->category->create( array(
'name' => 'Test Category 1',
'slug' => 'test1',
) );
$c2 = $this->factory->category->create( array(
'name' => 'Test Category 2',
'slug' => 'test2',
) );
$p1 = $this->factory->post->create();
$p2 = $this->factory->post->create();
$p3 = $this->factory->post->create();
wp_set_object_terms( $p1, $c1, 'category' );
wp_set_object_terms( $p2, array( $c1, $c2 ), 'category' );
wp_set_object_terms( $p3, $c2, 'category' );
$url = add_query_arg( array(
'cat' => $c1,
), '/' );
$this->go_to( $url );
$matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
$this->assertEqualSets( array( $p1, $p2 ), $matching_posts );
}
public function test_category_querystring_multiple_terms_comma_separated() {
$c1 = $this->factory->category->create( array(
'name' => 'Test Category 1',
'slug' => 'test1',
) );
$c2 = $this->factory->category->create( array(
'name' => 'Test Category 2',
'slug' => 'test2',
) );
$c3 = $this->factory->category->create( array(
'name' => 'Test Category 3',
'slug' => 'test3',
) );
$p1 = $this->factory->post->create();
$p2 = $this->factory->post->create();
$p3 = $this->factory->post->create();
$p4 = $this->factory->post->create();
wp_set_object_terms( $p1, $c1, 'category' );
wp_set_object_terms( $p2, array( $c1, $c2 ), 'category' );
wp_set_object_terms( $p3, $c2, 'category' );
wp_set_object_terms( $p4, $c3, 'category' );
$url = add_query_arg( array(
'cat' => implode( ',',array( $c1,$c2 ) ),
), '/' );
$this->go_to( $url );
$matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
$this->assertEqualSets( array( $p1, $p2, $p3 ), $matching_posts );
}
/**
* @ticket 33532
*/
public function test_category_querystring_multiple_terms_formatted_as_array() {
$c1 = $this->factory->category->create( array(
'name' => 'Test Category 1',
'slug' => 'test1',
) );
$c2 = $this->factory->category->create( array(
'name' => 'Test Category 2',
'slug' => 'test2',
) );
$c3 = $this->factory->category->create( array(
'name' => 'Test Category 3',
'slug' => 'test3',
) );
$p1 = $this->factory->post->create();
$p2 = $this->factory->post->create();
$p3 = $this->factory->post->create();
$p4 = $this->factory->post->create();
wp_set_object_terms( $p1, $c1, 'category' );
wp_set_object_terms( $p2, array( $c1, $c2 ), 'category' );
wp_set_object_terms( $p3, $c2, 'category' );
wp_set_object_terms( $p4, $c3, 'category' );
$url = add_query_arg( array(
'cat' => array( $c1, $c2 ),
), '/' );
$this->go_to( $url );
$matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
$this->assertEqualSets( array( $p1, $p2, $p3 ), $matching_posts );
}
public function test_tag_querystring_single_term() {
$t1 = $this->factory->tag->create_and_get( array(
'name' => 'Test Tag 1',
'slug' => 'test1',
) );
$t2 = $this->factory->tag->create_and_get( array(
'name' => 'Test Tag 2',
'slug' => 'test2',
) );
$p1 = $this->factory->post->create();
$p2 = $this->factory->post->create();
$p3 = $this->factory->post->create();
wp_set_object_terms( $p1, $t1->slug, 'post_tag' );
wp_set_object_terms( $p2, array( $t1->slug, $t2->slug ), 'post_tag' );
wp_set_object_terms( $p3, $t2->slug, 'post_tag' );
$url = add_query_arg( array(
'tag' => $t1,
), '/' );
$this->go_to( $url );
$matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
$this->assertEqualSets( array( $p1, $p2 ), $matching_posts );
}
public function test_tag_querystring_multiple_terms_comma_separated() {
$c1 = $this->factory->tag->create_and_get( array(
'name' => 'Test Tag 1',
'slug' => 'test1',
) );
$c2 = $this->factory->tag->create_and_get( array(
'name' => 'Test Tag 2',
'slug' => 'test2',
) );
$c3 = $this->factory->tag->create_and_get( array(
'name' => 'Test Tag 3',
'slug' => 'test3',
) );
$p1 = $this->factory->post->create();
$p2 = $this->factory->post->create();
$p3 = $this->factory->post->create();
$p4 = $this->factory->post->create();
wp_set_object_terms( $p1, $c1->slug, 'post_tag' );
wp_set_object_terms( $p2, array( $c1->slug, $c2->slug ), 'post_tag' );
wp_set_object_terms( $p3, $c2->slug, 'post_tag' );
wp_set_object_terms( $p4, $c3->slug, 'post_tag' );
$url = add_query_arg( array(
'tag' => implode( ',',array( $c1->slug,$c2->slug ) ),
), '/' );
$this->go_to( $url );
$matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
$this->assertEqualSets( array( $p1, $p2, $p3 ), $matching_posts );
}
/**
* @ticket 33532
*/
public function test_tag_querystring_multiple_terms_formatted_as_array() {
$c1 = $this->factory->tag->create_and_get( array(
'name' => 'Test Tag 1',
'slug' => 'test1',
) );
$c2 = $this->factory->tag->create_and_get( array(
'name' => 'Test Tag 2',
'slug' => 'test2',
) );
$c3 = $this->factory->tag->create_and_get( array(
'name' => 'Test Tag 3',
'slug' => 'test3',
) );
$p1 = $this->factory->post->create();
$p2 = $this->factory->post->create();
$p3 = $this->factory->post->create();
$p4 = $this->factory->post->create();
wp_set_object_terms( $p1, $c1->slug, 'post_tag' );
wp_set_object_terms( $p2, array( $c1->slug, $c2->slug ), 'post_tag' );
wp_set_object_terms( $p3, $c2->slug, 'post_tag' );
wp_set_object_terms( $p4, $c3->slug, 'post_tag' );
$url = add_query_arg( array(
'tag' => array($c1->slug,$c2->slug),
), '/' );
$this->go_to( $url );
$matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
$this->assertEqualSets( array( $p1, $p2, $p3 ), $matching_posts );
}
public function test_custom_taxonomy_querystring_single_term() {
register_taxonomy( 'test_tax_cat', 'post' );
wp_insert_term( 'test1', 'test_tax_cat' );
wp_insert_term( 'test2', 'test_tax_cat' );
wp_insert_term( 'test3', 'test_tax_cat' );
$p1 = $this->factory->post->create();
$p2 = $this->factory->post->create();
$p3 = $this->factory->post->create();
wp_set_object_terms( $p1, 'test1', 'test_tax_cat' );
wp_set_object_terms( $p2, array( 'test1', 'test2' ), 'test_tax_cat' );
wp_set_object_terms( $p3, 'test2', 'test_tax_cat' );
$url = add_query_arg( array(
'test_tax_cat' => 'test1',
), '/' );
$this->go_to( $url );
$this->assertEqualSets( array( $p1, $p2 ), wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' ) );
}
public function test_custom_taxonomy_querystring_multiple_terms_comma_separated() {
register_taxonomy( 'test_tax_cat', 'post' );
wp_insert_term( 'test1', 'test_tax_cat' );
wp_insert_term( 'test2', 'test_tax_cat' );
wp_insert_term( 'test3', 'test_tax_cat' );
$p1 = $this->factory->post->create();
$p2 = $this->factory->post->create();
$p3 = $this->factory->post->create();
$p4 = $this->factory->post->create();
wp_set_object_terms( $p1, 'test1', 'test_tax_cat' );
wp_set_object_terms( $p2, array( 'test1', 'test2' ), 'test_tax_cat' );
wp_set_object_terms( $p3, 'test2', 'test_tax_cat' );
wp_set_object_terms( $p4, "test3", 'test_tax_cat' );
$url = add_query_arg( array(
'test_tax_cat' => 'test1,test2',
), '/' );
$this->go_to( $url );
$this->assertEqualSets( array( $p1, $p2, $p3 ), wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' ) );
}
/**
* @ticket 32454
*/
public function test_custom_taxonomy_querystring_multiple_terms_formatted_as_array() {
register_taxonomy( 'test_tax_cat', 'post' );
wp_insert_term( 'test1', 'test_tax_cat' );
wp_insert_term( 'test2', 'test_tax_cat' );
wp_insert_term( 'test3', 'test_tax_cat' );
$p1 = $this->factory->post->create();
$p2 = $this->factory->post->create();
$p3 = $this->factory->post->create();
$p4 = $this->factory->post->create();
wp_set_object_terms( $p1, 'test1', 'test_tax_cat' );
wp_set_object_terms( $p2, array( 'test1', 'test2' ), 'test_tax_cat' );
wp_set_object_terms( $p3, 'test2', 'test_tax_cat' );
wp_set_object_terms( $p4, "test3", 'test_tax_cat' );
$url = add_query_arg( array(
'test_tax_cat' => array( 'test1', 'test2' ),
), '/' );
$this->go_to( $url );
$this->assertEqualSets( array( $p1, $p2, $p3 ), wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' ) );
}
/**
* @ticket 31355
*/
public function test_pages_dont_404_when_queried_post_id_is_modified() {
$post_id = $this->factory->post->create( array( 'post_title' => 'A Test Page', 'post_type' => 'page' ) );
add_action( 'parse_query', array( $this, 'filter_parse_query_to_modify_queried_post_id' ) );
$url = get_permalink( $post_id );
$this->go_to( $url );
remove_action( 'parse_query', array( $this, 'filter_parse_query_to_modify_queried_post_id' ) );
$this->assertFalse( $GLOBALS['wp_query']->is_404() );
$this->assertEquals( $post_id, $GLOBALS['wp_query']->post->ID );
}
/**
* @ticket 31355
*/
public function test_custom_hierarchical_post_types_404_when_queried_post_id_is_modified() {
global $wp_rewrite;
register_post_type( 'guide', array( 'name' => 'Guide', 'public' => true, 'hierarchical' => true ) );
$wp_rewrite->flush_rules();
$post_id = $this->factory->post->create( array( 'post_title' => 'A Test Guide', 'post_type' => 'guide' ) );
add_action( 'parse_query', array( $this, 'filter_parse_query_to_modify_queried_post_id' ) );
$url = get_permalink( $post_id );
$this->go_to( $url );
remove_action( 'parse_query', array( $this, 'filter_parse_query_to_modify_queried_post_id' ) );
$this->assertFalse( $GLOBALS['wp_query']->is_404() );
$this->assertEquals( $post_id, $GLOBALS['wp_query']->post->ID );
}
public function filter_parse_query_to_modify_queried_post_id( $query ) {
$post = get_queried_object();
}
}