silverstripe-googlesitemaps/tests/GoogleSitemapTest.php

327 lines
12 KiB
PHP
Raw Normal View History

<?php
2017-10-20 00:16:53 +02:00
namespace Wilr\GoogleSitemaps\Tests;
2016-11-02 12:39:25 +01:00
use SilverStripe\Control\Director;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\Tab;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
use SilverStripe\Versioned\Versioned;
2017-10-20 00:16:53 +02:00
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;
2016-11-02 12:39:25 +01:00
2015-12-17 19:09:19 +01:00
class GoogleSitemapTest extends FunctionalTest
{
2017-10-20 00:16:53 +02:00
protected static $fixture_file = 'GoogleSitemapTest.yml';
protected static $extra_dataobjects = [
TestDataObject::class,
OtherDataObject::class,
UnviewableDataObject::class
];
2015-12-17 19:09:19 +01:00
2017-10-20 00:16:53 +02:00
protected static $extra_extensions = [
GoogleSitemapExtension::class
];
2015-12-17 19:09:19 +01:00
public function setUp()
{
parent::setUp();
if (class_exists('Page')) {
2017-10-20 00:16:53 +02:00
$this->loadFixture($this->resolveFixturePath('GoogleSitemapPageTest.yml'));
2015-12-17 19:09:19 +01:00
}
2016-11-02 12:39:25 +01:00
2015-12-17 19:09:19 +01:00
GoogleSitemap::clear_registered_dataobjects();
GoogleSitemap::clear_registered_routes();
}
2015-12-17 19:09:19 +01:00
public function tearDown()
{
parent::tearDown();
2015-12-17 19:09:19 +01:00
GoogleSitemap::clear_registered_dataobjects();
GoogleSitemap::clear_registered_routes();
}
2017-10-20 00:16:53 +02:00
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());
}
2015-12-17 19:09:19 +01:00
public function testIndexFileWithCustomRoute()
{
GoogleSitemap::register_route('/test/');
2015-12-17 19:09:19 +01:00
$response = $this->get('sitemap.xml');
$body = $response->getBody();
2015-12-17 19:09:19 +01:00
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapRoute/1") ."</loc>";
$this->assertEquals(1, substr_count($body, $expected), 'A link to the custom routes exists');
}
public function testGetItems()
{
2017-10-20 00:16:53 +02:00
GoogleSitemap::register_dataobject(TestDataObject::class, '');
2015-12-17 19:09:19 +01:00
2017-10-20 00:16:53 +02:00
$items = GoogleSitemap::get_items(TestDataObject::class, 1);
2015-12-17 19:09:19 +01:00
$this->assertEquals(2, $items->count());
$this->assertDOSEquals(array(
array("Priority" => "0.2"),
array("Priority" => "0.4")
), $items);
2017-10-20 00:16:53 +02:00
GoogleSitemap::register_dataobject(OtherDataObject::class);
$this->assertEquals(1, GoogleSitemap::get_items(OtherDataObject::class, 1)->count());
2015-12-17 19:09:19 +01:00
2017-10-20 00:16:53 +02:00
GoogleSitemap::register_dataobject(UnviewableDataObject::class);
$this->assertEquals(0, GoogleSitemap::get_items(UnviewableDataObject::class, 1)->count());
2015-12-17 19:09:19 +01:00
}
public function testGetItemsWithCustomRoutes()
{
GoogleSitemap::register_routes(array(
'/test-route/',
'/someother-route/',
'/fake-sitemap-route/'
));
$items = GoogleSitemap::get_items('GoogleSitemapRoute', 1);
$this->assertEquals(3, $items->count());
}
public function testAccessingSitemapRootXMLFile()
{
2017-10-20 00:16:53 +02:00
GoogleSitemap::register_dataobject(TestDataObject::class);
GoogleSitemap::register_dataobject(OtherDataObject::class);
2015-12-17 19:09:19 +01:00
$response = $this->get('sitemap.xml');
$body = $response->getBody();
2015-12-17 19:09:19 +01:00
// the sitemap should contain <loc> to both those files and not the other
// dataobject as it hasn't been registered
2017-10-20 00:16:53 +02:00
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1") ."</loc>";
2015-12-17 19:09:19 +01:00
$this->assertEquals(1, substr_count($body, $expected), 'A link to GoogleSitemapTest_DataObject exists');
2016-11-02 12:39:25 +01:00
2017-10-20 00:16:53 +02:00
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-OtherDataObject/1") ."</loc>";
2015-12-17 19:09:19 +01:00
$this->assertEquals(1, substr_count($body, $expected), 'A link to GoogleSitemapTest_OtherDataObject exists');
2017-10-20 00:16:53 +02:00
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-UnviewableDataObject/2") ."</loc>";
2015-12-17 19:09:19 +01:00
$this->assertEquals(0, substr_count($body, $expected), 'A link to a GoogleSitemapTest_UnviewableDataObject does not exist');
}
public function testLastModifiedDateOnRootXML()
{
2017-10-20 00:16:53 +02:00
Config::inst()->update(GoogleSitemap::class, 'enabled', true);
2015-12-17 19:09:19 +01:00
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();
DB::query("UPDATE \"SiteTree_Live\" SET \"LastEdited\"='2014-03-14 00:00:00' WHERE \"ID\"='".$page->ID."'");
DB::query("UPDATE \"SiteTree_Live\" SET \"LastEdited\"='2014-01-01 00:00:00' WHERE \"ID\"='".$page2->ID."'");
$response = $this->get('sitemap.xml');
$body = $response->getBody();
$expected = '<lastmod>2014-03-14</lastmod>';
$this->assertEquals(1, substr_count($body, $expected), 'The last mod date should use most recent LastEdited date');
}
public function testIndexFilePaginatedSitemapFiles()
{
$original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
2017-10-20 00:16:53 +02:00
Config::inst()->update(GoogleSitemap::class, 'objects_per_sitemap', 1);
GoogleSitemap::register_dataobject(TestDataObject::class);
2015-12-17 19:09:19 +01:00
$response = $this->get('sitemap.xml');
$body = $response->getBody();
2017-10-20 00:16:53 +02:00
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1") ."</loc>";
2015-12-17 19:09:19 +01:00
$this->assertEquals(1, substr_count($body, $expected), 'A link to the first page of GoogleSitemapTest_DataObject exists');
2017-10-20 00:16:53 +02:00
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/2") ."</loc>";
2015-12-17 19:09:19 +01:00
$this->assertEquals(1, substr_count($body, $expected), 'A link to the second page GoogleSitemapTest_DataObject exists');
2017-10-20 00:16:53 +02:00
Config::inst()->update(GoogleSitemap::class, 'objects_per_sitemap', $original);
2015-12-17 19:09:19 +01:00
}
public function testRegisterRoutesIncludesAllRoutes()
{
GoogleSitemap::register_route('/test/');
GoogleSitemap::register_routes(array(
'/test/', // duplication should be replaced
'/unittests/',
'/anotherlink/'
), 'weekly');
$response = $this->get('sitemap.xml/sitemap/GoogleSitemapRoute/1');
$body = $response->getBody();
$this->assertEquals(200, $response->getStatusCode(), 'successful loaded nested sitemap');
$this->assertEquals(3, substr_count($body, "<loc>"));
}
public function testAccessingNestedSiteMap()
{
$original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
2017-10-20 00:16:53 +02:00
Config::inst()->update(GoogleSitemap::class, 'objects_per_sitemap', 1);
GoogleSitemap::register_dataobject(TestDataObject::class);
2017-10-20 00:16:53 +02:00
$response = $this->get('sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1');
$body = $response->getBody();
$this->assertEquals(200, $response->getStatusCode(), 'successful loaded nested sitemap');
2015-12-17 19:09:19 +01:00
2017-10-20 00:16:53 +02:00
Config::inst()->update(GoogleSitemap::class, 'objects_per_sitemap', $original);
2015-12-17 19:09:19 +01:00
}
public function testGetItemsWithPages()
{
if (!class_exists('Page')) {
$this->markTestIncomplete('No cms module installed, page related test skipped');
}
2016-11-02 12:39:25 +01:00
2015-12-17 19:09:19 +01:00
$page = $this->objFromFixture('Page', 'Page1');
$page->publish('Stage', 'Live');
$page->flushCache();
2016-11-02 12:39:25 +01:00
2015-12-17 19:09:19 +01:00
$page2 = $this->objFromFixture('Page', 'Page2');
$page2->publish('Stage', 'Live');
$page2->flushCache();
$this->assertDOSContains(array(
array('Title' => 'Testpage1'),
array('Title' => 'Testpage2')
2017-10-20 00:16:53 +02:00
), GoogleSitemap::get_items('\SilverStripe\CMS\Model\SiteTree'), "There should be 2 pages in the sitemap after publishing");
2016-11-02 12:39:25 +01:00
2015-12-17 19:09:19 +01:00
// check if we make a page readonly that it is hidden
$page2->CanViewType = 'LoggedInUsers';
$page2->write();
$page2->publish('Stage', 'Live');
2016-11-02 12:39:25 +01:00
2017-10-20 00:16:53 +02:00
$this->logOut();
2016-11-02 12:39:25 +01:00
2015-12-17 19:09:19 +01:00
$this->assertDOSEquals(array(
array('Title' => 'Testpage1')
2017-10-20 00:16:53 +02:00
), GoogleSitemap::get_items('\SilverStripe\CMS\Model\SiteTree'), "There should be only 1 page, other is logged in only");
2015-12-17 19:09:19 +01:00
}
2016-11-02 12:39:25 +01:00
2015-12-17 19:09:19 +01:00
public function testAccess()
{
2017-10-20 00:16:53 +02:00
Config::inst()->update(GoogleSitemap::class, 'enabled', true);
2016-11-02 12:39:25 +01:00
2015-12-17 19:09:19 +01:00
$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'));
2016-11-02 12:39:25 +01:00
2017-10-20 00:16:53 +02:00
GoogleSitemap::register_dataobject(TestDataObject::class);
$response = $this->get('sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1');
2015-12-17 19:09:19 +01:00
$this->assertEquals(200, $response->getStatusCode(), 'Sitemap returns a 200 success when enabled');
$this->assertEquals('application/xml; charset="utf-8"', $response->getHeader('Content-Type'));
2017-10-20 00:16:53 +02:00
Config::inst()->update(GoogleSitemap::class, 'enabled', false);
2016-11-02 12:39:25 +01:00
2015-12-17 19:09:19 +01:00
$response = $this->get('sitemap.xml');
$this->assertEquals(404, $response->getStatusCode(), 'Sitemap index returns a 404 when disabled');
2017-10-20 00:16:53 +02:00
$response = $this->get('sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1');
2015-12-17 19:09:19 +01:00
$this->assertEquals(404, $response->getStatusCode(), 'Sitemap file returns a 404 when disabled');
}
2016-11-02 12:39:25 +01:00
2015-12-17 19:09:19 +01:00
public function testDecoratorAddsFields()
{
if (!class_exists("Page")) {
$this->markTestIncomplete('No cms module installed, page related test skipped');
}
$page = $this->objFromFixture('Page', 'Page1');
2016-11-02 12:39:25 +01:00
2015-12-17 19:09:19 +01:00
$fields = $page->getSettingsFields();
$tab = $fields->fieldByName('Root')->fieldByName('Settings')->fieldByName('GoogleSitemap');
2016-11-02 12:39:25 +01:00
$this->assertInstanceOf(Tab::class, $tab);
$this->assertInstanceOf(DropdownField::class, $tab->fieldByName('Priority'));
$this->assertInstanceOf(LiteralField::class, $tab->fieldByName('GoogleSitemapIntro'));
2015-12-17 19:09:19 +01:00
}
2016-11-02 12:39:25 +01:00
2015-12-17 19:09:19 +01:00
public function testGetPriority()
{
if (!class_exists("Page")) {
$this->markTestIncomplete('No cms module installed, page related test skipped');
}
2016-11-02 12:39:25 +01:00
2015-12-17 19:09:19 +01:00
$page = $this->objFromFixture('Page', 'Page1');
// invalid field doesn't break google
$page->Priority = 'foo';
$this->assertEquals(0.5, $page->getGooglePriority());
// custom value (set as string as db field is varchar)
$page->Priority = '0.2';
$this->assertEquals(0.2, $page->getGooglePriority());
2016-11-02 12:39:25 +01:00
2015-12-17 19:09:19 +01:00
// -1 indicates that we should not index this
$page->Priority = -1;
$this->assertFalse($page->getGooglePriority());
}
2016-11-02 12:39:25 +01:00
2015-12-17 19:09:19 +01:00
public function testUnpublishedPage()
{
2017-10-20 00:16:53 +02:00
if (!class_exists('SilverStripe\CMS\SiteTree')) {
2015-12-17 19:09:19 +01:00
$this->markTestSkipped('Test skipped; CMS module required for testUnpublishedPage');
}
2016-11-02 12:39:25 +01:00
2017-10-20 00:16:53 +02:00
$orphanedPage = new \SilverStripe\CMS\SiteTree();
2015-12-17 19:09:19 +01:00
$orphanedPage->ParentID = 999999; // missing parent id
$orphanedPage->write();
$orphanedPage->publish("Stage", "Live");
2016-11-02 12:39:25 +01:00
2017-10-20 00:16:53 +02:00
$rootPage = new \SilverStripe\CMS\SiteTree();
2015-12-17 19:09:19 +01:00
$rootPage->ParentID = 0;
$rootPage->write();
$rootPage->publish("Stage", "Live");
2016-11-02 12:39:25 +01:00
2015-12-17 19:09:19 +01:00
$oldMode = Versioned::get_reading_mode();
2016-11-02 12:39:25 +01:00
Versioned::set_reading_mode('Live');
2015-12-17 19:09:19 +01:00
try {
$this->assertEmpty($orphanedPage->hasPublishedParent());
$this->assertEmpty($orphanedPage->canIncludeInGoogleSitemap());
$this->assertNotEmpty($rootPage->hasPublishedParent());
$this->assertNotEmpty($rootPage->canIncludeInGoogleSitemap());
} catch (Exception $ex) {
Versioned::set_reading_mode($oldMode);
throw $ex;
} // finally {
Versioned::set_reading_mode($oldMode);
// }
}
}