From 02f1d9fd95ab79ad84428b7d4e9658a054bba334 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Mon, 5 Aug 2024 10:28:33 +0200 Subject: [PATCH 1/3] fix handling empty data in ValueToDuplicatesTransformer The transformer receives the data of child forms that have already been through the transformation schema. If no custom view transformer was used on that child form the empty would already have been changed to null. Thus, receiving an empty string here means that the child form explicitly asked for it and the value must not be exchanged with null. --- .../ValueToDuplicatesTransformer.php | 2 +- .../ValueToDuplicatesTransformerTest.php | 2 +- .../Extension/Core/Type/RepeatedTypeTest.php | 31 +++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/Extension/Core/DataTransformer/ValueToDuplicatesTransformer.php b/Extension/Core/DataTransformer/ValueToDuplicatesTransformer.php index 5249e3b364..8dd5acb616 100644 --- a/Extension/Core/DataTransformer/ValueToDuplicatesTransformer.php +++ b/Extension/Core/DataTransformer/ValueToDuplicatesTransformer.php @@ -62,7 +62,7 @@ public function reverseTransform($array) $emptyKeys = []; foreach ($this->keys as $key) { - if (isset($array[$key]) && '' !== $array[$key] && false !== $array[$key] && [] !== $array[$key]) { + if (isset($array[$key]) && false !== $array[$key] && [] !== $array[$key]) { if ($array[$key] !== $result) { throw new TransformationFailedException('All values in the array should be the same.'); } diff --git a/Tests/Extension/Core/DataTransformer/ValueToDuplicatesTransformerTest.php b/Tests/Extension/Core/DataTransformer/ValueToDuplicatesTransformerTest.php index fdfd983576..462472da98 100644 --- a/Tests/Extension/Core/DataTransformer/ValueToDuplicatesTransformerTest.php +++ b/Tests/Extension/Core/DataTransformer/ValueToDuplicatesTransformerTest.php @@ -70,7 +70,7 @@ public function testReverseTransformCompletelyEmpty() 'c' => '', ]; - $this->assertNull($this->transformer->reverseTransform($input)); + $this->assertSame('', $this->transformer->reverseTransform($input)); } public function testReverseTransformCompletelyNull() diff --git a/Tests/Extension/Core/Type/RepeatedTypeTest.php b/Tests/Extension/Core/Type/RepeatedTypeTest.php index b2a295b276..ca0de12233 100644 --- a/Tests/Extension/Core/Type/RepeatedTypeTest.php +++ b/Tests/Extension/Core/Type/RepeatedTypeTest.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Form\Tests\Extension\Core\Type; +use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Form; use Symfony\Component\Form\Tests\Fixtures\NotMappedType; use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; @@ -191,6 +192,36 @@ public function testSetOptionsPerChildAndOverwrite() $this->assertTrue($form['second']->isRequired()); } + /** + * @dataProvider emptyDataProvider + */ + public function testSubmitNullForTextTypeWithEmptyDataOptionSetToEmptyString($emptyData, $submittedData, $expected) + { + $form = $this->factory->create(static::TESTED_TYPE, null, [ + 'type' => TextType::class, + 'options' => [ + 'empty_data' => $emptyData, + ] + ]); + $form->submit($submittedData); + + $this->assertSame($expected, $form->getData()); + } + + public static function emptyDataProvider() + { + yield ['', null, '']; + yield ['', ['first' => null, 'second' => null], '']; + yield ['', ['first' => '', 'second' => null], '']; + yield ['', ['first' => null, 'second' => ''], '']; + yield ['', ['first' => '', 'second' => ''], '']; + yield [null, null, null]; + yield [null, ['first' => null, 'second' => null], null]; + yield [null, ['first' => '', 'second' => null], null]; + yield [null, ['first' => null, 'second' => ''], null]; + yield [null, ['first' => '', 'second' => ''], null]; + } + public function testSubmitUnequal() { $input = ['first' => 'foo', 'second' => 'bar']; From dc6e7486029b86f5023ea81476fc5419b43366c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sun, 28 Jul 2024 21:52:06 +0200 Subject: [PATCH 2/3] [Form] NumberType: Fix parsing of numbers in exponential notation with negative exponent --- .../NumberToLocalizedStringTransformer.php | 3 ++- ...NumberToLocalizedStringTransformerTest.php | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php b/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php index f06fd80a11..d407e88586 100644 --- a/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php +++ b/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php @@ -143,7 +143,8 @@ public function reverseTransform($value) $value = str_replace(',', $decSep, $value); } - if (str_contains($value, $decSep)) { + //If the value is in exponential notation with a negative exponent, we end up with a float value too + if (str_contains($value, $decSep) || false !== stripos($value, 'e-')) { $type = \NumberFormatter::TYPE_DOUBLE; } else { $type = \PHP_INT_SIZE === 8 diff --git a/Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php b/Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php index 9c2e3bcae3..47b97ce530 100644 --- a/Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php +++ b/Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php @@ -632,4 +632,31 @@ public function testReverseTransformSmallInt() $this->assertSame(1.0, $transformer->reverseTransform('1')); } + + /** + * @dataProvider eNotationProvider + */ + public function testReverseTransformENotation($output, $input) + { + \Locale::setDefault('en'); + + $transformer = new NumberToLocalizedStringTransformer(); + + $this->assertSame($output, $transformer->reverseTransform($input)); + } + + public static function eNotationProvider(): array + { + return [ + [0.001, '1E-3'], + [0.001, '1.0E-3'], + [0.001, '1e-3'], + [0.001, '1.0e-03'], + [1000.0, '1E3'], + [1000.0, '1.0E3'], + [1000.0, '1e3'], + [1000.0, '1.0e3'], + [1232.0, '1.232e3'], + ]; + } } From 0b589850148240bb0d5647284e5cc1538f30e930 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Fri, 9 Aug 2024 16:59:37 +0200 Subject: [PATCH 3/3] skip tests requiring the intl extension if it's not installed --- .../DataTransformer/NumberToLocalizedStringTransformerTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php b/Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php index 47b97ce530..f5246e2222 100644 --- a/Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php +++ b/Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php @@ -638,6 +638,8 @@ public function testReverseTransformSmallInt() */ public function testReverseTransformENotation($output, $input) { + IntlTestHelper::requireFullIntl($this); + \Locale::setDefault('en'); $transformer = new NumberToLocalizedStringTransformer();