View source
<?php
namespace Drupal\Tests\config_rewrite\Kernel;
use Drupal\KernelTests\KernelTestBase;
class ConfigRewriteTest extends KernelTestBase {
public static $modules = [
'system',
'user',
'config_rewrite',
'config_rewrite_test',
'config_rewrite_test_rewrite',
'language',
];
protected $activeConfigStorage;
protected $configRewriter;
protected $languageConfigFactoryOverride;
protected function setUp() {
parent::setUp();
$this->configRewriter = $this->container
->get('config_rewrite.config_rewriter');
$this->activeConfigStorage = $this->container
->get('config.storage');
$this->languageConfigFactoryOverride = $this->container
->get('language.config_factory_override');
$this
->installSchema('system', [
'sequence',
]);
$this
->installEntitySchema('user_role');
$this
->installConfig([
'language',
'config_rewrite_test',
]);
}
function testConfigRewrite() {
$expected_original_data = [
'label' => 'Test 1',
'is_admin' => FALSE,
'permissions' => [
'access user profiles',
],
];
$data = $this->activeConfigStorage
->read('user.role.test1');
$this
->assertIdentical($data['label'], $expected_original_data['label']);
$this
->assertIdentical($data['permissions'], $expected_original_data['permissions']);
$this->configRewriter
->rewriteModuleConfig('config_rewrite_test_rewrite');
$expected_rewritten_data = [
'label' => 'Test 1 rewritten',
'is_admin' => FALSE,
'permissions' => [
'access user profiles',
'change own username',
],
];
$user_role = $this->activeConfigStorage
->read('user.role.test1');
$this
->assertEquals($user_role['label'], $expected_rewritten_data['label']);
$this
->assertEquals($user_role['is_admin'], $expected_rewritten_data['is_admin']);
$this
->assertEquals($user_role['permissions'], $expected_rewritten_data['permissions']);
$expected_rewritten_data = [
'label' => 'Test 2 rewritten',
'is_admin' => FALSE,
'permissions' => [
'access user profiles',
'change own username',
],
];
$user_role = $this->activeConfigStorage
->read('user.role.test2');
$this
->assertEquals($user_role['label'], $expected_rewritten_data['label']);
$this
->assertEquals($user_role['is_admin'], $expected_rewritten_data['is_admin']);
$this
->assertEquals($user_role['permissions'], $expected_rewritten_data['permissions']);
$this
->assertFalse(isset($user_role['config_rewrite']));
$expected_rewritten_data = [
'label' => 'Test 3 replaced',
'is_admin' => FALSE,
'permissions' => [
'change own username',
],
];
$user_role = $this->activeConfigStorage
->read('user.role.test3');
$this
->assertEquals($user_role['label'], $expected_rewritten_data['label']);
$this
->assertEquals($user_role['is_admin'], $expected_rewritten_data['is_admin']);
$this
->assertEquals($user_role['permissions'], $expected_rewritten_data['permissions']);
$this
->assertFalse(isset($user_role['config_rewrite']));
$expected_rewritten_data = [
'label' => 'Test 4 réécrit',
];
$user_role = $this->languageConfigFactoryOverride
->getOverride('fr', 'user.role.test4')
->get();
$this
->assertEquals($user_role['label'], $expected_rewritten_data['label']);
}
}