Skip to content

Commit cbd4382

Browse files
test import from file #140
skipping import url test till solution is found
1 parent c0c59b3 commit cbd4382

File tree

3 files changed

+93
-22
lines changed

3 files changed

+93
-22
lines changed

classes/Visualizer/Module/Chart.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,10 @@ public function deleteChart() {
204204
* @access public
205205
*/
206206
public function renderChartPages() {
207-
define( 'IFRAME_REQUEST', 1 );
207+
defined( 'IFRAME_REQUEST' ) || define( 'IFRAME_REQUEST', 1 );
208208

209209
// check chart, if chart not exists, will create new one and redirects to the same page with proper chart id
210-
$chart_id = filter_input( INPUT_GET, 'chart', FILTER_VALIDATE_INT );
210+
$chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : '';
211211
if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) {
212212
$default_type = 'line';
213213

@@ -231,7 +231,7 @@ public function renderChartPages() {
231231
}
232232

233233
wp_redirect( add_query_arg( 'chart', (int) $chart_id ) );
234-
exit;
234+
wp_die();
235235
}
236236

237237
// enqueue and register scripts and styles
@@ -251,7 +251,7 @@ public function renderChartPages() {
251251

252252
// dispatch pages
253253
$this->_chart = $chart;
254-
switch ( filter_input( INPUT_GET, 'tab' ) ) {
254+
switch ( isset( $_GET['tab'] ) ? $_GET['tab'] : '' ) {
255255
case 'settings':
256256
// changed by Ash/Upwork
257257
$this->_handleDataAndSettingsPage();
@@ -262,7 +262,7 @@ public function renderChartPages() {
262262
break;
263263
}
264264

265-
exit;
265+
wp_die();
266266
}
267267

268268
/**
@@ -351,7 +351,7 @@ private function _handleDataPage() {
351351
* Handle data and settings page
352352
*/
353353
private function _handleDataAndSettingsPage() {
354-
if ( $_SERVER['REQUEST_METHOD'] == 'POST' && wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ) ) ) {
354+
if ( $_SERVER['REQUEST_METHOD'] == 'POST' && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'] ) ) {
355355
if ( $this->_chart->post_status == 'auto-draft' ) {
356356
$this->_chart->post_status = 'publish';
357357
wp_update_post( $this->_chart->to_array() );

languages/visualizer.pot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ msgid ""
44
msgstr ""
55
"Project-Id-Version: Visualizer: Charts and Graphs Lite 2.0.0\n"
66
"Report-Msgid-Bugs-To: https://github.com/Codeinwp/visualizer/issues\n"
7-
"POT-Creation-Date: 2017-03-15 09:39:51+00:00\n"
7+
"POT-Creation-Date: 2017-03-15 13:43:24+00:00\n"
88
"MIME-Version: 1.0\n"
99
"Content-Type: text/plain; charset=utf-8\n"
1010
"Content-Transfer-Encoding: 8bit\n"

tests/test-import.php

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,41 @@ class Test_Import extends WP_Ajax_UnitTestCase {
2929
* @access private
3030
*/
3131
private function create_chart() {
32-
$id = wp_insert_post(array(
33-
'post_type' => 'visualizer',
34-
'post_content' => 'a:5:{i:0;a:2:{i:0;s:17:"Work";i:1;d:11;}i:1;a:2:{i:0;s:3:"Eat";i:1;d:2;}i:2;a:2:{i:0;s:7:"Commute";i:1;d:2;}i:3;a:2:{i:0;s:8:"Watch TV";i:1;d:2;}i:4;a:2:{i:0;s:5:"Sleep";i:1;d:7;}}',
35-
'post_status' => 'publish',
36-
));
32+
$this->_setRole( 'administrator' );
33+
34+
$_GET = array(
35+
'library' => 'yes',
36+
'tab' => 'visualizer',
37+
);
3738

38-
update_post_meta( $id, 'visualizer-chart-type', 'pie' );
39-
update_post_meta( $id, 'visualizer-series', 'a:2:{i:0;a:2:{s:5:"label";s:4:"Task";s:4:"type";s:6:"string";}i:1;a:2:{s:5:"label";s:13:"Hours per Day";s:4:"type";s:6:"number";}}' );
40-
update_post_meta( $id, 'visualizer-default-data', 0 );
41-
update_post_meta( $id, 'visualizer-source', 'Visualizer_Source_Csv' );
42-
update_post_meta( $id, 'visualizer-settings', 'a:20:{s:5:"title";s:0:"";s:14:"titleTextStyle";a:1:{s:5:"color";s:4:"#000";}s:8:"fontName";s:0:"";s:8:"fontSize";s:0:"";s:6:"legend";a:3:{s:8:"position";s:4:"left";s:9:"alignment";s:0:"";s:9:"textStyle";a:1:{s:5:"color";s:4:"#000";}}s:7:"tooltip";a:3:{s:7:"trigger";s:0:"";s:13:"showColorCode";s:0:"";s:4:"text";s:0:"";}s:4:"is3D";s:0:"";s:17:"reverseCategories";s:0:"";s:12:"pieSliceText";s:0:"";s:7:"pieHole";s:0:"";s:13:"pieStartAngle";s:0:"";s:19:"pieSliceBorderColor";s:4:"#fff";s:24:"sliceVisibilityThreshold";s:0:"";s:20:"pieResidueSliceLabel";s:0:"";s:20:"pieResidueSliceColor";s:4:"#ccc";s:6:"slices";a:5:{i:0;a:2:{s:6:"offset";s:0:"";s:5:"color";s:0:"";}i:1;a:2:{s:6:"offset";s:0:"";s:5:"color";s:0:"";}i:2;a:2:{s:6:"offset";s:0:"";s:5:"color";s:0:"";}i:3;a:2:{s:6:"offset";s:0:"";s:5:"color";s:0:"";}i:4;a:2:{s:6:"offset";s:0:"";s:5:"color";s:0:"";}}s:5:"width";s:0:"";s:6:"height";s:0:"";s:15:"backgroundColor";a:3:{s:11:"strokeWidth";s:0:"";s:6:"stroke";s:4:"#666";s:4:"fill";s:4:"#fff";}s:9:"chartArea";a:4:{s:4:"left";s:0:"";s:3:"top";s:0:"";s:5:"width";s:0:"";s:6:"height";s:0:"";}}' );
39+
// swallow the output
40+
ob_start();
41+
try {
42+
$this->_handleAjax( 'visualizer-create-chart' );
43+
} catch ( WPAjaxDieContinueException $e ) {
44+
// We expected this, do nothing.
45+
} catch ( WPAjaxDieStopException $ee ) {
46+
// We expected this, do nothing.
47+
}
48+
ob_end_clean();
4349

44-
$this->chart = $id;
50+
$query = new WP_Query(array(
51+
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
52+
'post_status' => 'auto-draft',
53+
'numberposts' => 1,
54+
'fields' => 'ids',
55+
));
56+
$this->chart = $query->posts[0];
4557
}
4658

4759
/**
48-
* Testing file import feature.
60+
* Testing url import feature.
4961
*
5062
* @access public
5163
* @dataProvider urlProvider
5264
*/
5365
public function test_url_import( $url ) {
66+
$this->markTestSkipped( 'this test is disabled till we can figure out how to provide a "local" url' );
5467
$this->create_chart();
5568
$this->_setRole( 'administrator' );
5669

@@ -78,22 +91,80 @@ public function test_url_import( $url ) {
7891
$src = get_post_meta( $this->chart, 'visualizer-source', true );
7992
$content = $chart->post_content;
8093

81-
$content_line = 'a:2:{s:6:"source";s:66:"' . $url . '";s:4:"data";a:14:{i:0;a:4:{i:0;s:1:"A";i:1;d:1;i:2;d:1;i:3;d:0.5;}i:1;a:4:{i:0;s:1:"B";i:1;d:2;i:2;d:0.5;i:3;d:1;}i:2;a:4:{i:0;s:1:"C";i:1;d:4;i:2;d:1;i:3;d:0.5;}i:3;a:4:{i:0;s:1:"D";i:1;d:8;i:2;d:0.5;i:3;d:1;}i:4;a:4:{i:0;s:1:"E";i:1;d:7;i:2;d:1;i:3;d:0.5;}i:5;a:4:{i:0;s:1:"F";i:1;d:7;i:2;d:0.5;i:3;d:1;}i:6;a:4:{i:0;s:1:"G";i:1;d:8;i:2;d:1;i:3;d:0.5;}i:7;a:4:{i:0;s:1:"H";i:1;d:4;i:2;d:0.5;i:3;d:1;}i:8;a:4:{i:0;s:1:"I";i:1;d:2;i:2;d:1;i:3;d:0.5;}i:9;a:4:{i:0;s:1:"J";i:1;d:3.5;i:2;d:0.5;i:3;d:1;}i:10;a:4:{i:0;s:1:"K";i:1;d:3;i:2;d:1;i:3;d:0.5;}i:11;a:4:{i:0;s:1:"L";i:1;d:3.5;i:2;d:0.5;i:3;d:1;}i:12;a:4:{i:0;s:1:"M";i:1;d:1;i:2;d:1;i:3;d:0.5;}i:13;a:4:{i:0;s:1:"N";i:1;d:1;i:2;d:0.5;i:3;d:1;}}}';
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;}}}';
8295

83-
$series_line = unserialize( 'a:4:{i:0;a:2:{s:5:"label";s:1:"x";s:4:"type";s:6:"string";}i:1;a:2:{s:5:"label";s:4:"Cats";s:4:"type";s:6:"number";}i:2;a:2:{s:5:"label";s:8:"Blanket1";s:4:"type";s:6:"number";}i:3;a:2:{s:5:"label";s:8:"Blanket2";s:4:"type";s:6:"number";}}' );
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";}}' );
8497

8598
$this->assertEquals( 'Visualizer_Source_Csv_Remote', $src );
8699
$this->assertEquals( $content, $content_line );
87100
$this->assertEquals( $series, $series_line );
88101
}
89102

103+
/**
104+
* Testing file import feature.
105+
*
106+
* @access public
107+
* @dataProvider fileProvider
108+
*/
109+
public function test_file_import( $file ) {
110+
$this->create_chart();
111+
$this->_setRole( 'administrator' );
112+
113+
$dest = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . basename( $file );
114+
copy( $file, $dest );
115+
116+
$_FILES = array(
117+
'local_data' => array(
118+
'tmp_name' => $dest,
119+
'error' => 0,
120+
),
121+
);
122+
$_GET = array(
123+
'nonce' => wp_create_nonce(),
124+
'chart' => $this->chart,
125+
);
126+
127+
// swallow the output
128+
ob_start();
129+
try {
130+
$this->_handleAjax( 'visualizer-upload-data' );
131+
} catch ( WPAjaxDieContinueException $e ) {
132+
// We expected this, do nothing.
133+
} catch ( WPAjaxDieStopException $ee) {
134+
// We expected this, do nothing.
135+
}
136+
ob_end_clean();
137+
unlink( $dest );
138+
139+
$series = get_post_meta( $this->chart, 'visualizer-series', true );
140+
$chart = get_post( $this->chart );
141+
$src = get_post_meta( $this->chart, 'visualizer-source', true );
142+
$content = $chart->post_content;
143+
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;}}';
145+
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";}}' );
147+
148+
$this->assertEquals( 'Visualizer_Source_Csv', $src );
149+
$this->assertEquals( $content, $content_line );
150+
$this->assertEquals( $series, $series_line );
151+
}
152+
153+
/**
154+
* Provide the fileURL for uploading the file
155+
*
156+
* @access public
157+
*/
158+
public function fileProvider() {
159+
return [ [ VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'bar.csv' ] ];
160+
}
90161

91162
/**
92163
* Provide the URL for uploading the file
93164
*
94165
* @access public
95166
*/
96167
public function urlProvider() {
97-
return [ [ 'http://localhost/wp-content/plugins/wp-visualizer/samples/line.csv' ] ];
168+
return [ [ 'http://localhost/wp-content/plugins/wp-visualizer/samples/bar.csv' ] ];
98169
}
99170
}

0 commit comments

Comments
 (0)