db backup prior to drupal security update
[yaffs-website] / web / core / modules / shortcut / src / Tests / ShortcutLinksTest.php
1 <?php
2
3 namespace Drupal\shortcut\Tests;
4
5 use Drupal\block_content\Entity\BlockContentType;
6 use Drupal\Component\Utility\SafeMarkup;
7 use Drupal\Component\Render\FormattableMarkup;
8 use Drupal\Core\Url;
9 use Drupal\shortcut\Entity\Shortcut;
10 use Drupal\shortcut\Entity\ShortcutSet;
11 use Drupal\views\Entity\View;
12
13 /**
14  * Create, view, edit, delete, and change shortcut links.
15  *
16  * @group shortcut
17  */
18 class ShortcutLinksTest extends ShortcutTestBase {
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = ['router_test', 'views', 'block'];
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function setUp() {
31     parent::setUp();
32
33     $this->drupalPlaceBlock('page_title_block');
34   }
35
36   /**
37    * Tests that creating a shortcut works properly.
38    */
39   public function testShortcutLinkAdd() {
40     $set = $this->set;
41
42     // Create an alias for the node so we can test aliases.
43     $path = [
44       'source' => '/node/' . $this->node->id(),
45       'alias' => '/' . $this->randomMachineName(8),
46     ];
47     $this->container->get('path.alias_storage')->save($path['source'], $path['alias']);
48
49     // Create some paths to test.
50     $test_cases = [
51       '/',
52       '/admin',
53       '/admin/config/system/site-information',
54       '/node/' . $this->node->id() . '/edit',
55       $path['alias'],
56       '/router_test/test2',
57       '/router_test/test3/value',
58     ];
59
60     $test_cases_non_access = [
61       '/admin',
62       '/admin/config/system/site-information',
63     ];
64
65     // Check that each new shortcut links where it should.
66     foreach ($test_cases as $test_path) {
67       $title = $this->randomMachineName();
68       $form_data = [
69         'title[0][value]' => $title,
70         'link[0][uri]' => $test_path,
71       ];
72       $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $set->id() . '/add-link', $form_data, t('Save'));
73       $this->assertResponse(200);
74       $this->assertText(t('Added a shortcut for @title.', ['@title' => $title]));
75       $saved_set = ShortcutSet::load($set->id());
76       $paths = $this->getShortcutInformation($saved_set, 'link');
77       $this->assertTrue(in_array('internal:' . $test_path, $paths), 'Shortcut created: ' . $test_path);
78
79       if (in_array($test_path, $test_cases_non_access)) {
80         $this->assertNoLink($title, SafeMarkup::format('Shortcut link %url not accessible on the page.', ['%url' => $test_path]));
81       }
82       else {
83         $this->assertLink($title, 0, SafeMarkup::format('Shortcut link %url found on the page.', ['%url' => $test_path]));
84       }
85     }
86     $saved_set = ShortcutSet::load($set->id());
87     // Test that saving and re-loading a shortcut preserves its values.
88     $shortcuts = $saved_set->getShortcuts();
89     foreach ($shortcuts as $entity) {
90       // Test the node routes with parameters.
91       $entity->save();
92       $loaded = Shortcut::load($entity->id());
93       $this->assertEqual($entity->link->uri, $loaded->link->uri);
94       $this->assertEqual($entity->link->options, $loaded->link->options);
95     }
96
97     // Log in as non admin user, to check that access is checked when creating
98     // shortcuts.
99     $this->drupalLogin($this->shortcutUser);
100     $title = $this->randomMachineName();
101     $form_data = [
102       'title[0][value]' => $title,
103       'link[0][uri]' => '/admin',
104     ];
105     $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $set->id() . '/add-link', $form_data, t('Save'));
106     $this->assertResponse(200);
107     $this->assertRaw(t("The path '@link_path' is inaccessible.", ['@link_path' => '/admin']));
108
109     $form_data = [
110       'title[0][value]' => $title,
111       'link[0][uri]' => '/node',
112     ];
113     $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $set->id() . '/add-link', $form_data, t('Save'));
114     $this->assertLink($title, 0, 'Shortcut link found on the page.');
115
116     // Create a new shortcut set and add a link to it.
117     $this->drupalLogin($this->adminUser);
118     $edit = [
119       'label' => $this->randomMachineName(),
120       'id' => strtolower($this->randomMachineName()),
121     ];
122     $this->drupalPostForm('admin/config/user-interface/shortcut/add-set', $edit, t('Save'));
123     $title = $this->randomMachineName();
124     $form_data = [
125       'title[0][value]' => $title,
126       'link[0][uri]' => '/admin',
127     ];
128     $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $edit['id'] . '/add-link', $form_data, t('Save'));
129     $this->assertResponse(200);
130   }
131
132   /**
133    * Tests that the "add to shortcut" and "remove from shortcut" links work.
134    */
135   public function testShortcutQuickLink() {
136     \Drupal::service('theme_handler')->install(['seven']);
137     $this->config('system.theme')->set('admin', 'seven')->save();
138     $this->config('node.settings')->set('use_admin_theme', '1')->save();
139     $this->container->get('router.builder')->rebuild();
140
141     $this->drupalLogin($this->rootUser);
142     $this->drupalGet('admin/config/system/cron');
143
144     // Test the "Add to shortcuts" link.
145     $this->clickLink('Add to Default shortcuts');
146     $this->assertText('Added a shortcut for Cron.');
147     $this->assertLink('Cron', 0, 'Shortcut link found on page');
148
149     $this->drupalGet('admin/structure');
150     $this->assertLink('Cron', 0, 'Shortcut link found on different page');
151
152     // Test the "Remove from shortcuts" link.
153     $this->clickLink('Cron');
154     $this->clickLink('Remove from Default shortcuts');
155     $this->assertText('The shortcut Cron has been deleted.');
156     $this->assertNoLink('Cron', 'Shortcut link removed from page');
157
158     $this->drupalGet('admin/structure');
159     $this->assertNoLink('Cron', 'Shortcut link removed from different page');
160
161     $this->drupalGet('admin/people');
162
163     // Test the "Add to shortcuts" link for a page generated by views.
164     $this->clickLink('Add to Default shortcuts');
165     $this->assertText('Added a shortcut for People.');
166     $this->assertShortcutQuickLink('Remove from Default shortcuts');
167
168     // Test the "Remove from  shortcuts" link for a page generated by views.
169     $this->clickLink('Remove from Default shortcuts');
170     $this->assertText('The shortcut People has been deleted.');
171     $this->assertShortcutQuickLink('Add to Default shortcuts');
172
173     // Test two pages which use same route name but different route parameters.
174     $this->drupalGet('node/add/page');
175     // Add Shortcut for Basic Page.
176     $this->clickLink('Add to Default shortcuts');
177     $this->assertText('Added a shortcut for Create Basic page.');
178     // Assure that Article does not have its shortcut indicated as set.
179     $this->drupalGet('node/add/article');
180     $link = $this->xpath('//a[normalize-space()=:label]', [':label' => 'Remove from Default shortcuts']);
181     $this->assertTrue(empty($link), 'Link Remove to Default shortcuts not found for Create Article page.');
182     // Add Shortcut for Article.
183     $this->clickLink('Add to Default shortcuts');
184     $this->assertText('Added a shortcut for Create Article.');
185
186     $this->config('system.theme')->set('default', 'seven')->save();
187     $this->drupalGet('node/' . $this->node->id());
188     $title = $this->node->getTitle();
189
190     // Test the "Add to shortcuts" link for node view route.
191     $this->clickLink('Add to Default shortcuts');
192     $this->assertText(new FormattableMarkup('Added a shortcut for @title.', ['@title' => $title]));
193     $this->assertShortcutQuickLink('Remove from Default shortcuts');
194
195     // Test the "Remove from shortcuts" link for node view route.
196     $this->clickLink('Remove from Default shortcuts');
197     $this->assertText(new FormattableMarkup('The shortcut @title has been deleted.', ['@title' => $title]));
198     $this->assertShortcutQuickLink('Add to Default shortcuts');
199
200     \Drupal::service('module_installer')->install(['block_content']);
201     BlockContentType::create([
202       'id' => 'basic',
203       'label' => 'Basic block',
204       'revision' => FALSE,
205     ])->save();
206     // Test page with HTML tags in title.
207     $this->drupalGet('admin/structure/block/block-content/manage/basic');
208     $page_title = new FormattableMarkup('Edit %label custom block type', ['%label' => 'Basic block']);
209     $this->assertRaw($page_title);
210     // Add shortcut to this page.
211     $this->clickLink('Add to Default shortcuts');
212     $this->assertRaw(new FormattableMarkup('Added a shortcut for %title.', [
213       '%title' => trim(strip_tags($page_title)),
214     ]));
215
216   }
217
218   /**
219    * Tests that shortcut links can be renamed.
220    */
221   public function testShortcutLinkRename() {
222     $set = $this->set;
223
224     // Attempt to rename shortcut link.
225     $new_link_name = $this->randomMachineName();
226
227     $shortcuts = $set->getShortcuts();
228     $shortcut = reset($shortcuts);
229     $this->drupalPostForm('admin/config/user-interface/shortcut/link/' . $shortcut->id(), ['title[0][value]' => $new_link_name], t('Save'));
230     $saved_set = ShortcutSet::load($set->id());
231     $titles = $this->getShortcutInformation($saved_set, 'title');
232     $this->assertTrue(in_array($new_link_name, $titles), 'Shortcut renamed: ' . $new_link_name);
233     $this->assertLink($new_link_name, 0, 'Renamed shortcut link appears on the page.');
234     $this->assertText(t('The shortcut @link has been updated.', ['@link' => $new_link_name]));
235   }
236
237   /**
238    * Tests that changing the path of a shortcut link works.
239    */
240   public function testShortcutLinkChangePath() {
241     $set = $this->set;
242
243     // Tests changing a shortcut path.
244     $new_link_path = '/admin/config';
245
246     $shortcuts = $set->getShortcuts();
247     $shortcut = reset($shortcuts);
248     $this->drupalPostForm('admin/config/user-interface/shortcut/link/' . $shortcut->id(), ['title[0][value]' => $shortcut->getTitle(), 'link[0][uri]' => $new_link_path], t('Save'));
249     $saved_set = ShortcutSet::load($set->id());
250     $paths = $this->getShortcutInformation($saved_set, 'link');
251     $this->assertTrue(in_array('internal:' . $new_link_path, $paths), 'Shortcut path changed: ' . $new_link_path);
252     $this->assertLinkByHref($new_link_path, 0, 'Shortcut with new path appears on the page.');
253     $this->assertText(t('The shortcut @link has been updated.', ['@link' => $shortcut->getTitle()]));
254   }
255
256   /**
257    * Tests that changing the route of a shortcut link works.
258    */
259   public function testShortcutLinkChangeRoute() {
260     $this->drupalLogin($this->rootUser);
261     $this->drupalGet('admin/content');
262     $this->assertResponse(200);
263     // Disable the view.
264     View::load('content')->disable()->save();
265     /** @var \Drupal\Core\Routing\RouteBuilderInterface $router_builder */
266     $router_builder = \Drupal::service('router.builder');
267     $router_builder->rebuildIfNeeded();
268     $this->drupalGet('admin/content');
269     $this->assertResponse(200);
270   }
271
272   /**
273    * Tests deleting a shortcut link.
274    */
275   public function testShortcutLinkDelete() {
276     $set = $this->set;
277
278     $shortcuts = $set->getShortcuts();
279     $shortcut = reset($shortcuts);
280     $this->drupalPostForm('admin/config/user-interface/shortcut/link/' . $shortcut->id() . '/delete', [], 'Delete');
281     $saved_set = ShortcutSet::load($set->id());
282     $ids = $this->getShortcutInformation($saved_set, 'id');
283     $this->assertFalse(in_array($shortcut->id(), $ids), 'Successfully deleted a shortcut.');
284
285     // Delete all the remaining shortcut links.
286     entity_delete_multiple('shortcut', array_filter($ids));
287
288     // Get the front page to check that no exceptions occur.
289     $this->drupalGet('');
290   }
291
292   /**
293    * Tests that the add shortcut link is not displayed for 404/403 errors.
294    *
295    * Tests that the "Add to shortcuts" link is not displayed on a page not
296    * found or a page the user does not have access to.
297    */
298   public function testNoShortcutLink() {
299     // Change to a theme that displays shortcuts.
300     \Drupal::service('theme_handler')->install(['seven']);
301     $this->config('system.theme')
302       ->set('default', 'seven')
303       ->save();
304
305     $this->drupalGet('page-that-does-not-exist');
306     $result = $this->xpath('//a[contains(@class, "shortcut-action--add")]');
307     $this->assertTrue(empty($result), 'Add to shortcuts link was not shown on a page not found.');
308
309     // The user does not have access to this path.
310     $this->drupalGet('admin/modules');
311     $result = $this->xpath('//a[contains(@class, "shortcut-action--add")]');
312     $this->assertTrue(empty($result), 'Add to shortcuts link was not shown on a page the user does not have access to.');
313
314     // Verify that the testing mechanism works by verifying the shortcut link
315     // appears on admin/content.
316     $this->drupalGet('admin/content');
317     $result = $this->xpath('//a[contains(@class, "shortcut-action--remove")]');
318     $this->assertTrue(!empty($result), 'Remove from shortcuts link was shown on a page the user does have access to.');
319
320     // Verify that the shortcut link appears on routing only pages.
321     $this->drupalGet('router_test/test2');
322     $result = $this->xpath('//a[contains(@class, "shortcut-action--add")]');
323     $this->assertTrue(!empty($result), 'Add to shortcuts link was shown on a page the user does have access to.');
324   }
325
326   /**
327    * Tests that the 'access shortcuts' permissions works properly.
328    */
329   public function testAccessShortcutsPermission() {
330     // Change to a theme that displays shortcuts.
331     \Drupal::service('theme_handler')->install(['seven']);
332     $this->config('system.theme')
333       ->set('default', 'seven')
334       ->save();
335
336     // Add cron to the default shortcut set.
337     $this->drupalLogin($this->rootUser);
338     $this->drupalGet('admin/config/system/cron');
339     $this->clickLink('Add to Default shortcuts');
340
341     // Verify that users without the 'access shortcuts' permission can't see the
342     // shortcuts.
343     $this->drupalLogin($this->drupalCreateUser(['access toolbar']));
344     $this->assertNoLink('Shortcuts', 'Shortcut link not found on page.');
345
346     // Verify that users without the 'administer site configuration' permission
347     // can't see the cron shortcuts.
348     $this->drupalLogin($this->drupalCreateUser(['access toolbar', 'access shortcuts']));
349     $this->assertNoLink('Shortcuts', 'Shortcut link not found on page.');
350     $this->assertNoLink('Cron', 'Cron shortcut link not found on page.');
351
352     // Verify that users with the 'access shortcuts' permission can see the
353     // shortcuts.
354     $this->drupalLogin($this->drupalCreateUser([
355       'access toolbar', 'access shortcuts', 'administer site configuration',
356     ]));
357     $this->clickLink('Shortcuts', 0, 'Shortcut link found on page.');
358     $this->assertLink('Cron', 0, 'Cron shortcut link found on page.');
359
360     $this->verifyAccessShortcutsPermissionForEditPages();
361   }
362
363   /**
364    * Tests the shortcuts are correctly ordered by weight in the toolbar.
365    */
366   public function testShortcutLinkOrder() {
367     // Ensure to give permissions to access the shortcuts.
368     $this->drupalLogin($this->drupalCreateUser(['access toolbar', 'access shortcuts', 'access content overview', 'administer content types']));
369     $this->drupalGet(Url::fromRoute('<front>'));
370     $shortcuts = $this->cssSelect('#toolbar-item-shortcuts-tray .toolbar-menu a');
371     $this->assertEqual((string) $shortcuts[0], 'Add content');
372     $this->assertEqual((string) $shortcuts[1], 'All content');
373     foreach ($this->set->getShortcuts() as $shortcut) {
374       $shortcut->setWeight($shortcut->getWeight() * -1)->save();
375     }
376     $this->drupalGet(Url::fromRoute('<front>'));
377     $shortcuts = $this->cssSelect('#toolbar-item-shortcuts-tray .toolbar-menu a');
378     $this->assertEqual((string) $shortcuts[0], 'All content');
379     $this->assertEqual((string) $shortcuts[1], 'Add content');
380   }
381
382   /**
383    * Tests that the 'access shortcuts' permission is required for shortcut set
384    * administration page access.
385    */
386   private function verifyAccessShortcutsPermissionForEditPages() {
387     // Create a user with customize links and switch sets permissions  but
388     // without the 'access shortcuts' permission.
389     $test_permissions = [
390       'customize shortcut links',
391       'switch shortcut sets',
392     ];
393     $noaccess_user = $this->drupalCreateUser($test_permissions);
394     $this->drupalLogin($noaccess_user);
395
396     // Verify that set administration pages are inaccessible without the
397     // 'access shortcuts' permission.
398     $edit_paths = [
399       'admin/config/user-interface/shortcut/manage/default/customize',
400       'admin/config/user-interface/shortcut/manage/default',
401       'user/' . $noaccess_user->id() . '/shortcuts',
402     ];
403
404     foreach ($edit_paths as $path) {
405       $this->drupalGet($path);
406       $message = format_string('Access is denied on %s', ['%s' => $path]);
407       $this->assertResponse(403, $message);
408     }
409   }
410
411   /**
412    * Tests that the 'access shortcuts' permission is required to access the
413    * shortcut block.
414    */
415   public function testShortcutBlockAccess() {
416     // Creates a block instance and place in a region through api.
417     $block = $this->drupalPlaceBlock('shortcuts');
418
419     // Verify that users with the 'access shortcuts' permission can see the
420     // shortcut block.
421     $this->drupalLogin($this->shortcutUser);
422     $this->drupalGet('');
423     $this->assertBlockAppears($block);
424
425     $this->drupalLogout();
426
427     // Verify that users without the 'access shortcuts' permission can see the
428     // shortcut block.
429     $this->drupalLogin($this->drupalCreateUser([]));
430     $this->drupalGet('');
431     $this->assertNoBlockAppears($block);
432   }
433
434   /**
435    * Passes if a shortcut quick link with the specified label is found.
436    *
437    * An optional link index may be passed.
438    *
439    * @param string $label
440    *   Text between the anchor tags.
441    * @param int $index
442    *   Link position counting from zero.
443    * @param string $message
444    *   (optional) A message to display with the assertion. Do not translate
445    *   messages: use format_string() to embed variables in the message text, not
446    *   t(). If left blank, a default message will be displayed.
447    * @param string $group
448    *   (optional) The group this message is in, which is displayed in a column
449    *   in test output. Use 'Debug' to indicate this is debugging output. Do not
450    *   translate this string. Defaults to 'Other'; most tests do not override
451    *   this default.
452    *
453    * @return bool
454    *   TRUE if the assertion succeeded, FALSE otherwise.
455    */
456   protected function assertShortcutQuickLink($label, $index = 0, $message = '', $group = 'Other') {
457     $links = $this->xpath('//a[normalize-space()=:label]', [':label' => $label]);
458     $message = ($message ? $message : SafeMarkup::format('Shortcut quick link with label %label found.', ['%label' => $label]));
459     return $this->assert(isset($links[$index]), $message, $group);
460   }
461
462 }