Permalink
Browse files

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
  • Loading branch information...
1 parent 7fc63de commit fd8e0044c40a233585be8445469d631c1f293bca @sminnee sminnee committed Oct 13, 2010
Showing with 47 additions and 6 deletions.
  1. +3 −1 core/model/SiteTree.php
  2. +7 −2 core/model/Versioned.php
  3. +37 −3 tests/model/VersionedTest.php
View
@@ -2115,7 +2115,9 @@ function doUnpublish() {
}
/**
- * 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
@@ -387,7 +387,7 @@ function augmentWrite(&$manipulation) {
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 @@ function publish($fromStage, $toStage, $createNewVersion = false) {
$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");
@@ -15,7 +15,7 @@ function testForceChangeUpdatesVersion() {
$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 @@ function testForceChangeUpdatesVersion() {
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 @@ function testVersionedFieldsAdded() {
$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 {

0 comments on commit fd8e004

Please sign in to comment.