View source
<?php
namespace Drupal\Tests\config_actions\Kernel;
use Drupal\Core\File\FileSystem;
use Drupal\KernelTests\KernelTestBase;
use org\bovigo\vfs\vfsStream;
use Drupal\config_actions\Plugin\ConfigActionsSource\ConfigActionsFile;
use Drupal\Core\Site\Settings;
class ConfigActionsSourceTest extends KernelTestBase {
public static $modules = [
'system',
'config_actions',
'test_config_actions',
];
protected $strictConfigSchema = FALSE;
protected $sourceManager;
protected $fileSystem;
protected $logger;
public function setUp() {
parent::setUp();
$this
->installConfig('system');
$this->sourceManager = $this->container
->get('plugin.manager.config_actions_source');
$stream_wrapper_manager = $this
->createMock('Drupal\\Core\\StreamWrapper\\StreamWrapperManagerInterface');
$settings = new Settings([]);
$this->logger = $this
->createMock('Psr\\Log\\LoggerInterface');
$this->fileSystem = new FileSystem($stream_wrapper_manager, $settings, $this->logger);
}
public function testArray() {
$source = [
'mykey' => 'test value',
];
$options = [
'source' => $source,
];
$plugin = $this->sourceManager
->createInstance('array', $options);
$this
->assertTrue($plugin
->detect($source), 'Source detected');
$this
->assertEquals($source, $plugin
->load(), 'Loading plugin data');
$new_data = [
'newkey' => 'new value',
];
$source = array_merge($source, $new_data);
$this
->assertTrue($plugin
->save($new_data), 'Saving data to plugin');
$this
->assertEquals($source, $plugin
->load());
$new_data = [
'mykey' => 'new value2',
'newkey' => 'new value2',
];
$source = $new_data;
$this
->assertTrue($plugin
->save($new_data), 'Saving data to plugin');
$this
->assertEquals($source, $plugin
->load());
$new_data = [
'newkey' => 'new value2',
];
$source = $new_data;
$plugin
->setMerge(FALSE);
$this
->assertTrue($plugin
->save($new_data), 'Saving data to plugin');
$this
->assertEquals($source, $plugin
->load());
}
public function testId() {
$source = 'core.date_format.long';
$options = [
'source' => $source,
];
$plugin = $this->sourceManager
->createInstance('id', $options);
$this
->assertTrue($plugin
->detect($source), 'Source detected');
$data = $plugin
->load();
$this
->assertEquals('long', $data['id']);
$new_data = [
'label' => "My Label",
];
$plugin
->setMerge(TRUE);
$this
->assertTrue($plugin
->save($new_data), 'Saving data to plugin');
$data = $plugin
->load();
$this
->assertEquals('My Label', $data['label']);
}
public function testFile() {
$source = 'field.field.node.image.yml';
$options = [
'source' => $source,
'base' => DRUPAL_ROOT . '/' . drupal_get_path('module', 'test_config_actions'),
];
$plugin = $this->sourceManager
->createInstance('file', $options);
$this
->assertTrue($plugin
->detect($source), 'Source detected');
$data = $plugin
->load();
$this
->assertEquals('node.@bundle@.@field_name@', $data['id']);
$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);
}
$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');
$plugin = $this->sourceManager
->createInstance('file', $options);
$data = $plugin
->load();
$this
->assertEquals($new_data, $data);
$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');
$plugin = $this->sourceManager
->createInstance('file', $options);
$data = $plugin
->load();
$this
->assertEquals($new_data, $data);
}
public function testList() {
$source = [
'field.field.node.image.yml',
'core.date_format.long',
];
$options = [
'source' => $source,
'base' => DRUPAL_ROOT . '/' . drupal_get_path('module', 'test_config_actions'),
];
$plugin = $this->sourceManager
->createInstance('list', $options);
$this
->assertTrue($plugin
->detect($source), 'Source detected');
$data = $plugin
->load();
$this
->assertEquals('node.@bundle@.@field_name@', $data['id']);
$source = [
'field.field.node.image.DOESNOTEXIST.yml',
'core.date_format.long',
];
$options = [
'source' => $source,
'base' => DRUPAL_ROOT . '/' . drupal_get_path('module', 'test_config_actions'),
];
$plugin = $this->sourceManager
->createInstance('list', $options);
$this
->assertTrue($plugin
->detect($source), 'Source detected');
$data = $plugin
->load();
$this
->assertEquals('long', $data['id']);
$source = [
'core.date_format.long',
'field.field.node.image.yml',
];
$options = [
'source' => $source,
'base' => DRUPAL_ROOT . '/' . drupal_get_path('module', 'test_config_actions'),
];
$plugin = $this->sourceManager
->createInstance('list', $options);
$this
->assertTrue($plugin
->detect($source), 'Source detected');
$data = $plugin
->load();
$this
->assertEquals('long', $data['id']);
$source = [
'core.date_format.MYFORMAT',
'field.field.node.image.yml',
];
$options = [
'source' => $source,
'base' => DRUPAL_ROOT . '/' . drupal_get_path('module', 'test_config_actions'),
];
$plugin = $this->sourceManager
->createInstance('list', $options);
$this
->assertTrue($plugin
->detect($source), 'Source detected');
$data = $plugin
->load();
$this
->assertEquals('node.@bundle@.@field_name@', $data['id']);
}
public function testTemplate() {
$source = 'field.field.node.image';
$options = [
'source' => $source,
];
$plugin = $this->sourceManager
->createInstance('template', $options);
$this
->assertFalse($plugin
->detect($source), 'No autodetection');
$data = $plugin
->load();
$this
->assertEquals('@field_name@', $data['field_name']);
$this
->assertFalse($plugin
->save([]), 'Should not save');
}
}