View source
<?php
namespace Drupal\Tests\menu_firstchild\Functional;
use Drupal\Core\Url;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\system\Entity\Menu;
use Drupal\system\MenuStorage;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\menu_ui\Traits\MenuUiTrait;
class MenuFirstchildTest extends BrowserTestBase {
use MenuUiTrait;
use StringTranslationTrait;
public static $modules = [
'node',
'field',
'text',
'options',
'block',
'contextual',
'menu_link_content',
'menu_ui',
'path',
'menu_firstchild',
];
protected $nodes = [];
protected $defaultTheme = 'seven';
protected $adminUser;
protected $authenticatedUser;
protected $menu;
protected function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'access administration pages',
'administer blocks',
'administer menu',
]);
$this->authenticatedUser = $this
->drupalCreateUser([]);
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
'display_submitted' => FALSE,
]);
$this->nodes[] = $this
->drupalCreateNode([
'type' => 'page',
'status' => TRUE,
'title' => 'Item 1',
]);
$this->nodes[] = $this
->drupalCreateNode([
'type' => 'page',
'status' => TRUE,
'title' => 'Item 1',
]);
}
public function testBasicFunc() {
$session = $this
->assertSession();
$first_url = Url::fromRoute('entity.node.canonical', [
'node' => $this->nodes[0]
->id(),
])
->toString();
$node_f_url = "/node/" . $this->nodes[0]
->id();
$this
->drupalGet($node_f_url);
$session
->statusCodeEquals(200, "Status 200");
$second_url = Url::fromRoute('entity.node.canonical', [
'node' => $this->nodes[1]
->id(),
])
->toString();
$node_s_url = "/node/" . $this->nodes[1]
->id();
$this
->drupalGet($node_s_url);
$session
->statusCodeEquals(200, "Status 200");
$this
->drupalLogin($this->adminUser);
$this
->drupalPlaceBlock('local_actions_block', [
'label' => 'Primary admin actions',
'region' => 'content',
'theme' => 'seven',
]);
$this
->drupalGet("admin/structure/block/list/seven");
$this->menu = $this
->addCustomMenu();
$menu_name = $this->menu
->id();
$this
->drupalGet(Url::fromRoute('entity.menu.edit_form', [
'menu' => $menu_name,
]));
$this
->clickLink($this
->t('Add link'));
$this
->drupalPostForm(NULL, [
'link[0][uri]' => 'route:<none>',
'title[0][value]' => "MFC Parent",
'menu_firstchild_enabled' => TRUE,
'expanded' => TRUE,
'description[0][value]' => "MFC Parent",
'weight[0][value]' => 10,
], $this
->t('Save'));
$this
->assertUrl(Url::fromRoute('entity.menu.edit_form', [
'menu' => $menu_name,
]));
$menu_links = \Drupal::entityTypeManager()
->getStorage('menu_link_content')
->loadByProperties([
'title' => "MFC Parent",
]);
$parent_menu_link = reset($menu_links);
$this
->assertInstanceOf(MenuLinkContent::class, $parent_menu_link);
$this
->assertMenuLink([
'menu_name' => $menu_name,
'children' => [],
], $parent_menu_link
->getPluginId());
$this
->clickLink($this
->t('Add link'));
$this
->drupalPostForm(NULL, [
'link[0][uri]' => '/node/' . $this->nodes[0]
->id(),
'title[0][value]' => "First Child",
'description[0][value]' => "First Child",
'menu_parent' => $menu_name . ':' . $parent_menu_link
->getPluginId(),
'weight[0][value]' => 0,
], $this
->t('Save'));
$this
->assertUrl(Url::fromRoute('entity.menu.edit_form', [
'menu' => $menu_name,
]));
$menu_links = \Drupal::entityTypeManager()
->getStorage('menu_link_content')
->loadByProperties([
'title' => "First Child",
]);
$c1_menu_link = reset($menu_links);
$this
->assertInstanceOf(MenuLinkContent::class, $c1_menu_link);
$this
->assertMenuLink([
'menu_name' => $menu_name,
'parent' => $parent_menu_link
->getPluginId(),
], $c1_menu_link
->getPluginId());
$this
->clickLink($this
->t('Add link'));
$this
->drupalPostForm(NULL, [
'link[0][uri]' => '/node/' . $this->nodes[1]
->id(),
'title[0][value]' => "Second Child",
'description[0][value]' => "Second Child",
'menu_parent' => $menu_name . ':' . $parent_menu_link
->getPluginId(),
'weight[0][value]' => 10,
], $this
->t('Save'));
$this
->assertUrl(Url::fromRoute('entity.menu.edit_form', [
'menu' => $menu_name,
]));
$menu_links = \Drupal::entityTypeManager()
->getStorage('menu_link_content')
->loadByProperties([
'title' => "Second Child",
]);
$c2_menu_link = reset($menu_links);
$this
->assertInstanceOf(MenuLinkContent::class, $c2_menu_link);
$this
->assertMenuLink([
'menu_name' => $menu_name,
'parent' => $parent_menu_link
->getPluginId(),
], $c2_menu_link
->getPluginId());
$this
->drupalGet('<front>');
$this
->assertLink($this
->t('MFC Parent'));
$links = $this
->xpath('//a[contains(@href, :href)][@title = :label]', [
':href' => $first_url,
':label' => "MFC Parent",
]);
$this
->assert(!empty($links), "MFC Parent Link Does not exist: " . $first_url . " :: " . count($links));
$this
->assertLink($this
->t('First Child'));
$links = $this
->xpath('//a[contains(@href, :href1)][@title = :label1]/following-sibling::ul[contains(@class, "menu")]/*/a[contains(@href, :href2)][@title = :label2]', [
':href1' => $first_url,
':label1' => "MFC Parent",
':href2' => $first_url,
':label2' => "First Child",
]);
$this
->assert(!empty($links), "First Child Link Does not exist");
$this
->assertLink($this
->t('Second Child'));
$links = $this
->xpath('//a[contains(@href, :href1)][@title = :label1]/following-sibling::ul[contains(@class, "menu")]/*/a[contains(@href, :href2)][@title = :label2]', [
':href1' => $first_url,
':label1' => "MFC Parent",
':href2' => $second_url,
':label2' => "Second Child",
]);
$this
->assert(!empty($links), "Second Child Link Does not exist");
\Drupal::service('module_installer')
->install([
'menu_firstchild_test',
]);
\Drupal::service('cache.menu')
->invalidateAll();
$this
->drupalGet('<front>');
$links = $this
->xpath('//a[contains(@class, :class)][@title = :label]', [
':class' => "custom-class-to-test-for",
':label' => "MFC Parent",
]);
}
public function addCustomMenu() {
$this
->drupalGet('admin/structure/menu/add');
$menu_name = strtolower($this
->randomMachineName(MenuStorage::MAX_ID_LENGTH + 1));
$label = $this
->randomMachineName(16);
$edit = [
'id' => $menu_name,
'description' => '',
'label' => $label,
];
$this
->drupalPostForm('admin/structure/menu/add', $edit, $this
->t('Save'));
$this
->assertRaw($this
->t('@name cannot be longer than %max characters but is currently %length characters long.', [
'@name' => $this
->t('Menu name'),
'%max' => MenuStorage::MAX_ID_LENGTH,
'%length' => mb_strlen($menu_name),
]));
$menu_name = strtolower($this
->randomMachineName(MenuStorage::MAX_ID_LENGTH));
$edit['id'] = $menu_name;
$this
->drupalPostForm('admin/structure/menu/add', $edit, $this
->t('Save'));
$this
->assertNoRaw($this
->t('@name cannot be longer than %max characters but is currently %length characters long.', [
'@name' => $this
->t('Menu name'),
'%max' => MenuStorage::MAX_ID_LENGTH,
'%length' => mb_strlen($menu_name),
]));
$this
->assertRaw($this
->t('Menu %label has been added.', [
'%label' => $label,
]));
$this
->drupalGet('admin/structure/menu');
$this
->assertText($label, 'Menu created');
$this
->drupalGet('admin/structure/block/list/' . $this
->config('system.theme')
->get('default'));
$this
->clickLink('Place block');
$this
->assertText($label);
$block = $this
->drupalPlaceBlock('system_menu_block:' . $menu_name, [
'label' => 'Primary admin actions',
'region' => 'content',
'theme' => 'seven',
]);
$this->blockPlacements[$menu_name] = $block
->id();
return Menu::load($menu_name);
}
}