View source
<?php
namespace Drupal\Tests\webform\Functional;
use Drupal\webform\Entity\Webform;
class WebformResultsExportOptionsTest extends WebformBrowserTestBase {
public static $modules = [
'node',
'locale',
'webform',
'webform_test_submissions',
];
protected static $testWebforms = [
'test_submissions',
];
public function testExportOptions() {
$admin_submission_user = $this
->drupalCreateUser([
'administer webform submission',
]);
$webform = Webform::load('test_submissions');
$submissions = array_values(\Drupal::entityTypeManager()
->getStorage('webform_submission')
->loadByProperties([
'webform_id' => 'test_submissions',
]));
$nodes = array_values(\Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties([
'type' => 'webform_test_submissions',
]));
$this
->drupalLogin($admin_submission_user);
$this
->getExport($webform);
$this
->assertRaw('"First name","Last name"');
$this
->assertRaw('George,Washington');
$this
->assertRaw('Abraham,Lincoln');
$this
->assertRaw('Hillary,Clinton');
$this
->assertRaw("quotes' \"\"", "html <markup>");
$this
->getExport($webform, [
'delimiter' => '|',
]);
$this
->assertRaw('"First name"|"Last name"');
$this
->assertRaw('George|Washington');
$this
->getExport($webform, [
'header_format' => 'label',
]);
$this
->assertRaw('"First name","Last name"');
$this
->getExport($webform, [
'header_format' => 'key',
]);
$this
->assertRaw('first_name,last_name');
$this
->getExport($webform, [
'options_single_format' => 'compact',
'options_multiple_format' => 'compact',
]);
$this
->assertRaw('"Flag colors"');
$this
->assertRaw('Red;White;Blue');
$this
->getExport($webform, [
'options_single_format' => 'separate',
'options_multiple_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($webform, [
'options_item_format' => 'label',
]);
$this
->assertRaw('Red;White;Blue');
$this
->getExport($webform, [
'options_item_format' => 'key',
]);
$this
->assertNoRaw('Red;White;Blue');
$this
->assertRaw('red;white;blue');
$this
->getExport($webform, [
'multiple_delimiter' => '|',
]);
$this
->assertRaw('Red|White|Blue');
$this
->getExport($webform, [
'multiple_delimiter' => ',',
]);
$this
->assertRaw('"Red,White,Blue"');
$this
->getExport($webform, [
'entity_reference_items' => 'id,title,url',
]);
$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($webform, [
'entity_reference_items' => 'id',
]);
$this
->getExport($webform, [
'entity_reference_items' => 'title,url',
]);
$this
->assertNoRaw('"Favorite node: ID","Favorite node: Title","Favorite node: URL"');
$this
->assertNoRaw('' . $nodes[0]
->id() . ',"' . $nodes[0]
->label() . '",' . $nodes[0]
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString());
$this
->assertRaw('"Favorite node: Title","Favorite node: URL"');
$this
->assertRaw('"' . $nodes[0]
->label() . '",' . $nodes[0]
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString());
$this
->getExport($webform, [
'header_format' => 'label',
]);
$this
->assertRaw('"Likert: Question 1","Likert: Question 2","Likert: Question 3"');
$this
->getExport($webform, [
'header_format' => 'key',
]);
$this
->assertNoRaw('"Likert: Question 1","Likert: Question 2","Likert: Question 3"');
$this
->assertRaw('likert__q1,likert__q2,likert__q3');
$this
->getExport($webform, [
'likert_answers_format' => 'label',
]);
$this
->assertRaw('"Answer 1","Answer 1","Answer 1"');
$this
->getExport($webform, [
'likert_answers_format' => 'key',
]);
$this
->assertNoRaw('"Option 1","Option 1","Option 1"');
$this
->assertRaw('1,1,1');
$this
->getExport($webform, [
'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($webform, [
'header_format' => 'label',
'header_prefix' => FALSE,
]);
$this
->assertRaw('Address,"Address 2",City/Town,State/Province,"ZIP/Postal Code",Country');
$this
->getExport($webform, [
'range_type' => 'latest',
'range_latest' => 2,
]);
$this
->assertRaw('Hillary,Clinton');
$this
->assertNoRaw('George,Washington');
$this
->assertNoRaw('Abraham,Lincoln');
$this
->getExport($webform, [
'order' => 'asc',
]);
$this
->assertPattern('/George.*Abraham.*Hillary/ms');
$this
->getExport($webform, [
'order' => 'desc',
]);
$this
->assertPattern('/Hillary.*Abraham.*George/ms');
$this
->getExport($webform, [
'range_type' => 'sid',
'range_start' => $submissions[1]
->id(),
]);
$this
->assertNoRaw('George,Washington');
$this
->assertRaw('Abraham,Lincoln');
$this
->assertRaw('Hillary,Clinton');
$this
->getExport($webform, [
'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]
->setOwner($admin_submission_user)
->save();
$this
->getExport($webform, [
'uid' => $admin_submission_user
->id(),
]);
$this
->assertRaw('George,Washington');
$this
->assertNoRaw('Abraham,Lincoln');
$this
->assertNoRaw('Hillary,Clinton');
$this
->getExport($webform, [
'range_type' => 'date',
'range_start' => '2000-01-01',
'range_end' => '2001-01-01',
]);
$this
->assertRaw('George,Washington');
$this
->assertRaw('Abraham,Lincoln');
$this
->assertNoRaw('Hillary,Clinton');
$this
->drupalGet('/admin/structure/webform/manage/' . $webform
->id() . '/results/download');
$this
->assertNoFieldById('edit-entity-type', NULL);
$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/webform/manage/' . $webform
->id() . '/results/download');
$this
->assertFieldById('edit-entity-type', NULL);
$this
->getExport($webform, [
'entity_type' => 'user',
]);
$this
->assertRaw('George,Washington');
$this
->assertRaw('Abraham,Lincoln');
$this
->assertNoRaw('Hillary,Clinton');
$this
->getExport($webform, [
'entity_type' => 'user',
'entity_id' => '1',
]);
$this
->assertRaw('George,Washington');
$this
->assertNoRaw('Abraham,Lincoln');
$this
->assertNoRaw('Hillary,Clinton');
$this
->drupalLogin($this->rootUser);
$edit = [
'exporter' => 'table',
];
$this
->drupalPostForm('/admin/structure/webform/manage/' . $webform
->id() . '/results/download', $edit, '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');
$edit = [
'exporter' => 'delimited',
'exporters[delimited][delimiter]' => '|',
];
$this
->drupalPostForm('/admin/structure/webform/config/exporters', $edit, 'Save configuration');
$this
->drupalPostForm('/admin/structure/webform/manage/' . $webform
->id() . '/results/download', [], 'Download');
$this
->assertRaw('"Submission ID"|"Submission URI"');
$edit = [
'exporter' => 'delimited',
'exporters[delimited][delimiter]' => '.',
];
$this
->drupalPostForm('/admin/structure/webform/manage/' . $webform
->id() . '/results/download', $edit, 'Save settings');
$this
->drupalPostForm('/admin/structure/webform/manage/' . $webform
->id() . '/results/download', [], 'Download');
$this
->assertRaw('"Submission ID"."Submission URI"');
$this
->drupalPostForm('/admin/structure/webform/manage/' . $webform
->id() . '/results/download', [], 'Reset settings');
$this
->drupalPostForm('/admin/structure/webform/manage/' . $webform
->id() . '/results/download', [], 'Download');
$this
->assertRaw('"Submission ID"|"Submission URI"');
}
}