You are here

public function YamlFormResultsExportTest::testExportOptions in YAML Form 8

Tests export options.

File

src/Tests/YamlFormResultsExportTest.php, line 107

Class

YamlFormResultsExportTest
Tests for form results export.

Namespace

Drupal\yamlform\Tests

Code

public function testExportOptions() {

  /** @var \Drupal\yamlform\YamlFormInterface $yamlform */

  /** @var \Drupal\yamlform\YamlFormSubmissionInterface[] $submissions */
  list($yamlform, $submissions) = $this
    ->createYamlFormWithSubmissions();
  $this
    ->drupalLogin($this->adminSubmissionUser);

  // Check default options.
  $this
    ->getExport($yamlform);
  $this
    ->assertRaw('"First name","Last name"');
  $this
    ->assertRaw('George,Washington');
  $this
    ->assertRaw('Abraham,Lincoln');
  $this
    ->assertRaw('Hillary,Clinton');

  // Check delimiter.
  $this
    ->getExport($yamlform, [
    'delimiter' => '|',
  ]);
  $this
    ->assertRaw('"First name"|"Last name"');
  $this
    ->assertRaw('George|Washington');

  // Check header keys = label.
  $this
    ->getExport($yamlform, [
    'header_format' => 'label',
  ]);
  $this
    ->assertRaw('"First name","Last name"');

  // Check header keys = key.
  $this
    ->getExport($yamlform, [
    'header_format' => 'key',
  ]);
  $this
    ->assertRaw('first_name,last_name');

  // Check options format compact.
  $this
    ->getExport($yamlform, [
    'options_format' => 'compact',
  ]);
  $this
    ->assertRaw('"Flag colors"');
  $this
    ->assertRaw('"Red,White,Blue"');

  // Check options format separate.
  $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"');

  // Check options item format label.
  $this
    ->getExport($yamlform, [
    'options_item_format' => 'label',
  ]);
  $this
    ->assertRaw('"Red,White,Blue"');

  // Check options item format key.
  $this
    ->getExport($yamlform, [
    'options_item_format' => 'key',
  ]);
  $this
    ->assertNoRaw('"Red,White,Blue"');
  $this
    ->assertRaw('"red,white,blue"');

  // Check entity reference format link.
  $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());

  // Check entity reference format id.
  $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());

  // Check likert questions format label.
  $this
    ->getExport($yamlform, [
    'header_format' => 'label',
  ]);
  $this
    ->assertRaw('"Likert: Question 1","Likert: Question 2","Likert: Question 3"');

  // Check likert questions format key.
  $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');

  // Check likert answers format label.
  $this
    ->getExport($yamlform, [
    'likert_answers_format' => 'label',
  ]);
  $this
    ->assertRaw('"Answer 1","Answer 1","Answer 1"');

  // Check likert answers format key.
  $this
    ->getExport($yamlform, [
    'likert_answers_format' => 'key',
  ]);
  $this
    ->assertNoRaw('"Option 1","Option 1","Option 1"');
  $this
    ->assertRaw('1,1,1');

  // Check composite w/o header prefix.
  $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"');

  // Check composite w header prefix.
  $this
    ->getExport($yamlform, [
    'header_format' => 'label',
    'header_prefix' => FALSE,
  ]);
  $this
    ->assertRaw('Address,"Address 2",City/Town,State/Province,"Zip/Postal Code",Country');

  // Check limit.
  $this
    ->getExport($yamlform, [
    'range_type' => 'latest',
    'range_latest' => 1,
  ]);
  $this
    ->assertRaw('Hillary,Clinton');
  $this
    ->assertNoRaw('George,Washington');
  $this
    ->assertNoRaw('Abraham,Lincoln');

  // Check sid start.
  $this
    ->getExport($yamlform, [
    'range_type' => 'sid',
    'range_start' => $submissions[1]
      ->id(),
  ]);
  $this
    ->assertNoRaw('George,Washington');
  $this
    ->assertRaw('Abraham,Lincoln');
  $this
    ->assertRaw('Hillary,Clinton');

  // Check sid range.
  $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');

  // Check date range.
  $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');

  // Check entity type and id hidden.
  $this
    ->drupalGet('admin/structure/yamlform/manage/' . $yamlform
    ->id() . '/results/download');
  $this
    ->assertNoFieldById('edit-export-download-submitted-entity-type');

  // Change submission 0 & 1 to be submitted user account.
  $submissions[0]
    ->set('entity_type', 'user')
    ->set('entity_id', '1')
    ->save();
  $submissions[1]
    ->set('entity_type', 'user')
    ->set('entity_id', '2')
    ->save();

  // Check entity type and id visible.
  $this
    ->drupalGet('admin/structure/yamlform/manage/' . $yamlform
    ->id() . '/results/download');
  $this
    ->assertFieldById('edit-export-download-submitted-entity-type');

  // Check entity type limit.
  $this
    ->getExport($yamlform, [
    'entity_type' => 'user',
  ]);
  $this
    ->assertRaw('George,Washington');
  $this
    ->assertRaw('Abraham,Lincoln');
  $this
    ->assertNoRaw('Hillary,Clinton');

  // Check entity type and id limit.
  $this
    ->getExport($yamlform, [
    'entity_type' => 'user',
    'entity_id' => '1',
  ]);
  $this
    ->assertRaw('George,Washington');
  $this
    ->assertNoRaw('Abraham,Lincoln');
  $this
    ->assertNoRaw('Hillary,Clinton');

  // Check changing default exporter to 'table' settings.
  $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');

  // Check changing default export (delimiter) settings.
  $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"');

  // Check saved form export (delimiter) settings.
  $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"');

  // Check delete form export (delimiter) settings.
  $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"');
}