You are here

public function YamlFormResultsExportTest::testDownloadFiles in YAML Form 8

Tests download files.

File

src/Tests/YamlFormResultsExportTest.php, line 27

Class

YamlFormResultsExportTest
Tests for form results export.

Namespace

Drupal\yamlform\Tests

Code

public function testDownloadFiles() {
  $this
    ->drupalLogin($this->adminFormUser);

  /** @var \Drupal\yamlform\YamlFormInterface $yamlform_managed_file */
  $yamlform_managed_file = YamlForm::load('test_element_managed_file');

  /** @var \Drupal\yamlform\YamlFormSubmissionExporterInterface $submission_exporter */
  $submission_exporter = \Drupal::service('yamlform_submission.exporter');
  $submission_exporter
    ->setYamlForm($yamlform_managed_file);
  $submission_exporter
    ->setExporter();
  $sids = [];
  $sids[] = $this
    ->postSubmissionTest($yamlform_managed_file);
  $sids[] = $this
    ->postSubmissionTest($yamlform_managed_file);
  $sids[] = $this
    ->postSubmissionTest($yamlform_managed_file);

  /* Download CSV */

  // Download tar ball archive with CSV.
  $edit = [
    'export[download][files]' => TRUE,
  ];
  $this
    ->drupalPostForm('admin/structure/yamlform/manage/test_element_managed_file/results/download', $edit, t('Download'));

  // Load the tar and get a list of files.
  $tar = new ArchiveTar($submission_exporter
    ->getArchiveFilePath(), 'gz');
  $files = [];
  $content_list = $tar
    ->listContent();
  foreach ($content_list as $file) {
    $files[$file['filename']] = $file['filename'];
  }

  // Check that CSV file exists.
  $this
    ->assert(isset($files['test_element_managed_file/test_element_managed_file.csv']));

  // Check submission file directories.

  /** @var \Drupal\yamlform\YamlFormSubmissionInterface[] $submissions */
  $submissions = YamlFormSubmission::loadMultiple($sids);
  foreach ($submissions as $submission) {
    $serial = $submission
      ->serial();
    $fid = $submission
      ->getData('managed_file_single');
    $filename = File::load($fid)
      ->getFilename();
    $this
      ->assert(isset($files["submission-{$serial}/{$filename}"]));
  }

  /* Download YAML */

  // Download tar ball archive with YAML documents.
  $edit = [
    'export[download][files]' => TRUE,
    'export[format][exporter]' => 'yaml',
  ];
  $this
    ->drupalPostForm('admin/structure/yamlform/manage/test_element_managed_file/results/download', $edit, t('Download'));

  // Load the tar and get a list of files.
  $tar = new ArchiveTar($submission_exporter
    ->getArchiveFilePath(), 'gz');
  $files = [];
  $content_list = $tar
    ->listContent();
  foreach ($content_list as $file) {
    $files[$file['filename']] = $file['filename'];
  }

  // Check that CSV file does not exists.
  $this
    ->assert(!isset($files['test_element_managed_file/test_element_managed_file.csv']));

  // Check submission file directories.

  /** @var \Drupal\yamlform\YamlFormSubmissionInterface[] $submissions */
  $submissions = YamlFormSubmission::loadMultiple($sids);
  foreach ($submissions as $submission) {
    $serial = $submission
      ->serial();
    $fid = $submission
      ->getData('managed_file_single');
    $filename = File::load($fid)
      ->getFilename();
    $this
      ->assert(isset($files["submission-{$serial}.yml"]));
    $this
      ->assert(isset($files["submission-{$serial}/{$filename}"]));
  }
}