691e5a89cb8d3d956faa1ae6fba99ef3cc58c5c9
[yaffs-website] / web / modules / contrib / pathauto / src / Tests / PathautoMassDeleteTest.php
1 <?php
2
3 namespace Drupal\pathauto\Tests;
4
5 use Drupal\pathauto\PathautoState;
6 use Drupal\simpletest\WebTestBase;
7
8 /**
9  * Mass delete functionality tests.
10  *
11  * @group pathauto
12  */
13 class PathautoMassDeleteTest extends WebTestBase {
14
15   use PathautoTestHelperTrait;
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = array('node', 'taxonomy', 'pathauto');
23
24   /**
25    * Admin user.
26    *
27    * @var \Drupal\user\UserInterface
28    */
29   protected $adminUser;
30
31   /**
32    * The test nodes.
33    *
34    * @var \Drupal\node\NodeInterface
35    */
36   protected $nodes;
37
38   /**
39    * The test accounts.
40    *
41    * @var \Drupal\user\UserInterface
42    */
43   protected $accounts;
44
45   /**
46    * The test terms.
47    *
48    * @var \Drupal\taxonomy\TermInterface
49    */
50   protected $terms;
51
52   /**
53    * {@inheritdoc}
54    */
55   function setUp() {
56     parent::setUp();
57
58     $permissions = array(
59       'administer pathauto',
60       'administer url aliases',
61       'create url aliases',
62     );
63     $this->adminUser = $this->drupalCreateUser($permissions);
64     $this->drupalLogin($this->adminUser);
65
66     $this->createPattern('node', '/content/[node:title]');
67     $this->createPattern('user', '/users/[user:name]');
68     $this->createPattern('taxonomy_term', '/[term:vocabulary]/[term:name]');
69   }
70
71   /**
72    * Tests the deletion of all the aliases.
73    */
74   function testDeleteAll() {
75     // 1. Test that deleting all the aliases, of any type, works.
76     $this->generateAliases();
77     $edit = array(
78       'delete[all_aliases]' => TRUE,
79       'options[keep_custom_aliases]' => FALSE,
80     );
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');
84
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.");
88
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();
94       $edit = array(
95         'delete[plugins][' . $pathauto_plugin . ']' => TRUE,
96         'options[keep_custom_aliases]' => FALSE,
97       );
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);
104       }
105
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) {
110           continue;
111         }
112         foreach ($this->{$_attribute} as $entity) {
113           $this->assertEntityAliasExists($entity);
114         }
115       }
116     }
117
118     // 3. Test deleting automatically generated aliases only.
119     $this->generateAliases();
120     $edit = array(
121       'delete[all_aliases]' => TRUE,
122       'options[keep_custom_aliases]' => TRUE,
123     );
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');
127
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.');
132   }
133
134   /**
135    * Helper function to generate aliases.
136    */
137   function generateAliases() {
138     // Delete all aliases to avoid duplicated aliases. They will be recreated
139     // below.
140     $this->deleteAllAliases();
141
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
144     // them.
145     if (empty($this->nodes)) {
146       // Create a large number of nodes (100+) to make sure that the batch code
147       // works.
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;
153       }
154     }
155     else {
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()]);
160         }
161         $node->save();
162       }
163     }
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);
167
168     if (empty($this->accounts)) {
169       for ($i = 1; $i <= 5; $i++) {
170         $account = $this->drupalCreateUser();
171         $this->accounts[$account->id()] = $account;
172       }
173     }
174     else {
175       foreach ($this->accounts as $id => $account) {
176         $account->save();
177       }
178     }
179
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;
185       }
186     }
187     else {
188       foreach ($this->terms as $term) {
189         $term->save();
190       }
191     }
192
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);
197       }
198     }
199   }
200
201 }