BUGFIX Don't create new version on related VirtualPage records in SiteTree->onAfterWrite() when the write was triggered through writeWithoutVersion(). Use a new $_nextWriteWithoutVersion flag replacing the -1 Version number for this purpose (AIR-97) (see similar commit in sapphire)

This commit is contained in:
Ingo Schommer 2012-01-14 11:11:21 +01:00
parent 2587a4abee
commit 87c318d168
2 changed files with 24 additions and 4 deletions

View File

@ -1448,7 +1448,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
// The only way after a write() call to determine if it was triggered by a writeWithoutVersion(), // The only way after a write() call to determine if it was triggered by a writeWithoutVersion(),
// which we have to pass on to the virtual page writes as well. // which we have to pass on to the virtual page writes as well.
$previous = ($this->Version > 1) ? Versioned::get_version($this->class, $this->ID, $this->Version-1) : null; $previous = ($this->Version > 1) ? Versioned::get_version($this->class, $this->ID, $this->Version-1) : null;
$withoutVersion = (!$previous || $previous->Version == $this->Version); $withoutVersion = $this->getExtensionInstance('Versioned')->_nextWriteWithoutVersion;
foreach($linkedPages as $page) { foreach($linkedPages as $page) {
$page->copyFrom($page->CopyContentFrom()); $page->copyFrom($page->CopyContentFrom());
if($withoutVersion) $page->writeWithoutVersion(); if($withoutVersion) $page->writeWithoutVersion();

View File

@ -423,28 +423,48 @@ class VirtualPageTest extends SapphireTest {
function testWriteWithoutVersion() { function testWriteWithoutVersion() {
$original = new SiteTree(); $original = new SiteTree();
$original->write(); $original->write();
// Create a second version (different behaviour),
// as SiteTree->onAfterWrite() checks for Version == 1
$original->Title = 'prepare';
$original->write();
$originalVersion = $original->Version; $originalVersion = $original->Version;
$virtual = new VirtualPage(); $virtual = new VirtualPage();
$virtual->CopyContentFromID = $original->ID; $virtual->CopyContentFromID = $original->ID;
$virtual->write(); $virtual->write();
// Create a second version, see above.
$virtual->Title = 'prepare';
$virtual->write();
$virtualVersion = $virtual->Version; $virtualVersion = $virtual->Version;
$virtual->Title = 'changed 1'; $virtual->Title = 'changed 1';
$virtual->writeWithoutVersion(); $virtual->writeWithoutVersion();
$this->assertEquals($virtual->Version, $virtualVersion, 'Explicit write'); $this->assertEquals(
$virtual->Version,
$virtualVersion,
'writeWithoutVersion() on VirtualPage doesnt increment version'
);
$original->Title = 'changed 2'; $original->Title = 'changed 2';
$original->writeWithoutVersion(); $original->writeWithoutVersion();
DataObject::flush_and_destroy_cache(); DataObject::flush_and_destroy_cache();
$virtual = DataObject::get_by_id('VirtualPage', $virtual->ID, false); $virtual = DataObject::get_by_id('VirtualPage', $virtual->ID, false);
$this->assertEquals($virtual->Version, $virtualVersion, 'Implicit write through original'); $this->assertEquals(
$virtual->Version,
$virtualVersion,
'writeWithoutVersion() on original page doesnt increment version on related VirtualPage'
);
$original->Title = 'changed 3'; $original->Title = 'changed 3';
$original->write(); $original->write();
DataObject::flush_and_destroy_cache(); DataObject::flush_and_destroy_cache();
$virtual = DataObject::get_by_id('VirtualPage', $virtual->ID, false); $virtual = DataObject::get_by_id('VirtualPage', $virtual->ID, false);
$this->assertGreaterThan($virtualVersion, $virtual->Version, 'Implicit write through original'); $this->assertGreaterThan(
$virtualVersion,
$virtual->Version,
'write() on original page does increment version on related VirtualPage'
);
} }
} }