2016-02-25 05:32:41 +01:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
|
|
|
|
2016-10-12 01:19:07 +02:00
|
|
|
use SilverStripe\ORM\Versioning\ChangeSet;
|
|
|
|
use SilverStripe\ORM\Versioning\ChangeSetItem;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-10-14 03:30:05 +02:00
|
|
|
use DateTime;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-02-25 05:32:41 +01:00
|
|
|
/**
|
|
|
|
* Tests ownership API of versioned DataObjects
|
|
|
|
*/
|
|
|
|
class VersionedOwnershipTest extends SapphireTest {
|
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
2016-10-14 03:30:05 +02:00
|
|
|
VersionedOwnershipTest\TestObject::class,
|
|
|
|
VersionedOwnershipTest\Subclass::class,
|
|
|
|
VersionedOwnershipTest\Related::class,
|
|
|
|
VersionedOwnershipTest\Attachment::class,
|
|
|
|
VersionedOwnershipTest\RelatedMany::class,
|
|
|
|
VersionedOwnershipTest\TestPage::class,
|
|
|
|
VersionedOwnershipTest\Banner::class,
|
|
|
|
VersionedOwnershipTest\Image::class,
|
|
|
|
VersionedOwnershipTest\CustomRelation::class,
|
2016-02-25 05:32:41 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
protected static $fixture_file = 'VersionedOwnershipTest.yml';
|
|
|
|
|
2016-03-22 02:39:25 +01:00
|
|
|
public function setUp() {
|
2016-02-25 05:32:41 +01:00
|
|
|
parent::setUp();
|
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2016-02-25 05:32:41 +01:00
|
|
|
|
|
|
|
// Automatically publish any object named *_published
|
|
|
|
foreach($this->getFixtureFactory()->getFixtures() as $class => $fixtures) {
|
|
|
|
foreach($fixtures as $name => $id) {
|
|
|
|
if(stripos($name, '_published') !== false) {
|
|
|
|
/** @var Versioned|DataObject $object */
|
|
|
|
$object = DataObject::get($class)->byID($id);
|
2016-04-01 05:27:59 +02:00
|
|
|
$object->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2016-02-25 05:32:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
/**
|
2016-06-15 06:03:16 +02:00
|
|
|
* Virtual "sleep" that doesn't actually slow execution, only advances DBDateTime::now()
|
2016-03-02 06:18:10 +01:00
|
|
|
*
|
|
|
|
* @param int $minutes
|
|
|
|
*/
|
|
|
|
protected function sleep($minutes) {
|
2016-06-15 06:03:16 +02:00
|
|
|
$now = DBDatetime::now();
|
2016-03-02 06:18:10 +01:00
|
|
|
$date = DateTime::createFromFormat('Y-m-d H:i:s', $now->getValue());
|
|
|
|
$date->modify("+{$minutes} minutes");
|
2016-06-15 06:03:16 +02:00
|
|
|
DBDatetime::set_mock_now($date->format('Y-m-d H:i:s'));
|
2016-03-02 06:18:10 +01:00
|
|
|
}
|
|
|
|
|
2016-02-25 05:32:41 +01:00
|
|
|
/**
|
|
|
|
* Test basic findOwned() in stage mode
|
|
|
|
*/
|
|
|
|
public function testFindOwned() {
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\Subclass $subclass1 */
|
|
|
|
$subclass1 = $this->objFromFixture(VersionedOwnershipTest\Subclass::class, 'subclass1_published');
|
2016-02-25 05:32:41 +01:00
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Related 1'],
|
|
|
|
['Title' => 'Attachment 1'],
|
|
|
|
['Title' => 'Attachment 2'],
|
|
|
|
['Title' => 'Attachment 5'],
|
2016-03-02 06:18:10 +01:00
|
|
|
['Title' => 'Related Many 1'],
|
|
|
|
['Title' => 'Related Many 2'],
|
|
|
|
['Title' => 'Related Many 3'],
|
2016-02-25 05:32:41 +01:00
|
|
|
],
|
|
|
|
$subclass1->findOwned()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Non-recursive search
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Related 1'],
|
2016-03-02 06:18:10 +01:00
|
|
|
['Title' => 'Related Many 1'],
|
|
|
|
['Title' => 'Related Many 2'],
|
|
|
|
['Title' => 'Related Many 3'],
|
2016-02-25 05:32:41 +01:00
|
|
|
],
|
|
|
|
$subclass1->findOwned(false)
|
|
|
|
);
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\Subclass $subclass2 */
|
|
|
|
$subclass2 = $this->objFromFixture(VersionedOwnershipTest\Subclass::class, 'subclass2_published');
|
2016-02-25 05:32:41 +01:00
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Related 2'],
|
|
|
|
['Title' => 'Attachment 3'],
|
|
|
|
['Title' => 'Attachment 4'],
|
|
|
|
['Title' => 'Attachment 5'],
|
2016-03-02 06:18:10 +01:00
|
|
|
['Title' => 'Related Many 4'],
|
2016-02-25 05:32:41 +01:00
|
|
|
],
|
|
|
|
$subclass2->findOwned()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Non-recursive search
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
2016-03-02 06:18:10 +01:00
|
|
|
['Title' => 'Related 2'],
|
|
|
|
['Title' => 'Related Many 4'],
|
2016-02-25 05:32:41 +01:00
|
|
|
],
|
|
|
|
$subclass2->findOwned(false)
|
|
|
|
);
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\Related $related1 */
|
|
|
|
$related1 = $this->objFromFixture(VersionedOwnershipTest\Related::class, 'related1');
|
2016-02-25 05:32:41 +01:00
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Attachment 1'],
|
|
|
|
['Title' => 'Attachment 2'],
|
|
|
|
['Title' => 'Attachment 5'],
|
|
|
|
],
|
|
|
|
$related1->findOwned()
|
|
|
|
);
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\Related $related2 */
|
|
|
|
$related2 = $this->objFromFixture(VersionedOwnershipTest\Related::class, 'related2_published');
|
2016-02-25 05:32:41 +01:00
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Attachment 3'],
|
|
|
|
['Title' => 'Attachment 4'],
|
|
|
|
['Title' => 'Attachment 5'],
|
|
|
|
],
|
|
|
|
$related2->findOwned()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test findOwners
|
|
|
|
*/
|
|
|
|
public function testFindOwners() {
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\Attachment $attachment1 */
|
|
|
|
$attachment1 = $this->objFromFixture(VersionedOwnershipTest\Attachment::class, 'attachment1');
|
2016-02-25 05:32:41 +01:00
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Related 1'],
|
|
|
|
['Title' => 'Subclass 1'],
|
|
|
|
],
|
|
|
|
$attachment1->findOwners()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Non-recursive search
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Related 1'],
|
|
|
|
],
|
|
|
|
$attachment1->findOwners(false)
|
|
|
|
);
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\Attachment $attachment5 */
|
|
|
|
$attachment5 = $this->objFromFixture(VersionedOwnershipTest\Attachment::class, 'attachment5_published');
|
2016-02-25 05:32:41 +01:00
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Related 1'],
|
|
|
|
['Title' => 'Related 2'],
|
|
|
|
['Title' => 'Subclass 1'],
|
|
|
|
['Title' => 'Subclass 2'],
|
|
|
|
],
|
|
|
|
$attachment5->findOwners()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Non-recursive
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Related 1'],
|
|
|
|
['Title' => 'Related 2'],
|
|
|
|
],
|
|
|
|
$attachment5->findOwners(false)
|
|
|
|
);
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\Related $related1 */
|
|
|
|
$related1 = $this->objFromFixture(VersionedOwnershipTest\Related::class, 'related1');
|
2016-02-25 05:32:41 +01:00
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Subclass 1'],
|
|
|
|
],
|
|
|
|
$related1->findOwners()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test findOwners on Live stage
|
|
|
|
*/
|
|
|
|
public function testFindOwnersLive() {
|
|
|
|
// Modify a few records on stage
|
2016-10-14 03:30:05 +02:00
|
|
|
$related2 = $this->objFromFixture(VersionedOwnershipTest\Related::class, 'related2_published');
|
2016-02-25 05:32:41 +01:00
|
|
|
$related2->Title .= ' Modified';
|
|
|
|
$related2->write();
|
2016-10-14 03:30:05 +02:00
|
|
|
$attachment3 = $this->objFromFixture(VersionedOwnershipTest\Attachment::class, 'attachment3_published');
|
2016-02-25 05:32:41 +01:00
|
|
|
$attachment3->Title .= ' Modified';
|
|
|
|
$attachment3->write();
|
2016-10-14 03:30:05 +02:00
|
|
|
$attachment4 = $this->objFromFixture(VersionedOwnershipTest\Attachment::class, 'attachment4_published');
|
2016-02-25 05:32:41 +01:00
|
|
|
$attachment4->delete();
|
2016-10-14 03:30:05 +02:00
|
|
|
$subclass2ID = $this->idFromFixture(VersionedOwnershipTest\Subclass::class, 'subclass2_published');
|
2016-02-25 05:32:41 +01:00
|
|
|
|
|
|
|
// Check that stage record is ok
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\Subclass $subclass2Stage */
|
|
|
|
$subclass2Stage = Versioned::get_by_stage(VersionedOwnershipTest\Subclass::class, 'Stage')->byID($subclass2ID);
|
2016-02-25 05:32:41 +01:00
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Related 2 Modified'],
|
|
|
|
['Title' => 'Attachment 3 Modified'],
|
|
|
|
['Title' => 'Attachment 5'],
|
2016-03-02 06:18:10 +01:00
|
|
|
['Title' => 'Related Many 4'],
|
2016-02-25 05:32:41 +01:00
|
|
|
],
|
|
|
|
$subclass2Stage->findOwned()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Non-recursive
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Related 2 Modified'],
|
2016-03-02 06:18:10 +01:00
|
|
|
['Title' => 'Related Many 4'],
|
2016-02-25 05:32:41 +01:00
|
|
|
],
|
|
|
|
$subclass2Stage->findOwned(false)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Live records are unchanged
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\Subclass $subclass2Live */
|
|
|
|
$subclass2Live = Versioned::get_by_stage(VersionedOwnershipTest\Subclass::class, 'Live')->byID($subclass2ID);
|
2016-02-25 05:32:41 +01:00
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Related 2'],
|
|
|
|
['Title' => 'Attachment 3'],
|
|
|
|
['Title' => 'Attachment 4'],
|
|
|
|
['Title' => 'Attachment 5'],
|
2016-03-02 06:18:10 +01:00
|
|
|
['Title' => 'Related Many 4'],
|
2016-02-25 05:32:41 +01:00
|
|
|
],
|
|
|
|
$subclass2Live->findOwned()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test non-recursive
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Related 2'],
|
2016-03-02 06:18:10 +01:00
|
|
|
['Title' => 'Related Many 4'],
|
2016-02-25 05:32:41 +01:00
|
|
|
],
|
|
|
|
$subclass2Live->findOwned(false)
|
|
|
|
);
|
|
|
|
}
|
2016-03-02 06:18:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that objects are correctly published recursively
|
|
|
|
*/
|
|
|
|
public function testRecursivePublish() {
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\Subclass $parent */
|
|
|
|
$parent = $this->objFromFixture(VersionedOwnershipTest\Subclass::class, 'subclass1_published');
|
2016-03-02 06:18:10 +01:00
|
|
|
$parentID = $parent->ID;
|
2016-10-14 03:30:05 +02:00
|
|
|
$banner1 = $this->objFromFixture(VersionedOwnershipTest\RelatedMany::class, 'relatedmany1_published');
|
|
|
|
$banner2 = $this->objFromFixture(VersionedOwnershipTest\RelatedMany::class, 'relatedmany2_published');
|
2016-03-02 06:18:10 +01:00
|
|
|
$banner2ID = $banner2->ID;
|
|
|
|
|
|
|
|
// Modify, Add, and Delete banners on stage
|
|
|
|
$banner1->Title = 'Renamed Banner 1';
|
|
|
|
$banner1->write();
|
|
|
|
|
|
|
|
$banner2->delete();
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$banner4 = new VersionedOwnershipTest\RelatedMany();
|
2016-03-02 06:18:10 +01:00
|
|
|
$banner4->Title = 'New Banner';
|
|
|
|
$parent->Banners()->add($banner4);
|
|
|
|
|
|
|
|
// Check state of objects before publish
|
|
|
|
$oldLiveBanners = [
|
|
|
|
['Title' => 'Related Many 1'],
|
2016-10-12 01:19:07 +02:00
|
|
|
['Title' => 'Related Many 2'], // Will be unlinked (but not deleted)
|
2016-03-02 06:18:10 +01:00
|
|
|
// `Related Many 3` isn't published
|
|
|
|
];
|
|
|
|
$newBanners = [
|
|
|
|
['Title' => 'Renamed Banner 1'], // Renamed
|
|
|
|
['Title' => 'Related Many 3'], // Published without changes
|
|
|
|
['Title' => 'New Banner'], // Created
|
|
|
|
];
|
2016-10-14 03:30:05 +02:00
|
|
|
$parentDraft = Versioned::get_by_stage(VersionedOwnershipTest\Subclass::class, Versioned::DRAFT)
|
2016-03-02 06:18:10 +01:00
|
|
|
->byID($parentID);
|
|
|
|
$this->assertDOSEquals($newBanners, $parentDraft->Banners());
|
2016-10-14 03:30:05 +02:00
|
|
|
$parentLive = Versioned::get_by_stage(VersionedOwnershipTest\Subclass::class, Versioned::LIVE)
|
2016-03-02 06:18:10 +01:00
|
|
|
->byID($parentID);
|
|
|
|
$this->assertDOSEquals($oldLiveBanners, $parentLive->Banners());
|
|
|
|
|
|
|
|
// On publishing of owner, all children should now be updated
|
2016-10-12 01:19:07 +02:00
|
|
|
$now = DBDatetime::now();
|
|
|
|
DBDatetime::set_mock_now($now); // Lock 'now' to predictable time
|
2016-04-01 05:27:59 +02:00
|
|
|
$parent->publishRecursive();
|
2016-03-02 06:18:10 +01:00
|
|
|
|
|
|
|
// Now check each object has the correct state
|
2016-10-14 03:30:05 +02:00
|
|
|
$parentDraft = Versioned::get_by_stage(VersionedOwnershipTest\Subclass::class, Versioned::DRAFT)
|
2016-03-02 06:18:10 +01:00
|
|
|
->byID($parentID);
|
|
|
|
$this->assertDOSEquals($newBanners, $parentDraft->Banners());
|
2016-10-14 03:30:05 +02:00
|
|
|
$parentLive = Versioned::get_by_stage(VersionedOwnershipTest\Subclass::class, Versioned::LIVE)
|
2016-03-02 06:18:10 +01:00
|
|
|
->byID($parentID);
|
|
|
|
$this->assertDOSEquals($newBanners, $parentLive->Banners());
|
|
|
|
|
|
|
|
// Check that the deleted banner hasn't actually been deleted from the live stage,
|
|
|
|
// but in fact has been unlinked.
|
2016-10-14 03:30:05 +02:00
|
|
|
$banner2Live = Versioned::get_by_stage(VersionedOwnershipTest\RelatedMany::class, Versioned::LIVE)
|
2016-03-02 06:18:10 +01:00
|
|
|
->byID($banner2ID);
|
|
|
|
$this->assertEmpty($banner2Live->PageID);
|
2016-10-12 01:19:07 +02:00
|
|
|
|
|
|
|
// Test that a changeset was created
|
|
|
|
/** @var ChangeSet $changeset */
|
|
|
|
$changeset = ChangeSet::get()->sort('"ChangeSet"."ID" DESC')->first();
|
|
|
|
$this->assertNotEmpty($changeset);
|
|
|
|
|
|
|
|
// Test that this changeset is inferred
|
|
|
|
$this->assertTrue((bool)$changeset->IsInferred);
|
|
|
|
$this->assertEquals(
|
|
|
|
"Generated by publish of 'Subclass 1' at ".$now->Nice(),
|
|
|
|
$changeset->getTitle()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test that this changeset contains all items
|
|
|
|
$this->assertDOSContains(
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'ObjectID' => $parent->ID,
|
|
|
|
'ObjectClass' => $parent->baseClass(),
|
|
|
|
'Added' => ChangeSetItem::EXPLICITLY
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'ObjectID' => $banner1->ID,
|
|
|
|
'ObjectClass' => $banner1->baseClass(),
|
|
|
|
'Added' => ChangeSetItem::IMPLICITLY
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'ObjectID' => $banner4->ID,
|
|
|
|
'ObjectClass' => $banner4->baseClass(),
|
|
|
|
'Added' => ChangeSetItem::IMPLICITLY
|
|
|
|
]
|
|
|
|
],
|
|
|
|
$changeset->Changes()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Objects that are unlinked should not need to be a part of the changeset
|
|
|
|
$this->assertNotDOSContains(
|
|
|
|
[[ 'ObjectID' => $banner2ID, 'ObjectClass' => $banner2->baseClass() ]],
|
|
|
|
$changeset->Changes()
|
|
|
|
);
|
2016-03-02 06:18:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that owning objects get unpublished as needed
|
|
|
|
*/
|
|
|
|
public function testRecursiveUnpublish() {
|
|
|
|
// Unsaved objects can't be unpublished
|
2016-10-14 03:30:05 +02:00
|
|
|
$unsaved = new VersionedOwnershipTest\Subclass();
|
2016-03-02 06:18:10 +01:00
|
|
|
$this->assertFalse($unsaved->doUnpublish());
|
|
|
|
|
|
|
|
// Draft-only objects can't be unpublished
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\RelatedMany $banner3Unpublished */
|
|
|
|
$banner3Unpublished = $this->objFromFixture(VersionedOwnershipTest\RelatedMany::class, 'relatedmany3');
|
2016-03-02 06:18:10 +01:00
|
|
|
$this->assertFalse($banner3Unpublished->doUnpublish());
|
|
|
|
|
|
|
|
// First test: mid-level unpublish; We expect that owners should be unpublished, but not
|
|
|
|
// owned objects, nor other siblings shared by the same owner.
|
2016-10-14 03:30:05 +02:00
|
|
|
$related2 = $this->objFromFixture(VersionedOwnershipTest\Related::class, 'related2_published');
|
|
|
|
/** @var VersionedOwnershipTest\Attachment $attachment3 */
|
|
|
|
$attachment3 = $this->objFromFixture(VersionedOwnershipTest\Attachment::class, 'attachment3_published');
|
|
|
|
/** @var VersionedOwnershipTest\RelatedMany $relatedMany4 */
|
|
|
|
$relatedMany4 = $this->objFromFixture(VersionedOwnershipTest\RelatedMany::class, 'relatedmany4_published');
|
|
|
|
/** @var VersionedOwnershipTest\Related $related2 */
|
2016-03-02 06:18:10 +01:00
|
|
|
$this->assertTrue($related2->doUnpublish());
|
2016-10-14 03:30:05 +02:00
|
|
|
$subclass2 = $this->objFromFixture(VersionedOwnershipTest\Subclass::class, 'subclass2_published');
|
2016-03-02 06:18:10 +01:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\Subclass $subclass2 */
|
2016-03-02 06:18:10 +01:00
|
|
|
$this->assertFalse($subclass2->isPublished()); // Owner IS unpublished
|
|
|
|
$this->assertTrue($attachment3->isPublished()); // Owned object is NOT unpublished
|
|
|
|
$this->assertTrue($relatedMany4->isPublished()); // Owned object by owner is NOT unpublished
|
|
|
|
|
|
|
|
// Second test: multi-level unpublish should recursively cascade down all owning objects
|
|
|
|
// Publish related2 again
|
2016-04-01 05:27:59 +02:00
|
|
|
$subclass2->publishRecursive();
|
2016-03-02 06:18:10 +01:00
|
|
|
$this->assertTrue($subclass2->isPublished());
|
|
|
|
$this->assertTrue($related2->isPublished());
|
|
|
|
$this->assertTrue($attachment3->isPublished());
|
|
|
|
|
|
|
|
// Unpublish leaf node
|
|
|
|
$this->assertTrue($attachment3->doUnpublish());
|
|
|
|
|
|
|
|
// Now all owning objects (only) are unpublished
|
|
|
|
$this->assertFalse($attachment3->isPublished()); // Unpublished because we just unpublished it
|
|
|
|
$this->assertFalse($related2->isPublished()); // Unpublished because it owns attachment3
|
|
|
|
$this->assertFalse($subclass2->isPublished()); // Unpublished ecause it owns related2
|
|
|
|
$this->assertTrue($relatedMany4->isPublished()); // Stays live because recursion only affects owners not owned.
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRecursiveArchive() {
|
|
|
|
// When archiving an object, any published owners should be unpublished at the same time
|
|
|
|
// but NOT achived
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\Attachment $attachment3 */
|
|
|
|
$attachment3 = $this->objFromFixture(VersionedOwnershipTest\Attachment::class, 'attachment3_published');
|
2016-03-02 06:18:10 +01:00
|
|
|
$attachment3ID = $attachment3->ID;
|
|
|
|
$this->assertTrue($attachment3->doArchive());
|
|
|
|
|
|
|
|
// This object is on neither stage nor live
|
2016-10-14 03:30:05 +02:00
|
|
|
$stageAttachment = Versioned::get_by_stage(VersionedOwnershipTest\Attachment::class, Versioned::DRAFT)
|
2016-03-02 06:18:10 +01:00
|
|
|
->byID($attachment3ID);
|
2016-10-14 03:30:05 +02:00
|
|
|
$liveAttachment = Versioned::get_by_stage(VersionedOwnershipTest\Attachment::class, Versioned::LIVE)
|
2016-03-02 06:18:10 +01:00
|
|
|
->byID($attachment3ID);
|
|
|
|
$this->assertEmpty($stageAttachment);
|
|
|
|
$this->assertEmpty($liveAttachment);
|
|
|
|
|
|
|
|
// Owning object is unpublished only
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\Related $stageOwner */
|
|
|
|
$stageOwner = $this->objFromFixture(VersionedOwnershipTest\Related::class, 'related2_published');
|
2016-03-02 06:18:10 +01:00
|
|
|
$this->assertTrue($stageOwner->isOnDraft());
|
|
|
|
$this->assertFalse($stageOwner->isPublished());
|
|
|
|
|
|
|
|
// Bottom level owning object is also unpublished
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\Subclass $stageTopOwner */
|
|
|
|
$stageTopOwner = $this->objFromFixture(VersionedOwnershipTest\Subclass::class, 'subclass2_published');
|
2016-03-02 06:18:10 +01:00
|
|
|
$this->assertTrue($stageTopOwner->isOnDraft());
|
|
|
|
$this->assertFalse($stageTopOwner->isPublished());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRecursiveRevertToLive() {
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\Subclass $parent */
|
|
|
|
$parent = $this->objFromFixture(VersionedOwnershipTest\Subclass::class, 'subclass1_published');
|
2016-03-02 06:18:10 +01:00
|
|
|
$parentID = $parent->ID;
|
2016-10-14 03:30:05 +02:00
|
|
|
$banner1 = $this->objFromFixture(VersionedOwnershipTest\RelatedMany::class, 'relatedmany1_published');
|
|
|
|
$banner2 = $this->objFromFixture(VersionedOwnershipTest\RelatedMany::class, 'relatedmany2_published');
|
2016-03-02 06:18:10 +01:00
|
|
|
$banner2ID = $banner2->ID;
|
|
|
|
|
|
|
|
// Modify, Add, and Delete banners on stage
|
|
|
|
$banner1->Title = 'Renamed Banner 1';
|
|
|
|
$banner1->write();
|
|
|
|
|
|
|
|
$banner2->delete();
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$banner4 = new VersionedOwnershipTest\RelatedMany();
|
2016-03-02 06:18:10 +01:00
|
|
|
$banner4->Title = 'New Banner';
|
|
|
|
$banner4->write();
|
|
|
|
$parent->Banners()->add($banner4);
|
|
|
|
|
|
|
|
// Check state of objects before publish
|
|
|
|
$liveBanners = [
|
|
|
|
['Title' => 'Related Many 1'],
|
|
|
|
['Title' => 'Related Many 2'],
|
|
|
|
];
|
|
|
|
$modifiedBanners = [
|
|
|
|
['Title' => 'Renamed Banner 1'], // Renamed
|
|
|
|
['Title' => 'Related Many 3'], // Published without changes
|
|
|
|
['Title' => 'New Banner'], // Created
|
|
|
|
];
|
2016-10-14 03:30:05 +02:00
|
|
|
$parentDraft = Versioned::get_by_stage(VersionedOwnershipTest\Subclass::class, Versioned::DRAFT)
|
2016-03-02 06:18:10 +01:00
|
|
|
->byID($parentID);
|
|
|
|
$this->assertDOSEquals($modifiedBanners, $parentDraft->Banners());
|
2016-10-14 03:30:05 +02:00
|
|
|
$parentLive = Versioned::get_by_stage(VersionedOwnershipTest\Subclass::class, Versioned::LIVE)
|
2016-03-02 06:18:10 +01:00
|
|
|
->byID($parentID);
|
|
|
|
$this->assertDOSEquals($liveBanners, $parentLive->Banners());
|
|
|
|
|
|
|
|
// When reverting parent, all records should be put back on stage
|
|
|
|
$this->assertTrue($parent->doRevertToLive());
|
|
|
|
|
|
|
|
// Now check each object has the correct state
|
2016-10-14 03:30:05 +02:00
|
|
|
$parentDraft = Versioned::get_by_stage(VersionedOwnershipTest\Subclass::class, Versioned::DRAFT)
|
2016-03-02 06:18:10 +01:00
|
|
|
->byID($parentID);
|
|
|
|
$this->assertDOSEquals($liveBanners, $parentDraft->Banners());
|
2016-10-14 03:30:05 +02:00
|
|
|
$parentLive = Versioned::get_by_stage(VersionedOwnershipTest\Subclass::class, Versioned::LIVE)
|
2016-03-02 06:18:10 +01:00
|
|
|
->byID($parentID);
|
|
|
|
$this->assertDOSEquals($liveBanners, $parentLive->Banners());
|
|
|
|
|
|
|
|
// Check that the newly created banner, even though it still exist, has been
|
|
|
|
// unlinked from the reverted draft record
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\RelatedMany $banner4Draft */
|
|
|
|
$banner4Draft = Versioned::get_by_stage(VersionedOwnershipTest\RelatedMany::class, Versioned::DRAFT)
|
2016-03-02 06:18:10 +01:00
|
|
|
->byID($banner4->ID);
|
|
|
|
$this->assertTrue($banner4Draft->isOnDraft());
|
|
|
|
$this->assertFalse($banner4Draft->isPublished());
|
|
|
|
$this->assertEmpty($banner4Draft->PageID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that rolling back to a single version works recursively
|
|
|
|
*/
|
|
|
|
public function testRecursiveRollback() {
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\Subclass $subclass2 */
|
2016-03-02 06:18:10 +01:00
|
|
|
$this->sleep(1);
|
2016-10-14 03:30:05 +02:00
|
|
|
$subclass2 = $this->objFromFixture(VersionedOwnershipTest\Subclass::class, 'subclass2_published');
|
2016-03-02 06:18:10 +01:00
|
|
|
|
|
|
|
// Create a few new versions
|
|
|
|
$versions = [];
|
|
|
|
for($version = 1; $version <= 3; $version++) {
|
|
|
|
// Write owned objects
|
|
|
|
$this->sleep(1);
|
|
|
|
foreach($subclass2->findOwned(true) as $obj) {
|
|
|
|
$obj->Title .= " - v{$version}";
|
|
|
|
$obj->write();
|
|
|
|
}
|
|
|
|
// Write parent
|
|
|
|
$this->sleep(1);
|
|
|
|
$subclass2->Title .= " - v{$version}";
|
|
|
|
$subclass2->write();
|
|
|
|
$versions[$version] = $subclass2->Version;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check reverting to first version
|
|
|
|
$this->sleep(1);
|
|
|
|
$subclass2->doRollbackTo($versions[1]);
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\Subclass $subclass2Draft */
|
|
|
|
$subclass2Draft = Versioned::get_by_stage(VersionedOwnershipTest\Subclass::class, Versioned::DRAFT)
|
2016-03-02 06:18:10 +01:00
|
|
|
->byID($subclass2->ID);
|
|
|
|
$this->assertEquals('Subclass 2 - v1', $subclass2Draft->Title);
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Related 2 - v1'],
|
|
|
|
['Title' => 'Attachment 3 - v1'],
|
|
|
|
['Title' => 'Attachment 4 - v1'],
|
|
|
|
['Title' => 'Attachment 5 - v1'],
|
|
|
|
['Title' => 'Related Many 4 - v1'],
|
|
|
|
],
|
|
|
|
$subclass2Draft->findOwned(true)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check rolling forward to a later version
|
|
|
|
$this->sleep(1);
|
|
|
|
$subclass2->doRollbackTo($versions[3]);
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\Subclass $subclass2Draft */
|
|
|
|
$subclass2Draft = Versioned::get_by_stage(VersionedOwnershipTest\Subclass::class, Versioned::DRAFT)
|
2016-03-02 06:18:10 +01:00
|
|
|
->byID($subclass2->ID);
|
|
|
|
$this->assertEquals('Subclass 2 - v1 - v2 - v3', $subclass2Draft->Title);
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Related 2 - v1 - v2 - v3'],
|
|
|
|
['Title' => 'Attachment 3 - v1 - v2 - v3'],
|
|
|
|
['Title' => 'Attachment 4 - v1 - v2 - v3'],
|
|
|
|
['Title' => 'Attachment 5 - v1 - v2 - v3'],
|
|
|
|
['Title' => 'Related Many 4 - v1 - v2 - v3'],
|
|
|
|
],
|
|
|
|
$subclass2Draft->findOwned(true)
|
|
|
|
);
|
|
|
|
|
|
|
|
// And rolling back one version
|
|
|
|
$this->sleep(1);
|
|
|
|
$subclass2->doRollbackTo($versions[2]);
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\Subclass $subclass2Draft */
|
|
|
|
$subclass2Draft = Versioned::get_by_stage(VersionedOwnershipTest\Subclass::class, Versioned::DRAFT)
|
2016-03-02 06:18:10 +01:00
|
|
|
->byID($subclass2->ID);
|
|
|
|
$this->assertEquals('Subclass 2 - v1 - v2', $subclass2Draft->Title);
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Related 2 - v1 - v2'],
|
|
|
|
['Title' => 'Attachment 3 - v1 - v2'],
|
|
|
|
['Title' => 'Attachment 4 - v1 - v2'],
|
|
|
|
['Title' => 'Attachment 5 - v1 - v2'],
|
|
|
|
['Title' => 'Related Many 4 - v1 - v2'],
|
|
|
|
],
|
|
|
|
$subclass2Draft->findOwned(true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-03-22 02:39:25 +01:00
|
|
|
/**
|
|
|
|
* Test that you can find owners without owned_by being defined explicitly
|
|
|
|
*/
|
|
|
|
public function testInferedOwners() {
|
|
|
|
// Make sure findOwned() works
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\TestPage $page1 */
|
|
|
|
$page1 = $this->objFromFixture(VersionedOwnershipTest\TestPage::class, 'page1_published');
|
|
|
|
/** @var VersionedOwnershipTest\TestPage $page2 */
|
|
|
|
$page2 = $this->objFromFixture(VersionedOwnershipTest\TestPage::class, 'page2_published');
|
2016-03-22 02:39:25 +01:00
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Banner 1'],
|
|
|
|
['Title' => 'Image 1'],
|
|
|
|
['Title' => 'Custom 1'],
|
|
|
|
],
|
|
|
|
$page1->findOwned()
|
|
|
|
);
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Banner 2'],
|
|
|
|
['Title' => 'Banner 3'],
|
|
|
|
['Title' => 'Image 1'],
|
|
|
|
['Title' => 'Image 2'],
|
|
|
|
['Title' => 'Custom 2'],
|
|
|
|
],
|
|
|
|
$page2->findOwned()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check that findOwners works
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\Image $image1 */
|
|
|
|
$image1 = $this->objFromFixture(VersionedOwnershipTest\Image::class, 'image1_published');
|
|
|
|
/** @var VersionedOwnershipTest\Image $image2 */
|
|
|
|
$image2 = $this->objFromFixture(VersionedOwnershipTest\Image::class, 'image2_published');
|
2016-03-22 02:39:25 +01:00
|
|
|
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Banner 1'],
|
|
|
|
['Title' => 'Banner 2'],
|
|
|
|
['Title' => 'Page 1'],
|
|
|
|
['Title' => 'Page 2'],
|
|
|
|
],
|
|
|
|
$image1->findOwners()
|
|
|
|
);
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Banner 1'],
|
|
|
|
['Title' => 'Banner 2'],
|
|
|
|
],
|
|
|
|
$image1->findOwners(false)
|
|
|
|
);
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Banner 3'],
|
|
|
|
['Title' => 'Page 2'],
|
|
|
|
],
|
|
|
|
$image2->findOwners()
|
|
|
|
);
|
|
|
|
$this->assertDOSEquals(
|
|
|
|
[
|
|
|
|
['Title' => 'Banner 3'],
|
|
|
|
],
|
|
|
|
$image2->findOwners(false)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test custom relation can findOwners()
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var VersionedOwnershipTest\CustomRelation $custom1 */
|
|
|
|
$custom1 = $this->objFromFixture(VersionedOwnershipTest\CustomRelation::class, 'custom1_published');
|
2016-03-22 02:39:25 +01:00
|
|
|
$this->assertDOSEquals(
|
|
|
|
[['Title' => 'Page 1']],
|
|
|
|
$custom1->findOwners()
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-25 05:32:41 +01:00
|
|
|
}
|