function SettingsTest::testFakeDeprecatedSettings

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Site/SettingsTest.php \Drupal\Tests\Core\Site\SettingsTest::testFakeDeprecatedSettings()
  2. 10 core/tests/Drupal/Tests/Core/Site/SettingsTest.php \Drupal\Tests\Core\Site\SettingsTest::testFakeDeprecatedSettings()

Tests deprecation messages and values when using fake deprecated settings.

Note: Tests for real deprecated settings should not be added to this test or provider. This test is only for the general deprecated settings API itself.

@dataProvider providerTestFakeDeprecatedSettings

@covers ::handleDeprecations @covers ::initialize

@group legacy

Parameters

string[] $settings_config: Array of settings to put in the settings.php file for testing.

string $setting_name: The name of the setting this case should use for Settings::get().

string $expected_value: The expected value of the setting.

bool $expect_deprecation_message: Should the case expect a deprecation message? Defaults to TRUE.

See also

self::testRealDeprecatedSettings()

self::providerTestRealDeprecatedSettings()

File

core/tests/Drupal/Tests/Core/Site/SettingsTest.php, line 183

Class

SettingsTest
@coversDefaultClass \Drupal\Core\Site\Settings @runTestsInSeparateProcesses @group Site

Namespace

Drupal\Tests\Core\Site

Code

public function testFakeDeprecatedSettings(array $settings_config, string $setting_name, string $expected_value, bool $expect_deprecation_message = TRUE) : void {
    $settings_file_content = "<?php\n";
    foreach ($settings_config as $name => $value) {
        $settings_file_content .= "\$settings['{$name}'] = '{$value}';\n";
    }
    $class_loader = NULL;
    $vfs_root = vfsStream::setup('root');
    $sites_directory = vfsStream::newDirectory('sites')->at($vfs_root);
    vfsStream::newFile('settings.php')->at($sites_directory)
        ->setContent($settings_file_content);
    // This is the deprecated setting used by all cases for this test method.
    $deprecated_setting = [
        'replacement' => 'happy_replacement',
        'message' => 'The settings key "deprecated_legacy" is deprecated in drupal:9.1.0 and will be removed in drupal:10.0.0. Use "happy_replacement" instead. See https://www.drupal.org/node/3163226.',
    ];
    $class = new \ReflectionClass(Settings::class);
    $instance_property = $class->getProperty('deprecatedSettings');
    $deprecated_settings = $instance_property->getValue();
    $deprecated_settings['deprecated_legacy'] = $deprecated_setting;
    $instance_property->setValue(NULL, $deprecated_settings);
    if ($expect_deprecation_message) {
        $this->expectDeprecation($deprecated_setting['message']);
    }
    Settings::initialize(vfsStream::url('root'), 'sites', $class_loader);
    $this->assertEquals($expected_value, Settings::get($setting_name));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.