Skip to content

Commit 0511105

Browse files
phpunit more tests #142
1 parent 3ceab83 commit 0511105

File tree

3 files changed

+95
-5
lines changed

3 files changed

+95
-5
lines changed

classes/Visualizer/Module/Chart.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public function renderChartPages() {
250250
}
251251

252252
// dispatch pages
253-
$this->_chart = $chart;
253+
$this->_chart = get_post( $chart_id );
254254
switch ( isset( $_GET['tab'] ) ? $_GET['tab'] : '' ) {
255255
case 'settings':
256256
// changed by Ash/Upwork
@@ -552,7 +552,7 @@ public function exportData() {
552552
$chart_id = $success = false;
553553
$capable = current_user_can( 'edit_posts' );
554554
if ( $capable ) {
555-
$chart_id = filter_input( INPUT_GET, 'chart', FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => 1 ) ) );
555+
$chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => 1 ) ) ) : '';
556556
if ( $chart_id ) {
557557
$chart = get_post( $chart_id );
558558
$success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER;
@@ -561,7 +561,7 @@ public function exportData() {
561561

562562
if ( $success ) {
563563
$settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true );
564-
$filename = $settings['title'];
564+
$filename = isset( $settings['title'] ) ? $settings['title'] : '';
565565
if ( empty( $filename ) ) {
566566
$filename = 'export.csv';
567567
} else {
@@ -620,6 +620,6 @@ public function exportData() {
620620
));
621621
}// End if().
622622

623-
exit;
623+
wp_die();
624624
}
625625
}

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 13:57:55+00:00\n"
7+
"POT-Creation-Date: 2017-03-16 05:02:53+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-export.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/**
4+
* Test class for the exporting features.
5+
*
6+
* @package Visualizer
7+
* @subpackage Tests
8+
* @copyright Copyright (c) 2017, Marius Cristea
9+
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
10+
* @since 2.0.0
11+
*/
12+
class Test_Export extends WP_Ajax_UnitTestCase {
13+
14+
/**
15+
* The chart id of the chart created
16+
*
17+
* @since 2.0.0
18+
*
19+
* @access private
20+
* @var int
21+
*/
22+
private $chart;
23+
24+
/**
25+
* Create a chart
26+
*
27+
* @since 2.0.0
28+
*
29+
* @access private
30+
*/
31+
private function create_chart() {
32+
$this->_setRole( 'administrator' );
33+
34+
$_GET = array(
35+
'library' => 'yes',
36+
'tab' => 'visualizer',
37+
);
38+
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();
49+
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];
57+
}
58+
59+
/**
60+
* Testing export
61+
*
62+
* @access public
63+
*/
64+
public function test_download_export() {
65+
$this->create_chart();
66+
$this->_setRole( 'administrator' );
67+
68+
$_GET = array(
69+
'security' => wp_create_nonce( Visualizer_Plugin::ACTION_EXPORT_DATA . Visualizer_Plugin::VERSION ),
70+
'chart' => $this->chart,
71+
);
72+
73+
// swallow the output
74+
ob_start();
75+
try {
76+
$this->_handleAjax( 'visualizer-export-data' );
77+
} catch ( WPAjaxDieContinueException $e ) {
78+
// We expected this, do nothing.
79+
} catch ( WPAjaxDieStopException $ee) {
80+
// We expected this, do nothing.
81+
}
82+
ob_end_clean();
83+
84+
$response = json_decode( $this->_last_response );
85+
$this->assertInternalType( 'object', $response );
86+
$this->assertObjectHasAttribute( 'success', $response );
87+
$this->assertObjectHasAttribute( 'data', $response );
88+
$this->assertTrue( $response->success );
89+
}
90+
}

0 commit comments

Comments
 (0)