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