You are here

protected function CSVExportViewsDataExportExporterTests::testEncodingOptions in Views data export 7.4

Test encoding options.

File

tests/exporter_tests/csv.test, line 261

Class

CSVExportViewsDataExportExporterTests

Code

protected function testEncodingOptions() {

  // A dataset with a non-ASCII UTF-8 character, to help us ensure UTF-8.
  // (As ASCII is a subset of UTF-8, php will detect as ASCII if there are
  // no UTF-8-only characters.)
  $utf_8_dataset = array(
    array(
      'name' => 'JƟhn',
      'age' => 25,
      'job' => 'Singer',
      'created' => gmmktime(0, 0, 0, 1, 1, 2000),
    ),
  );

  // The encodings we want to test, as form_input_value => expected_encoding.
  $encodings = array(
    'utf8_decode' => 'ISO-8859-1',
    'UTF-8' => 'UTF-8',
    //'illegal-encoding-name&*%$' => 'UTF-8',
    '' => 'UTF-8',
  );

  // Render content with each encoding.
  $row_count = 0;
  foreach ($encodings as $encoding => $expected) {
    $result = $this
      ->executeFullWrite($this
      ->getExporter(array(
      'separator' => ',',
      'encoding' => $encoding,
    )), $utf_8_dataset, $row_count, array(
      'name' => 'Name',
      'age' => 'Age',
      'job' => 'Job',
      'created' => 'Created',
    ));
    $row_count++;

    // Check the resulting encoding.
    $result_encoding = mb_detect_encoding($result);
    $encoding_match = in_array($result_encoding, array(
      $expected,
      'ASCII',
    ));
    $this
      ->assertTrue($encoding_match, 'Detected encoding ' . $result_encoding . ' matches ' . $expected . ' for input value ' . $encoding);
  }
}