Skip to content
This repository
Browse code

BUGFIX: ticket #5239 - Infinite loop: live subpage of draft page redi…

…rects onto itself when called just by url segment
  • Loading branch information...
commit f7b6f1de5aff8451ab6bb6bc6021036ce70bc0f9 1 parent abc87a4
carlos barberis authored July 18, 2012 hafriedlander committed July 23, 2012
9  code/controllers/ModelAsController.php
@@ -159,7 +159,11 @@ static function find_old_page($URLSegment,$parentID = 0, $ignoreNestedURLs = fal
159 159
 				'SiteTree', 
160 160
 				"\"URLSegment\" = '$URLSegment'" . ($useParentIDFilter ? ' AND "ParentID" = ' . (int)$parentID : '')
161 161
 			);
162  
-			if($pages && $pages->Count() == 1) return $pages->First();
  162
+
  163
+			if($pages && $pages->Count() == 1 && ($page = $pages->First())) {
  164
+				$parent = $page->ParentID ? $page->Parent() : $page;
  165
+				if($parent->isPublished()) return $page;
  166
+			}
163 167
 		}
164 168
 		
165 169
 		// Get an old version of a page that has been renamed.
@@ -175,8 +179,9 @@ static function find_old_page($URLSegment,$parentID = 0, $ignoreNestedURLs = fal
175 179
 		$record = $query->execute()->first();
176 180
 		
177 181
 		if($record && ($oldPage = DataObject::get_by_id('SiteTree', $record['RecordID']))) {
  182
+			$oldParent = $oldPage->ParentID ? $oldPage->Parent() : $oldPage;
178 183
 			// Run the page through an extra filter to ensure that all extensions are applied.
179  
-			if(SiteTree::get_by_link($oldPage->RelativeLink())) return $oldPage;
  184
+			if(SiteTree::get_by_link($oldPage->RelativeLink()) && $oldParent->isPublished()) return $oldPage;
180 185
 		}
181 186
 	}
182 187
 	
30  tests/controller/ModelAsControllerTest.php
@@ -233,5 +233,33 @@ function testFindOldPage(){
233 233
 		$response = ModelAsController::find_old_page('oldpage2',$page2->ID);
234 234
 		$this->assertEquals(false, $response );
235 235
 	}
236  
-	
  236
+
  237
+	/**
  238
+	 * go to a page that's been published but is child of an unpublished page
  239
+	 *
  240
+	 * NOTE: This test requires nested_urls
  241
+	 */
  242
+	function testChildOfDraft() {
  243
+		RootURLController::reset();
  244
+		SiteTree::enable_nested_urls();
  245
+
  246
+		$draft = new Page();
  247
+		$draft->Title = 'Root Leve Draft Page';
  248
+		$draft->URLSegment = 'root';
  249
+		$draft->write();
  250
+
  251
+		$published = new Page();
  252
+		$published->Title = 'Published Page Under Draft Page';
  253
+		$published->URLSegment = 'sub-root';
  254
+		$published->write();
  255
+		$published->publish('Stage', 'Live');
  256
+		$response = $this->get('root/sub-root');
  257
+
  258
+		$this->assertEquals(
  259
+		$response->getStatusCode(),
  260
+			404,
  261
+			'The page should not be found since its parent has not been published, in this case http://<yousitename>/root/sub-root or http://<yousitename>/sub-root'
  262
+		);
  263
+	}
  264
+
237 265
 }

0 notes on commit f7b6f1d

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