You are here

public function ConfigActionsPluginTest::testAdd in Config Actions 8

@covers \Drupal\config_actions\Plugin\ConfigActions\ConfigActionsAdd

File

tests/src/Kernel/ConfigActionsPluginTest.php, line 310

Class

ConfigActionsPluginTest
test the ConfigActions plugins

Namespace

Drupal\Tests\config_actions\Kernel

Code

public function testAdd() {

  // Find a config file that has an array we can add to without violating schema.
  $source = 'system.action.node_delete_action';

  // Create a new key and value in config.
  $value = [
    'abc' => 123,
    'def' => 'test',
  ];
  $action = [
    'plugin' => 'add',
    'source' => $source,
    'path' => [
      'newkey',
    ],
    'value' => $value,
  ];
  $orig_config = $this
    ->getConfig($source);
  $tree = $this->configActions
    ->processAction($action);
  $orig_config['newkey'] = $value;

  // First check raw data transformation contains new key.
  self::assertEquals($orig_config, $tree);

  // Now check actual stored config since you can't just add new key values to schema.
  $new_config = $this
    ->getConfig($source);
  self::assertArrayNotHasKey('newkey', $new_config);

  // "Append" additional data to existing key.
  $value = 'mydata';
  $action = [
    'plugin' => 'add',
    'source' => $source,
    'path' => [
      'configuration',
    ],
    'value' => $value,
  ];
  $orig_config = $this
    ->getConfig($source);
  $this->configActions
    ->processAction($action);
  $new_config = $this
    ->getConfig($source);
  $orig_config['configuration'] = [
    $value,
  ];
  self::assertEquals($orig_config, $new_config);

  // Now add another additional value to existing key.
  $value2 = 'another';
  $action = [
    'plugin' => 'add',
    'source' => $source,
    'path' => [
      'configuration',
    ],
    'value' => $value2,
  ];
  $orig_config = $this
    ->getConfig($source);
  $this->configActions
    ->processAction($action);
  $new_config = $this
    ->getConfig($source);
  $orig_config['configuration'] = [
    $value,
    $value2,
  ];
  self::assertEquals($orig_config, $new_config);

  // Test "change" vs "add".
  $action = [
    'plugin' => 'change',
    'source' => $source,
    'path' => [
      'configuration',
    ],
    'value' => [
      $value,
    ],
  ];
  $orig_config = $this
    ->getConfig($source);
  $this->configActions
    ->processAction($action);
  $new_config = $this
    ->getConfig($source);
  $orig_config['configuration'] = [
    $value,
  ];
  self::assertEquals($orig_config, $new_config);
}