Make WordPress Core

Ticket #20308: 20308.2.diff

File 20308.2.diff, 3.3 KB (added by wonderboymusic, 11 years ago)
  • src/wp-includes/query.php

     
    26732673                                $post_type_cap = $post_type;
    26742674                }
    26752675
     2676                if ( array_key_exists( 'has_password', $q ) ) {
     2677                        $where .= sprintf( " AND $wpdb->posts.post_password %s ''", $q['has_password'] ? '!=' : '=' );
     2678                }
     2679
     2680                if ( isset( $q['post_password'] ) ) {
     2681                        $where .= $wpdb->prepare( " AND $wpdb->posts.post_password = %s", $q['post_password'] );
     2682                        if ( empty( $q['perm'] ) ) {
     2683                                $q['perm'] = 'readable';
     2684                        }
     2685                }
     2686
    26762687                if ( 'any' == $post_type ) {
    26772688                        $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
    26782689                        if ( empty( $in_search_post_types ) )
  • tests/phpunit/tests/query/results.php

     
    533533                $this->assertFalse( $this->q->is_month );
    534534                $this->assertFalse( $this->q->is_year );
    535535        }
     536
     537        function test_post_password() {
     538                $post_id1 = (string) $this->factory->post->create( array( 'post_password' => '' ) );
     539                $post_id2 = (string) $this->factory->post->create( array( 'post_password' => 'burrito' ) );
     540                $post_id3 = (string) $this->factory->post->create( array( 'post_password' => 'burrito' ) );
     541
     542                // there are too many posts already in the database
     543                $args = array( 'post__in' => array( $post_id1, $post_id2, $post_id3 ), 'fields' => 'ids', 'orderby' => 'ID' );
     544
     545                $result1 = $this->q->query( array_merge( $args, array( 'has_password' => true ) ) );
     546                $this->assertEquals( array( $post_id3, $post_id2 ), $result1 );
     547                $result2 = $this->q->query( array_merge( $args, array( 'has_password' => false ) ) );
     548                $this->assertEquals( array( $post_id1 ), $result2 );
     549                $result3 = $this->q->query( array_merge( $args, array( 'has_password' => null ) ) );
     550                $this->assertEquals( array( $post_id1 ), $result3 );
     551
     552                $result4 = $this->q->query( array_merge( $args, array( 'has_password' => true, 'post_password' => '' ) ) );
     553                $this->assertEquals( array(), $result4 );
     554                $result5 = $this->q->query( array_merge( $args, array( 'has_password' => false, 'post_password' => '' ) ) );
     555                $this->assertEquals( array( $post_id1 ), $result5 );
     556                $result6 = $this->q->query( array_merge( $args, array( 'has_password' => null, 'post_password' => '' ) ) );
     557                $this->assertEquals( array( $post_id1 ), $result6 );
     558
     559                $result7 = $this->q->query( array_merge( $args, array( 'has_password' => true, 'post_password' => 'burrito' ) ) );
     560                $this->assertEquals( array( $post_id3, $post_id2 ), $result7 );
     561                $result8 = $this->q->query( array_merge( $args, array( 'has_password' => false, 'post_password' => 'burrito' ) ) );
     562                $this->assertEquals( array(), $result8 );
     563                $result9 = $this->q->query( array_merge( $args, array( 'has_password' => null, 'post_password' => 'burrito' ) ) );
     564                $this->assertEquals( array(), $result9 );
     565
     566                $result10 = $this->q->query( array_merge( $args, array( 'post_password' => '' ) ) );
     567                $this->assertEquals( array( $post_id1 ), $result10 );
     568                $result11 = $this->q->query( array_merge( $args, array( 'post_password' => 'burrito' ) ) );
     569                $this->assertEquals( array( $post_id3, $post_id2 ), $result11 );
     570        }
    536571}