Skip to content

Commit

Permalink
Merge pull request #840 from City-of-Helsinki/UHF-11367
Browse files Browse the repository at this point in the history
UHF-11367: Test coverage
  • Loading branch information
tuutti authored Feb 6, 2025
2 parents b3605d1 + 12318be commit e701749
Show file tree
Hide file tree
Showing 5 changed files with 320 additions and 43 deletions.
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@

declare(strict_types=1);

use Drupal\helfi_api_base\Language\DefaultLanguageResolver;

/**
* Implements hook_preprocess_HOOK().
*/
function helfi_alt_lang_fallback_preprocess_region(&$variables): void {
/** @var \Drupal\helfi_alt_lang_fallback\AltLanguageFallbacks $lang_fallbacks */
$lang_fallbacks = \Drupal::service('helfi_alt_lang_fallback');
/** @var \Drupal\helfi_api_base\Language\DefaultLanguageResolver $defaultLanguageResolver */
$defaultLanguageResolver = \Drupal::service(DefaultLanguageResolver::class);
if ($lang_fallbacks->shouldAttributesBeAddedToRegion($variables['region'])) {
$attributes = $lang_fallbacks->getLangAttributes();
$attributes = $defaultLanguageResolver->getFallbackLangAttributes();
$variables['attributes'] = array_replace($variables['attributes'], $attributes);
}
}
Expand All @@ -34,12 +38,15 @@ function helfi_alt_lang_fallback_preprocess_block(&$variables): void {
if (!$lang_fallbacks->shouldAttributesBeAddedToBlock($variables['plugin_id'])) {
return;
}
/** @var \Drupal\helfi_api_base\Language\DefaultLanguageResolver $defaultLanguageResolver */
$defaultLanguageResolver = \Drupal::service(DefaultLanguageResolver::class);

// Check if block has fallback content. Otherwise add current lang attributes.
if ($lang_fallbacks->checkIfBlockHasFallbackContent($variables)) {
$attributes = $lang_fallbacks->getLangAttributes();
$attributes = $defaultLanguageResolver->getFallbackLangAttributes();
}
else {
$attributes = $lang_fallbacks->getCurrentLangAttributes();
$attributes = $defaultLanguageResolver->getCurrentLangAttributes();
}

// Add either fallback or current language attributes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,6 @@ public static function create(ContainerInterface $container) : self {
);
}

/**
* Check if current or specific language is considered not fully supported.
*
* Does not account for language being actually in use.
*
* @param string|null $langcode
* Langcode to check. Defaults to current language.
*
* @return bool
* If language is considered alternative and not fully supported.
*/
public function isAltLanguage(?string $langcode = NULL): bool {
return $this->defaultLanguageResolver->isAltLanguage($langcode);
}

