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;
}
return $link;
} }
Versioned::reading_stage($oldStage);
return $link;
} }
/** /**

View File

@ -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/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'); $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);