From ecd3551ec09554988b2071ac9b9947da64af3b7f Mon Sep 17 00:00:00 2001 From: Nik Rolls Date: Tue, 12 Apr 2016 22:55:34 +1200 Subject: [PATCH] Added support for namespaced data objects --- code/GoogleSitemap.php | 11 ++++++- code/controllers/GoogleSitemapController.php | 11 ++++++- tests/GoogleSitemapTest.php | 31 +++++++++++++++++++- tests/NamespacedTestObject.php | 27 +++++++++++++++++ 4 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 tests/NamespacedTestObject.php diff --git a/code/GoogleSitemap.php b/code/GoogleSitemap.php index 542e421..b22ee08 100644 --- a/code/GoogleSitemap.php +++ b/code/GoogleSitemap.php @@ -380,7 +380,7 @@ class GoogleSitemap extends Object $lastModified = ($sliced) ? $sliced->dbObject('LastEdited')->Format('Y-m-d') : date('Y-m-d'); $sitemaps->push(new ArrayData(array( - 'ClassName' => $class, + 'ClassName' => $this->sanitiseClassName($class), 'Page' => $i, 'LastModified' => $lastModified ))); @@ -491,4 +491,13 @@ class GoogleSitemap extends Object { return GoogleSitemap::create(); } + + /** + * Sanitise a namespaced class' name for inclusion in a link + * @return string + */ + protected function sanitiseClassName($class) + { + return str_replace('\\', '-', $class); + } } diff --git a/code/controllers/GoogleSitemapController.php b/code/controllers/GoogleSitemapController.php index 9af1d0a..c84c860 100644 --- a/code/controllers/GoogleSitemapController.php +++ b/code/controllers/GoogleSitemapController.php @@ -57,7 +57,7 @@ class GoogleSitemapController extends Controller */ public function sitemap() { - $class = $this->request->param('ID'); + $class = $this->unsanitiseClassName($this->request->param('ID')); $page = $this->request->param('OtherID'); if (GoogleSitemap::enabled() && $class && $page) { @@ -76,4 +76,13 @@ class GoogleSitemapController extends Controller return new SS_HTTPResponse('Page not found', 404); } } + + /** + * Unsanitise a namespaced class' name from a URL param + * @return string + */ + protected function unsanitiseClassName($class) + { + return str_replace('-', '\\', $class); + } } diff --git a/tests/GoogleSitemapTest.php b/tests/GoogleSitemapTest.php index 25100a1..99f9f03 100644 --- a/tests/GoogleSitemapTest.php +++ b/tests/GoogleSitemapTest.php @@ -14,7 +14,8 @@ class GoogleSitemapTest extends FunctionalTest protected $extraDataObjects = array( 'GoogleSitemapTest_DataObject', 'GoogleSitemapTest_OtherDataObject', - 'GoogleSitemapTest_UnviewableDataObject' + 'GoogleSitemapTest_UnviewableDataObject', + 'SilverStripe\GoogleSitemaps\Test_DataObject', ); public function setUp() @@ -188,6 +189,34 @@ class GoogleSitemapTest extends FunctionalTest Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', $original); } + public function testAccessingNestedNamespacedSiteMap() + { + $original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap'); + Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', 1); + GoogleSitemap::register_dataobject("SilverStripe\\GoogleSitemaps\\Test_DataObject"); + + $response = $this->get('sitemap.xml/sitemap/SilverStripe-GoogleSitemaps-Test_DataObject/1'); + $body = $response->getBody(); + + $this->assertEquals(200, $response->getStatusCode(), 'successful loaded nested sitemap'); + + Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', $original); + } + + public function testAccessingNestedNamespacedSiteMapCaseInsensitive() + { + $original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap'); + Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', 1); + GoogleSitemap::register_dataobject("SilverStripe\\GoogleSitemaps\\Test_DataObject"); + + $response = $this->get('sitemap.xml/sitemap/silverstripe-googlesitemaps-test_dataobject/1'); + $body = $response->getBody(); + + $this->assertEquals(200, $response->getStatusCode(), 'successful loaded nested sitemap'); + + Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', $original); + } + public function testGetItemsWithPages() { if (!class_exists('Page')) { diff --git a/tests/NamespacedTestObject.php b/tests/NamespacedTestObject.php new file mode 100644 index 0000000..957df2e --- /dev/null +++ b/tests/NamespacedTestObject.php @@ -0,0 +1,27 @@ + 'Varchar(10)' + ); + + public function canView($member = null) + { + return true; + } + + public function AbsoluteLink() + { + return Director::absoluteBaseURL(); + } +}