Skip to content

Commit

Permalink
UHF-9135: Some fallback menu test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tuutti committed Oct 23, 2023
1 parent 0b530cf commit f03cb15
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion src/Plugin/Block/MobileMenuFallbackBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<front>'),
],
];
Expand Down
139 changes: 68 additions & 71 deletions templates/menu--external-menu--fallback.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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' %}

<div class="mobile-menu-fallback mobile-menu-fallback__index-1" id="js-menu-fallback">
<div class="mobile-menu-fallback mobile-menu-fallback__index-1" id="js-menu-fallback">

{# Render "back" menu link. #}
{% if menu_link_back %}
{% apply spaceless %}
{% set menu_link_back_title %}
<span class="mmenu__back-wrapper">{{ menu_link_back.title }}</span>
{% 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 %}
<span class="mmenu__back-wrapper">{{ menu_link_back.title }}</span>
{% 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 %}
<ul{{ attributes.addClass(ul_class) }}>
{% 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',
]%}
<li{{ item.attributes.addClass(item_classes) }}>
{% set item_title = item.title %}
{% if items %}
<ul{{ attributes.addClass(ul_class) }}>
{% 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',
]%}
<li{{ item.attributes.addClass(item_classes) }}>
{% 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 %}
<span{{ create_attribute({'class': 'mmenu__item__nolink'} ) }}>{{ item.title }}</span>
{% 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 %}
<span{{ create_attribute({'class': 'mmenu__item__nolink'} ) }}>{{ item.title }}</span>
{% endif %}

{% if has_children %}
{% set next_item_title %}
<span class="visually-hidden">{{ 'Open submenu:'|t({}, {'context': 'Mobile navigation submenu prefix'}) }} {{ item_title }}</span>
{% endset %}
{% apply spaceless %}
{% set link_attributes = { 'class': [ 'mmenu__forward' ] } %}
{{ link(next_item_title, item.next_url, link_attributes) }}
{% endapply %}
{% endif %}
</li>
{% endfor %}
</ul>
{% endif %}
{% if has_children %}
{% set next_item_title %}
<span class="visually-hidden">{{ 'Open submenu:'|t({}, {'context': 'Mobile navigation submenu prefix'}) }} {{ item_title }}</span>
{% endset %}
{% apply spaceless %}
{% set link_attributes = { 'class': [ 'mmenu__forward' ] } %}
{{ link(next_item_title, item.next_url, link_attributes) }}
{% endapply %}
{% endif %}
</li>
{% endfor %}
</ul>
{% endif %}

</div>
{% endif %}
</div>
3 changes: 3 additions & 0 deletions tests/src/Functional/MenuBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -20,6 +21,7 @@
class MenuBlockTest extends BrowserTestBase {

use EnvironmentResolverTrait;
use MenuLinkTrait;

/**
* {@inheritdoc}
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion tests/src/Kernel/KernelTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
66 changes: 66 additions & 0 deletions tests/src/Kernel/Plugin/Block/MobileMenuFallbackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types = 1);

namespace Drupal\Tests\helfi_navigation\Kernel\Plugin\Block;

use Drupal\helfi_api_base\Environment\EnvironmentEnum;
use Drupal\helfi_api_base\Environment\Project;
use Drupal\helfi_navigation\Plugin\Block\MobileMenuFallbackBlock;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\helfi_api_base\Traits\EnvironmentResolverTrait;
use Drupal\Tests\helfi_api_base\Traits\LanguageManagerTrait;
use Drupal\Tests\helfi_navigation\Kernel\MenuTreeBuilderTestBase;

/**
* Tests Mobile menu fallback.
*
* @group helfi_navigation
*/
class MobileMenuFallbackTest extends MenuTreeBuilderTestBase {

use EnvironmentResolverTrait;
use LanguageManagerTrait;

/**
* {@inheritdoc}
*/
protected static $modules = [
'block',
'locale',
'menu_block_current_language',
];

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
NodeType::create(['type' => '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']);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Traits/MenuLinkTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit f03cb15

Please sign in to comment.