3 namespace Drupal\pathauto\Tests;
5 use Drupal\pathauto\Entity\PathautoPattern;
6 use Drupal\node\Entity\Node;
7 use Drupal\pathauto\PathautoState;
8 use Drupal\simpletest\WebTestBase;
11 * Tests pathauto node UI integration.
15 class PathautoNodeWebTest extends WebTestBase {
17 use PathautoTestHelperTrait;
24 public static $modules = array('node', 'pathauto', 'views', 'taxonomy', 'pathauto_views_test');
29 * @var \Drupal\user\UserInterface
39 $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
40 $this->drupalCreateContentType(array('type' => 'article'));
42 // Allow other modules to add additional permissions for the admin user.
44 'administer pathauto',
45 '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
62 $this->drupalGet('node/add/page');
63 $this->assertFieldChecked('edit-path-0-pathauto');
65 // Create a node by saving the node form.
66 $title = ' Testing: node title [';
67 $automatic_alias = '/content/testing-node-title';
68 $this->drupalPostForm(NULL, array('title[0][value]' => $title), t('Save'));
69 $node = $this->drupalGetNodeByTitle($title);
71 // Look for alias generated in the form.
72 $this->drupalGet("node/{$node->id()}/edit");
73 $this->assertFieldChecked('edit-path-0-pathauto');
74 $this->assertFieldByName('path[0][alias]', $automatic_alias, 'Generated alias visible in the path alias field.');
76 // Check whether the alias actually works.
77 $this->drupalGet($automatic_alias);
78 $this->assertText($title, 'Node accessible through automatic alias.');
80 // Manually set the node's alias.
81 $manual_alias = '/content/' . $node->id();
83 'path[0][pathauto]' => FALSE,
84 'path[0][alias]' => $manual_alias,
86 $this->drupalPostForm($node->toUrl('edit-form'), $edit, t('Save'));
87 $this->assertText(t('@type @title has been updated.', array('@type' => 'page', '@title' => $title)));
89 // Check that the automatic alias checkbox is now unchecked by default.
90 $this->drupalGet("node/{$node->id()}/edit");
91 $this->assertNoFieldChecked('edit-path-0-pathauto');
92 $this->assertFieldByName('path[0][alias]', $manual_alias);
94 // Submit the node form with the default values.
95 $this->drupalPostForm(NULL, array('path[0][pathauto]' => FALSE), t('Save'));
96 $this->assertText(t('@type @title has been updated.', array('@type' => 'page', '@title' => $title)));
98 // Test that the old (automatic) alias has been deleted and only accessible
99 // through the new (manual) alias.
100 $this->drupalGet($automatic_alias);
101 $this->assertResponse(404, 'Node not accessible through automatic alias.');
102 $this->drupalGet($manual_alias);
103 $this->assertText($title, 'Node accessible through manual alias.');
105 // Test that the manual alias is not kept for new nodes when the pathauto
106 // checkbox is ticked.
107 $title = 'Automatic Title';
109 'title[0][value]' => $title,
110 'path[0][pathauto]' => TRUE,
111 'path[0][alias]' => '/should-not-get-created',
113 $this->drupalPostForm('node/add/page', $edit, t('Save'));
114 $this->assertNoAliasExists(array('alias' => 'should-not-get-created'));
115 $node = $this->drupalGetNodeByTitle($title);
116 $this->assertEntityAlias($node, '/content/automatic-title');
118 // Remove the pattern for nodes, the pathauto checkbox should not be
120 $ids = \Drupal::entityQuery('pathauto_pattern')
121 ->condition('type', 'canonical_entities:node')
123 foreach (PathautoPattern::loadMultiple($ids) as $pattern) {
127 $this->drupalGet('node/add/article');
128 $this->assertNoFieldById('edit-path-0-pathauto');
129 $this->assertFieldByName('path[0][alias]', '');
132 $edit['title'] = 'My test article';
133 $this->drupalCreateNode($edit);
134 //$this->drupalPostForm(NULL, $edit, t('Save'));
135 $node = $this->drupalGetNodeByTitle($edit['title']);
137 // Pathauto checkbox should still not exist.
138 $this->drupalGet($node->toUrl('edit-form'));
139 $this->assertNoFieldById('edit-path-0-pathauto');
140 $this->assertFieldByName('path[0][alias]', '');
141 $this->assertNoEntityAlias($node);
145 * Test node operations.
147 function testNodeOperations() {
148 $node1 = $this->drupalCreateNode(array('title' => 'node1'));
149 $node2 = $this->drupalCreateNode(array('title' => 'node2'));
151 // Delete all current URL aliases.
152 $this->deleteAllAliases();
154 $this->drupalGet('admin/content');
156 // Check which of the two nodes is first.
157 if (strpos($this->getTextContent(), 'node1') < strpos($this->getTextContent(), 'node2')) {
165 'action' => 'pathauto_update_alias_node',
166 'node_bulk_form[' . $index . ']' => TRUE,
168 $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
169 $this->assertText('Update URL alias was applied to 1 item.');
171 $this->assertEntityAlias($node1, '/content/' . $node1->getTitle());
172 $this->assertEntityAlias($node2, '/node/' . $node2->id());
176 * @todo Merge this with existing node test methods?
178 public function testNodeState() {
179 $nodeNoAliasUser = $this->drupalCreateUser(array('bypass node access'));
180 $nodeAliasUser = $this->drupalCreateUser(array('bypass node access', 'create url aliases'));
182 $node = $this->drupalCreateNode(array(
183 'title' => 'Node version one',
186 'pathauto' => PathautoState::SKIP,
190 $this->assertNoEntityAlias($node);
192 // Set a manual path alias for the node.
193 $node->path->alias = '/test-alias';
196 // Ensure that the pathauto field was saved to the database.
197 \Drupal::entityTypeManager()->getStorage('node')->resetCache();
198 $node = Node::load($node->id());
199 $this->assertIdentical($node->path->pathauto, PathautoState::SKIP);
201 // Ensure that the manual path alias was saved and an automatic alias was not generated.
202 $this->assertEntityAlias($node, '/test-alias');
203 $this->assertNoEntityAliasExists($node, '/content/node-version-one');
205 // Save the node as a user who does not have access to path fieldset.
206 $this->drupalLogin($nodeNoAliasUser);
207 $this->drupalGet('node/' . $node->id() . '/edit');
208 $this->assertNoFieldByName('path[0][pathauto]');
210 $edit = array('title[0][value]' => 'Node version two');
211 $this->drupalPostForm(NULL, $edit, 'Save');
212 $this->assertText('Basic page Node version two has been updated.');
214 $this->assertEntityAlias($node, '/test-alias');
215 $this->assertNoEntityAliasExists($node, '/content/node-version-one');
216 $this->assertNoEntityAliasExists($node, '/content/node-version-two');
218 // Load the edit node page and check that the Pathauto checkbox is unchecked.
219 $this->drupalLogin($nodeAliasUser);
220 $this->drupalGet('node/' . $node->id() . '/edit');
221 $this->assertNoFieldChecked('edit-path-0-pathauto');
223 // Edit the manual alias and save the node.
225 'title[0][value]' => 'Node version three',
226 'path[0][alias]' => '/manually-edited-alias',
228 $this->drupalPostForm(NULL, $edit, 'Save');
229 $this->assertText('Basic page Node version three has been updated.');
231 $this->assertEntityAlias($node, '/manually-edited-alias');
232 $this->assertNoEntityAliasExists($node, '/test-alias');
233 $this->assertNoEntityAliasExists($node, '/content/node-version-one');
234 $this->assertNoEntityAliasExists($node, '/content/node-version-two');
235 $this->assertNoEntityAliasExists($node, '/content/node-version-three');
237 // Programatically save the node with an automatic alias.
238 \Drupal::entityTypeManager()->getStorage('node')->resetCache();
239 $node = Node::load($node->id());
240 $node->path->pathauto = PathautoState::CREATE;
243 // Ensure that the pathauto field was saved to the database.
244 \Drupal::entityTypeManager()->getStorage('node')->resetCache();
245 $node = Node::load($node->id());
246 $this->assertIdentical($node->path->pathauto, PathautoState::CREATE);
248 $this->assertEntityAlias($node, '/content/node-version-three');
249 $this->assertNoEntityAliasExists($node, '/manually-edited-alias');
250 $this->assertNoEntityAliasExists($node, '/test-alias');
251 $this->assertNoEntityAliasExists($node, '/content/node-version-one');
252 $this->assertNoEntityAliasExists($node, '/content/node-version-two');
255 $this->assertNull(\Drupal::keyValue('pathauto_state.node')->get($node->id()), 'Pathauto state was deleted');
260 * Tests that nodes without a Pathauto pattern can set custom aliases.
262 public function testCustomAliasWithoutPattern() {
263 // First, delete all patterns to be sure that there will be no match.
264 $entity_ids = \Drupal::entityQuery('pathauto_pattern')->execute();
265 $entities = PathautoPattern::loadMultiple($entity_ids);
266 foreach ($entities as $entity) {
270 // Next, create a node with a custom alias.
272 'title[0][value]' => 'Sample article',
273 'path[0][alias]' => '/sample-article',
275 $this->drupalPostForm('node/add/article', $edit, t('Save'));
276 $this->assertText(t('article Sample article has been created.'));
279 $this->assertAliasExists(array('alias' => '/sample-article'));
280 $this->drupalGet('sample-article');
281 $this->assertResponse(200, 'A node without a pattern can have a custom alias.');
283 // Now create a node through the API.
284 $node = Node::create(['type' => 'article', 'title' => 'Sample article API', 'path' => ['alias' => '/sample-article-api']]);
288 $this->assertAliasExists(['alias' => '/sample-article-api']);
289 $this->drupalGet('sample-article-api');
290 $this->assertResponse(200);