Update tests

This commit is contained in:
Will Rossiter 2017-10-20 11:16:53 +13:00
parent 2699956b23
commit c8e142718d
10 changed files with 184 additions and 200 deletions

View File

@ -1,27 +1,33 @@
# See https://github.com/silverstripe-labs/silverstripe-travis-support for setup details
sudo: false
language: php language: php
php: dist: trusty
- 5.5
- 5.6
- 7.0
env: env:
- DB=MYSQL CORE_RELEASE=master global:
- COMPOSER_ROOT_VERSION=2.x-dev
matrix: matrix:
include: include:
- php: 5.6 - php: 5.6
env: DB=PGSQL CORE_RELEASE=master env: DB=MYSQL PHPCS_TEST=1 PHPUNIT_TEST=1
- php: 7.0
env: DB=MYSQL PHPUNIT_TEST=1
- php: 7.1
env: DB=MYSQL PHPUNIT_COVERAGE_TEST=1
before_script: before_script:
- composer self-update || true # Init PHP
- git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support - phpenv rehash
- php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss - phpenv config-rm xdebug.ini
- cd ~/builds/ss - composer validate
- composer require silverstripe/recipe-cms 1.0.x-dev --no-update
- if [[ $DB == PGSQL ]]; then composer require silverstripe/postgresql:2.0.x-dev --no-update; fi
- composer install --prefer-dist --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile
script: script:
- vendor/bin/phpunit googlesitemaps/tests - if [[ $PHPUNIT_TEST ]]; then vendor/bin/phpunit tests/; fi
- if [[ $PHPUNIT_COVERAGE_TEST ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=coverage.xml; fi
- if [[ $PHPCS_TEST ]]; then vendor/bin/phpcs --standard=vendor/silverstripe/framework/phpcs.xml.dist src/ tests/ ; fi
after_success:
- if [[ $PHPUNIT_COVERAGE_TEST ]]; then bash <(curl -s https://codecov.io/bash) -f coverage.xml; fi

14
phpunit.xml.dist Normal file
View File

@ -0,0 +1,14 @@
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<testsuite name="Default">
<directory>tests</directory>
</testsuite>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
<exclude>
<directory suffix=".php">tests/</directory>
</exclude>
</whitelist>
</filter>
</phpunit>

View File

@ -38,13 +38,17 @@ class GoogleSitemapExtension extends DataExtension
$can = $this->owner->getGooglePriority(); $can = $this->owner->getGooglePriority();
} }
if ($can === 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. 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. // 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) {
$can = min($override);
}
if ($override) {
$can = min($override, $can);
}
return $can; return $can;
} }

View File

