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.
This commit is contained in:
Ingo Schommer 2012-08-28 19:00:42 +02:00
parent 24b0e5dbad
commit 8a514d8b64
2 changed files with 25 additions and 8 deletions

View File

@ -455,18 +455,18 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
* Get the absolute URL for this page on the Live site. * Get the absolute URL for this page on the Live site.
*/ */
public function getAbsoluteLiveLink($includeStageEqualsLive = true) { 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); $live = Versioned::get_one_by_stage('SiteTree', 'Live', '"SiteTree"."ID" = ' . $this->ID);
if($live) { if($live) {
$link = $live->AbsoluteLink(); $link = $live->AbsoluteLink();
if($includeStageEqualsLive) $link .= '?stage=Live';
if($includeStageEqualsLive) { } else {
$link .= '?stage=Live'; $link = null;
} }
Versioned::reading_stage($oldStage);
return $link; return $link;
}
} }
/** /**

View File

@ -357,6 +357,23 @@ class SiteTreeTest extends SapphireTest {
$this->assertEquals('about-us/tom&jerry', $about->RelativeLink('tom&jerry'), 'Doesnt url encode parameter'); $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() { function testDeleteFromStageOperatesRecursively() {
SiteTree::set_enforce_strict_hierarchy(false); SiteTree::set_enforce_strict_hierarchy(false);
$pageAbout = $this->objFromFixture('Page', 'about'); $pageAbout = $this->objFromFixture('Page', 'about');