Make WordPress Core


Ignore:
Timestamp:
09/04/2013 09:32:11 PM (12 years ago)
Author:
wonderboymusic
Message:

Introduce author__in and author__not_in query vars. Fixes issue with multiple author exclusion when comma-separated string is passed for author. Adds a bunch of missing unit tests.

Props pollett for initial patch.
Fixes #16854.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/query/results.php

    r25239 r25248  
    389389        $this->assertNotRegExp( '#AND 1=0#', $this->q->request );
    390390    }
     391
     392    function test_query_author_vars() {
     393        $author_1 = $this->factory->user->create( array( 'user_login' => 'admin1', 'user_pass' => rand_str(), 'role' => 'author' ) );
     394        $post_1 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_1, 'post_date' => '2007-01-01 00:00:00' ) );
     395
     396        $author_2 = $this->factory->user->create( array( 'user_login' => rand_str(), 'user_pass' => rand_str(), 'role' => 'author' ) );
     397        $post_2 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_2, 'post_date' => '2007-01-01 00:00:00' ) );
     398
     399        $author_3 = $this->factory->user->create( array( 'user_login' => rand_str(), 'user_pass' => rand_str(), 'role' => 'author' ) );
     400        $post_3 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_3, 'post_date' => '2007-01-01 00:00:00' ) );
     401
     402        $author_4 = $this->factory->user->create( array( 'user_login' => rand_str(), 'user_pass' => rand_str(), 'role' => 'author' ) );
     403        $post_4 = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_author' => $author_4, 'post_date' => '2007-01-01 00:00:00' ) );
     404
     405        $posts = $this->q->query( array(
     406            'author' => '',
     407            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     408        ) );
     409        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     410        $this->assertEqualSets( array( $author_1, $author_2, $author_3, $author_4 ), $author_ids );
     411
     412        $posts = $this->q->query( array(
     413            'author' => 0,
     414            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     415        ) );
     416        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     417        $this->assertEqualSets( array( $author_1, $author_2, $author_3, $author_4 ), $author_ids );
     418
     419        $posts = $this->q->query( array(
     420            'author' => '0',
     421            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     422        ) );
     423        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     424        $this->assertEqualSets( array( $author_1, $author_2, $author_3, $author_4 ), $author_ids );
     425
     426        $posts = $this->q->query( array(
     427            'author' => $author_1,
     428            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     429        ) );
     430        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     431        $this->assertEqualSets( array( $author_1 ), $author_ids );
     432
     433        $posts = $this->q->query( array(
     434            'author' => "$author_1",
     435            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     436        ) );
     437        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     438        $this->assertEqualSets( array( $author_1 ), $author_ids );
     439
     440        $posts = $this->q->query( array(
     441            'author' => "{$author_1},{$author_2}",
     442            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     443        ) );
     444        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     445        $this->assertEqualSets( array( $author_1, $author_2 ), $author_ids );
     446
     447        $posts = $this->q->query( array(
     448            'author' => "-{$author_1},{$author_2}",
     449            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     450        ) );
     451        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     452        $this->assertEqualSets( array( $author_2, $author_3, $author_4 ), $author_ids );
     453
     454        $posts = $this->q->query( array(
     455            'author' => "{$author_1},-{$author_2}",
     456            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     457        ) );
     458        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     459        $this->assertEqualSets( array( $author_1, $author_3, $author_4 ), $author_ids );
     460
     461        $posts = $this->q->query( array(
     462            'author' => "-{$author_1},-{$author_2}",
     463            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     464        ) );
     465        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     466        $this->assertEqualSets( array( $author_3, $author_4 ), $author_ids );
     467
     468        $posts = $this->q->query( array(
     469            'author__in' => array( $author_1, $author_2 ),
     470            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     471        ) );
     472        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     473        $this->assertEqualSets( array( $author_1, $author_2 ), $author_ids );
     474
     475        $posts = $this->q->query( array(
     476            'author__not_in' => array( $author_1, $author_2 ),
     477            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     478        ) );
     479        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     480        $this->assertEqualSets( array( $author_3, $author_4 ), $author_ids );
     481
     482        $posts = $this->q->query( array(
     483            'author_name' => 'admin1',
     484            'post__in' => array( $post_1, $post_2, $post_3, $post_4 )
     485        ) );
     486        $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) );
     487        $this->assertEqualSets( array( $author_1 ), $author_ids );
     488    }
    391489}
Note: See TracChangeset for help on using the changeset viewer.