Make WordPress Core

Ticket #23033: 23033.4.diff

File 23033.4.diff, 7.1 KB (added by ericlewis, 10 years ago)
  • tests/phpunit/tests/post/query.php

     
    200200    }
    201201
    202202    /**
     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
     335    /**
    203336     * @ticket 20604
    204337     */
    205338    function test_taxonomy_empty_or() {
  • tests/phpunit/tests/meta/query.php

     
    105105                $this->assertEquals( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_key = 'my_third_key'" ) );
    106106
    107107        }
     108
     109        /**
     110         * @ticket 23033
     111         */
     112        function test_get_cast_for_type() {
     113                $query = new WP_Meta_Query();
     114                $this->assertEquals( 'BINARY', $query->get_cast_for_type( 'BINARY' ) );
     115                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'CHAR' ) );
     116                $this->assertEquals( 'DATE', $query->get_cast_for_type( 'DATE' ) );
     117                $this->assertEquals( 'DATETIME', $query->get_cast_for_type( 'DATETIME' ) );
     118                $this->assertEquals( 'SIGNED', $query->get_cast_for_type( 'SIGNED' ) );
     119                $this->assertEquals( 'UNSIGNED', $query->get_cast_for_type( 'UNSIGNED' ) );
     120                $this->assertEquals( 'TIME', $query->get_cast_for_type( 'TIME' ) );
     121                $this->assertEquals( 'SIGNED', $query->get_cast_for_type( 'NUMERIC' ) );
     122                $this->assertEquals( 'NUMERIC(10)', $query->get_cast_for_type( 'NUMERIC(10)' ) );
     123                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10)' ) );
     124                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10 )' ) );
     125                $this->assertEquals( 'NUMERIC(10, 5)', $query->get_cast_for_type( 'NUMERIC(10, 5)' ) );
     126                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC(10,  5)' ) );
     127                $this->assertEquals( 'NUMERIC(10,5)', $query->get_cast_for_type( 'NUMERIC(10,5)' ) );
     128                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10, 5 )' ) );
     129                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC(10, 5 )' ) );
     130                $this->assertEquals( 'DECIMAL', $query->get_cast_for_type( 'DECIMAL' ) );
     131                $this->assertEquals( 'DECIMAL(10)', $query->get_cast_for_type( 'DECIMAL(10)' ) );
     132                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL( 10 )' ) );
     133                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL( 10)' ) );
     134                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL(10 )' ) );
     135                $this->assertEquals( 'DECIMAL(10, 5)', $query->get_cast_for_type( 'DECIMAL(10, 5)' ) );
     136                $this->assertEquals( 'DECIMAL(10,5)', $query->get_cast_for_type( 'DECIMAL(10,5)' ) );
     137                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL(10,  5)' ) );
     138
     139                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'ANYTHING ELSE' ) );
     140        }
    108141}
  • src/wp-includes/meta.php

     
    702702
    703703                $meta_type = strtoupper( $type );
    704704
    705                 if ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED', 'NUMERIC' ) ) )
     705                if ( ! preg_match( '/^(?:BINARY|CHAR|DATE|DATETIME|SIGNED|UNSIGNED|TIME|NUMERIC(?:\(\d+(?:,\s?\d+)?\))?|DECIMAL(?:\(\d+(?:,\s?\d+)?\))?)$/', $meta_type ) )
    706706                        return 'CHAR';
    707707
    708708                if ( 'NUMERIC' == $meta_type )