From 5f06483b7351dd0b63ac10966626b20c352098a0 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Tue, 28 Jan 2014 10:03:55 +1300 Subject: [PATCH] BUG Fix issue with children of unpublished pages appearing in sitemap as apparent root pages --- .../GoogleSitemapSiteTreeExtension.php | 22 +++++++++++++ tests/GoogleSitemapTest.php | 32 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/code/extensions/GoogleSitemapSiteTreeExtension.php b/code/extensions/GoogleSitemapSiteTreeExtension.php index 7d3d85b..cb39372 100644 --- a/code/extensions/GoogleSitemapSiteTreeExtension.php +++ b/code/extensions/GoogleSitemapSiteTreeExtension.php @@ -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; diff --git a/tests/GoogleSitemapTest.php b/tests/GoogleSitemapTest.php index 36499b5..13cbf2a 100644 --- a/tests/GoogleSitemapTest.php +++ b/tests/GoogleSitemapTest.php @@ -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); + // } + } } /**