123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- <?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\r;Songwriter\r;Pianist"
- "2","George","27","Singer;Guitar player;Sitar player"
- "3","Ringo","28","Drummer"
- "4","Paul","26","Songwriter\rBass 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);
- }
- }
|