function ConfigExportCommandTest::testExportCommand

Tests export of configuration.

File

core/tests/Drupal/KernelTests/Core/Config/ConfigExportCommandTest.php, line 88

Class

ConfigExportCommandTest
Tests the 'config:export' command.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testExportCommand() : void {
  $active = $this->container
    ->get('config.storage');
  $sync = $this->container
    ->get('config.storage.sync');
  // Ensure the override is in effect.
  self::assertSame('Foo', \Drupal::config('system.maintenance')->get('message'));
  // Before the first export the config is the same.
  self::assertEmpty($sync->listAll());
  // Execute it with --yes.
  $tester = $this->commandTester();
  $code = $tester->execute([
    '--yes' => TRUE,
  ]);
  $display = $tester->getDisplay();
  self::assertSame(Command::SUCCESS, $code);
  self::assertStringContainsString('config_test.dynamic.dotted.default', $display);
  self::assertStringContainsString('Configuration successfully exported', $display);
  // Ensure the override was not exported.
  $this->assertNotSame('Foo', $sync->read('system.maintenance')['message']);
  // The sync storage and the active are now the same.
  self::assertStorageEquals($active, $sync);
  // Run the command again. We do not expect a confirmation question.
  $tester = $this->commandTester();
  $code = $tester->execute([]);
  self::assertSame(Command::SUCCESS, $code);
  // Nothing changed, normalize tester output because the string is long.
  $display = preg_replace('/\\s+/', ' ', $tester->getDisplay(TRUE));
  self::assertStringContainsString('The active configuration to export matches the configuration in the sync directory.', $display);
  // Back up the sync storage.
  $expected = new MemoryStorage();
  $this->copyConfig($sync, $expected);
  // Install the transformation test module.
  $this->enableModules([
    'config_transformer_test',
  ]);
  $this->config('system.site')
    ->set('name', 'dr transformed')
    ->set('slogan', 'dr not transformed for pirates')
    ->save();
  // Reset the export storage so that it does a transformation again.
  (function () {
    unset($this->storage);
  })
    ->call($this->container
    ->get('config.storage.export'));
  // Execute the command but cancel the export.
  $tester = $this->commandTester();
  $tester->setInputs([
    'no',
  ]);
  try {
    $tester->execute([], [
      'interactive' => TRUE,
    ]);
    $this->fail('Expected a UserAbortException to be thrown.');
  } catch (UserAbortException) {
    // Expected.
  }
  // The sync storage remains intact.
  self::assertStorageEquals($expected, $sync);
  // Execute the command and answer yes.
  $tester = $this->commandTester();
  $tester->setInputs([
    'yes',
  ]);
  $code = $tester->execute([], [
    'interactive' => TRUE,
  ]);
  self::assertSame(Command::SUCCESS, $code);
  // The test transformer transforms the slogan on export.
  $exported_site = $sync->read('system.site');
  self::assertSame('dr transformed', $exported_site['name']);
  self::assertSame('dr config for export Arrr', $exported_site['slogan']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.