You are here

public function LegacyAliasStorageTest::testCRUD in Drupal 8

@covers ::load @covers ::save @covers ::delete @expectedDeprecation \Drupal\Core\Path\AliasStorage is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use the "path_alias.repository" service instead, or the entity storage handler for the "path_alias" entity type for CRUD methods. See https://www.drupal.org/node/3013865.

File

core/tests/Drupal/KernelTests/Core/Path/LegacyAliasStorageTest.php, line 61

Class

LegacyAliasStorageTest
@coversDefaultClass \Drupal\Core\Path\AliasStorage @group path @group legacy

Namespace

Drupal\KernelTests\Core\Path

Code

public function testCRUD() {
  $entity_storage = \Drupal::entityTypeManager()
    ->getStorage('path_alias');
  $aliases = $this
    ->sampleUrlAliases();

  // Create a few aliases
  foreach ($aliases as $idx => $alias) {
    $this->storage
      ->save($alias['source'], $alias['alias'], $alias['langcode']);
    $result = $entity_storage
      ->getQuery()
      ->condition('path', $alias['source'])
      ->condition('alias', $alias['alias'])
      ->condition('langcode', $alias['langcode'])
      ->execute();
    $this
      ->assertCount(1, $result, "Created an entry for {$alias['alias']}.");

    // Cache the pid for further tests.
    $aliases[$idx]['pid'] = reset($result);
  }

  // Load a few aliases
  foreach ($aliases as $alias) {
    $pid = $alias['pid'];
    $loadedAlias = $this->storage
      ->load([
      'pid' => $pid,
    ]);
    $this
      ->assertEquals($alias, $loadedAlias, "Loaded the expected path with pid {$pid}.");
  }

  // Load alias by source path.
  $loadedAlias = $this->storage
    ->load([
    'source' => '/node/1',
  ]);
  $this
    ->assertEquals('/alias_for_node_1_und', $loadedAlias['alias'], 'The last created alias loaded by default.');

  // Update a few aliases
  foreach ($aliases as $alias) {
    $fields = $this->storage
      ->save($alias['source'], $alias['alias'] . '_updated', $alias['langcode'], $alias['pid']);
    $this
      ->assertEquals($alias['alias'], $fields['original']['alias']);
    $result = $entity_storage
      ->getQuery()
      ->condition('path', $alias['source'])
      ->condition('alias', $alias['alias'] . '_updated')
      ->condition('langcode', $alias['langcode'])
      ->execute();
    $pid = reset($result);
    $this
      ->assertEquals($alias['pid'], $pid, "Updated entry for pid {$pid}.");
  }

  // Delete a few aliases
  foreach ($aliases as $alias) {
    $pid = $alias['pid'];
    $this->storage
      ->delete([
      'pid' => $pid,
    ]);
    $result = $entity_storage
      ->getQuery()
      ->condition('id', $pid)
      ->execute();
    $this
      ->assertCount(0, $result, "Deleted entry with pid {$pid}.");
  }
}