Make WordPress Core


Ignore:
Timestamp:
11/08/2013 11:10:41 PM (11 years ago)
Author:
wonderboymusic
Message:

Produce proper CAST for DECIMAL and NUMERIC in Meta Query. Adds a bunch of unit tests.

Props ericlewis.
Fixes #23033.

File:
1 edited

Legend:

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

    r25002 r26055  
    200200    }
    201201
     202    /**
     203     * @ticket 23033
     204     */
     205    function test_meta_query_decimal_results() {
     206        $post_1 = $this->factory->post->create();
     207        $post_2 = $this->factory->post->create();
     208        $post_3 = $this->factory->post->create();
     209        $post_4 = $this->factory->post->create();
     210
     211        update_post_meta( $post_1, 'decimal_value', '-0.3' );
     212        update_post_meta( $post_2, 'decimal_value', '0.23409844' );
     213        update_post_meta( $post_3, 'decimal_value', '0.3' );
     214        update_post_meta( $post_4, 'decimal_value', '0.4' );
     215
     216        $query = new WP_Query( array(
     217            'meta_query' => array(
     218                    array(
     219                        'key' => 'decimal_value',
     220                        'value' => '.300',
     221                        'compare' => '=',
     222                        'type' => 'DECIMAL(10,2)'
     223                    )
     224                ),
     225        ) );
     226        $this->assertEquals( array( $post_3 ), wp_list_pluck( $query->posts, 'ID' ) );
     227
     228        $query = new WP_Query( array(
     229            'meta_query' => array(
     230                    array(
     231                        'key' => 'decimal_value',
     232                        'value' => '0.35',
     233                        'compare' => '>',
     234                        'type' => 'DECIMAL(10,2)'
     235                    )
     236                ),
     237        ) );
     238        $this->assertEquals( array( $post_4 ), wp_list_pluck( $query->posts, 'ID' ) );
     239
     240        $query = new WP_Query( array(
     241            'meta_query' => array(
     242                    array(
     243                        'key' => 'decimal_value',
     244                        'value' => '0.3',
     245                        'compare' => '>=',
     246                        'type' => 'DECIMAL(10,2)'
     247                    )
     248                ),
     249        ) );
     250        $this->assertEquals( array( $post_3, $post_4 ), wp_list_pluck( $query->posts, 'ID' ) );
     251
     252        $query = new WP_Query( array(
     253            'meta_query' => array(
     254                    array(
     255                        'key' => 'decimal_value',
     256                        'value' => '0',
     257                        'compare' => '<',
     258                        'type' => 'DECIMAL(10,2)'
     259                    )
     260                ),
     261        ) );
     262        $this->assertEquals( array( $post_1 ), wp_list_pluck( $query->posts, 'ID' ) );
     263
     264        $query = new WP_Query( array(
     265            'meta_query' => array(
     266                    array(
     267                        'key' => 'decimal_value',
     268                        'value' => '0.3',
     269                        'compare' => '<=',
     270                        'type' => 'DECIMAL(10,2)'
     271                    )
     272                ),
     273
     274        ) );
     275        $this->assertEquals( array( $post_1, $post_2, $post_3 ), wp_list_pluck( $query->posts, 'ID' ) );
     276
     277        $query = new WP_Query( array(
     278            'meta_query' => array(
     279                    array(
     280                        'key' => 'decimal_value',
     281                        'value' => array( 0.23409845, .31 ),
     282                        'compare' => 'BETWEEN',
     283                        'type' => 'DECIMAL(10, 10)'
     284                    )
     285                ),
     286        ) );
     287        $this->assertEquals( array( $post_3 ), wp_list_pluck( $query->posts, 'ID' ) );
     288
     289        $query = new WP_Query( array(
     290            'meta_query' => array(
     291                    array(
     292                        'key' => 'decimal_value',
     293                        'value' => array( 0.23409845, .31 ),
     294                        'compare' => 'NOT BETWEEN',
     295                        'type' => 'DECIMAL(10,10)'
     296                    )
     297                ),
     298        ) );
     299        $this->assertEquals( array( $post_1, $post_2, $post_4 ), wp_list_pluck( $query->posts, 'ID' ) );
     300
     301        $query = new WP_Query( array(
     302            'meta_query' => array(
     303                    array(
     304                        'key' => 'decimal_value',
     305                        'value' => '.3',
     306                        'compare' => 'LIKE',
     307                        'type' => 'DECIMAL(10,2)'
     308                    )
     309                ),
     310        ) );
     311        $this->assertEquals( array( $post_1, $post_3 ), wp_list_pluck( $query->posts, 'ID' ) );
     312
     313            $query = new WP_Query( array(
     314                'meta_query' => array(
     315                        array(
     316                            'key' => 'decimal_value',
     317                            'value' => '.3',
     318                            'compare' => 'NOT LIKE',
     319                            'type' => 'DECIMAL(10,2)'
     320                        )
     321                    ),
     322            ) );
     323            $this->assertEquals( array( $post_2, $post_4 ), wp_list_pluck( $query->posts, 'ID' ) );
     324
     325        $query = new WP_Query( array(
     326            'orderby' => 'meta_value',
     327            'order' => 'DESC',
     328            'meta_key' => 'decimal_value',
     329            'meta_type' => 'DECIMAL(10, 2)'
     330        ) );
     331        $this->assertEquals( array( $post_4, $post_3, $post_2, $post_1 ), wp_list_pluck( $query->posts, 'ID' ) );
     332
     333    }
     334
    202335    /**
    203336     * @ticket 20604
Note: See TracChangeset for help on using the changeset viewer.