diff --git a/core/model/VirtualPage.php b/core/model/VirtualPage.php index f2b436f17..2ba154f26 100755 --- a/core/model/VirtualPage.php +++ b/core/model/VirtualPage.php @@ -115,7 +115,7 @@ class VirtualPage extends Page { ) { $source = DataObject::get_one("SiteTree",sprintf('"SiteTree"."ID" = %d', $this->CopyContentFromID)); if($source) { - $this->copyFrom($source); + $this->copyFrom($source, false); $this->URLSegment = $source->URLSegment . '-' . $this->ID; } } @@ -124,10 +124,25 @@ class VirtualPage extends Page { parent::onBeforeWrite(); } + function onAfterWrite() { + parent::onAfterWrite(); + + // Don't do this stuff when we're publishing + if(!$this->extension_instances['Versioned']->migratingVersion) { + if( + $this->isChanged('CopyContentFromID') + && $this->CopyContentFromID != 0 + && $this instanceof VirtualPage + ) { + $this->updateImageTracking(); + } + } + } + /** * Ensure we have an up-to-date version of everything. */ - function copyFrom($source) { + function copyFrom($source, $updateImageTracking = true) { if($source) { foreach($this->getVirtualFields() as $virtualField) { $this->$virtualField = $source->$virtualField; @@ -138,9 +153,22 @@ class VirtualPage extends Page { if($this->isChanged('CopyContentFromID')) { $this->ShowInMenus = $source->ShowInMenus; } + + if($updateImageTracking) $this->updateImageTracking(); } } + function updateImageTracking() { + // Doesn't work on unsaved records + if(!$this->ID) return; + + // Remove CopyContentFrom() from the cache + unset($this->components['CopyContentFrom']); + + // Update ImageTracking + $this->ImageTracking()->setByIdList($this->CopyContentFrom()->ImageTracking()->column('ID')); + } + /** * Allow attributes on the master page to pass * through to the virtual page diff --git a/tests/FileLinkTrackingTest.php b/tests/FileLinkTrackingTest.php index 59311d778..2b62211b7 100644 --- a/tests/FileLinkTrackingTest.php +++ b/tests/FileLinkTrackingTest.php @@ -34,7 +34,28 @@ class FileLinkTrackingTest extends SapphireTest { $this->assertContains('', DB::query("SELECT \"Content\" FROM \"SiteTree\" WHERE \"ID\" = $page->ID")->value()); } - + + function testFileLinkRewritingOnVirtualPages() { + // Publish the source page + $page = $this->objFromFixture('Page', 'page1'); + $this->assertTrue($page->doPublish()); + + // Create a virtual page from it, and publish that + $svp = new VirtualPage(); + $svp->CopyContentFromID = $page->ID; + $svp->write(); + $svp->doPublish(); + + // Rename the file + $file = $this->objFromFixture('File', 'file1'); + $file->Name = 'renamed-test-file.pdf'; + + // Verify that the draft and publish virtual pages both have the corrected link + $this->assertContains('', + DB::query("SELECT \"Content\" FROM \"SiteTree\" WHERE \"ID\" = $svp->ID")->value()); + $this->assertContains('', + DB::query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = $svp->ID")->value()); + } } ?>