API Use base data class for ChangeSetItem#ObjectClass, not just ClassName

This is a data structure change, but makes ChangeSetItems less likely to break
on /dev/build where a ClassName changes
This commit is contained in:
Hamish Friedlander 2016-04-12 08:39:55 +12:00
parent 38025f5e47
commit 2d16d69ddb
5 changed files with 42 additions and 12 deletions

View File

@ -117,7 +117,7 @@ class ChangeSet extends DataObject {
$references = [ $references = [
'ObjectID' => $object->ID, 'ObjectID' => $object->ID,
'ObjectClass' => $object->ClassName 'ObjectClass' => ClassInfo::baseDataClass($object)
]; ];
// Get existing item in case already added // Get existing item in case already added
@ -146,7 +146,7 @@ class ChangeSet extends DataObject {
public function removeObject(DataObject $object) { public function removeObject(DataObject $object) {
$item = ChangeSetItem::get()->filter([ $item = ChangeSetItem::get()->filter([
'ObjectID' => $object->ID, 'ObjectID' => $object->ID,
'ObjectClass' => $object->ClassName, 'ObjectClass' => ClassInfo::baseDataClass($object),
'ChangeSetID' => $this->ID 'ChangeSetID' => $this->ID
])->first(); ])->first();
@ -161,7 +161,7 @@ class ChangeSet extends DataObject {
protected function implicitKey($item) { protected function implicitKey($item) {
if ($item instanceof ChangeSetItem) return $item->ObjectClass.'.'.$item->ObjectID; if ($item instanceof ChangeSetItem) return $item->ObjectClass.'.'.$item->ObjectID;
return $item->ClassName.'.'.$item->ID; return ClassInfo::baseDataClass($item).'.'.$item->ID;
} }
protected function calculateImplicit() { protected function calculateImplicit() {
@ -183,7 +183,7 @@ class ChangeSet extends DataObject {
$referenced[$key] = [ $referenced[$key] = [
'ObjectID' => $referee->ID, 'ObjectID' => $referee->ID,
'ObjectClass' => $referee->ClassName 'ObjectClass' => ClassInfo::baseDataClass($referee)
]; ];
$references[$key][] = $item->ID; $references[$key][] = $item->ID;

View File

@ -9,8 +9,8 @@ use SilverStripe\Filesystem\Thumbnail;
* A single line in a changeset * A single line in a changeset
* *
* @property string $Added * @property string $Added
* @property string $ObjectClass * @property string $ObjectClass The _base_ data class for the referenced DataObject
* @property int $ObjectID * @property int $ObjectID The numeric ID for the referenced object
* @method ManyManyList ReferencedBy() List of explicit items that require this change * @method ManyManyList ReferencedBy() List of explicit items that require this change
* @method ManyManyList References() List of implicit items required by this change * @method ManyManyList References() List of implicit items required by this change
* @method ChangeSet ChangeSet() * @method ChangeSet ChangeSet()
@ -65,6 +65,12 @@ class ChangeSetItem extends DataObject implements Thumbnail {
) )
); );
public function onBeforeWrite() {
// Make sure ObjectClass refers to the base data class in the case of old or wrong code
$this->ObjectClass = ClassInfo::baseDataClass($this->ObjectClass);
parent::onBeforeWrite();
}
public function getTitle() { public function getTitle() {
// Get title of modified object // Get title of modified object
$object = $this->getObjectLatestVersion(); $object = $this->getObjectLatestVersion();
@ -74,8 +80,6 @@ class ChangeSetItem extends DataObject implements Thumbnail {
return $this->i18n_singular_name() . ' #' . $this->ID; return $this->i18n_singular_name() . ' #' . $this->ID;
} }
/** /**
* Get a thumbnail for this object * Get a thumbnail for this object
* *
@ -91,7 +95,6 @@ class ChangeSetItem extends DataObject implements Thumbnail {
return null; return null;
} }
/** /**
* Get the type of change: none, created, deleted, modified, manymany * Get the type of change: none, created, deleted, modified, manymany
* *

View File

@ -28,7 +28,7 @@ class ChangeSetItemTest extends SapphireTest {
$item = new ChangeSetItem([ $item = new ChangeSetItem([
'ObjectID' => $object->ID, 'ObjectID' => $object->ID,
'ObjectClass' => $object->ClassName 'ObjectClass' => ClassInfo::baseDataClass($object->ClassName)
]); ]);
$this->assertEquals( $this->assertEquals(

View File

@ -98,6 +98,16 @@ class ChangeSetTest_End extends DataObject implements TestOnly {
]; ];
} }
/**
* @mixin Versioned
*/
class ChangeSetTest_EndChild extends ChangeSetTest_End implements TestOnly {
private static $db = [
'Qux' => 'Int',
];
}
/** /**
* Test {@see ChangeSet} and {@see ChangeSetItem} models * Test {@see ChangeSet} and {@see ChangeSetItem} models
*/ */
@ -109,6 +119,7 @@ class ChangeSetTest extends SapphireTest {
'ChangeSetTest_Base', 'ChangeSetTest_Base',
'ChangeSetTest_Mid', 'ChangeSetTest_Mid',
'ChangeSetTest_End', 'ChangeSetTest_End',
'ChangeSetTest_EndChild',
]; ];
/** /**
@ -139,7 +150,7 @@ class ChangeSetTest extends SapphireTest {
$object = $this->objFromFixture($class, $identifier); $object = $this->objFromFixture($class, $identifier);
foreach($items as $i => $item) { foreach($items as $i => $item) {
if ($item->ObjectClass == $object->ClassName && $item->ObjectID == $object->ID && $item->Added == $mode) { if ($item->ObjectClass == ClassInfo::baseDataClass($object) && $item->ObjectID == $object->ID && $item->Added == $mode) {
unset($items[$i]); unset($items[$i]);
continue 2; continue 2;
} }
@ -161,6 +172,19 @@ class ChangeSetTest extends SapphireTest {
} }
} }
public function testAddObject() {
$cs = new ChangeSet();
$cs->write();
$cs->addObject($this->objFromFixture('ChangeSetTest_End', 'end1'));
$cs->addObject($this->objFromFixture('ChangeSetTest_EndChild', 'endchild1'));
$this->assertChangeSetLooksLike($cs, [
'ChangeSetTest_End.end1' => ChangeSetItem::EXPLICITLY,
'ChangeSetTest_EndChild.endchild1' => ChangeSetItem::EXPLICITLY
]);
}
public function testRepeatedSyncIsNOP() { public function testRepeatedSyncIsNOP() {
$this->publishAllFixtures(); $this->publishAllFixtures();
@ -256,7 +280,6 @@ class ChangeSetTest extends SapphireTest {
$this->assertTrue($cs->isSynced()); $this->assertTrue($cs->isSynced());
} }
public function testCanPublish() { public function testCanPublish() {
// Create changeset containing all items (unpublished) // Create changeset containing all items (unpublished)
$this->logInWithPermission('ADMIN'); $this->logInWithPermission('ADMIN');

View File

@ -6,6 +6,10 @@ ChangeSetTest_End:
Baz: 1 Baz: 1
end2: end2:
Baz: 2 Baz: 2
ChangeSetTest_EndChild:
endchild1:
Baz: 3
Qux: 3
ChangeSetTest_Mid: ChangeSetTest_Mid:
mid1: mid1:
Bar: 1 Bar: 1