Add tests for edge cases in CurrencyField

This commit is contained in:
Robbie Averill 2018-10-20 17:33:59 +02:00
parent 8929b8204f
commit d56bad7568
2 changed files with 45 additions and 19 deletions

View File

@ -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)

View File

@ -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());
}
}