Skip to content

Commit 5854017

Browse files
Merge pull request #515 from contactashish13/issue-514
Cloning a chart resets the chart library to Google
2 parents 1815432 + c97b0d7 commit 5854017

File tree

2 files changed

+63
-11
lines changed

2 files changed

+63
-11
lines changed

classes/Visualizer/Module/Chart.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ public function renderChartPages() {
443443
add_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 1 );
444444
add_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
445445
add_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
446+
add_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, '' );
446447
add_post_meta(
447448
$chart_id,
448449
Visualizer_Plugin::CF_SETTINGS,
@@ -1022,19 +1023,10 @@ public function uploadData() {
10221023
*/
10231024
public function cloneChart() {
10241025
$chart_id = $success = false;
1025-
$nonce = wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), Visualizer_Plugin::ACTION_CLONE_CHART );
1026+
$nonce = isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'], Visualizer_Plugin::ACTION_CLONE_CHART );
10261027
$capable = current_user_can( 'edit_posts' );
10271028
if ( $nonce && $capable ) {
1028-
$chart_id = filter_input(
1029-
INPUT_GET,
1030-
'chart',
1031-
FILTER_VALIDATE_INT,
1032-
array(
1033-
'options' => array(
1034-
'min_range' => 1,
1035-
),
1036-
)
1037-
);
1029+
$chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : '';
10381030
if ( $chart_id ) {
10391031
$chart = get_post( $chart_id );
10401032
$success = $chart && $chart->post_type === Visualizer_Plugin::CPT_VISUALIZER;
@@ -1053,6 +1045,7 @@ public function cloneChart() {
10531045
);
10541046
if ( $new_chart_id && ! is_wp_error( $new_chart_id ) ) {
10551047
add_post_meta( $new_chart_id, Visualizer_Plugin::CF_CHART_TYPE, get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true ) );
1048+
add_post_meta( $new_chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, true ) );
10561049
add_post_meta( $new_chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, get_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, true ) );
10571050
add_post_meta( $new_chart_id, Visualizer_Plugin::CF_SOURCE, get_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, true ) );
10581051
add_post_meta( $new_chart_id, Visualizer_Plugin::CF_SERIES, get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true ) );
@@ -1067,6 +1060,11 @@ public function cloneChart() {
10671060
);
10681061
}
10691062
}
1063+
1064+
if ( defined( 'WP_TESTS_DOMAIN' ) ) {
1065+
wp_die();
1066+
}
1067+
10701068
wp_redirect( $redirect );
10711069
exit;
10721070
}

tests/test-import.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,60 @@ class Test_Import extends WP_Ajax_UnitTestCase {
2121
*/
2222
private $chart;
2323

24+
/**
25+
* Testing cloing of chart.
26+
*
27+
* @access public
28+
*/
29+
public function test_clone_chart() {
30+
$this->create_chart();
31+
$this->_setRole( 'administrator' );
32+
$_GET = array(
33+
'nonce' => wp_create_nonce( Visualizer_Plugin::ACTION_CLONE_CHART ),
34+
'chart' => $this->chart,
35+
);
36+
// swallow the output
37+
ob_start();
38+
try {
39+
$this->_handleAjax( 'visualizer-clone-chart' );
40+
} catch ( WPAjaxDieContinueException $e ) {
41+
// We expected this, do nothing.
42+
} catch ( WPAjaxDieStopException $ee ) {
43+
// We expected this, do nothing.
44+
}
45+
ob_end_clean();
46+
47+
$query = new WP_Query(
48+
array(
49+
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
50+
'post_status' => 'auto-draft',
51+
'numberposts' => 1,
52+
'fields' => 'ids',
53+
'post__not_in' => array( $this->chart ),
54+
)
55+
);
56+
57+
$new_id = $query->posts[0];
58+
59+
// all post meta existing in old chart should exist in new chart.
60+
$old_meta = $new_meta = array();
61+
$post_meta = get_post_meta( $this->chart );
62+
foreach ( $post_meta as $key => $value ) {
63+
if ( strpos( $key, 'visualizer-' ) !== false ) {
64+
$old_meta [ $key ] = $value;
65+
}
66+
}
67+
68+
$post_meta = get_post_meta( $new_id );
69+
foreach ( $post_meta as $key => $value ) {
70+
if ( strpos( $key, 'visualizer-' ) !== false ) {
71+
$new_meta [ $key ] = $value;
72+
}
73+
}
74+
75+
$this->assertEquals( $old_meta, $new_meta );
76+
}
77+
2478
/**
2579
* Testing url import feature.
2680
*

0 commit comments

Comments
 (0)