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