You are here

csv_export.test in Views data export 7.3

Same filename and directory in other branches
  1. 7.4 tests/csv_export.test

File

tests/csv_export.test
View source
<?php

class CSVExportViewsDataExportTests extends ViewsDataExportSimpleExportTest {
  protected $profile = 'testing';
  public static function getInfo() {
    return array(
      'name' => 'CSV Export',
      'description' => 'Various tests for exporting to CSV.',
      'group' => 'Views Data Export',
    );
  }
  protected $vde_export_type = 'CSV';
  protected function getStylePluginName() {
    return 'views_data_export_csv';
  }
  protected function getExportView($path = 'vde_test') {

    // Create the basic view.
    $view = $this
      ->getBasicExportView();
    $display = $view
      ->new_display('views_data_export', 'Data export', 'vde_test');
    $display
      ->override_option('style_plugin', $this
      ->getStylePluginName());
    $display
      ->override_option('path', $path);
    $expected = '"ID","Name","Age"
"1","John","25"
"2","George","27"
"3","Ringo","28"
"4","Paul","26"
"5","Meredith","30"';
    return array(
      &$view,
      $expected,
    );
  }

  /**
   * Test to ensure that HTML tags are kept in CSV files when requested.
   */
  protected function testKeepHTML() {
    $view = $this
      ->getBasicExportView();
    $display = $view->display['default']->handler;
    $display
      ->override_option('fields', array(
      'id' => array(
        'id' => 'id',
        'table' => 'views_test',
        'field' => 'id',
        'relationship' => 'none',
        // Add a label to include HTML
        'label' => '<strong id="id">ID</strong>',
      ),
      'name' => array(
        'id' => 'name',
        'table' => 'views_test',
        'field' => 'name',
        'relationship' => 'none',
        // Alter this field to include HTML.
        'alter' => array(
          'alter_text' => TRUE,
          'text' => '<em>[name]</em>',
        ),
      ),
      'age' => array(
        'id' => 'age',
        'table' => 'views_test',
        'field' => 'age',
        'relationship' => 'none',
      ),
    ));
    $style_options = array(
      'keep_html' => TRUE,
    );
    $expected = '"<strong id=""id"">ID</strong>","Name","Age"
"1","<em>John</em>","25"
"2","<em>George</em>","27"
"3","<em>Ringo</em>","28"
"4","<em>Paul</em>","26"
"5","<em>Meredith</em>","30"';
    $message = 'Keep HTML test in ' . $this->vde_export_type . ' export matched expected output.';
    $this
      ->executeAndCompareGivenView($view, $expected, $message, $style_options);

    // And now make sure that HTML tags are stripped correctly.
    $style_options = array(
      'keep_html' => FALSE,
    );
    $expected = '"ID","Name","Age"
"1","John","25"
"2","George","27"
"3","Ringo","28"
"4","Paul","26"
"5","Meredith","30"';
    $message = 'Keep HTML reverse test in ' . $this->vde_export_type . ' export matched expected output.';
    $this
      ->executeAndCompareGivenView($view, $expected, $message, $style_options);
  }

