WebformResultsExportDownloadTest.php in Webform 8.5
File
tests/src/Functional/WebformResultsExportDownloadTest.php
View source
<?php
namespace Drupal\Tests\webform\Functional;
use Drupal\file\Entity\File;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
class WebformResultsExportDownloadTest extends WebformBrowserTestBase {
public static $modules = [
'node',
'locale',
'webform',
];
protected static $testWebforms = [
'test_element_managed_file',
];
public function testDownloadFiles() {
$this
->drupalLogin($this->rootUser);
$webform_managed_file = Webform::load('test_element_managed_file');
$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) {
$submission_exporter
->setExporter([
'archive_type' => $archive_type,
]);
$edit = [
'files' => TRUE,
'archive_type' => $archive_type,
];
$this
->drupalPostForm('/admin/structure/webform/manage/test_element_managed_file/results/download', $edit, 'Download');
$files = $this
->getArchiveContents($submission_exporter
->getArchiveFilePath());
$this
->assertArrayHasKey('test_element_managed_file/test_element_managed_file.csv', $files);
$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);
}
$edit = [
'files' => TRUE,
'exporter' => 'yaml',
'archive_type' => $archive_type,
];
$this
->drupalPostForm('/admin/structure/webform/manage/test_element_managed_file/results/download', $edit, 'Download');
$files = $this
->getArchiveContents($submission_exporter
->getArchiveFilePath());
$this
->assertArrayNotHasKey('test_element_managed_file/test_element_managed_file.csv', $files);
$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);
}
}
}
protected function getArchiveContents($filepath) {
if (strpos($filepath, '.zip') !== FALSE) {
$archive = new \ZipArchive();
$archive
->open($filepath);
$files = [];
for ($i = 0; $i < $archive->numFiles; $i++) {
$files[] = $archive
->getNameIndex($i);
}
}
else {
$archive = new \Archive_Tar($filepath, 'gz');
$files = [];
foreach ($archive
->listContent() as $file_data) {
$files[] = $file_data['filename'];
}
}
return array_combine($files, $files);
}
}