BUGFIX Versioned->publish() with $createNewVersion=TRUE now increases version number of in-memory object (fixes #5261) (from r101739)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@112030 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2010-10-13 01:04:38 +00:00
parent 7fc63deda0
commit fd8e0044c4
3 changed files with 47 additions and 6 deletions

View File

@ -2115,7 +2115,9 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
}
/**
* Roll the draft version of this page to match the published page
* Roll the draft version of this page to match the published page.
* Caution: Doesn't overwrite the object properties with the rolled back version.
*
* @param $version Either the string 'Live' or a version number
*/
function doRollbackTo($version) {

View File

@ -387,7 +387,7 @@ class Versioned extends DataObjectDecorator {
unset($manipulation[$table]);
}
}
// Add the new version # back into the data object, for accessing after this write
if(isset($thisVersion)) $this->owner->Version = str_replace("'","",$thisVersion);
}
@ -476,7 +476,12 @@ class Versioned extends DataObjectDecorator {
$publisherID = isset(Member::currentUser()->ID) ? Member::currentUser()->ID : 0;
if($from) {
$from->forceChange();
if(!$createNewVersion) $from->migrateVersion($from->Version);
if($createNewVersion) {
$latest = self::get_latest_version($baseClass, $this->owner->ID);
$this->owner->Version = $latest->Version + 1;
} else {
$from->migrateVersion($from->Version);
}
// Mark this version as having been published at some stage
DB::query("UPDATE \"{$extTable}_versions\" SET \"WasPublished\" = '1', \"PublisherID\" = $publisherID WHERE \"RecordID\" = $from->ID AND \"Version\" = $from->Version");

View File

@ -15,7 +15,7 @@ class VersionedTest extends SapphireTest {
$oldVersion = $obj->Version;
$obj->forceChange();
$obj->write();
$this->assertTrue(
($obj->Version > $oldVersion),
"A object Version is increased when just calling forceChange() without any other changes"
@ -28,7 +28,7 @@ class VersionedTest extends SapphireTest {
function testGetIncludingDeleted() {
// Delete a page
$this->objFromFixture('Page', 'page3')->delete();
// Get all items, ignoring deleted
$remainingPages = DataObject::get("SiteTree", "\"ParentID\" = 0", "\"SiteTree\".\"ID\" ASC");
// Check that page 3 has gone
@ -51,12 +51,46 @@ class VersionedTest extends SapphireTest {
$obj = new VersionedTest_DataObject();
// Check that the Version column is added as a full-fledged column
$this->assertType('Int', $obj->dbObject('Version'));
$obj2 = new VersionedTest_Subclass();
// Check that the Version column is added as a full-fledged column
$this->assertType('Int', $obj2->dbObject('Version'));
}
function testPublishCreateNewVersion() {
$page1 = $this->objFromFixture('Page', 'page1');
$page1->Content = 'orig';
$page1->write();
$oldVersion = $page1->Version;
$page1->publish('Stage', 'Live', false);
$this->assertEquals($oldVersion, $page1->Version, 'publish() with $createNewVersion=FALSE');
$page1->Content = 'changed';
$page1->write();
$oldVersion = $page1->Version;
$page1->publish('Stage', 'Live', true);
$this->assertTrue($oldVersion < $page1->Version, 'publish() with $createNewVersion=TRUE');
}
function testRollbackTo() {
$page1 = $this->objFromFixture('Page', 'page1');
$page1->Content = 'orig';
$page1->write();
$page1->publish('Stage', 'Live');
$origVersion = $page1->Version;
$page1->Content = 'changed';
$page1->write();
$page1->publish('Stage', 'Live');
$changedVersion = $page1->Version;
$page1->doRollbackTo($origVersion);
$page1 = Versioned::get_one_by_stage('Page', 'Stage', $page1->ID);
$this->assertTrue($page1->Version > $changedVersion, 'Create a new higher version number');
$this->assertEquals('orig', $page1->Content, 'Copies the content from the old version');
}
}
class VersionedTest_DataObject extends DataObject implements TestOnly {