2010-01-18 01:56:29 +01:00
|
|
|
<?php
|
|
|
|
|
2010-10-19 02:32:18 +02:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-10-19 02:32:18 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2010-01-18 01:56:29 +01:00
|
|
|
class CurrencyFieldTest extends SapphireTest {
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testValidate() {
|
2010-10-19 02:32:18 +02:00
|
|
|
$f = new CurrencyField('TestField');
|
2014-11-12 03:19:12 +01:00
|
|
|
$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
|
2010-10-19 02:32:18 +02:00
|
|
|
$f->setValue('123.45');
|
|
|
|
$this->assertTrue(
|
2014-11-12 03:19:12 +01:00
|
|
|
$f->validate($validator),
|
2010-10-19 02:32:18 +02:00
|
|
|
'Validates positive decimals'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 02:32:18 +02:00
|
|
|
$f->setValue('-123.45');
|
|
|
|
$this->assertTrue(
|
2014-11-12 03:19:12 +01:00
|
|
|
$f->validate($validator),
|
2010-10-19 02:32:18 +02:00
|
|
|
'Validates negative decimals'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 02:32:18 +02:00
|
|
|
$f->setValue('$123.45');
|
|
|
|
$this->assertTrue(
|
2014-11-12 03:19:12 +01:00
|
|
|
$f->validate($validator),
|
2010-10-19 02:32:18 +02:00
|
|
|
'Validates positive decimals with sign'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 02:32:18 +02:00
|
|
|
$f->setValue('-$123.45');
|
|
|
|
$this->assertTrue(
|
2014-11-12 03:19:12 +01:00
|
|
|
$f->validate($validator),
|
2010-10-19 02:32:18 +02:00
|
|
|
'Validates negative decimals with sign'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 02:32:18 +02:00
|
|
|
$f->setValue('$-123.45');
|
|
|
|
$this->assertTrue(
|
2014-11-12 03:19:12 +01:00
|
|
|
$f->validate($validator),
|
2010-10-19 02:32:18 +02:00
|
|
|
'Validates negative decimals with sign'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 02:32:18 +02:00
|
|
|
$f->setValue('324511434634');
|
|
|
|
$this->assertTrue(
|
2014-11-12 03:19:12 +01:00
|
|
|
$f->validate($validator),
|
2010-10-19 02:32:18 +02:00
|
|
|
'Validates large integers'
|
|
|
|
);
|
2014-11-12 03:19:12 +01:00
|
|
|
|
|
|
|
$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
|
|
|
|
Config::inst()->update('Currency', 'currency_symbol', '€');
|
|
|
|
|
|
|
|
$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'
|
|
|
|
);
|
2010-10-19 02:32:18 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSetValue() {
|
2010-10-19 02:32:18 +02:00
|
|
|
$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
|
2010-10-19 02:32:18 +02:00
|
|
|
$f->setValue('123.45');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '$123.45',
|
|
|
|
'Prepends dollar sign to positive decimal'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 02:32:18 +02:00
|
|
|
$f->setValue('-123.45');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '$-123.45',
|
|
|
|
'Prepends dollar sign to negative decimal'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 02:32:18 +02:00
|
|
|
$f->setValue('$1');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '$1.00',
|
|
|
|
'Formats small value'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 02:32:18 +02:00
|
|
|
$f->setValue('$2.5');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '$2.50',
|
|
|
|
'Formats small value'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 02:32:18 +02:00
|
|
|
$f->setValue('$2500000.13');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '$2,500,000.13',
|
|
|
|
'Formats large value'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 02:32:18 +02:00
|
|
|
$f->setValue('$2.50000013');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '$2.50',
|
|
|
|
'Truncates long decimal portions'
|
|
|
|
);
|
2014-11-12 03:19:12 +01:00
|
|
|
|
|
|
|
$f->setValue('test123.00test');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '$123.00',
|
|
|
|
'Strips alpha values'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('test');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '$0.00',
|
|
|
|
'Does not set alpha values'
|
|
|
|
);
|
2015-11-10 02:38:51 +01:00
|
|
|
|
|
|
|
//update currency symbol via config
|
|
|
|
Config::inst()->update('Currency', 'currency_symbol', '€');
|
|
|
|
|
|
|
|
$f->setValue('123.45');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '€123.45',
|
|
|
|
'Prepends dollar sign to positive decimal'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('-123.45');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '€-123.45',
|
|
|
|
'Prepends dollar sign to negative decimal'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('€1');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '€1.00',
|
|
|
|
'Formats small value'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('€2.5');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '€2.50',
|
|
|
|
'Formats small value'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('€2500000.13');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '€2,500,000.13',
|
|
|
|
'Formats large value'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('€2.50000013');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '€2.50',
|
|
|
|
'Truncates long decimal portions'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('test123.00test');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '€123.00',
|
|
|
|
'Strips alpha values'
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('test');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->value, '€0.00',
|
|
|
|
'Does not set alpha values'
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDataValue() {
|
2010-10-19 02:32:18 +02:00
|
|
|
$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
|
2010-10-19 02:32:18 +02:00
|
|
|
$f->setValue('$123.45');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->dataValue(), 123.45
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 02:32:18 +02:00
|
|
|
$f->setValue('-$123.45');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->dataValue(), -123.45
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-19 02:32:18 +02: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
|
|
|
|
Config::inst()->update('Currency', 'currency_symbol', '€');
|
|
|
|
|
|
|
|
$f->setValue('€123.45');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->dataValue(), 123.45
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('-€123.45');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->dataValue(), -123.45
|
|
|
|
);
|
|
|
|
|
|
|
|
$f->setValue('€-123.45');
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->dataValue(), -123.45
|
|
|
|
);
|
2010-01-18 01:56:29 +01:00
|
|
|
}
|
|
|
|
}
|