From 92bcb27cac77d04300e69a2b6f5aba32eabb4842 Mon Sep 17 00:00:00 2001 From: Nick Date: Tue, 2 Sep 2014 00:35:23 +1200 Subject: [PATCH] Fix issue where the last mod timestamp of the sitemap index file wasnt using the most recently last edited timestamp, but the last item in the stack. Update test to check this, and include test for changeset bbd5e29 --- code/GoogleSitemap.php | 9 +++++---- tests/GoogleSitemapTest.php | 28 ++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/code/GoogleSitemap.php b/code/GoogleSitemap.php index f527785..aaa6050 100644 --- a/code/GoogleSitemap.php +++ b/code/GoogleSitemap.php @@ -283,11 +283,12 @@ class GoogleSitemap { $neededForPage = ceil($count / $countPerFile); for($i = 1; $i <= $neededForPage; $i++) { - $sliced = $instances - ->limit($countPerFile, ($i - 1) * $countPerFile) - ->last(); - $lastModified = ($sliced) ? $sliced->dbObject('LastEdited')->Format('Y-m-d') : date('Y-m-d'); + $lastEdited = $instances + ->limit($countPerFile, ($i - 1) * $countPerFile) + ->max('LastEdited'); + + $lastModified = ($lastEdited) ? date('Y-m-d', strtotime($lastEdited)) : date('Y-m-d'); $sitemaps->push(new ArrayData(array( 'ClassName' => 'SiteTree', diff --git a/tests/GoogleSitemapTest.php b/tests/GoogleSitemapTest.php index 13cbf2a..ff2b313 100644 --- a/tests/GoogleSitemapTest.php +++ b/tests/GoogleSitemapTest.php @@ -92,17 +92,29 @@ class GoogleSitemapTest extends FunctionalTest { } public function testLastModifiedDateOnRootXML() { - GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject"); + Config::inst()->update('GoogleSitemap', 'enabled', true); - DB::query(" - UPDATE GoogleSitemapTest_DataObject SET LastEdited = '2012-01-14'" - ); + if(!class_exists('Page')) { + $this->markTestIncomplete('No cms module installed, page related test skipped'); + } + + $page = $this->objFromFixture('Page', 'Page1'); + $page->publish('Stage', 'Live'); + $page->flushCache(); + + $page2 = $this->objFromFixture('Page', 'Page2'); + $page2->publish('Stage', 'Live'); + $page2->flushCache(); + + DB::query("UPDATE \"SiteTree_Live\" SET \"LastEdited\"='2014-03-14 00:00:00' WHERE \"ID\"='".$page->ID."'"); + DB::query("UPDATE \"SiteTree_Live\" SET \"LastEdited\"='2014-01-01 00:00:00' WHERE \"ID\"='".$page2->ID."'"); $response = $this->get('sitemap.xml'); $body = $response->getBody(); - $expected = "2012-01-14"; - $this->assertEquals(1, substr_count($body, $expected)); + $expected = '2014-03-14'; + + $this->assertEquals(1, substr_count($body, $expected) , 'The last mod date should use most recent LastEdited date'); } public function testIndexFilePaginatedSitemapFiles() { @@ -227,6 +239,10 @@ class GoogleSitemapTest extends FunctionalTest { // invalid field doesn't break google $page->Priority = 'foo'; $this->assertEquals(0.5, $page->getGooglePriority()); + + // custom value (set as string as db field is varchar) + $page->Priority = '0.2'; + $this->assertEquals(0.2, $page->getGooglePriority()); // -1 indicates that we should not index this $page->Priority = -1;