@ -2,7 +2,6 @@
namespace Wilr\GoogleSitemaps; namespace Wilr\GoogleSitemaps;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
use SilverStripe\Core\ClassInfo; use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
@ -14,7 +13,10 @@ use SilverStripe\View\ArrayData;
use SilverStripe\Core\Extensible; use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Injectable; use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Config\Configurable;
use Wilr\GoogleSitemaps\Extensions\GoogleSitemapExtension;
use ReflectionClass; use ReflectionClass;
use ReflectionException;
/** /**
* Sitemaps are a way to tell Google about pages on your site that they might * Sitemaps are a way to tell Google about pages on your site that they might
* not otherwise discover. In its simplest terms, a XML Sitemap usually called * not otherwise discover. In its simplest terms, a XML Sitemap usually called
@ -24,16 +26,18 @@ use ReflectionClass;
* all the pages on your site, including URLs that may not be discoverable by * all the pages on your site, including URLs that may not be discoverable by
* Google's normal crawling process. * Google's normal crawling process.
* *
* The GoogleSitemap handle requests to 'sitemap.xml' * The GoogleSitemap handle requests to 'sitemap.xml' the other two classes are
* the other two classes are used to render the sitemap. * used to render the sitemap.
* *
* You can notify ("ping") Google about a changed sitemap * You can notify ("ping") Google about a changed sitemap automatically whenever
* automatically whenever a new page is published or unpublished. * a new page is published or unpublished.
* By default, Google is not notified, and will pick up your new *
* sitemap whenever the GoogleBot visits your website. * By default, Google is not notified, and will pick up your new sitemap
* whenever the GoogleBot visits your website.
* *
* To Enable notification of Google after every publish set google_notification_enabled * To Enable notification of Google after every publish set google_notification_enabled
* to true in the googlesitemaps.yml config file. * to true in the googlesitemaps.yml config file.
*
* This file is usually located in the _config folder of your project folder. * This file is usually located in the _config folder of your project folder.
* e.g mysite/_config/googlesitemaps.yml * e.g mysite/_config/googlesitemaps.yml
* *
@ -42,7 +46,7 @@ use ReflectionClass;
* Name: customgooglesitemaps * Name: customgooglesitemaps
* After: googlesitemaps * After: 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
@ -63,7 +67,7 @@ class GoogleSitemap
* *
* @var array * @var array
*/ */
private static $dataobjects = array(); private static $dataobjects = [];
/** /**
* List of custom routes to include in the sitemap (such as controller * List of custom routes to include in the sitemap (such as controller
@ -71,7 +75,7 @@ class GoogleSitemap
* *
* @var array * @var array
*/ */
private static $routes = array(); private static $routes = [];
/** /**
* @config * @config
@ -97,7 +101,7 @@ class GoogleSitemap
public static function register_dataobject($className, $changeFreq = 'monthly', $priority = '0.6') public static function register_dataobject($className, $changeFreq = 'monthly', $priority = '0.6')
{ {
if (!self::is_registered($className)) { if (!self::is_registered($className)) {
$className::add_extension('GoogleSitemapExtension'); $className::add_extension(GoogleSitemapExtension::class);
self::$dataobjects[$className] = array( self::$dataobjects[$className] = array(
'frequency' => ($changeFreq) ? $changeFreq : 'monthly', 'frequency' => ($changeFreq) ? $changeFreq : 'monthly',
@ -242,8 +246,8 @@ class GoogleSitemap
Translatable::disable_locale_filter(); Translatable::disable_locale_filter();
} }
if ($class == SiteTree::class) { if ($class == 'SilverStripe\CMS\Model\SiteTree') {
$instances = Versioned::get_by_stage(SiteTree::class, '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);
@ -358,7 +362,7 @@ class GoogleSitemap
$sitemaps = new ArrayList(); $sitemaps = new ArrayList();
$filter = Config::inst()->get(__CLASS__, 'use_show_in_search'); $filter = Config::inst()->get(__CLASS__, 'use_show_in_search');
if (class_exists(SiteTree::class)) { if (class_exists('SilverStripe\CMS\Model\SiteTree')) {
// move to extension hook. At the moment moduleexists config hook // move to extension hook. At the moment moduleexists config hook
// does not work. // does not work.
if (class_exists('Translatable')) { if (class_exists('Translatable')) {
@ -366,7 +370,7 @@ class GoogleSitemap
} }
$filter = ($filter) ? "\"ShowInSearch\" = 1" : ""; $filter = ($filter) ? "\"ShowInSearch\" = 1" : "";
$class = SiteTree::class; $class = 'SilverStripe\CMS\Model\SiteTree';
$instances = Versioned::get_by_stage($class, 'Live', $filter); $instances = Versioned::get_by_stage($class, 'Live', $filter);
$this->extend("alterDataList", $instances, $class); $this->extend("alterDataList", $instances, $class);
$count = $instances->count(); $count = $instances->count();
@ -382,7 +386,7 @@ class GoogleSitemap
$lastModified = ($lastEdited) ? date('Y-m-d', strtotime($lastEdited)) : date('Y-m-d'); $lastModified = ($lastEdited) ? date('Y-m-d', strtotime($lastEdited)) : date('Y-m-d');
$sitemaps->push(new ArrayData(array( $sitemaps->push(new ArrayData(array(
'ClassName' => $this->sanitiseClassName(SiteTree::class), 'ClassName' => $this->sanitiseClassName('SilverStripe\CMS\Model\SiteTree'),
'LastModified' => $lastModified, 'LastModified' => $lastModified,
'Page' => $i 'Page' => $i
))); )));

View File

@ -1,6 +1,7 @@
<?php <?php
use SilverStripe\CMS\Model\SiteTree; namespace Wilr\GoogleSitemaps\Tests;
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;
@ -11,30 +12,33 @@ use SilverStripe\Forms\Tab;
use SilverStripe\ORM\DataObject; 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\Tests\Model\TestDataObject;
use Wilr\GoogleSitemaps\Tests\Model\OtherDataObject;
use Wilr\GoogleSitemaps\Tests\Model\UnviewableDataObject;
use Exception;
/**
* TODO: Migrate to new instance level interface instead of using static methods for retrieval of site maps and items (i.e. ->getSitemaps() instead of ::get_sitemaps()).
*
* @package googlesitemaps
* @subpackage tests
*/
class GoogleSitemapTest extends FunctionalTest class GoogleSitemapTest extends FunctionalTest
{ {
public static $fixture_file = 'googlesitemaps/tests/GoogleSitemapTest.yml'; protected static $fixture_file = 'GoogleSitemapTest.yml';
protected $extraDataObjects = array( protected static $extra_dataobjects = [
'GoogleSitemapTest_DataObject', TestDataObject::class,
'GoogleSitemapTest_OtherDataObject', OtherDataObject::class,
'GoogleSitemapTest_UnviewableDataObject', UnviewableDataObject::class
'SilverStripe\GoogleSitemaps\Test_DataObject', ];
);
protected static $extra_extensions = [
GoogleSitemapExtension::class
];
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
if (class_exists('Page')) { if (class_exists('Page')) {
$this->loadFixture('googlesitemaps/tests/GoogleSitemapPageTest.yml'); $this->loadFixture($this->resolveFixturePath('GoogleSitemapPageTest.yml'));
} }
GoogleSitemap::clear_registered_dataobjects(); GoogleSitemap::clear_registered_dataobjects();
@ -49,6 +53,17 @@ class GoogleSitemapTest extends FunctionalTest
GoogleSitemap::clear_registered_routes(); GoogleSitemap::clear_registered_routes();
} }
public function testCanIncludeInGoogleSitemap()
{
GoogleSitemap::register_dataobject(TestDataObject::class, '');
$unused = $this->objFromFixture(TestDataObject::class, 'UnindexedDataObject');
$this->assertFalse($unused->canIncludeInGoogleSitemap());
$used = $this->objFromFixture(TestDataObject::class, 'DataObjectTest2');
$this->assertTrue($used->canIncludeInGoogleSitemap());
}
public function testIndexFileWithCustomRoute() public function testIndexFileWithCustomRoute()
{ {
GoogleSitemap::register_route('/test/'); GoogleSitemap::register_route('/test/');
@ -63,9 +78,9 @@ class GoogleSitemapTest extends FunctionalTest
public function testGetItems() public function testGetItems()
{ {
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject", ''); GoogleSitemap::register_dataobject(TestDataObject::class, '');
$items = GoogleSitemap::get_items('GoogleSitemapTest_DataObject', 1); $items = GoogleSitemap::get_items(TestDataObject::class, 1);
$this->assertEquals(2, $items->count()); $this->assertEquals(2, $items->count());
$this->assertDOSEquals(array( $this->assertDOSEquals(array(
@ -73,11 +88,11 @@ class GoogleSitemapTest extends FunctionalTest
array("Priority" => "0.4") array("Priority" => "0.4")
), $items); ), $items);
GoogleSitemap::register_dataobject("GoogleSitemapTest_OtherDataObject"); GoogleSitemap::register_dataobject(OtherDataObject::class);
$this->assertEquals(1, GoogleSitemap::get_items('GoogleSitemapTest_OtherDataObject', 1)->count()); $this->assertEquals(1, GoogleSitemap::get_items(OtherDataObject::class, 1)->count());
GoogleSitemap::register_dataobject("GoogleSitemapTest_UnviewableDataObject"); GoogleSitemap::register_dataobject(UnviewableDataObject::class);
$this->assertEquals(0, GoogleSitemap::get_items('GoogleSitemapTest_UnviewableDataObject', 1)->count()); $this->assertEquals(0, GoogleSitemap::get_items(UnviewableDataObject::class, 1)->count());
} }
public function testGetItemsWithCustomRoutes() public function testGetItemsWithCustomRoutes()
@ -94,27 +109,27 @@ class GoogleSitemapTest extends FunctionalTest
public function testAccessingSitemapRootXMLFile() public function testAccessingSitemapRootXMLFile()
{ {
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject"); GoogleSitemap::register_dataobject(TestDataObject::class);
GoogleSitemap::register_dataobject("GoogleSitemapTest_OtherDataObject"); GoogleSitemap::register_dataobject(OtherDataObject::class);
$response = $this->get('sitemap.xml'); $response = $this->get('sitemap.xml');
$body = $response->getBody(); $body = $response->getBody();
// 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/GoogleSitemapTest_DataObject/1") ."</loc>"; $expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1") ."</loc>";
$this->assertEquals(1, substr_count($body, $expected), 'A link to GoogleSitemapTest_DataObject exists'); $this->assertEquals(1, substr_count($body, $expected), 'A link to GoogleSitemapTest_DataObject exists');
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapTest_OtherDataObject/1") ."</loc>"; $expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-OtherDataObject/1") ."</loc>";
$this->assertEquals(1, substr_count($body, $expected), 'A link to GoogleSitemapTest_OtherDataObject exists'); $this->assertEquals(1, substr_count($body, $expected), 'A link to GoogleSitemapTest_OtherDataObject exists');
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapTest_UnviewableDataObject/2") ."</loc>"; $expected = "<loc>". Director::absoluteURL("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'); $this->assertEquals(0, substr_count($body, $expected), 'A link to a GoogleSitemapTest_UnviewableDataObject does not exist');
} }
public function testLastModifiedDateOnRootXML() public function testLastModifiedDateOnRootXML()
{ {
Config::inst()->update('GoogleSitemap', 'enabled', true); Config::inst()->update(GoogleSitemap::class, 'enabled', true);
if (!class_exists('Page')) { if (!class_exists('Page')) {
$this->markTestIncomplete('No cms module installed, page related test skipped'); $this->markTestIncomplete('No cms module installed, page related test skipped');
@ -142,18 +157,18 @@ class GoogleSitemapTest extends FunctionalTest
public function testIndexFilePaginatedSitemapFiles() public function testIndexFilePaginatedSitemapFiles()
{ {
$original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap'); $original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', 1); Config::inst()->update(GoogleSitemap::class, 'objects_per_sitemap', 1);
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject"); GoogleSitemap::register_dataobject(TestDataObject::class);
$response = $this->get('sitemap.xml'); $response = $this->get('sitemap.xml');
$body = $response->getBody(); $body = $response->getBody();
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapTest_DataObject/1") ."</loc>"; $expected = "<loc>". Director::absoluteURL("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'); $this->assertEquals(1, substr_count($body, $expected), 'A link to the first page of GoogleSitemapTest_DataObject exists');
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapTest_DataObject/2") ."</loc>"; $expected = "<loc>". Director::absoluteURL("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'); $this->assertEquals(1, substr_count($body, $expected), 'A link to the second page GoogleSitemapTest_DataObject exists');
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', $original); Config::inst()->update(GoogleSitemap::class, 'objects_per_sitemap', $original);
} }
public function testRegisterRoutesIncludesAllRoutes() public function testRegisterRoutesIncludesAllRoutes()
@ -175,57 +190,15 @@ class GoogleSitemapTest extends FunctionalTest
public function testAccessingNestedSiteMap() public function testAccessingNestedSiteMap()
{ {
$original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap'); $original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', 1); Config::inst()->update(GoogleSitemap::class, 'objects_per_sitemap', 1);
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject"); GoogleSitemap::register_dataobject(TestDataObject::class);
$response = $this->get('sitemap.xml/sitemap/GoogleSitemapTest_DataObject/1'); $response = $this->get('sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1');
$body = $response->getBody(); $body = $response->getBody();
$this->assertEquals(200, $response->getStatusCode(), 'successful loaded nested sitemap'); $this->assertEquals(200, $response->getStatusCode(), 'successful loaded nested sitemap');
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', $original); Config::inst()->update(GoogleSitemap::class, 'objects_per_sitemap', $original);
}
public function testAccessingNestedSiteMapCaseInsensitive()
{
$original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', 1);
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject");
$response = $this->get('sitemap.xml/sitemap/googlesitemaptest_dataobject/1');
$body = $response->getBody();
$this->assertEquals(200, $response->getStatusCode(), 'successful loaded nested sitemap');
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()
@ -245,41 +218,40 @@ 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(SiteTree::class), "There should be 2 pages in the sitemap after publishing"); ), GoogleSitemap::get_items('\SilverStripe\CMS\Model\SiteTree'), "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';
$page2->write(); $page2->write();
$page2->publish('Stage', 'Live'); $page2->publish('Stage', 'Live');
$this->session()->inst_set('loggedInAs', null); $this->logOut();
$this->assertDOSEquals(array( $this->assertDOSEquals(array(
array('Title' => 'Testpage1') array('Title' => 'Testpage1')
), GoogleSitemap::get_items(SiteTree::class), "There should be only 1 page, other is logged in only"); ), GoogleSitemap::get_items('\SilverStripe\CMS\Model\SiteTree'), "There should be only 1 page, other is logged in only");
} }
public function testAccess() public function testAccess()
{ {
Config::inst()->update('GoogleSitemap', 'enabled', true); Config::inst()->update(GoogleSitemap::class, 'enabled', true);
$response = $this->get('sitemap.xml'); $response = $this->get('sitemap.xml');
$this->assertEquals(200, $response->getStatusCode(), 'Sitemap returns a 200 success when enabled'); $this->assertEquals(200, $response->getStatusCode(), 'Sitemap returns a 200 success when enabled');
$this->assertEquals('application/xml; charset="utf-8"', $response->getHeader('Content-Type')); $this->assertEquals('application/xml; charset="utf-8"', $response->getHeader('Content-Type'));
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject"); GoogleSitemap::register_dataobject(TestDataObject::class);
$response = $this->get('sitemap.xml/sitemap/GoogleSitemapTest_DataObject/1'); $response = $this->get('sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1');
$this->assertEquals(200, $response->getStatusCode(), 'Sitemap returns a 200 success when enabled'); $this->assertEquals(200, $response->getStatusCode(), 'Sitemap returns a 200 success when enabled');
$this->assertEquals('application/xml; charset="utf-8"', $response->getHeader('Content-Type')); $this->assertEquals('application/xml; charset="utf-8"', $response->getHeader('Content-Type'));
Config::inst()->remove('GoogleSitemap', 'enabled'); Config::inst()->update(GoogleSitemap::class, 'enabled', false);
Config::inst()->update('GoogleSitemap', 'enabled', false);
$response = $this->get('sitemap.xml'); $response = $this->get('sitemap.xml');
$this->assertEquals(404, $response->getStatusCode(), 'Sitemap index returns a 404 when disabled'); $this->assertEquals(404, $response->getStatusCode(), 'Sitemap index returns a 404 when disabled');
$response = $this->get('sitemap.xml/sitemap/GoogleSitemapTest_DataObject/1'); $response = $this->get('sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1');
$this->assertEquals(404, $response->getStatusCode(), 'Sitemap file returns a 404 when disabled'); $this->assertEquals(404, $response->getStatusCode(), 'Sitemap file returns a 404 when disabled');
} }
@ -322,16 +294,16 @@ class GoogleSitemapTest extends FunctionalTest
public function testUnpublishedPage() public function testUnpublishedPage()
{ {
if (!class_exists(SiteTree::class)) { if (!class_exists('SilverStripe\CMS\SiteTree')) {
$this->markTestSkipped('Test skipped; CMS module required for testUnpublishedPage'); $this->markTestSkipped('Test skipped; CMS module required for testUnpublishedPage');
} }
$orphanedPage = new SiteTree(); $orphanedPage = new \SilverStripe\CMS\SiteTree();
$orphanedPage->ParentID = 999999; // missing parent id $orphanedPage->ParentID = 999999; // missing parent id
$orphanedPage->write(); $orphanedPage->write();
$orphanedPage->publish("Stage", "Live"); $orphanedPage->publish("Stage", "Live");
$rootPage = new SiteTree(); $rootPage = new \SilverStripe\CMS\SiteTree();
$rootPage->ParentID = 0; $rootPage->ParentID = 0;
$rootPage->write(); $rootPage->write();
$rootPage->publish("Stage", "Live"); $rootPage->publish("Stage", "Live");
@ -352,69 +324,3 @@ class GoogleSitemapTest extends FunctionalTest
// } // }
} }
} }
/**
* @package googlesitemaps
* @subpackage tests
*/
class GoogleSitemapTest_DataObject extends DataObject implements TestOnly
{
private static $db = array(
'Priority' => 'Varchar(10)'
);
public function canView($member = null)
{
return true;
}
public function AbsoluteLink()
{
return Director::absoluteBaseURL();
}
}
/**
* @package googlesitemaps
* @subpackage tests
*/
class GoogleSitemapTest_OtherDataObject extends DataObject implements TestOnly
{
private static $db = array(
'Priority' => 'Varchar(10)'
);
public function canView($member = null)
{
return true;
}
public function AbsoluteLink()
{
return Director::absoluteBaseURL();
}
}
/**
* @package googlesitemaps
* @subpackage tests
*/
class GoogleSitemapTest_UnviewableDataObject extends DataObject implements TestOnly
{
private static $db = array(
'Priority' => 'Varchar(10)'
);
public function canView($member = null)
{
return false;
}
public function AbsoluteLink()
{
return Director::absoluteBaseURL();
}
}

