You are here

public function ConfigSplitCliServiceTest::testVanillaExport in Configuration Split 8

Test that our export behaves the same as Drupal core without a split.

File

tests/src/Kernel/ConfigSplitCliServiceTest.php, line 56

Class

ConfigSplitCliServiceTest
Test the cli service.

Namespace

Drupal\Tests\config_split\Kernel

Code

public function testVanillaExport() {

  // Set the "current user" to have "export configuration" permission.
  $account = $this
    ->prophesize(AccountInterface::class);
  $account
    ->id()
    ->willReturn(27);
  $account
    ->hasPermission('export configuration')
    ->willReturn(TRUE);
  $this->container
    ->set('current_user', $account
    ->reveal());

  // Export the configuration the way drupal core does it.
  $configController = ConfigController::create($this->container);

  // Download and open the tar file.
  $file = $configController
    ->downloadExport()
    ->getFile()
    ->openFile();
  $archive_data = $file
    ->fread($file
    ->getSize());

  // Save the tar file to unpack and read it.
  // See \Drupal\config\Tests\ConfigExportUITest::testExport()
  $uri = $this->container
    ->get('file_system')
    ->saveData($archive_data, 'temporary://config.tar.gz');
  $temp_folder = $this->container
    ->get('file_system')
    ->getTempDirectory();
  $file_target = StreamWrapperManager::getTarget($uri);
  $file_path = $temp_folder . '/' . $file_target;
  $archiver = new Tar($file_path);
  $this
    ->assertNotEmpty($archiver
    ->listContents(), 'Downloaded archive file is not empty.');

  // Extract the zip to a virtual file system.
  $core_export = vfsStream::setup('core-export');
  $archiver
    ->extract($core_export
    ->url());
  $this
    ->assertNotEmpty($core_export
    ->getChildren(), 'Successfully extract archive.');

  // Set a new virtual file system for the split export.
  $split_export = vfsStream::setup('split-export');
  $primary = new FileStorage($split_export
    ->url());
  $this
    ->assertEmpty($split_export
    ->getChildren(), 'Before exporting the folder is empty.');

  // Do the export without a split configuration to the export folder.
  $this->container
    ->get('config_split.cli')
    ->export($primary);

  // Assert that the exported configuration is the same in both cases.
  $this
    ->assertEquals(count($core_export
    ->getChildren()), count($split_export
    ->getChildren()), 'The same amount of config is exported.');
  foreach ($core_export
    ->getChildren() as $child) {
    $name = $child
      ->getName();
    if ($child
      ->getType() == vfsStreamContent::TYPE_FILE) {

      // If it is a file we can compare the content.
      $this
        ->assertEquals($child
        ->getContent(), $split_export
        ->getChild($name)
        ->getContent(), 'The content of the exported file is the same.');
    }
  }
}