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