Make WordPress Core

Ticket #24245: 24245.2.diff

File 24245.2.diff, 3.3 KB (added by wonderboymusic, 12 years ago)
  • tests/phpunit/tests/term/query.php

     
     1<?php
     2
     3/**
     4 * @group taxonomy
     5 */
     6class Tests_Tax_Query extends WP_UnitTestCase {
     7        protected $q;
     8
     9        function setUp() {
     10                parent::setUp();
     11                unset( $this->q );
     12                $this->q = new WP_Query();
     13        }
     14
     15        function test_category__and_var() {
     16                $term_id = $this->factory->category->create( array( 'slug' => 'woo', 'name' => 'WOO!' ) );
     17                $term_id2 = $this->factory->category->create( array( 'slug' => 'hoo', 'name' => 'HOO!' ) );
     18                $post_id = $this->factory->post->create();
     19
     20                wp_set_post_categories( $post_id, $term_id );
     21
     22                $posts = $this->q->query( array( 'category__and' => array( $term_id ) ) );
     23
     24                $this->assertEmpty( $this->q->get( 'category__and' ) );
     25                $this->assertCount( 0, $this->q->get( 'category__and' ) );
     26                $this->assertNotEmpty( $this->q->get( 'category__in' ) );
     27                $this->assertCount( 1, $this->q->get( 'category__in' ) );
     28
     29                $this->assertNotEmpty( $posts );
     30                $this->assertEquals( array( $post_id ), wp_list_pluck( $posts, 'ID' ) );
     31
     32                $posts2 = $this->q->query( array( 'category__and' => array( $term_id, $term_id2 ) ) );
     33                $this->assertNotEmpty( $this->q->get( 'category__and' ) );
     34                $this->assertCount( 2, $this->q->get( 'category__and' ) );
     35                $this->assertEmpty( $this->q->get( 'category__in' ) );
     36                $this->assertCount( 0, $this->q->get( 'category__in' ) );
     37
     38                $this->assertEmpty( $posts2 );
     39        }
     40}
     41 No newline at end of file
  • src/wp-includes/query.php

     
    17621762                        $q['cat'] = implode(',', $req_cats);
    17631763                }
    17641764
    1765                 if ( !empty($q['category__in']) ) {
    1766                         $q['category__in'] = array_map('absint', array_unique( (array) $q['category__in'] ) );
     1765                if ( ! empty( $q['category__and'] ) && 1 === count( (array) $q['category__and'] ) ) {
     1766                        $q['category__and'] = (array) $q['category__and'];
     1767                        if ( ! isset( $q['category__in'] ) )
     1768                                $q['category__in'] = array();
     1769                        $q['category__in'][] = absint( reset( $q['category__and'] ) );
     1770                        unset( $q['category__and'] );
     1771                }
     1772                       
     1773                if ( ! empty( $q['category__in'] ) ) {
     1774                        $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) );                 
    17671775                        $tax_query[] = array(
    17681776                                'taxonomy' => 'category',
    17691777                                'terms' => $q['category__in'],
     
    17721780                        );
    17731781                }
    17741782
    1775                 if ( !empty($q['category__not_in']) ) {
    1776                         $q['category__not_in'] = array_map('absint', array_unique( (array) $q['category__not_in'] ) );
     1783                if ( ! empty($q['category__not_in']) ) {
     1784                        $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) );
    17771785                        $tax_query[] = array(
    17781786                                'taxonomy' => 'category',
    17791787                                'terms' => $q['category__not_in'],
     
    17821790                        );
    17831791                }
    17841792
    1785                 if ( !empty($q['category__and']) ) {
    1786                         $q['category__and'] = array_map('absint', array_unique( (array) $q['category__and'] ) );
     1793                if ( ! empty($q['category__and']) ) {
     1794                        $q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) );
    17871795                        $tax_query[] = array(
    17881796                                'taxonomy' => 'category',
    17891797                                'terms' => $q['category__and'],