csv_export.test 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. class CSVExportViewsDataExportTests extends ViewsDataExportSimpleExportTest {
  3. protected $profile = 'testing';
  4. public static function getInfo() {
  5. return array(
  6. 'name' => 'CSV Export',
  7. 'description' => 'Various tests for exporting to CSV.',
  8. 'group' => 'Views Data Export',
  9. );
  10. }
  11. protected $vde_export_type = 'CSV';
  12. protected function getStylePluginName() {
  13. return 'views_data_export_csv';
  14. }
  15. protected function getExportView($path = 'vde_test') {
  16. // Create the basic view.
  17. $view = $this->getBasicExportView();
  18. $display = $view->new_display('views_data_export', 'Data export', 'vde_test');
  19. $display->override_option('style_plugin', $this->getStylePluginName());
  20. $display->override_option('path', $path);
  21. $expected = '"ID","Name","Age"
  22. "1","John","25"
  23. "2","George","27"
  24. "3","Ringo","28"
  25. "4","Paul","26"
  26. "5","Meredith","30"';
  27. return array(&$view, $expected);
  28. }
  29. /**
  30. * Test to ensure that HTML tags are kept in CSV files when requested.
  31. */
  32. protected function testKeepHTML() {
  33. $view = $this->getBasicExportView();
  34. $display = $view->display['default']->handler;
  35. $display->override_option('fields', array(
  36. 'id' => array(
  37. 'id' => 'id',
  38. 'table' => 'views_test',
  39. 'field' => 'id',
  40. 'relationship' => 'none',
  41. // Add a label to include HTML
  42. 'label' => '<strong id="id">ID</strong>',
  43. ),
  44. 'name' => array(
  45. 'id' => 'name',
  46. 'table' => 'views_test',
  47. 'field' => 'name',
  48. 'relationship' => 'none',
  49. // Alter this field to include HTML.
  50. 'alter' => array(
  51. 'alter_text' => TRUE,
  52. 'text' => '<em>[name]</em>',
  53. ),
  54. ),
  55. 'age' => array(
  56. 'id' => 'age',
  57. 'table' => 'views_test',
  58. 'field' => 'age',
  59. 'relationship' => 'none',
  60. ),
  61. ));
  62. $style_options = array(
  63. 'keep_html' => TRUE,
  64. );
  65. $expected = '"<strong id=""id"">ID</strong>","Name","Age"
  66. "1","<em>John</em>","25"
  67. "2","<em>George</em>","27"
  68. "3","<em>Ringo</em>","28"
  69. "4","<em>Paul</em>","26"
  70. "5","<em>Meredith</em>","30"';
  71. $message = 'Keep HTML test in ' . $this->vde_export_type . ' export matched expected output.';
  72. $this->executeAndCompareGivenView($view, $expected, $message, $style_options);
  73. // And now make sure that HTML tags are stripped correctly.
  74. $style_options = array(
  75. 'keep_html' => FALSE,
  76. );
  77. $expected = '"ID","Name","Age"
  78. "1","John","25"
  79. "2","George","27"
  80. "3","Ringo","28"
  81. "4","Paul","26"
  82. "5","Meredith","30"';
  83. $message = 'Keep HTML reverse test in ' . $this->vde_export_type . ' export matched expected output.';
  84. $this->executeAndCompareGivenView($view, $expected, $message, $style_options);
  85. }
  86. /**
  87. * Test to ensure that HTML tags are kept in CSV files when requested.
  88. */
  89. protected function testTrimFields() {
  90. $view = $this->getBasicExportView();
  91. $display = $view->display['default']->handler;
  92. $display->override_option('fields', array(
  93. 'id' => array(
  94. 'id' => 'id',
  95. 'table' => 'views_test',
  96. 'field' => 'id',
  97. 'relationship' => 'none',
  98. 'label' => 'ID',
  99. ),
  100. 'name' => array(
  101. 'id' => 'name',
  102. 'table' => 'views_test',
  103. 'field' => 'name',
  104. 'relationship' => 'none',
  105. // Alter this field to include HTML.
  106. 'alter' => array(
  107. 'alter_text' => TRUE,
  108. 'text' => ' [name] ',
  109. ),
  110. ),
  111. 'age' => array(
  112. 'id' => 'age',
  113. 'table' => 'views_test',
  114. 'field' => 'age',
  115. 'relationship' => 'none',
  116. ),
  117. ));
  118. $style_options = array(
  119. 'trim' => FALSE,
  120. );
  121. $expected = '"ID","Name","Age"
  122. "1"," John ","25"
  123. "2"," George ","27"
  124. "3"," Ringo ","28"
  125. "4"," Paul ","26"
  126. "5"," Meredith ","30"';
  127. $message = 'Trim reverse test in ' . $this->vde_export_type . ' export matched expected output.';
  128. $this->executeAndCompareGivenView($view, $expected, $message, $style_options);
  129. // And now make sure that trimming works as expected.
  130. $style_options = array(
  131. 'trim' => TRUE,
  132. );
  133. $expected = '"ID","Name","Age"
  134. "1","John","25"
  135. "2","George","27"
  136. "3","Ringo","28"
  137. "4","Paul","26"
  138. "5","Meredith","30"';
  139. $message = 'Trim test in ' . $this->vde_export_type . ' export matched expected output.';
  140. $this->executeAndCompareGivenView($view, $expected, $message, $style_options);
  141. }
  142. /**
  143. * Test to ensure that Encoding options work.
  144. */
  145. protected function testIconvEncoding() {
  146. $view = $this->getBasicExportView();
  147. db_update('views_test')
  148. ->fields(array('name' => 'Jo€hn'))
  149. ->condition('name', 'John')
  150. ->execute();
  151. $encodings = array(
  152. 'ISO-8859-1//TRANSLIT' => 'EUR',
  153. 'utf8_decode' => '?',
  154. );
  155. foreach ($encodings as $encoding => $conversion) {
  156. $style_options = array(
  157. 'encoding' => $encoding,
  158. );
  159. $expected = '"ID","Name","Age"
  160. "1","Jo' . $conversion . 'hn","25"
  161. "2","George","27"
  162. "3","Ringo","28"
  163. "4","Paul","26"
  164. "5","Meredith","30"';
  165. $message = 'Character encoding ' . $encoding . ' worked correctly.';
  166. $this->executeAndCompareGivenView($view, $expected, $message, $style_options);
  167. }
  168. }
  169. /**
  170. * Test to ensure that all new line characters are replaced in CSV files when requested.
  171. */
  172. protected function testReplaceNewLines() {
  173. $view = $this->getBasicExportView();
  174. $display = $view->display['default']->handler;
  175. $display->override_option('fields', array(
  176. 'id' => array(
  177. 'id' => 'id',
  178. 'table' => 'views_test',
  179. 'field' => 'id',
  180. 'relationship' => 'none',
  181. 'label' => 'ID',
  182. ),
  183. 'name' => array(
  184. 'id' => 'name',
  185. 'table' => 'views_test',
  186. 'field' => 'name',
  187. 'relationship' => 'none',
  188. ),
  189. 'age' => array(
  190. 'id' => 'age',
  191. 'table' => 'views_test',
  192. 'field' => 'age',
  193. 'relationship' => 'none',
  194. ),
  195. 'job' => array(
  196. 'id' => 'job',
  197. 'table' => 'views_test',
  198. 'field' => 'job',
  199. 'relationship' => 'none',
  200. ),
  201. ));
  202. $style_options = array(
  203. 'replace_newlines' => TRUE,
  204. 'newline_replacement' => ';',
  205. 'newline_token' => 0,
  206. );
  207. $expected = <<<EOT
  208. "ID","Name","Age","Job"
  209. "1","John","25","Singer\r;Songwriter\r;Pianist"
  210. "2","George","27","Singer;Guitar player;Sitar player"
  211. "3","Ringo","28","Drummer"
  212. "4","Paul","26","Songwriter\rBass guitarist"
  213. "5","Meredith","30","Speaker"
  214. EOT;
  215. $message = 'Linefeed characters are replaced.';
  216. $this->executeAndCompareGivenView($view, $expected, $message, $style_options);
  217. // And now replace all kind of newlines.
  218. $style_options = array(
  219. 'replace_newlines' => TRUE,
  220. 'newline_replacement' => ';',
  221. 'newline_token' => 1,
  222. );
  223. $expected = '"ID","Name","Age","Job"
  224. "1","John","25","Singer;Songwriter;Pianist"
  225. "2","George","27","Singer;Guitar player;Sitar player"
  226. "3","Ringo","28","Drummer"
  227. "4","Paul","26","Songwriter;Bass guitarist"
  228. "5","Meredith","30","Speaker"';
  229. $message = 'All newline characters are replaced.';
  230. $this->executeAndCompareGivenView($view, $expected, $message, $style_options);
  231. }
  232. }