From a3ce922f1dea4beea797639bd33d10b87eaf8a9f Mon Sep 17 00:00:00 2001 From: Dominik Beerbohm Date: Fri, 16 Feb 2024 12:42:51 +0100 Subject: [PATCH] ENH Allow better subclassing of MoneyField Move generation of NumberField from constructor to method to allow override in subclass. Addded test for MoneyField --- src/Forms/MoneyField.php | 18 ++++++++++++----- tests/php/Forms/MoneyFieldTest.php | 31 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/Forms/MoneyField.php b/src/Forms/MoneyField.php index 614d7ebf1d6..32961addf61 100644 --- a/src/Forms/MoneyField.php +++ b/src/Forms/MoneyField.php @@ -59,11 +59,7 @@ public function getAmountField() public function __construct($name, $title = null, $value = "") { $this->setName($name); - $this->fieldAmount = NumericField::create( - "{$name}[Amount]", - _t('SilverStripe\\Forms\\MoneyField.FIELDLABELAMOUNT', 'Amount') - ) - ->setScale(2); + $this->buildAmountField(); $this->buildCurrencyField(); parent::__construct($name, $title, $value); @@ -75,6 +71,18 @@ public function __clone() $this->fieldCurrency = clone $this->fieldCurrency; } + /** + * Builds a field to input the amount of money + */ + protected function buildAmountField(): void + { + $this->fieldAmount = NumericField::create( + $this->name . '[Amount]', + _t('SilverStripe\\Forms\\MoneyField.FIELDLABELAMOUNT', 'Amount') + ) + ->setScale(2); + } + /** * Builds a new currency field based on the allowed currencies configured * diff --git a/tests/php/Forms/MoneyFieldTest.php b/tests/php/Forms/MoneyFieldTest.php index 957b9e28109..8f3fc549505 100644 --- a/tests/php/Forms/MoneyFieldTest.php +++ b/tests/php/Forms/MoneyFieldTest.php @@ -2,9 +2,13 @@ namespace SilverStripe\Forms\Tests; +use SilverStripe\Forms\DropdownField; +use SilverStripe\Forms\HiddenField; +use SilverStripe\Forms\NumericField; use SilverStripe\Forms\RequiredFields; use SilverStripe\Forms\Tests\MoneyFieldTest\CustomSetter_Object; use SilverStripe\Forms\Tests\MoneyFieldTest\TestObject; +use SilverStripe\Forms\TextField; use SilverStripe\ORM\FieldType\DBMoney; use SilverStripe\Dev\SapphireTest; use SilverStripe\Forms\MoneyField; @@ -127,4 +131,31 @@ public function testValidation() ]); $this->assertFalse($field->validate($validator)); } + + public function testGetCurrencyField(): void + { + $field = new MoneyField('Money'); + $field->setAllowedCurrencies(['NZD', 'USD']); + + $this->assertInstanceOf(DropdownField::class, $field->getCurrencyField()); + $this->assertEquals('Money[Currency]', $field->getCurrencyField()->getName()); + + $field->setAllowedCurrencies(['USD']); + + $this->assertInstanceOf(HiddenField::class, $field->getCurrencyField()); + $this->assertEquals('Money[Currency]', $field->getCurrencyField()->getName()); + + $field->setAllowedCurrencies([]); + + $this->assertInstanceOf(TextField::class, $field->getCurrencyField()); + $this->assertEquals('Money[Currency]', $field->getCurrencyField()->getName()); + } + + public function testGetAmountField(): void + { + $field = new MoneyField('Money'); + $this->assertInstanceOf(NumericField::class, $field->getAmountField()); + $this->assertEquals(2, $field->getAmountField()->getScale()); + $this->assertEquals('Money[Amount]', $field->getAmountField()->getName()); + } }