Make WordPress Core

Ticket #36696: 36696_tests.patch

File 36696_tests.patch, 3.0 KB (added by gitlost, 9 years ago)

Unit tests.

  • tests/phpunit/tests/meta/query.php

     
    884884
    885885                $this->assertTrue( $q->has_or_relation() );
    886886        }
     887
     888        /**
     889         * @group 36696
     890         */
     891        function test_cast_both_sides() {
     892                global $wpdb;
     893
     894                $query = new WP_Meta_Query( array(
     895                        array(
     896                                'key'       => 'number_string',
     897                                'value'     => '1000000',
     898                                'compare'   => '<=',
     899                                'type'      => 'NUMERIC',
     900                        ),
     901                ) );
     902
     903                $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     904                $this->assertNotContains( "CAST({$wpdb->postmeta}.meta_value AS SIGNED) <= '1000000'", $sql['where'] );
     905                $this->assertContains( "CAST({$wpdb->postmeta}.meta_value AS SIGNED) <= 1000000", $sql['where'] );
     906        }
    887907}
  • tests/phpunit/tests/query/metaQuery.php

     
    16941694
    16951695                $this->assertEqualSets( array( 'foo_key', 'foo_key-1', 'foo_key-2' ), array_keys( $q->meta_query->get_clauses() ) );
    16961696        }
     1697
     1698        /**
     1699         * @ticket 36696
     1700         */
     1701        public function test_cast_value_numeric_big() {
     1702                $posts = $this->factory->post->create_many( 1 );
     1703
     1704                // Will trigger (depending on system) MySQL string to float type conversion on quoted numbers.
     1705                $big_int = 0x20000000000001; // 0x20 0000 0000 0001 (54 bit integer + 1, 9007199254740993).
     1706                update_post_meta( $posts[0], 'foo', $big_int - 1 );
     1707
     1708                $q = new WP_Query( array(
     1709                        'update_post_meta_cache' => false,
     1710                        'update_term_meta_cache' => false,
     1711                        'fields' => 'ids',
     1712                        'meta_query' => array(
     1713                                array(
     1714                                        'key' => 'foo',
     1715                                        'value' => $big_int,
     1716                                        'compare' => '<',
     1717                                        'type' => 'NUMERIC',
     1718                                ),
     1719                        ),
     1720                ) );
     1721               
     1722                $this->assertEquals( array( $posts[0] ), $q->posts );
     1723        }
     1724
     1725        /**
     1726         * @ticket 36696
     1727         */
     1728        public function test_cast_value_numeric_max() {
     1729                $posts = $this->factory->post->create_many( 1 );
     1730
     1731                $max_int = PHP_INT_MAX;
     1732                update_post_meta( $posts[0], 'foo', $max_int - 1 );
     1733
     1734                $q = new WP_Query( array(
     1735                        'update_post_meta_cache' => false,
     1736                        'update_term_meta_cache' => false,
     1737                        'fields' => 'ids',
     1738                        'meta_query' => array(
     1739                                array(
     1740                                        'key' => 'foo',
     1741                                        'value' => $max_int,
     1742                                        'compare' => '<',
     1743                                        'type' => 'NUMERIC',
     1744                                ),
     1745                        ),
     1746                ) );
     1747               
     1748                $this->assertEquals( array( $posts[0] ), $q->posts );
     1749        }
     1750
     1751        /**
     1752         * @ticket 36696
     1753         */
     1754        public function test_cast_value_numeric_ignore_if_like() {
     1755                $posts = $this->factory->post->create_many( 1 );
     1756
     1757                update_post_meta( $posts[0], 'foo', 10101 );
     1758
     1759                $q = new WP_Query( array(
     1760                        'update_post_meta_cache' => false,
     1761                        'update_term_meta_cache' => false,
     1762                        'fields' => 'ids',
     1763                        'meta_query' => array(
     1764                                array(
     1765                                        'key' => 'foo',
     1766                                        'value' => 101,
     1767                                        'compare' => 'LIKE',
     1768                                        'type' => 'NUMERIC',
     1769                                ),
     1770                        ),
     1771                ) );
     1772               
     1773                $this->assertEquals( array( $posts[0] ), $q->posts );
     1774        }
    16971775}