You are here

public function PathAliasTest::testAliases in Tome 8

Tests that exporting/importing path alias entities works.

File

modules/tome_sync/tests/src/Kernel/PathAliasTest.php, line 38

Class

PathAliasTest
Tests that the path alias normalization works.

Namespace

Drupal\Tests\tome_sync\Kernel

Code

public function testAliases() {

  /** @var \Symfony\Component\Serializer\Serializer $serializer */
  $serializer = \Drupal::service('serializer');

  /** @var \Drupal\Core\Config\StorageInterface $storage */
  $storage = \Drupal::service('tome_sync.storage.content');
  $article = Node::create([
    'type' => 'article',
    'title' => 'My article',
  ]);
  $article
    ->save();
  $article2 = Node::create([
    'type' => 'article',
    'title' => 'My article',
  ]);
  $article2
    ->save();
  $alias = PathAlias::create([
    'path' => '/node/' . $article
      ->id(),
    'alias' => '/node/' . $article2
      ->id(),
  ]);
  $alias
    ->save();
  $data = $storage
    ->read(TomeSyncHelper::getContentName($alias));
  $this
    ->assertEquals($data['path'][0]['value'], '/node/' . $article
    ->uuid());
  $this
    ->assertEquals($data['alias'][0]['value'], '/node/' . $article2
    ->uuid());

  /** @var \Drupal\path_alias\Entity\PathAlias $exported_alias */
  $exported_alias = $serializer
    ->denormalize($data, PathAlias::class, 'json');
  $this
    ->assertEquals($exported_alias
    ->getPath(), '/node/' . $article
    ->id());
  $this
    ->assertEquals($exported_alias
    ->getAlias(), '/node/' . $article2
    ->id());
  $index_file = Settings::get('tome_content_directory', '../content') . '/meta/index.json';
  $this
    ->assertFileExists($index_file);
  $index = json_decode(file_get_contents($index_file), TRUE);
  $alias_name = TomeSyncHelper::getContentName($alias);
  $article_name = TomeSyncHelper::getContentName($article);
  $articl2_name = TomeSyncHelper::getContentName($article2);
  $this
    ->assertArrayHasKey($alias_name, $index);
  $this
    ->assertContains($article_name, $index[$alias_name]);
  $this
    ->assertContains($articl2_name, $index[$alias_name]);
}