From 8a514d8b64a3a16f5fcbd40e6cf46f00ac973f79 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 28 Aug 2012 19:00:42 +0200 Subject: [PATCH] BUG Correct live state in SiteTree->getAbsoluteLiveLink() Was fetching the record from live (and its direct URLSegment), but all of its parents from the current stage, which might be draft, leading to "mixed" draft/live nested URLs which might no longer be reachable in live mode. --- code/model/SiteTree.php | 16 ++++++++-------- tests/model/SiteTreeTest.php | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/code/model/SiteTree.php b/code/model/SiteTree.php index b4a25ef7..178522d9 100644 --- a/code/model/SiteTree.php +++ b/code/model/SiteTree.php @@ -455,18 +455,18 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid * Get the absolute URL for this page on the Live site. */ public function getAbsoluteLiveLink($includeStageEqualsLive = true) { + $oldStage = Versioned::current_stage(); + Versioned::reading_stage('Live'); $live = Versioned::get_one_by_stage('SiteTree', 'Live', '"SiteTree"."ID" = ' . $this->ID); - if($live) { $link = $live->AbsoluteLink(); - - if($includeStageEqualsLive) { - $link .= '?stage=Live'; - } - - return $link; - + if($includeStageEqualsLive) $link .= '?stage=Live'; + } else { + $link = null; } + + Versioned::reading_stage($oldStage); + return $link; } /** diff --git a/tests/model/SiteTreeTest.php b/tests/model/SiteTreeTest.php index f8134aa3..c8a74551 100644 --- a/tests/model/SiteTreeTest.php +++ b/tests/model/SiteTreeTest.php @@ -356,6 +356,23 @@ class SiteTreeTest extends SapphireTest { $this->assertEquals('about-us/edit', $about->RelativeLink('edit'), 'Matches URLSegment plus parameter on top level'); $this->assertEquals('about-us/tom&jerry', $about->RelativeLink('tom&jerry'), 'Doesnt url encode parameter'); } + + function testAbsoluteLiveLink() { + $parent = $this->objFromFixture('Page', 'about'); + $child = $this->objFromFixture('Page', 'staff'); + + SiteTree::enable_nested_urls(); + + $child->publish('Stage', 'Live'); + $parent->URLSegment = 'changed-on-live'; + $parent->write(); + $parent->publish('Stage', 'Live'); + $parent->URLSegment = 'changed-on-draft'; + $parent->write(); + + $this->assertStringEndsWith('changed-on-live/my-staff/', $child->getAbsoluteLiveLink(false)); + $this->assertStringEndsWith('changed-on-live/my-staff/?stage=Live', $child->getAbsoluteLiveLink()); + } function testDeleteFromStageOperatesRecursively() { SiteTree::set_enforce_strict_hierarchy(false);