LinkAttributesMenuTest.php in Link Attributes widget 8
File
tests/src/Functional/LinkAttributesMenuTest.php
View source
<?php
namespace Drupal\Tests\link_attributes\Functional;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\Tests\block\Traits\BlockCreationTrait;
use Drupal\Tests\BrowserTestBase;
class LinkAttributesMenuTest extends BrowserTestBase {
use BlockCreationTrait;
protected static $modules = [
'link_attributes',
'menu_ui',
'menu_link_content',
'block',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this
->placeBlock('system_menu_block:footer');
}
public function testMenuLinkAdmin() {
$this
->drupalLogin($this
->drupalCreateUser(array_keys(\Drupal::service('user.permissions')
->getPermissions())));
$this
->drupalGet('admin/structure/menu/manage/footer/add');
$this
->submitForm([
'title[0][value]' => 'A menu link',
'link[0][uri]' => '<front>',
'link[0][options][attributes][target]' => '_blank',
'link[0][options][attributes][class]' => 'menu__link--really_special menu__link--another-class',
], 'Save');
$this
->drupalGet('user');
$page = $this
->getSession()
->getPage();
$link = $page
->findLink('A menu link');
$this
->assertNotNull($link);
$this
->assertEquals('_blank', $link
->getAttribute('target'));
$this
->assertEquals('menu__link--really_special menu__link--another-class', $link
->getAttribute('class'));
$this
->assertFalse($link
->hasAttribute('rel'));
$id = \Drupal::entityQuery('menu_link_content')
->condition('title', 'A menu link')
->execute();
$menu_link = MenuLinkContent::load(reset($id));
$expected = [
'menu__link--really_special',
'menu__link--another-class',
];
$this
->assertEquals($expected, $menu_link
->getUrlObject()
->getOption('attributes')['class']);
$this
->drupalGet($menu_link
->toUrl('edit-form'));
$this
->assertSession()
->fieldValueEquals('link[0][options][attributes][class]', 'menu__link--really_special menu__link--another-class');
$this
->drupalGet('admin/structure/menu/manage/footer/add');
$this
->submitForm([
'title[0][value]' => 'No target menu link',
'link[0][uri]' => '<front>',
'link[0][options][attributes][target]' => '',
'link[0][options][attributes][rel]' => 'author',
], 'Save');
$this
->drupalGet('user');
$page = $this
->getSession()
->getPage();
$link = $page
->findLink('No target menu link');
$this
->assertNotNull($link);
$this
->assertEquals('author', $link
->getAttribute('rel'));
$this
->assertFalse($link
->hasAttribute('target'));
$this
->assertFalse($link
->hasAttribute('class'));
}
}