93f4ca32d44f73f9253b128c0af62acc7b73b9d4
[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   /**
54    * {inheritdoc}
55    */
56   function setUp() {
57     parent::setUp();
58
59     $permissions = array(
60       'administer pathauto',
61       'administer url aliases',
62       'create url aliases',
63     );
64     $this->adminUser = $this->drupalCreateUser($permissions);
65     $this->drupalLogin($this->adminUser);
66
67     $this->createPattern('node', '/content/[node:title]');
68     $this->createPattern('user', '/users/[user:name]');
69     $this->createPattern('taxonomy_term', '/[term:vocabulary]/[term:name]');
70   }
71
72   /**
73    * Tests the deletion of all the aliases.
74    */
75   function testDeleteAll() {
76     // 1. Test that deleting all the aliases, of any type, works.
77     $this->generateAliases();
78     $edit = array(
79       'delete[all_aliases]' => TRUE,
80       'options[keep_custom_aliases]' => FALSE,
81     );
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');
85
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.");
89
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();
95       $edit = array(
96         'delete[plugins][' . $pathauto_plugin . ']' => TRUE,
97         'options[keep_custom_aliases]' => FALSE,
98       );
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);
105       }
106
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) {
111           continue;
112         }
113         foreach ($this->{$_attribute} as $entity) {
114           $this->assertEntityAliasExists($entity);
115         }
116       }
117     }
118
119     // 3. Test deleting automatically generated aliases only.
120     $this->generateAliases();
121     $edit = array(
122       'delete[all_aliases]' => TRUE,
123       'options[keep_custom_aliases]' => TRUE,
124     );
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');
128
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.');
133   }
134
135   /**
136    * Helper function to generate aliases.
137    */
138   function generateAliases() {
139     // Delete all aliases to avoid duplicated aliases. They will be recreated 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 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;
152       }
153     }
154     else {
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()]);
159         }
160         $node->save();
161       }
162     }
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);
166
167     if (empty($this->accounts)) {
168       for ($i = 1; $i <= 5; $i++) {
169         $account = $this->drupalCreateUser();
170         $this->accounts[$account->id()] = $account;
171       }
172     }
173     else {
174       foreach ($this->accounts as $id => $account) {
175         $account->save();
176       }
177     }
178
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;
184       }
185     }
186     else {
187       foreach ($this->terms as $term) {
188         $term->save();
189       }
190     }
191
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);
196       }
197     }
198   }
199
200 }