You are here

public function PathautoLocaleTest::testLanguageAliases in Pathauto 8

Test that when an English node is updated, its old English alias is updated and its newer French alias is left intact.

File

tests/src/FunctionalJavascript/PathautoLocaleTest.php, line 47

Class

PathautoLocaleTest
Test pathauto functionality with localization and translation.

Namespace

Drupal\Tests\pathauto\FunctionalJavascript

Code

public function testLanguageAliases() {
  $this
    ->createPattern('node', '/content/[node:title]');

  // Add predefined French language.
  ConfigurableLanguage::createFromLangcode('fr')
    ->save();
  $node = [
    'title' => 'English node',
    'langcode' => 'en',
    'path' => [
      [
        'alias' => '/english-node',
        'pathauto' => FALSE,
      ],
    ],
  ];
  $node = $this
    ->drupalCreateNode($node);
  $english_alias = $this
    ->loadPathAliasByConditions([
    'alias' => '/english-node',
    'langcode' => 'en',
  ]);
  $this
    ->assertNotEmpty($english_alias, 'Alias created with proper language.');

  // Also save a French alias that should not be left alone, even though
  // it is the newer alias.
  $this
    ->saveEntityAlias($node, '/french-node', 'fr');

  // Add an alias with the soon-to-be generated alias, causing the upcoming
  // alias update to generate a unique alias with the '-0' suffix.
  $this
    ->createPathAlias('/node/invalid', '/content/english-node', Language::LANGCODE_NOT_SPECIFIED);

  // Update the node, triggering a change in the English alias.
  $node->path->pathauto = PathautoState::CREATE;
  $node
    ->save();

  // Check that the new English alias replaced the old one.
  $this
    ->assertEntityAlias($node, '/content/english-node-0', 'en');
  $this
    ->assertEntityAlias($node, '/french-node', 'fr');
  $this
    ->assertAliasExists([
    'id' => $english_alias
      ->id(),
    'alias' => '/content/english-node-0',
  ]);

  // Create a new node with the same title as before but without
  // specifying a language.
  $node = $this
    ->drupalCreateNode([
    'title' => 'English node',
    'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);

  // Check that the new node had a unique alias generated with the '-0'
  // suffix.
  $this
    ->assertEntityAlias($node, '/content/english-node-0', LanguageInterface::LANGCODE_NOT_SPECIFIED);
}