View source
<?php
namespace Drupal\Tests\menu_admin_per_menu\Functional;
use Drupal\Tests\BrowserTestBase;
class MenuAdminPerMenuNodeFormTest extends BrowserTestBase {
protected static $modules = [
'block',
'node',
'menu_admin_per_menu_test',
];
protected $defaultTheme = 'stark';
protected $contentOnlyUser;
protected $contentAndMenuUser;
protected $menu1User;
protected $menu2User;
protected $menu3User;
protected function setUp() : void {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
'display_submitted' => FALSE,
]);
$this
->drupalCreateContentType([
'type' => 'menu_test',
'name' => 'Menu test',
'display_submitted' => FALSE,
'third_party_settings' => [
'menu_ui' => [
'available_menus' => [
'menu_1',
'menu_2',
'menu_3',
],
],
],
]);
$this
->drupalPlaceBlock('system_menu_block:main');
$this->contentOnlyUser = $this
->drupalCreateUser([
'access content',
'administer content types',
]);
$this->contentAndMenuUser = $this
->drupalCreateUser([
'access content',
'administer content types',
'administer main menu items',
]);
$this->menu1User = $this
->createUser([
'access content',
'administer content types',
'administer menu_1 menu items',
], 'Menu 1 user');
$this->menu2User = $this
->createUser([
'access content',
'administer content types',
'administer menu_2 menu items',
], 'Menu 2 user');
$this->menu3User = $this
->createUser([
'access content',
'administer content types',
], 'Menu 3 user');
}
public function testResaveMenuLinkWithoutAccess() {
$menu_link_title = $this
->randomString();
$this
->drupalLogin($this->contentAndMenuUser);
$this
->drupalGet('node/add/page');
$this
->submitForm([
'title[0][value]' => $this
->randomString(),
'body[0][value]' => $this
->randomString(),
'menu[enabled]' => 1,
'menu[title]' => $menu_link_title,
], 'Save');
$this
->assertSession()
->linkExists($menu_link_title);
$this
->drupalLogout();
$this
->drupalLogin($this->contentOnlyUser);
$this
->drupalGet('node/add/page');
$this
->submitForm([
'title[0][value]' => $this
->randomString(),
'body[0][value]' => $this
->randomString(),
], 'Save');
$this
->assertSession()
->linkExists($menu_link_title);
$this
->drupalLogout();
$this
->drupalGet('');
$this
->assertSession()
->linkExists($menu_link_title);
}
public function testMenuPermissionsOnNodeForm() {
$assert_session = $this
->assertSession();
$this
->drupalLogin($this->contentOnlyUser);
$this
->drupalGet('node/add/menu_test');
$assert_session
->fieldNotExists('menu[enabled]');
$assert_session
->fieldNotExists('menu[menu_parent]');
$this
->drupalLogin($this->menu1User);
$this
->drupalGet('node/add/menu_test');
$assert_session
->fieldExists('menu[enabled]');
$assert_session
->optionExists('menu[menu_parent]', 'menu_1:');
$assert_session
->optionExists('menu[menu_parent]', 'menu_1:menu_1.link');
$assert_session
->optionNotExists('menu[menu_parent]', 'menu_2:');
$assert_session
->optionNotExists('menu[menu_parent]', 'menu_2:menu_2.link');
$assert_session
->optionNotExists('menu[menu_parent]', 'menu_3:');
$assert_session
->optionNotExists('menu[menu_parent]', 'menu_3:menu_3.link');
$this
->drupalLogin($this->menu2User);
$this
->drupalGet('node/add/menu_test');
$assert_session
->fieldExists('menu[enabled]');
$assert_session
->optionNotExists('menu[menu_parent]', 'menu_1:');
$assert_session
->optionNotExists('menu[menu_parent]', 'menu_1:menu_1.link');
$assert_session
->optionExists('menu[menu_parent]', 'menu_2:');
$assert_session
->optionExists('menu[menu_parent]', 'menu_2:menu_2.link');
$assert_session
->optionNotExists('menu[menu_parent]', 'menu_3:');
$assert_session
->optionNotExists('menu[menu_parent]', 'menu_3:menu_3.link');
$this
->drupalLogin($this->menu3User);
$this
->drupalGet('node/add/menu_test');
$assert_session
->fieldExists('menu[enabled]');
$assert_session
->optionNotExists('menu[menu_parent]', 'menu_1:');
$assert_session
->optionNotExists('menu[menu_parent]', 'menu_1:menu_1.link');
$assert_session
->optionNotExists('menu[menu_parent]', 'menu_2:');
$assert_session
->optionNotExists('menu[menu_parent]', 'menu_2:menu_2.link');
$assert_session
->optionExists('menu[menu_parent]', 'menu_3:');
$assert_session
->optionExists('menu[menu_parent]', 'menu_3:menu_3.link');
}
public function testEntityOperationsAccess() {
$this
->drupalLogin($this->rootUser);
$node = $this
->drupalCreateNode([
'type' => 'menu_test',
]);
$this
->drupalGet('/admin/content');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->linkByHrefNotExists(sprintf('/admin/structure/menu/manage/%s', $node
->id()));
$this
->assertSession()
->linkByHrefNotExists(sprintf('/admin/structure/menu/manage/%s/add', $node
->id()));
}
}