2010-01-18 01:56:29 +01:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\CurrencyField;
|
2018-10-20 17:33:59 +02:00
|
|
|
use SilverStripe\Forms\CurrencyField_Readonly;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\RequiredFields;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBCurrency;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class CurrencyFieldTest extends SapphireTest
|
|
|
|
{
|
2010-01-18 01:56:29 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testValidate()
|
|
|
|
{
|
|
|
|
$f = new CurrencyField('TestField');
|
|
|
|
$validator = new RequiredFields();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-11-10 02:38:51 +01:00
|
|
|
//tests with default currency symbol setting
|
2016-12-16 05:34:21 +01:00
|
|
|
$f->setValue('123.45');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate($validator),
|
|
|
|
'Validates positive decimals'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('-123.45');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate($validator),
|
|
|
|
'Validates negative decimals'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('$123.45');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate($validator),
|
|
|
|
'Validates positive decimals with sign'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('-$123.45');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate($validator),
|
|
|
|
'Validates negative decimals with sign'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('$-123.45');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate($validator),
|
|
|
|
'Validates negative decimals with sign'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('324511434634');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate($validator),
|
|
|
|
'Validates large integers'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('test$1.23test');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate($validator),
|
|
|
|
'Alphanumeric is valid'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('$test');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate($validator),
|
|
|
|
'Words are valid'
|
|
|
|
);
|
2015-11-10 02:38:51 +01:00
|
|
|
|
|
|
|
//tests with updated currency symbol setting
|
2022-11-15 06:20:54 +01:00
|
|
|
DBCurrency::config()->set('currency_symbol', '€');
|
2015-11-10 02:38:51 +01:00
|
|
|
|
|
|
|
$f->setValue('123.45');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate($validator),
|
|
|
|
'Validates positive decimals'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('-123.45');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate($validator),
|
|
|
|
'Validates negative decimals'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('€123.45');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate($validator),
|
|
|
|
'Validates positive decimals with sign'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('-€123.45');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate($validator),
|
|
|
|
'Validates negative decimals with sign'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('€-123.45');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate($validator),
|
|
|
|
'Validates negative decimals with sign'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('324511434634');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate($validator),
|
|
|
|
'Validates large integers'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('test€1.23test');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate($validator),
|
|
|
|
'Alphanumeric is valid'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('€test');
|
|
|
|
$this->assertTrue(
|
|
|
|
$f->validate($validator),
|
|
|
|
'Words are valid'
|
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testSetValue()
|
|
|
|
{
|
|
|
|
$f = new CurrencyField('TestField');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-11-10 02:38:51 +01:00
|
|
|
//tests with default currency symbol setting
|
2016-12-16 05:34:21 +01:00
|
|
|
$f->setValue('123.45');
|
|
|
|
$this->assertEquals(
|
2018-10-20 17:33:59 +02:00
|
|
|
$f->Value(),
|
2016-12-16 05:34:21 +01:00
|
|
|
'$123.45',
|
|
|
|
'Prepends dollar sign to positive decimal'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('-123.45');
|
|
|
|
$this->assertEquals(
|
2018-10-20 17:33:59 +02:00
|
|
|
$f->Value(),
|
2016-12-16 05:34:21 +01:00
|
|
|
'$-123.45',
|
|
|
|
'Prepends dollar sign to negative decimal'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('$1');
|
|
|
|
$this->assertEquals(
|
2018-10-20 17:33:59 +02:00
|
|
|
$f->Value(),
|
2016-12-16 05:34:21 +01:00
|
|
|
'$1.00',
|
|
|
|
'Formats small value'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('$2.5');
|
|
|
|
$this->assertEquals(
|
2018-10-20 17:33:59 +02:00
|
|
|
$f->Value(),
|
2016-12-16 05:34:21 +01:00
|
|
|
'$2.50',
|
|
|
|
'Formats small value'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('$2500000.13');
|
|
|
|
$this->assertEquals(
|
2018-10-20 17:33:59 +02:00
|
|
|
$f->Value(),
|
2016-12-16 05:34:21 +01:00
|
|
|
'$2,500,000.13',
|
|
|
|
'Formats large value'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('$2.50000013');
|
|
|
|
$this->assertEquals(
|
2018-10-20 17:33:59 +02:00
|
|
|
$f->Value(),
|
2016-12-16 05:34:21 +01:00
|
|
|
'$2.50',
|
|
|
|
'Truncates long decimal portions'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('test123.00test');
|
|
|
|
$this->assertEquals(
|
2018-10-20 17:33:59 +02:00
|
|
|
$f->Value(),
|
2016-12-16 05:34:21 +01:00
|
|
|
'$123.00',
|
|
|
|
'Strips alpha values'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('test');
|
|
|
|
$this->assertEquals(
|
2018-10-20 17:33:59 +02:00
|
|
|
$f->Value(),
|
2016-12-16 05:34:21 +01:00
|
|
|
'$0.00',
|
|
|
|
'Does not set alpha values'
|
|
|
|
);
|
2015-11-10 02:38:51 +01:00
|
|
|
|
|
|
|
//update currency symbol via config
|
2022-11-15 06:20:54 +01:00
|
|
|
DBCurrency::config()->set('currency_symbol', '€');
|
2015-11-10 02:38:51 +01:00
|
|
|
|
|
|
|
$f->setValue('123.45');
|
|
|
|
$this->assertEquals(
|
2018-10-20 17:33:59 +02:00
|
|
|
$f->Value(),
|
2016-12-16 05:34:21 +01:00
|
|
|
'€123.45',
|
2015-11-10 02:38:51 +01:00
|
|
|
'Prepends dollar sign to positive decimal'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('-123.45');
|
|
|
|
$this->assertEquals(
|
2018-10-20 17:33:59 +02:00
|
|
|
$f->Value(),
|
2016-12-16 05:34:21 +01:00
|
|
|
'€-123.45',
|
2015-11-10 02:38:51 +01:00
|
|
|
'Prepends dollar sign to negative decimal'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('€1');
|
|
|
|
$this->assertEquals(
|
2018-10-20 17:33:59 +02:00
|
|
|
$f->Value(),
|
2016-12-16 05:34:21 +01:00
|
|
|
'€1.00',
|
2015-11-10 02:38:51 +01:00
|
|
|
'Formats small value'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('€2.5');
|
|
|
|
$this->assertEquals(
|
2018-10-20 17:33:59 +02:00
|
|
|
$f->Value(),
|
2016-12-16 05:34:21 +01:00
|
|
|
'€2.50',
|
2015-11-10 02:38:51 +01:00
|
|
|
'Formats small value'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('€2500000.13');
|
|
|
|
$this->assertEquals(
|
2018-10-20 17:33:59 +02:00
|
|
|
$f->Value(),
|
2016-12-16 05:34:21 +01:00
|
|
|
'€2,500,000.13',
|
2015-11-10 02:38:51 +01:00
|
|
|
'Formats large value'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('€2.50000013');
|
|
|
|
$this->assertEquals(
|
2018-10-20 17:33:59 +02:00
|
|
|
$f->Value(),
|
2016-12-16 05:34:21 +01:00
|
|
|
'€2.50',
|
2015-11-10 02:38:51 +01:00
|
|
|
'Truncates long decimal portions'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('test123.00test');
|
|
|
|
$this->assertEquals(
|
2018-10-20 17:33:59 +02:00
|
|
|
$f->Value(),
|
2016-12-16 05:34:21 +01:00
|
|
|
'€123.00',
|
2015-11-10 02:38:51 +01:00
|
|
|
'Strips alpha values'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('test');
|
|
|
|
$this->assertEquals(
|
2018-10-20 17:33:59 +02:00
|
|
|
$f->Value(),
|
2016-12-16 05:34:21 +01:00
|
|
|
'€0.00',
|
2015-11-10 02:38:51 +01:00
|
|
|
'Does not set alpha values'
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testDataValue()
|
|
|
|
{
|
|
|
|
$f = new CurrencyField('TestField');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-11-10 02:38:51 +01:00
|
|
|
//tests with default currency symbol settings
|
2016-12-16 05:34:21 +01:00
|
|
|
$f->setValue('$123.45');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->dataValue(),
|
|
|
|
123.45
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$f->setValue('-$123.45');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->dataValue(),
|
|
|
|
-123.45
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$f->setValue('$-123.45');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->dataValue(),
|
|
|
|
-123.45
|
|
|
|
);
|
2015-11-10 02:38:51 +01:00
|
|
|
|
|
|
|
//tests with updated currency symbol setting
|
2022-11-15 06:20:54 +01:00
|
|
|
DBCurrency::config()->set('currency_symbol', '€');
|
2015-11-10 02:38:51 +01:00
|
|
|
|
|
|
|
$f->setValue('€123.45');
|
|
|
|
$this->assertEquals(
|
2016-12-16 05:34:21 +01:00
|
|
|
$f->dataValue(),
|
|
|
|
123.45
|
2015-11-10 02:38:51 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('-€123.45');
|
|
|
|
$this->assertEquals(
|
2016-12-16 05:34:21 +01:00
|
|
|
$f->dataValue(),
|
|
|
|
-123.45
|
2015-11-10 02:38:51 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('€-123.45');
|
|
|
|
$this->assertEquals(
|
2016-12-16 05:34:21 +01:00
|
|
|
$f->dataValue(),
|
|
|
|
-123.45
|
2015-11-10 02:38:51 +01:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2018-10-20 17:33:59 +02:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2022-11-15 06:20:54 +01:00
|
|
|
DBCurrency::config()->set('currency_symbol', '€');
|
2018-10-20 17:33:59 +02:00
|
|
|
$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');
|
2022-11-15 06:20:54 +01:00
|
|
|
$this->assertStringContainsString(
|
|
|
|
'Please enter a valid currency',
|
|
|
|
json_encode($validator->getResult()->__serialize())
|
|
|
|
);
|
2018-10-20 17:33:59 +02:00
|
|
|
}
|
2010-01-18 01:56:29 +01:00
|
|
|
}
|