464b86c3972d4d66cbcc770b9cd5d23e7a297119
[yaffs-website] / web / core / modules / menu_link_content / tests / src / Functional / MenuLinkContentFormTest.php
1 <?php
2
3 namespace Drupal\Tests\menu_link_content\Functional;
4
5 use Drupal\menu_link_content\Entity\MenuLinkContent;
6 use Drupal\Tests\BrowserTestBase;
7
8 /**
9  * Tests the menu link content UI.
10  *
11  * @group Menu
12  */
13 class MenuLinkContentFormTest extends BrowserTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = [
21     'menu_link_content',
22   ];
23
24   /**
25    * User with 'administer menu' and 'link to any page' permission.
26    *
27    * @var \Drupal\user\Entity\User
28    */
29
30   protected $adminUser;
31
32   /**
33    * User with only 'administer menu' permission.
34    *
35    * @var \Drupal\user\Entity\User
36    */
37
38   protected $basicUser;
39
40   /**
41    * {@inheritdoc}
42    */
43   protected function setUp() {
44     parent::setUp();
45     $this->adminUser = $this->drupalCreateUser(['administer menu', 'link to any page']);
46     $this->basicUser = $this->drupalCreateUser(['administer menu']);
47     $this->drupalLogin($this->adminUser);
48   }
49
50   /**
51    * Tests the 'link to any page' permission for a restricted page.
52    */
53   public function testMenuLinkContentFormLinkToAnyPage() {
54     $menu_link = MenuLinkContent::create([
55       'title' => 'Menu link test',
56       'provider' => 'menu_link_content',
57       'menu_name' => 'admin',
58       'link' => ['uri' => 'internal:/user/login'],
59     ]);
60     $menu_link->save();
61
62     // The user should be able to edit a menu link to the page, even though
63     // the user cannot access the page itself.
64     $this->drupalGet('/admin/structure/menu/item/' . $menu_link->id() . '/edit');
65     $this->assertResponse(200);
66
67     $this->drupalLogin($this->basicUser);
68
69     $this->drupalGet('/admin/structure/menu/item/' . $menu_link->id() . '/edit');
70     $this->assertResponse(403);
71   }
72
73   /**
74    * Tests the MenuLinkContentForm class.
75    */
76   public function testMenuLinkContentForm() {
77     $this->drupalGet('admin/structure/menu/manage/admin/add');
78     $element = $this->xpath('//select[@id = :id]/option[@selected]', [':id' => 'edit-menu-parent']);
79     $this->assertTrue($element, 'A default menu parent was found.');
80     $this->assertEqual('admin:', $element[0]->getValue(), '<Administration> menu is the parent.');
81
82     $this->drupalPostForm(
83       NULL,
84       [
85         'title[0][value]' => t('Front page'),
86         'link[0][uri]' => '<front>',
87       ],
88       t('Save')
89     );
90     $this->assertText(t('The menu link has been saved.'));
91   }
92
93   /**
94    * Tests validation for the MenuLinkContentForm class.
95    */
96   public function testMenuLinkContentFormValidation() {
97     $this->drupalGet('admin/structure/menu/manage/admin/add');
98     $this->drupalPostForm(
99       NULL,
100       [
101         'title[0][value]' => t('Test page'),
102         'link[0][uri]' => '<test>',
103       ],
104       t('Save')
105     );
106     $this->assertText(t('Manually entered paths should start with /, ? or #.'));
107   }
108
109 }