Skip to content

Commit 2bf8356

Browse files
phpunit more tests #142
1 parent 0511105 commit 2bf8356

File tree

3 files changed

+201
-21
lines changed

3 files changed

+201
-21
lines changed

classes/Visualizer/Module/Chart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public function renderChartPages() {
231231
}
232232

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

237237
// enqueue and register scripts and styles

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-16 05:02:53+00:00\n"
7+
"POT-Creation-Date: 2017-03-30 07:38:04+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: 199 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private function create_chart() {
6262
* @access public
6363
* @dataProvider urlProvider
6464
*/
65-
public function test_url_import( $url ) {
65+
public function test_url_import( $url, $content, $series ) {
6666
$this->markTestSkipped( 'this test is disabled till we can figure out how to provide a "local" url' );
6767
$this->create_chart();
6868
$this->_setRole( 'administrator' );
@@ -86,18 +86,14 @@ public function test_url_import( $url ) {
8686
}
8787
ob_end_clean();
8888

89-
$series = get_post_meta( $this->chart, 'visualizer-series', true );
89+
$series_new = get_post_meta( $this->chart, 'visualizer-series', true );
9090
$chart = get_post( $this->chart );
9191
$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;
9793

9894
$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 );
10197
}
10298

10399
/**
@@ -106,7 +102,7 @@ public function test_url_import( $url ) {
106102
* @access public
107103
* @dataProvider fileProvider
108104
*/
109-
public function test_file_import( $file ) {
105+
public function test_file_import( $file, $content, $series ) {
110106
$this->create_chart();
111107
$this->_setRole( 'administrator' );
112108

@@ -136,28 +132,146 @@ public function test_file_import( $file ) {
136132
ob_end_clean();
137133
unlink( $dest );
138134

139-
$series = get_post_meta( $this->chart, 'visualizer-series', true );
135+
$series_new = get_post_meta( $this->chart, 'visualizer-series', true );
140136
$chart = get_post( $this->chart );
141137
$src = get_post_meta( $this->chart, 'visualizer-source', true );
142-
$content = $chart->post_content;
138+
$content_new = $chart->post_content;
143139

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+
}
145144

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+
}
147155

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 );
151219
}
152220

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+
}
153265
/**
154266
* Provide the fileURL for uploading the file
155267
*
156268
* @access public
157269
*/
158270
public function fileProvider() {
271+
$file = VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'bar.csv';
272+
list($content, $series) = $this->parseFile( $file );
159273
return array(
160-
array( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'bar.csv' ),
274+
array( $file, $content, $series ),
161275
);
162276
}
163277

@@ -167,8 +281,74 @@ public function fileProvider() {
167281
* @access public
168282
*/
169283
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 );
170288
return array(
171-
array( 'http://localhost/wp-content/plugins/wp-visualizer/samples/bar.csv' ),
289+
array( $url, array( 'source' => $url, 'data' => $content ), $series ),
172290
);
173291
}
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+
}
174354
}

0 commit comments

Comments
 (0)