Chris@0: adminUser = $this->drupalCreateUser(['administer blocks', 'administer languages']); Chris@0: $this->drupalLogin($this->adminUser); Chris@0: Chris@0: // Add predefined language. Chris@0: $edit = [ Chris@0: 'predefined_langcode' => 'fr', Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language')); Chris@0: $this->assertText('French', 'Language added successfully.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the visibility settings for the blocks based on language. Chris@0: */ Chris@0: public function testLanguageBlockVisibility() { Chris@0: // Check if the visibility setting is available. Chris@0: $default_theme = $this->config('system.theme')->get('default'); Chris@0: $this->drupalGet('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme); Chris@0: Chris@0: $this->assertField('visibility[language][langcodes][en]', 'Language visibility field is visible.'); Chris@0: $this->assertNoField('visibility[language][context_mapping][language]', 'Language type field is not visible.'); Chris@0: Chris@0: // Enable a standard block and set the visibility setting for one language. Chris@0: $edit = [ Chris@0: 'visibility[language][langcodes][en]' => TRUE, Chris@0: 'id' => strtolower($this->randomMachineName(8)), Chris@0: 'region' => 'sidebar_first', Chris@0: ]; Chris@0: $this->drupalPostForm('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme, $edit, t('Save block')); Chris@0: Chris@0: // Change the default language. Chris@0: $edit = [ Chris@0: 'site_default_language' => 'fr', Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration')); Chris@0: Chris@0: // Check that a page has a block. Chris@0: $this->drupalGet('en'); Chris@0: $this->assertText('Powered by Drupal', 'The body of the custom block appears on the page.'); Chris@0: Chris@0: // Check that a page doesn't has a block for the current language anymore. Chris@0: $this->drupalGet('fr'); Chris@0: $this->assertNoText('Powered by Drupal', 'The body of the custom block does not appear on the page.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests if the visibility settings are removed if the language is deleted. Chris@0: */ Chris@0: public function testLanguageBlockVisibilityLanguageDelete() { Chris@0: // Enable a standard block and set the visibility setting for one language. Chris@0: $edit = [ Chris@0: 'visibility' => [ Chris@0: 'language' => [ Chris@0: 'langcodes' => [ Chris@0: 'fr' => 'fr', Chris@0: ], Chris@0: 'context_mapping' => ['language' => '@language.current_language_context:language_interface'], Chris@0: ], Chris@0: ], Chris@0: ]; Chris@0: $block = $this->drupalPlaceBlock('system_powered_by_block', $edit); Chris@0: Chris@0: // Check that we have the language in config after saving the setting. Chris@0: $visibility = $block->getVisibility(); Chris@0: $this->assertEqual('fr', $visibility['language']['langcodes']['fr'], 'Language is set in the block configuration.'); Chris@0: Chris@0: // Delete the language. Chris@0: $this->drupalPostForm('admin/config/regional/language/delete/fr', [], t('Delete')); Chris@0: Chris@0: // Check that the language is no longer stored in the configuration after Chris@0: // it is deleted. Chris@0: $block = Block::load($block->id()); Chris@0: $visibility = $block->getVisibility(); Chris@0: $this->assertTrue(empty($visibility['language']['langcodes']['fr']), 'Language is no longer not set in the block configuration after deleting the block.'); Chris@0: Chris@0: // Ensure that the block visibility for language is gone from the UI. Chris@0: $this->drupalGet('admin/structure/block'); Chris@0: $this->clickLink('Configure'); Chris@0: $elements = $this->xpath('//details[@id="edit-visibility-language"]'); Chris@0: $this->assertTrue(empty($elements)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests block language visibility with different language types. Chris@0: */ Chris@0: public function testMultipleLanguageTypes() { Chris@0: // Customize content language detection to be different from interface Chris@0: // language detection. Chris@0: $edit = [ Chris@0: // Interface language detection: only using session. Chris@0: 'language_interface[enabled][language-url]' => FALSE, Chris@0: 'language_interface[enabled][language-session]' => TRUE, Chris@0: // Content language detection: only using URL. Chris@0: 'language_content[configurable]' => TRUE, Chris@0: 'language_content[enabled][language-url]' => TRUE, Chris@0: 'language_content[enabled][language-interface]' => FALSE, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings')); Chris@0: Chris@0: // Check if the visibility setting is available with a type setting. Chris@0: $default_theme = $this->config('system.theme')->get('default'); Chris@0: $this->drupalGet('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme); Chris@0: $this->assertField('visibility[language][langcodes][en]', 'Language visibility field is visible.'); Chris@0: $this->assertField('visibility[language][context_mapping][language]', 'Language type field is visible.'); Chris@0: Chris@0: // Enable a standard block and set visibility to French only. Chris@0: $block_id = strtolower($this->randomMachineName(8)); Chris@0: $edit = [ Chris@0: 'visibility[language][context_mapping][language]' => '@language.current_language_context:language_interface', Chris@0: 'visibility[language][langcodes][fr]' => TRUE, Chris@0: 'id' => $block_id, Chris@0: 'region' => 'sidebar_first', Chris@0: ]; Chris@0: $this->drupalPostForm('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme, $edit, t('Save block')); Chris@0: Chris@0: // Interface negotiation depends on request arguments. Chris@0: $this->drupalGet('node', ['query' => ['language' => 'en']]); Chris@0: $this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.'); Chris@0: $this->drupalGet('node', ['query' => ['language' => 'fr']]); Chris@0: $this->assertText('Powered by Drupal', 'The body of the block appears on the page.'); Chris@0: Chris@0: // Log in again in order to clear the interface language stored in the Chris@0: // session. Chris@0: $this->drupalLogout(); Chris@0: $this->drupalLogin($this->adminUser); Chris@0: Chris@0: // Content language does not depend on session/request arguments. Chris@0: // It will fall back on English (site default) and not display the block. Chris@0: $this->drupalGet('en'); Chris@0: $this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.'); Chris@0: $this->drupalGet('fr'); Chris@0: $this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.'); Chris@0: Chris@0: // Change visibility to now depend on content language for this block. Chris@0: $edit = [ Chris@17: 'visibility[language][context_mapping][language]' => '@language.current_language_context:language_content', Chris@0: ]; Chris@0: $this->drupalPostForm('admin/structure/block/manage/' . $block_id, $edit, t('Save block')); Chris@0: Chris@0: // Content language negotiation does not depend on request arguments. Chris@0: // It will fall back on English (site default) and not display the block. Chris@0: $this->drupalGet('node', ['query' => ['language' => 'en']]); Chris@0: $this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.'); Chris@0: $this->drupalGet('node', ['query' => ['language' => 'fr']]); Chris@0: $this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.'); Chris@0: Chris@0: // Content language negotiation depends on path prefix. Chris@0: $this->drupalGet('en'); Chris@0: $this->assertNoText('Powered by Drupal', 'The body of the block does not appear on the page.'); Chris@0: $this->drupalGet('fr'); Chris@0: $this->assertText('Powered by Drupal', 'The body of the block appears on the page.'); Chris@0: } Chris@0: Chris@0: }