View File

@ -1,4 +1,4 @@
GoogleSitemapTest_DataObject: Wilr\GoogleSitemaps\Tests\Model\TestDataObject:
DataObjectTest1: DataObjectTest1:
Priority: 0.4 Priority: 0.4
DataObjectTest2: DataObjectTest2:
@ -6,10 +6,10 @@ GoogleSitemapTest_DataObject:
UnindexedDataObject: UnindexedDataObject:
Priority: -1 Priority: -1
GoogleSitemapTest_OtherDataObject: Wilr\GoogleSitemaps\Tests\Model\OtherDataObject:
OtherDataObjectTest2: OtherDataObjectTest2:
Priority: 0.3 Priority: 0.3
GoogleSitemapTest_UnviewableDataObject: Wilr\GoogleSitemaps\Tests\Model\UnviewableDataObject:
Unviewable1: Unviewable1:
Priority: 0.4 Priority: 0.4

View File

@ -1,16 +1,16 @@
<?php <?php
namespace SilverStripe\GoogleSitemaps; namespace Wilr\GoogleSitemaps\Tests\Model;
use SilverStripe\Control\Director;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Control\Director;
class Test_DataObject extends DataObject implements TestOnly class OtherDataObject extends DataObject implements TestOnly
{ {
private static $db = [ private static $db = array(
'Priority' => 'Varchar(10)' 'Priority' => 'Varchar(10)'
]; );
public function canView($member = null) public function canView($member = null)
{ {

View File

@ -0,0 +1,26 @@
<?php
namespace Wilr\GoogleSitemaps\Tests\Model;
use SilverStripe\ORM\DataObject;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Control\Director;
class TestDataObject extends DataObject implements TestOnly
{
private static $db = array(
'Priority' => 'Varchar(10)'
);
public function canView($member = null)
{
return true;
}
public function AbsoluteLink()
{
return Director::absoluteBaseURL();
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace Wilr\GoogleSitemaps\Tests\Model;
use SilverStripe\ORM\DataObject;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Control\Director;
class UnviewableDataObject extends DataObject implements TestOnly
{
private static $db = array(
'Priority' => 'Varchar(10)'
);
public function canView($member = null)
{
return false;
}
public function AbsoluteLink()
{
return Director::absoluteBaseURL();
}
}

View File

@ -7,7 +7,6 @@
<title>XML Sitemap</title> <title>XML Sitemap</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="resources/wilr/silverstripe-googlesitemaps/css/style.css" /> <link rel="stylesheet" href="resources/wilr/silverstripe-googlesitemaps/css/style.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head> </head>
<body> <body>
<div id="content"> <div id="content">