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
60 'administer pathauto',
61 'administer url aliases',
64 $this->adminUser = $this->drupalCreateUser($permissions);
65 $this->drupalLogin($this->adminUser);
67 $this->createPattern('node', '/content/[node:title]');
68 $this->createPattern('user', '/users/[user:name]');
69 $this->createPattern('taxonomy_term', '/[term:vocabulary]/[term:name]');
73 * Tests the deletion of all the aliases.
75 function testDeleteAll() {
76 // 1. Test that deleting all the aliases, of any type, works.
77 $this->generateAliases();
79 'delete[all_aliases]' => TRUE,
80 'options[keep_custom_aliases]' => FALSE,
82 $this->drupalPostForm('admin/config/search/path/delete_bulk', $edit, t('Delete aliases now!'));
83 $this->assertText(t('All of your path aliases have been deleted.'));
84 $this->assertUrl('admin/config/search/path/delete_bulk');
86 // Make sure that all of them are actually deleted.
87 $aliases = \Drupal::database()->select('url_alias', 'ua')->fields('ua', array())->execute()->fetchAll();
88 $this->assertEqual($aliases, array(), "All the aliases have been deleted.");
90 // 2. Test deleting only specific (entity type) aliases.
91 $manager = $this->container->get('plugin.manager.alias_type');
92 $pathauto_plugins = array('canonical_entities:node' => 'nodes', 'canonical_entities:taxonomy_term' => 'terms', 'canonical_entities:user' => 'accounts');
93 foreach ($pathauto_plugins as $pathauto_plugin => $attribute) {
94 $this->generateAliases();
96 'delete[plugins][' . $pathauto_plugin . ']' => TRUE,
97 'options[keep_custom_aliases]' => FALSE,
99 $this->drupalPostForm('admin/config/search/path/delete_bulk', $edit, t('Delete aliases now!'));
100 $alias_type = $manager->createInstance($pathauto_plugin);
101 $this->assertRaw(t('All of your %label path aliases have been deleted.', array('%label' => $alias_type->getLabel())));
102 // Check that the aliases were actually deleted.
103 foreach ($this->{$attribute} as $entity) {
104 $this->assertNoEntityAlias($entity);
107 // Check that the other aliases are not deleted.
108 foreach ($pathauto_plugins as $_pathauto_plugin => $_attribute) {
109 // Skip the aliases that should be deleted.
110 if ($_pathauto_plugin == $pathauto_plugin) {
113 foreach ($this->{$_attribute} as $entity) {
114 $this->assertEntityAliasExists($entity);
119 // 3. Test deleting automatically generated aliases only.
120 $this->generateAliases();
122 'delete[all_aliases]' => TRUE,
123 'options[keep_custom_aliases]' => TRUE,
125 $this->drupalPostForm('admin/config/search/path/delete_bulk', $edit, t('Delete aliases now!'));
126 $this->assertText(t('All of your automatically generated path aliases have been deleted.'));
127 $this->assertUrl('admin/config/search/path/delete_bulk');
129 // Make sure that only custom aliases and aliases with no information about
130 // their state still exist.
131 $aliases = \Drupal::database()->select('url_alias', 'ua')->fields('ua', ['source'])->execute()->fetchCol();
132 $this->assertEqual($aliases, ['/node/101', '/node/104', '/node/105'], 'Custom aliases still exist.');
136 * Helper function to generate aliases.
138 function generateAliases() {
139 // Delete all aliases to avoid duplicated aliases. They will be recreated below.
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 works.
147 for ($i = 1; $i <= 105; $i++) {
148 // Set the alias of two nodes manually.
149 $settings = ($i > 103) ? ['path' => ['alias' => "/custom_alias_$i", 'pathauto' => PathautoState::SKIP]] : [];
150 $node = $this->drupalCreateNode($settings);
151 $this->nodes[$node->id()] = $node;
155 foreach ($this->nodes as $node) {
156 if ($node->id() > 103) {
157 // The alias is set manually.
158 $node->set('path', ['alias' => '/custom_alias_' . $node->id()]);
163 // Delete information about the state of an alias to make sure that aliases
164 // with no such data are left alone by default.
165 \Drupal::keyValue('pathauto_state.node')->delete(101);
167 if (empty($this->accounts)) {
168 for ($i = 1; $i <= 5; $i++) {
169 $account = $this->drupalCreateUser();
170 $this->accounts[$account->id()] = $account;
174 foreach ($this->accounts as $id => $account) {
179 if (empty($this->terms)) {
180 $vocabulary = $this->addVocabulary(array('name' => 'test vocabulary', 'vid' => 'test_vocabulary'));
181 for ($i = 1; $i <= 5; $i++) {
182 $term = $this->addTerm($vocabulary);
183 $this->terms[$term->id()] = $term;
187 foreach ($this->terms as $term) {
192 // Check that we have aliases for the entities.
193 foreach (array('nodes', 'accounts', 'terms') as $attribute) {
194 foreach ($this->{$attribute} as $entity) {
195 $this->assertEntityAliasExists($entity);