diff --git a/.travis.yml b/.travis.yml index d4ed1d5..8794572 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,33 +5,23 @@ sudo: false language: php php: - - 5.3 - - 5.4 - 5.5 - 5.6 - 7.0 env: - - DB=MYSQL CORE_RELEASE=3.2 + - DB=MYSQL CORE_RELEASE=master matrix: include: - php: 5.6 - env: DB=MYSQL CORE_RELEASE=3 - - php: 5.6 - env: DB=MYSQL CORE_RELEASE=3.1 - - php: 5.6 - env: DB=PGSQL CORE_RELEASE=3.2 - allow_failures: - - php: 7.0 + env: DB=PGSQL CORE_RELEASE=master before_script: - composer self-update || true - git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support - php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss - cd ~/builds/ss - - composer install - - composer require wilr/silverstripe-googlesitemaps script: - vendor/bin/phpunit googlesitemaps/tests diff --git a/_config.php b/_config.php deleted file mode 100644 index 07af9ac..0000000 --- a/_config.php +++ /dev/null @@ -1,11 +0,0 @@ -filter('ShowInSearch', 1); @@ -341,7 +352,7 @@ class GoogleSitemap extends Object $sitemaps = new ArrayList(); $filter = Config::inst()->get('GoogleSitemap', 'use_show_in_search'); - if (class_exists('SiteTree')) { + if (class_exists(SiteTree::class)) { // move to extension hook. At the moment moduleexists config hook // does not work. if (class_exists('Translatable')) { @@ -349,7 +360,7 @@ class GoogleSitemap extends Object } $filter = ($filter) ? "\"ShowInSearch\" = 1" : ""; - $class = 'SiteTree'; + $class = SiteTree::class; $instances = Versioned::get_by_stage($class, 'Live', $filter); $this->extend("alterDataList", $instances, $class); $count = $instances->count(); @@ -365,7 +376,7 @@ class GoogleSitemap extends Object $lastModified = ($lastEdited) ? date('Y-m-d', strtotime($lastEdited)) : date('Y-m-d'); $sitemaps->push(new ArrayData(array( - 'ClassName' => 'SiteTree', + 'ClassName' => $this->sanitiseClassName(SiteTree::class), 'LastModified' => $lastModified, 'Page' => $i ))); diff --git a/code/controllers/GoogleSitemapController.php b/code/controllers/GoogleSitemapController.php index 334c60b..75d86a7 100644 --- a/code/controllers/GoogleSitemapController.php +++ b/code/controllers/GoogleSitemapController.php @@ -1,7 +1,12 @@ update('SSViewer', 'set_source_file_comments', false); - + $this->getResponse()->addHeader('Content-Type', 'application/xml; charset="utf-8"'); $this->getResponse()->addHeader('X-Robots-Tag', 'noindex'); @@ -45,14 +50,14 @@ class GoogleSitemapController extends Controller 'Sitemaps' => $sitemaps ); } else { - return new SS_HTTPResponse('Page not found', 404); + return new HTTPResponse('Page not found', 404); } } /** - * Specific controller action for displaying a particular list of links + * Specific controller action for displaying a particular list of links * for a class - * + * * @return mixed */ public function sitemap() @@ -60,9 +65,9 @@ class GoogleSitemapController extends Controller $class = $this->unsanitiseClassName($this->request->param('ID')); $page = $this->request->param('OtherID'); - if (GoogleSitemap::enabled() && $class && $page && ($class == 'SiteTree' || $class == 'GoogleSitemapRoute' || GoogleSitemap::is_registered($class))) { + if (GoogleSitemap::enabled() && $class && $page && ($class == SiteTree::class || $class == 'GoogleSitemapRoute' || GoogleSitemap::is_registered($class))) { Config::inst()->update('SSViewer', 'set_source_file_comments', false); - + $this->getResponse()->addHeader('Content-Type', 'application/xml; charset="utf-8"'); $this->getResponse()->addHeader('X-Robots-Tag', 'noindex'); @@ -73,7 +78,7 @@ class GoogleSitemapController extends Controller 'Items' => $items ); } else { - return new SS_HTTPResponse('Page not found', 404); + return new HTTPResponse('Page not found', 404); } } diff --git a/code/extensions/GoogleSitemapExtension.php b/code/extensions/GoogleSitemapExtension.php index ac5907c..f3f95a9 100644 --- a/code/extensions/GoogleSitemapExtension.php +++ b/code/extensions/GoogleSitemapExtension.php @@ -1,5 +1,9 @@ value = ($this->owner->Created) ? $this->owner->Created : $date; - $now = new SS_Datetime(); + $now = new DBDatetime(); $now->value = $date; $versions = ($this->owner->Version) ? $this->owner->Version : 1; diff --git a/code/extensions/GoogleSitemapSiteTreeExtension.php b/code/extensions/GoogleSitemapSiteTreeExtension.php index f448769..779ef3a 100644 --- a/code/extensions/GoogleSitemapSiteTreeExtension.php +++ b/code/extensions/GoogleSitemapSiteTreeExtension.php @@ -1,5 +1,10 @@ findOrMakeTab('Root.Settings'); - + $message = "

"; $message .= sprintf(_t('GoogleSitemaps.METANOTEPRIORITY', "Manually specify a Google Sitemaps priority for this page (%s)"), '?' ); $message .= "

"; - + $tabset->push(new Tab('GoogleSitemap', _t('GoogleSitemaps.TABGOOGLESITEMAP', 'Google Sitemap'), new LiteralField("GoogleSitemapIntro", $message), $priority = new DropdownField("Priority", $this->owner->fieldLabel('Priority'), $prorities, $this->owner->Priority) @@ -59,26 +64,26 @@ class GoogleSitemapSiteTreeExtension extends GoogleSitemapExtension $labels['Priority'] = _t('GoogleSitemaps.METAPAGEPRIO', "Page Priority"); } - + /** * Ensure that all parent pages of this page (if any) are published - * + * * @return boolean */ public function hasPublishedParent() { - + // Skip root pages if (empty($this->owner->ParentID)) { return true; } - + // Ensure direct parent exists $parent = $this->owner->Parent(); if (empty($parent) || !$parent->exists()) { return false; } - + // Check ancestry return $parent->hasPublishedParent(); } @@ -88,12 +93,12 @@ class GoogleSitemapSiteTreeExtension extends GoogleSitemapExtension */ public function canIncludeInGoogleSitemap() { - + // Check that parent page is published if (!$this->owner->hasPublishedParent()) { return false; } - + $result = parent::canIncludeInGoogleSitemap(); $result = ($this->owner instanceof ErrorPage) ? false : $result; @@ -111,7 +116,7 @@ class GoogleSitemapSiteTreeExtension extends GoogleSitemapExtension if (!$priority) { $parentStack = $this->owner->parentStack(); $numParents = is_array($parentStack) ? count($parentStack) - 1 : 0; - + $num = max(0.1, 1.0 - ($numParents / 10)); $result = str_replace(",", ".", $num); diff --git a/tests/GoogleSitemapTest.php b/tests/GoogleSitemapTest.php index 99f9f03..2abbe90 100644 --- a/tests/GoogleSitemapTest.php +++ b/tests/GoogleSitemapTest.php @@ -1,5 +1,17 @@ getSitemaps() instead of ::get_sitemaps()). * @@ -25,7 +37,7 @@ class GoogleSitemapTest extends FunctionalTest if (class_exists('Page')) { $this->loadFixture('googlesitemaps/tests/GoogleSitemapPageTest.yml'); } - + GoogleSitemap::clear_registered_dataobjects(); GoogleSitemap::clear_registered_routes(); } @@ -93,7 +105,7 @@ class GoogleSitemapTest extends FunctionalTest // dataobject as it hasn't been registered $expected = "". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapTest_DataObject/1") .""; $this->assertEquals(1, substr_count($body, $expected), 'A link to GoogleSitemapTest_DataObject exists'); - + $expected = "". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapTest_OtherDataObject/1") .""; $this->assertEquals(1, substr_count($body, $expected), 'A link to GoogleSitemapTest_OtherDataObject exists'); @@ -222,11 +234,11 @@ class GoogleSitemapTest extends FunctionalTest 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(); @@ -234,29 +246,29 @@ class GoogleSitemapTest extends FunctionalTest $this->assertDOSContains(array( array('Title' => 'Testpage1'), array('Title' => 'Testpage2') - ), GoogleSitemap::get_items('SiteTree'), "There should be 2 pages in the sitemap after publishing"); - + ), GoogleSitemap::get_items(SiteTree::class), "There should be 2 pages in the sitemap after publishing"); + // check if we make a page readonly that it is hidden $page2->CanViewType = 'LoggedInUsers'; $page2->write(); $page2->publish('Stage', 'Live'); - + $this->session()->inst_set('loggedInAs', null); - + $this->assertDOSEquals(array( array('Title' => 'Testpage1') - ), GoogleSitemap::get_items('SiteTree'), "There should be only 1 page, other is logged in only"); + ), GoogleSitemap::get_items(SiteTree::class), "There should be only 1 page, other is logged in only"); } - + public function testAccess() { Config::inst()->update('GoogleSitemap', 'enabled', true); - + $response = $this->get('sitemap.xml'); $this->assertEquals(200, $response->getStatusCode(), 'Sitemap returns a 200 success when enabled'); $this->assertEquals('application/xml; charset="utf-8"', $response->getHeader('Content-Type')); - + GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject"); $response = $this->get('sitemap.xml/sitemap/GoogleSitemapTest_DataObject/1'); $this->assertEquals(200, $response->getStatusCode(), 'Sitemap returns a 200 success when enabled'); @@ -264,14 +276,14 @@ class GoogleSitemapTest extends FunctionalTest Config::inst()->remove('GoogleSitemap', 'enabled'); Config::inst()->update('GoogleSitemap', 'enabled', false); - + $response = $this->get('sitemap.xml'); $this->assertEquals(404, $response->getStatusCode(), 'Sitemap index returns a 404 when disabled'); $response = $this->get('sitemap.xml/sitemap/GoogleSitemapTest_DataObject/1'); $this->assertEquals(404, $response->getStatusCode(), 'Sitemap file returns a 404 when disabled'); } - + public function testDecoratorAddsFields() { if (!class_exists("Page")) { @@ -279,21 +291,21 @@ class GoogleSitemapTest extends FunctionalTest } $page = $this->objFromFixture('Page', 'Page1'); - + $fields = $page->getSettingsFields(); $tab = $fields->fieldByName('Root')->fieldByName('Settings')->fieldByName('GoogleSitemap'); - - $this->assertInstanceOf('Tab', $tab); - $this->assertInstanceOf('DropdownField', $tab->fieldByName('Priority')); - $this->assertInstanceOf('LiteralField', $tab->fieldByName('GoogleSitemapIntro')); + + $this->assertInstanceOf(Tab::class, $tab); + $this->assertInstanceOf(DropdownField::class, $tab->fieldByName('Priority')); + $this->assertInstanceOf(LiteralField::class, $tab->fieldByName('GoogleSitemapIntro')); } - + public function testGetPriority() { if (!class_exists("Page")) { $this->markTestIncomplete('No cms module installed, page related test skipped'); } - + $page = $this->objFromFixture('Page', 'Page1'); // invalid field doesn't break google @@ -303,31 +315,31 @@ class GoogleSitemapTest extends FunctionalTest // 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; $this->assertFalse($page->getGooglePriority()); } - + public function testUnpublishedPage() { - if (!class_exists('SiteTree')) { + if (!class_exists(SiteTree::class)) { $this->markTestSkipped('Test skipped; CMS module required for testUnpublishedPage'); } - + $orphanedPage = new SiteTree(); $orphanedPage->ParentID = 999999; // missing parent id $orphanedPage->write(); $orphanedPage->publish("Stage", "Live"); - + $rootPage = new SiteTree(); $rootPage->ParentID = 0; $rootPage->write(); $rootPage->publish("Stage", "Live"); - + $oldMode = Versioned::get_reading_mode(); - Versioned::reading_stage('Live'); - + Versioned::set_reading_mode('Live'); + try { $this->assertEmpty($orphanedPage->hasPublishedParent()); $this->assertEmpty($orphanedPage->canIncludeInGoogleSitemap()); @@ -348,8 +360,8 @@ class GoogleSitemapTest extends FunctionalTest */ class GoogleSitemapTest_DataObject extends DataObject implements TestOnly { - - public static $db = array( + + private static $db = array( 'Priority' => 'Varchar(10)' ); @@ -371,7 +383,7 @@ class GoogleSitemapTest_DataObject extends DataObject implements TestOnly class GoogleSitemapTest_OtherDataObject extends DataObject implements TestOnly { - public static $db = array( + private static $db = array( 'Priority' => 'Varchar(10)' ); diff --git a/tests/GoogleSitemapTest.yml b/tests/GoogleSitemapTest.yml index 0c99170..577e68e 100644 --- a/tests/GoogleSitemapTest.yml +++ b/tests/GoogleSitemapTest.yml @@ -4,7 +4,7 @@ GoogleSitemapTest_DataObject: DataObjectTest2: Priority: 0.2 UnindexedDataObject: - Priority: -1 + Priority: -1 GoogleSitemapTest_OtherDataObject: OtherDataObjectTest2: @@ -12,4 +12,4 @@ GoogleSitemapTest_OtherDataObject: GoogleSitemapTest_UnviewableDataObject: Unviewable1: - Priority: 0.4 \ No newline at end of file + Priority: 0.4 diff --git a/tests/NamespacedTestObject.php b/tests/NamespacedTestObject.php index 957df2e..2f8ebd4 100644 --- a/tests/NamespacedTestObject.php +++ b/tests/NamespacedTestObject.php @@ -2,7 +2,9 @@ namespace SilverStripe\GoogleSitemaps; -use Director, DataObject, TestOnly; +use SilverStripe\Control\Director; +use SilverStripe\Dev\TestOnly; +use SilverStripe\ORM\DataObject; /** * @package googlesitemaps @@ -10,7 +12,6 @@ use Director, DataObject, TestOnly; */ class Test_DataObject extends DataObject implements TestOnly { - public static $db = array( 'Priority' => 'Varchar(10)' );