3 namespace Drupal\Tests\block\Functional;
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\block\Entity\Block;
9 * Tests if a block can be configured to be only visible on a particular
14 class BlockLanguageTest extends BrowserTestBase {
17 * An administrative user to configure the test environment.
26 public static $modules = ['language', 'block', 'content_translation'];
28 protected function setUp() {
31 // Create a new user, allow him to manage the blocks and the languages.
32 $this->adminUser = $this->drupalCreateUser(['administer blocks', 'administer languages']);
33 $this->drupalLogin($this->adminUser);
35 // Add predefined language.
37 'predefined_langcode' => 'fr',
39 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
40 $this->assertText('French', 'Language added successfully.');
44 * Tests the visibility settings for the blocks based on language.
46 public function testLanguageBlockVisibility() {
47 // Check if the visibility setting is available.
48 $default_theme = $this->config('system.theme')->get('default');
49 $this->drupalGet('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme);
51 $this->assertField('visibility[language][langcodes][en]', 'Language visibility field is visible.');
52 $this->assertNoField('visibility[language][context_mapping][language]', 'Language type field is not visible.');
54 // Enable a standard block and set the visibility setting for one language.
56 'visibility[language][langcodes][en]' => TRUE,
57 'id' => strtolower($this->randomMachineName(8)),
58 'region' => 'sidebar_first',
60 $this->drupalPostForm('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme, $edit, t('Save block'));
62 // Change the default language.
64 'site_default_language' => 'fr',
66 $this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration'));
68 // Check that a page has a block.
69 $this->drupalGet('en');
70 $this->assertText('Powered by Drupal', 'The body of the custom block appears on the page.');
72 // Check that a page doesn't has a block for the current language anymore.
73 $this->drupalGet('fr');
74 $this->assertNoText('Powered by Drupal', 'The body of the custom block does not appear on the page.');
78 * Tests if the visibility settings are removed if the language is deleted.
80 public function testLanguageBlockVisibilityLanguageDelete() {
81 // Enable a standard block and set the visibility setting for one language.
88 'context_mapping' => ['language' => '@language.current_language_context:language_interface'],
92 $block = $this->drupalPlaceBlock('system_powered_by_block', $edit);
94 // Check that we have the language in config after saving the setting.
95 $visibility = $block->getVisibility();
96 $this->assertEqual('fr', $visibility['language']['langcodes']['fr'], 'Language is set in the block configuration.');
98 // Delete the language.
99 $this->drupalPostForm('admin/config/regional/language/delete/fr', [], t('Delete'));
101 // Check that the language is no longer stored in the configuration after
103 $block = Block::load($block->id());
104 $visibility = $block->getVisibility();
105 $this->assertTrue(empty($visibility['language']['langcodes']['fr']), 'Language is no longer not set in the block configuration after deleting the block.');
107 // Ensure that the block visibility for language is gone from the UI.
108 $this->drupalGet('admin/structure/block');
109 $this->clickLink('Configure');
110 $elements = $this->xpath('//details[@id="edit-visibility-language"]');
111 $this->assertTrue(empty($elements));
115 * Tests block language visibility with different language types.
117 public function testMultipleLanguageTypes() {
118 // Customize content language detection to be different from interface
119 // language detection.
121 // Interface language detection: only using session.
122 'language_interface[enabled][language-url]' => FALSE,
123 'language_interface[enabled][language-session]' => TRUE,
124 // Content language detection: only using URL.
125 'language_content[configurable]' => TRUE,
126 'language_content[enabled][language-url]' => TRUE,
127 'language_content[enabled][language-interface]' => FALSE,
129 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
131 // Check if the visibility setting is available with a type setting.
132 $default_theme = $this->config('system.theme')->get('default');
133 $this->drupalGet('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme);
134 $this->assertField('visibility[language][langcodes][en]', 'Language visibility field is visible.');
135 $this->assertField('visibility[language][context_mapping][language]', 'Language type field is visible.');
137 // Enable a standard block and set visibility to French only.
138 $block_id = strtolower($this->randomMachineName(8));
140 'visibility[language][context_mapping][language]' => '@language.current_language_context:language_interface',
141 'visibility[language][langcodes][fr]' => TRUE,
143 'region' => 'sidebar_first',
145 $this->drupalPostForm('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme, $edit, t('Save block'));
147 // Interface negotiation depends on request arguments.
148 $this->drupalGet('node', ['query' => ['language' => 'en']]);
149 $this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
150 $this->drupalGet('node', ['query' => ['language' => 'fr']]);
151 $this->assertText('Powered by Drupal', 'The body of the block appears on the page.');
153 // Log in again in order to clear the interface language stored in the
155 $this->drupalLogout();
156 $this->drupalLogin($this->adminUser);
158 // Content language does not depend on session/request arguments.
159 // It will fall back on English (site default) and not display the block.
160 $this->drupalGet('en');
161 $this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
162 $this->drupalGet('fr');
163 $this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
165 // Change visibility to now depend on content language for this block.
167 'visibility[language][context_mapping][language]' => '@language.current_language_context:language_content'
169 $this->drupalPostForm('admin/structure/block/manage/' . $block_id, $edit, t('Save block'));
171 // Content language negotiation does not depend on request arguments.
172 // It will fall back on English (site default) and not display the block.
173 $this->drupalGet('node', ['query' => ['language' => 'en']]);
174 $this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
175 $this->drupalGet('node', ['query' => ['language' => 'fr']]);
176 $this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
178 // Content language negotiation depends on path prefix.
179 $this->drupalGet('en');
180 $this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
181 $this->drupalGet('fr');
182 $this->assertText('Powered by Drupal', 'The body of the block appears on the page.');