Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / system / tests / src / Functional / Menu / BreadcrumbTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Menu;
4
5 use Drupal\Core\Url;
6 use Drupal\node\Entity\NodeType;
7 use Drupal\Tests\BrowserTestBase;
8 use Drupal\user\RoleInterface;
9
10 /**
11  * Tests breadcrumbs functionality.
12  *
13  * @group Menu
14  */
15 class BreadcrumbTest extends BrowserTestBase {
16
17   use AssertBreadcrumbTrait;
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['menu_test', 'block'];
25
26   /**
27    * An administrative user.
28    *
29    * @var \Drupal\user\UserInterface
30    */
31   protected $adminUser;
32
33   /**
34    * A regular user.
35    *
36    * @var \Drupal\user\UserInterface
37    */
38   protected $webUser;
39
40   /**
41    * Test paths in the Standard profile.
42    *
43    * @var string
44    */
45   protected $profile = 'standard';
46
47   protected function setUp() {
48     parent::setUp();
49
50     $perms = array_keys(\Drupal::service('user.permissions')->getPermissions());
51     $this->adminUser = $this->drupalCreateUser($perms);
52     $this->drupalLogin($this->adminUser);
53
54     // This test puts menu links in the Tools menu and then tests for their
55     // presence on the page, so we need to ensure that the Tools block will be
56     // displayed in the admin theme.
57     $this->drupalPlaceBlock('system_menu_block:tools', [
58       'region' => 'content',
59       'theme' => $this->config('system.theme')->get('admin'),
60     ]);
61   }
62
63   /**
64    * Tests breadcrumbs on node and administrative paths.
65    */
66   public function testBreadCrumbs() {
67     // Prepare common base breadcrumb elements.
68     $home = ['' => 'Home'];
69     $admin = $home + ['admin' => t('Administration')];
70     $config = $admin + ['admin/config' => t('Configuration')];
71     $type = 'article';
72
73     // Verify Taxonomy administration breadcrumbs.
74     $trail = $admin + [
75       'admin/structure' => t('Structure'),
76     ];
77     $this->assertBreadcrumb('admin/structure/taxonomy', $trail);
78
79     $trail += [
80       'admin/structure/taxonomy' => t('Taxonomy'),
81     ];
82     $this->assertBreadcrumb('admin/structure/taxonomy/manage/tags', $trail);
83     $trail += [
84       'admin/structure/taxonomy/manage/tags' => t('Edit Tags'),
85     ];
86     $this->assertBreadcrumb('admin/structure/taxonomy/manage/tags/overview', $trail);
87     $this->assertBreadcrumb('admin/structure/taxonomy/manage/tags/add', $trail);
88
89     // Verify Menu administration breadcrumbs.
90     $trail = $admin + [
91       'admin/structure' => t('Structure'),
92     ];
93     $this->assertBreadcrumb('admin/structure/menu', $trail);
94
95     $trail += [
96       'admin/structure/menu' => t('Menus'),
97     ];
98     $this->assertBreadcrumb('admin/structure/menu/manage/tools', $trail);
99
100     $trail += [
101       'admin/structure/menu/manage/tools' => t('Tools'),
102     ];
103     $this->assertBreadcrumb("admin/structure/menu/link/node.add_page/edit", $trail);
104     $this->assertBreadcrumb('admin/structure/menu/manage/tools/add', $trail);
105
106     // Verify Node administration breadcrumbs.
107     $trail = $admin + [
108       'admin/structure' => t('Structure'),
109       'admin/structure/types' => t('Content types'),
110     ];
111     $this->assertBreadcrumb('admin/structure/types/add', $trail);
112     $this->assertBreadcrumb("admin/structure/types/manage/$type", $trail);
113     $trail += [
114       "admin/structure/types/manage/$type" => t('Article'),
115     ];
116     $this->assertBreadcrumb("admin/structure/types/manage/$type/fields", $trail);
117     $this->assertBreadcrumb("admin/structure/types/manage/$type/display", $trail);
118     $trail_teaser = $trail + [
119       "admin/structure/types/manage/$type/display" => t('Manage display'),
120     ];
121     $this->assertBreadcrumb("admin/structure/types/manage/$type/display/teaser", $trail_teaser);
122     $this->assertBreadcrumb("admin/structure/types/manage/$type/delete", $trail);
123     $trail += [
124       "admin/structure/types/manage/$type/fields" => t('Manage fields'),
125     ];
126     $this->assertBreadcrumb("admin/structure/types/manage/$type/fields/node.$type.body", $trail);
127
128     // Verify Filter text format administration breadcrumbs.
129     $filter_formats = filter_formats();
130     $format = reset($filter_formats);
131     $format_id = $format->id();
132     $trail = $config + [
133       'admin/config/content' => t('Content authoring'),
134     ];
135     $this->assertBreadcrumb('admin/config/content/formats', $trail);
136
137     $trail += [
138       'admin/config/content/formats' => t('Text formats and editors'),
139     ];
140     $this->assertBreadcrumb('admin/config/content/formats/add', $trail);
141     $this->assertBreadcrumb("admin/config/content/formats/manage/$format_id", $trail);
142     // @todo Remove this part once we have a _title_callback, see
143     //   https://www.drupal.org/node/2076085.
144     $trail += [
145       "admin/config/content/formats/manage/$format_id" => $format->label(),
146     ];
147     $this->assertBreadcrumb("admin/config/content/formats/manage/$format_id/disable", $trail);
148
149     // Verify node breadcrumbs (without menu link).
150     $node1 = $this->drupalCreateNode();
151     $nid1 = $node1->id();
152     $trail = $home;
153     $this->assertBreadcrumb("node/$nid1", $trail);
154     // Also verify that the node does not appear elsewhere (e.g., menu trees).
155     $this->assertNoLink($node1->getTitle());
156     // Also verify that the node does not appear elsewhere (e.g., menu trees).
157     $this->assertNoLink($node1->getTitle());
158
159     $trail += [
160       "node/$nid1" => $node1->getTitle(),
161     ];
162     $this->assertBreadcrumb("node/$nid1/edit", $trail);
163
164     // Verify that breadcrumb on node listing page contains "Home" only.
165     $trail = [];
166     $this->assertBreadcrumb('node', $trail);
167
168     // Verify node breadcrumbs (in menu).
169     // Do this separately for Main menu and Tools menu, since only the
170     // latter is a preferred menu by default.
171     // @todo Also test all themes? Manually testing led to the suspicion that
172     //   breadcrumbs may differ, possibly due to theme overrides.
173     $menus = ['main', 'tools'];
174     // Alter node type menu settings.
175     $node_type = NodeType::load($type);
176     $node_type->setThirdPartySetting('menu_ui', 'available_menus', $menus);
177     $node_type->setThirdPartySetting('menu_ui', 'parent', 'tools:');
178     $node_type->save();
179
180     foreach ($menus as $menu) {
181       // Create a parent node in the current menu.
182       $title = $this->randomMachineName();
183       $node2 = $this->drupalCreateNode([
184         'type' => $type,
185         'title' => $title,
186         'menu' => [
187           'enabled' => 1,
188           'title' => 'Parent ' . $title,
189           'description' => '',
190           'menu_name' => $menu,
191           'parent' => '',
192         ],
193       ]);
194
195       if ($menu == 'tools') {
196         $parent = $node2;
197       }
198     }
199
200     // Create a Tools menu link for 'node', move the last parent node menu
201     // link below it, and verify a full breadcrumb for the last child node.
202     $menu = 'tools';
203     $edit = [
204       'title[0][value]' => 'Root',
205       'link[0][uri]' => '/node',
206     ];
207     $this->drupalPostForm("admin/structure/menu/manage/$menu/add", $edit, t('Save'));
208     $menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => 'Root']);
209     $link = reset($menu_links);
210
211     $edit = [
212       'menu[menu_parent]' => $link->getMenuName() . ':' . $link->getPluginId(),
213     ];
214     $this->drupalPostForm('node/' . $parent->id() . '/edit', $edit, t('Save'));
215     $expected = [
216       "node" => $link->getTitle(),
217     ];
218     $trail = $home + $expected;
219     $tree = $expected + [
220       'node/' . $parent->id() => $parent->menu['title'],
221     ];
222     $trail += [
223       'node/' . $parent->id() => $parent->menu['title'],
224     ];
225
226     // Add a taxonomy term/tag to last node, and add a link for that term to the
227     // Tools menu.
228     $tags = [
229       'Drupal' => [],
230       'Breadcrumbs' => [],
231     ];
232     $edit = [
233       'field_tags[target_id]' => implode(',', array_keys($tags)),
234     ];
235     $this->drupalPostForm('node/' . $parent->id() . '/edit', $edit, t('Save'));
236
237     // Put both terms into a hierarchy Drupal ยป Breadcrumbs. Required for both
238     // the menu links and the terms itself, since taxonomy_term_page() resets
239     // the breadcrumb based on taxonomy term hierarchy.
240     $parent_tid = 0;
241     foreach ($tags as $name => $null) {
242       $terms = entity_load_multiple_by_properties('taxonomy_term', ['name' => $name]);
243       $term = reset($terms);
244       $tags[$name]['term'] = $term;
245       if ($parent_tid) {
246         $edit = [
247           'parent[]' => [$parent_tid],
248         ];
249         $this->drupalPostForm("taxonomy/term/{$term->id()}/edit", $edit, t('Save'));
250       }
251       $parent_tid = $term->id();
252     }
253     $parent_mlid = '';
254     foreach ($tags as $name => $data) {
255       $term = $data['term'];
256       $edit = [
257         'title[0][value]' => "$name link",
258         'link[0][uri]' => "/taxonomy/term/{$term->id()}",
259         'menu_parent' => "$menu:{$parent_mlid}",
260         'enabled[value]' => 1,
261       ];
262       $this->drupalPostForm("admin/structure/menu/manage/$menu/add", $edit, t('Save'));
263       $menu_links = entity_load_multiple_by_properties('menu_link_content', [
264         'title' => $edit['title[0][value]'],
265         'link.uri' => 'internal:/taxonomy/term/' . $term->id(),
266       ]);
267       $tags[$name]['link'] = reset($menu_links);
268       $parent_mlid = $tags[$name]['link']->getPluginId();
269     }
270
271     // Verify expected breadcrumbs for menu links.
272     $trail = $home;
273     $tree = [];
274     // Logout the user because we want to check the active class as well, which
275     // is just rendered as anonymous user.
276     $this->drupalLogout();
277     foreach ($tags as $name => $data) {
278       $term = $data['term'];
279       /** @var \Drupal\menu_link_content\MenuLinkContentInterface $link */
280       $link = $data['link'];
281
282       $link_path = $link->getUrlObject()->getInternalPath();
283       $tree += [
284         $link_path => $link->getTitle(),
285       ];
286       $this->assertBreadcrumb($link_path, $trail, $term->getName(), $tree);
287       $this->assertEscaped($parent->getTitle(), 'Tagged node found.');
288
289       // Additionally make sure that this link appears only once; i.e., the
290       // untranslated menu links automatically generated from menu router items
291       // ('taxonomy/term/%') should never be translated and appear in any menu
292       // other than the breadcrumb trail.
293       $elements = $this->xpath('//nav[@id=:menu]/descendant::a[@href=:href]', [
294         ':menu' => 'block-bartik-tools',
295         ':href' => Url::fromUri('base:' . $link_path)->toString(),
296       ]);
297       $this->assertTrue(count($elements) == 1, "Link to {$link_path} appears only once.");
298
299       // Next iteration should expect this tag as parent link.
300       // Note: Term name, not link name, due to taxonomy_term_page().
301       $trail += [
302         $link_path => $term->getName(),
303       ];
304     }
305
306     // Verify breadcrumbs on user and user/%.
307     // We need to log back in and out below, and cannot simply grant the
308     // 'administer users' permission, since user_page() makes your head explode.
309     user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
310       'access user profiles',
311     ]);
312
313     // Verify breadcrumb on front page.
314     $this->assertBreadcrumb('<front>', []);
315
316     // Verify breadcrumb on user pages (without menu link) for anonymous user.
317     $trail = $home;
318     $this->assertBreadcrumb('user', $trail, t('Log in'));
319     $this->assertBreadcrumb('user/' . $this->adminUser->id(), $trail, $this->adminUser->getUsername());
320
321     // Verify breadcrumb on user pages (without menu link) for registered users.
322     $this->drupalLogin($this->adminUser);
323     $trail = $home;
324     $this->assertBreadcrumb('user', $trail, $this->adminUser->getUsername());
325     $this->assertBreadcrumb('user/' . $this->adminUser->id(), $trail, $this->adminUser->getUsername());
326     $trail += [
327       'user/' . $this->adminUser->id() => $this->adminUser->getUsername(),
328     ];
329     $this->assertBreadcrumb('user/' . $this->adminUser->id() . '/edit', $trail, $this->adminUser->getUsername());
330
331     // Create a second user to verify breadcrumb on user pages again.
332     $this->webUser = $this->drupalCreateUser([
333       'administer users',
334       'access user profiles',
335     ]);
336     $this->drupalLogin($this->webUser);
337
338     // Verify correct breadcrumb and page title on another user's account pages.
339     $trail = $home;
340     $this->assertBreadcrumb('user/' . $this->adminUser->id(), $trail, $this->adminUser->getUsername());
341     $trail += [
342       'user/' . $this->adminUser->id() => $this->adminUser->getUsername(),
343     ];
344     $this->assertBreadcrumb('user/' . $this->adminUser->id() . '/edit', $trail, $this->adminUser->getUsername());
345
346     // Verify correct breadcrumb and page title when viewing own user account.
347     $trail = $home;
348     $this->assertBreadcrumb('user/' . $this->webUser->id(), $trail, $this->webUser->getUsername());
349     $trail += [
350       'user/' . $this->webUser->id() => $this->webUser->getUsername(),
351     ];
352     $this->assertBreadcrumb('user/' . $this->webUser->id() . '/edit', $trail, $this->webUser->getUsername());
353
354     // Create an only slightly privileged user being able to access site reports
355     // but not administration pages.
356     $this->webUser = $this->drupalCreateUser([
357       'access site reports',
358     ]);
359     $this->drupalLogin($this->webUser);
360
361     // Verify that we can access recent log entries, there is a corresponding
362     // page title, and that the breadcrumb is just the Home link (because the
363     // user is not able to access "Administer".
364     $trail = $home;
365     $this->assertBreadcrumb('admin', $trail, t('Access denied'));
366     $this->assertSession()->statusCodeEquals(403);
367
368     // Since the 'admin' path is not accessible, we still expect only the Home
369     // link.
370     $this->assertBreadcrumb('admin/reports', $trail, t('Reports'));
371     $this->assertSession()->statusCodeNotEquals(403);
372
373     // Since the Reports page is accessible, that will show.
374     $trail += ['admin/reports' => t('Reports')];
375     $this->assertBreadcrumb('admin/reports/dblog', $trail, t('Recent log messages'));
376     $this->assertSession()->statusCodeNotEquals(403);
377
378     // Ensure that the breadcrumb is safe against XSS.
379     $this->drupalGet('menu-test/breadcrumb1/breadcrumb2/breadcrumb3');
380     $this->assertRaw('<script>alert(12);</script>');
381     $this->assertEscaped('<script>alert(123);</script>');
382   }
383
384 }