View source
<?php
namespace Drupal\yamlform\Tests;
use Drupal\Core\Archiver\ArchiveTar;
use Drupal\file\Entity\File;
use Drupal\yamlform\Entity\YamlForm;
use Drupal\yamlform\Entity\YamlFormSubmission;
class YamlFormResultsExportTest extends YamlFormTestBase {
protected static $modules = [
'system',
'block',
'node',
'user',
'locale',
'yamlform',
'yamlform_test',
];
public function testDownloadFiles() {
$this
->drupalLogin($this->adminFormUser);
$yamlform_managed_file = YamlForm::load('test_element_managed_file');
$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);
$edit = [
'export[download][files]' => TRUE,
];
$this
->drupalPostForm('admin/structure/yamlform/manage/test_element_managed_file/results/download', $edit, t('Download'));
$tar = new ArchiveTar($submission_exporter
->getArchiveFilePath(), 'gz');
$files = [];
$content_list = $tar
->listContent();
foreach ($content_list as $file) {
$files[$file['filename']] = $file['filename'];
}
$this
->assert(isset($files['test_element_managed_file/test_element_managed_file.csv']));
$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}"]));
}
$edit = [
'export[download][files]' => TRUE,
'export[format][exporter]' => 'yaml',
];
$this
->drupalPostForm('admin/structure/yamlform/manage/test_element_managed_file/results/download', $edit, t('Download'));
$tar = new ArchiveTar($submission_exporter
->getArchiveFilePath(), 'gz');
$files = [];
$content_list = $tar
->listContent();
foreach ($content_list as $file) {
$files[$file['filename']] = $file['filename'];
}
$this
->assert(!isset($files['test_element_managed_file/test_element_managed_file.csv']));
$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}"]));
}
}
public function testExportOptions() {
list($yamlform, $submissions) = $this
->createYamlFormWithSubmissions();
$this
->drupalLogin($this->adminSubmissionUser);
$this
->getExport($yamlform);
$this
->assertRaw('"First name","Last name"');
$this
->assertRaw('George,Washington');
$this
->assertRaw('Abraham,Lincoln');
$this
->assertRaw('Hillary,Clinton');
$this
->getExport($yamlform, [
'delimiter' => '|',
]);
$this
->assertRaw('"First name"|"Last name"');
$this
->assertRaw('George|Washington');
$this
->getExport($yamlform, [
'header_format' => 'label',
]);
$this
->assertRaw('"First name","Last name"');
$this
->getExport($yamlform, [
'header_format' => 'key',
]);
$this
->assertRaw('first_name,last_name');
$this
->getExport($yamlform, [
'options_format' => 'compact',
]);
$this
->assertRaw('"Flag colors"');
$this
->assertRaw('"Red,White,Blue"');
$this
->getExport($yamlform, [
'options_format' => 'separate',
]);
$this
->assertRaw('"Flag colors: Red","Flag colors: White","Flag colors: Blue"');
$this
->assertNoRaw('"Flag colors"');
$this
->assertRaw('X,X,X');
$this
->assertNoRaw('"Red,White,Blue"');
$this
->getExport($yamlform, [
'options_item_format' => 'label',
]);
$this
->assertRaw('"Red,White,Blue"');
$this
->getExport($yamlform, [
'options_item_format' => 'key',
]);
$this
->assertNoRaw('"Red,White,Blue"');
$this
->assertRaw('"red,white,blue"');
$nodes = $this
->getNodes();
$this
->getExport($yamlform, [
'entity_reference_format' => 'link',
]);
$this
->assertRaw('"Favorite node: ID","Favorite node: Title","Favorite node: URL"');
$this
->assertRaw('' . $nodes[0]
->id() . ',"' . $nodes[0]
->label() . '",' . $nodes[0]
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString());
$this
->getExport($yamlform, [
'entity_reference_format' => 'id',
]);
$this
->assertRaw('"Favorite node"');
$this
->assertNoRaw('"Favorite node Title","Favorite node ID","Favorite node URL"');
$this
->assertRaw(',node:' . $nodes[0]
->id() . ',');
$this
->assertNoRaw('"' . $nodes[0]
->label() . '",' . $nodes[0]
->id() . ',' . $nodes[0]
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString());
$this
->getExport($yamlform, [
'header_format' => 'label',
]);
$this
->assertRaw('"Likert: Question 1","Likert: Question 2","Likert: Question 3"');
$this
->getExport($yamlform, [
'header_format' => 'key',
]);
$this
->assertNoRaw('"Likert: Question 1","Likert: Question 2","Likert: Question 3"');
$this
->assertRaw('likert__q1,likert__q2,likert__q3');
$this
->getExport($yamlform, [
'likert_answers_format' => 'label',
]);
$this
->assertRaw('"Answer 1","Answer 1","Answer 1"');
$this
->getExport($yamlform, [
'likert_answers_format' => 'key',
]);
$this
->assertNoRaw('"Option 1","Option 1","Option 1"');
$this
->assertRaw('1,1,1');
$this
->getExport($yamlform, [
'header_format' => 'label',
'header_prefix' => TRUE,
]);
$this
->assertRaw('"Address: Address","Address: Address 2","Address: City/Town","Address: State/Province","Address: Zip/Postal Code","Address: Country"');
$this
->getExport($yamlform, [
'header_format' => 'label',
'header_prefix' => FALSE,
]);
$this
->assertRaw('Address,"Address 2",City/Town,State/Province,"Zip/Postal Code",Country');
$this
->getExport($yamlform, [
'range_type' => 'latest',
'range_latest' => 1,
]);
$this
->assertRaw('Hillary,Clinton');
$this
->assertNoRaw('George,Washington');
$this
->assertNoRaw('Abraham,Lincoln');
$this
->getExport($yamlform, [
'range_type' => 'sid',
'range_start' => $submissions[1]
->id(),
]);
$this
->assertNoRaw('George,Washington');
$this
->assertRaw('Abraham,Lincoln');
$this
->assertRaw('Hillary,Clinton');
$this
->getExport($yamlform, [
'range_type' => 'sid',
'range_start' => $submissions[1]
->id(),
'range_end' => $submissions[1]
->id(),
]);
$this
->assertNoRaw('George,Washington');
$this
->assertRaw('Abraham,Lincoln');
$this
->assertNoRaw('Hillary,Clinton');
$submissions[0]
->set('created', strtotime('1/01/2000'))
->save();
$submissions[1]
->set('created', strtotime('1/01/2001'))
->save();
$submissions[2]
->set('created', strtotime('1/01/2002'))
->save();
$this
->getExport($yamlform, [
'range_type' => 'date',
'range_start' => '12/31/2000',
'range_end' => '12/31/2001',
]);
$this
->assertNoRaw('George,Washington');
$this
->assertRaw('Abraham,Lincoln');
$this
->assertNoRaw('Hillary,Clinton');
$this
->drupalGet('admin/structure/yamlform/manage/' . $yamlform
->id() . '/results/download');
$this
->assertNoFieldById('edit-export-download-submitted-entity-type');
$submissions[0]
->set('entity_type', 'user')
->set('entity_id', '1')
->save();
$submissions[1]
->set('entity_type', 'user')
->set('entity_id', '2')
->save();
$this
->drupalGet('admin/structure/yamlform/manage/' . $yamlform
->id() . '/results/download');
$this
->assertFieldById('edit-export-download-submitted-entity-type');
$this
->getExport($yamlform, [
'entity_type' => 'user',
]);
$this
->assertRaw('George,Washington');
$this
->assertRaw('Abraham,Lincoln');
$this
->assertNoRaw('Hillary,Clinton');
$this
->getExport($yamlform, [
'entity_type' => 'user',
'entity_id' => '1',
]);
$this
->assertRaw('George,Washington');
$this
->assertNoRaw('Abraham,Lincoln');
$this
->assertNoRaw('Hillary,Clinton');
$this
->drupalLogin($this->adminFormUser);
$this
->drupalPostForm('admin/structure/yamlform/manage/' . $yamlform
->id() . '/results/download', [
'export[format][exporter]' => 'table',
], t('Download'));
$this
->assertRaw('<body><table border="1"><thead><tr bgcolor="#cccccc" valign="top"><th>Serial number</th>');
$this
->assertPattern('#<td>George</td>\\s+<td>Washington</td>\\s+<td>Male</td>#ms');
$this
->drupalLogin($this->adminFormUser);
$this
->drupalPostForm('admin/structure/yamlform/settings', [
'export[format][delimiter]' => '|',
], t('Save configuration'));
$this
->drupalPostForm('admin/structure/yamlform/manage/' . $yamlform
->id() . '/results/download', [], t('Download'));
$this
->assertRaw('"Submission ID"|"Submission URI"');
$this
->drupalPostForm('admin/structure/yamlform/manage/' . $yamlform
->id() . '/results/download', [
'export[format][delimiter]' => '.',
], t('Save settings'));
$this
->drupalPostForm('admin/structure/yamlform/manage/' . $yamlform
->id() . '/results/download', [], t('Download'));
$this
->assertRaw('"Submission ID"."Submission URI"');
$this
->drupalPostForm('admin/structure/yamlform/manage/' . $yamlform
->id() . '/results/download', [], t('Reset settings'));
$this
->drupalPostForm('admin/structure/yamlform/manage/' . $yamlform
->id() . '/results/download', [], t('Download'));
$this
->assertRaw('"Submission ID"|"Submission URI"');
}
}