2009-05-26 00:21:02 +02:00
|
|
|
<?php
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2017-01-26 05:20:08 +01:00
|
|
|
use SilverStripe\Forms\RequiredFields;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Forms\Tests\MoneyFieldTest\CustomSetter_Object;
|
|
|
|
use SilverStripe\Forms\Tests\MoneyFieldTest\TestObject;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBMoney;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\MoneyField;
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class MoneyFieldTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
2017-03-24 12:17:26 +01:00
|
|
|
protected static $extra_dataobjects = array(
|
2016-12-16 05:34:21 +01:00
|
|
|
TestObject::class,
|
|
|
|
CustomSetter_Object::class,
|
|
|
|
);
|
|
|
|
|
|
|
|
public function testSaveInto()
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
$testObject = new TestObject();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-01-26 05:20:08 +01:00
|
|
|
$money = new DBMoney();
|
|
|
|
$money->setAmount(123456.78);
|
|
|
|
$money->setCurrency('EUR');
|
|
|
|
$field = new MoneyField('MyMoney', 'MyMoney', $money);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-01-26 05:20:08 +01:00
|
|
|
$field->saveInto($testObject);
|
|
|
|
$this->assertEquals(123456.78, $testObject->MyMoney->getAmount());
|
|
|
|
$this->assertEquals('EUR', $testObject->MyMoney->getCurrency());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetValueAsMoney()
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
$testObject = new TestObject();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-01-26 05:20:08 +01:00
|
|
|
$field = new MoneyField('MyMoney', 'MyMoney');
|
|
|
|
$field->setLocale('en_NZ');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-01-26 05:20:08 +01:00
|
|
|
$money = new DBMoney();
|
|
|
|
$money->setAmount(123456.78);
|
|
|
|
$money->setCurrency('EUR');
|
|
|
|
$field->setValue($money);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-01-26 05:20:08 +01:00
|
|
|
$field->saveInto($testObject);
|
|
|
|
$this->assertEquals(123456.78, $testObject->MyMoney->getAmount());
|
|
|
|
$this->assertEquals('EUR', $testObject->MyMoney->getCurrency());
|
|
|
|
$this->assertEquals('123456.78 EUR', $field->dataValue());
|
|
|
|
$this->assertEquals('€123,456.78', $field->Value());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetValueAsArray()
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
$testObject = new TestObject();
|
|
|
|
$field = new MoneyField('MyMoney', 'MyMoney');
|
|
|
|
$field->setSubmittedValue([
|
|
|
|
'Currency' => 'EUR',
|
|
|
|
'Amount' => 123456.78
|
|
|
|
]);
|
|
|
|
|
|
|
|
$field->saveInto($testObject);
|
|
|
|
$this->assertEquals(123456.78, $testObject->MyMoney->getAmount());
|
|
|
|
$this->assertEquals('EUR', $testObject->MyMoney->getCurrency());
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-01-26 05:20:08 +01:00
|
|
|
public function testSetValueAsString()
|
|
|
|
{
|
|
|
|
$testObject = new TestObject();
|
|
|
|
$field = new MoneyField('MyMoney');
|
|
|
|
$field->setLocale('en_NZ');
|
|
|
|
$field->setValue('1.01 usd');
|
|
|
|
$field->saveInto($testObject);
|
|
|
|
$this->assertEquals(1.01, $testObject->MyMoney->getAmount());
|
|
|
|
$this->assertEquals('USD', $testObject->MyMoney->getCurrency());
|
|
|
|
$this->assertEquals('1.01 USD', $field->dataValue());
|
|
|
|
$this->assertEquals('US$1.01', $field->Value());
|
|
|
|
|
|
|
|
$testObject = new TestObject();
|
|
|
|
$field = new MoneyField('MyMoney');
|
|
|
|
$field->setLocale('en_NZ');
|
|
|
|
$field->setValue('1.01');
|
|
|
|
$field->saveInto($testObject);
|
|
|
|
$this->assertEquals(1.01, $testObject->MyMoney->getAmount());
|
|
|
|
$this->assertNull($testObject->MyMoney->getCurrency());
|
|
|
|
$this->assertEquals('1.01', $field->dataValue());
|
|
|
|
$this->assertEquals('$1.01', $field->Value());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This UT tests if saveInto used customised getters/setters correctly.
|
|
|
|
* Saving values for CustomMoney shall go through the setCustomMoney_Test
|
|
|
|
* setter method and double the value.
|
|
|
|
*/
|
|
|
|
public function testSetValueViaSetter()
|
|
|
|
{
|
|
|
|
$o = new CustomSetter_Object();
|
|
|
|
|
|
|
|
$f = new MoneyField('CustomMoney', 'Test Money Field');
|
2017-01-26 05:20:08 +01:00
|
|
|
$f->setSubmittedValue([
|
|
|
|
'Currency'=>'EUR',
|
|
|
|
'Amount'=>123456.78
|
|
|
|
]);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$f->saveInto($o);
|
|
|
|
$this->assertEquals((2 * 123456.78), $o->MyMoney->getAmount());
|
|
|
|
$this->assertEquals('EUR', $o->MyMoney->getCurrency());
|
|
|
|
}
|
2017-01-26 05:20:08 +01:00
|
|
|
|
|
|
|
public function testValidation()
|
|
|
|
{
|
|
|
|
$field = new MoneyField('Money');
|
|
|
|
$field->setAllowedCurrencies(['NZD', 'USD']);
|
|
|
|
|
|
|
|
// Valid currency
|
|
|
|
$validator = new RequiredFields();
|
|
|
|
$field->setSubmittedValue([
|
|
|
|
'Currency' => 'NZD',
|
|
|
|
'Amount' => 123
|
|
|
|
]);
|
|
|
|
$this->assertTrue($field->validate($validator));
|
|
|
|
|
|
|
|
// Invalid currency
|
|
|
|
$field->setSubmittedValue([
|
|
|
|
'Currency' => 'EUR',
|
|
|
|
'Amount' => 123
|
|
|
|
]);
|
|
|
|
$this->assertFalse($field->validate($validator));
|
|
|
|
}
|
2009-05-26 00:21:02 +02:00
|
|
|
}
|