annotate core/modules/block/tests/src/Functional/BlockLanguageTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\block\Functional;
Chris@0 4
Chris@0 5 use Drupal\Tests\BrowserTestBase;
Chris@0 6 use Drupal\block\Entity\Block;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Tests if a block can be configured to be only visible on a particular
Chris@0 10 * language.
Chris@0 11 *
Chris@0 12 * @group block
Chris@0 13 */
Chris@0 14 class BlockLanguageTest extends BrowserTestBase {
Chris@0 15
Chris@0 16 /**
Chris@0 17 * An administrative user to configure the test environment.
Chris@0 18 */
Chris@0 19 protected $adminUser;
Chris@0 20
Chris@0 21 /**
Chris@0 22 * Modules to install.
Chris@0 23 *
Chris@0 24 * @var array
Chris@0 25 */
Chris@0 26 public static $modules = ['language', 'block', 'content_translation'];
Chris@0 27
Chris@0 28 protected function setUp() {
Chris@0 29 parent::setUp();
Chris@0 30
Chris@0 31 // Create a new user, allow him to manage the blocks and the languages.
Chris@0 32 $this->adminUser = $this->drupalCreateUser(['administer blocks', 'administer languages']);
Chris@0 33 $this->drupalLogin($this->adminUser);
Chris@0 34
Chris@0 35 // Add predefined language.
Chris@0 36 $edit = [
Chris@0 37 'predefined_langcode' => 'fr',
Chris@0 38 ];
Chris@0 39 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
Chris@0 40 $this->assertText('French', 'Language added successfully.');
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * Tests the visibility settings for the blocks based on language.
Chris@0 45 */
Chris@0 46 public function testLanguageBlockVisibility() {
Chris@0 47 // Check if the visibility setting is available.
Chris@0 48 $default_theme = $this->config('system.theme')->get('default');
Chris@0 49 $this->drupalGet('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme);
Chris@0 50
Chris@0 51 $this->assertField('visibility[language][langcodes][en]', 'Language visibility field is visible.');
Chris@0 52 $this->assertNoField('visibility[language][context_mapping][language]', 'Language type field is not visible.');
Chris@0 53
Chris@0 54 // Enable a standard block and set the visibility setting for one language.
Chris@0 55 $edit = [
Chris@0 56 'visibility[language][langcodes][en]' => TRUE,
Chris@0 57 'id' => strtolower($this->randomMachineName(8)),
Chris@0 58 'region' => 'sidebar_first',
Chris@0 59 ];
Chris@0 60 $this->drupalPostForm('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme, $edit, t('Save block'));
Chris@0 61
Chris@0 62 // Change the default language.
Chris@0 63 $edit = [
Chris@0 64 'site_default_language' => 'fr',
Chris@0 65 ];
Chris@0 66 $this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration'));
Chris@0 67
Chris@0 68 // Check that a page has a block.
Chris@0 69 $this->drupalGet('en');
Chris@0 70 $this->assertText('Powered by Drupal', 'The body of the custom block appears on the page.');
Chris@0 71
Chris@0 72 // Check that a page doesn't has a block for the current language anymore.
Chris@0 73 $this->drupalGet('fr');
Chris@0 74 $this->assertNoText('Powered by Drupal', 'The body of the custom block does not appear on the page.');
Chris@0 75 }
Chris@0 76
Chris@0 77 /**
Chris@0 78 * Tests if the visibility settings are removed if the language is deleted.
Chris@0 79 */
Chris@0 80 public function testLanguageBlockVisibilityLanguageDelete() {
Chris@0 81 // Enable a standard block and set the visibility setting for one language.
Chris@0 82 $edit = [
Chris@0 83 'visibility' => [
Chris@0 84 'language' => [
Chris@0 85 'langcodes' => [
Chris@0 86 'fr' => 'fr',
Chris@0 87 ],
Chris@0 88 'context_mapping' => ['language' => '@language.current_language_context:language_interface'],
Chris@0 89 ],
Chris@0 90 ],
Chris@0 91 ];
Chris@0 92 $block = $this->drupalPlaceBlock('system_powered_by_block', $edit);
Chris@0 93
Chris@0 94 // Check that we have the language in config after saving the setting.
Chris@0 95 $visibility = $block->getVisibility();
Chris@0 96 $this->assertEqual('fr', $visibility['language']['langcodes']['fr'], 'Language is set in the block configuration.');
Chris@0 97
Chris@0 98 // Delete the language.
Chris@0 99 $this->drupalPostForm('admin/config/regional/language/delete/fr', [], t('Delete'));
Chris@0 100
Chris@0 101 // Check that the language is no longer stored in the configuration after
Chris@0 102 // it is deleted.
Chris@0 103 $block = Block::load($block->id());
Chris@0 104 $visibility = $block->getVisibility();
Chris@0 105 $this->assertTrue(empty($visibility['language']['langcodes']['fr']), 'Language is no longer not set in the block configuration after deleting the block.');
Chris@0 106
Chris@0 107 // Ensure that the block visibility for language is gone from the UI.
Chris@0 108 $this->drupalGet('admin/structure/block');
Chris@0 109 $this->clickLink('Configure');
Chris@0 110 $elements = $this->xpath('//details[@id="edit-visibility-language"]');
Chris@0 111 $this->assertTrue(empty($elements));
Chris@0 112 }
Chris@0 113
Chris@0 114 /**
Chris@0 115 * Tests block language visibility with different language types.
Chris@0 116 */
Chris@0 117 public function testMultipleLanguageTypes() {
Chris@0 118 // Customize content language detection to be different from interface
Chris@0 119 // language detection.
Chris@0 120 $edit = [
Chris@0 121 // Interface language detection: only using session.
Chris@0 122 'language_interface[enabled][language-url]' => FALSE,
Chris@0 123 'language_interface[enabled][language-session]' => TRUE,
Chris@0 124 // Content language detection: only using URL.
Chris@0 125 'language_content[configurable]' => TRUE,
Chris@0 126 'language_content[enabled][language-url]' => TRUE,
Chris@0 127 'language_content[enabled][language-interface]' => FALSE,
Chris@0 128 ];
Chris@0 129 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
Chris@0 130
Chris@0 131 // Check if the visibility setting is available with a type setting.
Chris@0 132 $default_theme = $this->config('system.theme')->get('default');
Chris@0 133 $this->drupalGet('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme);
Chris@0 134 $this->assertField('visibility[language][langcodes][en]', 'Language visibility field is visible.');
Chris@0 135 $this->assertField('visibility[language][context_mapping][language]', 'Language type field is visible.');
Chris@0 136
Chris@0 137 // Enable a standard block and set visibility to French only.
Chris@0 138 $block_id = strtolower($this->randomMachineName(8));
Chris@0 139 $edit = [
Chris@0 140 'visibility[language][context_mapping][language]' => '@language.current_language_context:language_interface',
Chris@0 141 'visibility[language][langcodes][fr]' => TRUE,
Chris@0 142 'id' => $block_id,
Chris@0 143 'region' => 'sidebar_first',
Chris@0 144 ];
Chris@0 145 $this->drupalPostForm('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme, $edit, t('Save block'));
Chris@0 146
Chris@0 147 // Interface negotiation depends on request arguments.
Chris@0 148 $this->drupalGet('node', ['query' => ['language' => 'en']]);
Chris@0 149 $this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
Chris@0 150 $this->drupalGet('node', ['query' => ['language' => 'fr']]);
Chris@0 151 $this->assertText('Powered by Drupal', 'The body of the block appears on the page.');
Chris@0 152
Chris@0 153 // Log in again in order to clear the interface language stored in the
Chris@0 154 // session.
Chris@0 155 $this->drupalLogout();
Chris@0 156 $this->drupalLogin($this->adminUser);
Chris@0 157
Chris@0 158 // Content language does not depend on session/request arguments.
Chris@0 159 // It will fall back on English (site default) and not display the block.
Chris@0 160 $this->drupalGet('en');
Chris@0 161 $this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
Chris@0 162 $this->drupalGet('fr');
Chris@0 163 $this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
Chris@0 164
Chris@0 165 // Change visibility to now depend on content language for this block.
Chris@0 166 $edit = [
Chris@17 167 'visibility[language][context_mapping][language]' => '@language.current_language_context:language_content',
Chris@0 168 ];
Chris@0 169 $this->drupalPostForm('admin/structure/block/manage/' . $block_id, $edit, t('Save block'));
Chris@0 170
Chris@0 171 // Content language negotiation does not depend on request arguments.
Chris@0 172 // It will fall back on English (site default) and not display the block.
Chris@0 173 $this->drupalGet('node', ['query' => ['language' => 'en']]);
Chris@0 174 $this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
Chris@0 175 $this->drupalGet('node', ['query' => ['language' => 'fr']]);
Chris@0 176 $this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
Chris@0 177
Chris@0 178 // Content language negotiation depends on path prefix.
Chris@0 179 $this->drupalGet('en');
Chris@0 180 $this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.');
Chris@0 181 $this->drupalGet('fr');
Chris@0 182 $this->assertText('Powered by Drupal', 'The body of the block appears on the page.');
Chris@0 183 }
Chris@0 184
Chris@0 185 }