View source
<?php
namespace Drupal\Tests\menu_block\Functional;
use Drupal\menu_block\Plugin\Block\MenuBlock;
use Drupal\Tests\BrowserTestBase;
class MenuBlockTest extends BrowserTestBase {
public static $modules = [
'block',
'menu_block',
'menu_block_test',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected $menuLinkManager;
protected $blockStorage;
protected $blockViewBuilder;
protected $menuLinkContentStorage;
protected $moduleHandler;
protected $links;
public function setUp() {
parent::setUp();
$this->menuLinkManager = \Drupal::service('plugin.manager.menu.link');
$this->blockStorage = \Drupal::service('entity_type.manager')
->getStorage('block');
$this->blockViewBuilder = \Drupal::service('entity_type.manager')
->getViewBuilder('block');
$this->menuLinkContentStorage = \Drupal::service('entity_type.manager')
->getStorage('menu_link_content');
$this->moduleHandler = \Drupal::moduleHandler();
$this->links = $this
->createLinkHierarchy();
$this->adminUser = $this
->drupalCreateUser([
'administer blocks',
'access administration pages',
]);
$this
->drupalLogin($this->adminUser);
}
protected function createLinkHierarchy() {
$this->menuLinkManager
->deleteLinksInMenu('main');
$base_options = [
'provider' => 'menu_block',
'menu_name' => 'main',
];
$parent = $base_options + [
'title' => 'parent menu item',
'link' => [
'uri' => 'internal:/menu-block-test/hierarchy/parent',
],
];
$link = $this->menuLinkContentStorage
->create($parent);
$link
->save();
$links['parent'] = $link
->getPluginId();
$child_1 = $base_options + [
'title' => 'child-1 menu item',
'link' => [
'uri' => 'internal:/menu-block-test/hierarchy/parent/child-1',
],
'parent' => $links['parent'],
];
$link = $this->menuLinkContentStorage
->create($child_1);
$link
->save();
$links['child-1'] = $link
->getPluginId();
$child_1_1 = $base_options + [
'title' => 'child-1-1 menu item',
'link' => [
'uri' => 'internal:/menu-block-test/hierarchy/parent/child-1/child-1-1',
],
'parent' => $links['child-1'],
];
$link = $this->menuLinkContentStorage
->create($child_1_1);
$link
->save();
$links['child-1-1'] = $link
->getPluginId();
$child_1_2 = $base_options + [
'title' => 'child-1-2 menu item',
'link' => [
'uri' => 'internal:/menu-block-test/hierarchy/parent/child-1/child-1-2',
],
'parent' => $links['child-1'],
];
$link = $this->menuLinkContentStorage
->create($child_1_2);
$link
->save();
$links['child-1-2'] = $link
->getPluginId();
$child_2 = $base_options + [
'title' => 'child-2 menu item',
'link' => [
'uri' => 'internal:/menu-block-test/hierarchy/parent/child-2',
],
'parent' => $links['parent'],
];
$link = $this->menuLinkContentStorage
->create($child_2);
$link
->save();
$links['child-2'] = $link
->getPluginId();
return $links;
}
public function testMenuBlockFormDisplay() {
$this
->drupalGet('admin/structure/block/add/menu_block:main');
$this
->assertSession()
->pageTextContains('Initial visibility level');
$this
->assertSession()
->pageTextContains('Number of levels to display');
$this
->assertSession()
->pageTextContains('Expand all menu items');
$this
->assertSession()
->pageTextContains('Fixed parent item');
$this
->assertSession()
->pageTextContains('Make the initial visibility level follow the active menu item.');
$this
->assertSession()
->pageTextContains('Theme hook suggestion');
}
public function testMenuBlockUi() {
$block_id = 'main';
$this
->drupalGet('admin/structure/block/add/menu_block:main');
$this
->submitForm([
'id' => $block_id,
'settings[label]' => 'Main navigation',
'settings[label_display]' => FALSE,
'settings[level]' => 2,
'settings[depth]' => 6,
'settings[expand_all_items]' => TRUE,
'settings[parent]' => 'main:',
'settings[follow]' => TRUE,
'settings[follow_parent]' => 'active',
'settings[suggestion]' => 'main',
'region' => 'primary_menu',
], 'Save block');
$block = $this->blockStorage
->load($block_id);
$block_settings = $block
->get('settings');
$this
->assertSame(2, $block_settings['level']);
$this
->assertSame(6, $block_settings['depth']);
$this
->assertTrue($block_settings['expand_all_items']);
$this
->assertSame('main:', $block_settings['parent']);
$this
->assertTrue($block_settings['follow']);
$this
->assertSame('active', $block_settings['follow_parent']);
$this
->assertSame('main', $block_settings['suggestion']);
}
public function testMenuBlockLevel() {
$block_id = 'main';
$this
->drupalGet('admin/structure/block/add/menu_block:main');
$this
->submitForm([
'id' => $block_id,
'settings[label]' => 'Main navigation',
'settings[label_display]' => FALSE,
'settings[level]' => 1,
'region' => 'primary_menu',
], 'Save block');
$this
->drupalGet('<front>');
$this
->assertSession()
->pageTextContains('parent menu item');
$this
->assertSession()
->pageTextNotContains('child-1 menu item');
$this
->assertSession()
->pageTextNotContains('child-1-1 menu item');
$this
->assertSession()
->pageTextNotContains('child-2 menu item');
$this
->drupalGet('admin/structure/block/manage/' . $block_id);
$this
->submitForm([
'settings[level]' => 2,
], 'Save block');
$this
->drupalGet('menu-block-test/hierarchy/parent');
$this
->assertSession()
->pageTextContains('child-1 menu item');
$this
->assertSession()
->pageTextContains('child-2 menu item');
$this
->assertSession()
->pageTextNotContains('parent menu item');
$this
->assertSession()
->pageTextNotContains('child-1-1 menu item');
}
public function testMenuBlockDepth() {
$block_id = 'main';
$this
->drupalGet('admin/structure/block/add/menu_block:main');
$this
->submitForm([
'id' => $block_id,
'settings[label]' => 'Main navigation',
'settings[label_display]' => FALSE,
'settings[level]' => 1,
'settings[depth]' => 1,
'settings[expand_all_items]' => TRUE,
'region' => 'primary_menu',
], 'Save block');
$this
->drupalGet('<front>');
$this
->assertSession()
->pageTextContains('parent menu item');
$this
->assertSession()
->pageTextNotContains('child-1 menu item');
$this
->assertSession()
->pageTextNotContains('child-1-1 menu item');
$this
->assertSession()
->pageTextNotContains('child-2 menu item');
$this
->drupalGet('admin/structure/block/manage/' . $block_id);
$this
->submitForm([
'settings[depth]' => 2,
], 'Save block');
$this
->drupalGet('menu-block-test/hierarchy/parent');
$this
->assertSession()
->pageTextContains('parent menu item');
$this
->assertSession()
->pageTextContains('child-1 menu item');
$this
->assertSession()
->pageTextContains('child-2 menu item');
$this
->assertSession()
->pageTextNotContains('child-1-1 menu item');
$this
->drupalGet('admin/structure/block/manage/' . $block_id);
$this
->submitForm([
'settings[depth]' => 0,
], 'Save block');
$this
->drupalGet('<front>');
$this
->assertSession()
->pageTextContains('parent menu item');
$this
->assertSession()
->pageTextContains('child-1 menu item');
$this
->assertSession()
->pageTextContains('child-2 menu item');
$this
->assertSession()
->pageTextContains('child-1-1 menu item');
}
public function testMenuBlockExpand() {
$block_id = 'main';
$this
->drupalGet('admin/structure/block/add/menu_block:main');
$this
->submitForm([
'id' => $block_id,
'settings[label]' => 'Main navigation',
'settings[label_display]' => FALSE,
'settings[level]' => 1,
'settings[expand_all_items]' => TRUE,
'region' => 'primary_menu',
], 'Save block');
$this
->drupalGet('<front>');
$this
->assertSession()
->pageTextContains('parent menu item');
$this
->assertSession()
->pageTextContains('child-1 menu item');
$this
->assertSession()
->pageTextContains('child-1-1 menu item');
$this
->assertSession()
->pageTextContains('child-2 menu item');
$this
->drupalGet('admin/structure/block/manage/' . $block_id);
$this
->submitForm([
'settings[expand_all_items]' => FALSE,
], 'Save block');
$this
->drupalGet('<front>');
$this
->assertSession()
->pageTextContains('parent menu item');
$this
->assertSession()
->pageTextNotContains('child-1 menu item');
$this
->assertSession()
->pageTextNotContains('child-1-1 menu item');
$this
->assertSession()
->pageTextNotContains('child-2 menu item');
}
public function testMenuBlockParent() {
$block_id = 'main';
$this
->drupalGet('admin/structure/block/add/menu_block:main');
$this
->submitForm([
'id' => $block_id,
'settings[label]' => 'Main navigation',
'settings[label_display]' => FALSE,
'settings[parent]' => 'main:' . $this->links['parent'],
'region' => 'primary_menu',
], 'Save block');
$this
->drupalGet('<front>');
$this
->assertSession()
->pageTextNotContains('parent menu item');
$this
->assertSession()
->pageTextContains('child-1 menu item');
$this
->assertSession()
->pageTextNotContains('child-1-1 menu item');
$this
->drupalGet('admin/structure/block/manage/' . $block_id);
$this
->submitForm([
'settings[parent]' => 'main:' . $this->links['child-1'],
], 'Save block');
$this
->assertSession()
->pageTextNotContains('parent menu item');
$this
->assertSession()
->pageTextNotContains('child-1 menu item');
$this
->assertSession()
->pageTextContains('child-1-1 menu item');
$this
->assertSession()
->pageTextContains('child-1-2 menu item');
}
public function testMenuBlockFollow() {
$block_id = 'main';
$this
->drupalGet('admin/structure/block/add/menu_block:main');
$this
->submitForm([
'id' => $block_id,
'settings[label]' => 'Main navigation',
'settings[label_display]' => FALSE,
'settings[follow]' => TRUE,
'settings[follow_parent]' => 'child',
'region' => 'primary_menu',
], 'Save block');
$this
->drupalGet('<front>');
$this
->assertSession()
->pageTextContains('parent menu item');
$this
->assertSession()
->pageTextNotContains('child-1 menu item');
$this
->assertSession()
->pageTextNotContains('child-1-1 menu item');
$this
->drupalGet('menu-block-test/hierarchy/parent');
$this
->assertSession()
->pageTextNotContains('parent menu item');
$this
->assertSession()
->pageTextContains('child-1 menu item');
$this
->assertSession()
->pageTextContains('child-2 menu item');
$this
->drupalGet('menu-block-test/hierarchy/parent/child-1');
$this
->assertSession()
->pageTextNotContains('parent menu item');
$this
->assertSession()
->pageTextNotContains('child-1 menu item');
$this
->assertSession()
->pageTextContains('child-1-1 menu item');
$this
->assertSession()
->pageTextContains('child-1-2 menu item');
$this
->assertSession()
->pageTextNotContains('child-2 menu item');
$this
->drupalGet('admin/structure/block/manage/' . $block_id);
$this
->submitForm([
'settings[follow_parent]' => 'active',
], 'Save block');
$this
->drupalGet('<front>');
$this
->assertSession()
->pageTextContains('parent menu item');
$this
->assertSession()
->pageTextNotContains('child-1 menu item');
$this
->assertSession()
->pageTextNotContains('child-1-1 menu item');
$this
->drupalGet('menu-block-test/hierarchy/parent');
$this
->assertSession()
->pageTextContains('parent menu item');
$this
->assertSession()
->pageTextContains('child-1 menu item');
$this
->assertSession()
->pageTextNotContains('child-1-1 menu item');
}
public function testMenuBlockSuggestion() {
$block_id = 'main';
$this
->drupalGet('admin/structure/block/add/menu_block:main');
$this
->submitForm([
'id' => $block_id,
'settings[label]' => 'Main navigation',
'settings[label_display]' => FALSE,
'settings[suggestion]' => 'mainnav',
'region' => 'primary_menu',
], 'Save block');
$block = $this->blockStorage
->load($block_id);
$plugin = $block
->getPlugin();
$variables = [];
$variables['elements']['#configuration'] = $plugin
->getConfiguration();
$variables['elements']['#plugin_id'] = $plugin
->getPluginId();
$variables['elements']['#id'] = $block
->id();
$variables['elements']['#base_plugin_id'] = $plugin
->getBaseId();
$variables['elements']['#derivative_plugin_id'] = $plugin
->getDerivativeId();
$variables['elements']['content'] = [];
$suggestions = $this->moduleHandler
->invokeAll('theme_suggestions_block', [
$variables,
]);
$base_theme_hook = 'menu_block';
$hooks = [
'theme_suggestions',
'theme_suggestions_' . $base_theme_hook,
];
$this->moduleHandler
->alter($hooks, $suggestions, $variables, $base_theme_hook);
$this
->assertSame($suggestions, [
'block__menu_block',
'block__menu_block',
'block__menu_block__main',
'block__main',
'block__menu_block__mainnav',
], 'Found expected block suggestions.');
$variables = [
'menu_name' => 'main',
'menu_block_configuration' => $plugin
->getConfiguration(),
];
$suggestions = $this->moduleHandler
->invokeAll('theme_suggestions_menu', [
$variables,
]);
$base_theme_hook = 'menu';
$hooks = [
'theme_suggestions',
'theme_suggestions_' . $base_theme_hook,
];
$this->moduleHandler
->alter($hooks, $suggestions, $variables, $base_theme_hook);
$this
->assertSame($suggestions, [
'menu__main',
'menu__mainnav',
], 'Found expected menu suggestions.');
}
public function testMenuBlockTitleOptions() {
$block_id = 'main';
$this
->drupalGet('admin/structure/block/add/menu_block:main');
$this
->submitForm([
'id' => $block_id,
'settings[label]' => 'Block title',
'settings[label_display]' => TRUE,
'settings[label_link]' => FALSE,
'settings[parent]' => 'main:' . $this->links['child-1'],
'region' => 'primary_menu',
], 'Save block');
$options = [
'block label' => [
'option' => MenuBlock::LABEL_BLOCK,
'title' => 'Block title',
],
'menu label' => [
'option' => MenuBlock::LABEL_MENU,
'title' => 'Main navigation',
],
'fixed menu item' => [
'option' => MenuBlock::LABEL_FIXED,
'title' => 'child-1 menu item',
],
'fixed menu item as link' => [
'option' => MenuBlock::LABEL_FIXED,
'title' => 'child-1 menu item',
'label_link' => TRUE,
],
'fixed menu item parent' => [
'option' => MenuBlock::LABEL_FIXED,
'title' => 'child-1 menu item',
'test_link' => 'menu-block-test/hierarchy/parent',
],
'active item' => [
'option' => MenuBlock::LABEL_ACTIVE_ITEM,
'title' => 'child-1-1 menu item',
],
'active item as link' => [
'option' => MenuBlock::LABEL_ACTIVE_ITEM,
'title' => 'child-1-1 menu item',
'label_link' => TRUE,
],
'parent item' => [
'option' => MenuBlock::LABEL_PARENT,
'title' => 'child-1 menu item',
],
'parent item as link' => [
'option' => MenuBlock::LABEL_PARENT,
'title' => 'child-1 menu item',
'label_link' => TRUE,
],
'parent item top level' => [
'option' => MenuBlock::LABEL_PARENT,
'title' => 'parent menu item',
'test_link' => 'menu-block-test/hierarchy/parent',
],
'parent item 2' => [
'option' => MenuBlock::LABEL_PARENT,
'title' => 'parent menu item',
'test_link' => 'menu-block-test/hierarchy/parent/child-1',
],
'parent item 3' => [
'option' => MenuBlock::LABEL_PARENT,
'title' => 'child-1 menu item',
'test_link' => 'menu-block-test/hierarchy/parent/child-1/child-1-2',
],
'menu root' => [
'option' => MenuBlock::LABEL_ROOT,
'title' => 'parent menu item',
],
'menu root as link' => [
'option' => MenuBlock::LABEL_ROOT,
'title' => 'parent menu item',
'label_link' => TRUE,
],
'menu root 2' => [
'option' => MenuBlock::LABEL_ROOT,
'title' => 'parent menu item',
'test_link' => 'menu-block-test/hierarchy/parent/child-1',
],
'menu root 3' => [
'option' => MenuBlock::LABEL_ROOT,
'title' => 'parent menu item',
'test_link' => 'menu-block-test/hierarchy/parent/child-1/child-1-2',
],
'menu root hidden title' => [
'option' => MenuBlock::LABEL_ROOT,
'title' => 'parent menu item',
'label_display' => FALSE,
],
];
foreach ($options as $case_id => $option) {
$label_display = $option['label_display'] ?? TRUE;
$label_link = $option['label_link'] ?? FALSE;
$this
->drupalGet('admin/structure/block/manage/main');
$this
->submitForm([
'settings[label_type]' => $option['option'],
'settings[label_display]' => $label_display,
'settings[label_link]' => $label_link,
], 'Save block');
$test_link = empty($option['test_link']) ? 'menu-block-test/hierarchy/parent/child-1/child-1-1' : $option['test_link'];
$this
->drupalGet($test_link);
$block_label = $this
->assertSession()
->elementExists('css', 'h2#block-main-menu');
$this
->assertEquals($option['title'], $block_label
->getText(), "Test case '{$case_id}' should have the right title.");
$visible = !$block_label
->hasClass('visually-hidden');
$this
->assertEquals($label_display, $visible, "Test case '{$case_id}' should have the right visibility.");
if ($label_link) {
$this
->assertStringContainsString('<a href="', $block_label
->getHtml(), "Test case '{$case_id}' should have a link in the block title.");
}
else {
$this
->assertStringNotContainsString('<a href="', $block_label
->getHtml(), "Test case '{$case_id}' should not have a link in the block title.");
}
}
}
}