View source
<?php
namespace Drupal\Tests\block\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\block\Entity\Block;
class BlockLanguageTest extends BrowserTestBase {
protected $adminUser;
public static $modules = [
'language',
'block',
'content_translation',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer blocks',
'administer languages',
]);
$this
->drupalLogin($this->adminUser);
$edit = [
'predefined_langcode' => 'fr',
];
$this
->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
$this
->assertText('French', 'Language added successfully.');
}
public function testLanguageBlockVisibility() {
$default_theme = $this
->config('system.theme')
->get('default');
$this
->drupalGet('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme);
$this
->assertField('visibility[language][langcodes][en]', 'Language visibility field is visible.');
$this
->assertNoField('visibility[language][context_mapping][language]', 'Language type field is not visible.');
$edit = [
'visibility[language][langcodes][en]' => TRUE,
'id' => strtolower($this
->randomMachineName(8)),
'region' => 'sidebar_first',
];
$this
->drupalPostForm('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme, $edit, t('Save block'));
$edit = [
'site_default_language' => 'fr',
];
$this
->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration'));
$this
->drupalGet('en');
$this
->assertText('Powered by Drupal', 'The body of the custom block appears on the page.');
$this
->drupalGet('fr');
$this
->assertNoText('Powered by Drupal', 'The body of the custom block does not appear on the page.');
}
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
->assertEqual('fr', $visibility['language']['langcodes']['fr'], 'Language is set in the block configuration.');
$this
->drupalPostForm('admin/config/regional/language/delete/fr', [], t('Delete'));
$block = Block::load($block
->id());
$visibility = $block
->getVisibility();
$this
->assertTrue(empty($visibility['language']['langcodes']['fr']), '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
->assertTrue(empty($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
->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
$default_theme = $this
->config('system.theme')
->get('default');
$this
->drupalGet('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme);
$this
->assertField('visibility[language][langcodes][en]', 'Language visibility field is visible.');
$this
->assertField('visibility[language][context_mapping][language]', 'Language type field is visible.');
$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
->drupalPostForm('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme, $edit, t('Save block'));
$this
->drupalGet('node', [
'query' => [
'language' => 'en',
],
]);
$this
->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
$this
->drupalGet('node', [
'query' => [
'language' => 'fr',
],
]);
$this
->assertText('Powered by Drupal', 'The body of the block appears on the page.');
$this
->drupalLogout();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('en');
$this
->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
$this
->drupalGet('fr');
$this
->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
$edit = [
'visibility[language][context_mapping][language]' => '@language.current_language_context:language_content',
];
$this
->drupalPostForm('admin/structure/block/manage/' . $block_id, $edit, t('Save block'));
$this
->drupalGet('node', [
'query' => [
'language' => 'en',
],
]);
$this
->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
$this
->drupalGet('node', [
'query' => [
'language' => 'fr',
],
]);
$this
->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
$this
->drupalGet('en');
$this
->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
$this
->drupalGet('fr');
$this
->assertText('Powered by Drupal', 'The body of the block appears on the page.');
}
}