You are here

public function PathautoKernelTest::testUpdateActions in Pathauto 8

Test the different update actions in \Drupal::service('pathauto.generator')->createEntityAlias().

File

tests/src/Kernel/PathautoKernelTest.php, line 278

Class

PathautoKernelTest
Unit tests for Pathauto functions.

Namespace

Drupal\Tests\pathauto\Kernel

Code

public function testUpdateActions() {
  $config = $this
    ->config('pathauto.settings');

  // Test PATHAUTO_UPDATE_ACTION_NO_NEW with unaliased node and 'insert'.
  $config
    ->set('update_action', PathautoGeneratorInterface::UPDATE_ACTION_NO_NEW);
  $config
    ->save();
  $node = $this
    ->drupalCreateNode([
    'title' => 'First title',
  ]);
  $this
    ->assertEntityAlias($node, '/content/first-title');
  $node->path->pathauto = PathautoState::CREATE;

  // Default action is PATHAUTO_UPDATE_ACTION_DELETE.
  $config
    ->set('update_action', PathautoGeneratorInterface::UPDATE_ACTION_DELETE);
  $config
    ->save();
  $node
    ->setTitle('Second title');
  $node
    ->save();
  $this
    ->assertEntityAlias($node, '/content/second-title');
  $this
    ->assertNoAliasExists([
    'alias' => '/content/first-title',
  ]);

  // Test PATHAUTO_UPDATE_ACTION_LEAVE.
  $config
    ->set('update_action', PathautoGeneratorInterface::UPDATE_ACTION_LEAVE);
  $config
    ->save();
  $node
    ->setTitle('Third title');
  $node
    ->save();
  $this
    ->assertEntityAlias($node, '/content/third-title');
  $this
    ->assertAliasExists([
    'path' => '/' . $node
      ->toUrl()
      ->getInternalPath(),
    'alias' => '/content/second-title',
  ]);
  $config
    ->set('update_action', PathautoGeneratorInterface::UPDATE_ACTION_DELETE);
  $config
    ->save();
  $node
    ->setTitle('Fourth title');
  $node
    ->save();
  $this
    ->assertEntityAlias($node, '/content/fourth-title');
  $this
    ->assertNoAliasExists([
    'alias' => '/content/third-title',
  ]);

  // The older second alias is not deleted yet.
  $older_path = $this
    ->assertAliasExists([
    'path' => '/' . $node
      ->toUrl()
      ->getInternalPath(),
    'alias' => '/content/second-title',
  ]);
  \Drupal::service('entity_type.manager')
    ->getStorage('path_alias')
    ->delete([
    $older_path,
  ]);
  $config
    ->set('update_action', PathautoGeneratorInterface::UPDATE_ACTION_NO_NEW);
  $config
    ->save();
  $node
    ->setTitle('Fifth title');
  $node
    ->save();
  $this
    ->assertEntityAlias($node, '/content/fourth-title');
  $this
    ->assertNoAliasExists([
    'alias' => '/content/fifth-title',
  ]);

  // Test PATHAUTO_UPDATE_ACTION_NO_NEW with unaliased node and 'update'.
  $this
    ->deleteAllAliases();
  $node
    ->save();
  $this
    ->assertEntityAlias($node, '/content/fifth-title');

  // Test PATHAUTO_UPDATE_ACTION_NO_NEW with unaliased node and 'bulkupdate'.
  $this
    ->deleteAllAliases();
  $node
    ->setTitle('Sixth title');
  \Drupal::service('pathauto.generator')
    ->updateEntityAlias($node, 'bulkupdate');
  $this
    ->assertEntityAlias($node, '/content/sixth-title');
}