<?php
namespace Drupal\block\Tests;
use Drupal\simpletest\WebTestBase;
class BlockLanguageTest extends WebTestBase {
protected $adminUser;
public static $modules = array(
'language',
'block',
);
public static function getInfo() {
return array(
'name' => 'Language block visibility',
'description' => 'Tests if a block can be configure to be only visibile on a particular language.',
'group' => 'Block',
);
}
function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser(array(
'administer blocks',
'administer languages',
'administer site configuration',
));
$this
->drupalLogin($this->adminUser);
$edit = array(
'predefined_langcode' => 'fr',
);
$this
->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
$this
->assertText('French', 'Language added successfully.');
}
public function testLanguageBlockVisibility() {
$default_theme = 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.');
$edit = array(
'visibility[language][langcodes][en]' => TRUE,
'machine_name' => strtolower($this
->randomName(8)),
'region' => 'sidebar_first',
);
$this
->drupalPost('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme, $edit, t('Save block'));
$edit = array(
'site_default_language' => 'fr',
);
$this
->drupalpost('admin/config/regional/settings', $edit, t('Save configuration'));
drupal_static_reset('language_list');
$this
->drupalget('', array(
'language' => language_load('en'),
));
$this
->assertText('Powered by Drupal', 'The body of the custom block appears on the page.');
$this
->drupalGet('', array(
'language' => language_load('fr'),
));
$this
->assertNoText('Powered by Drupal', 'The body of the custom block does not appear on the page.');
}
public function testLanguageBlockVisibilityLanguageDelete() {
$edit = array(
'visibility' => array(
'language' => array(
'language_type' => 'language_interface',
'langcodes' => array(
'fr' => 'fr',
),
),
),
'machine_name' => 'language_block_test',
);
$block = $this
->drupalPlaceBlock('system_powered_by_block', $edit);
$visibility = $block
->get('visibility');
$language = $visibility['language']['langcodes']['fr'];
$this
->assertTrue('fr' === $language, 'Language is set in the block configuration.');
$this
->drupalPost('admin/config/regional/language/delete/fr', array(), t('Delete'));
$block = entity_load('block', $block
->id());
$visibility = $block
->get('visibility');
$this
->assertTrue(empty($visibility['language']['langcodes']['fr']), 'Language is no longer not set in the block configuration after deleting the block.');
}
}