3 namespace Drupal\pathauto\Tests;
5 use Drupal\pathauto\PathautoState;
6 use Drupal\simpletest\WebTestBase;
9 * Mass delete functionality tests.
13 class PathautoMassDeleteTest extends WebTestBase {
15 use PathautoTestHelperTrait;
22 public static $modules = array('node', 'taxonomy', 'pathauto');
27 * @var \Drupal\user\UserInterface
34 * @var \Drupal\node\NodeInterface
41 * @var \Drupal\user\UserInterface
48 * @var \Drupal\taxonomy\TermInterface
59 'administer pathauto',
60 'administer url aliases',
63 $this->adminUser = $this->drupalCreateUser($permissions);
64 $this->drupalLogin($this->adminUser);
66 $this->createPattern('node', '/content/[node:title]');
67 $this->createPattern('user', '/users/[user:name]');
68 $this->createPattern('taxonomy_term', '/[term:vocabulary]/[term:name]');
72 * Tests the deletion of all the aliases.
74 function testDeleteAll() {
75 // 1. Test that deleting all the aliases, of any type, works.
76 $this->generateAliases();
78 'delete[all_aliases]' => TRUE,
79 'options[keep_custom_aliases]' => FALSE,
81 $this->drupalPostForm('admin/config/search/path/delete_bulk', $edit, t('Delete aliases now!'));
82 $this->assertText(t('All of your path aliases have been deleted.'));
83 $this->assertUrl('admin/config/search/path/delete_bulk');
85 // Make sure that all of them are actually deleted.
86 $aliases = \Drupal::database()->select('url_alias', 'ua')->fields('ua', array())->execute()->fetchAll();
87 $this->assertEqual($aliases, array(), "All the aliases have been deleted.");
89 // 2. Test deleting only specific (entity type) aliases.
90 $manager = $this->container->get('plugin.manager.alias_type');
91 $pathauto_plugins = array('canonical_entities:node' => 'nodes', 'canonical_entities:taxonomy_term' => 'terms', 'canonical_entities:user' => 'accounts');
92 foreach ($pathauto_plugins as $pathauto_plugin => $attribute) {
93 $this->generateAliases();
95 'delete[plugins][' . $pathauto_plugin . ']' => TRUE,
96 'options[keep_custom_aliases]' => FALSE,
98 $this->drupalPostForm('admin/config/search/path/delete_bulk', $edit, t('Delete aliases now!'));
99 $alias_type = $manager->createInstance($pathauto_plugin);
100 $this->assertRaw(t('All of your %label path aliases have been deleted.', array('%label' => $alias_type->getLabel())));
101 // Check that the aliases were actually deleted.
102 foreach ($this->{$attribute} as $entity) {
103 $this->assertNoEntityAlias($entity);
106 // Check that the other aliases are not deleted.
107 foreach ($pathauto_plugins as $_pathauto_plugin => $_attribute) {
108 // Skip the aliases that should be deleted.
109 if ($_pathauto_plugin == $pathauto_plugin) {
112 foreach ($this->{$_attribute} as $entity) {
113 $this->assertEntityAliasExists($entity);
118 // 3. Test deleting automatically generated aliases only.
119 $this->generateAliases();
121 'delete[all_aliases]' => TRUE,
122 'options[keep_custom_aliases]' => TRUE,
124 $this->drupalPostForm('admin/config/search/path/delete_bulk', $edit, t('Delete aliases now!'));
125 $this->assertText(t('All of your automatically generated path aliases have been deleted.'));
126 $this->assertUrl('admin/config/search/path/delete_bulk');
128 // Make sure that only custom aliases and aliases with no information about
129 // their state still exist.
130 $aliases = \Drupal::database()->select('url_alias', 'ua')->fields('ua', ['source'])->execute()->fetchCol();
131 $this->assertEqual($aliases, ['/node/101', '/node/104', '/node/105'], 'Custom aliases still exist.');
135 * Helper function to generate aliases.
137 function generateAliases() {
138 // Delete all aliases to avoid duplicated aliases. They will be recreated
140 $this->deleteAllAliases();
142 // We generate a bunch of aliases for nodes, users and taxonomy terms. If
143 // the entities are already created we just update them, otherwise we create
145 if (empty($this->nodes)) {
146 // Create a large number of nodes (100+) to make sure that the batch code
148 for ($i = 1; $i <= 105; $i++) {
149 // Set the alias of two nodes manually.
150 $settings = ($i > 103) ? ['path' => ['alias' => "/custom_alias_$i", 'pathauto' => PathautoState::SKIP]] : [];
151 $node = $this->drupalCreateNode($settings);
152 $this->nodes[$node->id()] = $node;
156 foreach ($this->nodes as $node) {
157 if ($node->id() > 103) {
158 // The alias is set manually.
159 $node->set('path', ['alias' => '/custom_alias_' . $node->id()]);
164 // Delete information about the state of an alias to make sure that aliases
165 // with no such data are left alone by default.
166 \Drupal::keyValue('pathauto_state.node')->delete(101);
168 if (empty($this->accounts)) {
169 for ($i = 1; $i <= 5; $i++) {
170 $account = $this->drupalCreateUser();
171 $this->accounts[$account->id()] = $account;
175 foreach ($this->accounts as $id => $account) {
180 if (empty($this->terms)) {
181 $vocabulary = $this->addVocabulary(array('name' => 'test vocabulary', 'vid' => 'test_vocabulary'));
182 for ($i = 1; $i <= 5; $i++) {
183 $term = $this->addTerm($vocabulary);
184 $this->terms[$term->id()] = $term;
188 foreach ($this->terms as $term) {
193 // Check that we have aliases for the entities.
194 foreach (array('nodes', 'accounts', 'terms') as $attribute) {
195 foreach ($this->{$attribute} as $entity) {
196 $this->assertEntityAliasExists($entity);