Permalink
Browse files

BUGFIX Respect writeWithoutVersion() when updating VirtualPage relati…

…onships in SiteTree->onAfterWrite() (AIR-97)
  • Loading branch information...
1 parent 21d10ad commit f98bc8480c23acbf5bb1bb93072aec15ccb94ebb @chillu chillu committed Oct 7, 2011
Showing with 37 additions and 4 deletions.
  1. +10 −4 code/model/SiteTree.php
  2. +27 −0 tests/model/VirtualPageTest.php
View
@@ -1453,11 +1453,17 @@ function onAfterWrite() {
// Need to flush cache to avoid outdated versionnumber references
$this->flushCache();
- // Update any virtual pages that might need updating
$linkedPages = $this->VirtualPages();
- if($linkedPages) foreach($linkedPages as $page) {
- $page->copyFrom($page->CopyContentFrom());
- $page->write();
+ 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());
+ if($withoutVersion) $page->writeWithoutVersion();
+ else $page->write();
+ }
}
parent::onAfterWrite();
@@ -419,6 +419,33 @@ function testCopyFrom() {
VirtualPage::$initially_copied_fields = $origInitiallyCopiedFields;
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 {

0 comments on commit f98bc84

Please sign in to comment.