You are here

public function XmlSitemapCustomFunctionalTest::testCustomFileLinks in XML sitemap 2.x

Same name and namespace in other branches
  1. 8 xmlsitemap_custom/tests/src/Functional/XmlSitemapCustomFunctionalTest.php \Drupal\Tests\xmlsitemap_custom\Functional\XmlSitemapCustomFunctionalTest::testCustomFileLinks()

Test adding files as custom links.

File

xmlsitemap_custom/tests/src/Functional/XmlSitemapCustomFunctionalTest.php, line 77

Class

XmlSitemapCustomFunctionalTest
Tests the functionality of xmlsitemap_custom module.

Namespace

Drupal\Tests\xmlsitemap_custom\Functional

Code

public function testCustomFileLinks() {

  // Test an invalid file.
  $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'],
  ]);

  // Test an inaccessible file.
  $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'],
  ]);

  // Test a valid file.
  $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.'));

  // Test a duplicate url.
  $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.'));
}