ConfigLanguageOverrideTest.php 5.09 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\config\Tests\ConfigLanguageOverrideTest.
6 7 8 9
 */

namespace Drupal\config\Tests;

10
use Drupal\language\Entity\ConfigurableLanguage;
11 12 13
use Drupal\simpletest\DrupalUnitTestBase;

/**
14 15 16
 * Confirm that language overrides work.
 *
 * @group config
17
 */
18
class ConfigLanguageOverrideTest extends DrupalUnitTestBase {
19 20 21 22 23 24

  /**
   * Modules to enable.
   *
   * @var array
   */
25
  public static $modules = array('user', 'language', 'config_test',  'system', 'field');
26

27
  protected function setUp() {
28
    parent::setUp();
29
    $this->installConfig(array('config_test'));
30 31 32 33 34 35
  }

  /**
   * Tests locale override based on language.
   */
  function testConfigLanguageOverride() {
36 37
    // The language module implements a config factory override object that
    // overrides configuration when the Language module is enabled. This test ensures that
38
    // English overrides work.
39
    \Drupal::languageManager()->setConfigOverrideLanguage(language_load('en'));
40 41 42
    $config = \Drupal::config('config_test.system');
    $this->assertIdentical($config->get('foo'), 'en bar');

43 44 45 46
    // Ensure that the raw data is not translated.
    $raw = $config->getRawData();
    $this->assertIdentical($raw['foo'], 'bar');

47 48
    ConfigurableLanguage::createFromLangcode('fr')->save();
    ConfigurableLanguage::createFromLangcode('de')->save();
49

50
    \Drupal::languageManager()->setConfigOverrideLanguage(language_load('fr'));
51 52 53
    $config = \Drupal::config('config_test.system');
    $this->assertIdentical($config->get('foo'), 'fr bar');

54
    \Drupal::languageManager()->setConfigOverrideLanguage(language_load('de'));
55 56 57 58 59 60
    $config = \Drupal::config('config_test.system');
    $this->assertIdentical($config->get('foo'), 'de bar');

    // Test overrides of completely new configuration objects. In normal runtime
    // this should only happen for configuration entities as we should not be
    // creating simple configuration objects on the fly.
61 62 63 64
    \Drupal::languageManager()
      ->getLanguageConfigOverride('de', 'config_test.new')
      ->set('language', 'override')
      ->save();
65 66 67
    $config = \Drupal::config('config_test.new');
    $this->assertTrue($config->isNew(), 'The configuration object config_test.new is new');
    $this->assertIdentical($config->get('language'), 'override');
68 69
    $old_state = \Drupal::configFactory()->getOverrideState();
    \Drupal::configFactory()->setOverrideState(FALSE);
70 71
    $config = \Drupal::config('config_test.new');
    $this->assertIdentical($config->get('language'), NULL);
72
    \Drupal::configFactory()->setOverrideState($old_state);
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

    // Test how overrides react to base configuration changes. Set up some base
    // values.
    \Drupal::config('config_test.foo')
      ->set('value', array('key' => 'original'))
      ->set('label', 'Original')
      ->save();
    \Drupal::languageManager()
      ->getLanguageConfigOverride('de', 'config_test.foo')
      ->set('value', array('key' => 'override'))
      ->set('label', 'Override')
      ->save();
    \Drupal::languageManager()
      ->getLanguageConfigOverride('fr', 'config_test.foo')
      ->set('value', array('key' => 'override'))
      ->save();
    \Drupal::configFactory()->clearStaticCache();
    $config = \Drupal::config('config_test.foo');
    $this->assertIdentical($config->get('value'), array('key' => 'override'));

    // Ensure renaming the config will rename the override.
    \Drupal::configFactory()->rename('config_test.foo', 'config_test.bar');
    $config = \Drupal::config('config_test.bar');
    $this->assertEqual($config->get('value'), array('key' => 'original'));
    $override = \Drupal::languageManager()->getLanguageConfigOverride('de', 'config_test.foo');
    $this->assertTrue($override->isNew());
    $this->assertEqual($override->get('value'), NULL);
    $override = \Drupal::languageManager()->getLanguageConfigOverride('de', 'config_test.bar');
    $this->assertFalse($override->isNew());
    $this->assertEqual($override->get('value'), array('key' => 'override'));
    $override = \Drupal::languageManager()->getLanguageConfigOverride('fr', 'config_test.bar');
    $this->assertFalse($override->isNew());
    $this->assertEqual($override->get('value'), array('key' => 'override'));

    // Ensure changing data in the config will update the overrides.
    $config = \Drupal::config('config_test.bar')->clear('value.key')->save();
    $this->assertEqual($config->get('value'), array());
    $override = \Drupal::languageManager()->getLanguageConfigOverride('de', 'config_test.bar');
    $this->assertFalse($override->isNew());
    $this->assertEqual($override->get('value'), NULL);
    // The French override will become empty and therefore removed.
    $override = \Drupal::languageManager()->getLanguageConfigOverride('fr', 'config_test.bar');
    $this->assertTrue($override->isNew());
    $this->assertEqual($override->get('value'), NULL);

    // Ensure deleting the config will delete the override.
    \Drupal::configFactory()->get('config_test.bar')->delete();
    $override = \Drupal::languageManager()->getLanguageConfigOverride('de', 'config_test.bar');
    $this->assertTrue($override->isNew());
    $this->assertEqual($override->get('value'), NULL);
123 124 125
  }
}