Ticket #43578: 43578.diff
File 43578.diff, 5.4 KB (added by , 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..4138e91 100644
class wpdb { 2321 2321 * and formats. Returns false for invalid values. 2322 2322 */ 2323 2323 protected function process_fields( $table, $data, $format ) { 2324 $data = $this->process_field_formats( $data, $format );2324 $data = $this->process_field_formats( $data, $format, $table ); 2325 2325 if ( false === $data ) { 2326 2326 return false; 2327 2327 } … … class wpdb { 2349 2349 * Prepares arrays of value/format pairs as passed to wpdb CRUD methods. 2350 2350 * 2351 2351 * @since 4.2.0 2352 * @since 5.0 Added optional $table hint (#43578) 2352 2353 * 2353 * @param array $data Array of fields to values. 2354 * @param mixed $format Formats to be mapped to the values in $data. 2354 * @param array $data Array of fields to values. 2355 * @param mixed $format Formats to be mapped to the values in $data. 2356 * @param string $table The table being processed. 2355 2357 * @return array Array, keyed by field names with values being an array 2356 2358 * of 'value' and 'format' keys. 2357 2359 */ 2358 protected function process_field_formats( $data, $format ) { 2360 protected function process_field_formats( $data, $format /**, $table */ ) { 2361 static $built_in_tables; 2362 static $built_in_field_types; 2363 2364 // Record the built-in tables and field_types as early as possible. 2365 if ( is_null( $built_in_tables ) ) { 2366 $built_in_field_types = array_keys( $this->field_types ); 2367 $built_in_tables = $this->tables(); 2368 } 2369 2359 2370 $formats = $original_formats = (array) $format; 2371 $table = null; 2372 2373 /** 2374 * The $table argument is supplied. 2375 * 2376 * Classes extending wpdb can override process_field_formats, 2377 * so we are unable to introduce a new parameter into the signature. 2378 * We have to use a "ghost" parameter in order to prevent the triggering of 2379 * "Declaration of ... should be compatible with ..." warnings in subclasses. 2380 */ 2381 if ( func_num_args() == 3 ) { 2382 $table = func_get_arg( 2 ); 2383 } 2384 2385 if ( is_null( $table ) ) { 2386 $method = get_class( $this ) . '::process_field_formats'; 2387 /* translators: %s: database access abstraction class, usually wpdb or a class extending wpdb */ 2388 _doing_it_wrong( $method, sprintf( __( '%s expects $table argument.' ), $method ), '5.0' ); 2389 } 2360 2390 2361 2391 foreach ( $data as $field => $value ) { 2362 2392 $value = array( … … class wpdb { 2370 2400 $value['format'] = reset( $original_formats ); 2371 2401 } 2372 2402 } elseif ( isset( $this->field_types[ $field ] ) ) { 2373 $value['format'] = $this->field_types[ $field ]; 2403 // Table is unspecified or built-in, use default format logic. 2404 if ( ! $table || in_array( $table, $built_in_tables ) ) { 2405 $value['format'] = $this->field_types[ $field ]; 2406 2407 // Custom table with custom field defined 2408 } else if ( ! in_array( $field, $built_in_field_types ) ) { 2409 $value['format'] = $this->field_types[ $field ]; 2410 } 2374 2411 } 2375 2412 2376 2413 $data[ $field ] = $value; -
tests/phpunit/tests/db.php
diff --git tests/phpunit/tests/db.php tests/phpunit/tests/db.php index 5cfd3d4..c2efe8c 100644
class Tests_DB extends WP_UnitTestCase { 933 933 934 934 /** 935 935 * @dataProvider data_process_field_formats 936 * @expectedIncorrectUsage wpdb_exposed_methods_for_testing::process_field_formats 936 937 * @ticket 21212 937 938 */ 938 939 function test_process_field_formats( $data, $format, $expected, $message ) { … … class Tests_DB extends WP_UnitTestCase { 1771 1772 ), 1772 1773 ); 1773 1774 } 1775 1776 /** 1777 * @ticket 43578 1778 * @dataProvider data_process_custom_table_field_formats 1779 */ 1780 public function test_process_custom_table_field_formats( $data, $format, $table, $expected, $incorrect_usage = false ) { 1781 self::$_wpdb->field_types['custom_field'] = '%d'; 1782 1783 if ( $incorrect_usage ) { 1784 $this->setExpectedIncorrectUsage( 'wpdb_exposed_methods_for_testing::process_field_formats' ); 1785 } 1786 $actual = self::$_wpdb->process_field_formats( $data, $format, $table ); 1787 $this->assertSame( $expected, $actual ); 1788 } 1789 1790 /** 1791 * @ticket 43578 1792 */ 1793 public function data_process_custom_table_field_formats() { 1794 global $wpdb; 1795 1796 return array( 1797 // Specified core table, use field_formats. 1798 array( array( 'user_id' => '1' ), array(), $wpdb->usermeta, 1799 array( 'user_id' => array( 'value' => '1', 'format' => '%d' ) ) ), 1800 1801 // Custom table, use %s default. 1802 array( array( 'user_id' => '1' ), array(), 'custom_test_table', 1803 array( 'user_id' => array( 'value' => '1', 'format' => '%s' ) ) ), 1804 1805 // Custom table with specified format. 1806 array( array( 'user_id' => '1' ), array( '%d' ), 'custom_test_table', 1807 array( 'user_id' => array( 'value' => '1', 'format' => '%d' ) ) ), 1808 1809 // Custom table with custom field format. 1810 array( array( 'custom_field' => '1' ), array(), 'custom_test_table', 1811 array( 'custom_field' => array( 'value' => '1', 'format' => '%d' ) ) ), 1812 1813 // Custom table with custom field format, no table specified. 1814 array( array( 'custom_field' => '1' ), array(), null, 1815 array( 'custom_field' => array( 'value' => '1', 'format' => '%d' ) ), true ), 1816 1817 // Incorrect usage, no table specified. 1818 array( array( 'user_id' => '1' ), array( '%d' ), null, 1819 array( 'user_id' => array( 'value' => '1', 'format' => '%d' ) ), true ), 1820 1821 // Incorrect usage, no table specified. 1822 array( array( 'not_user_id' => '1' ), array(), null, 1823 array( 'not_user_id' => array( 'value' => '1', 'format' => '%s' ) ), true ), 1824 ); 1825 } 1774 1826 }