View source
<?php
namespace Drupal\Tests\xmlsitemap_custom\Functional;
use Drupal\Tests\xmlsitemap\Functional\XmlSitemapTestBase;
class XmlSitemapCustomFunctionalTest extends XmlSitemapTestBase {
public static $modules = [
'xmlsitemap_custom',
'path',
];
protected $aliasStorage;
protected function setUp() {
parent::setUp();
$this->aliasStorage = $this->entityTypeManager
->getStorage('path_alias');
$this->admin_user = $this
->drupalCreateUser([
'access content',
'administer xmlsitemap',
]);
$this
->drupalLogin($this->admin_user);
}
public function testCustomLinks() {
$language = $this->languageManager
->getCurrentLanguage();
$this->aliasStorage
->create([
'path' => '/system/files',
'alias' => '/public-files',
'langcode' => $language
->getId(),
])
->save();
$this
->drupalGet('admin/config/search/xmlsitemap/custom');
$this
->clickLink(t('Add custom link'));
$edit['loc'] = '/invalid-testing-path';
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertSession()
->pageTextContains(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', [
'@link' => $edit['loc'],
]));
$this
->assertNoSitemapLink([
'type' => 'custom',
'loc' => $edit['loc'],
]);
$edit['loc'] = '/admin/people';
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertSession()
->pageTextContains(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', [
'@link' => $edit['loc'],
]));
$this
->assertNoSitemapLink([
'type' => 'custom',
'loc' => $edit['loc'],
]);
$edit['loc'] = '/admin/config/search/xmlsitemap/custom/add';
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertSession()
->pageTextContains(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', [
'@link' => $edit['loc'],
]));
$this
->assertNoSitemapLink([
'type' => 'custom',
'loc' => $edit['loc'],
]);
}
public function testCustomFileLinks() {
$edit['loc'] = '/' . $this
->randomMachineName();
$this
->drupalPostForm('admin/config/search/xmlsitemap/custom/add', $edit, t('Save'));
$this
->assertSession()
->pageTextContains(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', [
'@link' => $edit['loc'],
]));
$this
->assertNoSitemapLink([
'type' => 'custom',
'loc' => $edit['loc'],
]);
$edit['loc'] = '/.htaccess';
$this
->drupalPostForm('admin/config/search/xmlsitemap/custom/add', $edit, t('Save'));
$this
->assertSession()
->pageTextContains(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', [
'@link' => $edit['loc'],
]));
$this
->assertNoSitemapLink([
'type' => 'custom',
'loc' => $edit['loc'],
]);
$edit['loc'] = '/core/misc/drupal.js';
$this
->drupalPostForm('admin/config/search/xmlsitemap/custom/add', $edit, t('Save'));
$this
->assertSession()
->pageTextContains(t('The custom link for @link was saved.', [
'@link' => $edit['loc'],
]));
$links = $this->linkStorage
->loadMultiple([
'type' => 'custom',
'loc' => $edit['loc'],
]);
$this
->assertEquals(1, count($links), t('Custom link saved in the database.'));
$edit['loc'] = '/core/misc/drupal.js';
$this
->drupalPostForm('admin/config/search/xmlsitemap/custom/add', $edit, t('Save'));
$this
->assertSession()
->pageTextContains(t('There is already an existing link in the sitemap with the path @link.', [
'@link' => $edit['loc'],
]));
$links = $this->linkStorage
->loadMultiple([
'type' => 'custom',
'loc' => $edit['loc'],
]);
$this
->assertEquals(1, count($links), t('Custom link saved in the database.'));
}
}