You are here

public function PathautoMassDeleteTest::testDeleteAll in Pathauto 8

Tests the deletion of all the aliases.

File

tests/src/Functional/PathautoMassDeleteTest.php, line 79

Class

PathautoMassDeleteTest
Mass delete functionality tests.

Namespace

Drupal\Tests\pathauto\Functional

Code

public function testDeleteAll() {

  /** @var \Drupal\pathauto\AliasStorageHelperInterface $alias_storage_helper */
  $alias_storage_helper = \Drupal::service('pathauto.alias_storage_helper');

  // 1. Test that deleting all the aliases, of any type, works.
  $this
    ->generateAliases();
  $edit = [
    'delete[all_aliases]' => TRUE,
    'options[keep_custom_aliases]' => FALSE,
  ];
  $this
    ->drupalPostForm('admin/config/search/path/delete_bulk', $edit, t('Delete aliases now!'));
  $this
    ->assertText(t('All of your path aliases have been deleted.'));
  $this
    ->assertUrl('admin/config/search/path/delete_bulk');

  // Make sure that all of them are actually deleted.
  $this
    ->assertEquals(0, $alias_storage_helper
    ->countAll(), 'All the aliases have been deleted.');

  // 2. Test deleting only specific (entity type) aliases.
  $manager = $this->container
    ->get('plugin.manager.alias_type');
  $pathauto_plugins = [
    'canonical_entities:node' => 'nodes',
    'canonical_entities:taxonomy_term' => 'terms',
    'canonical_entities:user' => 'accounts',
  ];
  foreach ($pathauto_plugins as $pathauto_plugin => $attribute) {
    $this
      ->generateAliases();
    $edit = [
      'delete[plugins][' . $pathauto_plugin . ']' => TRUE,
      'options[keep_custom_aliases]' => FALSE,
    ];
    $this
      ->drupalPostForm('admin/config/search/path/delete_bulk', $edit, t('Delete aliases now!'));
    $alias_type = $manager
      ->createInstance($pathauto_plugin);
    $this
      ->assertRaw(t('All of your %label path aliases have been deleted.', [
      '%label' => $alias_type
        ->getLabel(),
    ]));

    // Check that the aliases were actually deleted.
    foreach ($this->{$attribute} as $entity) {
      $this
        ->assertNoEntityAlias($entity);
    }

    // Check that the other aliases are not deleted.
    foreach ($pathauto_plugins as $_pathauto_plugin => $_attribute) {

      // Skip the aliases that should be deleted.
      if ($_pathauto_plugin == $pathauto_plugin) {
        continue;
      }
      foreach ($this->{$_attribute} as $entity) {
        $this
          ->assertEntityAliasExists($entity);
      }
    }
  }

  // 3. Test deleting automatically generated aliases only.
  $this
    ->generateAliases();
  $edit = [
    'delete[all_aliases]' => TRUE,
    'options[keep_custom_aliases]' => TRUE,
  ];
  $this
    ->drupalPostForm('admin/config/search/path/delete_bulk', $edit, t('Delete aliases now!'));
  $this
    ->assertText(t('All of your automatically generated path aliases have been deleted.'));
  $this
    ->assertUrl('admin/config/search/path/delete_bulk');

  // Make sure that only custom aliases and aliases with no information about
  // their state still exist.
  $this
    ->assertEquals(3, $alias_storage_helper
    ->countAll(), 'Custom aliases still exist.');
  $this
    ->assertEquals('/node/101', $alias_storage_helper
    ->loadBySource('/node/101', 'en')['source']);
  $this
    ->assertEquals('/node/104', $alias_storage_helper
    ->loadBySource('/node/104', 'en')['source']);
  $this
    ->assertEquals('/node/105', $alias_storage_helper
    ->loadBySource('/node/105', 'en')['source']);
}