You are here

public function ConfigRewriteTest::testConfigRewrite in Configuration Rewrite 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/ConfigRewriteTest.php \Drupal\Tests\config_rewrite\Kernel\ConfigRewriteTest::testConfigRewrite()

@covers ::rewriteModuleConfig @covers ::rewriteConfig

File

tests/src/Kernel/ConfigRewriteTest.php, line 59

Class

ConfigRewriteTest
@coversDefaultClass \Drupal\config_rewrite\ConfigRewriter @group config_rewrite

Namespace

Drupal\Tests\config_rewrite\Kernel

Code

public function testConfigRewrite() {
  $expected_original_data = [
    'label' => 'Test 1',
    'is_admin' => FALSE,
    'permissions' => [
      'access user profiles',
    ],
  ];

  // Verify that the original configuration data exists.
  $data = $this->activeConfigStorage
    ->read('user.role.test1');
  $this
    ->assertIdentical($data['label'], $expected_original_data['label']);
  $this
    ->assertIdentical($data['permissions'], $expected_original_data['permissions']);

  // Rewrite configuration.
  $this->configRewriter
    ->rewriteModuleConfig('config_rewrite_test_rewrite');

  // Test a rewrite where config_rewrite is not set.
  // Test that data is modified.
  $expected_rewritten_data = [
    'label' => 'Test 1 rewritten',
    // Unchanged.
    'is_admin' => FALSE,
    // Merged.
    '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']);

  // Test a rewrite where config_rewrite is set to an unsupported value.
  // Test that data is modified.
  $expected_rewritten_data = [
    'label' => 'Test 2 rewritten',
    // Unchanged.
    'is_admin' => FALSE,
    // Merged.
    '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']);

  // Test that the "config_rewrite" key was unset.
  $this
    ->assertFalse(isset($user_role['config_rewrite']));

  // Test a rewrite where config_rewrite is set to "replace".
  // Test that data is replaced.
  $expected_rewritten_data = [
    'label' => 'Test 3 replaced',
    // Unchanged.
    'is_admin' => FALSE,
    // Rewritten.
    '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']);

  // Test that the "config_rewrite" key was unset.
  $this
    ->assertFalse(isset($user_role['config_rewrite']));

  // Test a multilingual 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']);

  // Test a rewrite where config_rewrite.replace contains keys.
  // Test that data is rewritten as expected.
  $expected_rewritten_data = [
    //rewritten
    'label' => 'Test 5 rewritten & replaced',
    //removed.

    //'is_admin' => FALSE,

    // Replaced.
    'permissions' => [
      'change own username',
    ],
  ];
  $user_role = $this->activeConfigStorage
    ->read('user.role.test5');
  $this
    ->assertEquals($user_role['label'], $expected_rewritten_data['label']);
  $this
    ->assertArrayNotHasKey('is_admin', $user_role);
  $this
    ->assertEquals($user_role['permissions'], $expected_rewritten_data['permissions']);

  // Test that the "config_rewrite" key was unset.
  $this
    ->assertFalse(isset($user_role['config_rewrite']));
}