Skip to content

Commit 52fabde

Browse files
make sure import from chart is working fine #47
1 parent 12299a8 commit 52fabde

File tree

1 file changed

+78
-42
lines changed

1 file changed

+78
-42
lines changed

tests/test-import.php

Lines changed: 78 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,22 @@ public function test_file_import( $file ) {
156156
* @access public
157157
* @dataProvider editorDataProvider
158158
*/
159-
public function test_pro_editor( $data ) {
160-
if ( ! defined( 'VISUALIZER_PRO_VERSION' ) ) {
161-
$this->markTestSkipped( 'PRO not installed/available, skipping test' );
162-
}
159+
public function test_pro_editor($data) {
160+
if ( !defined( 'VISUALIZER_PRO_VERSION' ) ) {
161+
$this->markTestSkipped( 'PRO not installed/available, skipping test');
162+
}
163163

164164
$this->create_chart();
165165
$this->_setRole( 'administrator' );
166166

167167
$_POST = array(
168-
'chart_data' => $data,
168+
'chart_data' => $data
169169
);
170170
$_GET = array(
171171
'nonce' => wp_create_nonce(),
172172
'chart' => $this->chart,
173173
);
174-
$_FILES = array();
174+
$_FILES = array();
175175

176176
// swallow the output
177177
ob_start();
@@ -192,45 +192,81 @@ public function test_pro_editor( $data ) {
192192
$this->assertEquals( $content, $content_line );
193193
}
194194

195+
/**
196+
* Testing fetch from chat feature. We only need to test fetching, because we already have a test case for uploading data
197+
*
198+
* @access public
199+
*/
200+
public function test_pro_fetch_from_chart() {
201+
if ( !defined( 'VISUALIZER_PRO_VERSION' ) ) {
202+
$this->markTestSkipped( 'PRO not installed/available, skipping test');
203+
}
204+
205+
$this->create_chart();
206+
$this->_setRole( 'administrator' );
207+
208+
$_GET = array(
209+
'nonce' => wp_create_nonce(),
210+
'chart_id' => $this->chart,
211+
);
212+
213+
// swallow the output
214+
ob_start();
215+
try {
216+
$this->_handleAjax( 'visualizer-fetch-data' );
217+
} catch ( WPAjaxDieContinueException $e ) {
218+
// We expected this, do nothing.
219+
} catch ( WPAjaxDieStopException $ee) {
220+
// We expected this, do nothing.
221+
}
222+
ob_end_clean();
223+
224+
$response = json_decode( $this->_last_response );
225+
$this->assertInternalType( 'object', $response );
226+
$this->assertObjectHasAttribute( 'success', $response );
227+
$this->assertObjectHasAttribute( 'data', $response );
228+
$this->assertTrue( $response->success );
229+
}
230+
195231
/**
196232
* Provide the "edited" data
197233
*
198234
* @access public
199235
*/
200236
public function editorDataProvider() {
201-
$data = array();
202-
$file = VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'line.csv';
203-
if ( ($handle = fopen( $file, 'r' )) !== false ) {
204-
$row = 0;
205-
while ( ($line = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE )) !== false ) {
206-
if ( $row++ <= 1 ) {
207-
$cols = count( $line );
208-
$datum = array();
209-
for ( $col = 0; $col < $cols; $col++ ) {
210-
$datum[] = '"' . $line[ $col ] . '"';
211-
}
212-
} else {
213-
$cols = count( $line );
214-
$datum = array();
215-
for ( $col = 0; $col < $cols; $col++ ) {
216-
if ( is_numeric( $line[ $col ] ) ) {
217-
// multiply all numbers by 10
218-
$datum[] = $line[ $col ] * 10;
219-
} else {
220-
$datum[] = '"' . $line[ $col ] . '"';
221-
}
222-
}
223-
}
224-
$data[] = $datum;
225-
}
226-
}
237+
$data = array();
238+
$file = VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'line.csv';
239+
if (($handle = fopen($file, "r")) !== FALSE) {
240+
$row = 0;
241+
while (($line = fgetcsv($handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE)) !== FALSE) {
242+
if ($row++ <= 1) {
243+
$cols = count($line);
244+
$datum = array();
245+
for ($col = 0; $col < $cols; $col++) {
246+
$datum[] = '"'. $line[$col] . '"';
247+
}
248+
} else {
249+
$cols = count($line);
250+
$datum = array();
251+
for ($col = 0; $col < $cols; $col++) {
252+
if (is_numeric($line[$col])) {
253+
// multiply all numbers by 10
254+
$datum[] = $line[$col] * 10;
255+
} else {
256+
$datum[] = '"' . $line[$col] . '"';
257+
}
258+
}
259+
}
260+
$data[] = $datum;
261+
}
262+
}
227263

228-
$csv = array();
229-
foreach ( $data as $row ) {
230-
$csv[] = '[' . implode( ',', $row ) . ']';
231-
}
232-
$csv = '[' . implode( ',', $csv ) . ']';
233-
return array( array( $csv ) );
264+
$csv = array();
265+
foreach ($data as $row) {
266+
$csv[] = "[" . implode(",", $row) . "]";
267+
}
268+
$csv = "[" . implode(",", $csv) . "]";
269+
return array(array($csv));
234270
}
235271
/**
236272
* Provide the fileURL for uploading the file
@@ -239,8 +275,8 @@ public function editorDataProvider() {
239275
*/
240276
public function fileProvider() {
241277
return array(
242-
array( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'bar.csv' ),
243-
);
278+
array(VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'bar.csv')
279+
);
244280
}
245281

246282
/**
@@ -250,7 +286,7 @@ public function fileProvider() {
250286
*/
251287
public function urlProvider() {
252288
return array(
253-
array( 'http://localhost/wp-content/plugins/wp-visualizer/samples/bar.csv' ),
254-
);
289+
array('http://localhost/wp-content/plugins/wp-visualizer/samples/bar.csv')
290+
);
255291
}
256292
}

0 commit comments

Comments
 (0)