From d621d00eea25cacc930084f75e9b6dc837e3a9d2 Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 10 Aug 2023 10:37:21 +0200 Subject: [PATCH 1/3] DBComposite::writeToManipulation() is never called --- src/ORM/DataObject.php | 2 +- tests/php/ORM/DBCompositeTest.php | 33 ++++++++++++++++++- .../php/ORM/DBCompositeTest/DBDoubleMoney.php | 17 ++++++++++ tests/php/ORM/DBCompositeTest/TestObject.php | 3 +- 4 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 tests/php/ORM/DBCompositeTest/DBDoubleMoney.php diff --git a/src/ORM/DataObject.php b/src/ORM/DataObject.php index 1adaf9f1460..e461c6abb70 100644 --- a/src/ORM/DataObject.php +++ b/src/ORM/DataObject.php @@ -1491,7 +1491,7 @@ protected function prepareManipulationTable($baseTable, $now, $isNewRecord, &$ma $specification = $schema->fieldSpec( $class, $fieldName, - DataObjectSchema::DB_ONLY | DataObjectSchema::UNINHERITED + DataObjectSchema::UNINHERITED ); if (!$specification) { continue; diff --git a/tests/php/ORM/DBCompositeTest.php b/tests/php/ORM/DBCompositeTest.php index 68acb97c9ac..29a2b1bf110 100644 --- a/tests/php/ORM/DBCompositeTest.php +++ b/tests/php/ORM/DBCompositeTest.php @@ -53,7 +53,8 @@ public function testCompositeFieldMetaDataFunctions() $this->assertEquals( [ 'MyMoney' => 'Money', - 'OverriddenMoney' => 'Money' + 'OverriddenMoney' => 'Money', + 'DoubleMoney' => DBCompositeTest\DBDoubleMoney::class ], $schema->compositeFields(DBCompositeTest\TestObject::class) ); @@ -68,6 +69,7 @@ public function testCompositeFieldMetaDataFunctions() 'MyMoney' => 'Money', 'OtherMoney' => 'Money', 'OverriddenMoney' => 'Money', + 'DoubleMoney' => DBCompositeTest\DBDoubleMoney::class ], $schema->compositeFields(DBCompositeTest\SubclassedDBFieldObject::class) ); @@ -111,4 +113,33 @@ public function testInheritedTables() $this->assertEquals('DBCompositeTest_SubclassedDBFieldObject', $object2->dbObject('OtherMoney')->getTable()); $this->assertEquals('DBCompositeTest_SubclassedDBFieldObject', $object2->dbObject('OverriddenMoney')->getTable()); } + + public function testSetFieldDynamicPropertyException() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage(implode(' ', [ + 'Field abc does not exist.', + 'If this was accessed via a dynamic property then call setDynamicData() instead.' + ])); + $object = new DBCompositeTest\TestObject(); + $object->MyMoney->abc = 'def'; + } + + public function testWriteToManipuationIsCalledWhenWritingDataObject() + { + $obj = DBCompositeTest\TestObject::create(); + $obj->DoubleMoney = ['Amount' => 10, 'Currency' => 'CAD']; + $moneyField = $obj->dbObject('DoubleMoney'); + $this->assertEquals(10, $moneyField->getAmount()); + + $obj->write(); + + // Custom money class should double the amount before writing + $this->assertEquals(20, $moneyField->getAmount()); + + // Note: these would fail since dbObject will return a new instance + // of the DoubleMoney field based on the initial values + // $this->assertSame($moneyField, $obj->dbObject('DoubleMoney')); + // $this->assertEquals(20, $obj->dbObject('DoubleMoney')->getAmount()); + } } diff --git a/tests/php/ORM/DBCompositeTest/DBDoubleMoney.php b/tests/php/ORM/DBCompositeTest/DBDoubleMoney.php new file mode 100644 index 00000000000..1952e3cec9f --- /dev/null +++ b/tests/php/ORM/DBCompositeTest/DBDoubleMoney.php @@ -0,0 +1,17 @@ +setAmount($this->getAmount() * 2); + + parent::writeToManipulation($manipulation); + } +} diff --git a/tests/php/ORM/DBCompositeTest/TestObject.php b/tests/php/ORM/DBCompositeTest/TestObject.php index cfc74b2d963..10658c6933e 100644 --- a/tests/php/ORM/DBCompositeTest/TestObject.php +++ b/tests/php/ORM/DBCompositeTest/TestObject.php @@ -12,6 +12,7 @@ class TestObject extends DataObject implements TestOnly private static $db = [ 'Title' => 'Text', 'MyMoney' => 'Money', - 'OverriddenMoney' => 'Money' + 'OverriddenMoney' => 'Money', + 'DoubleMoney' => DBDoubleMoney::class, ]; } From 0d4231abb8a35043d44058afdf1a2e3df1147240 Mon Sep 17 00:00:00 2001 From: Thomas Portelange Date: Fri, 11 Aug 2023 13:24:01 +0200 Subject: [PATCH 2/3] comment test for ss4 --- tests/php/ORM/DBCompositeTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/php/ORM/DBCompositeTest.php b/tests/php/ORM/DBCompositeTest.php index 29a2b1bf110..d43cf5fd801 100644 --- a/tests/php/ORM/DBCompositeTest.php +++ b/tests/php/ORM/DBCompositeTest.php @@ -114,6 +114,7 @@ public function testInheritedTables() $this->assertEquals('DBCompositeTest_SubclassedDBFieldObject', $object2->dbObject('OverriddenMoney')->getTable()); } + /* Uncomment this for SS5 public function testSetFieldDynamicPropertyException() { $this->expectException(InvalidArgumentException::class); @@ -124,6 +125,7 @@ public function testSetFieldDynamicPropertyException() $object = new DBCompositeTest\TestObject(); $object->MyMoney->abc = 'def'; } + */ public function testWriteToManipuationIsCalledWhenWritingDataObject() { From 637859a1f45d50978b93e12bc90f1a21079a7678 Mon Sep 17 00:00:00 2001 From: Thomas Portelange Date: Mon, 14 Aug 2023 09:26:33 +0200 Subject: [PATCH 3/3] Update tests/php/ORM/DBCompositeTest.php Co-authored-by: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> --- tests/php/ORM/DBCompositeTest.php | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/tests/php/ORM/DBCompositeTest.php b/tests/php/ORM/DBCompositeTest.php index d43cf5fd801..c4fe99ecc1f 100644 --- a/tests/php/ORM/DBCompositeTest.php +++ b/tests/php/ORM/DBCompositeTest.php @@ -114,19 +114,6 @@ public function testInheritedTables() $this->assertEquals('DBCompositeTest_SubclassedDBFieldObject', $object2->dbObject('OverriddenMoney')->getTable()); } - /* Uncomment this for SS5 - public function testSetFieldDynamicPropertyException() - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage(implode(' ', [ - 'Field abc does not exist.', - 'If this was accessed via a dynamic property then call setDynamicData() instead.' - ])); - $object = new DBCompositeTest\TestObject(); - $object->MyMoney->abc = 'def'; - } - */ - public function testWriteToManipuationIsCalledWhenWritingDataObject() { $obj = DBCompositeTest\TestObject::create();