From d621d00eea25cacc930084f75e9b6dc837e3a9d2 Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 10 Aug 2023 10:37:21 +0200 Subject: [PATCH 1/5] 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 1adaf9f14..e461c6abb 100644 --- a/src/ORM/DataObject.php +++ b/src/ORM/DataObject.php @@ -1491,7 +1491,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity $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 68acb97c9..29a2b1bf1 100644 --- a/tests/php/ORM/DBCompositeTest.php +++ b/tests/php/ORM/DBCompositeTest.php @@ -53,7 +53,8 @@ class DBCompositeTest extends SapphireTest $this->assertEquals( [ 'MyMoney' => 'Money', - 'OverriddenMoney' => 'Money' + 'OverriddenMoney' => 'Money', + 'DoubleMoney' => DBCompositeTest\DBDoubleMoney::class ], $schema->compositeFields(DBCompositeTest\TestObject::class) ); @@ -68,6 +69,7 @@ class DBCompositeTest extends SapphireTest 'MyMoney' => 'Money', 'OtherMoney' => 'Money', 'OverriddenMoney' => 'Money', + 'DoubleMoney' => DBCompositeTest\DBDoubleMoney::class ], $schema->compositeFields(DBCompositeTest\SubclassedDBFieldObject::class) ); @@ -111,4 +113,33 @@ class DBCompositeTest extends SapphireTest $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 000000000..1952e3cec --- /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 cfc74b2d9..10658c693 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/5] 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 29a2b1bf1..d43cf5fd8 100644 --- a/tests/php/ORM/DBCompositeTest.php +++ b/tests/php/ORM/DBCompositeTest.php @@ -114,6 +114,7 @@ class DBCompositeTest extends SapphireTest $this->assertEquals('DBCompositeTest_SubclassedDBFieldObject', $object2->dbObject('OverriddenMoney')->getTable()); } + /* Uncomment this for SS5 public function testSetFieldDynamicPropertyException() { $this->expectException(InvalidArgumentException::class); @@ -124,6 +125,7 @@ class DBCompositeTest extends SapphireTest $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/5] 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 d43cf5fd8..c4fe99ecc 100644 --- a/tests/php/ORM/DBCompositeTest.php +++ b/tests/php/ORM/DBCompositeTest.php @@ -114,19 +114,6 @@ class DBCompositeTest extends SapphireTest $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(); From c7cd26299a3945de0d3635bdfa845bb771cd95a6 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Tue, 15 Aug 2023 11:31:20 +1200 Subject: [PATCH 4/5] Fix ArrayList canFilterBy to work with ArrayData (#10915) --- src/ORM/ArrayList.php | 10 +++++++++- tests/php/ORM/ArrayListTest.php | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/ORM/ArrayList.php b/src/ORM/ArrayList.php index c5c478326..d0b8ae6db 100644 --- a/src/ORM/ArrayList.php +++ b/src/ORM/ArrayList.php @@ -604,7 +604,15 @@ class ArrayList extends ViewableData implements SS_List, Filterable, Sortable, L $firstRecord = $this->first(); - return is_array($firstRecord) ? array_key_exists($by, $firstRecord) : property_exists($firstRecord, $by ?? ''); + if (is_array($firstRecord)) { + return array_key_exists($by, $firstRecord); + } + + if ($firstRecord instanceof ViewableData) { + return $firstRecord->hasField($by); + } + + return property_exists($firstRecord, $by ?? ''); } /** diff --git a/tests/php/ORM/ArrayListTest.php b/tests/php/ORM/ArrayListTest.php index 9cb606030..a2253f280 100644 --- a/tests/php/ORM/ArrayListTest.php +++ b/tests/php/ORM/ArrayListTest.php @@ -7,6 +7,7 @@ use SilverStripe\Dev\SapphireTest; use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\Filterable; +use SilverStripe\View\ArrayData; use stdClass; class ArrayListTest extends SapphireTest @@ -1174,6 +1175,20 @@ class ArrayListTest extends SapphireTest $this->assertFalse($list->canFilterBy('Age')); } + public function testCanFilterByArrayData() + { + $list = new ArrayList( + [ + new ArrayData(['Name' => 'Steve']), + new ArrayData(['Name' => 'Bob']), + new ArrayData(['Name' => 'John']) + ] + ); + + $this->assertTrue($list->canFilterBy('Name')); + $this->assertFalse($list->canFilterBy('Age')); + } + public function testCanFilterByEmpty() { $list = new ArrayList(); From 3e72f5e6946b00301f326cee2f6b63fece0e2081 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Mon, 21 Aug 2023 12:32:21 +1200 Subject: [PATCH 5/5] ENH Update translations (#10920) --- lang/en.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lang/en.yml b/lang/en.yml index b2a60c947..9eecf2ddc 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -258,7 +258,9 @@ en: db_CanEditType: 'Can edit type' db_CanViewType: 'Can view type' many_many_EditorGroups: 'Editor groups' + many_many_EditorMembers: 'Editor members' many_many_ViewerGroups: 'Viewer groups' + many_many_ViewerMembers: 'Viewer members' SilverStripe\Security\LoginAttempt: Email: 'Email Address' EmailHashed: 'Email Address (hashed)'