ConfigOverridesPriorityTest.php 4.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Definition of Drupal\config\Tests\ConfigOverridesPriorityTest.
 */

namespace Drupal\config\Tests;

use Drupal\Core\Language\Language;
11
use Drupal\simpletest\KernelTestBase;
12 13

/**
14 15 16 17
 * Tests that language, module and settings.php are applied in the correct
 * order.
 *
 * @group config
18
 */
19
class ConfigOverridesPriorityTest extends KernelTestBase {
20

21
  public static $modules = array('system', 'config', 'config_override_test', 'language');
22 23 24 25 26 27 28 29 30 31 32 33 34 35

  public function testOverridePriorities() {
    $GLOBALS['config_test_run_module_overrides'] = FALSE;

    $non_overridden_mail =  'site@example.com';
    $language_overridden_mail = 'french@example.com';

    $language_overridden_name = 'French site name';
    $module_overridden_name = 'ZOMG overridden site name';
    $non_overridden_name = 'ZOMG this name is on disk mkay';

    $module_overridden_slogan = 'Yay for overrides!';
    $non_overridden_slogan = 'Yay for defaults!';

36
    /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    $config_factory = $this->container->get('config.factory');
    $config_factory
      ->get('system.site')
      ->set('name', $non_overridden_name)
      ->set('slogan', $non_overridden_slogan)
      ->set('mail', $non_overridden_mail)
      ->set('weight_select_max', 50)
      ->save();

    // Ensure that no overrides are applying.
    $this->assertEqual($non_overridden_name, $config_factory->get('system.site')->get('name'));
    $this->assertEqual($non_overridden_slogan, $config_factory->get('system.site')->get('slogan'));
    $this->assertEqual($non_overridden_mail, $config_factory->get('system.site')->get('mail'));
    $this->assertEqual(50, $config_factory->get('system.site')->get('weight_select_max'));

    // Override using language.
    $language = new Language(array(
      'name' => 'French',
      'id' => 'fr',
    ));
57 58
    \Drupal::languageManager()->setConfigOverrideLanguage($language);
    \Drupal::languageManager()
59
      ->getLanguageConfigOverride($language->getId(), 'system.site')
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
      ->set('name', $language_overridden_name)
      ->set('mail', $language_overridden_mail)
      ->save();

    $this->assertEqual($language_overridden_name, $config_factory->get('system.site')->get('name'));
    $this->assertEqual($non_overridden_slogan, $config_factory->get('system.site')->get('slogan'));
    $this->assertEqual($language_overridden_mail, $config_factory->get('system.site')->get('mail'));
    $this->assertEqual(50, $config_factory->get('system.site')->get('weight_select_max'));

    // Enable module overrides. Do not override system.site:mail to prove that
    // the language override still applies.
    $GLOBALS['config_test_run_module_overrides'] = TRUE;
    $config_factory->reset('system.site');
    $this->assertEqual($module_overridden_name, $config_factory->get('system.site')->get('name'));
    $this->assertEqual($module_overridden_slogan, $config_factory->get('system.site')->get('slogan'));
    $this->assertEqual($language_overridden_mail, $config_factory->get('system.site')->get('mail'));
    $this->assertEqual(50, $config_factory->get('system.site')->get('weight_select_max'));

    // Configure a global override to simulate overriding using settings.php. Do
    // not override system.site:mail or system.site:slogan to prove that the
    // language and module overrides still apply.
81
    $GLOBALS['config']['system.site']['name'] = 'Site name global conf override';
82 83 84 85 86 87
    $config_factory->reset('system.site');
    $this->assertEqual('Site name global conf override', $config_factory->get('system.site')->get('name'));
    $this->assertEqual($module_overridden_slogan, $config_factory->get('system.site')->get('slogan'));
    $this->assertEqual($language_overridden_mail, $config_factory->get('system.site')->get('mail'));
    $this->assertEqual(50, $config_factory->get('system.site')->get('weight_select_max'));

88 89
    $old_state = $config_factory->getOverrideState();
    $config_factory->setOverrideState(FALSE);
90 91 92 93
    $this->assertEqual($non_overridden_name, $config_factory->get('system.site')->get('name'));
    $this->assertEqual($non_overridden_slogan, $config_factory->get('system.site')->get('slogan'));
    $this->assertEqual($non_overridden_mail, $config_factory->get('system.site')->get('mail'));
    $this->assertEqual(50, $config_factory->get('system.site')->get('weight_select_max'));
94
    $config_factory->setOverrideState($old_state);
95 96 97 98

    unset($GLOBALS['config_test_run_module_overrides']);
  }
}