Merge pull request #100 from nikrolls/support-namespaced-dataobjects

Added support for namespaced data objects
This commit is contained in:
Will Rossiter 2016-04-13 08:00:40 +12:00
commit 6fd2901aaf
4 changed files with 77 additions and 3 deletions

View File

@ -380,7 +380,7 @@ class GoogleSitemap extends Object
$lastModified = ($sliced) ? $sliced->dbObject('LastEdited')->Format('Y-m-d') : date('Y-m-d'); $lastModified = ($sliced) ? $sliced->dbObject('LastEdited')->Format('Y-m-d') : date('Y-m-d');
$sitemaps->push(new ArrayData(array( $sitemaps->push(new ArrayData(array(
'ClassName' => $class, 'ClassName' => $this->sanitiseClassName($class),
'Page' => $i, 'Page' => $i,
'LastModified' => $lastModified 'LastModified' => $lastModified
))); )));
@ -491,4 +491,13 @@ class GoogleSitemap extends Object
{ {
return GoogleSitemap::create(); return GoogleSitemap::create();
} }
/**
* Sanitise a namespaced class' name for inclusion in a link
* @return string
*/
protected function sanitiseClassName($class)
{
return str_replace('\\', '-', $class);
}
} }

View File

@ -57,7 +57,7 @@ class GoogleSitemapController extends Controller
*/ */
public function sitemap() public function sitemap()
{ {
$class = $this->request->param('ID'); $class = $this->unsanitiseClassName($this->request->param('ID'));
$page = $this->request->param('OtherID'); $page = $this->request->param('OtherID');
if (GoogleSitemap::enabled() && $class && $page) { if (GoogleSitemap::enabled() && $class && $page) {
@ -76,4 +76,13 @@ class GoogleSitemapController extends Controller
return new SS_HTTPResponse('Page not found', 404); 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);
}
} }

View File

@ -14,7 +14,8 @@ class GoogleSitemapTest extends FunctionalTest
protected $extraDataObjects = array( protected $extraDataObjects = array(
'GoogleSitemapTest_DataObject', 'GoogleSitemapTest_DataObject',
'GoogleSitemapTest_OtherDataObject', 'GoogleSitemapTest_OtherDataObject',
'GoogleSitemapTest_UnviewableDataObject' 'GoogleSitemapTest_UnviewableDataObject',
'SilverStripe\GoogleSitemaps\Test_DataObject',
); );
public function setUp() public function setUp()
@ -188,6 +189,34 @@ class GoogleSitemapTest extends FunctionalTest
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', $original); 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() public function testGetItemsWithPages()
{ {
if (!class_exists('Page')) { if (!class_exists('Page')) {

View File

@ -0,0 +1,27 @@
<?php
namespace SilverStripe\GoogleSitemaps;
use Director, DataObject, TestOnly;
/**
* @package googlesitemaps
* @subpackage tests
*/
class Test_DataObject extends DataObject implements TestOnly
{
public static $db = array(
'Priority' => 'Varchar(10)'
);
public function canView($member = null)
{
return true;
}
public function AbsoluteLink()
{
return Director::absoluteBaseURL();
}
}