You are here

public function MenuBlockTest::testMenuBlockSuggestion in Menu Block 8

Tests the menu_block suggestion option.

File

tests/src/Functional/MenuBlockTest.php, line 416

Class

MenuBlockTest
Tests for the menu_block module.

Namespace

Drupal\Tests\menu_block\Functional

Code

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');

  /** @var \Drupal\block\BlockInterface $block */
  $block = $this->blockStorage
    ->load($block_id);
  $plugin = $block
    ->getPlugin();

  // Check theme suggestions for block template.
  $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.');

  // Check theme suggestions for menu template.
  $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.');
}