Skip to content

Commit

Permalink
Merge branch '1.5' into 1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Aug 10, 2020
2 parents 6194fbc + fe896f8 commit 6aedf12
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Versioned.php
Original file line number Diff line number Diff line change
Expand Up @@ -1876,6 +1876,7 @@ public function doArchive()
*/
public function doUnpublish()
{
/** @var DataObject|Versioned $owner */
$owner = $this->owner;
// Skip if this record isn't saved
if (!$owner->isInDB()) {
Expand All @@ -1893,9 +1894,16 @@ public function doUnpublish()
static::withVersionedMode(function () use ($owner) {
static::set_stage(static::LIVE);

// This way our ID won't be unset
$clone = clone $owner;
$clone->delete();
// Re-fetch the current DataObject to ensure we have data from the LIVE stage
// This is particularly relevant for DataObject's in a modified state so that
// any delete extensions have the correct database record values
/** @var DataObject|Versioned $obj */
$obj = $owner::get()->byID($owner->ID);
if (!$obj) {
return;
}
$obj->setDeleteWritesVersion($owner->getDeleteWritesVersion());
$obj->delete();
});

$owner->invokeWithExtensions('onAfterUnpublish');
Expand Down
17 changes: 17 additions & 0 deletions tests/php/VersionedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1588,4 +1588,21 @@ public function testWriteWithoutVersion()
$versions
);
}

public function testLiveObjectDeletedOnUnpublish()
{
/** @var VersionedTest\TestObject|Versioned $obj */
$obj = new VersionedTest\TestObject();

// publish
$obj->Name = 'First name';
$obj->publishSingle();

// put into a modified state
$obj->Name = 'Second name';
$obj->write();

$obj->doUnpublish();
$this->assertEquals('First name', VersionedTest\TestObject::$nameValueOfObjectJustDeleted);
}
}
11 changes: 11 additions & 0 deletions tests/php/VersionedTest/TestObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class TestObject extends DataObject implements TestOnly
*/
public static $setNameWithoutVersionAfterPublish = null;

/**
* Used to record the $obj->Name value of the last object deleted
*/
public static $nameValueOfObjectJustDeleted = '';

public function canView($member = null)
{
$extended = $this->extendedCan(__FUNCTION__, $member);
Expand All @@ -74,4 +79,10 @@ public function onAfterPublish($original)
$this->writeWithoutVersion();
}
}

public function onAfterDelete()
{
parent::onAfterDelete();
self::$nameValueOfObjectJustDeleted = $this->Name;
}
}

0 comments on commit 6aedf12

Please sign in to comment.