You are here

public function WebformResultsExportDownloadTest::testDownloadFiles in Webform 6.x

Same name and namespace in other branches
  1. 8.5 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 */
  $webform = Webform::load('test_exporter_archive');

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

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

    /* Download CSV */

    // Download archive with CSV (delimited).
    $edit = [
      'exporter' => 'delimited',
      'archive_type' => $test['archive_type'],
      'files' => $test['files'],
      'attachments' => $test['attachments'],
    ];
    $this
      ->drupalPostForm('/admin/structure/webform/manage/test_exporter_archive/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
      ->debug($files);
    $this
      ->assertArrayHasKey('test_exporter_archive/test_exporter_archive.csv', $files);

    // Check submission file directories.

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

    /* Download YAML */

    // Download archive with YAML documents.
    $edit = [
      'exporter' => 'yaml',
      'archive_type' => $test['archive_type'],
      'files' => $test['files'],
      'attachments' => $test['attachments'],
    ];
    $this
      ->drupalPostForm('/admin/structure/webform/manage/test_exporter_archive/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_exporter_archive/test_exporter_archive.csv', $files);

    // Check submission file directories.

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