bug #29185 [Form] Fixed keeping hash of equal \DateTimeInterface on submit (HeahDude)

This PR was merged into the 2.8 branch.

Discussion
----------

[Form] Fixed keeping hash of equal \DateTimeInterface on submit

| Q             | A
| ------------- | ---
| Branch?       | 2.8
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | ~
| License       | MIT
| Doc PR        | ~

Commits
-------

bc2e2cb5ad [Form] Fixed keeping hash of equal \DateTimeInterface on submit
This commit is contained in:
Nicolas Grekas 2018-11-13 17:38:43 +01:00
commit 32c01724cd
2 changed files with 45 additions and 4 deletions

View File

@ -73,16 +73,17 @@ class PropertyPathMapper implements DataMapperInterface
// Write-back is disabled if the form is not synchronized (transformation failed),
// if the form was not submitted and if the form is disabled (modification not allowed)
if (null !== $propertyPath && $config->getMapped() && $form->isSubmitted() && $form->isSynchronized() && !$form->isDisabled()) {
// If the field is of type DateTime and the data is the same skip the update to
$propertyValue = $form->getData();
// If the field is of type DateTime or DateTimeInterface and the data is the same skip the update to
// keep the original object hash
if ($form->getData() instanceof \DateTime && $form->getData() == $this->propertyAccessor->getValue($data, $propertyPath)) {
if (($propertyValue instanceof \DateTime || $propertyValue instanceof \DateTimeInterface) && $propertyValue == $this->propertyAccessor->getValue($data, $propertyPath)) {
continue;
}
// If the data is identical to the value in $data, we are
// dealing with a reference
if (!\is_object($data) || !$config->getByReference() || $form->getData() !== $this->propertyAccessor->getValue($data, $propertyPath)) {
$this->propertyAccessor->setValue($data, $propertyPath, $form->getData());
if (!\is_object($data) || !$config->getByReference() || $propertyValue !== $this->propertyAccessor->getValue($data, $propertyPath)) {
$this->propertyAccessor->setValue($data, $propertyPath, $propertyValue);
}
}
}

View File

@ -357,4 +357,44 @@ class PropertyPathMapperTest extends TestCase
$this->mapper->mapFormsToData(array($form), $car);
}
/**
* @dataProvider provideDate
*/
public function testMapFormsToDataDoesNotChangeEqualDateTimeInstance($date)
{
$article = array();
$publishedAt = $date;
$article['publishedAt'] = clone $publishedAt;
$propertyPath = $this->getPropertyPath('[publishedAt]');
$this->propertyAccessor->expects($this->once())
->method('getValue')
->willReturn($article['publishedAt'])
;
$this->propertyAccessor->expects($this->never())
->method('setValue')
;
$config = new FormConfigBuilder('publishedAt', \get_class($publishedAt), $this->dispatcher);
$config->setByReference(false);
$config->setPropertyPath($propertyPath);
$config->setData($publishedAt);
$form = $this->getForm($config);
$this->mapper->mapFormsToData(array($form), $article);
}
public function provideDate()
{
$data = array(
'\DateTime' => array(new \DateTime()),
);
if (class_exists('DateTimeImmutable')) {
$data['\DateTimeImmutable'] = array(new \DateTimeImmutable());
}
return $data;
}
}