BUGFIX Respect writeWithoutVersion() when updating VirtualPage relationships in SiteTree->onAfterWrite() (AIR-97)

This commit is contained in:
Ingo Schommer 2011-10-07 11:12:52 +02:00
parent 21d10ad32f
commit f98bc8480c
2 changed files with 37 additions and 4 deletions

View File

@ -1453,11 +1453,17 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
// Need to flush cache to avoid outdated versionnumber references // Need to flush cache to avoid outdated versionnumber references
$this->flushCache(); $this->flushCache();
// Update any virtual pages that might need updating
$linkedPages = $this->VirtualPages(); $linkedPages = $this->VirtualPages();
if($linkedPages) foreach($linkedPages as $page) { if($linkedPages) {
// 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);
foreach($linkedPages as $page) {
$page->copyFrom($page->CopyContentFrom()); $page->copyFrom($page->CopyContentFrom());
$page->write(); if($withoutVersion) $page->writeWithoutVersion();
else $page->write();
}
} }
parent::onAfterWrite(); parent::onAfterWrite();

View File

@ -419,6 +419,33 @@ class VirtualPageTest extends SapphireTest {
VirtualPage::$initially_copied_fields = $origInitiallyCopiedFields; VirtualPage::$initially_copied_fields = $origInitiallyCopiedFields;
VirtualPage::$non_virtual_fields = $origNonVirtualField; VirtualPage::$non_virtual_fields = $origNonVirtualField;
} }
function testWriteWithoutVersion() {
$original = new SiteTree();
$original->write();
$originalVersion = $original->Version;
$virtual = new VirtualPage();
$virtual->CopyContentFromID = $original->ID;
$virtual->write();
$virtualVersion = $virtual->Version;
$virtual->Title = 'changed 1';
$virtual->writeWithoutVersion();
$this->assertEquals($virtual->Version, $virtualVersion, 'Explicit write');
$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');
$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');
}
} }
class VirtualPageTest_ClassA extends Page implements TestOnly { class VirtualPageTest_ClassA extends Page implements TestOnly {