/**
* Checks if region has fallback language content.
*
Expand All @@ -146,7 +131,7 @@ public function isAltLanguage(?string $langcode = NULL): bool {
*/
public function shouldAttributesBeAddedToRegion(string $region_name): bool {
// Only act on alternative languages.
if (!$this->isAltLanguage()) {
if (!$this->defaultLanguageResolver->isAltLanguage()) {
return FALSE;
}

Expand All @@ -166,7 +151,7 @@ public function shouldAttributesBeAddedToRegion(string $region_name): bool {
*/
public function shouldAttributesBeAddedToBlock(string $plugin_id): bool {
// Only act on alternative languages.
if (!$this->isAltLanguage()) {
if (!$this->defaultLanguageResolver->isAltLanguage()) {
return FALSE;
}

Expand All @@ -184,7 +169,7 @@ public function shouldAttributesBeAddedToBlock(string $plugin_id): bool {
*/
public function checkIfBlockHasFallbackContent(array $variables): bool {
// Only act on alternative languages.
if (!$this->isAltLanguage()) {
if (!$this->defaultLanguageResolver->isAltLanguage()) {
return FALSE;
}

Expand Down Expand Up @@ -213,7 +198,7 @@ public function checkIfBlockHasFallbackContent(array $variables): bool {
*/
public function shouldMenuTreeBeReplaced(string $menu_name, array $items): bool {
// Only act on alternative languages.
if (!$this->isAltLanguage()) {
if (!$this->defaultLanguageResolver->isAltLanguage()) {
return FALSE;
}

Expand Down Expand Up @@ -283,24 +268,4 @@ public function replaceMenuTree(string $menu_name): array {
return [];
}

/**
* Gets lang, dir and other attributes for fallback elements.
*
* @return array
* Array with attributes.
*/
public function getLangAttributes(): array {
return $this->defaultLanguageResolver->getFallbackLangAttributes();
}

/**
* Gets lang, dir and other attributes for fallback elements.
*
* @return array
* Array with attributes.
*/
public function getCurrentLangAttributes(): array {
return $this->defaultLanguageResolver->getCurrentLangAttributes();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\helfi_alt_lang_fallback\Kernel;

use Drupal\helfi_alt_lang_fallback\AltLanguageFallbacks;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\system\Entity\Menu;
use Drupal\Tests\helfi_api_base\Traits\ApiTestTrait;
use Drupal\Tests\helfi_api_base\Traits\LanguageManagerTrait;

/**
* Tests the Alt language fallbacks resource.
*
* @group helfi_alt_lang_fallback
*/
class AltLanguageFallbacksTest extends KernelTestBase {

use LanguageManagerTrait;
use ApiTestTrait;

/**
* {@inheritdoc}
*/
protected static $modules = [
'helfi_language_negotiator_test',
'helfi_api_base',
'helfi_alt_lang_fallback',
'menu_block_current_language',
'content_translation',
'language',
'menu_link_content',
'locale',
'block',
'link',
'user',
'system',
];

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();

$this->installConfig(['language', 'content_translation']);
$this->installEntitySchema('menu_link_content');

$this->installConfig(['content_translation']);
$this->setupLanguages();
// Install estonian.
ConfigurableLanguage::createFromLangcode('et')->save();

$this->config('language.negotiation')
->set('url.prefixes', ['en' => 'en', 'fi' => 'fi', 'sv' => 'sv', 'et' => 'et'])
->save();

$this->enableTranslation(['menu_link_content']);

Menu::create([
'id' => 'headertopnavigation',
'label' => 'Header top navigation',
])->save();

\Drupal::service('kernel')->rebuildContainer();
}

/**
* Creates a new menu link.
*
* @param string $title
* The title.
* @param string $langcode
* The langcode.
* @param string $menuName
* The menu name.
* @param string $uri
* The uri.
*
* @return \Drupal\menu_link_content\Entity\MenuLinkContent
* The menu link.
*/
private function createMenuLink(string $title, string $langcode, string $menuName, string $uri = 'internal:/test-page') : MenuLinkContent {
$link = MenuLinkContent::create([
'menu_name' => $menuName,
'title' => $title,
'langcode' => $langcode,
'link' => [
'uri' => $uri,
],
]);
$link->save();

return $link;
}

/**
* Renders the given menu block.
*
* @param string $menuId
* The menu to render.
* @param string $langcode
* The langcode.
*
* @return array
* The menu tree.
*/
private function renderMenuBlock(string $menuId, string $langcode) : array {
$this->setOverrideLanguageCode($langcode);
$sut = AltLanguageFallbacks::create($this->container);
return $sut->replaceMenuTree($menuId);
}

/**
* Tests replaceMenuTree().
*/
public function testReplaceMenuTree() : void {
$links = [
[
'title' => 'Test title en',
'langcode' => 'en',
],
[
'title' => 'Test title fi',
'langcode' => 'fi',
],
];

foreach ($links as $item) {
$this->createMenuLink($item['title'], $item['langcode'], 'headertopnavigation');
}
// Make sure english link is rendered for alt language.
$this->createMenuLink('Test title et', 'et', 'headertopnavigation', 'route:<nolink>');
$build = $this->renderMenuBlock('headertopnavigation', 'et');
$this->assertCount(1, $build);
$item = reset($build);
$this->assertEquals('Test title en', $item['title']);

$variables = [
'menu_name' => 'headertopnavigation',
'items' => $build,
];
helfi_alt_lang_fallback_preprocess_menu($variables);

// Make sure preprocess yields same menu items.
$this->assertEquals($build, $variables['items']);
}

}
Loading

0 comments on commit e701749

Please sign in to comment.