From d56bad7568df1b4da0e196c961b7d0d3b310f285 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Sat, 20 Oct 2018 17:33:59 +0200 Subject: [PATCH] Add tests for edge cases in CurrencyField --- src/Forms/CurrencyField.php | 5 +-- tests/php/Forms/CurrencyFieldTest.php | 59 +++++++++++++++++++-------- 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/src/Forms/CurrencyField.php b/src/Forms/CurrencyField.php index 80bf1a51e..5e2555c70 100644 --- a/src/Forms/CurrencyField.php +++ b/src/Forms/CurrencyField.php @@ -39,9 +39,8 @@ class CurrencyField extends TextField { if ($this->value) { return preg_replace('/[^0-9.\-]/', '', $this->value); - } else { - return 0.00; } + return 0.00; } public function Type() @@ -54,7 +53,7 @@ class CurrencyField extends TextField */ public function performReadonlyTransformation() { - return $this->castedCopy('SilverStripe\\Forms\\CurrencyField_Readonly'); + return $this->castedCopy(CurrencyField_Readonly::class); } public function validate($validator) diff --git a/tests/php/Forms/CurrencyFieldTest.php b/tests/php/Forms/CurrencyFieldTest.php index a985dffe1..dbb595996 100644 --- a/tests/php/Forms/CurrencyFieldTest.php +++ b/tests/php/Forms/CurrencyFieldTest.php @@ -5,6 +5,7 @@ namespace SilverStripe\Forms\Tests; use SilverStripe\Core\Config\Config; use SilverStripe\Dev\SapphireTest; use SilverStripe\Forms\CurrencyField; +use SilverStripe\Forms\CurrencyField_Readonly; use SilverStripe\Forms\RequiredFields; use SilverStripe\ORM\FieldType\DBCurrency; @@ -124,56 +125,56 @@ class CurrencyFieldTest extends SapphireTest //tests with default currency symbol setting $f->setValue('123.45'); $this->assertEquals( - $f->value, + $f->Value(), '$123.45', 'Prepends dollar sign to positive decimal' ); $f->setValue('-123.45'); $this->assertEquals( - $f->value, + $f->Value(), '$-123.45', 'Prepends dollar sign to negative decimal' ); $f->setValue('$1'); $this->assertEquals( - $f->value, + $f->Value(), '$1.00', 'Formats small value' ); $f->setValue('$2.5'); $this->assertEquals( - $f->value, + $f->Value(), '$2.50', 'Formats small value' ); $f->setValue('$2500000.13'); $this->assertEquals( - $f->value, + $f->Value(), '$2,500,000.13', 'Formats large value' ); $f->setValue('$2.50000013'); $this->assertEquals( - $f->value, + $f->Value(), '$2.50', 'Truncates long decimal portions' ); $f->setValue('test123.00test'); $this->assertEquals( - $f->value, + $f->Value(), '$123.00', 'Strips alpha values' ); $f->setValue('test'); $this->assertEquals( - $f->value, + $f->Value(), '$0.00', 'Does not set alpha values' ); @@ -183,56 +184,56 @@ class CurrencyFieldTest extends SapphireTest $f->setValue('123.45'); $this->assertEquals( - $f->value, + $f->Value(), '€123.45', 'Prepends dollar sign to positive decimal' ); $f->setValue('-123.45'); $this->assertEquals( - $f->value, + $f->Value(), '€-123.45', 'Prepends dollar sign to negative decimal' ); $f->setValue('€1'); $this->assertEquals( - $f->value, + $f->Value(), '€1.00', 'Formats small value' ); $f->setValue('€2.5'); $this->assertEquals( - $f->value, + $f->Value(), '€2.50', 'Formats small value' ); $f->setValue('€2500000.13'); $this->assertEquals( - $f->value, + $f->Value(), '€2,500,000.13', 'Formats large value' ); $f->setValue('€2.50000013'); $this->assertEquals( - $f->value, + $f->Value(), '€2.50', 'Truncates long decimal portions' ); $f->setValue('test123.00test'); $this->assertEquals( - $f->value, + $f->Value(), '€123.00', 'Strips alpha values' ); $f->setValue('test'); $this->assertEquals( - $f->value, + $f->Value(), '€0.00', 'Does not set alpha values' ); @@ -282,4 +283,30 @@ class CurrencyFieldTest extends SapphireTest -123.45 ); } + + public function testDataValueReturnsEmptyFloat() + { + $field = new CurrencyField('Test', '', null); + $this->assertSame(0.00, $field->dataValue()); + } + + public function testPerformReadonlyTransformation() + { + $field = new CurrencyField('Test'); + $result = $field->performReadonlyTransformation(); + $this->assertInstanceOf(CurrencyField_Readonly::class, $result); + } + + public function testInvalidCurrencySymbol() + { + $field = new CurrencyField('Test', '', '$5.00'); + $validator = new RequiredFields(); + + DBCurrency::config()->update('currency_symbol', '€'); + $result = $field->validate($validator); + + $this->assertFalse($result, 'Validation should fail since wrong currency was used'); + $this->assertFalse($validator->getResult()->isValid(), 'Validator should receive failed state'); + $this->assertContains('Please enter a valid currency', $validator->getResult()->serialize()); + } }