BUG Readonly and disabled CurrencyFields no longer always returns dollar currency sign, now respect config

This commit is contained in:
Robbie Averill 2018-10-20 17:41:41 +02:00
parent d56bad7568
commit c06cf4820e
4 changed files with 96 additions and 5 deletions

View File

@ -3,6 +3,7 @@
namespace SilverStripe\Forms; namespace SilverStripe\Forms;
use SilverStripe\Core\Convert; use SilverStripe\Core\Convert;
use SilverStripe\ORM\FieldType\DBCurrency;
/** /**
* Readonly version of a {@link CurrencyField}. * Readonly version of a {@link CurrencyField}.
@ -13,7 +14,7 @@ class CurrencyField_Disabled extends CurrencyField
protected $disabled = true; 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 * @param array $properties
* @return string * @return string
@ -22,7 +23,8 @@ class CurrencyField_Disabled extends CurrencyField
{ {
if ($this->value) { if ($this->value) {
$val = Convert::raw2xml($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); $valforInput = Convert::raw2att($val);
} else { } else {
$valforInput = ''; $valforInput = '';

View File

@ -3,6 +3,7 @@
namespace SilverStripe\Forms; namespace SilverStripe\Forms;
use SilverStripe\Core\Convert; use SilverStripe\Core\Convert;
use SilverStripe\ORM\FieldType\DBCurrency;
/** /**
* Readonly version of a {@link CurrencyField}. * 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 * @param array $properties
* @return string * @return string
*/ */
public function Field($properties = array()) public function Field($properties = array())
{ {
$currencySymbol = DBCurrency::config()->get('currency_symbol');
if ($this->value) { if ($this->value) {
$val = Convert::raw2xml($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); $valforInput = Convert::raw2att($val);
} else { } else {
$val = '<i>' . _t('SilverStripe\\Forms\\CurrencyField.CURRENCYSYMBOL', '$') . '0.00</i>'; $val = '<i>' . $currencySymbol . '0.00</i>';
$valforInput = ''; $valforInput = '';
} }
return "<span class=\"readonly " . $this->extraClass() . "\" id=\"" . $this->ID() . "\">$val</span>" return "<span class=\"readonly " . $this->extraClass() . "\" id=\"" . $this->ID() . "\">$val</span>"

View File

@ -0,0 +1,34 @@
<?php
namespace SilverStripe\Forms\Tests;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\CurrencyField_Disabled;
use SilverStripe\ORM\FieldType\DBCurrency;
class CurrencyField_DisabledTest extends SapphireTest
{
public function testFieldWithValue()
{
$field = new CurrencyField_Disabled('Test', '', '$5.00');
$result = $field->Field();
$this->assertContains('<input', $result, 'An input should be rendered');
$this->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('<input', $result, 'An input should be rendered');
$this->assertContains('disabled', $result, 'The input should be disabled');
$this->assertContains('€5.00', $result, 'The value should be rendered');
}
}

View File

@ -0,0 +1,53 @@
<?php
namespace SilverStripe\Forms\Tests;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\CurrencyField_Readonly;
use SilverStripe\ORM\FieldType\DBCurrency;
class CurrencyField_ReadonlyTest extends SapphireTest
{
public function testPerformReadonlyTransformation()
{
$field = new CurrencyField_Readonly('Test', '', '$5.00');
$result = $field->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('<input', $result, 'An input should be rendered');
$this->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('<input', $result, 'An input should be rendered');
$this->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('<input', $result, 'An input should be rendered');
$this->assertContains('readonly', $result, 'The input should be readonly');
$this->assertContains('€5.00', $result, 'The value should be rendered');
}
}