3 namespace Drupal\pathauto\Tests;
5 use Drupal\pathauto\PathautoGeneratorInterface;
6 use Drupal\pathauto\PathautoState;
7 use Drupal\simpletest\WebTestBase;
10 * Bulk update functionality tests.
14 class PathautoBulkUpdateTest extends WebTestBase {
16 use PathautoTestHelperTrait;
23 public static $modules = array('node', 'pathauto', 'forum');
28 * @var \Drupal\user\UserInterface
35 * @var \Drupal\node\NodeInterface
40 * The created patterns.
42 * @var \Drupal\pathauto\PathautoPatternInterface
52 // Allow other modules to add additional permissions for the admin user.
54 'administer pathauto',
55 'administer url aliases',
59 $this->adminUser = $this->drupalCreateUser($permissions);
60 $this->drupalLogin($this->adminUser);
62 $this->patterns = array();
63 $this->patterns['node'] = $this->createPattern('node', '/content/[node:title]');
64 $this->patterns['user'] = $this->createPattern('user', '/users/[user:name]');
65 $this->patterns['forum'] = $this->createPattern('forum', '/forums/[term:name]');
68 function testBulkUpdate() {
70 $this->nodes = array();
71 for ($i = 1; $i <= 5; $i++) {
72 $node = $this->drupalCreateNode();
73 $this->nodes[$node->id()] = $node;
76 // Clear out all aliases.
77 $this->deleteAllAliases();
79 // Bulk create aliases.
81 'update[canonical_entities:node]' => TRUE,
82 'update[canonical_entities:user]' => TRUE,
83 'update[forum]' => TRUE,
85 $this->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
87 // This has generated 8 aliases: 5 nodes, 2 users and 1 forum.
88 $this->assertText('Generated 8 URL aliases.');
90 // Check that aliases have actually been created.
91 foreach ($this->nodes as $node) {
92 $this->assertEntityAliasExists($node);
94 $this->assertEntityAliasExists($this->adminUser);
95 // This is the default "General discussion" forum.
96 $this->assertAliasExists(['source' => '/taxonomy/term/1']);
99 $new_node = $this->drupalCreateNode(array('path' => array('alias' => '', 'pathauto' => PathautoState::SKIP)));
101 // Run the update again which should not run against any nodes.
102 $this->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
103 $this->assertText('No new URL aliases to generate.');
104 $this->assertNoEntityAliasExists($new_node);
106 // Make sure existing aliases can be overridden.
107 $this->drupalPostForm('admin/config/search/path/settings', ['update_action' => PathautoGeneratorInterface::UPDATE_ACTION_DELETE], t('Save configuration'));
109 // Patterns did not change, so no aliases should be regenerated.
110 $edit['action'] = 'all';
111 $this->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
112 $this->assertText('No new URL aliases to generate.');
114 // Update the node pattern, and leave other patterns alone. Existing nodes
115 // should get a new alias, except the node above whose alias is manually
116 // set. Other aliases must be left alone.
117 $this->patterns['node']->delete();
118 $this->patterns['node'] = $this->createPattern('node', '/archive/node-[node:nid]');
120 $this->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
121 $this->assertText('Generated 5 URL aliases.');
123 // Prevent existing aliases to be overridden. The bulk generate page should
124 // only offer to create an alias for paths which have none.
125 $this->drupalPostForm('admin/config/search/path/settings', ['update_action' => PathautoGeneratorInterface::UPDATE_ACTION_NO_NEW], t('Save configuration'));
127 $this->drupalGet('admin/config/search/path/update_bulk');
128 $this->assertFieldByName('action', 'create');
129 $this->assertText('Pathauto settings are set to ignore paths which already have a URL alias.');
130 $this->assertNoFieldByName('action', 'update');
131 $this->assertNoFieldByName('action', 'all');
135 * Tests alias generation for nodes that existed before installing Pathauto.
137 function testBulkUpdateExistingContent() {
139 $node = $this->drupalCreateNode();
141 // Delete its alias and Pathauto metadata.
142 \Drupal::service('pathauto.alias_storage_helper')->deleteEntityPathAll($node);
143 $node->path->first()->get('pathauto')->purge();
144 \Drupal::entityTypeManager()->getStorage('node')->resetCache(array($node->id()));
146 // Execute bulk generation.
147 // Bulk create aliases.
149 'update[canonical_entities:node]' => TRUE,
151 $this->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
153 // Verify that the alias was created for the node.
154 $this->assertText('Generated 1 URL alias.');