View source
<?php
namespace Drupal\Tests\simple_sitemap\Functional;
use Drupal\Core\Url;
class SimplesitemapTest extends SimplesitemapTestBase {
public function testInitialGeneration() {
$this->generator
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseContains('urlset');
$this
->assertSession()
->responseContains(Url::fromRoute('<front>')
->setAbsolute()
->toString());
$this
->assertSession()
->responseContains('1.0');
$this
->assertSession()
->responseContains('daily');
}
public function testAddCustomLink() {
$this->generator
->addCustomLink('/node/' . $this->node
->id(), [
'priority' => 0.2,
'changefreq' => 'monthly',
])
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseContains('node/' . $this->node
->id());
$this
->assertSession()
->responseContains('0.2');
$this
->assertSession()
->responseContains('monthly');
$this
->drupalLogin($this->privilegedUser);
$this
->drupalGet('admin/config/search/simplesitemap/custom');
$this
->assertSession()
->pageTextContains('/node/' . $this->node
->id() . ' 0.2 monthly');
$this->generator
->addCustomLink('/node/' . $this->node
->id(), [
'changefreq' => 'yearly',
])
->generateSitemap('nobatch');
$this
->drupalGet('admin/config/search/simplesitemap/custom');
$this
->assertSession()
->pageTextContains('/node/' . $this->node
->id() . ' yearly');
}
public function testAddCustomLinkDefaults() {
$this->generator
->removeCustomLinks()
->addCustomLink('/node/' . $this->node
->id())
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseContains('node/' . $this->node
->id());
$this
->assertSession()
->responseContains('0.5');
$this
->assertSession()
->responseNotContains('changefreq');
}
public function testRemoveCustomLink() {
$this->generator
->addCustomLink('/node/' . $this->node
->id())
->removeCustomLink('/node/' . $this->node
->id())
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseNotContains('node/' . $this->node
->id());
}
public function testRemoveCustomLinks() {
$this->generator
->removeCustomLinks()
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseNotContains(Url::fromRoute('<front>')
->setAbsolute()
->toString());
}
public function testSetBundleSettings() {
$this
->assertFalse($this->generator
->bundleIsIndexed('node', 'page'));
$this->generator
->removeCustomLinks()
->setBundleSettings('node', 'page', [
'index' => TRUE,
'priority' => 0.5,
'changefreq' => 'hourly',
])
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseContains('node/' . $this->node
->id());
$this
->assertSession()
->responseContains('0.5');
$this
->assertSession()
->responseContains('hourly');
$this
->assertTrue($this->generator
->bundleIsIndexed('node', 'page'));
$this->generator
->setBundleSettings('node', 'page', [
'priority' => 0.9,
])
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseContains('node/' . $this->node
->id());
$this
->assertSession()
->responseNotContains('0.5');
$this
->assertSession()
->responseContains('0.9');
$this->generator
->setBundleSettings('node', 'page', [
'changefreq' => 'daily',
])
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseContains('node/' . $this->node
->id());
$this
->assertSession()
->responseNotContains('hourly');
$this
->assertSession()
->responseContains('daily');
$this->generator
->setBundleSettings('node', 'page', [
'changefreq' => '',
])
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseContains('node/' . $this->node
->id());
$this
->assertSession()
->responseNotContains('changefreq');
$this
->assertSession()
->responseNotContains('daily');
$this
->drupalCreateContentType([
'type' => 'blog',
]);
$node3 = $this
->createNode([
'title' => 'Node3',
'type' => 'blog',
]);
$this->generator
->setBundleSettings('node', 'page', [
'index' => TRUE,
])
->setBundleSettings('node', 'blog', [
'index' => TRUE,
])
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseContains('node/' . $this->node
->id());
$this
->assertSession()
->responseContains('node/' . $node3
->id());
$this->generator
->setBundleSettings('node', 'page', [
'index' => FALSE,
])
->setBundleSettings('node', 'blog', [
'index' => FALSE,
])
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseNotContains('node/' . $this->node
->id());
$this
->assertSession()
->responseNotContains('node/' . $node3
->id());
}
public function testSetBundleSettingsDefaults() {
$this->generator
->setBundleSettings('node', 'page')
->removeCustomLinks()
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseContains('node/' . $this->node
->id());
$this
->assertSession()
->responseContains('0.5');
$this
->assertSession()
->responseNotContains('changefreq');
}
public function testLastmod() {
$this->generator
->setBundleSettings('node', 'page')
->removeCustomLinks()
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseContains('lastmod');
$this->generator
->setBundleSettings('node', 'page', [
'index' => FALSE,
])
->addCustomLink('/node/' . $this->node
->id())
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseContains('lastmod');
$this->generator
->removeCustomLinks()
->addCustomLink('/')
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseNotContains('lastmod');
}
public function testRemoveDuplicatesSetting() {
$this->generator
->setBundleSettings('node', 'page', [
'index' => TRUE,
])
->addCustomLink('/node/1')
->saveSetting('remove_duplicates', TRUE)
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertUniqueTextWorkaround('node/' . $this->node
->id());
$this->generator
->saveSetting('remove_duplicates', FALSE)
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertNoUniqueTextWorkaround('node/' . $this->node
->id());
}
public function testMaxLinksSetting() {
$this->generator
->setBundleSettings('node', 'page')
->saveSetting('max_links', 1)
->removeCustomLinks()
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseContains('sitemaps/1/sitemap.xml');
$this
->assertSession()
->responseContains('sitemaps/2/sitemap.xml');
$this
->drupalGet('sitemaps/1/sitemap.xml');
$this
->assertSession()
->responseContains('node/' . $this->node
->id());
$this
->assertSession()
->responseContains('0.5');
$this
->assertSession()
->responseNotContains('node/' . $this->node2
->id());
$this
->drupalGet('sitemaps/2/sitemap.xml');
$this
->assertSession()
->responseContains('node/' . $this->node2
->id());
$this
->assertSession()
->responseContains('0.5');
$this
->assertSession()
->responseNotContains('node/' . $this->node
->id());
}
public function testBatchProcessLimitSetting() {
for ($i = 3; $i <= 50; $i++) {
$this
->createNode([
'title' => "Node{$i}",
'type' => 'page',
]);
}
$sitemap = $this->generator
->setBundleSettings('node', 'page')
->generateSitemap('nobatch')
->getSitemap();
$sitemap2 = $this->generator
->saveSetting('batch_process_limit', 1)
->generateSitemap('nobatch')
->getSitemap();
$sitemap3 = $this->generator
->saveSetting('batch_process_limit', 10)
->generateSitemap('nobatch')
->getSitemap();
$this
->assertEquals($sitemap2, $sitemap);
$this
->assertEquals($sitemap3, $sitemap);
$sitemap_index = $this->generator
->setBundleSettings('node', 'page')
->saveSetting('batch_process_limit', 1500)
->saveSetting('max_links', 30)
->generateSitemap('nobatch')
->getSitemap();
$sitemap_chunk = $this->generator
->getSitemap(1);
$sitemap_index2 = $this->generator
->saveSetting('batch_process_limit', 1)
->generateSitemap('nobatch')
->getSitemap();
$sitemap_chunk2 = $this->generator
->getSitemap(1);
$sitemap_index3 = $this->generator
->saveSetting('batch_process_limit', 10)
->generateSitemap('nobatch')
->getSitemap();
$sitemap_chunk3 = $this->generator
->getSitemap(1);
$this
->assertSame($sitemap_index2, $sitemap_index);
$this
->assertSame($sitemap_chunk2, $sitemap_chunk);
$this
->assertSame($sitemap_index3, $sitemap_index);
$this
->assertSame($sitemap_chunk3, $sitemap_chunk);
}
public function testBaseUrlSetting() {
$this->generator
->setBundleSettings('node', 'page')
->saveSetting('base_url', 'http://base_url_test')
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseContains('http://base_url_test');
$this->generator
->saveSetting('max_links', 1)
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseContains('http://base_url_test/sitemaps/1/sitemap.xml');
}
public function testCacheability() {
$this->generator
->setBundleSettings('node', 'page')
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertEquals('MISS', $this
->drupalGetHeader('X-Drupal-Cache'));
$this
->assertSession()
->responseContains('node/' . $this->node
->id());
$this
->drupalGet('sitemap.xml');
$this
->assertEquals('HIT', $this
->drupalGetHeader('X-Drupal-Cache'));
$this
->assertSession()
->responseContains('node/' . $this->node
->id());
}
public function testSetEntityInstanceSettings() {
$this->generator
->setBundleSettings('node', 'page')
->removeCustomLinks()
->setEntityInstanceSettings('node', $this->node
->id(), [
'priority' => 0.1,
'changefreq' => 'never',
])
->setEntityInstanceSettings('node', $this->node2
->id(), [
'index' => FALSE,
])
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseContains('node/' . $this->node
->id());
$this
->assertSession()
->responseContains('0.1');
$this
->assertSession()
->responseContains('never');
$this
->assertSession()
->responseNotContains('node/' . $this->node2
->id());
$this
->assertSession()
->responseNotContains('0.5');
$this
->drupalLogin($this->privilegedUser);
$this
->drupalGet('node/' . $this->node
->id() . '/edit');
$this
->assertSession()
->responseContains('<option value="0.1" selected="selected">0.1</option>');
$this
->assertSession()
->responseContains('<option value="never" selected="selected">never</option>');
$result = $this->database
->select('simple_sitemap_entity_overrides', 'o')
->fields('o', [
'inclusion_settings',
])
->condition('o.entity_type', 'node')
->condition('o.entity_id', $this->node
->id())
->execute()
->fetchField();
$this
->assertFalse(empty($result));
$this->generator
->setBundleSettings('node', 'page', [
'priority' => 0.1,
'changefreq' => 'never',
])
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseContains('node/' . $this->node
->id());
$this
->assertSession()
->responseContains('0.1');
$this
->assertSession()
->responseContains('never');
$this
->assertSession()
->responseNotContains('node/' . $this->node2
->id());
$this
->assertSession()
->responseNotContains('0.5');
$this
->drupalGet('node/' . $this->node
->id() . '/edit');
$this
->assertSession()
->responseContains('<option value="0.1" selected="selected">0.1 (default)</option>');
$this
->assertSession()
->responseContains('<option value="never" selected="selected">never (default)</option>');
$result = $this->database
->select('simple_sitemap_entity_overrides', 'o')
->fields('o', [
'inclusion_settings',
])
->condition('o.entity_type', 'node')
->condition('o.entity_id', $this->node
->id())
->execute()
->fetchField();
$this
->assertTrue(empty($result));
}
public function testDisableEntityType() {
$this->generator
->setBundleSettings('node', 'page')
->disableEntityType('node');
$this
->drupalLogin($this->privilegedUser);
$this
->drupalGet('admin/structure/types/manage/page');
$this
->assertSession()
->pageTextNotContains('Simple XML sitemap');
$this->generator
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseNotContains('node/' . $this->node
->id());
$this
->assertFalse($this->generator
->entityTypeIsEnabled('node'));
}
public function testEnableEntityType() {
$this->generator
->disableEntityType('node')
->enableEntityType('node')
->setBundleSettings('node', 'page');
$this
->drupalLogin($this->privilegedUser);
$this
->drupalGet('admin/structure/types/manage/page');
$this
->assertSession()
->pageTextContains('Simple XML sitemap');
$this->generator
->generateSitemap('nobatch');
$this
->drupalGet('sitemap.xml');
$this
->assertSession()
->responseContains('node/' . $this->node
->id());
$this
->assertTrue($this->generator
->entityTypeIsEnabled('node'));
}
}