You are here

public function ConfigHandlerTest::testGeneratePatchFileWithConfigExport in Update helper 8

Same name and namespace in other branches
  1. 2.x tests/src/Kernel/ConfigHandlerTest.php \Drupal\Tests\update_helper\Kernel\ConfigHandlerTest::testGeneratePatchFileWithConfigExport()

@covers \Drupal\update_helper\ConfigHandler::generatePatchFile

File

tests/src/Kernel/ConfigHandlerTest.php, line 152

Class

ConfigHandlerTest
Automated tests for ConfigName class.

Namespace

Drupal\Tests\update_helper\Kernel

Code

public function testGeneratePatchFileWithConfigExport() {

  /** @var \Drupal\update_helper\ConfigHandler $configHandler */
  $configHandler = \Drupal::service('update_helper.config_handler');

  /** @var \Drupal\Component\Serialization\SerializationInterface $yamlSerializer */
  $yamlSerializer = \Drupal::service('serialization.yaml');

  /** @var \Drupal\Core\Config\FileStorage $extensionStorage */
  $extensionStorage = \Drupal::service('config_update.extension_storage');
  $configFilePath = $extensionStorage
    ->getFilePath('field.storage.node.body');

  /** @var \Drupal\config_update\ConfigRevertInterface $configReverter */
  $configReverter = \Drupal::service('config_update.config_update');
  $configReverter
    ->import('field_storage_config', 'node.body');

  /** @var \Drupal\Core\Config\ConfigFactory $configFactory */
  $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);

  // Check file configuration before export.
  $fileData = $yamlSerializer
    ->decode(file_get_contents($configFilePath));
  $this
    ->assertEqual('text_with_summary', $fileData['type']);
  $this
    ->assertEqual([], $fileData['settings']);

  // Generate patch and export config after configuration change.
  $data = $configHandler
    ->generatePatchFile([
    'node',
  ], FALSE);
  $this
    ->assertEqual('field.storage.node.body:' . PHP_EOL . '  expected_config:' . PHP_EOL . '    settings: {  }' . PHP_EOL . '    type: text_with_summary' . PHP_EOL . '  update_actions:' . PHP_EOL . '    change:' . PHP_EOL . '      settings:' . PHP_EOL . '        max_length: 321' . PHP_EOL . '      type: text' . PHP_EOL, $data);

  // Check newly exported configuration.
  $fileData = $yamlSerializer
    ->decode(file_get_contents($configFilePath));
  $this
    ->assertEqual('text', $fileData['type']);
  $this
    ->assertEqual([
    'max_length' => 321,
  ], $fileData['settings']);
}