You are here

public function ConfigActionsPluginTest::testTemplate in Config Actions 8

@covers \Drupal\config_actions\Plugin\ConfigActions\ConfigActionsChange

File

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

Class

ConfigActionsPluginTest
test the ConfigActions plugins

Namespace

Drupal\Tests\config_actions\Kernel

Code

public function testTemplate() {

  // Test basic template function using change plugin.
  $config_id = 'field.storage.node.myimage';
  $source_file = dirname(__FILE__) . '/field.storage.node.image.yml';
  $dest = 'field.storage.node.@field_name@';
  $replace = [
    '@field_name@' => 'myimage',
    '@cardinality@' => 2,
  ];
  $action = [
    'source' => $source_file,
    'dest' => $dest,
    'replace' => $replace,
  ];
  $orig_config = Yaml::decode(file_get_contents($source_file));
  $orig_config = ConfigActionsTransform::replace($orig_config, $replace);
  $this->configActions
    ->processAction($action);
  $new_config = $this
    ->getConfig($config_id);
  self::assertEquals($orig_config, $new_config);
  self::assertTrue(is_int($new_config['cardinality']), 'Cardinality test should set config as integer value');

  // Clean up for next test.
  $this
    ->deleteConfig($config_id);

  // Test replace_in to prevent string replacement
  $dest = 'field.storage.node.field_name';
  $replace = [
    'field_name' => 'myimage',
    '@cardinality@' => 1,
  ];
  $action = [
    'source' => $source_file,
    'dest' => $dest,
    'replace' => $replace,
    'replace_in' => [],
  ];
  $orig_config = Yaml::decode(file_get_contents($source_file));
  $tree = $this->configActions
    ->processAction($action);
  self::assertEquals($orig_config, $tree);

  // Check saved config
  $new_config = $this
    ->getConfig($dest);
  self::assertEquals($orig_config, $new_config);

  // Ensure config didn't get created with new name.
  $new_config = $this
    ->getConfig($config_id);
  self::assertEmpty($new_config);

  // Clean up for next test.
  $this
    ->deleteConfig($config_id);

  // Test using an array of sources to override existing config
  // First time it should create new config
  $dest = $config_id;
  $replace = [
    '@field_name@' => 'myimage',
  ];
  $action = [
    'source' => [
      '@dest@',
      $source_file,
    ],
    'dest' => $dest,
    'replace' => $replace,
    'value' => [
      'cardinality' => 2,
      'translatable' => true,
    ],
  ];
  $orig_config = Yaml::decode(file_get_contents($source_file));
  $orig_config = ConfigActionsTransform::replace($orig_config, $replace);
  $orig_config['cardinality'] = 2;
  $orig_config['translatable'] = true;
  $this->configActions
    ->processAction($action);
  $new_config = $this
    ->getConfig($config_id);
  self::assertEquals($orig_config, $new_config);

  // Second time it should use existing config
  // Changing 'translatable', but 'cardinality' should still be 2,
  // and NOT the 1 that is in the original template/default
  // Clear cache to ensure previous data isn't still being used.
  $this->configActions
    ->clearSourceCache();
  $dest = $config_id;
  $replace = [
    '@field_name@' => 'myimage',
  ];
  $action = [
    'source' => [
      '@dest@',
      $source_file,
    ],
    'dest' => $dest,
    'replace' => $replace,
    'value' => [
      'translatable' => false,
    ],
  ];
  $orig_config = Yaml::decode(file_get_contents($source_file));
  $orig_config = ConfigActionsTransform::replace($orig_config, $replace);
  $orig_config['cardinality'] = 2;
  $orig_config['translatable'] = false;
  $this->configActions
    ->processAction($action);
  $new_config = $this
    ->getConfig($config_id);
  self::assertEquals($orig_config, $new_config);
}