From c06cf4820e02a923a683419b03979bd3677f38db Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Sat, 20 Oct 2018 17:41:41 +0200 Subject: [PATCH] BUG Readonly and disabled CurrencyFields no longer always returns dollar currency sign, now respect config --- src/Forms/CurrencyField_Disabled.php | 6 ++- src/Forms/CurrencyField_Readonly.php | 8 +-- .../php/Forms/CurrencyField_DisabledTest.php | 34 ++++++++++++ .../php/Forms/CurrencyField_ReadonlyTest.php | 53 +++++++++++++++++++ 4 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 tests/php/Forms/CurrencyField_DisabledTest.php create mode 100644 tests/php/Forms/CurrencyField_ReadonlyTest.php diff --git a/src/Forms/CurrencyField_Disabled.php b/src/Forms/CurrencyField_Disabled.php index 7e743755e..68145d41f 100644 --- a/src/Forms/CurrencyField_Disabled.php +++ b/src/Forms/CurrencyField_Disabled.php @@ -3,6 +3,7 @@ namespace SilverStripe\Forms; use SilverStripe\Core\Convert; +use SilverStripe\ORM\FieldType\DBCurrency; /** * Readonly version of a {@link CurrencyField}. @@ -13,7 +14,7 @@ class CurrencyField_Disabled extends CurrencyField protected $disabled = true; /** - * overloaded to display the correctly formated value for this datatype + * Overloaded to display the correctly formatted value for this data type * * @param array $properties * @return string @@ -22,7 +23,8 @@ class CurrencyField_Disabled extends CurrencyField { if ($this->value) { $val = Convert::raw2xml($this->value); - $val = _t('SilverStripe\\Forms\\CurrencyField.CURRENCYSYMBOL', '$') . number_format(preg_replace('/[^0-9.-]/', "", $val), 2); + $val = DBCurrency::config()->get('currency_symbol') + . number_format(preg_replace('/[^0-9.-]/', '', $val), 2); $valforInput = Convert::raw2att($val); } else { $valforInput = ''; diff --git a/src/Forms/CurrencyField_Readonly.php b/src/Forms/CurrencyField_Readonly.php index 775b9eb6b..292827600 100644 --- a/src/Forms/CurrencyField_Readonly.php +++ b/src/Forms/CurrencyField_Readonly.php @@ -3,6 +3,7 @@ namespace SilverStripe\Forms; use SilverStripe\Core\Convert; +use SilverStripe\ORM\FieldType\DBCurrency; /** * Readonly version of a {@link CurrencyField}. @@ -11,19 +12,20 @@ class CurrencyField_Readonly extends ReadonlyField { /** - * Overloaded to display the correctly formated value for this datatype + * Overloaded to display the correctly formatted value for this data type * * @param array $properties * @return string */ public function Field($properties = array()) { + $currencySymbol = DBCurrency::config()->get('currency_symbol'); if ($this->value) { $val = Convert::raw2xml($this->value); - $val = _t('SilverStripe\\Forms\\CurrencyField.CURRENCYSYMBOL', '$') . number_format(preg_replace('/[^0-9.-]/', "", $val), 2); + $val = $currencySymbol . number_format(preg_replace('/[^0-9.-]/', '', $val), 2); $valforInput = Convert::raw2att($val); } else { - $val = '' . _t('SilverStripe\\Forms\\CurrencyField.CURRENCYSYMBOL', '$') . '0.00'; + $val = '' . $currencySymbol . '0.00'; $valforInput = ''; } return "extraClass() . "\" id=\"" . $this->ID() . "\">$val" diff --git a/tests/php/Forms/CurrencyField_DisabledTest.php b/tests/php/Forms/CurrencyField_DisabledTest.php new file mode 100644 index 000000000..ed832521c --- /dev/null +++ b/tests/php/Forms/CurrencyField_DisabledTest.php @@ -0,0 +1,34 @@ +Field(); + + $this->assertContains('assertContains('disabled', $result, 'The input should be disabled'); + $this->assertContains('$5.00', $result, 'The value should be rendered'); + } + + /** + * @todo: Update the expectation when intl for currencies is implemented + */ + public function testFieldWithCustomisedCurrencySymbol() + { + DBCurrency::config()->update('currency_symbol', '€'); + $field = new CurrencyField_Disabled('Test', '', '€5.00'); + $result = $field->Field(); + + $this->assertContains('assertContains('disabled', $result, 'The input should be disabled'); + $this->assertContains('€5.00', $result, 'The value should be rendered'); + } +} diff --git a/tests/php/Forms/CurrencyField_ReadonlyTest.php b/tests/php/Forms/CurrencyField_ReadonlyTest.php new file mode 100644 index 000000000..289327cd1 --- /dev/null +++ b/tests/php/Forms/CurrencyField_ReadonlyTest.php @@ -0,0 +1,53 @@ +performReadonlyTransformation(); + $this->assertInstanceOf(CurrencyField_Readonly::class, $result); + $this->assertNotSame($result, $field, 'Should return a clone of the field'); + } + + public function testFieldWithValue() + { + $field = new CurrencyField_Readonly('Test', '', '$5.00'); + $result = $field->Field(); + + $this->assertContains('assertContains('readonly', $result, 'The input should be readonly'); + $this->assertContains('$5.00', $result, 'The value should be rendered'); + } + + public function testFieldWithOutValue() + { + DBCurrency::config()->update('currency_symbol', 'AUD'); + $field = new CurrencyField_Readonly('Test', '', null); + $result = $field->Field(); + + $this->assertContains('assertContains('readonly', $result, 'The input should be readonly'); + $this->assertContains('AUD0.00', $result, 'The value should be rendered'); + } + + /** + * @todo: Update the expectation when intl for currencies is implemented + */ + public function testFieldWithCustomisedCurrencySymbol() + { + DBCurrency::config()->update('currency_symbol', '€'); + $field = new CurrencyField_Readonly('Test', '', '€5.00'); + $result = $field->Field(); + + $this->assertContains('assertContains('readonly', $result, 'The input should be readonly'); + $this->assertContains('€5.00', $result, 'The value should be rendered'); + } +}