-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathtest-import.php
More file actions
338 lines (312 loc) · 9.07 KB
/
test-import.php
File metadata and controls
338 lines (312 loc) · 9.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
/**
* Test class for the importing features.
*
* @package Visualizer
* @subpackage Tests
* @copyright Copyright (c) 2017, Marius Cristea
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* @since 2.0.0
*/
class Test_Import extends WP_Ajax_UnitTestCase {
/**
* The chart id of the chart created
*
* @since 2.0.0
*
* @access private
* @var int
*/
private $chart;
/**
* Testing cloing of chart.
*
* @access public
*/
public function test_clone_chart() {
$this->create_chart();
$this->_setRole( 'administrator' );
$_GET = array(
'nonce' => wp_create_nonce( Visualizer_Plugin::ACTION_CLONE_CHART ),
'chart' => $this->chart,
);
// swallow the output
ob_start();
try {
$this->_handleAjax( 'visualizer-clone-chart' );
} catch ( WPAjaxDieContinueException $e ) {
// We expected this, do nothing.
} catch ( WPAjaxDieStopException $ee ) {
// We expected this, do nothing.
}
ob_end_clean();
$query = new WP_Query(
array(
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
'post_status' => 'auto-draft',
'numberposts' => 1,
'fields' => 'ids',
'post__not_in' => array( $this->chart ),
)
);
$new_id = $query->posts[0];
// all post meta existing in old chart should exist in new chart.
$old_meta = $new_meta = array();
$post_meta = get_post_meta( $this->chart );
foreach ( $post_meta as $key => $value ) {
if ( strpos( $key, 'visualizer-' ) !== false ) {
$old_meta [ $key ] = $value;
}
}
$post_meta = get_post_meta( $new_id );
foreach ( $post_meta as $key => $value ) {
if ( strpos( $key, 'visualizer-' ) !== false ) {
$new_meta [ $key ] = $value;
}
}
$this->assertEquals( $old_meta, $new_meta );
}
/**
* Testing url import feature.
*
* @access public
* @dataProvider urlProvider
*/
public function test_url_import( $url, $content, $series ) {
$this->markTestSkipped( 'this test is disabled till we can figure out how to provide a "local" url' );
$this->create_chart();
$this->_setRole( 'administrator' );
$_POST = array(
'remote_data' => $url,
);
$_GET = array(
'nonce' => wp_create_nonce( 'visualizer-upload-data' ),
'chart' => $this->chart,
);
// swallow the output
ob_start();
try {
$this->_handleAjax( 'visualizer-upload-data' );
} catch ( WPAjaxDieContinueException $e ) {
// We expected this, do nothing.
} catch ( WPAjaxDieStopException $ee ) {
// We expected this, do nothing.
}
ob_end_clean();
$series_new = get_post_meta( $this->chart, 'visualizer-series', true );
$chart = get_post( $this->chart );
$src = get_post_meta( $this->chart, 'visualizer-source', true );
$content_new = $chart->post_content;
$this->assertEquals( 'Visualizer_Source_Csv_Remote', $src );
$this->assertEquals( $content_new, serialize( $content ) );
$this->assertEquals( $series_new, $series );
}
/**
* Create a chart
*
* @since 2.0.0
*
* @access private
*/
private function create_chart() {
$this->_setRole( 'administrator' );
$_GET = array(
'library' => 'yes',
'tab' => 'visualizer',
);
// swallow the output
ob_start();
try {
$this->_handleAjax( 'visualizer-create-chart' );
} catch ( WPAjaxDieContinueException $e ) {
// We expected this, do nothing.
} catch ( WPAjaxDieStopException $ee ) {
// We expected this, do nothing.
}
ob_end_clean();
$query = new WP_Query(
array(
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
'post_status' => 'auto-draft',
'numberposts' => 1,
'fields' => 'ids',
)
);
$this->chart = $query->posts[0];
}
/**
* Testing file import feature.
*
* @access public
* @dataProvider fileProvider
*/
public function test_file_import( $file, $content, $series ) {
$this->create_chart();
$this->_setRole( 'administrator' );
$dest = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . basename( $file );
copy( $file, $dest );
$_FILES = array(
'local_data' => array(
'tmp_name' => $dest,
'error' => 0,
),
);
$_GET = array(
'nonce' => wp_create_nonce( 'visualizer-upload-data' ),
'chart' => $this->chart,
);
// swallow the output
ob_start();
try {
$this->_handleAjax( 'visualizer-upload-data' );
} catch ( WPAjaxDieContinueException $e ) {
// We expected this, do nothing.
} catch ( WPAjaxDieStopException $ee ) {
// We expected this, do nothing.
}
ob_end_clean();
unlink( $dest );
$series_new = get_post_meta( $this->chart, 'visualizer-series', true );
$chart = get_post( $this->chart );
$src = get_post_meta( $this->chart, 'visualizer-source', true );
$content_new = $chart->post_content;
$this->assertEquals( 'Visualizer_Source_Csv', $src );
$this->assertEquals( $content_new, serialize( $content ) );
$this->assertEquals( $series_new, $series );
}
/**
* Testing database import feature.
*
* @access public
*/
public function test_db_import() {
$this->markTestSkipped( 'this test is disabled till we can figure out why its not recognizing the new ajax methods' );
$this->create_chart();
$this->_setRole( 'administrator' );
$ids = $this->factory->post->create_many( 10 );
global $wpdb;
$_GET = array(
'security' => wp_create_nonce( Visualizer_Plugin::ACTION_SAVE_DB_QUERY . Visualizer_Plugin::VERSION ),
'chart' => $this->chart,
);
$_POST = array(
'params' => array(
'from' => $wpdb->prefix . 'posts',
'select' => 'count(*), ' . $wpdb->prefix . 'posts.ID',
'group' => $wpdb->prefix . 'posts.ID',
'limit' => 5,
),
'refresh' => 1,
);
try {
$this->_handleAjax( Visualizer_Plugin::ACTION_SAVE_DB_QUERY );
} catch ( WPAjaxDieContinueException $e ) {
// We expected this, do nothing.
} catch ( WPAjaxDieStopException $ee ) {
// We expected this, do nothing.
}
$post = get_post( $this->chart );
echo $post->post_content;
}
/**
* Testing cron custom schedule.
*
* @return void
*/
public function test_custom_cron_schedule() {
$schedules = wp_get_schedules();
$this->assertArrayHasKey( 'visualizer_ten_minutes', $schedules );
}
/**
* Provide the fileURL for uploading the file
*
* @access public
*/
public function fileProvider() {
$this->assertFileExists( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples', 'Folder "samples" does not exist' );
$file = VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'bar.csv';
list( $content, $series ) = $this->parseFile( $file );
return array(
array( $file, $content, $series ),
);
}
/**
* Provide the parsed (and manipulated, if required) data for the specific data file
*
* @access private
*/
private function parseFile( $file, $multiplyValuesBy = 1 ) {
$file = $file;
ini_set( 'auto_detect_line_endings', true );
$handle = fopen( $file, 'rb' );
// read column titles
$labels = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
// read series types
$types = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
$_series = array();
for ( $i = 0, $len = count( $labels ); $i < $len; $i ++ ) {
$default_type = $i === 0 ? 'string' : 'number';
$_series[] = array(
'label' => $labels[ $i ],
'type' => isset( $types[ $i ] ) ? $types[ $i ] : $default_type,
);
}
$_content = array();
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
while ( ( $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ) ) !== false ) {
foreach ( $_series as $i => $series ) {
// if no value exists for the seires, then add null
if ( ! isset( $data[ $i ] ) ) {
$data[ $i ] = null;
}
if ( is_null( $data[ $i ] ) ) {
continue;
}
switch ( $series['type'] ) {
case 'number':
$data[ $i ] = ( is_numeric( $data[ $i ] ) ) ? floatval( $data[ $i ] * $multiplyValuesBy ) : ( is_numeric( str_replace( ',', '', $data[ $i ] ) ) ? floatval( ( str_replace( ',', '', $data[ $i ] ) ) * $multiplyValuesBy ) : null );
break;
case 'boolean':
$data[ $i ] = ! empty( $data[ $i ] ) ? filter_var( $data[ $i ], FILTER_VALIDATE_BOOLEAN ) : null;
break;
case 'timeofday':
$date = new DateTime( '1984-03-16T' . $data[ $i ] );
if ( $date ) {
$data[ $i ] = array(
intval( $date->format( 'H' ) ),
intval( $date->format( 'i' ) ),
intval( $date->format( 's' ) ),
0,
);
}
break;
}
}
$_content[] = $data;
}
fclose( $handle );
return array( $_content, $_series );
}
/**
* Provide the URL for uploading the file
*
* @access public
*/
public function urlProvider() {
$this->assertFileExists( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples', 'Folder "samples" does not exist' );
$url = 'https://demo.themeisle.com/wp-content/plugins/visualizer/samples/bar.csv';
$file = download_url( $url );
list( $content, $series ) = $this->parseFile( $file );
unlink( $file );
return array(
array(
$url,
array(
'source' => $url,
'data' => $content,
),
$series,
),
);
}
}