View source
<?php
namespace Drupal\Tests\simple_sitemap_views\Functional;
use Drupal\simple_sitemap\Entity\SimpleSitemapType;
class SimpleSitemapViewsTest extends SimpleSitemapViewsTestBase {
public function testSitemapSupportForViews() {
$this
->assertTrue($this->sitemapViews
->isEnabled());
$this->sitemapViews
->disable();
$this
->assertFalse($this->sitemapViews
->isEnabled());
$this->sitemapViews
->enable();
$this
->assertTrue($this->sitemapViews
->isEnabled());
}
public function testIndexableViews() {
$indexable_views = $this->sitemapViews
->getIndexableViews();
$this
->assertNotEmpty($indexable_views);
$test_view_exists = FALSE;
foreach ($indexable_views as &$view) {
if ($view
->id() == $this->testView
->id() && $view->current_display == $this->testView->current_display) {
$test_view_exists = TRUE;
break;
}
}
$this
->assertTrue($test_view_exists);
$indexable_arguments = $this->sitemapViews
->getIndexableArguments($this->testView, $this->sitemapVariant);
$this
->assertContains('type', $indexable_arguments);
$this
->assertContains('title', $indexable_arguments);
$this
->assertNotContains('nid', $indexable_arguments);
$indexable_arguments = $this->sitemapViews
->getIndexableArguments($this->testView2, $this->sitemapVariant);
$this
->assertContains('type', $indexable_arguments);
$this
->assertContains('%', $indexable_arguments);
}
public function testAddArgumentsToIndex() {
$this->sitemapViews
->addArgumentsToIndex($this->testView, [
'page2',
]);
$this
->assertIndexSize(0);
$args = [
'page',
$this->node
->getTitle(),
$this->node
->id(),
];
$this->sitemapViews
->addArgumentsToIndex($this->testView, $args);
$this
->assertIndexSize(0);
for ($i = 0; $i < 2; $i++) {
$this->sitemapViews
->addArgumentsToIndex($this->testView, [
'page',
]);
$this
->assertIndexSize(1);
}
$args = [
'page',
$this->node
->getTitle(),
];
$this->sitemapViews
->addArgumentsToIndex($this->testView, $args);
$this
->assertIndexSize(2);
$args = [
'page',
$this->node2
->getTitle(),
];
$this->sitemapViews
->addArgumentsToIndex($this->testView, $args);
$this
->assertIndexSize(2);
$this->sitemapViews
->addArgumentsToIndex($this->testView2, [
'page',
1,
]);
$this
->assertIndexSize(3);
}
public function testViewsUrlGenerator() {
$this
->assertArrayHasKey('views', SimpleSitemapType::load('default_hreflang')
->getUrlGenerators());
$title = $this->node
->getTitle();
$this->sitemapViews
->addArgumentsToIndex($this->testView, [
'page',
]);
$this->sitemapViews
->addArgumentsToIndex($this->testView, [
'page',
$title,
]);
$this->sitemapViews
->addArgumentsToIndex($this->testView2, [
'page',
1,
]);
$this->generator
->generateSitemap('backend');
$url1 = $this->testView
->getUrl()
->toString();
$url2 = $this->testView
->getUrl([
'page',
NULL,
NULL,
])
->toString();
$url3 = $this->testView
->getUrl([
'page',
$title,
NULL,
])
->toString();
$url4 = $this->testView2
->getUrl()
->toString();
$url5 = $this->testView2
->getUrl([
'page',
1,
])
->toString();
$this
->drupalGet($this->defaultSitemapUrl);
$this
->assertSession()
->responseContains($url1);
$this
->assertSession()
->responseContains($url2);
$this
->assertSession()
->responseContains($url3);
$this
->assertSession()
->responseNotContains($url4);
$this
->assertSession()
->responseContains($url5);
}
public function testGarbageCollector() {
$this->generator
->saveSetting('cron_generate', FALSE);
$this
->addRecordToIndex($this->testView
->id(), $this->testView->current_display, [
'type',
'title',
'nid',
], [
'page',
$this->node
->getTitle(),
$this->node
->id(),
]);
$this->cron
->run();
$this
->assertIndexSize(0);
$this
->addRecordToIndex('simple_sitemap_fake_view', $this->testView->current_display, [
'type',
'title',
], [
'page',
$this->node
->getTitle(),
]);
$this->cron
->run();
$this
->assertIndexSize(0);
$this
->addRecordToIndex($this->testView
->id(), 'simple_sitemap_fake_display', [
'type',
'title',
], [
'page',
$this->node
->getTitle(),
]);
$this->cron
->run();
$this
->assertIndexSize(0);
for ($i = 0; $i < 3; $i++) {
$this
->addRecordToIndex($this->testView
->id(), $this->testView->current_display, [
'type',
'title',
], [
'page2',
"Node{$i}",
]);
}
$this->cron
->run();
$this
->assertIndexSize(2);
$this->generator
->generateSitemap('backend');
$this
->assertIndexSize(0);
}
}