@@ -62,7 +62,7 @@ private function create_chart() {
62
62
* @access public
63
63
* @dataProvider urlProvider
64
64
*/
65
- public function test_url_import ( $ url ) {
65
+ public function test_url_import ( $ url, $ content , $ series ) {
66
66
$ this ->markTestSkipped ( 'this test is disabled till we can figure out how to provide a "local" url ' );
67
67
$ this ->create_chart ();
68
68
$ this ->_setRole ( 'administrator ' );
@@ -86,18 +86,14 @@ public function test_url_import( $url ) {
86
86
}
87
87
ob_end_clean ();
88
88
89
- $ series = get_post_meta ( $ this ->chart , 'visualizer-series ' , true );
89
+ $ series_new = get_post_meta ( $ this ->chart , 'visualizer-series ' , true );
90
90
$ chart = get_post ( $ this ->chart );
91
91
$ src = get_post_meta ( $ this ->chart , 'visualizer-source ' , true );
92
- $ content = $ chart ->post_content ;
93
-
94
- $ content_line = 'a:2:{s:6:"source";s: ' . strlen ( $ url ) . ':" ' . $ url . '";s:4:"data";a:6:{i:0;a:5:{i:0;s:4:"2003";i:1;d:1336060;i:2;d:400361;i:3;d:1001582;i:4;d:997974;}i:1;a:5:{i:0;s:4:"2004";i:1;d:1538156;i:2;d:366849;i:3;d:1119450;i:4;d:941795;}i:2;a:5:{i:0;s:4:"2005";i:1;d:1576579;i:2;d:440514;i:3;d:993360;i:4;d:930593;}i:3;a:5:{i:0;s:4:"2006";i:1;d:1600652;i:2;d:434552;i:3;d:1004163;i:4;d:897127;}i:4;a:5:{i:0;s:4:"2007";i:1;d:1968113;i:2;d:393032;i:3;d:979198;i:4;d:1080887;}i:5;a:5:{i:0;s:4:"2008";i:1;d:1901067;i:2;d:517206;i:3;d:916965;i:4;d:1056036;}}} ' ;
95
-
96
- $ series_line = unserialize ( 'a:5:{i:0;a:2:{s:5:"label";s:4:"Year";s:4:"type";s:6:"string";}i:1;a:2:{s:5:"label";s:7:"Austria";s:4:"type";s:6:"number";}i:2;a:2:{s:5:"label";s:8:"Bulgaria";s:4:"type";s:6:"number";}i:3;a:2:{s:5:"label";s:7:"Denmark";s:4:"type";s:6:"number";}i:4;a:2:{s:5:"label";s:6:"Greece";s:4:"type";s:6:"number";}} ' );
92
+ $ content_new = $ chart ->post_content ;
97
93
98
94
$ this ->assertEquals ( 'Visualizer_Source_Csv_Remote ' , $ src );
99
- $ this ->assertEquals ( $ content , $ content_line );
100
- $ this ->assertEquals ( $ series , $ series_line );
95
+ $ this ->assertEquals ( $ content_new , serialize ( $ content ) );
96
+ $ this ->assertEquals ( $ series_new , $ series );
101
97
}
102
98
103
99
/**
@@ -106,7 +102,7 @@ public function test_url_import( $url ) {
106
102
* @access public
107
103
* @dataProvider fileProvider
108
104
*/
109
- public function test_file_import ( $ file ) {
105
+ public function test_file_import ( $ file, $ content , $ series ) {
110
106
$ this ->create_chart ();
111
107
$ this ->_setRole ( 'administrator ' );
112
108
@@ -136,28 +132,146 @@ public function test_file_import( $file ) {
136
132
ob_end_clean ();
137
133
unlink ( $ dest );
138
134
139
- $ series = get_post_meta ( $ this ->chart , 'visualizer-series ' , true );
135
+ $ series_new = get_post_meta ( $ this ->chart , 'visualizer-series ' , true );
140
136
$ chart = get_post ( $ this ->chart );
141
137
$ src = get_post_meta ( $ this ->chart , 'visualizer-source ' , true );
142
- $ content = $ chart ->post_content ;
138
+ $ content_new = $ chart ->post_content ;
143
139
144
- $ content_line = 'a:6:{i:0;a:5:{i:0;s:4:"2003";i:1;d:1336060;i:2;d:400361;i:3;d:1001582;i:4;d:997974;}i:1;a:5:{i:0;s:4:"2004";i:1;d:1538156;i:2;d:366849;i:3;d:1119450;i:4;d:941795;}i:2;a:5:{i:0;s:4:"2005";i:1;d:1576579;i:2;d:440514;i:3;d:993360;i:4;d:930593;}i:3;a:5:{i:0;s:4:"2006";i:1;d:1600652;i:2;d:434552;i:3;d:1004163;i:4;d:897127;}i:4;a:5:{i:0;s:4:"2007";i:1;d:1968113;i:2;d:393032;i:3;d:979198;i:4;d:1080887;}i:5;a:5:{i:0;s:4:"2008";i:1;d:1901067;i:2;d:517206;i:3;d:916965;i:4;d:1056036;}} ' ;
140
+ $ this ->assertEquals ( 'Visualizer_Source_Csv ' , $ src );
141
+ $ this ->assertEquals ( $ content_new , serialize ( $ content ) );
142
+ $ this ->assertEquals ( $ series_new , $ series );
143
+ }
145
144
146
- $ series_line = unserialize ( 'a:5:{i:0;a:2:{s:5:"label";s:4:"Year";s:4:"type";s:6:"string";}i:1;a:2:{s:5:"label";s:7:"Austria";s:4:"type";s:6:"number";}i:2;a:2:{s:5:"label";s:8:"Bulgaria";s:4:"type";s:6:"number";}i:3;a:2:{s:5:"label";s:7:"Denmark";s:4:"type";s:6:"number";}i:4;a:2:{s:5:"label";s:6:"Greece";s:4:"type";s:6:"number";}} ' );
145
+ /**
146
+ * Testing editor feature.
147
+ *
148
+ * @access public
149
+ * @dataProvider editorDataProvider
150
+ */
151
+ public function test_pro_editor ( $ data , $ content ) {
152
+ if ( ! defined ( 'VISUALIZER_PRO_VERSION ' ) ) {
153
+ $ this ->markTestSkipped ( 'PRO not installed/available, skipping test ' );
154
+ }
147
155
148
- $ this ->assertEquals ( 'Visualizer_Source_Csv ' , $ src );
149
- $ this ->assertEquals ( $ content , $ content_line );
150
- $ this ->assertEquals ( $ series , $ series_line );
156
+ $ this ->create_chart ();
157
+ $ this ->_setRole ( 'administrator ' );
158
+
159
+ $ _POST = array (
160
+ 'chart_data ' => $ data ,
161
+ );
162
+ $ _GET = array (
163
+ 'nonce ' => wp_create_nonce (),
164
+ 'chart ' => $ this ->chart ,
165
+ );
166
+ $ _FILES = array ();
167
+
168
+ // swallow the output
169
+ ob_start ();
170
+ try {
171
+ $ this ->_handleAjax ( 'visualizer-upload-data ' );
172
+ } catch ( WPAjaxDieContinueException $ e ) {
173
+ // We expected this, do nothing.
174
+ } catch ( WPAjaxDieStopException $ ee ) {
175
+ // We expected this, do nothing.
176
+ }
177
+ ob_end_clean ();
178
+
179
+ $ chart = get_post ( $ this ->chart );
180
+ $ content_new = $ chart ->post_content ;
181
+
182
+ $ this ->assertEquals ( $ content_new , serialize ( $ content ) );
183
+ }
184
+
185
+ /**
186
+ * Testing fetch from chat feature. We only need to test fetching, because we already have a test case for uploading data
187
+ *
188
+ * @access public
189
+ */
190
+ public function test_pro_fetch_from_chart () {
191
+ if ( ! defined ( 'VISUALIZER_PRO_VERSION ' ) ) {
192
+ $ this ->markTestSkipped ( 'PRO not installed/available, skipping test ' );
193
+ }
194
+
195
+ $ this ->create_chart ();
196
+ $ this ->_setRole ( 'administrator ' );
197
+
198
+ $ _GET = array (
199
+ 'nonce ' => wp_create_nonce (),
200
+ 'chart_id ' => $ this ->chart ,
201
+ );
202
+
203
+ // swallow the output
204
+ ob_start ();
205
+ try {
206
+ $ this ->_handleAjax ( 'visualizer-fetch-data ' );
207
+ } catch ( WPAjaxDieContinueException $ e ) {
208
+ // We expected this, do nothing.
209
+ } catch ( WPAjaxDieStopException $ ee ) {
210
+ // We expected this, do nothing.
211
+ }
212
+ ob_end_clean ();
213
+
214
+ $ response = json_decode ( $ this ->_last_response );
215
+ $ this ->assertInternalType ( 'object ' , $ response );
216
+ $ this ->assertObjectHasAttribute ( 'success ' , $ response );
217
+ $ this ->assertObjectHasAttribute ( 'data ' , $ response );
218
+ $ this ->assertTrue ( $ response ->success );
151
219
}
152
220
221
+ /**
222
+ * Provide the "edited" data
223
+ *
224
+ * @access public
225
+ */
226
+ public function editorDataProvider () {
227
+ $ data = array ();
228
+ $ file = VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples ' . DIRECTORY_SEPARATOR . 'line.csv ' ;
229
+ list ($ content , $ series ) = $ this ->parseFile ( $ file , 10 );
230
+
231
+ if ( ($ handle = fopen ( $ file , 'r ' )) !== false ) {
232
+ $ row = 0 ;
233
+ while ( ($ line = fgetcsv ( $ handle , 0 , VISUALIZER_CSV_DELIMITER , VISUALIZER_CSV_ENCLOSURE )) !== false ) {
234
+ if ( $ row ++ <= 1 ) {
235
+ $ cols = count ( $ line );
236
+ $ datum = array ();
237
+ for ( $ col = 0 ; $ col < $ cols ; $ col ++ ) {
238
+ $ datum [] = '" ' . $ line [ $ col ] . '" ' ;
239
+ }
240
+ } else {
241
+ $ cols = count ( $ line );
242
+ $ datum = array ();
243
+ for ( $ col = 0 ; $ col < $ cols ; $ col ++ ) {
244
+ if ( is_numeric ( $ line [ $ col ] ) ) {
245
+ // multiply all numbers by 10
246
+ $ datum [] = $ line [ $ col ] * 10 ;
247
+ } else {
248
+ $ datum [] = '" ' . $ line [ $ col ] . '" ' ;
249
+ }
250
+ }
251
+ }
252
+ $ data [] = $ datum ;
253
+ }
254
+ }
255
+
256
+ $ csv = array ();
257
+ foreach ( $ data as $ row ) {
258
+ $ csv [] = '[ ' . implode ( ', ' , $ row ) . '] ' ;
259
+ }
260
+ $ csv = '[ ' . implode ( ', ' , $ csv ) . '] ' ;
261
+ return array (
262
+ array ( $ csv , $ content ),
263
+ );
264
+ }
153
265
/**
154
266
* Provide the fileURL for uploading the file
155
267
*
156
268
* @access public
157
269
*/
158
270
public function fileProvider () {
271
+ $ file = VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples ' . DIRECTORY_SEPARATOR . 'bar.csv ' ;
272
+ list ($ content , $ series ) = $ this ->parseFile ( $ file );
159
273
return array (
160
- array ( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . ' samples ' . DIRECTORY_SEPARATOR . ' bar.csv ' ),
274
+ array ( $ file , $ content , $ series ),
161
275
);
162
276
}
163
277
@@ -167,8 +281,74 @@ public function fileProvider() {
167
281
* @access public
168
282
*/
169
283
public function urlProvider () {
284
+ $ url = 'http://localhost/wp-content/plugins/wp-visualizer/samples/bar.csv ' ;
285
+ $ file = download_url ( $ url );
286
+ list ($ content , $ series ) = $ this ->parseFile ( $ file );
287
+ unlink ( $ file );
170
288
return array (
171
- array ( ' http://localhost/wp-content/plugins/wp-visualizer/samples/bar.csv ' ),
289
+ array ( $ url , array ( ' source ' => $ url , ' data ' => $ content ), $ series ),
172
290
);
173
291
}
292
+
293
+ /**
294
+ * Provide the parsed (and manipulated, if required) data for the specific data file
295
+ *
296
+ * @access private
297
+ */
298
+ private function parseFile ( $ file , $ multiplyValuesBy = 1 ) {
299
+ $ file = $ file ;
300
+ ini_set ( 'auto_detect_line_endings ' , true );
301
+ $ handle = fopen ( $ file , 'rb ' );
302
+
303
+ // read column titles
304
+ $ labels = fgetcsv ( $ handle , 0 , VISUALIZER_CSV_DELIMITER , VISUALIZER_CSV_ENCLOSURE );
305
+ // read series types
306
+ $ types = fgetcsv ( $ handle , 0 , VISUALIZER_CSV_DELIMITER , VISUALIZER_CSV_ENCLOSURE );
307
+
308
+ $ _series = array ();
309
+ for ( $ i = 0 , $ len = count ( $ labels ); $ i < $ len ; $ i ++ ) {
310
+ $ default_type = $ i == 0 ? 'string ' : 'number ' ;
311
+ $ _series [] = array (
312
+ 'label ' => $ labels [ $ i ],
313
+ 'type ' => isset ( $ types [ $ i ] ) ? $ types [ $ i ] : $ default_type ,
314
+ );
315
+ }
316
+
317
+ $ _content = array ();
318
+ while ( ( $ data = fgetcsv ( $ handle , 0 , VISUALIZER_CSV_DELIMITER , VISUALIZER_CSV_ENCLOSURE ) ) !== false ) {
319
+ foreach ( $ _series as $ i => $ series ) {
320
+ // if no value exists for the seires, then add null
321
+ if ( ! isset ( $ data [ $ i ] ) ) {
322
+ $ data [ $ i ] = null ;
323
+ }
324
+
325
+ if ( is_null ( $ data [ $ i ] ) ) {
326
+ continue ;
327
+ }
328
+
329
+ switch ( $ series ['type ' ] ) {
330
+ case 'number ' :
331
+ $ data [ $ i ] = ( is_numeric ( $ data [ $ i ] ) ) ? floatval ( $ data [ $ i ] * $ multiplyValuesBy ) : (is_numeric ( str_replace ( ', ' , '' , $ data [ $ i ] ) ) ? floatval ( ( str_replace ( ', ' , '' , $ data [ $ i ] ) ) * $ multiplyValuesBy ) : null );
332
+ break ;
333
+ case 'boolean ' :
334
+ $ data [ $ i ] = ! empty ( $ data [ $ i ] ) ? filter_validate ( $ data [ $ i ], FILTER_VALIDATE_BOOLEAN ) : null ;
335
+ break ;
336
+ case 'timeofday ' :
337
+ $ date = new DateTime ( '1984-03-16T ' . $ data [ $ i ] );
338
+ if ( $ date ) {
339
+ $ data [ $ i ] = array (
340
+ intval ( $ date ->format ( 'H ' ) ),
341
+ intval ( $ date ->format ( 'i ' ) ),
342
+ intval ( $ date ->format ( 's ' ) ),
343
+ 0 ,
344
+ );
345
+ }
346
+ break ;
347
+ }
348
+ }
349
+ $ _content [] = $ data ;
350
+ }
351
+ fclose ( $ handle );
352
+ return array ( $ _content , $ _series );
353
+ }
174
354
}
0 commit comments