You are here

public function XmlSitemapMultilingualTest::testLanguageSelection in XML sitemap 8

Same name and namespace in other branches
  1. 2.x tests/src/Functional/XmlSitemapMultilingualTest.php \Drupal\Tests\xmlsitemap\Functional\XmlSitemapMultilingualTest::testLanguageSelection()

Test Language Selection.

Test how links are included in a sitemap depending on the i18n_selection_mode config variable.

File

tests/src/Functional/XmlSitemapMultilingualTest.php, line 54

Class

XmlSitemapMultilingualTest
Tests the generation of multilingual sitemaps.

Namespace

Drupal\Tests\xmlsitemap\Functional

Code

public function testLanguageSelection() {
  $this
    ->drupalLogin($this->admin_user);

  // Create our three different language nodes.
  $node = $this
    ->addSitemapLink([
    'type' => 'node',
    'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $node_en = $this
    ->addSitemapLink([
    'type' => 'node',
    'language' => 'en',
  ]);
  $node_fr = $this
    ->addSitemapLink([
    'type' => 'node',
    'language' => 'fr',
  ]);

  // Create three non-node language nodes.
  $link = $this
    ->addSitemapLink([
    'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $link_en = $this
    ->addSitemapLink([
    'language' => 'en',
  ]);
  $link_fr = $this
    ->addSitemapLink([
    'language' => 'fr',
  ]);
  $this->config
    ->set('i18n_selection_mode', 'off')
    ->save();
  $this
    ->regenerateSitemap();
  $this
    ->drupalGetSitemap([
    'language' => 'en',
  ]);
  $this
    ->assertRawSitemapLinks($node, $node_en, $node_fr, $link, $link_en, $link_fr);
  $this
    ->drupalGet('fr/sitemap.xml');
  $this
    ->assertRawSitemapLinks($node, $node_en, $node_fr, $link, $link_en, $link_fr);
  $this->config
    ->set('i18n_selection_mode', 'simple')
    ->save();
  $this
    ->regenerateSitemap();
  $this
    ->drupalGetSitemap([
    'language' => 'en',
  ]);
  $this
    ->assertRawSitemapLinks($node, $node_en, $link, $link_en);
  $this
    ->assertNoRawSitemapLinks($node_fr, $link_fr);
  $this
    ->drupalGet('fr/sitemap.xml');
  $this
    ->assertRawSitemapLinks($node, $node_fr, $link, $link_fr);
  $this
    ->assertNoRawSitemapLinks($node_en, $link_en);
  $this->config
    ->set('i18n_selection_mode', 'mixed')
    ->save();
  $this
    ->regenerateSitemap();
  $this
    ->drupalGetSitemap([
    'language' => 'en',
  ]);
  $this
    ->assertRawSitemapLinks($node, $node_en, $link, $link_en);
  $this
    ->assertNoRawSitemapLinks($node_fr, $link_fr);
  $this
    ->drupalGet('fr/sitemap.xml');
  $this
    ->assertRawSitemapLinks($node, $node_en, $node_fr, $link, $link_en, $link_fr);
  $this->config
    ->set('i18n_selection_mode', 'default')
    ->save();
  $this
    ->regenerateSitemap();
  $this
    ->drupalGetSitemap([
    'language' => 'en',
  ]);
  $this
    ->assertRawSitemapLinks($node, $node_en, $link, $link_en);
  $this
    ->assertNoRawSitemapLinks($node_fr, $link_fr);
  $this
    ->drupalGet('fr/sitemap.xml');
  $this
    ->assertRawSitemapLinks($node, $node_en, $link, $link_en);
  $this
    ->assertNoRawSitemapLinks($node_fr, $link_fr);

  // With strict mode, the language neutral node should not be found, but the
  // language neutral non-node should be.
  $this->config
    ->set('i18n_selection_mode', 'strict')
    ->save();
  $this
    ->regenerateSitemap();
  $this
    ->drupalGetSitemap([
    'language' => 'en',
  ]);
  $this
    ->assertRawSitemapLinks($node_en, $link, $link_en);
  $this
    ->assertNoRawSitemapLinks($node, $node_fr, $link_fr);
  $this
    ->drupalGet('fr/sitemap.xml');
  $this
    ->assertRawSitemapLinks($node_fr, $link, $link_fr);
  $this
    ->assertNoRawSitemapLinks($node, $node_en, $link_en);
}