BlockLanguageTest.php 3.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php

/**
 * @file
 * Definition of Drupal\block\Tests\BlockLanguageTest.
 */

namespace Drupal\block\Tests;

use Drupal\simpletest\WebTestBase;

/**
 * Functional tests for the language list configuration forms.
 */
class BlockLanguageTest extends WebTestBase {
16

17 18 19 20 21
  /**
   * An administrative user to configure the test environment.
   */
  protected $adminUser;

22 23 24 25 26 27 28
  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('language', 'block');

29 30 31 32 33 34 35 36
  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',
    );
  }

37 38 39
  function setUp() {
    parent::setUp();

40
    // Create a new user, allow him to manage the blocks and the languages.
41
    $this->adminUser = $this->drupalCreateUser(array('administer blocks', 'administer languages', 'administer site configuration'));
42
    $this->drupalLogin($this->adminUser);
43 44 45 46 47

    // Add predefined language.
    $edit = array(
      'predefined_langcode' => 'fr',
    );
48
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
49
    $this->assertText('French', 'Language added successfully.');
50
  }
51

52 53 54 55
  /**
   * Tests the visibility settings for the blocks based on language.
   */
  public function testLanguageBlockVisibility() {
56
    // Check if the visibility setting is available.
57
    $default_theme = \Drupal::config('system.theme')->get('default');
58
    $this->drupalGet('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme);
59 60

    $this->assertField('visibility[language][langcodes][en]', 'Language visibility field is visible.');
61

62
    // Enable a standard block and set the visibility setting for one language.
63
    $edit = array(
64
      'visibility[language][langcodes][en]' => TRUE,
65
      'id' => strtolower($this->randomName(8)),
66
      'region' => 'sidebar_first',
67
    );
68
    $this->drupalPostForm('admin/structure/block/add/system_powered_by_block' . '/' . $default_theme, $edit, t('Save block'));
69 70 71

    // Change the default language.
    $edit = array(
72
      'site_default_language' => 'fr',
73
    );
74
    $this->drupalPostForm('admin/config/regional/settings', $edit, t('Save configuration'));
75

76
    // Check that a page has a block.
77
    $this->drupalGet('en');
78
    $this->assertText('Powered by Drupal', 'The body of the custom block appears on the page.');
79

80
    // Check that a page doesn't has a block for the current language anymore.
81
    $this->drupalGet('fr');
82
    $this->assertNoText('Powered by Drupal', 'The body of the custom block does not appear on the page.');
83 84 85 86 87 88
  }

  /**
   * Tests if the visibility settings are removed if the language is deleted.
   */
  public function testLanguageBlockVisibilityLanguageDelete() {
89
    // Enable a standard block and set the visibility setting for one language.
90
    $edit = array(
91 92 93 94 95 96 97 98
      'visibility' => array(
        'language' => array(
          'language_type' => 'language_interface',
          'langcodes' => array(
            'fr' => 'fr',
          ),
        ),
      ),
99
    );
100
    $block = $this->drupalPlaceBlock('system_powered_by_block', $edit);
101

102
    // Check that we have the language in config after saving the setting.
103
    $visibility = $block->get('visibility');
104 105
    $language = $visibility['language']['langcodes']['fr'];
    $this->assertTrue('fr' === $language, 'Language is set in the block configuration.');
106 107

    // Delete the language.
108
    $this->drupalPostForm('admin/config/regional/language/delete/fr', array(), t('Delete'));
109

110 111
    // Check that the language is no longer stored in the configuration after
    // it is deleted.
112 113 114
    $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.');
115 116 117
  }

}