You are here

public function PathautoTest::testPathauto in Tome 8

Tests that exporting/importing nodes with patterns works.

File

modules/tome_sync/tests/src/Kernel/PathautoTest.php, line 54

Class

PathautoTest
Tests that the pathuto integration works.

Namespace

Drupal\Tests\tome_sync\Kernel

Code

public function testPathauto() {

  /** @var \Drupal\Core\Entity\EntityRepository $repository */
  $repository = \Drupal::service('entity.repository');

  /** @var \Drupal\tome_sync\Importer $importer */
  $importer = \Drupal::service('tome_sync.importer');

  /** @var \Drupal\tome_sync\Exporter $exporter */
  $exporter = \Drupal::service('tome_sync.exporter');

  // Make sure normal entities work with our pattern.
  $article = Node::create([
    'type' => 'article',
    'title' => 'My article',
  ]);
  $article
    ->save();
  $source = '/' . $article
    ->toUrl()
    ->getInternalPath();
  \Drupal::service('path_alias.manager')
    ->cacheClear($source);
  $this
    ->assertNotEquals($source, \Drupal::service('path_alias.manager')
    ->getAliasByPath($source, 'en'));

  // Test that imported entities that use pathauto do not have aliases
  // automatically created, since path_alias entities should be imported
  // anyway.
  // @see tome_sync_pathauto_alias_alter()
  $uuid = \Drupal::service('uuid')
    ->generate();
  $article = Node::create([
    'type' => 'article',
    'title' => 'Another article',
    'uuid' => $uuid,
    'path' => [
      'path' => '',
      'pathauto' => PathautoState::CREATE,
    ],
  ]);
  $exporter
    ->exportContent($article);
  $importer
    ->importContent('node', $uuid);
  $article = $repository
    ->loadEntityByUuid('node', $uuid);
  $source = '/' . $article
    ->toUrl()
    ->getInternalPath();
  \Drupal::service('path_alias.manager')
    ->cacheClear($source);
  $this
    ->assertEquals($source, \Drupal::service('path_alias.manager')
    ->getAliasByPath($source, 'en'));
}