reverted: --- b/core/modules/file/src/Plugin/Field/FieldType/FileItemMaxFileSizeValidator.php +++ /dev/null @@ -1,131 +0,0 @@ - $element['#title']]); - $form_state->setError($element, $translatable_markup); - } - } - - /** - * Validate the given string looks like a byte size string. - * - * @return bool - */ - private static function sizeIsValidByteString() { - return ( - self::sizeIsNotAlphaNumeric() || - self::sizeNotStartWithNumber() || - self::sizeHasNumbersFollowingAlphaChar() - ); - } - - /** - * Strips spaces from a string of text. - * - * @param string $string - * A string representing a size (5mb, 500GB etc) - * - * @return string - * Returns the inputted string with spaces removed. - */ - private static function stripSpacesFromString($string) { - return trim(preg_replace('/\s+/', '', $string)); - } - - /** - * Determines that a string does not start with a number. - * - * @return bool - * TRUE if the string does not start with a numeric char, FALSE otherwise. - */ - private static function sizeNotStartWithNumber() { - return !preg_match('/^\d/', self::$size); - } - - /** - * Determines if a string has numberic chars following after alpha chars. - * - * @return bool - * TRUE if the string has numeric values following after alpha chars. - */ - private static function sizeHasNumbersFollowingAlphaChar() { - return (bool) preg_match('/[a-zA-Z]+(\d+)/', self::$size); - } - - /** - * Determines if a string contains non alphanumeric characters. - * - * @return bool - * Returns TRUE if non alphanumeric characters are found. - */ - private static function sizeIsNotAlphaNumeric() { - return (bool) !preg_match('/^[a-zA-Z0-9]+$/', self::$size); - } - - /** - * Determines if Bytes:toInt() is able to parse the given string. - * - * @return bool - * 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 $valid; - } - -} reverted: --- b/core/modules/file/tests/src/Unit/FileItemFieldSettingsFormTest.php +++ /dev/null @@ -1,92 +0,0 @@ - 'Maximum upload size', - '#value' => $input, - ]; - - $form_state = $this->prophesize(FormState::class); - if ($should_error) { - $form_state->setError($element, Argument::type(TranslatableMarkup::class)) - ->shouldBeCalled(); - } - else { - $form_state->setError($element, Argument::type(TranslatableMarkup::class)) - ->shouldNotBeCalled(); - } - FileItemMaxFileSizeValidator::validateMaxFilesize($element, $form_state->reveal()); - - } - - /** - * Data provider for testValidateMaxfilesize(). - * - * @return array[] - * Array of inputs and error expectations. - */ - public function maxFileSizeInputsProvider() { - return [ - ['0 bytes', FALSE], - ['123456.03 ', TRUE], - [' ', FALSE], - ['', FALSE], - ['0 K', FALSE], - ['100bytes', FALSE], - ['10000', FALSE], - ['100bytes', FALSE], - ['100 bytes', FALSE], - ['999989 bytes', FALSE], - ['999989bytes', FALSE], - ['2', FALSE], - ['3K', FALSE], - ['5MB', FALSE], - ['10G', FALSE], - ['6GiB', FALSE], - ['8bytes', FALSE], - ['9mbytes', FALSE], - // Should we allow these spaces? - ['1 0 0 0 0 m b y tes', FALSE], - ['nonumbers', TRUE], - ['bread', TRUE], - ['bananas', TRUE], - ['1234b1', TRUE], - ['543xd1', TRUE], - ['-1', TRUE], - ['-975', TRUE], - [' word ', TRUE], - [' word', TRUE], - ['word ', TRUE], - [' words with spaces ', TRUE], - ['words with spaces ', TRUE], - [' words with spaces', TRUE], - [' 123456 ', FALSE], - [' 123456.03 ', TRUE], - ['123456.03 ', TRUE], - [' 123456.03', TRUE], - [' some words & stuff', TRUE], - ]; - } - -} only in patch2: unchanged: --- a/core/modules/file/src/Plugin/Field/FieldType/FileItemMaxFileSizeValidator.php +++ b/core/modules/file/src/Plugin/Field/FieldType/FileItemMaxFileSizeValidator.php @@ -37,7 +37,7 @@ public static function validateMaxFilesize(array $element, FormStateInterface $f // Spaces are irrelevant to us. Bytes::toInt will process with or without // them. Make our lives easier by just doing away with them. - self::$size = self::stripSpacesFromString($element['#value']); + self::$size = self::stripWhitespaceFromString($element['#value']); // Having removed whitespace, if our string is now empty then the user // either entered nothing or a string of whitespace. Accept both cases as @@ -77,7 +77,7 @@ private static function sizeIsValidByteString() { * @return string * Returns the inputted string with spaces removed. */ - private static function stripSpacesFromString($string) { + private static function stripWhitespaceFromString($string) { return trim(preg_replace('/\s+/', '', $string)); }