From f03cb157472778fbf559c87530a0df01c6b771bc Mon Sep 17 00:00:00 2001 From: tuutti Date: Mon, 23 Oct 2023 07:23:56 +0300 Subject: [PATCH] UHF-9135: Some fallback menu test coverage --- .github/workflows/ci.yml | 2 +- composer.json | 3 +- src/Plugin/Block/MobileMenuFallbackBlock.php | 2 +- .../menu--external-menu--fallback.html.twig | 139 +++++++++--------- tests/src/Functional/MenuBlockTest.php | 3 + tests/src/Kernel/KernelTestBase.php | 7 +- .../Plugin/Block/MobileMenuFallbackTest.php | 66 +++++++++ .../rest/resource/GlobalMobileMenuTest.php | 3 +- tests/src/Traits/MenuLinkTrait.php | 2 +- 9 files changed, 150 insertions(+), 77 deletions(-) create mode 100644 tests/src/Kernel/Plugin/Block/MobileMenuFallbackTest.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7218278..6964f63 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -56,7 +56,7 @@ jobs: working-directory: ${{ env.DRUPAL_ROOT }} run: | composer config repositories.5 path $GITHUB_WORKSPACE - composer require drupal/$MODULE_NAME -W + composer require drupal/menu_block_current_language drupal/$MODULE_NAME -W # We use COMPOSER_MIRROR_PATH_REPOS=1 to mirror local repository # instead of symlinking it to prevent code coverage issues with # phpunit. Copy .git folder manually so codecov can generate line by diff --git a/composer.json b/composer.json index 31d7bd5..661f876 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,7 @@ }, "require-dev": { "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0", - "drupal/coder": "^8.3" + "drupal/coder": "^8.3", + "drupal/menu_block_current_language": "^2.0" } } diff --git a/src/Plugin/Block/MobileMenuFallbackBlock.php b/src/Plugin/Block/MobileMenuFallbackBlock.php index 013e448..1840001 100644 --- a/src/Plugin/Block/MobileMenuFallbackBlock.php +++ b/src/Plugin/Block/MobileMenuFallbackBlock.php @@ -117,7 +117,7 @@ private function buildParentLinks(?MenuLinkInterface $activeLink, array $parents [ 'is_currentPage' => $this->pathMatcher->isFrontPage(), 'attributes' => new Attribute(), - 'title' => $this->configFactory->get('system.site')->get('name') ?? $this->t('Front page'), + 'title' => $this->configFactory->get('system.site')->get('name'), 'url' => Url::fromRoute(''), ], ]; diff --git a/templates/menu--external-menu--fallback.html.twig b/templates/menu--external-menu--fallback.html.twig index 37f9a21..468d210 100644 --- a/templates/menu--external-menu--fallback.html.twig +++ b/templates/menu--external-menu--fallback.html.twig @@ -20,83 +20,80 @@ #} {% import _self as menus %} -{% if use_global_navigation %} - {% set max_level = 5 %} {# How deep do we allow the navigation menu to traverse? Counting from "instance", "main level", "level 3", "level 4", "level 5"... #} {% set ul_class = 'mmenu__items' %} -
+
- {# Render "back" menu link. #} - {% if menu_link_back %} - {% apply spaceless %} - {% set menu_link_back_title %} - {{ menu_link_back.title }} - {% endset %} - {% set link_attributes = {'class': [ 'mmenu__back mmenu__back--in-path' ]} %} - {{ link(menu_link_back_title, menu_link_back.url, link_attributes) }} - {% endapply %} - {% endif %} + {# Render "back" menu link. #} + {% if menu_link_back %} + {% apply spaceless %} + {% set menu_link_back_title %} + {{ menu_link_back.title }} + {% endset %} + {% set link_attributes = {'class': [ 'mmenu__back mmenu__back--in-path' ]} %} + {{ link(menu_link_back_title, menu_link_back.url, link_attributes) }} + {% endapply %} + {% endif %} - {# Current page link #} - {% if menu_link_current_or_parent %} - {% apply spaceless %} - {% set menu_link_current_or_parent_title %} - {{ menu_link_current_or_parent.title }} - {% endset %} - {% set link_attributes = { - 'class': [ - 'mmenu__title-link', - menu_link_current_or_parent.is_currentPage ? 'mmenu__title-link--in-path', - ], - 'aria-current': menu_link_current_or_parent.is_currentPage ? 'page': null, - } %} - {{ link(menu_link_current_or_parent_title, menu_link_current_or_parent.url, link_attributes) }} - {% endapply %} - {% endif %} + {# Current page link #} + {% if menu_link_current_or_parent %} + {% apply spaceless %} + {% set menu_link_current_or_parent_title %} + {{ menu_link_current_or_parent.title }} + {% endset %} + {% set link_attributes = { + 'class': [ + 'mmenu__title-link', + menu_link_current_or_parent.is_currentPage ? 'mmenu__title-link--in-path', + ], + 'aria-current': menu_link_current_or_parent.is_currentPage ? 'page': null, + } %} + {{ link(menu_link_current_or_parent_title, menu_link_current_or_parent.url, link_attributes) }} + {% endapply %} + {% endif %} - {% if items %} - - {% for item in items %} - {% set level = menu_level + 1 %} - {% set has_children = false %} - {% if level <= max_level - 2 %} - {% set has_children = item.below %} - {% endif %} - {% set item_classes = [ - 'mmenu__item', - ]%} - - {% set item_title = item.title %} + {% if items %} + + {% for item in items %} + {% set level = menu_level + 1 %} + {% set has_children = false %} + {% if level <= max_level - 2 %} + {% set has_children = item.below %} + {% endif %} + {% set item_classes = [ + 'mmenu__item', + ]%} + + {% set item_title = item.title %} - {% if not item.is_nolink %} - {% apply spaceless %} - {% set link_attributes = { - 'class': [ - 'mmenu__item-link', - item.is_currentPage ? 'mmenu__item-link--in-path', - ], - 'aria-current': item.is_currentPage ? 'page': null, - } %} - {{ link(item_title, item.url, link_attributes) }} - {% endapply %} - {% else %} - {{ item.title }} - {% endif %} + {% if not item.is_nolink %} + {% apply spaceless %} + {% set link_attributes = { + 'class': [ + 'mmenu__item-link', + item.is_currentPage ? 'mmenu__item-link--in-path', + ], + 'aria-current': item.is_currentPage ? 'page': null, + } %} + {{ link(item_title, item.url, link_attributes) }} + {% endapply %} + {% else %} + {{ item.title }} + {% endif %} - {% if has_children %} - {% set next_item_title %} - {{ 'Open submenu:'|t({}, {'context': 'Mobile navigation submenu prefix'}) }} {{ item_title }} - {% endset %} - {% apply spaceless %} - {% set link_attributes = { 'class': [ 'mmenu__forward' ] } %} - {{ link(next_item_title, item.next_url, link_attributes) }} - {% endapply %} - {% endif %} - - {% endfor %} - - {% endif %} + {% if has_children %} + {% set next_item_title %} + {{ 'Open submenu:'|t({}, {'context': 'Mobile navigation submenu prefix'}) }} {{ item_title }} + {% endset %} + {% apply spaceless %} + {% set link_attributes = { 'class': [ 'mmenu__forward' ] } %} + {{ link(next_item_title, item.next_url, link_attributes) }} + {% endapply %} + {% endif %} + + {% endfor %} + + {% endif %} -
-{% endif %} +
diff --git a/tests/src/Functional/MenuBlockTest.php b/tests/src/Functional/MenuBlockTest.php index 6d5a6bc..cdb2ed7 100644 --- a/tests/src/Functional/MenuBlockTest.php +++ b/tests/src/Functional/MenuBlockTest.php @@ -11,6 +11,7 @@ use Drupal\node\Entity\NodeType; use Drupal\Tests\BrowserTestBase; use Drupal\Tests\helfi_api_base\Traits\EnvironmentResolverTrait; +use Drupal\Tests\helfi_navigation\Traits\MenuLinkTrait; /** * Tests menu blocks. @@ -20,6 +21,7 @@ class MenuBlockTest extends BrowserTestBase { use EnvironmentResolverTrait; + use MenuLinkTrait; /** * {@inheritdoc} @@ -72,6 +74,7 @@ public function setUp() : void { * Make sure menu block can be placed. */ public function testExternalMenuBlock() : void { + $this->createLinks(); // Verify that: // 1. Mega menu has only two levels of links. // 2. Block label is translated when a translation is provided. diff --git a/tests/src/Kernel/KernelTestBase.php b/tests/src/Kernel/KernelTestBase.php index 1cd1a2e..4508734 100644 --- a/tests/src/Kernel/KernelTestBase.php +++ b/tests/src/Kernel/KernelTestBase.php @@ -53,7 +53,12 @@ protected function setUp() : void { $this->setupLanguages(); $this->config('language.negotiation') - ->set('url.prefixes', ['en' => 'en', 'fi' => 'fi', 'sv' => 'sv']) + ->set('url.prefixes', [ + 'en' => 'en', + 'fi' => 'fi', + 'sv' => 'sv', + 'es' => 'es', + ]) ->save(); \Drupal::service('kernel')->rebuildContainer(); diff --git a/tests/src/Kernel/Plugin/Block/MobileMenuFallbackTest.php b/tests/src/Kernel/Plugin/Block/MobileMenuFallbackTest.php new file mode 100644 index 0000000..bcef7c4 --- /dev/null +++ b/tests/src/Kernel/Plugin/Block/MobileMenuFallbackTest.php @@ -0,0 +1,66 @@ + 'page'])->save(); + $this->setActiveProject(Project::ASUMINEN, EnvironmentEnum::Local); + + \Drupal::service('content_translation.manager')->setEnabled('node', 'page', TRUE); + } + + /** + * Tests build with different languages. + */ + public function testBuild() : void { + $this->setOverrideLanguageCode('en'); + $this->createLinks(); + $sut = MobileMenuFallbackBlock::create($this->container, [], '', ['provider' => 'helfi_navigation']); + $build = $sut->build(); + // Only links after 'Link 3' should be available because: + // - Anonymous user has no access to 'Link 1' and since 'Link 1 depth 1' + // is a child of 'Link 1' and children inherit permission from its + // parent, thus it should be hidden as well. + // - Link 2 is unpublished. + // - Link 3 is in different language. + $this->assertCount(3, $build['#items']); + + $this->setOverrideLanguageCode('fi'); + $build = $sut->build(); + $this->assertCount(3, $build['#items']); + } + +} diff --git a/tests/src/Kernel/Plugin/rest/resource/GlobalMobileMenuTest.php b/tests/src/Kernel/Plugin/rest/resource/GlobalMobileMenuTest.php index 1387873..1c6e21e 100644 --- a/tests/src/Kernel/Plugin/rest/resource/GlobalMobileMenuTest.php +++ b/tests/src/Kernel/Plugin/rest/resource/GlobalMobileMenuTest.php @@ -2,12 +2,13 @@ declare(strict_types = 1); -namespace Drupal\Tests\helfi_navigation\Kernel; +namespace Drupal\Tests\helfi_navigation\Kernel\Plugin\rest\Resource; use Drupal\helfi_api_base\Environment\EnvironmentEnum; use Drupal\helfi_api_base\Environment\Project; use Drupal\Tests\helfi_api_base\Traits\ApiTestTrait; use Drupal\Tests\helfi_api_base\Traits\EnvironmentResolverTrait; +use Drupal\Tests\helfi_navigation\Kernel\MenuTreeBuilderTestBase; use Drupal\user\Entity\Role; use Drupal\user\RoleInterface; use Prophecy\PhpUnit\ProphecyTrait; diff --git a/tests/src/Traits/MenuLinkTrait.php b/tests/src/Traits/MenuLinkTrait.php index 93b03fa..a27e353 100644 --- a/tests/src/Traits/MenuLinkTrait.php +++ b/tests/src/Traits/MenuLinkTrait.php @@ -30,7 +30,7 @@ trait MenuLinkTrait { */ protected function createNodeWithAlias() : NodeInterface { if (!NodeType::load('page')) { - NodeType::create(['type' => 'page']); + NodeType::create(['type' => 'page'])->save(); } $node = Node::create([ 'title' => 'Test',