WordPress.org

Make WordPress Core

Changeset 1068 in tests


Ignore:
Timestamp:
10/02/2012 07:17:31 PM (6 years ago)
Author:
ryan
Message:

Post meta query tests for various tickets.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/post/query.php

    r1067 r1068  
    181181    $this->assertEquals( array( $post_id, $post_id4, $post_id5 ), $posts );
    182182    }
     183
     184    /**
     185     * @ticket 16829
     186     */
     187    function test_meta_default_compare() {
     188    // compare should default to IN when meta_value is an array
     189    $post_id = $this->factory->post->create();
     190    add_post_meta( $post_id, 'foo', 'bar' );
     191    $post_id2 = $this->factory->post->create();
     192    add_post_meta( $post_id2, 'bar', 'baz' );
     193    $post_id3 = $this->factory->post->create();
     194    add_post_meta( $post_id3, 'foo', 'baz' );
     195    $post_id4 = $this->factory->post->create();
     196    add_post_meta( $post_id4, 'baz', 'bar' );
     197    $post_id5 = $this->factory->post->create();
     198    add_post_meta( $post_id5, 'foo', rand_str() );
     199
     200    $posts = get_posts( array(
     201        'meta_key' => 'foo',
     202        'meta_value' => array( 'bar', 'baz' )
     203    ) );
     204
     205    $this->assertEquals( 2, count( $posts ) );
     206    $posts = wp_list_pluck( $posts, 'ID' );
     207    $this->assertEquals( array( $post_id, $post_id3 ), $posts );
     208
     209    $posts = get_posts( array(
     210        'meta_key' => 'foo',
     211        'meta_value' => array( 'bar', 'baz' ),
     212        'meta_compare' => 'IN'
     213    ) );
     214
     215    $this->assertEquals( 2, count( $posts ) );
     216    $posts = wp_list_pluck( $posts, 'ID' );
     217    $this->assertEquals( array( $post_id, $post_id3 ), $posts );
     218    }
     219
     220    /**
     221     * @ticket 17264
     222     */
     223    function test_duplicate_posts_when_no_key() {
     224    $post_id = $this->factory->post->create();
     225    add_post_meta( $post_id, 'city', 'Lorem' );
     226    add_post_meta( $post_id, 'address', '123 Lorem St.' );
     227    $post_id2 = $this->factory->post->create();
     228    add_post_meta( $post_id2, 'city', 'Lorem' );
     229    $post_id3 = $this->factory->post->create();
     230    add_post_meta( $post_id3, 'city', 'Loren' );
     231
     232    $args = array(
     233        'meta_query' => array(
     234        array(
     235            'value' => 'lorem',
     236            'compare' => 'LIKE'
     237        )
     238        )
     239    );
     240
     241    $posts = get_posts( $args );
     242    $this->assertEquals( 2, count( $posts ) );
     243    $posts = wp_list_pluck( $posts, 'ID' );
     244    $this->assertEquals( array( $post_id, $post_id2 ), $posts );
     245    }
     246
     247    /**
     248     * @ticket 15292
     249     */
     250    function test_empty_meta_value() {
     251    $post_id = $this->factory->post->create();
     252    add_post_meta( $post_id, 'foo', '0' );
     253    add_post_meta( $post_id, 'bar', 0 );
     254    $post_id2 = $this->factory->post->create();
     255    add_post_meta( $post_id2, 'foo', 1 );
     256    $post_id3 = $this->factory->post->create();
     257    add_post_meta( $post_id3, 'baz', 0 );
     258    $post_id4 = $this->factory->post->create();
     259    add_post_meta( $post_id4, 'baz', 0 );
     260    $post_id5 = $this->factory->post->create();
     261    add_post_meta( $post_id5, 'baz', 0 );
     262    add_post_meta( $post_id5, 'bar', '0' );
     263    $post_id6 = $this->factory->post->create();
     264    add_post_meta( $post_id6, 'baz', 0 );
     265
     266    $posts = get_posts( array( 'meta_key' => 'foo', 'meta_value' => '0' ) );
     267    $this->assertEquals( 1, count ( $posts ) );
     268    $this->assertEquals( $post_id, $posts[0]->ID );
     269
     270    $posts = get_posts( array( 'meta_key' => 'bar', 'meta_value' => '0' ) );
     271    $this->assertEquals( 2, count ( $posts ) );
     272    $posts = wp_list_pluck( $posts, 'ID' );
     273    $this->assertEquals( array( $post_id, $post_id5 ), $posts );
     274
     275        $posts = get_posts( array( 'meta_key' => 'bar', 'meta_value' => 0 ) );
     276    $this->assertEquals( 2, count ( $posts ) );
     277    $posts = wp_list_pluck( $posts, 'ID' );
     278    $this->assertEquals( array( $post_id, $post_id5 ), $posts );
     279
     280        $posts = get_posts( array( 'meta_value' => 0 ) );
     281    $this->assertEquals( 5, count ( $posts ) );
     282    $posts = wp_list_pluck( $posts, 'ID' );
     283    $this->assertEquals( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts );
     284
     285        $posts = get_posts( array( 'meta_value' => '0' ) );
     286    $this->assertEquals( 5, count ( $posts ) );
     287    $posts = wp_list_pluck( $posts, 'ID' );
     288    $this->assertEquals( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts );
     289
     290    //$this->assertEquals( $post_id, $posts[0]->ID );
     291    //debug_log( $posts );
     292    }
    183293}
Note: See TracChangeset for help on using the changeset viewer.