Merge pull request #63 from tractorcow/pulls/fix-unpublished-ancestry

BUG Fix issue with children of unpublished pages appearing in sitemap as apparent root pages
This commit is contained in:
Will Rossiter 2014-01-27 20:41:57 -08:00
commit 3b3742a1bc
2 changed files with 54 additions and 0 deletions

View File

@ -56,11 +56,33 @@ class GoogleSitemapSiteTreeExtension extends GoogleSitemapExtension {
$labels['Priority'] = _t('GoogleSitemaps.METAPAGEPRIO', "Page Priority");
}
/**
* Ensure that all parent pages of this page (if any) are published
*
* @return boolean
*/
public function hasPublishedParent() {
// Skip root pages
if(empty($this->owner->ParentID)) return true;
// Ensure direct parent exists
$parent = $this->owner->Parent();
if(empty($parent) || !$parent->exists()) return false;
// Check ancestry
return $parent->hasPublishedParent();
}
/**
* @return boolean
*/
public function canIncludeInGoogleSitemap() {
// Check that parent page is published
if(!$this->owner->hasPublishedParent()) return false;
$result = parent::canIncludeInGoogleSitemap();
$result = ($this->owner instanceof ErrorPage) ? false : $result;

View File

@ -232,6 +232,38 @@ class GoogleSitemapTest extends FunctionalTest {
$page->Priority = -1;
$this->assertFalse($page->getGooglePriority());
}
public function testUnpublishedPage() {
if(!class_exists('SiteTree')) {
$this->markTestSkipped('Test skipped; CMS module required for testUnpublishedPage');
}
$orphanedPage = new SiteTree();
$orphanedPage->ParentID = 999999; // missing parent id
$orphanedPage->write();
$orphanedPage->publish("Stage", "Live");
$rootPage = new SiteTree();
$rootPage->ParentID = 0;
$rootPage->write();
$rootPage->publish("Stage", "Live");
$oldMode = Versioned::get_reading_mode();
Versioned::reading_stage('Live');
try {
$this->assertEmpty($orphanedPage->hasPublishedParent());
$this->assertEmpty($orphanedPage->canIncludeInGoogleSitemap());
$this->assertNotEmpty($rootPage->hasPublishedParent());
$this->assertNotEmpty($rootPage->canIncludeInGoogleSitemap());
} catch(Exception $ex) {
Versioned::set_reading_mode($oldMode);
throw $ex;
} // finally {
Versioned::set_reading_mode($oldMode);
// }
}
}
/**