Convert category__and to category__in (less expensive) and unset it when only one category is passed. Adds unit tests.

Fixes #24245.




git-svn-id: https://develop.svn.wordpress.org/trunk@25238 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor
2013-09-04 18:16:31 +00:00
parent 14da301031
commit 0d0b17b617
2 changed files with 54 additions and 6 deletions

View File

@@ -0,0 +1,40 @@
<?php
/**
* @group taxonomy
*/
class Tests_Tax_Query extends WP_UnitTestCase {
protected $q;
function setUp() {
parent::setUp();
unset( $this->q );
$this->q = new WP_Query();
}
function test_category__and_var() {
$term_id = $this->factory->category->create( array( 'slug' => 'woo', 'name' => 'WOO!' ) );
$term_id2 = $this->factory->category->create( array( 'slug' => 'hoo', 'name' => 'HOO!' ) );
$post_id = $this->factory->post->create();
wp_set_post_categories( $post_id, $term_id );
$posts = $this->q->query( array( 'category__and' => array( $term_id ) ) );
$this->assertEmpty( $this->q->get( 'category__and' ) );
$this->assertCount( 0, $this->q->get( 'category__and' ) );
$this->assertNotEmpty( $this->q->get( 'category__in' ) );
$this->assertCount( 1, $this->q->get( 'category__in' ) );
$this->assertNotEmpty( $posts );
$this->assertEquals( array( $post_id ), wp_list_pluck( $posts, 'ID' ) );
$posts2 = $this->q->query( array( 'category__and' => array( $term_id, $term_id2 ) ) );
$this->assertNotEmpty( $this->q->get( 'category__and' ) );
$this->assertCount( 2, $this->q->get( 'category__and' ) );
$this->assertEmpty( $this->q->get( 'category__in' ) );
$this->assertCount( 0, $this->q->get( 'category__in' ) );
$this->assertEmpty( $posts2 );
}
}