You are here

public function ConfigActionsSourceTest::testFile in Config Actions 8

@covers \Drupal\config_actions\Plugin\ConfigActionsSource\ConfigActionsId

File

tests/src/Kernel/ConfigActionsSourceTest.php, line 135

Class

ConfigActionsSourceTest
test the ConfigActionsSource plugins

Namespace

Drupal\Tests\config_actions\Kernel

Code

public function testFile() {
  $source = 'field.field.node.image.yml';
  $options = [
    'source' => $source,
    'base' => DRUPAL_ROOT . '/' . drupal_get_path('module', 'test_config_actions'),
  ];

  /** @var \Drupal\config_actions\ConfigActionsSourceInterface $plugin */
  $plugin = $this->sourceManager
    ->createInstance('file', $options);
  $this
    ->assertTrue($plugin
    ->detect($source), 'Source detected');
  $data = $plugin
    ->load();
  $this
    ->assertEquals('node.@bundle@.@field_name@', $data['id']);

  // Test file saving
  $path = DRUPAL_ROOT . drupal_get_path('module', 'test_config_actions');
  $config_file = 'myactions.yml';
  $filename = $path . '/' . $config_file;
  if (file_exists($filename)) {
    $this->fileSystem
      ->delete($filename);
  }

  // Now, write config data to the myactions.yml file
  $options = [
    'source' => $config_file,
    'base' => $path,
  ];
  $plugin = $this->sourceManager
    ->createInstance('file', $options);
  $new_data = [
    'mykey' => "Test value",
  ];
  $this
    ->assertTrue($plugin
    ->save($new_data), 'Saving data to plugin');

  // Finally, read the data back from the file and see if it matches.
  $plugin = $this->sourceManager
    ->createInstance('file', $options);
  $data = $plugin
    ->load();
  $this
    ->assertEquals($new_data, $data);

  // Next, perform the same test but instead of using the base_path, use
  // an absolute path in the source filename.
  $base_path = $path . '/' . drupal_get_path('module', 'test_config_actions');
  $config_file = 'myactions.yml';
  $options = [
    'source' => $base_path . '/' . $config_file,
  ];
  $plugin = $this->sourceManager
    ->createInstance('file', $options);
  $new_data = [
    'mykey' => "Test value",
  ];
  $this
    ->assertTrue($plugin
    ->save($new_data), 'Saving data to plugin');

  // Finally, read the data back from the file and see if it matches.
  $plugin = $this->sourceManager
    ->createInstance('file', $options);
  $data = $plugin
    ->load();
  $this
    ->assertEquals($new_data, $data);
}