From 286115174d10f15e7027cfdf3722c5db8ebb175b Mon Sep 17 00:00:00 2001 From: Mohamed Alsharaf Date: Mon, 13 Feb 2023 13:38:57 +1300 Subject: [PATCH] Add support for silverstripe 5 --- composer.json | 48 ++++--- phpunit.xml.dist | 26 ++-- src/Control/GoogleSitemapController.php | 2 +- src/GoogleSitemap.php | 4 +- .../Control/GoogleSitemapController.ss | 4 +- .../GoogleSitemapController_sitemap.ss | 2 +- tests/GoogleSitemapTest.php | 125 ++++++------------ tests/xml/testAccessingSitemapRootXMLFile.xml | 12 ++ .../testIndexFilePaginatedSitemapFiles.xml | 16 +++ tests/xml/testIndexFileWithCustomRoute.xml | 7 + 10 files changed, 125 insertions(+), 121 deletions(-) create mode 100644 tests/xml/testAccessingSitemapRootXMLFile.xml create mode 100644 tests/xml/testIndexFilePaginatedSitemapFiles.xml create mode 100644 tests/xml/testIndexFileWithCustomRoute.xml diff --git a/composer.json b/composer.json index 4c7e1e5..6d8c20e 100644 --- a/composer.json +++ b/composer.json @@ -1,25 +1,32 @@ { - "name": "wilr/silverstripe-googlesitemaps", - "description": "SilverStripe support for the Google Sitemaps XML, enabling Google and other search engines to see all urls on your site. This helps your SilverStripe website rank well in search engines, and to encourage the information on your site to be discovered quickly.", - "type": "silverstripe-vendormodule", - "keywords": ["silverstripe", "googlesitemaps", "seo"], - "homepage": "https://github.com/wilr/silverstripe-googlesitemaps", - "license": "BSD-3-Clause", - "authors": [{ - "name": "Will Rossiter", - "email": "will@fullscreen.io" - }], - "require": { - "silverstripe/framework": "^4" - }, + "name": "wilr/silverstripe-googlesitemaps", + "description": "SilverStripe support for the Google Sitemaps XML, enabling Google and other search engines to see all urls on your site. This helps your SilverStripe website rank well in search engines, and to encourage the information on your site to be discovered quickly.", + "type": "silverstripe-vendormodule", + "keywords": [ + "silverstripe", + "googlesitemaps", + "seo" + ], + "homepage": "https://github.com/wilr/silverstripe-googlesitemaps", + "license": "BSD-3-Clause", + "authors": [ + { + "name": "Will Rossiter", + "email": "will@fullscreen.io" + } + ], + "require": { + "php": "^8.1", + "silverstripe/framework": "^5" + }, "require-dev": { - "phpunit/phpunit": "^5.7", + "phpunit/phpunit": "^9.5", "squizlabs/php_codesniffer": "^3" }, "replace": { - "silverstripe/googlesitemaps":"*" + "silverstripe/googlesitemaps": "*" }, - "extra": { + "extra": { "branch-alias": { "dev-main": "2.x-dev" }, @@ -35,5 +42,12 @@ } }, "prefer-stable": true, - "minimum-stability": "dev" + "minimum-stability": "dev", + "config": { + "allow-plugins": { + "composer/installers": true, + "silverstripe/vendor-plugin": true, + "silverstripe/recipe-plugin": true + } + } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 3e41eee..741927a 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,14 +1,14 @@ - - - tests - - - - - src/ - - tests/ - - - + + + + + src/ + + + tests/ + + + + tests + diff --git a/src/Control/GoogleSitemapController.php b/src/Control/GoogleSitemapController.php index 0ba5312..5a5b348 100644 --- a/src/Control/GoogleSitemapController.php +++ b/src/Control/GoogleSitemapController.php @@ -95,7 +95,7 @@ class GoogleSitemapController extends Controller */ protected function unsanitiseClassName($class) { - return str_replace('-', '\\', $class); + return str_replace('-', '\\', (string) $class); } /** diff --git a/src/GoogleSitemap.php b/src/GoogleSitemap.php index 987bb18..86200e9 100644 --- a/src/GoogleSitemap.php +++ b/src/GoogleSitemap.php @@ -390,7 +390,7 @@ class GoogleSitemap for ($i = 1; $i <= $neededForPage; $i++) { $lastEdited = $instances ->limit($countPerFile, ($i - 1) * $countPerFile) - ->sort(array()) + ->sort(null) ->max('LastEdited'); $lastModified = ($lastEdited) ? date('Y-m-d', strtotime($lastEdited)) : date('Y-m-d'); @@ -515,6 +515,6 @@ class GoogleSitemap */ protected function sanitiseClassName($class) { - return str_replace('\\', '-', $class); + return str_replace('\\', '-', (string) $class); } } diff --git a/templates/Wilr/GoogleSitemaps/Control/GoogleSitemapController.ss b/templates/Wilr/GoogleSitemaps/Control/GoogleSitemapController.ss index 0f6d132..1e0852f 100644 --- a/templates/Wilr/GoogleSitemaps/Control/GoogleSitemapController.ss +++ b/templates/Wilr/GoogleSitemaps/Control/GoogleSitemapController.ss @@ -1,8 +1,8 @@ - + <% loop Sitemaps %> - {$AbsoluteBaseURL}sitemap.xml/sitemap/$ClassName/$Page.xml + {$AbsoluteBaseURL}/sitemap.xml/sitemap/$ClassName/$Page.xml <% if $LastModified %>$LastModified<% end_if %> <% end_loop %> diff --git a/templates/Wilr/GoogleSitemaps/Control/GoogleSitemapController_sitemap.ss b/templates/Wilr/GoogleSitemaps/Control/GoogleSitemapController_sitemap.ss index 8e9a493..99aa385 100644 --- a/templates/Wilr/GoogleSitemaps/Control/GoogleSitemapController_sitemap.ss +++ b/templates/Wilr/GoogleSitemaps/Control/GoogleSitemapController_sitemap.ss @@ -1,5 +1,5 @@ - + <% loop $Items %> diff --git a/tests/GoogleSitemapTest.php b/tests/GoogleSitemapTest.php index 9faa31a..ff659b5 100644 --- a/tests/GoogleSitemapTest.php +++ b/tests/GoogleSitemapTest.php @@ -20,7 +20,10 @@ use Wilr\GoogleSitemaps\Tests\Model\UnviewableDataObject; class GoogleSitemapTest extends FunctionalTest { - protected static $fixture_file = 'GoogleSitemapTest.yml'; + protected static $fixture_file = [ + 'GoogleSitemapTest.yml', + 'GoogleSitemapPageTest.yml', + ]; protected static $extra_dataobjects = [ TestDataObject::class, @@ -32,19 +35,15 @@ class GoogleSitemapTest extends FunctionalTest GoogleSitemapExtension::class ]; - public function setUp() + protected function setUp(): void { parent::setUp(); - if (class_exists('Page')) { - $this->loadFixture($this->resolveFixturePath('GoogleSitemapPageTest.yml')); - } - GoogleSitemap::clear_registered_dataobjects(); GoogleSitemap::clear_registered_routes(); } - public function tearDown() + protected function tearDown(): void { parent::tearDown(); @@ -52,7 +51,7 @@ class GoogleSitemapTest extends FunctionalTest GoogleSitemap::clear_registered_routes(); } - public function testCanIncludeInGoogleSitemap() + public function testCanIncludeInGoogleSitemap(): void { GoogleSitemap::register_dataobject(TestDataObject::class, ''); @@ -64,26 +63,24 @@ class GoogleSitemapTest extends FunctionalTest $this->assertTrue($used->canIncludeInGoogleSitemap()); } - public function testIndexFileWithCustomRoute() + public function testIndexFileWithCustomRoute(): void { GoogleSitemap::register_route('/test/'); $response = $this->get('sitemap.xml'); $body = $response->getBody(); - $expected = "". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapRoute/1") .""; - $this->assertEquals(1, substr_count($body, $expected), 'A link to the custom routes exists'); + $this->assertXmlStringEqualsXmlFile(__DIR__ . '/xml/' . __FUNCTION__ . '.xml', $body, 'A link to the custom routes exists'); } - - public function testGetItems() + public function testGetItems(): void { GoogleSitemap::register_dataobject(TestDataObject::class, ''); $items = GoogleSitemap::get_items(TestDataObject::class, 1); $this->assertEquals(2, $items->count()); - $this->assertDOSEquals(array( + $this->assertListEquals(array( array("Priority" => "0.2"), array("Priority" => "0.4") ), $items); @@ -95,7 +92,7 @@ class GoogleSitemapTest extends FunctionalTest $this->assertEquals(0, GoogleSitemap::get_items(UnviewableDataObject::class, 1)->count()); } - public function testGetItemsWithCustomRoutes() + public function testGetItemsWithCustomRoutes(): void { GoogleSitemap::register_routes(array( '/test-route/', @@ -107,7 +104,7 @@ class GoogleSitemapTest extends FunctionalTest $this->assertEquals(3, $items->count()); } - public function testAccessingSitemapRootXMLFile() + public function testAccessingSitemapRootXMLFile(): void { GoogleSitemap::register_dataobject(TestDataObject::class); GoogleSitemap::register_dataobject(OtherDataObject::class); @@ -115,50 +112,23 @@ class GoogleSitemapTest extends FunctionalTest $response = $this->get('sitemap.xml'); $body = $response->getBody(); - // the sitemap should contain to both those files and not the other - // dataobject as it hasn't been registered - $expected = "". Director::absoluteURL( - "sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1" - ) .""; - $this->assertEquals( - 1, - substr_count($body, $expected), - 'A link to GoogleSitemapTest_DataObject exists' - ); - - $expected = "". Director::absoluteURL( - "sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-OtherDataObject/1" - ) .""; - $this->assertEquals( - 1, - substr_count($body, $expected), - 'A link to GoogleSitemapTest_OtherDataObject exists' - ); - - $expected = "". Director::absoluteURL( - "sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-UnviewableDataObject/2" - ) .""; - $this->assertEquals( - 0, - substr_count($body, $expected), - 'A link to a GoogleSitemapTest_UnviewableDataObject does not exist' - ); + $this->assertXmlStringEqualsXmlFile(__DIR__ . '/xml/' . __FUNCTION__ . '.xml', $body); } - public function testLastModifiedDateOnRootXML() + public function testLastModifiedDateOnRootXML(): void { - Config::inst()->update(GoogleSitemap::class, 'enabled', true); + Config::inst()->set(GoogleSitemap::class, 'enabled', true); if (!class_exists('Page')) { $this->markTestIncomplete('No cms module installed, page related test skipped'); } $page = $this->objFromFixture('Page', 'Page1'); - $page->publish('Stage', 'Live'); + $page->publishSingle(); $page->flushCache(); $page2 = $this->objFromFixture('Page', 'Page2'); - $page2->publish('Stage', 'Live'); + $page2->publishSingle(); $page2->flushCache(); DB::query("UPDATE \"SiteTree_Live\" SET \"LastEdited\"='2014-03-14 00:00:00' WHERE \"ID\"='".$page->ID."'"); @@ -176,36 +146,21 @@ class GoogleSitemapTest extends FunctionalTest ); } - public function testIndexFilePaginatedSitemapFiles() + public function testIndexFilePaginatedSitemapFiles(): void { $original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap'); - Config::inst()->update(GoogleSitemap::class, 'objects_per_sitemap', 1); + Config::inst()->set(GoogleSitemap::class, 'objects_per_sitemap', 1); GoogleSitemap::register_dataobject(TestDataObject::class); $response = $this->get('sitemap.xml'); $body = $response->getBody(); - $expected = "". Director::absoluteURL( - "sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1" - ) .""; - $this->assertEquals( - 1, - substr_count($body, $expected), - 'A link to the first page of GoogleSitemapTest_DataObject exists' - ); - $expected = "". Director::absoluteURL( - "sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/2" - ) .""; - $this->assertEquals( - 1, - substr_count($body, $expected), - 'A link to the second page GoogleSitemapTest_DataObject exists' - ); + $this->assertXmlStringEqualsXmlFile(__DIR__ . '/xml/' . __FUNCTION__ . '.xml', $body); - Config::inst()->update(GoogleSitemap::class, 'objects_per_sitemap', $original); + Config::inst()->set(GoogleSitemap::class, 'objects_per_sitemap', $original); } - public function testRegisterRoutesIncludesAllRoutes() + public function testRegisterRoutesIncludesAllRoutes(): void { GoogleSitemap::register_route('/test/'); GoogleSitemap::register_routes(array( @@ -221,10 +176,10 @@ class GoogleSitemapTest extends FunctionalTest $this->assertEquals(3, substr_count($body, "")); } - public function testAccessingNestedSiteMap() + public function testAccessingNestedSiteMap(): void { $original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap'); - Config::inst()->update(GoogleSitemap::class, 'objects_per_sitemap', 1); + Config::inst()->set(GoogleSitemap::class, 'objects_per_sitemap', 1); GoogleSitemap::register_dataobject(TestDataObject::class); $response = $this->get('sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1'); @@ -232,24 +187,24 @@ class GoogleSitemapTest extends FunctionalTest $this->assertEquals(200, $response->getStatusCode(), 'successful loaded nested sitemap'); - Config::inst()->update(GoogleSitemap::class, 'objects_per_sitemap', $original); + Config::inst()->set(GoogleSitemap::class, 'objects_per_sitemap', $original); } - public function testGetItemsWithPages() + public function testGetItemsWithPages(): void { if (!class_exists('Page')) { $this->markTestIncomplete('No cms module installed, page related test skipped'); } $page = $this->objFromFixture('Page', 'Page1'); - $page->publish('Stage', 'Live'); + $page->publishSingle(); $page->flushCache(); $page2 = $this->objFromFixture('Page', 'Page2'); - $page2->publish('Stage', 'Live'); + $page2->publishSingle(); $page2->flushCache(); - $this->assertDOSContains(array( + $this->assertListContains(array( array('Title' => 'Testpage1'), array('Title' => 'Testpage2') ), GoogleSitemap::inst()->getItems(SiteTree::class), "There should be 2 pages in the sitemap after publishing"); @@ -257,18 +212,18 @@ class GoogleSitemapTest extends FunctionalTest // check if we make a page readonly that it is hidden $page2->CanViewType = 'LoggedInUsers'; $page2->write(); - $page2->publish('Stage', 'Live'); + $page2->publishSingle(); $this->logOut(); - $this->assertDOSEquals(array( + $this->assertListEquals(array( array('Title' => 'Testpage1') ), GoogleSitemap::inst()->getItems(SiteTree::class), "There should be only 1 page, other is logged in only"); } - public function testAccess() + public function testAccess(): void { - Config::inst()->update(GoogleSitemap::class, 'enabled', true); + Config::inst()->set(GoogleSitemap::class, 'enabled', true); $response = $this->get('sitemap.xml'); @@ -280,7 +235,7 @@ class GoogleSitemapTest extends FunctionalTest $this->assertEquals(200, $response->getStatusCode(), 'Sitemap returns a 200 success when enabled'); $this->assertEquals('application/xml; charset="utf-8"', $response->getHeader('Content-Type')); - Config::inst()->update(GoogleSitemap::class, 'enabled', false); + Config::inst()->set(GoogleSitemap::class, 'enabled', false); $response = $this->get('sitemap.xml'); $this->assertEquals(404, $response->getStatusCode(), 'Sitemap index returns a 404 when disabled'); @@ -289,7 +244,7 @@ class GoogleSitemapTest extends FunctionalTest $this->assertEquals(404, $response->getStatusCode(), 'Sitemap file returns a 404 when disabled'); } - public function testDecoratorAddsFields() + public function testDecoratorAddsFields(): void { if (!class_exists("Page")) { $this->markTestIncomplete('No cms module installed, page related test skipped'); @@ -305,7 +260,7 @@ class GoogleSitemapTest extends FunctionalTest $this->assertInstanceOf(LiteralField::class, $tab->fieldByName('GoogleSitemapIntro')); } - public function testGetPriority() + public function testGetPriority(): void { if (!class_exists("Page")) { $this->markTestIncomplete('No cms module installed, page related test skipped'); @@ -326,7 +281,7 @@ class GoogleSitemapTest extends FunctionalTest $this->assertFalse($page->getGooglePriority()); } - public function testUnpublishedPage() + public function testUnpublishedPage(): void { if (!class_exists('SilverStripe\CMS\Model\SiteTree')) { $this->markTestSkipped('Test skipped; CMS module required for testUnpublishedPage'); @@ -335,12 +290,12 @@ class GoogleSitemapTest extends FunctionalTest $orphanedPage = new \SilverStripe\CMS\Model\SiteTree(); $orphanedPage->ParentID = 999999; // missing parent id $orphanedPage->write(); - $orphanedPage->publish("Stage", "Live"); + $orphanedPage->publishSingle(); $rootPage = new \SilverStripe\CMS\Model\SiteTree(); $rootPage->ParentID = 0; $rootPage->write(); - $rootPage->publish("Stage", "Live"); + $rootPage->publishSingle(); $oldMode = Versioned::get_reading_mode(); Versioned::set_reading_mode('Live'); diff --git a/tests/xml/testAccessingSitemapRootXMLFile.xml b/tests/xml/testAccessingSitemapRootXMLFile.xml new file mode 100644 index 0000000..81887a8 --- /dev/null +++ b/tests/xml/testAccessingSitemapRootXMLFile.xml @@ -0,0 +1,12 @@ + + + + + http://localhostsitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1 + 2023-02-13 + + + http://localhostsitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-OtherDataObject/1 + 2023-02-13 + + diff --git a/tests/xml/testIndexFilePaginatedSitemapFiles.xml b/tests/xml/testIndexFilePaginatedSitemapFiles.xml new file mode 100644 index 0000000..24e5635 --- /dev/null +++ b/tests/xml/testIndexFilePaginatedSitemapFiles.xml @@ -0,0 +1,16 @@ + + + + + http://localhostsitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1 + 2023-02-13 + + + http://localhostsitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/2 + 2023-02-13 + + + http://localhostsitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/3 + 2023-02-13 + + diff --git a/tests/xml/testIndexFileWithCustomRoute.xml b/tests/xml/testIndexFileWithCustomRoute.xml new file mode 100644 index 0000000..6dc3307 --- /dev/null +++ b/tests/xml/testIndexFileWithCustomRoute.xml @@ -0,0 +1,7 @@ + + + + + http://localhostsitemap.xml/sitemap/GoogleSitemapRoute/1 + +