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;