bug #25781 [Form] Disallow transform dates beyond the year 9999 (curry684)

This PR was squashed before being merged into the 2.7 branch (closes #25781).

Discussion
----------

[Form] Disallow transform dates beyond the year 9999

Fixes #14727

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | not really
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #14727
| License       | MIT

Explicitly locked out submission of dates beyond December 31st 9999 in forms as PHP is highly incapable of consistently handling such dates. Before this patch dates were randomly transformed or mangled.

Technically there is a BC break as this will now cause validation to fail on input that was *accepted* before, but it was mangled. Not my call but I prefer the rejection over data corruption:
```
// Old behavior
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', null, null, \IntlDateFormatter::GREGORIAN, 'yyyy-MM-dd');
$result = $transformer->reverseTransform('20107-03-21');

// $result is now 2007-03-21
```

Commits
-------

70cc969537 [Form] Disallow transform dates beyond the year 9999
This commit is contained in:
Fabien Potencier 2018-01-17 09:13:32 +01:00
commit e6e235b6f4
2 changed files with 21 additions and 0 deletions

View File

@ -123,6 +123,9 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
if (0 != intl_get_error_code()) {
throw new TransformationFailedException(intl_get_error_message());
} elseif ($timestamp > 253402214400) {
// This timestamp represents UTC midnight of 9999-12-31 to prevent 5+ digit years
throw new TransformationFailedException('Years beyond 9999 are not supported.');
}
try {

View File

@ -343,4 +343,22 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC');
$transformer->reverseTransform('1789-07-14');
}
/**
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testReverseTransformFiveDigitYears()
{
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', null, null, \IntlDateFormatter::GREGORIAN, 'yyyy-MM-dd');
$transformer->reverseTransform('20107-03-21');
}
/**
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testReverseTransformFiveDigitYearsWithTimestamp()
{
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', null, null, \IntlDateFormatter::GREGORIAN, 'yyyy-MM-dd HH:mm:ss');
$transformer->reverseTransform('20107-03-21 12:34:56');
}
}