2016-03-30 23:37:12 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\Versioning\ChangeSetItem;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-03-30 23:37:12 +02:00
|
|
|
|
|
|
|
class ChangeSetItemTest extends SapphireTest {
|
|
|
|
|
|
|
|
protected $extraDataObjects = [
|
2016-10-14 03:30:05 +02:00
|
|
|
ChangeSetItemTest\VersionedObject::class
|
2016-03-30 23:37:12 +02:00
|
|
|
];
|
|
|
|
|
2016-10-12 01:19:07 +02:00
|
|
|
public function testChangeType() {
|
|
|
|
$this->logInWithPermission('ADMIN');
|
2016-10-14 03:30:05 +02:00
|
|
|
$object = new ChangeSetItemTest\VersionedObject(['Foo' => 1]);
|
2016-03-30 23:37:12 +02:00
|
|
|
$object->write();
|
|
|
|
|
|
|
|
$item = new ChangeSetItem([
|
|
|
|
'ObjectID' => $object->ID,
|
2016-05-25 07:09:29 +02:00
|
|
|
'ObjectClass' => $object->baseClass(),
|
2016-03-30 23:37:12 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
ChangeSetItem::CHANGE_CREATED, $item->ChangeType,
|
|
|
|
'New objects that aren\'t yet published should return created'
|
|
|
|
);
|
|
|
|
|
2016-04-01 05:27:59 +02:00
|
|
|
$object->publishRecursive();
|
2016-03-30 23:37:12 +02:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
ChangeSetItem::CHANGE_NONE, $item->ChangeType,
|
|
|
|
'Objects that have just been published should return no change'
|
|
|
|
);
|
|
|
|
|
|
|
|
$object->Foo += 1;
|
|
|
|
$object->write();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
ChangeSetItem::CHANGE_MODIFIED, $item->ChangeType,
|
|
|
|
'Object that have unpublished changes written to draft should show as modified'
|
|
|
|
);
|
|
|
|
|
2016-04-01 05:27:59 +02:00
|
|
|
$object->publishRecursive();
|
2016-03-30 23:37:12 +02:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
ChangeSetItem::CHANGE_NONE, $item->ChangeType,
|
|
|
|
'Objects that have just been published should return no change'
|
|
|
|
);
|
|
|
|
|
|
|
|
// We need to use a copy, because ID is set to 0 by delete, causing the following unpublish to fail
|
|
|
|
$objectCopy = clone $object; $objectCopy->delete();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
ChangeSetItem::CHANGE_DELETED, $item->ChangeType,
|
|
|
|
'Objects that have been deleted from draft (but not yet unpublished) should show as deleted'
|
|
|
|
);
|
|
|
|
|
|
|
|
$object->doUnpublish();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
ChangeSetItem::CHANGE_NONE, $item->ChangeType,
|
|
|
|
'Objects that have been deleted and then unpublished should return no change'
|
|
|
|
);
|
|
|
|
}
|
2016-04-11 22:54:38 +02:00
|
|
|
|
2016-10-12 01:19:07 +02:00
|
|
|
public function testGetForObject() {
|
|
|
|
$this->logInWithPermission('ADMIN');
|
2016-10-14 03:30:05 +02:00
|
|
|
$object = new ChangeSetItemTest\VersionedObject(['Foo' => 1]);
|
2016-04-11 22:54:38 +02:00
|
|
|
$object->write();
|
|
|
|
|
|
|
|
$item = new ChangeSetItem([
|
|
|
|
'ObjectID' => $object->ID,
|
2016-05-25 07:09:29 +02:00
|
|
|
'ObjectClass' => $object->baseClass(),
|
2016-04-11 22:54:38 +02:00
|
|
|
]);
|
|
|
|
$item->write();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-10-14 03:30:05 +02:00
|
|
|
ChangeSetItemTest\VersionedObject::get()->byID($object->ID)->toMap(),
|
2016-04-11 22:54:38 +02:00
|
|
|
ChangeSetItem::get_for_object($object)->first()->Object()->toMap()
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-10-14 03:30:05 +02:00
|
|
|
ChangeSetItemTest\VersionedObject::get()->byID($object->ID)->toMap(),
|
2016-04-11 22:54:38 +02:00
|
|
|
ChangeSetItem::get_for_object_by_id($object->ID, $object->ClassName)->first()->Object()->toMap()
|
|
|
|
);
|
|
|
|
}
|
2016-03-30 23:37:12 +02:00
|
|
|
}
|