diff --git a/core/modules/file/src/Plugin/Field/FieldType/FileItemMaxFileSizeValidator.php b/core/modules/file/src/Plugin/Field/FieldType/FileItemMaxFileSizeValidator.php index 6cb2fdb52d..713cf743af 100644 --- a/core/modules/file/src/Plugin/Field/FieldType/FileItemMaxFileSizeValidator.php +++ b/core/modules/file/src/Plugin/Field/FieldType/FileItemMaxFileSizeValidator.php @@ -57,9 +57,10 @@ class FileItemMaxFileSizeValidator { } /** - * Validate the given string looks like a byte size string. + * Validates the given string looks like a byte size string. * * @return bool + * TRUE if size is a valid byte size string, FALSE if not. */ private static function sizeIsValidByteString() { return ( @@ -70,7 +71,7 @@ class FileItemMaxFileSizeValidator { } /** - * Strips spaces from a string of text. + * Strips whitespace from a string of text. * * @param string $string * A string representing a size (5mb, 500GB etc) @@ -119,14 +120,13 @@ class FileItemMaxFileSizeValidator { * TRUE if string can be parsed by Bytes::toInt(), FALSE otherwise. */ private static function toIntCanParseString() { - $valid = TRUE; try { Bytes::toInt(self::$size); } catch (\Exception $e) { - $valid = FALSE; + return FALSE; } - return $valid; + return TRUE; } } diff --git a/core/modules/file/tests/src/Unit/FileItemFieldSettingsFormTest.php b/core/modules/file/tests/src/Unit/FileItemFieldSettingsFormTest.php index d8f7696a2a..65de0d88dd 100644 --- a/core/modules/file/tests/src/Unit/FileItemFieldSettingsFormTest.php +++ b/core/modules/file/tests/src/Unit/FileItemFieldSettingsFormTest.php @@ -47,7 +47,7 @@ class FileItemFieldSettingsFormTest extends UnitTestCase { * Array of inputs and error expectations. */ public function maxFileSizeInputsProvider() { - return [ + $data = [ ['0 bytes', FALSE], ['123456.03 ', TRUE], [' ', FALSE], @@ -55,7 +55,6 @@ class FileItemFieldSettingsFormTest extends UnitTestCase { ['0 K', FALSE], ['100bytes', FALSE], ['10000', FALSE], - ['100bytes', FALSE], ['100 bytes', FALSE], ['999989 bytes', FALSE], ['999989bytes', FALSE], @@ -87,6 +86,17 @@ class FileItemFieldSettingsFormTest extends UnitTestCase { [' 123456.03', TRUE], [' some words & stuff', TRUE], ]; + + // Improve test output by using the input value as a label for the dataset. + foreach ($data as $index => $item) { + // If the index looks like an in eg "1000" "2" etc, php still detects + // it as a numeric index. Adding text to all the labels to force them + // to render. + $data["Sample input: '{$item[0]}'"] = $item; + unset($data[$index]); + } + + return $data; } }