2011-06-12 16:14:29 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2011-06-20 15:56:01 +02:00
|
|
|
* @package googlesitemaps
|
2012-06-30 11:39:35 +02:00
|
|
|
* @subpackage tests
|
2011-06-12 16:14:29 +02:00
|
|
|
*/
|
2011-06-20 15:56:01 +02:00
|
|
|
class GoogleSitemapTest extends FunctionalTest {
|
|
|
|
|
|
|
|
public static $fixture_file = 'googlesitemaps/tests/GoogleSitemapTest.yml';
|
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'GoogleSitemapTest_DataObject',
|
|
|
|
'GoogleSitemapTest_OtherDataObject',
|
|
|
|
'GoogleSitemapTest_UnviewableDataObject'
|
|
|
|
);
|
|
|
|
|
2012-07-06 06:44:26 +02:00
|
|
|
public function setUp() {
|
2012-06-30 11:39:35 +02:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
if(class_exists('Page')) {
|
|
|
|
$this->loadFixture('googlesitemaps/tests/GoogleSitemapPageTest.yml');
|
|
|
|
}
|
2012-09-06 12:20:44 +02:00
|
|
|
|
2013-01-15 10:29:59 +01:00
|
|
|
GoogleSitemap::clear_registered_dataobjects();
|
2013-01-17 22:49:32 +01:00
|
|
|
GoogleSitemap::clear_registered_routes();
|
2012-06-30 11:39:35 +02:00
|
|
|
}
|
|
|
|
|
2012-07-23 05:01:33 +02:00
|
|
|
public function tearDown() {
|
|
|
|
parent::tearDown();
|
|
|
|
|
|
|
|
GoogleSitemap::clear_registered_dataobjects();
|
2013-01-17 22:49:32 +01:00
|
|
|
GoogleSitemap::clear_registered_routes();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIndexFileWithCustomRoute() {
|
|
|
|
GoogleSitemap::register_route('/test/');
|
|
|
|
|
|
|
|
$response = $this->get('sitemap.xml');
|
|
|
|
$body = $response->getBody();
|
|
|
|
|
|
|
|
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapRoute/1") ."</loc>";
|
|
|
|
$this->assertEquals(1, substr_count($body, $expected) , 'A link to the custom routes exists');
|
2012-07-23 05:01:33 +02:00
|
|
|
}
|
|
|
|
|
2012-06-30 11:39:35 +02:00
|
|
|
|
2013-01-15 10:29:59 +01:00
|
|
|
public function testGetItems() {
|
2012-06-30 11:39:35 +02:00
|
|
|
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject", '');
|
2013-01-15 10:29:59 +01:00
|
|
|
|
|
|
|
$items = GoogleSitemap::get_items('GoogleSitemapTest_DataObject', 1);
|
|
|
|
$this->assertEquals(2, $items->count());
|
|
|
|
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array("Priority" => "0.2"),
|
|
|
|
array("Priority" => "0.4")
|
|
|
|
), $items);
|
2012-06-30 11:39:35 +02:00
|
|
|
|
|
|
|
GoogleSitemap::register_dataobject("GoogleSitemapTest_OtherDataObject");
|
2013-01-15 10:29:59 +01:00
|
|
|
$this->assertEquals(1, GoogleSitemap::get_items('GoogleSitemapTest_OtherDataObject', 1)->count());
|
2012-06-30 11:39:35 +02:00
|
|
|
|
|
|
|
GoogleSitemap::register_dataobject("GoogleSitemapTest_UnviewableDataObject");
|
2013-01-15 10:29:59 +01:00
|
|
|
$this->assertEquals(0, GoogleSitemap::get_items('GoogleSitemapTest_UnviewableDataObject', 1)->count());
|
|
|
|
}
|
|
|
|
|
2013-01-17 22:49:32 +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());
|
|
|
|
}
|
|
|
|
|
2013-01-15 10:29:59 +01:00
|
|
|
public function testAccessingSitemapRootXMLFile() {
|
|
|
|
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject");
|
|
|
|
GoogleSitemap::register_dataobject("GoogleSitemapTest_OtherDataObject");
|
|
|
|
|
|
|
|
$response = $this->get('sitemap.xml');
|
|
|
|
$body = $response->getBody();
|
|
|
|
|
|
|
|
// the sitemap should contain <loc> to both those files and not the other
|
|
|
|
// dataobject as it hasn't been registered
|
|
|
|
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapTest_DataObject/1") ."</loc>";
|
|
|
|
$this->assertEquals(1, substr_count($body, $expected) , 'A link to GoogleSitemapTest_DataObject exists');
|
|
|
|
|
|
|
|
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapTest_OtherDataObject/1") ."</loc>";
|
|
|
|
$this->assertEquals(1, substr_count($body, $expected) , 'A link to GoogleSitemapTest_OtherDataObject exists');
|
|
|
|
|
|
|
|
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapTest_UnviewableDataObject/2") ."</loc>";
|
|
|
|
$this->assertEquals(0, substr_count($body, $expected) , 'A link to a GoogleSitemapTest_UnviewableDataObject does not exist');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLastModifiedDateOnRootXML() {
|
|
|
|
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject");
|
|
|
|
|
|
|
|
DB::query("
|
|
|
|
UPDATE GoogleSitemapTest_DataObject SET LastEdited = '2012-01-14'"
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->get('sitemap.xml');
|
|
|
|
$body = $response->getBody();
|
|
|
|
|
|
|
|
$expected = "<lastmod>2012-01-14</lastmod>";
|
|
|
|
$this->assertEquals(1, substr_count($body, $expected));
|
2012-06-30 11:39:35 +02:00
|
|
|
}
|
|
|
|
|
2013-01-15 10:29:59 +01:00
|
|
|
public function testIndexFilePaginatedSitemapFiles() {
|
|
|
|
$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');
|
|
|
|
$body = $response->getBody();
|
|
|
|
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapTest_DataObject/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/GoogleSitemapTest_DataObject/2") ."</loc>";
|
|
|
|
$this->assertEquals(1, substr_count($body, $expected) , 'A link to the second page GoogleSitemapTest_DataObject exists');
|
|
|
|
|
|
|
|
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', $original);
|
|
|
|
}
|
|
|
|
|
2013-01-17 22:49:32 +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>"));
|
|
|
|
}
|
|
|
|
|
2013-01-15 10:29:59 +01:00
|
|
|
public function testAccessingNestedSiteMap() {
|
|
|
|
$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 testGetItemsWithPages() {
|
2012-06-30 11:39:35 +02:00
|
|
|
if(!class_exists('Page')) {
|
|
|
|
$this->markTestIncomplete('No cms module installed, page related test skipped');
|
|
|
|
}
|
2012-09-06 12:20:44 +02:00
|
|
|
|
2011-06-20 15:56:01 +02:00
|
|
|
$page = $this->objFromFixture('Page', 'Page1');
|
|
|
|
$page->publish('Stage', 'Live');
|
|
|
|
$page->flushCache();
|
2012-06-30 11:39:35 +02:00
|
|
|
|
2011-06-20 15:56:01 +02:00
|
|
|
$page2 = $this->objFromFixture('Page', 'Page2');
|
|
|
|
$page2->publish('Stage', 'Live');
|
|
|
|
$page2->flushCache();
|
|
|
|
|
2012-09-06 12:20:44 +02:00
|
|
|
$this->assertDOSContains(array(
|
2011-06-20 15:56:01 +02:00
|
|
|
array('Title' => 'Testpage1'),
|
|
|
|
array('Title' => 'Testpage2')
|
2013-01-15 10:29:59 +01:00
|
|
|
), GoogleSitemap::get_items('SiteTree'), "There should be 2 pages in the sitemap after publishing");
|
2012-06-30 11:39:35 +02:00
|
|
|
|
2011-06-20 15:56:01 +02:00
|
|
|
// check if we make a page readonly that it is hidden
|
|
|
|
$page2->CanViewType = 'LoggedInUsers';
|
|
|
|
$page2->write();
|
|
|
|
$page2->publish('Stage', 'Live');
|
2012-06-30 11:39:35 +02:00
|
|
|
|
2011-06-20 15:56:01 +02:00
|
|
|
$this->session()->inst_set('loggedInAs', null);
|
2012-09-06 12:20:44 +02:00
|
|
|
|
2011-06-20 15:56:01 +02:00
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array('Title' => 'Testpage1')
|
2013-01-15 10:29:59 +01:00
|
|
|
), GoogleSitemap::get_items('SiteTree'), "There should be only 1 page, other is logged in only");
|
2011-06-20 15:56:01 +02:00
|
|
|
}
|
|
|
|
|
2012-07-06 06:44:26 +02:00
|
|
|
public function testAccess() {
|
2013-01-15 10:29:59 +01:00
|
|
|
Config::inst()->update('GoogleSitemap', 'enabled', true);
|
2011-06-20 15:56:01 +02:00
|
|
|
|
|
|
|
$response = $this->get('sitemap.xml');
|
2012-06-30 11:39:35 +02:00
|
|
|
|
2011-06-20 15:56:01 +02:00
|
|
|
$this->assertEquals(200, $response->getStatusCode(), 'Sitemap returns a 200 success when enabled');
|
|
|
|
$this->assertEquals('application/xml; charset="utf-8"', $response->getHeader('Content-Type'));
|
|
|
|
|
2013-01-15 10:29:59 +01:00
|
|
|
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');
|
|
|
|
$this->assertEquals('application/xml; charset="utf-8"', $response->getHeader('Content-Type'));
|
|
|
|
|
|
|
|
Config::inst()->remove('GoogleSitemap', 'enabled');
|
|
|
|
Config::inst()->update('GoogleSitemap', 'enabled', false);
|
2011-06-20 15:56:01 +02:00
|
|
|
|
|
|
|
$response = $this->get('sitemap.xml');
|
2013-01-15 10:29:59 +01:00
|
|
|
$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');
|
2011-06-20 15:56:01 +02:00
|
|
|
}
|
|
|
|
|
2012-07-06 06:44:26 +02:00
|
|
|
public function testDecoratorAddsFields() {
|
2012-06-30 11:39:35 +02:00
|
|
|
if(!class_exists("Page")) {
|
|
|
|
$this->markTestIncomplete('No cms module installed, page related test skipped');
|
|
|
|
}
|
|
|
|
|
2011-06-20 15:56:01 +02:00
|
|
|
$page = $this->objFromFixture('Page', 'Page1');
|
2012-06-30 11:39:35 +02:00
|
|
|
|
2012-03-26 11:50:30 +02:00
|
|
|
$fields = $page->getSettingsFields();
|
|
|
|
$tab = $fields->fieldByName('Root')->fieldByName('Settings')->fieldByName('GoogleSitemap');
|
2012-06-30 11:39:35 +02:00
|
|
|
|
2011-06-20 15:56:01 +02:00
|
|
|
$this->assertInstanceOf('Tab', $tab);
|
|
|
|
$this->assertInstanceOf('DropdownField', $tab->fieldByName('Priority'));
|
|
|
|
$this->assertInstanceOf('LiteralField', $tab->fieldByName('GoogleSitemapIntro'));
|
|
|
|
}
|
|
|
|
|
2012-07-06 06:44:26 +02:00
|
|
|
public function testGetPriority() {
|
2012-06-30 11:39:35 +02:00
|
|
|
if(!class_exists("Page")) {
|
|
|
|
$this->markTestIncomplete('No cms module installed, page related test skipped');
|
|
|
|
}
|
2011-06-20 15:56:01 +02:00
|
|
|
|
2012-06-30 11:39:35 +02:00
|
|
|
$page = $this->objFromFixture('Page', 'Page1');
|
|
|
|
|
2011-06-20 15:56:01 +02:00
|
|
|
// invalid field doesn't break google
|
|
|
|
$page->Priority = 'foo';
|
2013-01-15 10:29:59 +01:00
|
|
|
$this->assertEquals(0.5, $page->getGooglePriority());
|
2011-06-20 15:56:01 +02:00
|
|
|
|
2013-01-15 10:29:59 +01:00
|
|
|
// -1 indicates that we should not index this
|
2011-06-20 15:56:01 +02:00
|
|
|
$page->Priority = -1;
|
2013-01-15 10:29:59 +01:00
|
|
|
$this->assertFalse($page->getGooglePriority());
|
2011-06-20 15:56:01 +02:00
|
|
|
}
|
2014-01-27 22:03:55 +01:00
|
|
|
|
|
|
|
public function testUnpublishedPage() {
|
|
|
|
|
|
|
|
if(!class_exists('SiteTree')) {
|
|
|
|
$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');
|
|
|
|
|
|
|
|
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);
|
|
|
|
// }
|
|
|
|
}
|
2011-06-12 16:14:29 +02:00
|
|
|
}
|
|
|
|
|
2011-06-20 15:56:01 +02:00
|
|
|
/**
|
|
|
|
* @package googlesitemaps
|
2012-06-30 11:39:35 +02:00
|
|
|
* @subpackage tests
|
2011-06-20 15:56:01 +02:00
|
|
|
*/
|
|
|
|
class GoogleSitemapTest_DataObject extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
public static $db = array(
|
|
|
|
'Priority' => 'Varchar(10)'
|
|
|
|
);
|
|
|
|
|
|
|
|
public function canView($member = null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function AbsoluteLink() {
|
2013-08-23 23:34:11 +02:00
|
|
|
return Director::absoluteBaseURL();
|
2011-06-20 15:56:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @package googlesitemaps
|
2012-06-30 11:39:35 +02:00
|
|
|
* @subpackage tests
|
2011-06-20 15:56:01 +02:00
|
|
|
*/
|
|
|
|
class GoogleSitemapTest_OtherDataObject extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
public static $db = array(
|
|
|
|
'Priority' => 'Varchar(10)'
|
|
|
|
);
|
|
|
|
|
|
|
|
public function canView($member = null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function AbsoluteLink() {
|
2013-08-23 23:34:11 +02:00
|
|
|
return Director::absoluteBaseURL();
|
2011-06-20 15:56:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @package googlesitemaps
|
2012-06-30 11:39:35 +02:00
|
|
|
* @subpackage tests
|
2011-06-20 15:56:01 +02:00
|
|
|
*/
|
|
|
|
class GoogleSitemapTest_UnviewableDataObject extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
public static $db = array(
|
|
|
|
'Priority' => 'Varchar(10)'
|
|
|
|
);
|
|
|
|
|
|
|
|
public function canView($member = null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function AbsoluteLink() {
|
2013-08-23 23:34:11 +02:00
|
|
|
return Director::absoluteBaseURL();
|
2011-06-20 15:56:01 +02:00
|
|
|
}
|
|
|
|
}
|