3 namespace Drupal\pathauto\Tests;
4 use Drupal\pathauto\Entity\PathautoPattern;
5 use Drupal\node\Entity\Node;
6 use Drupal\pathauto\PathautoState;
7 use Drupal\simpletest\WebTestBase;
10 * Tests pathauto node UI integration.
14 class PathautoNodeWebTest extends WebTestBase {
16 use PathautoTestHelperTrait;
23 public static $modules = array('node', 'pathauto', 'views', 'taxonomy', 'pathauto_views_test');
28 * @var \Drupal\user\UserInterface
38 $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
39 $this->drupalCreateContentType(array('type' => 'article'));
41 // Allow other modules to add additional permissions for the admin user.
43 'administer pathauto',
44 'administer url aliases',
48 'access content overview',
50 $this->adminUser = $this->drupalCreateUser($permissions);
51 $this->drupalLogin($this->adminUser);
53 $this->createPattern('node', '/content/[node:title]');
57 * Tests editing nodes with different settings.
59 function testNodeEditing() {
60 // Ensure that the Pathauto checkbox is checked by default on the node add form.
61 $this->drupalGet('node/add/page');
62 $this->assertFieldChecked('edit-path-0-pathauto');
64 // Create a node by saving the node form.
65 $title = ' Testing: node title [';
66 $automatic_alias = '/content/testing-node-title';
67 $this->drupalPostForm(NULL, array('title[0][value]' => $title), t('Save and publish'));
68 $node = $this->drupalGetNodeByTitle($title);
70 // Look for alias generated in the form.
71 $this->drupalGet("node/{$node->id()}/edit");
72 $this->assertFieldChecked('edit-path-0-pathauto');
73 $this->assertFieldByName('path[0][alias]', $automatic_alias, 'Generated alias visible in the path alias field.');
75 // Check whether the alias actually works.
76 $this->drupalGet($automatic_alias);
77 $this->assertText($title, 'Node accessible through automatic alias.');
79 // Manually set the node's alias.
80 $manual_alias = '/content/' . $node->id();
82 'path[0][pathauto]' => FALSE,
83 'path[0][alias]' => $manual_alias,
85 $this->drupalPostForm($node->toUrl('edit-form'), $edit, t('Save and keep published'));
86 $this->assertText(t('@type @title has been updated.', array('@type' => 'page', '@title' => $title)));
88 // Check that the automatic alias checkbox is now unchecked by default.
89 $this->drupalGet("node/{$node->id()}/edit");
90 $this->assertNoFieldChecked('edit-path-0-pathauto');
91 $this->assertFieldByName('path[0][alias]', $manual_alias);
93 // Submit the node form with the default values.
94 $this->drupalPostForm(NULL, array('path[0][pathauto]' => FALSE), t('Save and keep published'));
95 $this->assertText(t('@type @title has been updated.', array('@type' => 'page', '@title' => $title)));
97 // Test that the old (automatic) alias has been deleted and only accessible
98 // through the new (manual) alias.
99 $this->drupalGet($automatic_alias);
100 $this->assertResponse(404, 'Node not accessible through automatic alias.');
101 $this->drupalGet($manual_alias);
102 $this->assertText($title, 'Node accessible through manual alias.');
104 // Test that the manual alias is not kept for new nodes when the pathauto
105 // checkbox is ticked.
106 $title = 'Automatic Title';
108 'title[0][value]' => $title,
109 'path[0][pathauto]' => TRUE,
110 'path[0][alias]' => '/should-not-get-created',
112 $this->drupalPostForm('node/add/page', $edit, t('Save and publish'));
113 $this->assertNoAliasExists(array('alias' => 'should-not-get-created'));
114 $node = $this->drupalGetNodeByTitle($title);
115 $this->assertEntityAlias($node, '/content/automatic-title');
117 // Remove the pattern for nodes, the pathauto checkbox should not be
119 $ids = \Drupal::entityQuery('pathauto_pattern')
120 ->condition('type', 'canonical_entities:node')
122 foreach (PathautoPattern::loadMultiple($ids) as $pattern) {
126 $this->drupalGet('node/add/article');
127 $this->assertNoFieldById('edit-path-0-pathauto');
128 $this->assertFieldByName('path[0][alias]', '');
131 $edit['title'] = 'My test article';
132 $this->drupalCreateNode($edit);
133 //$this->drupalPostForm(NULL, $edit, t('Save and keep published'));
134 $node = $this->drupalGetNodeByTitle($edit['title']);
136 // Pathauto checkbox should still not exist.
137 $this->drupalGet($node->toUrl('edit-form'));
138 $this->assertNoFieldById('edit-path-0-pathauto');
139 $this->assertFieldByName('path[0][alias]', '');
140 $this->assertNoEntityAlias($node);
144 * Test node operations.
146 function testNodeOperations() {
147 $node1 = $this->drupalCreateNode(array('title' => 'node1'));
148 $node2 = $this->drupalCreateNode(array('title' => 'node2'));
150 // Delete all current URL aliases.
151 $this->deleteAllAliases();
153 $this->drupalGet('admin/content');
155 // Check which of the two nodes is first.
156 if (strpos($this->getTextContent(), 'node1') < strpos($this->getTextContent(), 'node2')) {
164 'action' => 'pathauto_update_alias_node',
165 'node_bulk_form[' . $index . ']' => TRUE,
167 $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
168 $this->assertText('Update URL alias was applied to 1 item.');
170 $this->assertEntityAlias($node1, '/content/' . $node1->getTitle());
171 $this->assertEntityAlias($node2, '/node/' . $node2->id());
175 * @todo Merge this with existing node test methods?
177 public function testNodeState() {
178 $nodeNoAliasUser = $this->drupalCreateUser(array('bypass node access'));
179 $nodeAliasUser = $this->drupalCreateUser(array('bypass node access', 'create url aliases'));
181 $node = $this->drupalCreateNode(array(
182 'title' => 'Node version one',
185 'pathauto' => PathautoState::SKIP,
189 $this->assertNoEntityAlias($node);
191 // Set a manual path alias for the node.
192 $node->path->alias = '/test-alias';
195 // Ensure that the pathauto field was saved to the database.
196 \Drupal::entityTypeManager()->getStorage('node')->resetCache();
197 $node = Node::load($node->id());
198 $this->assertIdentical($node->path->pathauto, PathautoState::SKIP);
200 // Ensure that the manual path alias was saved and an automatic alias was not generated.
201 $this->assertEntityAlias($node, '/test-alias');
202 $this->assertNoEntityAliasExists($node, '/content/node-version-one');
204 // Save the node as a user who does not have access to path fieldset.
205 $this->drupalLogin($nodeNoAliasUser);
206 $this->drupalGet('node/' . $node->id() . '/edit');
207 $this->assertNoFieldByName('path[0][pathauto]');
209 $edit = array('title[0][value]' => 'Node version two');
210 $this->drupalPostForm(NULL, $edit, 'Save');
211 $this->assertText('Basic page Node version two has been updated.');
213 $this->assertEntityAlias($node, '/test-alias');
214 $this->assertNoEntityAliasExists($node, '/content/node-version-one');
215 $this->assertNoEntityAliasExists($node, '/content/node-version-two');
217 // Load the edit node page and check that the Pathauto checkbox is unchecked.
218 $this->drupalLogin($nodeAliasUser);
219 $this->drupalGet('node/' . $node->id() . '/edit');
220 $this->assertNoFieldChecked('edit-path-0-pathauto');
222 // Edit the manual alias and save the node.
224 'title[0][value]' => 'Node version three',
225 'path[0][alias]' => '/manually-edited-alias',
227 $this->drupalPostForm(NULL, $edit, 'Save');
228 $this->assertText('Basic page Node version three has been updated.');
230 $this->assertEntityAlias($node, '/manually-edited-alias');
231 $this->assertNoEntityAliasExists($node, '/test-alias');
232 $this->assertNoEntityAliasExists($node, '/content/node-version-one');
233 $this->assertNoEntityAliasExists($node, '/content/node-version-two');
234 $this->assertNoEntityAliasExists($node, '/content/node-version-three');
236 // Programatically save the node with an automatic alias.
237 \Drupal::entityTypeManager()->getStorage('node')->resetCache();
238 $node = Node::load($node->id());
239 $node->path->pathauto = PathautoState::CREATE;
242 // Ensure that the pathauto field was saved to the database.
243 \Drupal::entityTypeManager()->getStorage('node')->resetCache();
244 $node = Node::load($node->id());
245 $this->assertIdentical($node->path->pathauto, PathautoState::CREATE);
247 $this->assertEntityAlias($node, '/content/node-version-three');
248 $this->assertNoEntityAliasExists($node, '/manually-edited-alias');
249 $this->assertNoEntityAliasExists($node, '/test-alias');
250 $this->assertNoEntityAliasExists($node, '/content/node-version-one');
251 $this->assertNoEntityAliasExists($node, '/content/node-version-two');
254 $this->assertNull(\Drupal::keyValue('pathauto_state.node')->get($node->id()), 'Pathauto state was deleted');
259 * Tests that nodes without a Pathauto pattern can set custom aliases.
261 public function testCustomAliasWithoutPattern() {
262 // First, delete all patterns to be sure that there will be no match.
263 $entity_ids = \Drupal::entityQuery('pathauto_pattern')->execute();
264 $entities = PathautoPattern::loadMultiple($entity_ids);
265 foreach ($entities as $entity) {
269 // Next, create a node with a custom alias.
271 'title[0][value]' => 'Sample article',
272 'path[0][alias]' => '/sample-article',
274 $this->drupalPostForm('node/add/article', $edit, t('Save and publish'));
275 $this->assertText(t('article Sample article has been created.'));
278 $this->assertAliasExists(array('alias' => '/sample-article'));
279 $this->drupalGet('sample-article');
280 $this->assertResponse(200, 'A node without a pattern can have a custom alias.');
282 // Now create a node through the API.
283 $node = Node::create(['type' => 'article', 'title' => 'Sample article API', 'path' => ['alias' => '/sample-article-api']]);
287 $this->assertAliasExists(['alias' => '/sample-article-api']);
288 $this->drupalGet('sample-article-api');
289 $this->assertResponse(200);