View source
<?php
namespace Drupal\Tests\pathauto\Functional;
use Drupal\pathauto\PathautoState;
use Drupal\Tests\BrowserTestBase;
class PathautoMassDeleteTest extends BrowserTestBase {
use PathautoTestHelperTrait;
protected $defaultTheme = 'stable';
public static $modules = [
'node',
'taxonomy',
'pathauto',
];
protected $adminUser;
protected $nodes;
protected $accounts;
protected $terms;
protected function setUp() {
parent::setUp();
$permissions = [
'administer pathauto',
'administer url aliases',
'create url aliases',
];
$this->adminUser = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->adminUser);
$this
->createPattern('node', '/content/[node:title]');
$this
->createPattern('user', '/users/[user:name]');
$this
->createPattern('taxonomy_term', '/[term:vocabulary]/[term:name]');
}
public function testDeleteAll() {
$alias_storage_helper = \Drupal::service('pathauto.alias_storage_helper');
$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');
$this
->assertEquals(0, $alias_storage_helper
->countAll(), 'All the aliases have been deleted.');
$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(),
]));
foreach ($this->{$attribute} as $entity) {
$this
->assertNoEntityAlias($entity);
}
foreach ($pathauto_plugins as $_pathauto_plugin => $_attribute) {
if ($_pathauto_plugin == $pathauto_plugin) {
continue;
}
foreach ($this->{$_attribute} as $entity) {
$this
->assertEntityAliasExists($entity);
}
}
}
$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');
$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']);
}
public function generateAliases() {
$this
->deleteAllAliases();
if (empty($this->nodes)) {
for ($i = 1; $i <= 105; $i++) {
$settings = $i > 103 ? [
'path' => [
'alias' => "/custom_alias_{$i}",
'pathauto' => PathautoState::SKIP,
],
] : [];
$node = $this
->drupalCreateNode($settings);
$this->nodes[$node
->id()] = $node;
}
}
else {
foreach ($this->nodes as $node) {
if ($node
->id() > 103) {
$node
->set('path', [
'alias' => '/custom_alias_' . $node
->id(),
]);
}
$node
->save();
}
}
\Drupal::keyValue('pathauto_state.node')
->delete(101);
if (empty($this->accounts)) {
for ($i = 1; $i <= 5; $i++) {
$account = $this
->drupalCreateUser();
$this->accounts[$account
->id()] = $account;
}
}
else {
foreach ($this->accounts as $account) {
$account
->save();
}
}
if (empty($this->terms)) {
$vocabulary = $this
->addVocabulary([
'name' => 'test vocabulary',
'vid' => 'test_vocabulary',
]);
for ($i = 1; $i <= 5; $i++) {
$term = $this
->addTerm($vocabulary);
$this->terms[$term
->id()] = $term;
}
}
else {
foreach ($this->terms as $term) {
$term
->save();
}
}
foreach ([
'nodes',
'accounts',
'terms',
] as $attribute) {
foreach ($this->{$attribute} as $entity) {
$this
->assertEntityAliasExists($entity);
}
}
}
}