Ticket #39053: 39053.diff
File 39053.diff, 4.2 KB (added by , 4 years ago) |
---|
-
src/wp-includes/functions.php
diff --git src/wp-includes/functions.php src/wp-includes/functions.php index 8593e8c..a905494 100644
function wp_validate_boolean( $var ) { 5377 5377 } 5378 5378 5379 5379 /** 5380 * Filter/validate a variable as an integer. 5381 * 5382 * Alternative to `filter_var( $var, FILTER_VALIDATE_INT, $options )`. 5383 * 5384 * PHP can be compiled without fiter_var so WordPress avoids using it. 5385 * 5386 * @since 4.8.0 5387 * 5388 * @param mixed $var Integer value to validate. 5389 * @param array $args Optional. { 5390 * Arguments to control behavior. Default empty array. 5391 * 5392 * @type mixed $default Value to return for invalid values. Default false. 5393 * @type int|bool $min_range Lowest valid value. Default false (no minimum). 5394 * @type int|bool $max_range Highest valid value. Default false (no maximum). 5395 * } 5396 * @return mixed Integer if value is valid, $args['default'] if invalid. 5397 */ 5398 function wp_validate_integer( $var, $args = array() ) { 5399 $defaults = array( 5400 'default' => false, 5401 'min_range' => false, 5402 'max_range' => false, 5403 ); 5404 5405 $args = wp_parse_args( $args, $defaults ); 5406 5407 if ( false !== $args['min_range'] ) { 5408 $args['min_range'] = wp_validate_integer( $args['min_range'] ); 5409 } 5410 5411 if ( false !== $args['max_range'] ) { 5412 $args['max_range'] = wp_validate_integer( $args['max_range'] ); 5413 } 5414 5415 if ( ! is_numeric( $var ) || floor( $var ) != $var ) { 5416 return $args['default']; 5417 } 5418 5419 $var = (int) $var; 5420 5421 if ( false !== $args['min_range'] && $var < $args['min_range'] ) { 5422 return $args['default']; 5423 } 5424 5425 if ( false !== $args['max_range'] && $var > $args['max_range'] ) { 5426 return $args['default']; 5427 } 5428 5429 // $var is a valid integer. 5430 return $var; 5431 } 5432 5433 /** 5380 5434 * Delete a file 5381 5435 * 5382 5436 * @since 4.2.0 -
tests/phpunit/tests/functions.php
diff --git tests/phpunit/tests/functions.php tests/phpunit/tests/functions.php index 5f4d6fd..67d29fa 100644
class Tests_Functions extends WP_UnitTestCase { 898 898 $unique_uuids = array_unique( $uuids ); 899 899 $this->assertEquals( $uuids, $unique_uuids ); 900 900 } 901 902 /** 903 * Tests wp_validate_integer(). 904 * 905 * @covers wp_validate_integer() 906 * @ticket 39053 907 */ 908 function test_wp_validate_integer() { 909 // Various tests to generate a 4. 910 $expected = 4; 911 912 $actual = wp_validate_integer( 4 ); 913 $this->assertSame( $expected, $actual ); 914 915 $actual = wp_validate_integer( '4' ); 916 $this->assertSame( $expected, $actual ); 917 918 $actual = wp_validate_integer( 1 + '3' ); 919 $this->assertSame( $expected, $actual ); 920 921 $actual = wp_validate_integer( 'Six', array( 'default' => 4 ) ); 922 $this->assertSame( $expected, $actual ); 923 924 // Various tests to generate a -4. 925 $expected = -4; 926 927 $actual = wp_validate_integer( -4 ); 928 $this->assertSame( $expected, $actual ); 929 930 $actual = wp_validate_integer( '-4' ); 931 $this->assertSame( $expected, $actual ); 932 933 $actual = wp_validate_integer( 1 - '5' ); 934 $this->assertSame( $expected, $actual ); 935 936 $actual = wp_validate_integer( 'Six', array( 'default' => -4 ) ); 937 $this->assertSame( $expected, $actual ); 938 939 // Various tests for invalid data. 940 $expected = false; 941 942 $actual = wp_validate_integer( 4.2 ); 943 $this->assertSame( $expected, $actual ); 944 945 $actual = wp_validate_integer( -4.2 ); 946 $this->assertSame( $expected, $actual ); 947 948 $actual = wp_validate_integer( '4.2' ); 949 $this->assertSame( $expected, $actual ); 950 951 $actual = wp_validate_integer( '-4.2' ); 952 $this->assertSame( $expected, $actual ); 953 954 $actual = wp_validate_integer( array( 'no_args' => 'here' ) ); 955 $this->assertSame( $expected, $actual ); 956 957 $actual = wp_validate_integer( -200, array( 958 'min_range' => 1, 959 ) ); 960 $this->assertSame( $expected, $actual ); 961 962 $actual = wp_validate_integer( 200, array( 963 'max_range' => 1, 964 ) ); 965 $this->assertSame( $expected, $actual ); 966 967 // Odd defaults 968 $actual = wp_validate_integer( 'WCUS Contributor Day', array( 969 'default' => array(), 970 ) ); 971 $this->assertSame( array(), $actual ); 972 973 $actual = wp_validate_integer( 'WCUS Contributor Day', array( 974 'default' => 'Twenty Sixteen', 975 ) ); 976 $this->assertSame( 'Twenty Sixteen', $actual ); 977 } 901 978 }