You are here

public function WebformResultsExportDownloadTest::testDownloadFiles in Webform 8.5

Same name and namespace in other branches
  1. 6.x tests/src/Functional/WebformResultsExportDownloadTest.php \Drupal\Tests\webform\Functional\WebformResultsExportDownloadTest::testDownloadFiles()

Tests download files.

File

tests/src/Functional/WebformResultsExportDownloadTest.php, line 33

Class

WebformResultsExportDownloadTest
Tests for webform results export download.

Namespace

Drupal\Tests\webform\Functional

Code

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

  /** @var \Drupal\webform\WebformInterface $webform_managed_file */
  $webform_managed_file = Webform::load('test_element_managed_file');

  /** @var \Drupal\webform\WebformSubmissionExporterInterface $submission_exporter */
  $submission_exporter = \Drupal::service('webform_submission.exporter');
  $submission_exporter
    ->setWebform($webform_managed_file);
  $submission_exporter
    ->setExporter();
  $sids = [];
  $sids[] = $this
    ->postSubmissionTest($webform_managed_file);
  $sids[] = $this
    ->postSubmissionTest($webform_managed_file);
  $sids[] = $this
    ->postSubmissionTest($webform_managed_file);
  $archive_types = [
    'tar',
    'zip',
  ];
  foreach ($archive_types as $archive_type) {

    // Set exporter archive type.
    $submission_exporter
      ->setExporter([
      'archive_type' => $archive_type,
    ]);

    /* Download CSV */

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

    // Load the archive and get a list of files.
    $files = $this
      ->getArchiveContents($submission_exporter
      ->getArchiveFilePath());

    // Check that CSV file exists.
    $this
      ->assertArrayHasKey('test_element_managed_file/test_element_managed_file.csv', $files);

    // Check submission file directories.

    /** @var \Drupal\webform\WebformSubmissionInterface[] $submissions */
    $submissions = WebformSubmission::loadMultiple($sids);
    foreach ($submissions as $submission) {
      $serial = $submission
        ->serial();
      $fid = $submission
        ->getElementData('managed_file_single');
      $filename = File::load($fid)
        ->getFilename();
      $this
        ->assertArrayHasKey("submission-{$serial}/{$filename}", $files);
    }

    /* Download YAML */

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

    // Load the archive and get a list of files.
    $files = $this
      ->getArchiveContents($submission_exporter
      ->getArchiveFilePath());

    // Check that CSV file does not exists.
    $this
      ->assertArrayNotHasKey('test_element_managed_file/test_element_managed_file.csv', $files);

    // Check submission file directories.

    /** @var \Drupal\webform\WebformSubmissionInterface[] $submissions */
    $submissions = WebformSubmission::loadMultiple($sids);
    foreach ($submissions as $submission) {
      $serial = $submission
        ->serial();
      $fid = $submission
        ->getElementData('managed_file_single');
      $filename = File::load($fid)
        ->getFilename();
      $this
        ->assertArrayHasKey("submission-{$serial}.yml", $files);
      $this
        ->assertArrayHasKey("submission-{$serial}/{$filename}", $files);
    }
  }
}