Make WordPress Core

Ticket #15158: 15158.2.diff

File 15158.2.diff, 4.1 KB (added by pento, 11 years ago)
  • src/wp-includes/wp-db.php

     
    18481848
    18491849                $formats = $values = array();
    18501850                foreach ( $data as $value ) {
     1851                        if ( is_null( $value['value'] ) ) {
     1852                                $formats[] = 'NULL';
     1853                                continue;
     1854                        }
     1855
    18511856                        $formats[] = $value['format'];
    18521857                        $values[]  = $value['value'];
    18531858                }
    … …  
    19041909
    19051910                $fields = $conditions = $values = array();
    19061911                foreach ( $data as $field => $value ) {
     1912                        if ( is_null( $value['value'] ) ) {
     1913                                $fields[] = "`$field` = NULL";
     1914                                continue;
     1915                        }
     1916
    19071917                        $fields[] = "`$field` = " . $value['format'];
    19081918                        $values[] = $value['value'];
    19091919                }
    19101920                foreach ( $where as $field => $value ) {
     1921                        if ( is_null( $value['value'] ) ) {
     1922                                $conditions[] = "`$field` IS NULL";
     1923                                continue;
     1924                        }
     1925
    19111926                        $conditions[] = "`$field` = " . $value['format'];
    19121927                        $values[] = $value['value'];
    19131928                }
    … …  
    19541969
    19551970                $conditions = $values = array();
    19561971                foreach ( $where as $field => $value ) {
     1972                        if ( is_null( $value['value'] ) ) {
     1973                                $conditions[] = "`$field` IS NULL";
     1974                                continue;
     1975                        }
     1976
    19571977                        $conditions[] = "`$field` = " . $value['format'];
    19581978                        $values[] = $value['value'];
    19591979                }
  • tests/phpunit/tests/db.php

     
    807807        function filter_pre_get_col_charset( $charset, $table, $column ) {
    808808                return 'fake_col_charset';
    809809        }
     810
     811        /**
     812         * @ticket 15158
     813         */
     814        function test_null_insert() {
     815                global $wpdb;
     816
     817                $key = 'null_insert_key';
     818
     819                $wpdb->insert(
     820                        $wpdb->postmeta,
     821                        array(
     822                                'meta_key' => $key,
     823                                'meta_value' => NULL
     824                        ),
     825                        array( '%s', '%s' )
     826                );
     827
     828                $row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" );
     829
     830                $this->assertNull( $row->meta_value );
     831        }
     832
     833        /**
     834         * @ticket 15158
     835         */
     836        function test_null_update_value() {
     837                global $wpdb;
     838
     839                $key = 'null_update_value_key';
     840                $value = 'null_update_value_key';
     841
     842                $wpdb->insert(
     843                        $wpdb->postmeta,
     844                        array(
     845                                'meta_key' => $key,
     846                                'meta_value' => $value
     847                        ),
     848                        array( '%s', '%s' )
     849                );
     850
     851                $row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" );
     852
     853                $this->assertSame( $value, $row->meta_value );
     854
     855                $wpdb->update(
     856                        $wpdb->postmeta,
     857                        array( 'meta_value' => NULL ),
     858                        array(
     859                                'meta_key' => $key,
     860                                'meta_value' => $value
     861                        ),
     862                        array( '%s' ),
     863                        array( '%s', '%s' )
     864                );
     865
     866                $row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" );
     867
     868                $this->assertNull( $row->meta_value );
     869        }
     870
     871        /**
     872         * @ticket 15158
     873         */
     874        function test_null_update_where() {
     875                global $wpdb;
     876
     877                $key = 'null_update_where_key';
     878                $value = 'null_update_where_key';
     879
     880                $wpdb->insert(
     881                        $wpdb->postmeta,
     882                        array(
     883                                'meta_key' => $key,
     884                                'meta_value' => NULL
     885                        ),
     886                        array( '%s', '%s' )
     887                );
     888
     889                $row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" );
     890
     891                $this->assertNull( $row->meta_value );
     892
     893                $wpdb->update(
     894                        $wpdb->postmeta,
     895                        array( 'meta_value' => $value ),
     896                        array(
     897                                'meta_key' => $key,
     898                                'meta_value' => NULL
     899                        ),
     900                        array( '%s' ),
     901                        array( '%s', '%s' )
     902                );
     903
     904                $row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" );
     905
     906                $this->assertSame( $value, $row->meta_value );
     907        }
     908
     909        /**
     910         * @ticket 15158
     911         */
     912        function test_null_delete() {
     913                global $wpdb;
     914
     915                $key = 'null_update_where_key';
     916                $value = 'null_update_where_key';
     917
     918                $wpdb->insert(
     919                        $wpdb->postmeta,
     920                        array(
     921                                'meta_key' => $key,
     922                                'meta_value' => NULL
     923                        ),
     924                        array( '%s', '%s' )
     925                );
     926
     927                $row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" );
     928
     929                $this->assertNull( $row->meta_value );
     930
     931                $wpdb->delete(
     932                        $wpdb->postmeta,
     933                        array(
     934                                'meta_key' => $key,
     935                                'meta_value' => NULL
     936                        ),
     937                        array( '%s', '%s' )
     938                );
     939
     940                $row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" );
     941
     942                $this->assertNull( $row );
     943        }
    810944}
    811