Skip to content
This repository
Browse code

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.
  • Loading branch information...
commit 8a514d8b64a3a16f5fcbd40e6cf46f00ac973f79 1 parent 24b0e5d
Ingo Schommer authored August 28, 2012
16  code/model/SiteTree.php
@@ -455,18 +455,18 @@ public function RelativeLink($action = null) {
455 455
 	 * Get the absolute URL for this page on the Live site.
456 456
 	 */
457 457
 	public function getAbsoluteLiveLink($includeStageEqualsLive = true) {
  458
+		$oldStage = Versioned::current_stage();
  459
+		Versioned::reading_stage('Live');
458 460
 		$live = Versioned::get_one_by_stage('SiteTree', 'Live', '"SiteTree"."ID" = ' . $this->ID);
459  
-		
460 461
 		if($live) {
461 462
 			$link = $live->AbsoluteLink();
462  
-			
463  
-			if($includeStageEqualsLive) {
464  
-				$link .= '?stage=Live';
465  
-			}
466  
-			
467  
-			return $link;
468  
-			
  463
+			if($includeStageEqualsLive) $link .= '?stage=Live';
  464
+		} else {
  465
+			$link = null;
469 466
 		}
  467
+
  468
+		Versioned::reading_stage($oldStage);
  469
+		return $link;
470 470
 	}
471 471
 	
472 472
 	/**
17  tests/model/SiteTreeTest.php
@@ -356,6 +356,23 @@ function testRelativeLink() {
356 356
 		$this->assertEquals('about-us/edit', $about->RelativeLink('edit'), 'Matches URLSegment plus parameter on top level');
357 357
 		$this->assertEquals('about-us/tom&jerry', $about->RelativeLink('tom&jerry'), 'Doesnt url encode parameter');
358 358
 	}
  359
+
  360
+	function testAbsoluteLiveLink() {
  361
+		$parent = $this->objFromFixture('Page', 'about');
  362
+		$child = $this->objFromFixture('Page', 'staff');
  363
+
  364
+		SiteTree::enable_nested_urls();		
  365
+
  366
+		$child->publish('Stage', 'Live');
  367
+		$parent->URLSegment = 'changed-on-live';
  368
+		$parent->write();
  369
+		$parent->publish('Stage', 'Live');
  370
+		$parent->URLSegment = 'changed-on-draft';
  371
+		$parent->write();
  372
+		
  373
+		$this->assertStringEndsWith('changed-on-live/my-staff/', $child->getAbsoluteLiveLink(false));
  374
+		$this->assertStringEndsWith('changed-on-live/my-staff/?stage=Live', $child->getAbsoluteLiveLink());
  375
+	}
359 376
 	
360 377
 	function testDeleteFromStageOperatesRecursively() {
361 378
 		SiteTree::set_enforce_strict_hierarchy(false);

0 notes on commit 8a514d8

Please sign in to comment.
Something went wrong with that request. Please try again.