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(),
// 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;
$withoutVersion = (!$previous || $previous->Version == $this->Version);
$withoutVersion = $this->getExtensionInstance('Versioned')->_nextWriteWithoutVersion;
foreach($linkedPages as $page) {
$page->copyFrom($page->CopyContentFrom());
if($withoutVersion) $page->writeWithoutVersion();

View File

@ -423,28 +423,48 @@ class VirtualPageTest extends SapphireTest {
function testWriteWithoutVersion() {
$original = new SiteTree();
$original->write();
// Create a second version (different behaviour),
// as SiteTree->onAfterWrite() checks for Version == 1
$original->Title = 'prepare';
$original->write();
$originalVersion = $original->Version;
$virtual = new VirtualPage();
$virtual->CopyContentFromID = $original->ID;
$virtual->write();
// Create a second version, see above.
$virtual->Title = 'prepare';
$virtual->write();
$virtualVersion = $virtual->Version;
$virtual->Title = 'changed 1';
$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->writeWithoutVersion();
DataObject::flush_and_destroy_cache();
$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->write();
DataObject::flush_and_destroy_cache();
$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'
);
}
}