View source
<?php
namespace Drupal\Tests\block\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\block\Entity\Block;
class BlockLanguageTest extends BrowserTestBase {
protected $adminUser;
protected static $modules = [
'language',
'block',
'content_translation',
'node',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer blocks',
'administer languages',
]);
$this
->drupalLogin($this->adminUser);
$edit = [
'predefined_langcode' => 'fr',
];
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add language');
$this
->assertSession()
->pageTextContains('French');
$this
->config('language.negotiation')
->set('url', [
'source' => 'path_prefix',
'prefixes' => [
'en' => 'en',
'fr' => 'fr',
],
])
->save();
$this
->drupalCreateContentType([
'type' => 'page',
]);
$this
->drupalCreateNode();
}
public function testLanguageBlockVisibility() {
$default_theme = $this
->config('system.theme')
->get('default');
$this
->drupalGet('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme);
$this
->assertSession()
->fieldExists('visibility[language][langcodes][en]');
$this
->assertSession()
->fieldNotExists('visibility[language][context_mapping][language]');
$edit = [
'visibility[language][langcodes][en]' => TRUE,
'id' => strtolower($this
->randomMachineName(8)),
'region' => 'sidebar_first',
];
$this
->drupalGet('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme);
$this
->submitForm($edit, 'Save block');
$edit = [
'site_default_language' => 'fr',
];
$this
->drupalGet('admin/config/regional/language');
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('en');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('Powered by Drupal');
$this
->drupalGet('fr');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextNotContains('Powered by Drupal');
}
public function testLanguageBlockVisibilityLanguageDelete() {
$edit = [
'visibility' => [
'language' => [
'langcodes' => [
'fr' => 'fr',
],
'context_mapping' => [
'language' => '@language.current_language_context:language_interface',
],
],
],
];
$block = $this
->drupalPlaceBlock('system_powered_by_block', $edit);
$visibility = $block
->getVisibility();
$this
->assertEquals('fr', $visibility['language']['langcodes']['fr'], 'Language is set in the block configuration.');
$this
->drupalGet('admin/config/regional/language/delete/fr');
$this
->submitForm([], 'Delete');
$block = Block::load($block
->id());
$visibility = $block
->getVisibility();
$this
->assertArrayNotHasKey('language', $visibility, 'Language is no longer not set in the block configuration after deleting the block.');
$this
->drupalGet('admin/structure/block');
$this
->clickLink('Configure');
$elements = $this
->xpath('//details[@id="edit-visibility-language"]');
$this
->assertEmpty($elements);
}
public function testMultipleLanguageTypes() {
$edit = [
'language_interface[enabled][language-url]' => FALSE,
'language_interface[enabled][language-session]' => TRUE,
'language_content[configurable]' => TRUE,
'language_content[enabled][language-url]' => TRUE,
'language_content[enabled][language-interface]' => FALSE,
];
$this
->drupalGet('admin/config/regional/language/detection');
$this
->submitForm($edit, 'Save settings');
$default_theme = $this
->config('system.theme')
->get('default');
$this
->drupalGet('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme);
$this
->assertSession()
->fieldExists('visibility[language][langcodes][en]');
$this
->assertSession()
->fieldExists('visibility[language][context_mapping][language]');
$block_id = strtolower($this
->randomMachineName(8));
$edit = [
'visibility[language][context_mapping][language]' => '@language.current_language_context:language_interface',
'visibility[language][langcodes][fr]' => TRUE,
'id' => $block_id,
'region' => 'sidebar_first',
];
$this
->drupalGet('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme);
$this
->submitForm($edit, 'Save block');
$this
->drupalGet('node/1', [
'query' => [
'language' => 'en',
],
]);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextNotContains('Powered by Drupal');
$this
->drupalGet('node/1', [
'query' => [
'language' => 'fr',
],
]);
$this
->assertSession()
->pageTextContains('Powered by Drupal');
$this
->drupalLogout();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('en');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextNotContains('Powered by Drupal');
$this
->drupalGet('fr');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextNotContains('Powered by Drupal');
$edit = [
'visibility[language][context_mapping][language]' => '@language.current_language_context:language_content',
];
$this
->drupalGet('admin/structure/block/manage/' . $block_id);
$this
->submitForm($edit, 'Save block');
$this
->drupalGet('node/1', [
'query' => [
'language' => 'en',
],
]);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextNotContains('Powered by Drupal');
$this
->drupalGet('node/1', [
'query' => [
'language' => 'fr',
],
]);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextNotContains('Powered by Drupal');
$this
->drupalGet('en');
$this
->assertSession()
->pageTextNotContains('Powered by Drupal');
$this
->drupalGet('fr');
$this
->assertSession()
->pageTextContains('Powered by Drupal');
}
}