Make WordPress Core

Changeset 26055


Ignore:
Timestamp:
11/08/2013 11:10:41 PM (13 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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/meta.php

    r26053 r26055  
    708708                $meta_type = strtoupper( $type );
    709709
    710                 if ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED', 'NUMERIC' ) ) )
     710                if ( ! preg_match( '/^(?:BINARY|CHAR|DATE|DATETIME|SIGNED|UNSIGNED|TIME|NUMERIC(?:\(\d+(?:,\s?\d+)?\))?|DECIMAL(?:\(\d+(?:,\s?\d+)?\))?)$/', $meta_type ) )
    711711                        return 'CHAR';
    712712
  • trunk/tests/phpunit/tests/meta/query.php

    r26053 r26055  
    109109        /**
    110110         * @ticket 22967
    111          */ 
     111         */
    112112        function test_null_value_sql() {
    113113                global $wpdb;
     
    120120                $this->assertEquals( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = '')" ) );
    121121        }
     122
     123        /**
     124         * @ticket 23033
     125         */
     126        function test_get_cast_for_type() {
     127                $query = new WP_Meta_Query();
     128                $this->assertEquals( 'BINARY', $query->get_cast_for_type( 'BINARY' ) );
     129                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'CHAR' ) );
     130                $this->assertEquals( 'DATE', $query->get_cast_for_type( 'DATE' ) );
     131                $this->assertEquals( 'DATETIME', $query->get_cast_for_type( 'DATETIME' ) );
     132                $this->assertEquals( 'SIGNED', $query->get_cast_for_type( 'SIGNED' ) );
     133                $this->assertEquals( 'UNSIGNED', $query->get_cast_for_type( 'UNSIGNED' ) );
     134                $this->assertEquals( 'TIME', $query->get_cast_for_type( 'TIME' ) );
     135                $this->assertEquals( 'SIGNED', $query->get_cast_for_type( 'NUMERIC' ) );
     136                $this->assertEquals( 'NUMERIC(10)', $query->get_cast_for_type( 'NUMERIC(10)' ) );
     137                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10)' ) );
     138                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10 )' ) );
     139                $this->assertEquals( 'NUMERIC(10, 5)', $query->get_cast_for_type( 'NUMERIC(10, 5)' ) );
     140                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC(10,  5)' ) );
     141                $this->assertEquals( 'NUMERIC(10,5)', $query->get_cast_for_type( 'NUMERIC(10,5)' ) );
     142                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10, 5 )' ) );
     143                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC(10, 5 )' ) );
     144                $this->assertEquals( 'DECIMAL', $query->get_cast_for_type( 'DECIMAL' ) );
     145                $this->assertEquals( 'DECIMAL(10)', $query->get_cast_for_type( 'DECIMAL(10)' ) );
     146                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL( 10 )' ) );
     147                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL( 10)' ) );
     148                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL(10 )' ) );
     149                $this->assertEquals( 'DECIMAL(10, 5)', $query->get_cast_for_type( 'DECIMAL(10, 5)' ) );
     150                $this->assertEquals( 'DECIMAL(10,5)', $query->get_cast_for_type( 'DECIMAL(10,5)' ) );
     151                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL(10,  5)' ) );
     152
     153                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'ANYTHING ELSE' ) );
     154        }
    122155}
  • 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.