ConfigLanguageOverride.php 2.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
<?php

/**
 * @file
 * Contains \Drupal\config\Tests\ConfigLanguageOverride.
 */

namespace Drupal\config\Tests;

use Drupal\Core\Language\Language;
use Drupal\simpletest\DrupalUnitTestBase;

/**
 * Tests language config override.
 */
class ConfigLanguageOverride extends DrupalUnitTestBase {

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

  public static function getInfo() {
    return array(
      'name' => 'Language override',
      'description' => 'Confirm that language overrides work',
      'group' => 'Configuration',
    );
  }

  public function setUp() {
    parent::setUp();
35
    $this->installConfig(array('config_test'));
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    \Drupal::configFactory()->setLanguage(language_default());
  }

  /**
   * Tests locale override based on language.
   */
  function testConfigLanguageOverride() {
    $config = \Drupal::config('config_test.system');
    $this->assertIdentical($config->get('foo'), 'en bar');

    language_save(new Language(array(
      'name' => 'French',
      'id' => 'fr',
    )));
    language_save(new Language(array(
      'name' => 'German',
      'id' => 'de',
    )));

    \Drupal::configFactory()->setLanguage(language_load('fr'));
    $config = \Drupal::config('config_test.system');
    $this->assertIdentical($config->get('foo'), 'fr bar');

    \Drupal::configFactory()->setLanguage(language_load('de'));
    $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.
    $language_config_name = \Drupal::configFactory()->getLanguageConfigName('de', 'config_test.new');
    \Drupal::config($language_config_name)->set('language', 'override')->save();
    \Drupal::config('config_test.new');
    $config = \Drupal::config('config_test.new');
    $this->assertTrue($config->isNew(), 'The configuration object config_test.new is new');
    $this->assertIdentical($config->get('language'), 'override');
    \Drupal::configFactory()->disableOverrides();
    $config = \Drupal::config('config_test.new');
    $this->assertIdentical($config->get('language'), NULL);
    \Drupal::configFactory()->enableOverrides();

    // Ensure that language configuration overrides can not be overridden.
    global $conf;
    $conf[$language_config_name]['language'] = 'conf cannot override';
    $this->assertIdentical(\Drupal::config($language_config_name)->get('language'), 'override');
  }
}