|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Drupal\Tests\oe_bootstrap_theme\Kernel; |
| 6 | + |
| 7 | +use Drupal\block\Entity\Block; |
| 8 | +use Drupal\menu_link_content\Entity\MenuLinkContent; |
| 9 | + |
| 10 | +/** |
| 11 | + * Tests the main navigation block accessibility markup. |
| 12 | + * |
| 13 | + * @group oe_bootstrap_theme |
| 14 | + */ |
| 15 | +class MainNavigationBlockTest extends AbstractKernelTestBase { |
| 16 | + |
| 17 | + /** |
| 18 | + * {@inheritdoc} |
| 19 | + */ |
| 20 | + protected static $modules = [ |
| 21 | + 'block', |
| 22 | + 'link', |
| 23 | + 'menu_link_content', |
| 24 | + ]; |
| 25 | + |
| 26 | + /** |
| 27 | + * {@inheritdoc} |
| 28 | + */ |
| 29 | + protected function setUp(): void { |
| 30 | + parent::setUp(); |
| 31 | + |
| 32 | + $this->installEntitySchema('menu_link_content'); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Ensures the main navigation block has an aria-label and no extra heading. |
| 37 | + */ |
| 38 | + public function testMainNavigationMarkup(): void { |
| 39 | + $this->createMainMenuLink(); |
| 40 | + |
| 41 | + $block = $this->createMainNavigationBlock(); |
| 42 | + |
| 43 | + $build = $this->container->get('entity_type.manager') |
| 44 | + ->getViewBuilder('block') |
| 45 | + ->view($block, 'block'); |
| 46 | + $html = $this->renderRoot($build); |
| 47 | + |
| 48 | + $this->assertRendering($html, [ |
| 49 | + 'count' => [ |
| 50 | + '.me-auto[role="navigation"][aria-label="Main navigation menu"]' => 1, |
| 51 | + 'h2' => 0, |
| 52 | + ], |
| 53 | + ]); |
| 54 | + $this->assertStringNotContainsString('Main navigation bar', $html); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Creates a simple main menu link so the block renders content. |
| 59 | + */ |
| 60 | + protected function createMainMenuLink(): MenuLinkContent { |
| 61 | + $menu_link = MenuLinkContent::create([ |
| 62 | + 'title' => 'Home', |
| 63 | + 'menu_name' => 'main', |
| 64 | + 'link' => [ |
| 65 | + 'uri' => 'internal:/', |
| 66 | + ], |
| 67 | + ]); |
| 68 | + $menu_link->save(); |
| 69 | + |
| 70 | + return $menu_link; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Creates the main navigation block using the theme defaults. |
| 75 | + */ |
| 76 | + protected function createMainNavigationBlock(): Block { |
| 77 | + $block = Block::create([ |
| 78 | + 'id' => 'test_main_navigation', |
| 79 | + 'plugin' => 'system_menu_block:main', |
| 80 | + 'theme' => 'oe_bootstrap_theme', |
| 81 | + 'region' => 'navbar_left', |
| 82 | + 'weight' => -3, |
| 83 | + 'status' => TRUE, |
| 84 | + 'settings' => [ |
| 85 | + 'id' => 'system_menu_block:main', |
| 86 | + 'label' => 'Main navigation menu', |
| 87 | + 'label_display' => FALSE, |
| 88 | + 'provider' => 'system', |
| 89 | + 'level' => 1, |
| 90 | + 'depth' => 0, |
| 91 | + 'expand_all_items' => FALSE, |
| 92 | + ], |
| 93 | + 'visibility' => [], |
| 94 | + ]); |
| 95 | + $block->save(); |
| 96 | + |
| 97 | + return $block; |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments