View source
<?php
namespace Drupal\Tests\update_helper\Kernel;
use Drupal\Component\Serialization\Yaml;
use Drupal\KernelTests\KernelTestBase;
use Drupal\update_helper\ConfigHandler;
use Drush\TestTraits\DrushTestTrait;
use Symfony\Component\Filesystem\Filesystem;
class ConfigHandlerTest extends KernelTestBase {
use DrushTestTrait;
protected static $configSchemaCheckerExclusions = [
'field.storage.node.body',
];
protected static $modules = [
'config_update',
'update_helper',
'user',
'text',
'field',
'node',
];
public static function getUpdateDefinition() {
return <<<EOF
field.storage.node.body:
expected_config:
lost_config: text
settings:
max_length: 123
status: false
type: text
update_actions:
delete:
lost_config: text
settings:
max_length: 123
add:
cardinality: 1
change:
settings: { }
status: true
type: text_with_summary
EOF;
}
protected $startingSha1;
protected function setUp() {
parent::setUp();
$extensionStorage = \Drupal::service('config_update.extension_storage');
$this->startingSha1 = sha1_file($extensionStorage
->getFilePath('field.storage.node.body'));
}
protected function setUpFilesystem() {
$public_file_directory = $this->siteDirectory . '/files';
require_once 'core/includes/file.inc';
mkdir($this->siteDirectory, 0775);
mkdir($this->siteDirectory . '/files', 0775);
mkdir($this->siteDirectory . '/files/config/sync', 0775, TRUE);
$this
->setSetting('file_public_path', $public_file_directory);
$this
->setSetting('config_sync_directory', $this->siteDirectory . '/files/config/sync');
$file_system = new Filesystem();
$file_system
->mirror('core/modules/node', $this->siteDirectory . '/modules/node');
}
public function testGeneratePatchFileFromActiveConfig() {
$configHandler = \Drupal::service('update_helper.config_handler');
$configReverter = \Drupal::service('config_update.config_update');
$configReverter
->import('field_storage_config', 'node.body');
$configFactory = \Drupal::service('config.factory');
$config = $configFactory
->getEditable('field.storage.node.body');
$configData = $config
->get();
$configData['status'] = FALSE;
$configData['type'] = 'text';
unset($configData['cardinality']);
$configData['settings'] = [
'max_length' => 123,
];
$configData['lost_config'] = 'text';
$config
->setData($configData)
->save(TRUE);
$data = $configHandler
->generatePatchFile([
'node',
], TRUE);
$this
->assertEquals($this
->getUpdateDefinition(), $data);
$extensionStorage = \Drupal::service('config_update.extension_storage');
$this
->assertEqual($this->startingSha1, sha1_file($extensionStorage
->getFilePath('field.storage.node.body')));
}
public function testGeneratePatchFileWithConfigExport() {
$configHandler = \Drupal::service('update_helper.config_handler');
$extensionStorage = \Drupal::service('config_update.extension_storage');
$configFilePath = $extensionStorage
->getFilePath('field.storage.node.body');
$configReverter = \Drupal::service('config_update.config_update');
$configReverter
->import('field_storage_config', 'node.body');
$configFactory = \Drupal::service('config.factory');
$config = $configFactory
->getEditable('field.storage.node.body');
$configData = $config
->get();
$configData['type'] = 'text';
$configData['settings'] = [
'max_length' => 321,
];
$config
->setData($configData)
->save(TRUE);
$fileData = Yaml::decode(file_get_contents($configFilePath));
$this
->assertEqual('text_with_summary', $fileData['type']);
$this
->assertEqual([], $fileData['settings']);
$data = $configHandler
->generatePatchFile([
'node',
], FALSE);
$expected = <<<EOF
field.storage.node.body:
expected_config:
settings: { }
type: text_with_summary
update_actions:
change:
settings:
max_length: 321
type: text
EOF;
$this
->assertEqual($expected, $data);
$fileData = Yaml::decode(file_get_contents($configFilePath));
$this
->assertEqual('text', $fileData['type']);
$this
->assertEqual([
'max_length' => 321,
], $fileData['settings']);
}
public function testGetPatchFileSerializerSupport() {
$configList = \Drupal::service('config_update.config_list');
$configReverter = \Drupal::service('config_update.config_update');
$configDiffer = \Drupal::service('update_helper.config_differ');
$configDiffTransformer = \Drupal::service('update_helper.config_diff_transformer');
$moduleHandler = \Drupal::service('module_handler');
$configExporter = \Drupal::service('update_helper.config_exporter');
$configHandlerYaml = new ConfigHandler($configList, $configReverter, $configDiffer, $configDiffTransformer, $moduleHandler, \Drupal::service('serialization.yaml'), $configExporter);
$this
->assertStringEndsWith('config_handler_test.yml', $configHandlerYaml
->getPatchFile('update_helper', 'config_handler_test'));
$configHandlerJson = new ConfigHandler($configList, $configReverter, $configDiffer, $configDiffTransformer, $moduleHandler, \Drupal::service('serialization.json'), $configExporter);
$this
->assertStringEndsWith('config_handler_test.json', $configHandlerJson
->getPatchFile('update_helper', 'config_handler_test'));
$configHandlerPhpSerialize = new ConfigHandler($configList, $configReverter, $configDiffer, $configDiffTransformer, $moduleHandler, \Drupal::service('serialization.phpserialize'), $configExporter);
$this
->assertStringEndsWith('config_handler_test.serialized', $configHandlerPhpSerialize
->getPatchFile('update_helper', 'config_handler_test'));
}
}