View source
<?php
namespace Drupal\Tests\Core\Config;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Render\Markup;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigValueException;
use PHPUnit\Framework\Error\Warning;
class ConfigTest extends UnitTestCase {
protected $config;
protected $storage;
protected $eventDispatcher;
protected $typedConfig;
protected $cacheTagsInvalidator;
protected function setUp() {
$this->storage = $this
->createMock('Drupal\\Core\\Config\\StorageInterface');
$this->eventDispatcher = $this
->createMock('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface');
$this->typedConfig = $this
->createMock('\\Drupal\\Core\\Config\\TypedConfigManagerInterface');
$this->config = new Config('config.test', $this->storage, $this->eventDispatcher, $this->typedConfig);
$this->cacheTagsInvalidator = $this
->createMock('Drupal\\Core\\Cache\\CacheTagsInvalidatorInterface');
$container = new ContainerBuilder();
$container
->set('cache_tags.invalidator', $this->cacheTagsInvalidator);
\Drupal::setContainer($container);
}
public function testSetName($name) {
$this->config
->setName($name);
$this
->assertEquals($name, $this->config
->getName());
$this->config
->validateName($name);
}
public function setNameProvider() {
return [
[
'test.name',
],
[
'test.' . str_repeat('a', Config::MAX_NAME_LENGTH - 5),
],
];
}
public function testIsNew() {
$this
->assertTrue($this->config
->isNew());
$this->config
->save();
$this
->assertFalse($this->config
->isNew());
}
public function testSetData($data) {
$this->config
->setData($data);
$this
->assertEquals($data, $this->config
->getRawData());
$this
->assertConfigDataEquals($data);
}
public function testSaveNew($data) {
$this->cacheTagsInvalidator
->expects($this
->never())
->method('invalidateTags');
$this->config
->setData($data);
foreach ($data as $key => $value) {
$this
->assertNull($this->config
->getOriginal($key, FALSE));
}
$config = $this->config
->save();
$this
->assertInstanceOf('\\Drupal\\Core\\Config\\Config', $config);
$this
->assertOriginalConfigDataEquals($data, TRUE);
}
public function testSaveExisting($data) {
$this->cacheTagsInvalidator
->expects($this
->once())
->method('invalidateTags')
->with([
'config:config.test',
]);
$this->config
->setData($data);
$this->config
->save();
$new_data = $data;
$new_data['a']['d'] = 2;
$this->config
->setData($new_data);
$this->config
->save();
$this
->assertOriginalConfigDataEquals($new_data, TRUE);
}
public function testOverrideData($data, $module_data, $setting_data) {
$this->config
->setData($data);
$this
->assertConfigDataEquals($data);
$this->config
->save();
$this
->assertFalse($this->config
->hasOverrides());
$this
->assertOverriddenKeys($data, []);
$this->config
->setModuleOverride($module_data);
$this
->assertConfigDataEquals($module_data);
$this
->assertOverriddenKeys($data, $module_data);
$this->config
->save();
$this
->assertConfigDataEquals($module_data);
$this
->assertOverriddenKeys($data, $module_data);
$this->config
->setModuleOverride([]);
$this
->assertOverriddenKeys($data, []);
$this->config
->setSettingsOverride($setting_data);
$this
->assertConfigDataEquals($setting_data);
$this
->assertOverriddenKeys($data, $setting_data);
$this->config
->save();
$this
->assertConfigDataEquals($setting_data);
$this
->assertOverriddenKeys($data, $setting_data);
$this->config
->setModuleOverride($module_data);
$merged_overrides = array_merge($module_data, $setting_data);
$this
->assertConfigDataEquals($setting_data);
$this
->assertOverriddenKeys($data, $merged_overrides);
$this->config
->save();
$this
->assertConfigDataEquals($setting_data);
$this
->assertOverriddenKeys($data, $merged_overrides);
$this
->assertOriginalConfigDataEquals($data, FALSE);
$this
->assertOriginalConfigDataEquals($setting_data, TRUE);
foreach ($setting_data as $key => $value) {
$config_value = $this->config
->getOriginal($key);
$this
->assertEquals($value, $config_value);
}
$this->config
->setModuleOverride([]);
$this->config
->setSettingsOverride([]);
$this
->assertConfigDataEquals($data);
$this
->assertOverriddenKeys($data, []);
$this->config
->save();
$this
->assertConfigDataEquals($data);
$this
->assertOverriddenKeys($data, []);
}
public function testSetValue($data) {
foreach ($data as $key => $value) {
$this->config
->set($key, $value);
}
$this
->assertConfigDataEquals($data);
}
public function testSetValidation() {
$this
->expectException(ConfigValueException::class);
$this->config
->set('testData', [
'dot.key' => 1,
]);
}
public function testSetIllegalOffsetValue() {
$this->config
->set('testData', 1);
$this
->expectException(Warning::class);
$this->config
->set('testData.illegalOffset', 1);
}
public function testInitWithData($data) {
$config = $this->config
->initWithData($data);
$this
->assertInstanceOf('\\Drupal\\Core\\Config\\Config', $config);
$this
->assertEquals(FALSE, $this->config
->isNew());
$this
->assertConfigDataEquals($data);
$this
->assertOriginalConfigDataEquals($data, TRUE);
$this
->assertOriginalConfigDataEquals($data, FALSE);
}
public function testClear($data) {
foreach ($data as $key => $value) {
$this->config
->set($key, $value);
$this
->assertEquals($value, $this->config
->get($key));
$this->config
->clear($key);
$this
->assertNull($this->config
->get($key));
}
}
public function testNestedClear($data) {
foreach ($data as $key => $value) {
$this->config
->set($key, $value);
foreach ($value as $nested_key => $nested_value) {
$full_nested_key = $key . '.' . $nested_key;
$this
->assertEquals($nested_value, $this->config
->get($full_nested_key));
$this->config
->clear($full_nested_key);
$this
->assertNull($this->config
->get($full_nested_key));
}
}
}
public function testDelete($data, $module_data) {
$this->cacheTagsInvalidator
->expects($this
->once())
->method('invalidateTags')
->with([
'config:config.test',
]);
foreach ($data as $key => $value) {
$this->config
->set($key, $value);
}
$this->config
->setModuleOverride($module_data);
$this->config
->save();
$this
->assertOriginalConfigDataEquals($data, FALSE);
$this
->assertConfigDataEquals($module_data);
$this
->assertOriginalConfigDataEquals($module_data, TRUE);
$this
->assertFalse($this->config
->isNew());
$this->config
->delete();
$this
->assertTrue($this->config
->isNew());
foreach ($data as $key => $value) {
$this
->assertEmpty($this->config
->getOriginal($key, FALSE));
}
foreach ($module_data as $key => $value) {
$this
->assertConfigDataEquals($module_data);
$this
->assertOriginalConfigDataEquals($module_data, TRUE);
}
}
public function testMerge($data, $data_to_merge, $merged_data) {
$this->config
->setData($data);
$this->config
->merge($data_to_merge);
$this
->assertEquals($merged_data, $this->config
->getRawData());
}
public function mergeDataProvider() {
return [
[
[
'a' => 1,
'b' => 2,
'c' => [
'd' => 3,
],
],
[
'a' => 2,
'e' => 4,
'c' => [
'f' => 5,
],
],
[
'a' => 2,
'b' => 2,
'c' => [
'd' => 3,
'f' => 5,
],
'e' => 4,
],
],
];
}
public function testValidateNameException($name, $exception_message) {
$this
->expectException('\\Drupal\\Core\\Config\\ConfigNameException');
$this
->expectExceptionMessage($exception_message);
$this->config
->validateName($name);
}
public function testGetCacheTags() {
$this
->assertSame([
'config:' . $this->config
->getName(),
], $this->config
->getCacheTags());
}
public function validateNameProvider() {
$return = [
[
'MissingNamespace',
'Missing namespace in Config object name MissingNamespace.',
],
[
str_repeat('a', Config::MAX_NAME_LENGTH) . ".",
'Config object name ' . str_repeat('a', Config::MAX_NAME_LENGTH) . '. exceeds maximum allowed length of ' . Config::MAX_NAME_LENGTH . ' characters.',
],
];
foreach ([
':',
'?',
'*',
'<',
'>',
'"',
"'",
'/',
'\\',
] as $char) {
$name = 'name.' . $char;
$return[] = [
$name,
"Invalid character in Config object name {$name}.",
];
}
return $return;
}
public function overrideDataProvider() {
$test_cases = [
[
[
'a' => 'originalValue',
],
[
'a' => 'moduleValue',
],
[
'a' => 'settingValue',
],
],
[
[
'a' => 'originalValue',
'b' => 'originalValue',
'c' => 'originalValue',
],
[
'a' => 'moduleValue',
'b' => 'moduleValue',
],
[
'a' => 'settingValue',
],
],
[
[
'a' => 'allTheSameValue',
],
[
'a' => 'allTheSameValue',
],
[
'a' => 'allTheSameValue',
],
],
];
foreach ($test_cases as $test_key => $test_case) {
foreach ($test_case as $parameter) {
$nested_parameter = [];
foreach ($parameter as $config_key => $value) {
$nested_value = $value;
for ($i = 5; $i >= 0; $i--) {
$nested_value = [
$i => $nested_value,
];
}
$nested_parameter[$config_key] = $nested_value;
}
$test_cases["nested:{$test_key}"][] = $nested_parameter;
}
}
return $test_cases;
}
public function simpleDataProvider() {
return [
[
[
'a' => '1',
'b' => '2',
'c' => '3',
],
],
];
}
public function nestedDataProvider() {
return [
[
[
'a' => [
'd' => 1,
],
'b' => [
'e' => 2,
],
'c' => [
'f' => 3,
],
],
],
];
}
public function assertConfigDataEquals($data) {
foreach ($data as $key => $value) {
$this
->assertEquals($value, $this->config
->get($key));
}
}
public function assertOriginalConfigDataEquals($data, $apply_overrides) {
foreach ($data as $key => $value) {
$config_value = $this->config
->getOriginal($key, $apply_overrides);
$this
->assertEquals($value, $config_value);
}
}
public function testSafeStringHandling() {
$safe_string = Markup::create('bar');
$this->config
->set('foo', $safe_string);
$this
->assertSame('bar', $this->config
->get('foo'));
$this->config
->set('foo', [
'bar' => $safe_string,
]);
$this
->assertSame('bar', $this->config
->get('foo.bar'));
$this->config
->setData([
'bar' => $safe_string,
]);
$this
->assertSame('bar', $this->config
->get('bar'));
$this->config
->initWithData([
'bar' => $safe_string,
]);
$this
->assertSame($safe_string, $this->config
->get('bar'));
}
protected function assertOverriddenKeys(array $data, array $overridden_data) {
if (empty($overridden_data)) {
$this
->assertFalse($this->config
->hasOverrides());
}
else {
$this
->assertTrue($this->config
->hasOverrides());
foreach ($overridden_data as $key => $value) {
if (is_array($value)) {
$nested_key = $key;
$nested_value = $overridden_data[$key];
while (is_array($nested_value)) {
$nested_key .= '.' . key($nested_value);
$this
->assertTrue($this->config
->hasOverrides($nested_key));
$nested_value = array_pop($nested_value);
}
}
$this
->assertTrue($this->config
->hasOverrides($key));
}
}
$non_overridden_keys = array_diff(array_keys($data), array_keys($overridden_data));
foreach ($non_overridden_keys as $non_overridden_key) {
$this
->assertFalse($this->config
->hasOverrides($non_overridden_key));
if (is_array($data[$non_overridden_key])) {
$nested_key = $non_overridden_key;
$nested_value = $data[$non_overridden_key];
while (is_array($nested_value)) {
$nested_key .= '.' . key($nested_value);
$this
->assertFalse($this->config
->hasOverrides($nested_key));
$nested_value = array_pop($nested_value);
}
}
}
}
}