You are here

public function LanguageNeutralAliasesTest::testSave in Language neutral aliases 8.2

Same name and namespace in other branches
  1. 3.x tests/src/Kernel/LanguageNeutralAliasesTest.php \Drupal\Tests\language_neutral_aliases\Kernel\LanguageNeutralAliasesTest::testSave()

Test that new aliases gets saved with language neutral.

File

tests/src/Kernel/LanguageNeutralAliasesTest.php, line 88

Class

LanguageNeutralAliasesTest
Test language neutral aliases.

Namespace

Drupal\Tests\language_neutral_aliases\Kernel

Code

public function testSave() {
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('path_alias');

  // A new alias with a language code should be saved as neutral.
  $values = [
    'path' => '/node/4',
    'alias' => '/path/fourth',
    'langcode' => 'de',
  ];
  $alias = $storage
    ->create($values);
  $storage
    ->save($alias);
  $actual = $storage
    ->load($alias
    ->id());
  $this
    ->assertEqual(4, $actual
    ->id());
  $this
    ->assertEqual('/node/4', $actual
    ->getPath());
  $this
    ->assertEqual('/path/fourth', $actual
    ->getAlias());
  $this
    ->assertEqual(LanguageInterface::LANGCODE_NOT_SPECIFIED, $actual
    ->get('langcode')->value);

  // Non-neutral aliases should be updated. This is not by design, but a side
  // effect of the move to entities. We can't return a clone when saving. But
  // as PathFieldItemList loads the alias through the repository, existing
  // non-neutral aliases should be hidden, and thus never saved.
  $alias = $storage
    ->load(1);
  $alias
    ->setAlias('/path/fifth');
  $storage
    ->save($alias);
  $actual = $storage
    ->load($alias
    ->id());
  $this
    ->assertEqual(1, $actual
    ->id());
  $this
    ->assertEqual('/node/1', $actual
    ->getPath());
  $this
    ->assertEqual('/path/fifth', $actual
    ->getAlias());
  $this
    ->assertEqual(LanguageInterface::LANGCODE_NOT_SPECIFIED, $actual
    ->get('langcode')->value);

  // Ensure that language neutral aliases can be updated.
  $alias = $storage
    ->load(4);
  $alias
    ->setAlias('/path/sixth');
  $actual = $storage
    ->load(4);
  $this
    ->assertEqual(4, $actual
    ->id());
  $this
    ->assertEqual('/node/4', $actual
    ->getPath());
  $this
    ->assertEqual('/path/sixth', $actual
    ->getAlias());
  $this
    ->assertEqual(LanguageInterface::LANGCODE_NOT_SPECIFIED, $actual
    ->get('langcode')->value);
}