You are here

public function PathautoBulkUpdateTest::testBulkUpdate in Pathauto 8

File

tests/src/Functional/PathautoBulkUpdateTest.php, line 73

Class

PathautoBulkUpdateTest
Bulk update functionality tests.

Namespace

Drupal\Tests\pathauto\Functional

Code

public function testBulkUpdate() {

  // Create some nodes.
  $this->nodes = [];
  for ($i = 1; $i <= 5; $i++) {
    $node = $this
      ->drupalCreateNode();
    $this->nodes[$node
      ->id()] = $node;
  }

  // Clear out all aliases.
  $this
    ->deleteAllAliases();

  // Bulk create aliases.
  $edit = [
    'update[canonical_entities:node]' => TRUE,
    'update[canonical_entities:user]' => TRUE,
    'update[forum]' => TRUE,
  ];
  $this
    ->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));

  // This has generated 8 aliases: 5 nodes, 2 users and 1 forum.
  $this
    ->assertText('Generated 8 URL aliases.');

  // Check that aliases have actually been created.
  foreach ($this->nodes as $node) {
    $this
      ->assertEntityAliasExists($node);
  }
  $this
    ->assertEntityAliasExists($this->adminUser);

  // This is the default "General discussion" forum.
  $this
    ->assertAliasExists([
    'path' => '/taxonomy/term/1',
  ]);

  // Add a new node.
  $new_node = $this
    ->drupalCreateNode([
    'path' => [
      'alias' => '',
      'pathauto' => PathautoState::SKIP,
    ],
  ]);

  // Run the update again which should not run against any nodes.
  $this
    ->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
  $this
    ->assertText('No new URL aliases to generate.');
  $this
    ->assertNoEntityAliasExists($new_node);

  // Make sure existing aliases can be overridden.
  $this
    ->drupalPostForm('admin/config/search/path/settings', [
    'update_action' => PathautoGeneratorInterface::UPDATE_ACTION_DELETE,
  ], t('Save configuration'));

  // Patterns did not change, so no aliases should be regenerated.
  $edit['action'] = 'all';
  $this
    ->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
  $this
    ->assertText('No new URL aliases to generate.');

  // Update the node pattern, and leave other patterns alone. Existing nodes
  // should get a new alias, except the node above whose alias is manually
  // set. Other aliases must be left alone.
  $this->patterns['node']
    ->delete();
  $this->patterns['node'] = $this
    ->createPattern('node', '/archive/node-[node:nid]');
  $this
    ->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
  $this
    ->assertText('Generated 5 URL aliases.');

  // Prevent existing aliases to be overridden. The bulk generate page should
  // only offer to create an alias for paths which have none.
  $this
    ->drupalPostForm('admin/config/search/path/settings', [
    'update_action' => PathautoGeneratorInterface::UPDATE_ACTION_NO_NEW,
  ], t('Save configuration'));
  $this
    ->drupalGet('admin/config/search/path/update_bulk');
  $this
    ->assertFieldByName('action', 'create');
  $this
    ->assertText('Pathauto settings are set to ignore paths which already have a URL alias.');
  $this
    ->assertNoFieldByName('action', 'update');
  $this
    ->assertNoFieldByName('action', 'all');
}