comparison core/modules/block/tests/src/Functional/BlockLanguageTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\block\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\block\Entity\Block;
7
8 /**
9 * Tests if a block can be configured to be only visible on a particular
10 * language.
11 *
12 * @group block
13 */
14 class BlockLanguageTest extends BrowserTestBase {
15
16 /**
17 * An administrative user to configure the test environment.
18 */
19 protected $adminUser;
20
21 /**
22 * Modules to install.
23 *
24 * @var array
25 */
26 public static $modules = ['language', 'block', 'content_translation'];
27
28 protected function setUp() {
29 parent::setUp();
30
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);
34
35 // Add predefined language.
36 $edit = [
37 'predefined_langcode' => 'fr',
38 ];
39 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
40 $this->assertText('French', 'Language added successfully.');
41 }
42
43 /**
44 * Tests the visibility settings for the blocks based on language.
45 */
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);
50
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.');
53
54 // Enable a standard block and set the visibility setting for one language.
55 $edit = [
56 'visibility[language][langcodes][en]' => TRUE,
57 'id' => strtolower($this->randomMachineName(8)),
58 'region' => 'sidebar_first',
59 ];
60 $this->drupalPostForm('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme, $edit, t('Save block'));
61
62 // Change the default language.
63 $edit = [
64 'site_default_language' => 'fr',
65 ];
66 $this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration'));
67
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.');
71
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.');
75 }
76
77 /**
78 * Tests if the visibility settings are removed if the language is deleted.
79 */
80 public function testLanguageBlockVisibilityLanguageDelete() {
81 // Enable a standard block and set the visibility setting for one language.
82 $edit = [
83 'visibility' => [
84 'language' => [
85 'langcodes' => [
86 'fr' => 'fr',
87 ],
88 'context_mapping' => ['language' => '@language.current_language_context:language_interface'],
89 ],
90 ],
91 ];
92 $block = $this->drupalPlaceBlock('system_powered_by_block', $edit);
93
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.');
97
98 // Delete the language.
99 $this->drupalPostForm('admin/config/regional/language/delete/fr', [], t('Delete'));
100
101 // Check that the language is no longer stored in the configuration after
102 // it is deleted.
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.');
106
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));
112 }
113
114 /**
115 * Tests block language visibility with different language types.
116 */
117 public function testMultipleLanguageTypes() {
118 // Customize content language detection to be different from interface
119 // language detection.
120 $edit = [
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,
128 ];
129 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
130
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.');
136
137 // Enable a standard block and set visibility to French only.
138 $block_id = strtolower($this->randomMachineName(8));
139 $edit = [
140 'visibility[language][context_mapping][language]' => '@language.current_language_context:language_interface',
141 'visibility[language][langcodes][fr]' => TRUE,
142 'id' => $block_id,
143 'region' => 'sidebar_first',
144 ];
145 $this->drupalPostForm('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme, $edit, t('Save block'));
146
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.');
152
153 // Log in again in order to clear the interface language stored in the
154 // session.
155 $this->drupalLogout();
156 $this->drupalLogin($this->adminUser);
157
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.');
164
165 // Change visibility to now depend on content language for this block.
166 $edit = [
167 'visibility[language][context_mapping][language]' => '@language.current_language_context:language_content'
168 ];
169 $this->drupalPostForm('admin/structure/block/manage/' . $block_id, $edit, t('Save block'));
170
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.');
177
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.');
183 }
184
185 }