Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / views / tests / src / Functional / Plugin / MenuLinkTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Plugin;
4
5 use Drupal\menu_link_content\Entity\MenuLinkContent;
6 use Drupal\Tests\views\Functional\ViewTestBase;
7
8 /**
9  * Tests the menu links created in views.
10  *
11  * @group views
12  */
13 class MenuLinkTest extends ViewTestBase {
14
15   /**
16    * Views used by this test.
17    *
18    * @var array
19    */
20   public static $testViews = ['test_menu_link'];
21
22   /**
23    * Modules to enable.
24    *
25    * @var array
26    */
27   public static $modules = ['views', 'views_ui', 'user', 'node', 'menu_ui', 'block'];
28
29   /**
30    * A user with permission to administer views, menus and view content.
31    *
32    * @var \Drupal\user\UserInterface
33    */
34   protected $adminUser;
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function setUp($import_test_views = TRUE) {
40     parent::setUp($import_test_views);
41
42     $this->enableViewsTestModule();
43
44     $this->adminUser = $this->drupalCreateUser(['administer views', 'administer menu']);
45     $this->drupalPlaceBlock('system_menu_block:main');
46     $this->drupalCreateContentType(['type' => 'page']);
47   }
48
49   /**
50    * Test that menu links using menu_link_content as parent are visible.
51    */
52   public function testHierarchicalMenuLinkVisibility() {
53     $this->drupalLogin($this->adminUser);
54
55     $node = $this->drupalCreateNode(['type' => 'page']);
56
57     // Create a primary level menu link to the node.
58     $link = MenuLinkContent::create([
59       'title' => 'Primary level node',
60       'menu_name' => 'main',
61       'bundle' => 'menu_link_content',
62       'parent' => '',
63       'link' => [['uri' => 'entity:node/' . $node->id()]],
64     ]);
65     $link->save();
66
67     $parent_menu_value = 'main:menu_link_content:' . $link->uuid();
68
69     // Alter the view's menu link in view page to use the menu link from the
70     // node as parent.
71     $this->drupalPostForm("admin/structure/views/nojs/display/test_menu_link/page_1/menu", [
72       'menu[type]' => 'normal',
73       'menu[title]' => 'Secondary level view page',
74       'menu[parent]' => $parent_menu_value,
75     ], 'Apply');
76
77     // Save view which has pending changes.
78     $this->drupalPostForm(NULL, [], 'Save');
79
80     // Test if the node as parent menu item is selected in our views settings.
81     $this->drupalGet('admin/structure/views/nojs/display/test_menu_link/page_1/menu');
82     $this->assertOptionSelected('edit-menu-parent', $parent_menu_value);
83
84     $this->drupalGet('');
85
86     // Test if the primary menu item (node) is visible, and the secondary menu
87     // item (view) is hidden.
88     $this->assertText('Primary level node');
89     $this->assertNoText('Secondary level view page');
90
91     // Go to the node page and ensure that both the first and second level items
92     // are visible.
93     $this->drupalGet($node->urlInfo());
94     $this->assertText('Primary level node');
95     $this->assertText('Secondary level view page');
96   }
97
98 }