diff -u b/tests/src/Functional/MenuBlockTest.php b/tests/src/Functional/MenuBlockTest.php --- b/tests/src/Functional/MenuBlockTest.php +++ b/tests/src/Functional/MenuBlockTest.php @@ -2,8 +2,6 @@ namespace Drupal\Tests\menu_block\Functional; -use Drupal\block\Entity\Block; -use Drupal\menu_link_content\Entity\MenuLinkContent; use Drupal\Tests\BrowserTestBase; /** @@ -26,6 +24,8 @@ /** * An administrative user to configure the test environment. + * + * @var \Drupal\user\Entity\User|false */ protected $adminUser; @@ -36,8 +36,39 @@ */ protected $menuLinkManager; + /** + * The block storage. + * + * @var \Drupal\Core\Entity\EntityStorageInterface + */ protected $blockStorage; + /** + * The block view builder. + * + * @var \Drupal\Core\Entity\EntityViewBuilderInterface + */ + protected $blockViewBuilder; + + /** + * The menu link content storage. + * + * @var \Drupal\Core\Entity\EntityStorageInterface + */ + protected $menuLinkContentStorage; + + /** + * The module handler service. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + /** + * An array containing the menu link plugin ids. + * + * @var array + */ protected $links; /** @@ -48,8 +79,12 @@ $this->menuLinkManager = \Drupal::service('plugin.manager.menu.link'); $this->blockStorage = \Drupal::service('entity_type.manager') - ->getStorage('block') - ; + ->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(); @@ -62,6 +97,72 @@ } /** + * Create a simple hierarchy of links. + */ + protected function createLinkHierarchy() { + // First remove all the menu links in the menu. + $this->menuLinkManager->deleteLinksInMenu('main'); + + // Then create a simple link hierarchy: + // - parent menu item + // - child-1 menu item + // - child-1-1 menu item + // - child-1-2 menu item + // - child-2 menu item. + $base_options = [ + 'provider' => 'menu_block', + 'menu_name' => 'main', + ]; + + $parent = $base_options + [ + 'title' => 'parent menu item', + 'link' => ['uri' => 'internal:/menu-test/hierarchy/parent'], + ]; + /** @var \Drupal\menu_link_content\MenuLinkContentInterface $link */ + $link = $this->menuLinkContentStorage->create($parent); + $link->save(); + $links['parent'] = $link->getPluginId(); + + $child_1 = $base_options + [ + 'title' => 'child-1 menu item', + 'link' => ['uri' => 'internal:/menu-test/hierarchy/parent/child'], + '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-test/hierarchy/parent/child2/child'], + '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-test/hierarchy/parent/child2/child'], + '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-test/hierarchy/parent/child'], + 'parent' => $links['parent'], + ]; + $link = $this->menuLinkContentStorage->create($child_2); + $link->save(); + $links['child-2'] = $link->getPluginId(); + + return $links; + } + + /** * Check if all menu block configuration options are available. */ public function testMenuBlockFormDisplay() { @@ -71,8 +172,7 @@ $this->assertSession()->pageTextContains('Expand all menu links'); $this->assertSession()->pageTextContains('Fixed parent item'); $this->assertSession() - ->pageTextContains('Make the initial visibility level follow the active menu item.') - ; + ->pageTextContains('Make the initial visibility level follow the active menu item.'); $this->assertSession()->pageTextContains('Theme hook suggestion'); } @@ -95,7 +195,7 @@ 'region' => 'primary_menu', ], 'Save block'); - $block = Block::load($block_id); + $block = $this->blockStorage->load($block_id); $block_settings = $block->get('settings'); self::assertSame(2, $block_settings['level']); self::assertSame(6, $block_settings['depth']); @@ -131,7 +231,8 @@ 'settings[level]' => 2, ], 'Save block'); - // Check if the menu items of level 2 are visible, but not the parent menu item. + // Check if the menu items of level 2 are visible, but not the parent menu + // item. $this->drupalGet('menu-test/hierarchy/parent'); $this->assertSession()->pageTextContains('child-1 menu item'); $this->assertSession()->pageTextContains('child-2 menu item'); @@ -166,7 +267,8 @@ 'settings[depth]' => 2, ], 'Save block'); - // Check if the menu items of level 2 are visible, but not the parent menu item. + // Check if the menu items of level 2 are visible, but not the parent menu + // item. $this->drupalGet('menu-test/hierarchy/parent'); $this->assertSession()->pageTextContains('parent menu item'); $this->assertSession()->pageTextContains('child-1 menu item'); @@ -177,7 +279,8 @@ 'settings[depth]' => 0, ], 'Save block'); - // Check if the menu items of level 2 are visible, but not the parent menu item. + // Check if the menu items of level 2 are visible, but not the parent menu + // item. $this->drupalGet(''); $this->assertSession()->pageTextContains('parent menu item'); $this->assertSession()->pageTextContains('child-1 menu item'); @@ -217,6 +320,9 @@ $this->assertSession()->pageTextNotContains('child-2 menu item'); } + /** + * Test the menu_block parent option. + */ public function testMenuBlockParent() { $block_id = 'main'; $this->drupalPostForm('admin/structure/block/add/menu_block:main', [ @@ -227,7 +333,6 @@ 'region' => 'primary_menu', ], 'Save block'); - $this->drupalGet(''); $this->assertSession()->pageTextNotContains('parent menu item'); $this->assertSession()->pageTextContains('child-1 menu item'); @@ -243,6 +348,9 @@ $this->assertSession()->pageTextContains('child-1-2 menu item'); } + /** + * Test the menu_block follow and follow_parent option. + */ public function testMenuBlockFollow() { $block_id = 'main'; $this->drupalPostForm('admin/structure/block/add/menu_block:main', [ @@ -287,68 +395,64 @@ } /** - * Create a simple hierarchy of links. + * Test the menu_block suggestion option. */ - protected function createLinkHierarchy() { - // First remove all the menu links in the menu. - $this->menuLinkManager->deleteLinksInMenu('main'); + public function testMenuBlockSuggestion() { + $block_id = 'main'; + $this->drupalPostForm('admin/structure/block/add/menu_block:main', [ + 'id' => $block_id, + 'settings[label]' => 'Main navigation', + 'settings[label_display]' => FALSE, + 'settings[suggestion]' => 'mainnav', + 'region' => 'primary_menu', + ], 'Save block'); - // Then create a simple link hierarchy: - // - parent menu item - // - child-1 menu item - // - child-1-1 menu item - // - child-1-2 menu item - // - child-2 menu item - $base_options = [ - 'provider' => 'menu_block', - 'menu_name' => 'main', + /** @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); - $parent = $base_options + [ - 'title' => 'parent menu item', - 'link' => ['uri' => 'internal:/menu-test/hierarchy/parent'], - ]; - $link = MenuLinkContent::create($parent); - $link->save(); - $links['parent'] = $link->getPluginId(); - - $child_1 = $base_options + [ - 'title' => 'child-1 menu item', - 'link' => ['uri' => 'internal:/menu-test/hierarchy/parent/child'], - 'parent' => $links['parent'], - ]; - $link = MenuLinkContent::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-test/hierarchy/parent/child2/child'], - 'parent' => $links['child-1'], - ]; - $link = MenuLinkContent::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-test/hierarchy/parent/child2/child'], - 'parent' => $links['child-1'], - ]; - $link = MenuLinkContent::create($child_1_2); - $link->save(); - $links['child-1-2'] = $link->getPluginId(); + $this->assertSame($suggestions, [ + 'block__menu_block', + 'block__menu_block', + 'block__menu_block__main', + 'block__main', + 'block__menu_block__mainnav', + ], 'Found expected block suggestions.'); - $child_2 = $base_options + [ - 'title' => 'child-2 menu item', - 'link' => ['uri' => 'internal:/menu-test/hierarchy/parent/child'], - 'parent' => $links['parent'], - ]; - $link = MenuLinkContent::create($child_2); - $link->save(); - $links['child-2'] = $link->getPluginId(); + // Check theme suggestions for menu template. + $variables = [ + 'menu_name' => 'main', + 'menu_block_configuration' => $plugin->getConfiguration(), + ]; + $suggestions = $this->moduleHandler->invokeAll('theme_suggestions_menu', [$variables]); - return $links; + $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.'); } }