Make WordPress Core

Ticket #43589: 43589.diff

File 43589.diff, 4.7 KB (added by soulseekah, 7 years ago)
  • src/wp-includes/wp-db.php

    diff --git src/wp-includes/wp-db.php src/wp-includes/wp-db.php
    index 77b64da..aa5fba1 100644
    class wpdb { 
    21762176                $sql = "$type INTO `$table` ($fields) VALUES ($formats)";
    21772177
    21782178                $this->check_current_query = false;
    2179                 return $this->query( $this->prepare( $sql, $values ) );
     2179
     2180                if ( count( $values ) ) {
     2181                        // Only prepare if there's actually something to prepare.
     2182                        $sql = $this->prepare( $sql, $values );
     2183                }
     2184
     2185                return $this->query( $sql );
    21802186        }
    21812187
    21822188        /**
    class wpdb { 
    22492255                $sql = "UPDATE `$table` SET $fields WHERE $conditions";
    22502256
    22512257                $this->check_current_query = false;
    2252                 return $this->query( $this->prepare( $sql, $values ) );
     2258
     2259                if ( count( $values ) ) {
     2260                        // Only prepare if there's actually something to prepare.
     2261                        $sql = $this->prepare( $sql, $values );
     2262                }
     2263
     2264                return $this->query( $sql );
    22532265        }
    22542266
    22552267        /**
  • tests/phpunit/tests/db.php

    diff --git tests/phpunit/tests/db.php tests/phpunit/tests/db.php
    index 5cfd3d4..53d63f6 100644
    class Tests_DB extends WP_UnitTestCase { 
    17711771                        ),
    17721772                );
    17731773        }
     1774
     1775        /**
     1776         * @ticket 43589
     1777         */
     1778        public function test_insert_null_value_on_null_column_single() {
     1779                global $wpdb;
     1780
     1781                $wpdb->query( "CREATE TABLE {$wpdb->prefix}test_null_column( a INT );" );
     1782
     1783                $this->assertEquals( 1, $wpdb->insert( "{$wpdb->prefix}test_null_column", array( 'a' => null ) ) );
     1784                $this->assertNull( $wpdb->get_var( "SELECT a FROM {$wpdb->prefix}test_null_column" ) );
     1785
     1786                $wpdb->query( "DROP TABLE {$wpdb->prefix}test_null_column" );
     1787        }
     1788
     1789        /**
     1790         * @ticket 43589
     1791         */
     1792        public function test_update_null_value_on_null_column_single() {
     1793                global $wpdb;
     1794
     1795                $wpdb->query( "CREATE TABLE {$wpdb->prefix}test_null_column( a INT );" );
     1796                $wpdb->insert( "{$wpdb->prefix}test_null_column", array( 'a' => null ) );
     1797
     1798                $this->assertEquals( 0, $wpdb->update( "{$wpdb->prefix}test_null_column", array( 'a' => null ), array( 'a' => null ) ) );
     1799                $this->assertEquals( array( null ), $wpdb->get_col( "SELECT a FROM {$wpdb->prefix}test_null_column" ) );
     1800
     1801                $this->assertEquals( 1, $wpdb->update( "{$wpdb->prefix}test_null_column", array( 'a' => 2 ), array( 'a' => null ) ) );
     1802                $this->assertEquals( 2, $wpdb->get_var( "SELECT a FROM {$wpdb->prefix}test_null_column" ) );
     1803
     1804                $this->assertEquals( 1, $wpdb->update( "{$wpdb->prefix}test_null_column", array( 'a' => null ), array( 'a' => 2 ) ) );
     1805                $this->assertEquals( array( null ), $wpdb->get_col( "SELECT a FROM {$wpdb->prefix}test_null_column" ) );
     1806
     1807                $wpdb->query( "DROP TABLE {$wpdb->prefix}test_null_column" );
     1808        }
     1809
     1810        /**
     1811         * @ticket 43589
     1812         */
     1813        public function test_insert_null_value_on_not_null_column() {
     1814                global $wpdb;
     1815
     1816                $wpdb->query( "CREATE TABLE {$wpdb->prefix}test_not_null_column( a INT NOT NULL );" );
     1817
     1818                $wpdb->suppress_errors( true );
     1819
     1820                // Column 'a' cannot be null for query
     1821                $wpdb->insert( "{$wpdb->prefix}test_not_null_column", array( 'a' => null ) );
     1822                $this->assertNotEmpty( $wpdb->last_error );
     1823
     1824                $wpdb->suppress_errors( false );
     1825
     1826                $this->assertEmpty( $wpdb->get_col( "SELECT a FROM {$wpdb->prefix}test_not_null_column" ) );
     1827
     1828                $wpdb->query( "DROP TABLE {$wpdb->prefix}test_not_null_column" );
     1829        }
     1830
     1831        /**
     1832         * @ticket 43589
     1833         */
     1834        public function test_update_null_value_on_not_null_column() {
     1835                global $wpdb;
     1836
     1837                $wpdb->query( "CREATE TABLE {$wpdb->prefix}test_not_null_column( a INT NOT NULL );" );
     1838                $wpdb->insert( "{$wpdb->prefix}test_not_null_column", array( 'a' => 1 ) );
     1839
     1840                $this->assertEquals( 1, $wpdb->update( "{$wpdb->prefix}test_not_null_column", array( 'a' => null ), array( 'a' => 1 ) ) );
     1841                $this->assertNotEmpty( $wpdb->get_results( 'SHOW WARNINGS' ) );
     1842
     1843                $this->assertEquals( 0, $wpdb->get_var( "SELECT a FROM {$wpdb->prefix}test_not_null_column" ) );
     1844
     1845                $wpdb->query( "DROP TABLE {$wpdb->prefix}test_not_null_column" );
     1846        }
     1847
     1848        /**
     1849         * @ticket 43589
     1850         */
     1851        public function test_update_null_value_on_not_null_column_strict() {
     1852                global $wpdb;
     1853
     1854                $wpdb->query( "CREATE TABLE {$wpdb->prefix}test_not_null_column( a INT NOT NULL );" );
     1855                $wpdb->insert( "{$wpdb->prefix}test_not_null_column", array( 'a' => 1 ) );
     1856
     1857                $mode = $wpdb->get_var( "SELECT @@sql_mode" );
     1858                $wpdb->query( "SET sql_mode = 'STRICT_ALL_TABLES'" );
     1859
     1860                $wpdb->suppress_errors( true );
     1861
     1862                $this->assertEquals( 0, $wpdb->update( "{$wpdb->prefix}test_not_null_column", array( 'a' => null ), array( 'a' => 1 ) ) );
     1863                $this->assertNotEmpty( $wpdb->last_error );
     1864
     1865                $wpdb->suppress_errors( false );
     1866
     1867                $this->assertEquals( 1, $wpdb->get_var( "SELECT a FROM {$wpdb->prefix}test_not_null_column" ) );
     1868
     1869                $wpdb->query( $wpdb->prepare( "SET sql_mode = %s", $mode ) );
     1870                $wpdb->query( "DROP TABLE {$wpdb->prefix}test_not_null_column" );
     1871        }
    17741872}