Version 1
[yaffs-website] / web / modules / contrib / pathauto / src / Tests / PathautoNodeWebTest.php
1 <?php
2
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;
8
9 /**
10  * Tests pathauto node UI integration.
11  *
12  * @group pathauto
13  */
14 class PathautoNodeWebTest extends WebTestBase {
15
16   use PathautoTestHelperTrait;
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = array('node', 'pathauto', 'views', 'taxonomy', 'pathauto_views_test');
24
25   /**
26    * Admin user.
27    *
28    * @var \Drupal\user\UserInterface
29    */
30   protected $adminUser;
31
32   /**
33    * {inheritdoc}
34    */
35   function setUp() {
36     parent::setUp();
37
38     $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
39     $this->drupalCreateContentType(array('type' => 'article'));
40
41     // Allow other modules to add additional permissions for the admin user.
42     $permissions = array(
43       'administer pathauto',
44       'administer url aliases',
45       'create url aliases',
46       'administer nodes',
47       'bypass node access',
48       'access content overview',
49     );
50     $this->adminUser = $this->drupalCreateUser($permissions);
51     $this->drupalLogin($this->adminUser);
52
53     $this->createPattern('node', '/content/[node:title]');
54   }
55
56   /**
57    * Tests editing nodes with different settings.
58    */
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');
63
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);
69
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.');
74
75     // Check whether the alias actually works.
76     $this->drupalGet($automatic_alias);
77     $this->assertText($title, 'Node accessible through automatic alias.');
78
79     // Manually set the node's alias.
80     $manual_alias = '/content/' . $node->id();
81     $edit = array(
82       'path[0][pathauto]' => FALSE,
83       'path[0][alias]' => $manual_alias,
84     );
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)));
87
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);
92
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)));
96
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.');
103
104     // Test that the manual alias is not kept for new nodes when the pathauto
105     // checkbox is ticked.
106     $title = 'Automatic Title';
107     $edit = array(
108       'title[0][value]' => $title,
109       'path[0][pathauto]' => TRUE,
110       'path[0][alias]' => '/should-not-get-created',
111     );
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');
116
117     // Remove the pattern for nodes, the pathauto checkbox should not be
118     // displayed.
119     $ids = \Drupal::entityQuery('pathauto_pattern')
120       ->condition('type', 'canonical_entities:node')
121       ->execute();
122     foreach (PathautoPattern::loadMultiple($ids) as $pattern) {
123       $pattern->delete();
124     }
125
126     $this->drupalGet('node/add/article');
127     $this->assertNoFieldById('edit-path-0-pathauto');
128     $this->assertFieldByName('path[0][alias]', '');
129
130     $edit = array();
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']);
135
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);
141   }
142
143   /**
144    * Test node operations.
145    */
146   function testNodeOperations() {
147     $node1 = $this->drupalCreateNode(array('title' => 'node1'));
148     $node2 = $this->drupalCreateNode(array('title' => 'node2'));
149
150     // Delete all current URL aliases.
151     $this->deleteAllAliases();
152
153     $this->drupalGet('admin/content');
154
155     // Check which of the two nodes is first.
156     if (strpos($this->getTextContent(), 'node1') < strpos($this->getTextContent(), 'node2')) {
157       $index = 0;
158     }
159     else {
160       $index = 1;
161     }
162
163     $edit = array(
164       'action' => 'pathauto_update_alias_node',
165       'node_bulk_form[' . $index . ']' => TRUE,
166     );
167     $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
168     $this->assertText('Update URL alias was applied to 1 item.');
169
170     $this->assertEntityAlias($node1, '/content/' . $node1->getTitle());
171     $this->assertEntityAlias($node2, '/node/' . $node2->id());
172   }
173
174   /**
175    * @todo Merge this with existing node test methods?
176    */
177   public function testNodeState() {
178     $nodeNoAliasUser = $this->drupalCreateUser(array('bypass node access'));
179     $nodeAliasUser = $this->drupalCreateUser(array('bypass node access', 'create url aliases'));
180
181     $node = $this->drupalCreateNode(array(
182       'title' => 'Node version one',
183       'type' => 'page',
184       'path' => array(
185         'pathauto' => PathautoState::SKIP,
186       ),
187     ));
188
189     $this->assertNoEntityAlias($node);
190
191     // Set a manual path alias for the node.
192     $node->path->alias = '/test-alias';
193     $node->save();
194
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);
199
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');
203
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]');
208
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.');
212
213     $this->assertEntityAlias($node, '/test-alias');
214     $this->assertNoEntityAliasExists($node, '/content/node-version-one');
215     $this->assertNoEntityAliasExists($node, '/content/node-version-two');
216
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');
221
222     // Edit the manual alias and save the node.
223     $edit = array(
224       'title[0][value]' => 'Node version three',
225       'path[0][alias]' => '/manually-edited-alias',
226     );
227     $this->drupalPostForm(NULL, $edit, 'Save');
228     $this->assertText('Basic page Node version three has been updated.');
229
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');
235
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;
240     $node->save();
241
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);
246
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');
252
253     $node->delete();
254     $this->assertNull(\Drupal::keyValue('pathauto_state.node')->get($node->id()), 'Pathauto state was deleted');
255   }
256
257
258   /**
259    * Tests that nodes without a Pathauto pattern can set custom aliases.
260    */
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) {
266       $entity->delete();
267     }
268
269     // Next, create a node with a custom alias.
270     $edit = [
271       'title[0][value]' => 'Sample article',
272       'path[0][alias]' => '/sample-article',
273     ];
274     $this->drupalPostForm('node/add/article', $edit, t('Save and publish'));
275     $this->assertText(t('article Sample article has been created.'));
276
277     // Test the alias.
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.');
281
282     // Now create a node through the API.
283     $node = Node::create(['type' => 'article', 'title' => 'Sample article API', 'path' => ['alias' => '/sample-article-api']]);
284     $node->save();
285
286     // Test the alias.
287     $this->assertAliasExists(['alias' => '/sample-article-api']);
288     $this->drupalGet('sample-article-api');
289     $this->assertResponse(200);
290   }
291
292 }