function BlockLanguageTest::testMultipleLanguageTypes

Same name and namespace in other branches
  1. 11.x core/modules/block/tests/src/Functional/BlockLanguageTest.php \Drupal\Tests\block\Functional\BlockLanguageTest::testMultipleLanguageTypes()
  2. 10 core/modules/block/tests/src/Functional/BlockLanguageTest.php \Drupal\Tests\block\Functional\BlockLanguageTest::testMultipleLanguageTypes()
  3. 9 core/modules/block/tests/src/Functional/BlockLanguageTest.php \Drupal\Tests\block\Functional\BlockLanguageTest::testMultipleLanguageTypes()

Tests block language visibility with different language types.

File

core/modules/block/tests/src/Functional/BlockLanguageTest.php, line 124

Class

BlockLanguageTest
Tests if a block can be configured to be only visible on a particular language.

Namespace

Drupal\Tests\block\Functional

Code

public function testMultipleLanguageTypes() {
  // Customize content language detection to be different from interface
  // language detection.
  $edit = [
    // Interface language detection: only using session.
'language_interface[enabled][language-url]' => FALSE,
    'language_interface[enabled][language-session]' => TRUE,
    // Content language detection: only using URL.
'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'));
  // Check if the visibility setting is available with a type setting.
  $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.');
  // Enable a standard block and set visibility to French only.
  $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'));
  // Interface negotiation depends on request arguments.
  $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.');
  // Log in again in order to clear the interface language stored in the
  // session.
  $this->drupalLogout();
  $this->drupalLogin($this->adminUser);
  // Content language does not depend on session/request arguments.
  // It will fall back on English (site default) and not display the block.
  $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.');
  // Change visibility to now depend on content language for this block.
  $edit = [
    'visibility[language][context_mapping][language]' => '@language.current_language_context:language_content',
  ];
  $this->drupalPostForm('admin/structure/block/manage/' . $block_id, $edit, t('Save block'));
  // Content language negotiation does not depend on request arguments.
  // It will fall back on English (site default) and not display the 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.');
  // Content language negotiation depends on path prefix.
  $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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.