  /**
   * Test to ensure that HTML tags are kept in CSV files when requested.
   */
  protected function testTrimFields() {
    $view = $this
      ->getBasicExportView();
    $display = $view->display['default']->handler;
    $display
      ->override_option('fields', array(
      'id' => array(
        'id' => 'id',
        'table' => 'views_test',
        'field' => 'id',
        'relationship' => 'none',
        'label' => 'ID',
      ),
      'name' => array(
        'id' => 'name',
        'table' => 'views_test',
        'field' => 'name',
        'relationship' => 'none',
        // Alter this field to include HTML.
        'alter' => array(
          'alter_text' => TRUE,
          'text' => ' [name]  ',
        ),
      ),
      'age' => array(
        'id' => 'age',
        'table' => 'views_test',
        'field' => 'age',
        'relationship' => 'none',
      ),
    ));
    $style_options = array(
      'trim' => FALSE,
    );
    $expected = '"ID","Name","Age"
"1"," John  ","25"
"2"," George  ","27"
"3"," Ringo  ","28"
"4"," Paul  ","26"
"5"," Meredith  ","30"';
    $message = 'Trim reverse test in ' . $this->vde_export_type . ' export matched expected output.';
    $this
      ->executeAndCompareGivenView($view, $expected, $message, $style_options);

    // And now make sure that trimming works as expected.
    $style_options = array(
      'trim' => TRUE,
    );
    $expected = '"ID","Name","Age"
"1","John","25"
"2","George","27"
"3","Ringo","28"
"4","Paul","26"
"5","Meredith","30"';
    $message = 'Trim test in ' . $this->vde_export_type . ' export matched expected output.';
    $this
      ->executeAndCompareGivenView($view, $expected, $message, $style_options);
  }

  /**
   * Test to ensure that Encoding options work.
   */
  protected function testIconvEncoding() {
    $view = $this
      ->getBasicExportView();
    db_update('views_test')
      ->fields(array(
      'name' => 'Jo€hn',
    ))
      ->condition('name', 'John')
      ->execute();
    $encodings = array(
      'ISO-8859-1//TRANSLIT' => 'EUR',
      'utf8_decode' => '?',
    );
    foreach ($encodings as $encoding => $conversion) {
      $style_options = array(
        'encoding' => $encoding,
      );
      $expected = '"ID","Name","Age"
"1","Jo' . $conversion . 'hn","25"
"2","George","27"
"3","Ringo","28"
"4","Paul","26"
"5","Meredith","30"';
      $message = 'Character encoding ' . $encoding . ' worked correctly.';
      $this
        ->executeAndCompareGivenView($view, $expected, $message, $style_options);
    }
  }

  /**
   * Test to ensure that all new line characters are replaced in CSV files when requested.
   */
  protected function testReplaceNewLines() {
    $view = $this
      ->getBasicExportView();
    $display = $view->display['default']->handler;
    $display
      ->override_option('fields', array(
      'id' => array(
        'id' => 'id',
        'table' => 'views_test',
        'field' => 'id',
        'relationship' => 'none',
        'label' => 'ID',
      ),
      'name' => array(
        'id' => 'name',
        'table' => 'views_test',
        'field' => 'name',
        'relationship' => 'none',
      ),
      'age' => array(
        'id' => 'age',
        'table' => 'views_test',
        'field' => 'age',
        'relationship' => 'none',
      ),
      'job' => array(
        'id' => 'job',
        'table' => 'views_test',
        'field' => 'job',
        'relationship' => 'none',
      ),
    ));
    $style_options = array(
      'replace_newlines' => TRUE,
      'newline_replacement' => ';',
      'newline_token' => 0,
    );
    $expected = <<<EOT
"ID","Name","Age","Job"
"1","John","25","Singer
;Songwriter
;Pianist"
"2","George","27","Singer;Guitar player;Sitar player"
"3","Ringo","28","Drummer"
"4","Paul","26","Songwriter
Bass guitarist"
"5","Meredith","30","Speaker"
EOT;
    $message = 'Linefeed characters are replaced.';
    $this
      ->executeAndCompareGivenView($view, $expected, $message, $style_options);

    // And now replace all kind of newlines.
    $style_options = array(
      'replace_newlines' => TRUE,
      'newline_replacement' => ';',
      'newline_token' => 1,
    );
    $expected = '"ID","Name","Age","Job"
"1","John","25","Singer;Songwriter;Pianist"
"2","George","27","Singer;Guitar player;Sitar player"
"3","Ringo","28","Drummer"
"4","Paul","26","Songwriter;Bass guitarist"
"5","Meredith","30","Speaker"';
    $message = 'All newline characters are replaced.';
    $this
      ->executeAndCompareGivenView($view, $expected, $message, $style_options);
  }

}

Classes