Merge branch '4.13' into 5.0

This commit is contained in:
Steve Boyd 2023-08-22 15:32:38 +12:00
commit ee0468e5b7
7 changed files with 67 additions and 4 deletions

View File

@ -258,7 +258,9 @@ en:
db_CanEditType: 'Can edit type' db_CanEditType: 'Can edit type'
db_CanViewType: 'Can view type' db_CanViewType: 'Can view type'
many_many_EditorGroups: 'Editor groups' many_many_EditorGroups: 'Editor groups'
many_many_EditorMembers: 'Editor members'
many_many_ViewerGroups: 'Viewer groups' many_many_ViewerGroups: 'Viewer groups'
many_many_ViewerMembers: 'Viewer members'
SilverStripe\Security\LoginAttempt: SilverStripe\Security\LoginAttempt:
Email: 'Email Address' Email: 'Email Address'
EmailHashed: 'Email Address (hashed)' EmailHashed: 'Email Address (hashed)'

View File

@ -583,7 +583,15 @@ class ArrayList extends ViewableData implements SS_List, Filterable, Sortable, L
$firstRecord = $this->first(); $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 ?? '');
} }
/** /**

View File

@ -1434,7 +1434,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
$specification = $schema->fieldSpec( $specification = $schema->fieldSpec(
$class, $class,
$fieldName, $fieldName,
DataObjectSchema::DB_ONLY | DataObjectSchema::UNINHERITED DataObjectSchema::UNINHERITED
); );
if (!$specification) { if (!$specification) {
continue; continue;

View File

@ -6,6 +6,7 @@ use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\Filterable; use SilverStripe\ORM\Filterable;
use SilverStripe\View\ArrayData;
use stdClass; use stdClass;
class ArrayListTest extends SapphireTest class ArrayListTest extends SapphireTest
@ -1208,6 +1209,20 @@ class ArrayListTest extends SapphireTest
$this->assertFalse($list->canFilterBy('Age')); $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() public function testCanFilterByEmpty()
{ {
$list = new ArrayList(); $list = new ArrayList();

View File

@ -51,7 +51,8 @@ class DBCompositeTest extends SapphireTest
$this->assertEquals( $this->assertEquals(
[ [
'MyMoney' => 'Money', 'MyMoney' => 'Money',
'OverriddenMoney' => 'Money' 'OverriddenMoney' => 'Money',
'DoubleMoney' => DBCompositeTest\DBDoubleMoney::class
], ],
$schema->compositeFields(DBCompositeTest\TestObject::class) $schema->compositeFields(DBCompositeTest\TestObject::class)
); );
@ -66,6 +67,7 @@ class DBCompositeTest extends SapphireTest
'MyMoney' => 'Money', 'MyMoney' => 'Money',
'OtherMoney' => 'Money', 'OtherMoney' => 'Money',
'OverriddenMoney' => 'Money', 'OverriddenMoney' => 'Money',
'DoubleMoney' => DBCompositeTest\DBDoubleMoney::class
], ],
$schema->compositeFields(DBCompositeTest\SubclassedDBFieldObject::class) $schema->compositeFields(DBCompositeTest\SubclassedDBFieldObject::class)
); );
@ -120,4 +122,22 @@ class DBCompositeTest extends SapphireTest
$object = new DBCompositeTest\TestObject(); $object = new DBCompositeTest\TestObject();
$object->MyMoney->abc = 'def'; $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());
}
} }

View File

@ -0,0 +1,17 @@
<?php
namespace SilverStripe\ORM\Tests\DBCompositeTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\FieldType\DBMoney;
class DBDoubleMoney extends DBMoney implements TestOnly
{
public function writeToManipulation(&$manipulation)
{
// Duplicate the amount before writing
$this->setAmount($this->getAmount() * 2);
parent::writeToManipulation($manipulation);
}
}

View File

@ -12,6 +12,7 @@ class TestObject extends DataObject implements TestOnly
private static $db = [ private static $db = [
'Title' => 'Text', 'Title' => 'Text',
'MyMoney' => 'Money', 'MyMoney' => 'Money',
'OverriddenMoney' => 'Money' 'OverriddenMoney' => 'Money',
'DoubleMoney' => DBDoubleMoney::class,
]; ];
} }