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 overriden.
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 should get a new alias,
115 // except the node above whose alias is manually set. Other aliases must be left alone.
116 $this->patterns['node']->delete();
117 $this->patterns['node'] = $this->createPattern('node', '/archive/node-[node:nid]');
119 $this->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
120 $this->assertText('Generated 5 URL aliases.');
122 // Prevent existing aliases to be overriden. The bulk generate page should only offer
123 // to create an alias for paths which have none.
124 $this->drupalPostForm('admin/config/search/path/settings', ['update_action' => PathautoGeneratorInterface::UPDATE_ACTION_NO_NEW], t('Save configuration'));
126 $this->drupalGet('admin/config/search/path/update_bulk');
127 $this->assertFieldByName('action', 'create');
128 $this->assertText('Pathauto settings are set to ignore paths which already have a URL alias.');
129 $this->assertNoFieldByName('action', 'update');
130 $this->assertNoFieldByName('action', 'all');
134 * Tests alias generation for nodes that existed before installing Pathauto.
136 function testBulkUpdateExistingContent() {
138 $node = $this->drupalCreateNode();
140 // Delete its alias and Pathauto metadata.
141 \Drupal::service('pathauto.alias_storage_helper')->deleteEntityPathAll($node);
142 $node->path->first()->get('pathauto')->purge();
143 \Drupal::entityTypeManager()->getStorage('node')->resetCache(array($node->id()));
145 // Execute bulk generation.
146 // Bulk create aliases.
148 'update[canonical_entities:node]' => TRUE,
150 $this->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
152 // Verify that the alias was created for the node.
153 $this->assertText('Generated 1 URL alias.');