[BUGFIX] EXT:form - avoid number reduction in currency filter 91/43491/3
authorRalf Zimmermann <ralf.zimmermann@tritum.de>
Wed, 23 Sep 2015 07:29:21 +0000 (09:29 +0200)
committerMorton Jonuschat <m.jonuschat@mojocode.de>
Fri, 13 Nov 2015 12:20:01 +0000 (13:20 +0100)
Avoid a number reduction each time the filter is applied.
Preserve the decimal places.

Resolves: #69807
Releases: 6.2
Change-Id: I3d7587ecf65447439bd8b58819805263aa5fa7f8
Reviewed-on: https://review.typo3.org/43491
Reviewed-by: Richard Haeser <richardhaeser@gmail.com>
Reviewed-by: Bjoern Jacob <bjoern.jacob@tritum.de>
Tested-by: Bjoern Jacob <bjoern.jacob@tritum.de>
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Tested-by: Morton Jonuschat <m.jonuschat@mojocode.de>
typo3/sysext/form/Classes/Filter/CurrencyFilter.php
typo3/sysext/form/Tests/Unit/Filter/CurrencyFilterTest.php

index 9858318..65779cc 100644 (file)
@@ -89,7 +89,21 @@ class CurrencyFilter implements \TYPO3\CMS\Form\Filter\FilterInterface {
         * @return string
         */
        public function filter($value) {
-               $value = (double) ((string) $value);
+               $value = str_replace(
+                       array(
+                               $this->thousandSeparator,
+                               $this->decimalsPoint,
+                       ),
+                       array(
+                               '',
+                               '.'
+                       ),
+                       (string)$value
+               );
+
+               // replace all non numeric characters, decimalPoint and negative sign
+               $value = preg_replace("/[^0-9.-]/", "", $value);
+               $value = (double)$value;
                return number_format($value, 2, $this->decimalsPoint, $this->thousandSeparator);
        }
 
index 9e3b4a2..96015d5 100644 (file)
@@ -30,32 +30,72 @@ class CurrencyFilterTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $this->fixture = new \TYPO3\CMS\Form\Filter\CurrencyFilter();
        }
 
+       /**
+        * Data provider for filterForVariousIntegerInputsReturnsFormattedCurrencyNotation
+        * Each data set is an array with the following elements:
+        *  - input value
+        *  - thousand separator character
+        *  - decimal point character
+        *  - expected result
+        *
+        * @returns array
+        */
        public function validDataProvider() {
                return array(
                        '1200 => 1.200,00' => array(
-                               1200, // input
-                               '.', // thousand separator
-                               ',', // decimal point
-                               '1.200,00' // expected
+                               '1200',
+                               '.',
+                               ',',
+                               '1.200,00'
                        ),
                        '0 => 0,00' => array(
-                               0,
+                               '0',
                                NULL,
                                ',',
                                '0,00'
                        ),
                        '3333.33 => 3,333.33' => array(
-                               3333.33,
+                               '3333.33',
                                ',',
                                '.',
                                '3,333.33'
                        ),
                        '1099.33 => 1 099,33' => array(
-                               1099.33,
+                               '1099.33',
                                ' ',
                                ',',
                                '1 099,33'
                        ),
+                       '1200,00 => 1.200,00' => array(
+                                       '1200,00',
+                                       '.',
+                                       ',',
+                                       '1.200,00'
+                       ),
+                       '1.200,00 => 1.200,00' => array(
+                                       '1.200,00',
+                                       '.',
+                                       ',',
+                                       '1.200,00'
+                       ),
+                       '1.200 => 1.200,00' => array(
+                                       '1.200',
+                                       '.',
+                                       ',',
+                                       '1.200,00'
+                       ),
+                       '-1 => -1,00' => array(
+                                       '-1',
+                                       '.',
+                                       ',',
+                                       '-1,00'
+                       ),
+                       '1.200 => 1.200,00' => array(
+                                       '1.200',
+                                       '.',
+                                       ',',
+                                       '1.200,00'
+                       ),
                );
        }