View source
<?php
namespace Drupal\Tests\menu_block_current_language\Functional;
use Drupal\Core\Language\LanguageInterface;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\Tests\content_translation\Functional\ContentTranslationTestBase;
class MenuBlockCurrentLanguageTest extends ContentTranslationTestBase {
protected $entityTypeId = 'menu_link_content';
protected $bundle = 'menu_link_content';
protected $defaultTheme = 'stark';
public static $modules = [
'language',
'locale',
'content_translation',
'block',
'test_page_test',
'menu_ui',
'menu_link_content',
'menu_block_current_language',
'menu_block_current_language_views_test',
];
protected $adminUser;
protected $regularUser;
protected $menuBlock;
protected function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer languages',
'access administration pages',
]);
$this
->drupalLogin($this->adminUser);
$edit = [
'language_interface[enabled][language-session]' => TRUE,
'language_interface[weight][language-session]' => -12,
];
$this
->drupalGet('/admin/config/regional/language/detection');
$this
->submitForm($edit, t('Save settings'));
$this->menuBlock = $this
->placeBlock('menu_block_current_language:main');
$this
->drupalLogout();
}
protected function createTestLink($langcode, $title, array $overrides = []) {
$defaults = [
'menu_name' => 'main',
'title' => $title,
'langcode' => $langcode,
'link' => [
'uri' => 'internal:/test-page',
],
];
$link = MenuLinkContent::create($overrides + $defaults);
$link
->save();
return $link;
}
public function testMenuBlockLanguageFilters() {
$config_key = sprintf('block.block.%s', $this->menuBlock
->id());
$this
->config($config_key)
->set('settings.translation_providers', [
'menu_link_content' => '0',
'views' => 'views',
'default' => 'default',
])
->save();
$link = $this
->createTestLink('en', 'First link', [
'expanded' => 1,
]);
$this
->drupalGet('test-page', [
'query' => [
'language' => 'en',
],
]);
$this
->assertSession()
->linkExists($link
->label());
$this
->drupalGet('test-page', [
'query' => [
'language' => 'fr',
],
]);
$this
->assertSession()
->linkExists($link
->label());
$this
->config($config_key)
->set('settings.translation_providers', [
'menu_link_content' => 'menu_link_content',
'views' => 'views',
'default' => 'default',
])
->save();
$this
->drupalGet('test-page', [
'query' => [
'language' => 'fr',
],
]);
$this
->assertSession()
->linkNotExists($link
->label());
$link
->addTranslation('fr', [
'title' => 'First french title',
])
->save();
$this
->drupalGet('test-page', [
'query' => [
'language' => 'fr',
],
]);
$this
->assertSession()
->linkExists('First french title');
$this
->drupalGet('test-page', [
'query' => [
'language' => 'en',
],
]);
$this
->assertSession()
->linkNotExists('First french title');
$link2 = $this
->createTestLink('fr', 'French only title');
$this
->drupalGet('test-page', [
'query' => [
'language' => 'en',
],
]);
$this
->assertSession()
->linkNotExists($link2
->label());
$sublink = $this
->createTestLink('en', 'Sublink en', [
'parent' => $link
->getPluginId(),
]);
$this
->drupalGet('test-page', [
'query' => [
'language' => 'en',
],
]);
$this
->assertSession()
->linkExists($sublink
->label());
$this
->drupalGet('test-page', [
'query' => [
'language' => 'fr',
],
]);
$this
->assertSession()
->linkNotExists($sublink
->label());
$sublink
->addTranslation('fr', [
'title' => 'French sublink',
])
->save();
$this
->drupalGet('test-page', [
'query' => [
'language' => 'fr',
],
]);
$this
->assertSession()
->linkExists('French sublink');
$languages = [
LanguageInterface::LANGCODE_NOT_APPLICABLE,
LanguageInterface::LANGCODE_NOT_SPECIFIED,
];
foreach ($languages as $langcode) {
$link = $this
->createTestLink($langcode, 'Untranslated ' . $langcode);
foreach ([
'fr',
'en',
] as $lang) {
$this
->drupalGet('test-page', [
'query' => [
'language' => $lang,
],
]);
$this
->assertSession()
->linkExists($link
->label());
}
}
$this
->drupalGet('test-view', [
'query' => [
'language' => 'en',
],
]);
$this
->assertSession()
->linkExists('Test menu link');
$this
->drupalGet('test-view', [
'query' => [
'language' => 'fr',
],
]);
$this
->assertSession()
->linkNotExists('Test menu link');
$this
->config($config_key)
->set('settings.translation_providers', [
'menu_link_content' => 'menu_link_content',
'views' => '0',
'default' => 'default',
])
->save();
$this
->drupalGet('test-view', [
'query' => [
'language' => 'fr',
],
]);
$this
->assertSession()
->linkExists('Test menu link');
$sync = \Drupal::service('config.storage.sync');
$this
->copyConfig(\Drupal::service('config.storage'), $sync);
$override_sync = $sync
->createCollection('language.fr');
$override_sync
->write('views.view.test_view', [
'display' => [
'page_1' => [
'display_options' => [
'menu' => [
'title' => 'FR Test menu link',
],
],
],
],
]);
$this
->configImporter()
->import();
$this
->rebuildContainer();
\Drupal::service('router.builder')
->rebuild();
$this
->drupalGet('test-view', [
'query' => [
'language' => 'fr',
],
]);
$this
->assertSession()
->linkExists('FR Test menu link');
$this
->drupalGet('test-view', [
'query' => [
'language' => 'en',
],
]);
$this
->assertSession()
->linkNotExists('FR Test menu link');
$this
->config($config_key)
->set('settings.translation_providers', [
'menu_link_content' => 'menu_link_content',
'views' => 'views',
'default' => 'default',
])
->save();
$this
->drupalGet('test-view', [
'query' => [
'language' => 'fr',
],
]);
$this
->assertSession()
->linkNotExists('Home');
$locale_storage = $this->container
->get('locale.storage');
$translations = $locale_storage
->getTranslations([], [
'filters' => [
'source' => 'Home',
],
]);
foreach ($translations as $translation) {
if ($translation->source !== 'Home') {
continue;
}
$target = $locale_storage
->createTranslation([
'lid' => $translation->lid,
'language' => 'fr',
]);
$target
->setString('French home')
->setCustomized()
->save();
_locale_refresh_translations([
'fr',
], [
$translation->lid,
]);
}
$this
->drupalGet('test-page', [
'query' => [
'language' => 'fr',
],
]);
$this
->assertSession()
->linkExists('French home');
$this
->drupalGet('test-page', [
'query' => [
'language' => 'en',
],
]);
$this
->assertSession()
->linkNotExists('French home');
}
public function testAuthenticated() {
$this
->drupalLogin($this->adminUser);
$this
->testMenuBlockLanguageFilters();
}
}