FIX Apply PSR-2 linting ruleset and phpcs configuration

This commit is contained in:
Robbie Averill 2018-02-15 12:06:50 +13:00
parent d1d5c2b268
commit 450283bd1b
7 changed files with 106 additions and 43 deletions

10
phpcs.xml.dist Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="SilverStripe">
<description>CodeSniffer ruleset for SilverStripe coding conventions.</description>
<!-- base rules are PSR-2 -->
<rule ref="PSR2" >
<!-- Current exclusions -->
<exclude name="PSR1.Methods.CamelCapsMethodName" />
</rule>
</ruleset>

View File

@ -68,7 +68,11 @@ class GoogleSitemapController extends Controller
$class = $this->unsanitiseClassName($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 && ($class == SiteTree::class || $class == 'GoogleSitemapRoute' || GoogleSitemap::is_registered($class))) { if (GoogleSitemap::enabled()
&& $class
&& $page
&& ($class == SiteTree::class || $class == 'GoogleSitemapRoute' || GoogleSitemap::is_registered($class))
) {
$this->getResponse()->addHeader('Content-Type', 'application/xml; charset="utf-8"'); $this->getResponse()->addHeader('Content-Type', 'application/xml; charset="utf-8"');
$this->getResponse()->addHeader('X-Robots-Tag', 'noindex'); $this->getResponse()->addHeader('X-Robots-Tag', 'noindex');
@ -78,9 +82,9 @@ class GoogleSitemapController extends Controller
return array( return array(
'Items' => $items 'Items' => $items
); );
} else {
return new HTTPResponse('Page not found', 404);
} }
return new HTTPResponse('Page not found', 404);
} }
/** /**

View File

@ -42,8 +42,9 @@ class GoogleSitemapExtension extends DataExtension
return false; return false;
} }
// Allow override. In this case, since this can return multiple results, we'll use an "and" based policy. That is // Allow override. In this case, since this can return multiple results, we'll use an "and" based policy.
// if any value is false then the current value will be false. Only only if all are true will we then return true. // That is if any value is false then the current value will be false. Only only if all are true will we
// then return true.
$override = $this->owner->invokeWithExtensions('alterCanIncludeInGoogleSitemap', $can); $override = $this->owner->invokeWithExtensions('alterCanIncludeInGoogleSitemap', $can);
if ($override) { if ($override) {

View File

@ -39,14 +39,26 @@ class GoogleSitemapSiteTreeExtension extends GoogleSitemapExtension
$tabset = $fields->findOrMakeTab('Root.Settings'); $tabset = $fields->findOrMakeTab('Root.Settings');
$message = "<p>"; $message = "<p>";
$message .= sprintf(_t('GoogleSitemaps.METANOTEPRIORITY', "Manually specify a Google Sitemaps priority for this page (%s)"), $message .= sprintf(
'<a href="http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=71936#prioritize" target="_blank">?</a>' _t(
'GoogleSitemaps.METANOTEPRIORITY',
"Manually specify a Google Sitemaps priority for this page (%s)"
),
'<a href="http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=71936#prioritize" '
. 'target="_blank">?</a>'
); );
$message .= "</p>"; $message .= "</p>";
$tabset->push(new Tab('GoogleSitemap', _t('GoogleSitemaps.TABGOOGLESITEMAP', 'Google Sitemap'), $tabset->push(new Tab(
new LiteralField("GoogleSitemapIntro", $message), 'GoogleSitemap',
$priority = new DropdownField("Priority", $this->owner->fieldLabel('Priority'), $prorities, $this->owner->Priority) _t('GoogleSitemaps.TABGOOGLESITEMAP', 'Google Sitemap'),
LiteralField::create("GoogleSitemapIntro", $message),
$priority = DropdownField::create(
"Priority",
$this->owner->fieldLabel('Priority'),
$prorities,
$this->owner->Priority
)
)); ));
$priority->setEmptyString(_t('GoogleSitemaps.PRIORITYAUTOSET', 'Auto-set based on page depth')); $priority->setEmptyString(_t('GoogleSitemaps.PRIORITYAUTOSET', 'Auto-set based on page depth'));

View File

@ -42,15 +42,15 @@ use ReflectionException;
* e.g mysite/_config/googlesitemaps.yml * e.g mysite/_config/googlesitemaps.yml
* *
* <example> * <example>
* --- * ---
* Name: customgooglesitemaps * Name: customgooglesitemaps
* After: googlesitemaps * After: googlesitemaps
* --- * ---
* Wilr\GoogleSitemaps\GoogleSitemap: * Wilr\GoogleSitemaps\GoogleSitemap:
* enabled: true * enabled: true
* objects_per_sitemap: 1000 * objects_per_sitemap: 1000
* google_notification_enabled: true * google_notification_enabled: true
* use_show_in_search: true * use_show_in_search: true
* </example> * </example>
* *
* @see http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=34609 * @see http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=34609
@ -249,11 +249,11 @@ class GoogleSitemap
if ($class == 'SilverStripe\CMS\Model\SiteTree') { if ($class == 'SilverStripe\CMS\Model\SiteTree') {
$instances = Versioned::get_by_stage('SilverStripe\CMS\Model\SiteTree', 'Live'); $instances = Versioned::get_by_stage('SilverStripe\CMS\Model\SiteTree', 'Live');
if($filter) { if ($filter) {
$instances = $instances->filter('ShowInSearch', 1); $instances = $instances->filter('ShowInSearch', 1);
} }
if($redirector) { if ($redirector) {
foreach (ClassInfo::subclassesFor('SilverStripe\\CMS\\Model\\RedirectorPage') as $redirectorClass) { foreach (ClassInfo::subclassesFor('SilverStripe\\CMS\\Model\\RedirectorPage') as $redirectorClass) {
$instances = $instances->exclude('ClassName', $redirectorClass); $instances = $instances->exclude('ClassName', $redirectorClass);
} }
@ -473,15 +473,19 @@ class GoogleSitemap
)); ));
$googleResponse = self::send_ping( $googleResponse = self::send_ping(
"www.google.com", "/webmasters/sitemaps/ping", sprintf("sitemap=%s", $location) "www.google.com",
"/webmasters/sitemaps/ping",
sprintf("sitemap=%s", $location)
); );
// bing // bing
$bing = Config::inst()->get(__CLASS__, 'bing_notification_enabled'); $bing = Config::inst()->get(__CLASS__, 'bing_notification_enabled');
if($bing) { if ($bing) {
$bingResponse = self::send_ping( $bingResponse = self::send_ping(
"www.bing.com", "/ping", sprintf("sitemap=%s", $location) "www.bing.com",
"/ping",
sprintf("sitemap=%s", $location)
); );
} }

View File

@ -2,22 +2,21 @@
namespace Wilr\GoogleSitemaps\Tests; namespace Wilr\GoogleSitemaps\Tests;
use Exception;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\FunctionalTest; use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\LiteralField; use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\Tab; use SilverStripe\Forms\Tab;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use SilverStripe\Versioned\Versioned; use SilverStripe\Versioned\Versioned;
use Wilr\GoogleSitemaps\GoogleSitemap;
use Wilr\GoogleSitemaps\Extensions\GoogleSitemapExtension; use Wilr\GoogleSitemaps\Extensions\GoogleSitemapExtension;
use Wilr\GoogleSitemaps\Tests\Model\TestDataObject; use Wilr\GoogleSitemaps\GoogleSitemap;
use Wilr\GoogleSitemaps\Tests\Model\OtherDataObject; use Wilr\GoogleSitemaps\Tests\Model\OtherDataObject;
use Wilr\GoogleSitemaps\Tests\Model\TestDataObject;
use Wilr\GoogleSitemaps\Tests\Model\UnviewableDataObject; use Wilr\GoogleSitemaps\Tests\Model\UnviewableDataObject;
use Exception;
class GoogleSitemapTest extends FunctionalTest class GoogleSitemapTest extends FunctionalTest
{ {
@ -117,14 +116,32 @@ class GoogleSitemapTest extends FunctionalTest
// the sitemap should contain <loc> to both those files and not the other // the sitemap should contain <loc> to both those files and not the other
// dataobject as it hasn't been registered // dataobject as it hasn't been registered
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1") ."</loc>"; $expected = "<loc>". Director::absoluteURL(
$this->assertEquals(1, substr_count($body, $expected), 'A link to GoogleSitemapTest_DataObject exists'); "sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1"
) ."</loc>";
$this->assertEquals(
1,
substr_count($body, $expected),
'A link to GoogleSitemapTest_DataObject exists'
);
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-OtherDataObject/1") ."</loc>"; $expected = "<loc>". Director::absoluteURL(
$this->assertEquals(1, substr_count($body, $expected), 'A link to GoogleSitemapTest_OtherDataObject exists'); "sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-OtherDataObject/1"
) ."</loc>";
$this->assertEquals(
1,
substr_count($body, $expected),
'A link to GoogleSitemapTest_OtherDataObject exists'
);
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-UnviewableDataObject/2") ."</loc>"; $expected = "<loc>". Director::absoluteURL(
$this->assertEquals(0, substr_count($body, $expected), 'A link to a GoogleSitemapTest_UnviewableDataObject does not exist'); "sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-UnviewableDataObject/2"
) ."</loc>";
$this->assertEquals(
0,
substr_count($body, $expected),
'A link to a GoogleSitemapTest_UnviewableDataObject does not exist'
);
} }
public function testLastModifiedDateOnRootXML() public function testLastModifiedDateOnRootXML()
@ -151,7 +168,11 @@ class GoogleSitemapTest extends FunctionalTest
$expected = '<lastmod>2014-03-14</lastmod>'; $expected = '<lastmod>2014-03-14</lastmod>';
$this->assertEquals(1, substr_count($body, $expected), 'The last mod date should use most recent LastEdited date'); $this->assertEquals(
1,
substr_count($body, $expected),
'The last mod date should use most recent LastEdited date'
);
} }
public function testIndexFilePaginatedSitemapFiles() public function testIndexFilePaginatedSitemapFiles()
@ -162,11 +183,23 @@ class GoogleSitemapTest extends FunctionalTest
$response = $this->get('sitemap.xml'); $response = $this->get('sitemap.xml');
$body = $response->getBody(); $body = $response->getBody();
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1") ."</loc>"; $expected = "<loc>". Director::absoluteURL(
$this->assertEquals(1, substr_count($body, $expected), 'A link to the first page of GoogleSitemapTest_DataObject exists'); "sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1"
) ."</loc>";
$this->assertEquals(
1,
substr_count($body, $expected),
'A link to the first page of GoogleSitemapTest_DataObject exists'
);
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/2") ."</loc>"; $expected = "<loc>". Director::absoluteURL(
$this->assertEquals(1, substr_count($body, $expected), 'A link to the second page GoogleSitemapTest_DataObject exists'); "sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/2"
) ."</loc>";
$this->assertEquals(
1,
substr_count($body, $expected),
'A link to the second page GoogleSitemapTest_DataObject exists'
);
Config::inst()->update(GoogleSitemap::class, 'objects_per_sitemap', $original); Config::inst()->update(GoogleSitemap::class, 'objects_per_sitemap', $original);
} }
@ -218,7 +251,7 @@ class GoogleSitemapTest extends FunctionalTest
$this->assertDOSContains(array( $this->assertDOSContains(array(
array('Title' => 'Testpage1'), array('Title' => 'Testpage1'),
array('Title' => 'Testpage2') array('Title' => 'Testpage2')
), GoogleSitemap::get_items('\SilverStripe\CMS\Model\SiteTree'), "There should be 2 pages in the sitemap after publishing"); ), GoogleSitemap::inst()->getItems(SiteTree::class), "There should be 2 pages in the sitemap after publishing");
// check if we make a page readonly that it is hidden // check if we make a page readonly that it is hidden
$page2->CanViewType = 'LoggedInUsers'; $page2->CanViewType = 'LoggedInUsers';
@ -229,7 +262,7 @@ class GoogleSitemapTest extends FunctionalTest
$this->assertDOSEquals(array( $this->assertDOSEquals(array(
array('Title' => 'Testpage1') array('Title' => 'Testpage1')
), GoogleSitemap::get_items('\SilverStripe\CMS\Model\SiteTree'), "There should be only 1 page, other is logged in only"); ), GoogleSitemap::inst()->getItems(SiteTree::class), "There should be only 1 page, other is logged in only");
} }
public function testAccess() public function testAccess()

View File

@ -6,7 +6,6 @@ use SilverStripe\ORM\DataObject;
use SilverStripe\Dev\TestOnly; use SilverStripe\Dev\TestOnly;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
class TestDataObject extends DataObject implements TestOnly class TestDataObject extends DataObject implements TestOnly
{ {