View source
<?php
namespace Drupal\Tests\pathauto\Functional;
use Drupal\pathauto\PathautoGeneratorInterface;
use Drupal\pathauto\PathautoState;
use Drupal\Tests\BrowserTestBase;
class PathautoBulkUpdateTest extends BrowserTestBase {
use PathautoTestHelperTrait;
protected $defaultTheme = 'stable';
public static $modules = [
'node',
'pathauto',
'forum',
];
protected $adminUser;
protected $nodes;
protected $patterns;
protected function setUp() {
parent::setUp();
$permissions = [
'administer pathauto',
'administer url aliases',
'create url aliases',
'administer forums',
];
$this->adminUser = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->adminUser);
$this->patterns = [];
$this->patterns['node'] = $this
->createPattern('node', '/content/[node:title]');
$this->patterns['user'] = $this
->createPattern('user', '/users/[user:name]');
$this->patterns['forum'] = $this
->createPattern('forum', '/forums/[term:name]');
}
public function testBulkUpdate() {
$this->nodes = [];
for ($i = 1; $i <= 5; $i++) {
$node = $this
->drupalCreateNode();
$this->nodes[$node
->id()] = $node;
}
$this
->deleteAllAliases();
$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
->assertText('Generated 8 URL aliases.');
foreach ($this->nodes as $node) {
$this
->assertEntityAliasExists($node);
}
$this
->assertEntityAliasExists($this->adminUser);
$this
->assertAliasExists([
'path' => '/taxonomy/term/1',
]);
$new_node = $this
->drupalCreateNode([
'path' => [
'alias' => '',
'pathauto' => PathautoState::SKIP,
],
]);
$this
->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
$this
->assertText('No new URL aliases to generate.');
$this
->assertNoEntityAliasExists($new_node);
$this
->drupalPostForm('admin/config/search/path/settings', [
'update_action' => PathautoGeneratorInterface::UPDATE_ACTION_DELETE,
], t('Save configuration'));
$edit['action'] = 'all';
$this
->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
$this
->assertText('No new URL aliases to generate.');
$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.');
$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');
}
public function testBulkUpdateExistingContent() {
$node = $this
->drupalCreateNode();
\Drupal::service('pathauto.alias_storage_helper')
->deleteEntityPathAll($node);
$node->path
->first()
->get('pathauto')
->purge();
\Drupal::entityTypeManager()
->getStorage('node')
->resetCache([
$node
->id(),
]);
$edit = [
'update[canonical_entities:node]' => TRUE,
];
$this
->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
$this
->assertText('Generated 1 URL alias.');
}
}