From c0c59b34a8c6b9a9eb8623b8de8811bafad0080b Mon Sep 17 00:00:00 2001 From: contactashish13 Date: Wed, 15 Mar 2017 15:48:09 +0530 Subject: [PATCH 1/4] test import from url #141 --- classes/Visualizer/Module/Chart.php | 9 +- index.php | 24 +- js/frame.js | 2 +- js/render.js | 424 ++++++++++++++-------------- languages/visualizer.pot | 7 +- tests/test-import.php | 84 +++++- 6 files changed, 309 insertions(+), 241 deletions(-) diff --git a/classes/Visualizer/Module/Chart.php b/classes/Visualizer/Module/Chart.php index 242914a8d..92abbf10f 100644 --- a/classes/Visualizer/Module/Chart.php +++ b/classes/Visualizer/Module/Chart.php @@ -443,13 +443,14 @@ public function renderFlattrScript() { */ public function uploadData() { // validate nonce - if ( ! wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ) ) ) { + // do not use filter_input as it does not work for phpunit test cases, use filter_var instead + if ( ! isset( $_GET['nonce'] ) || ! wp_verify_nonce( $_GET['nonce'] ) ) { status_header( 403 ); exit; } // check chart, if chart exists - $chart_id = filter_input( INPUT_GET, 'chart', FILTER_VALIDATE_INT ); + $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : ''; if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) { status_header( 400 ); exit; @@ -457,7 +458,7 @@ public function uploadData() { $source = null; $render = new Visualizer_Render_Page_Update(); - if ( filter_input( INPUT_POST, 'remote_data', FILTER_VALIDATE_URL ) ) { + if ( isset( $_POST['remote_data'] ) && filter_var( $_POST['remote_data'], FILTER_VALIDATE_URL ) ) { $source = new Visualizer_Source_Csv_Remote( $_POST['remote_data'] ); } elseif ( isset( $_FILES['local_data'] ) && $_FILES['local_data']['error'] == 0 ) { $source = new Visualizer_Source_Csv( $_FILES['local_data']['tmp_name'] ); @@ -488,7 +489,7 @@ public function uploadData() { } $render->render(); - exit; + wp_die(); } /** diff --git a/index.php b/index.php index 2d05829a1..6a9d0104e 100644 --- a/index.php +++ b/index.php @@ -90,14 +90,6 @@ function visualizer_launch() { define( 'VISUALIZER_CSV_ENCLOSURE', '"' ); } - // don't load the plugin if cron job is running or doing autosave - $doing_autosave = defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE; - $doing_cron = defined( 'DOING_CRON' ) && DOING_CRON; - $doing_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX; - if ( $doing_autosave || $doing_cron ) { - return; - } - // instantiate the plugin $plugin = Visualizer_Plugin::instance(); @@ -105,17 +97,13 @@ function visualizer_launch() { $plugin->setModule( Visualizer_Module_Setup::NAME ); $plugin->setModule( Visualizer_Module_Sources::NAME ); - if ( $doing_ajax ) { - // set ajax modules - $plugin->setModule( Visualizer_Module_Chart::NAME ); + $plugin->setModule( Visualizer_Module_Chart::NAME ); + if ( is_admin() ) { + // set admin modules + $plugin->setModule( Visualizer_Module_Admin::NAME ); } else { - if ( is_admin() ) { - // set admin modules - $plugin->setModule( Visualizer_Module_Admin::NAME ); - } else { - // set frontend modules - $plugin->setModule( Visualizer_Module_Frontend::NAME ); - } + // set frontend modules + $plugin->setModule( Visualizer_Module_Frontend::NAME ); } } diff --git a/js/frame.js b/js/frame.js index 513ff8aaa..3c3c1675d 100644 --- a/js/frame.js +++ b/js/frame.js @@ -31,7 +31,7 @@ parent.addClass('open'); } }); - $('#vz-import-file').click(function () { + $('#view-remote-file').click(function () { var url = $(this).parent().find('#remote-data').val(); if (url !== '') { diff --git a/js/render.js b/js/render.js index 0ad81630f..0ef1b1572 100644 --- a/js/render.js +++ b/js/render.js @@ -1,251 +1,249 @@ /* global google */ /* global visualizer */ -(function (v, g) { - var gv; - - v.objects = {}; - - v.renderChart = function (id) { - var chart, render, container, series, data, table, settings, i, j, row, date, axis, property, format, formatter; - - chart = v.charts[id]; - series = chart.series; - data = chart.data; - settings = chart.settings; - - container = document.getElementById(id); - table = new gv.DataTable({cols: series}); - - render = v.objects[id] || null; - if (!render) { - switch (chart.type) { - case "gauge": - case "table": - case "timeline": - render = chart.type.charAt(0).toUpperCase() + chart.type.slice(1); - break; - default: - render = chart.type.charAt(0).toUpperCase() + chart.type.slice(1) + 'Chart'; - break; - } +(function(v, g) { + var gv; - render = new gv[render](container); - } + v.objects = {}; - switch (v.charts[id].type) { - case 'pie': - if (settings.slices) { - for (i in settings.slices) { - if (settings.slices[i]['color'] === '') { - delete settings.slices[i]['color']; - } - } - } - break; - case 'line': - case 'bar': - case 'column': - case 'area': - case 'scatter': - case 'candlestick': - if (settings.series) { - for (i in settings.series) { - if (settings.series[i]['color'] === '') { - delete settings.series[i]['color']; - } - } - } + v.renderChart = function(id) { + var chart, render, container, series, data, table, settings, i, j, row, date, axis, property, format, formatter; - if (settings.series && settings.legend && settings.legend.position === "left") { - settings.targetAxisIndex = 1; - } - break; - case 'geo': - if (settings.region !== undefined && settings.region.replace(/^\s+|\s+$/g, '') === '') { - settings['region'] = 'world'; - } - break; - case 'table': - if (parseInt(settings['pagination']) !== 1) { - delete settings['pageSize']; - } - break; - case 'gauge': - break; - case 'timeline': - settings['timeline'] = []; - settings['timeline']['groupByRowLabel'] = settings['groupByRowLabel'] ? true : false; - settings['timeline']['colorByRowLabel'] = settings['colorByRowLabel'] ? true : false; - settings['timeline']['showRowLabels'] = settings['showRowLabels'] ? true : false; - if (settings['singleColor'] !== '') { - settings['timeline']['singleColor'] = settings['singleColor']; - } - break; - case 'combo': - if (settings.series) { - for (i in settings.series) { - if (settings.series[i]['type'] === '') { - delete settings.series[i]['type']; - } - if (settings.series[i]['color'] === '') { - delete settings.series[i]['color']; - } - } - } + chart = v.charts[id]; + series = chart.series; + data = chart.data; + settings = chart.settings; - if (settings.series && settings.legend && settings.legend.position === "left") { - settings.targetAxisIndex = 1; - } + container = document.getElementById(id); + table = new gv.DataTable({cols: series}); + + render = v.objects[id] || null; + if (!render) { + switch (chart.type) { + case "gauge": + case "table": + case "timeline": + render = chart.type.charAt(0).toUpperCase() + chart.type.slice(1); break; default: - return; + render = chart.type.charAt(0).toUpperCase() + chart.type.slice(1) + 'Chart'; + break; } - if (series[0] && (series[0].type === 'date' || series[0].type === 'datetime')) { - axis = false; - switch (v.charts[id].type) { - case 'line': - case 'area': - case 'scatter': - case 'candlestick': - case 'column': - axis = settings.hAxis; - break; - case 'bar': - axis = settings.vAxis; - break; - } - - if (axis) { - for (property in axis.viewWindow) { - date = new Date(axis.viewWindow[property]); - if (Object.prototype.toString.call(date) === "[object Date]") { - if (!isNaN(date.getTime())) { - axis.viewWindow[property] = date; - continue; - } - } - - delete axis.viewWindow[property]; - } + render = new gv[render](container); + } + + switch (v.charts[id].type) { + case 'pie': + if (settings.slices) { + for (i in settings.slices) { + if (settings.slices[i]['color'] === '') { + delete settings.slices[i]['color']; + } + } + } + break; + case 'line': + case 'bar': + case 'column': + case 'area': + case 'scatter': + case 'candlestick': + if (settings.series) { + for (i in settings.series) { + if (settings.series[i]['color'] === '') { + delete settings.series[i]['color']; + } + } + } + + if (settings.series && settings.legend && settings.legend.position === "left") + { + settings.targetAxisIndex = 1; } - } - - if (settings.hAxis) { - if (settings.hAxis.textStyle && settings.hAxis.textStyle !== '') { - settings.hAxis.textStyle = {color: settings.hAxis.textStyle}; + break; + case 'geo': + if (settings.region !== undefined && settings.region.replace(/^\s+|\s+$/g, '') === '') { + settings['region'] = 'world'; + } + break; + case 'table': + if (parseInt(settings['pagination']) !== 1) + { + delete settings['pageSize']; } - } - - if (settings.vAxis) { - if (settings.vAxis.textStyle && settings.vAxis.textStyle !== '') { - settings.vAxis.textStyle = {color: settings.vAxis.textStyle}; + break; + case 'gauge': + break; + case 'timeline': + settings['timeline'] = []; + settings['timeline']['groupByRowLabel'] = settings['groupByRowLabel'] ? true : false; + settings['timeline']['colorByRowLabel'] = settings['colorByRowLabel'] ? true : false; + settings['timeline']['showRowLabels'] = settings['showRowLabels'] ? true : false; + if(settings['singleColor'] !== '') { + settings['timeline']['singleColor'] = settings['singleColor']; } - } - - for (i = 0; i < data.length; i++) { - row = []; - for (j = 0; j < series.length; j++) { - if (series[j].type === 'date' || series[j].type === 'datetime') { - date = new Date(data[i][j]); - data[i][j] = null; - if (Object.prototype.toString.call(date) === "[object Date]") { - if (!isNaN(date.getTime())) { - data[i][j] = date; - } - } - } - row.push(data[i][j]); + break; + case 'combo': + if (settings.series) { + for (i in settings.series) { + if (settings.series[i]['type'] === '') { + delete settings.series[i]['type']; + } + if (settings.series[i]['color'] === '') { + delete settings.series[i]['color']; + } + } + } + + if (settings.series && settings.legend && settings.legend.position === "left") + { + settings.targetAxisIndex = 1; } - table.addRow(row); + break; + default: + return; + } + + if (series[0] && (series[0].type === 'date' || series[0].type === 'datetime')) { + axis = false; + switch (v.charts[id].type) { + case 'line': + case 'area': + case 'scatter': + case 'candlestick': + case 'column': + axis = settings.hAxis; + break; + case 'bar': + axis = settings.vAxis; + break; + } + + if (axis) { + for (property in axis.viewWindow) { + date = new Date(axis.viewWindow[property]); + if (Object.prototype.toString.call(date) === "[object Date]") { + if (!isNaN(date.getTime())) { + axis.viewWindow[property] = date; + continue; + } + } + + delete axis.viewWindow[property]; + } + } + } + + if(settings.hAxis){ + if(settings.hAxis.textStyle && settings.hAxis.textStyle !== ''){ + settings.hAxis.textStyle = {color: settings.hAxis.textStyle}; } + } - if (settings.series) { - for (i = 0; i < settings.series.length; i++) { - format = settings.series[i].format; - if (!format || format === '' || !series[i + 1]) { - { - continue; - } - - formatter = null; - switch (series[i + 1].type) { - case 'number': - formatter = new g.visualization.NumberFormat({pattern: format}); - break; - case 'date': - case 'datetime': - case 'timeofday': - formatter = new g.visualization.DateFormat({pattern: format}); - break; - } - - if (formatter) { - formatter.format(table, i + 1); - } - } - } - - render.draw(table, settings); + if(settings.vAxis){ + if(settings.vAxis.textStyle && settings.vAxis.textStyle !== ''){ + settings.vAxis.textStyle = {color: settings.vAxis.textStyle}; } - - - v.render = function () { - for (var id in (v.charts || {})) { - v.renderChart(id); - } - }; - - g.charts.load("current", {packages: ["corechart", "geochart", "gauge", "table", "timeline"]}); - g.charts.setOnLoadCallback(function () { - gv = g.visualization; - v.render(); - }); - }; - })(visualizer, google); - -(function ($, v) { - var resizeTimeout; - - $(document).ready(function () { - $(window).resize(function () { - clearTimeout(resizeTimeout); - resizeTimeout = setTimeout(v.render, 100); - }); + } + + for (i = 0; i < data.length; i++) { + row = []; + for (j = 0; j < series.length; j++) { + if (series[j].type === 'date' || series[j].type === 'datetime') { + date = new Date(data[i][j]); + data[i][j] = null; + if (Object.prototype.toString.call(date) === "[object Date]") { + if (!isNaN(date.getTime())) { + data[i][j] = date; + } + } + } + row.push(data[i][j]); + } + table.addRow(row); + } + + if (settings.series) { + for (i = 0; i < settings.series.length; i++) { + format = settings.series[i].format; + if (!format || format === '' || !series[i + 1]) { + continue; + } + + formatter = null; + switch (series[i + 1].type) { + case 'number': + formatter = new g.visualization.NumberFormat({pattern: format}); + break; + case 'date': + case 'datetime': + case 'timeofday': + formatter = new g.visualization.DateFormat({pattern: format}); + break; + } + + if (formatter) { + formatter.format(table, i + 1); + } + } + } + + render.draw(table, settings); + }; + + v.render = function() { + for (var id in (v.charts || {})) { + v.renderChart(id); + } + }; + + g.charts.load("current", {packages: ["corechart", "geochart", "gauge", "table", "timeline"]}); + g.charts.setOnLoadCallback(function() { + gv = g.visualization; + v.render(); + }); +})(visualizer, google); + +(function($, v) { + var resizeTimeout; + + $(document).ready(function() { + $(window).resize(function() { + clearTimeout(resizeTimeout); + resizeTimeout = setTimeout(v.render, 100); + }); resizeHiddenContainers(true); }); - $(window).load(function () { + $(window).load(function(){ resizeHiddenContainers(true); }); - function resizeHiddenContainers(everytime) { - $(".visualizer-front").parents().each(function () { - if (!$(this).is(":visible") && !$(this).hasClass("visualizer-hidden-container")) { + function resizeHiddenContainers(everytime){ + $(".visualizer-front").parents().each(function(){ + if(!$(this).is(":visible") && !$(this).hasClass("visualizer-hidden-container")){ $(this).addClass("visualizer-hidden-container"); } }); - var mutateObserver = new MutationObserver(function (records) { - records.forEach(function (record) { - if (record.attributeName === "style" || record.attributeName === "class") { - var element = $(record.target); - var displayStyle = window.getComputedStyle(element[0]).getPropertyValue("display"); - if (element.hasClass("visualizer-hidden-container-resized") || displayStyle === "none") { - return; - } + var mutateObserver = new MutationObserver(function(records) { + records.forEach(function(record) { + if(record.attributeName === "style" || record.attributeName === "class"){ + var element = $(record.target); + var displayStyle = window.getComputedStyle(element[0]).getPropertyValue("display"); + if(element.hasClass("visualizer-hidden-container-resized") || displayStyle === "none") { return ; } element.find(".visualizer-front").resize(); - if (!everytime) { - element.addClass("visualizer-hidden-container-resized"); + if(!everytime) { + element.addClass("visualizer-hidden-container-resized"); } } }); }); - $('.visualizer-hidden-container').each(function () { + $('.visualizer-hidden-container').each(function(){ mutateObserver.observe($(this)[0], {attributes: true}); }); - } + } })(jQuery, visualizer); \ No newline at end of file diff --git a/languages/visualizer.pot b/languages/visualizer.pot index d84aa5713..6d63a0bc0 100644 --- a/languages/visualizer.pot +++ b/languages/visualizer.pot @@ -3,7 +3,8 @@ msgid "" msgstr "" "Project-Id-Version: Visualizer: Charts and Graphs Lite 2.0.0\n" -"Report-Msgid-Bugs-To: https://github.com/Codeinwp/visualizer/issues\n" +"Report-Msgid-Bugs-To: https://github.com/Codeinwp/visualizer/issues\n" +"POT-Creation-Date: 2017-03-15 09:39:51+00:00\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" @@ -117,11 +118,11 @@ msgstr "" msgid "Insert Chart" msgstr "" -#: classes/Visualizer/Module/Chart.php:471 +#: classes/Visualizer/Module/Chart.php:472 msgid "CSV file with chart data was not uploaded. Please, try again." msgstr "" -#: classes/Visualizer/Module/Chart.php:486 +#: classes/Visualizer/Module/Chart.php:487 msgid "CSV file is broken or invalid. Please, try again." msgstr "" diff --git a/tests/test-import.php b/tests/test-import.php index 80ade68ef..685fafedd 100644 --- a/tests/test-import.php +++ b/tests/test-import.php @@ -9,11 +9,91 @@ * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License * @since 2.0.0 */ -class Test_Import extends WP_UnitTestCase { +class Test_Import extends WP_Ajax_UnitTestCase { + + /** + * The chart id of the chart created + * + * @since 2.0.0 + * + * @access private + * @var int + */ + private $chart; + + /** + * Create a chart + * + * @since 2.0.0 + * + * @access private + */ + private function create_chart() { + $id = wp_insert_post(array( + 'post_type' => 'visualizer', + '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;}}', + 'post_status' => 'publish', + )); + + update_post_meta( $id, 'visualizer-chart-type', 'pie' ); + 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";}}' ); + update_post_meta( $id, 'visualizer-default-data', 0 ); + update_post_meta( $id, 'visualizer-source', 'Visualizer_Source_Csv' ); + 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:"";}}' ); + + $this->chart = $id; + } + /** * Testing file import feature. + * + * @access public + * @dataProvider urlProvider */ - public function test_file_import() { + public function test_url_import( $url ) { + $this->create_chart(); + $this->_setRole( 'administrator' ); + $_POST = array( + 'remote_data' => $url, + ); + $_GET = array( + 'nonce' => wp_create_nonce(), + 'chart' => $this->chart, + ); + + // swallow the output + ob_start(); + try { + $this->_handleAjax( 'visualizer-upload-data' ); + } catch ( WPAjaxDieContinueException $e ) { + // We expected this, do nothing. + } catch ( WPAjaxDieStopException $ee) { + // We expected this, do nothing. + } + ob_end_clean(); + + $series = get_post_meta( $this->chart, 'visualizer-series', true ); + $chart = get_post( $this->chart ); + $src = get_post_meta( $this->chart, 'visualizer-source', true ); + $content = $chart->post_content; + + $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;}}}'; + + $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";}}' ); + + $this->assertEquals( 'Visualizer_Source_Csv_Remote', $src ); + $this->assertEquals( $content, $content_line ); + $this->assertEquals( $series, $series_line ); + } + + + /** + * Provide the URL for uploading the file + * + * @access public + */ + public function urlProvider() { + return [ [ 'http://localhost/wp-content/plugins/wp-visualizer/samples/line.csv' ] ]; } } From cbd4382113a136fe464101fa3082f9147c117802 Mon Sep 17 00:00:00 2001 From: contactashish13 Date: Wed, 15 Mar 2017 19:16:08 +0530 Subject: [PATCH 2/4] test import from file #140 skipping import url test till solution is found --- classes/Visualizer/Module/Chart.php | 12 ++-- languages/visualizer.pot | 2 +- tests/test-import.php | 101 +++++++++++++++++++++++----- 3 files changed, 93 insertions(+), 22 deletions(-) diff --git a/classes/Visualizer/Module/Chart.php b/classes/Visualizer/Module/Chart.php index 92abbf10f..3ecf7fbf8 100644 --- a/classes/Visualizer/Module/Chart.php +++ b/classes/Visualizer/Module/Chart.php @@ -204,10 +204,10 @@ public function deleteChart() { * @access public */ public function renderChartPages() { - define( 'IFRAME_REQUEST', 1 ); + defined( 'IFRAME_REQUEST' ) || define( 'IFRAME_REQUEST', 1 ); // check chart, if chart not exists, will create new one and redirects to the same page with proper chart id - $chart_id = filter_input( INPUT_GET, 'chart', FILTER_VALIDATE_INT ); + $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : ''; if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) { $default_type = 'line'; @@ -231,7 +231,7 @@ public function renderChartPages() { } wp_redirect( add_query_arg( 'chart', (int) $chart_id ) ); - exit; + wp_die(); } // enqueue and register scripts and styles @@ -251,7 +251,7 @@ public function renderChartPages() { // dispatch pages $this->_chart = $chart; - switch ( filter_input( INPUT_GET, 'tab' ) ) { + switch ( isset( $_GET['tab'] ) ? $_GET['tab'] : '' ) { case 'settings': // changed by Ash/Upwork $this->_handleDataAndSettingsPage(); @@ -262,7 +262,7 @@ public function renderChartPages() { break; } - exit; + wp_die(); } /** @@ -351,7 +351,7 @@ private function _handleDataPage() { * Handle data and settings page */ private function _handleDataAndSettingsPage() { - if ( $_SERVER['REQUEST_METHOD'] == 'POST' && wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ) ) ) { + if ( $_SERVER['REQUEST_METHOD'] == 'POST' && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'] ) ) { if ( $this->_chart->post_status == 'auto-draft' ) { $this->_chart->post_status = 'publish'; wp_update_post( $this->_chart->to_array() ); diff --git a/languages/visualizer.pot b/languages/visualizer.pot index 6d63a0bc0..a54c90452 100644 --- a/languages/visualizer.pot +++ b/languages/visualizer.pot @@ -4,7 +4,7 @@ msgid "" msgstr "" "Project-Id-Version: Visualizer: Charts and Graphs Lite 2.0.0\n" "Report-Msgid-Bugs-To: https://github.com/Codeinwp/visualizer/issues\n" -"POT-Creation-Date: 2017-03-15 09:39:51+00:00\n" +"POT-Creation-Date: 2017-03-15 13:43:24+00:00\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" diff --git a/tests/test-import.php b/tests/test-import.php index 685fafedd..2f068f9a2 100644 --- a/tests/test-import.php +++ b/tests/test-import.php @@ -29,28 +29,41 @@ class Test_Import extends WP_Ajax_UnitTestCase { * @access private */ private function create_chart() { - $id = wp_insert_post(array( - 'post_type' => 'visualizer', - '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;}}', - 'post_status' => 'publish', - )); + $this->_setRole( 'administrator' ); + + $_GET = array( + 'library' => 'yes', + 'tab' => 'visualizer', + ); - update_post_meta( $id, 'visualizer-chart-type', 'pie' ); - 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";}}' ); - update_post_meta( $id, 'visualizer-default-data', 0 ); - update_post_meta( $id, 'visualizer-source', 'Visualizer_Source_Csv' ); - 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:"";}}' ); + // swallow the output + ob_start(); + try { + $this->_handleAjax( 'visualizer-create-chart' ); + } catch ( WPAjaxDieContinueException $e ) { + // We expected this, do nothing. + } catch ( WPAjaxDieStopException $ee ) { + // We expected this, do nothing. + } + ob_end_clean(); - $this->chart = $id; + $query = new WP_Query(array( + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, + 'post_status' => 'auto-draft', + 'numberposts' => 1, + 'fields' => 'ids', + )); + $this->chart = $query->posts[0]; } /** - * Testing file import feature. + * Testing url import feature. * * @access public * @dataProvider urlProvider */ public function test_url_import( $url ) { + $this->markTestSkipped( 'this test is disabled till we can figure out how to provide a "local" url' ); $this->create_chart(); $this->_setRole( 'administrator' ); @@ -78,15 +91,73 @@ public function test_url_import( $url ) { $src = get_post_meta( $this->chart, 'visualizer-source', true ); $content = $chart->post_content; - $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;}}}'; + $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;}}}'; - $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";}}' ); + $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";}}' ); $this->assertEquals( 'Visualizer_Source_Csv_Remote', $src ); $this->assertEquals( $content, $content_line ); $this->assertEquals( $series, $series_line ); } + /** + * Testing file import feature. + * + * @access public + * @dataProvider fileProvider + */ + public function test_file_import( $file ) { + $this->create_chart(); + $this->_setRole( 'administrator' ); + + $dest = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . basename( $file ); + copy( $file, $dest ); + + $_FILES = array( + 'local_data' => array( + 'tmp_name' => $dest, + 'error' => 0, + ), + ); + $_GET = array( + 'nonce' => wp_create_nonce(), + 'chart' => $this->chart, + ); + + // swallow the output + ob_start(); + try { + $this->_handleAjax( 'visualizer-upload-data' ); + } catch ( WPAjaxDieContinueException $e ) { + // We expected this, do nothing. + } catch ( WPAjaxDieStopException $ee) { + // We expected this, do nothing. + } + ob_end_clean(); + unlink( $dest ); + + $series = get_post_meta( $this->chart, 'visualizer-series', true ); + $chart = get_post( $this->chart ); + $src = get_post_meta( $this->chart, 'visualizer-source', true ); + $content = $chart->post_content; + + $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;}}'; + + $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";}}' ); + + $this->assertEquals( 'Visualizer_Source_Csv', $src ); + $this->assertEquals( $content, $content_line ); + $this->assertEquals( $series, $series_line ); + } + + /** + * Provide the fileURL for uploading the file + * + * @access public + */ + public function fileProvider() { + return [ [ VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'bar.csv' ] ]; + } /** * Provide the URL for uploading the file @@ -94,6 +165,6 @@ public function test_url_import( $url ) { * @access public */ public function urlProvider() { - return [ [ 'http://localhost/wp-content/plugins/wp-visualizer/samples/line.csv' ] ]; + return [ [ 'http://localhost/wp-content/plugins/wp-visualizer/samples/bar.csv' ] ]; } } From 3ceab831384ae2449ae3ff799be2d73399892d65 Mon Sep 17 00:00:00 2001 From: contactashish13 Date: Wed, 15 Mar 2017 19:28:18 +0530 Subject: [PATCH 3/4] test import from file #140 --- languages/visualizer.pot | 2 +- tests/test-import.php | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/languages/visualizer.pot b/languages/visualizer.pot index a54c90452..b1382f139 100644 --- a/languages/visualizer.pot +++ b/languages/visualizer.pot @@ -4,7 +4,7 @@ msgid "" msgstr "" "Project-Id-Version: Visualizer: Charts and Graphs Lite 2.0.0\n" "Report-Msgid-Bugs-To: https://github.com/Codeinwp/visualizer/issues\n" -"POT-Creation-Date: 2017-03-15 13:43:24+00:00\n" +"POT-Creation-Date: 2017-03-15 13:57:55+00:00\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" diff --git a/tests/test-import.php b/tests/test-import.php index 2f068f9a2..40c6f510d 100644 --- a/tests/test-import.php +++ b/tests/test-import.php @@ -156,7 +156,9 @@ public function test_file_import( $file ) { * @access public */ public function fileProvider() { - return [ [ VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'bar.csv' ] ]; + return array( + array( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'bar.csv' ), + ); } /** @@ -165,6 +167,8 @@ public function fileProvider() { * @access public */ public function urlProvider() { - return [ [ 'http://localhost/wp-content/plugins/wp-visualizer/samples/bar.csv' ] ]; + return array( + array( 'http://localhost/wp-content/plugins/wp-visualizer/samples/bar.csv' ), + ); } } From 0511105ba3bd1421ae71558e87c1f6da907a8250 Mon Sep 17 00:00:00 2001 From: contactashish13 Date: Thu, 16 Mar 2017 10:37:43 +0530 Subject: [PATCH 4/4] phpunit more tests #142 --- classes/Visualizer/Module/Chart.php | 8 +-- languages/visualizer.pot | 2 +- tests/test-export.php | 90 +++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 tests/test-export.php diff --git a/classes/Visualizer/Module/Chart.php b/classes/Visualizer/Module/Chart.php index 3ecf7fbf8..642e52c04 100644 --- a/classes/Visualizer/Module/Chart.php +++ b/classes/Visualizer/Module/Chart.php @@ -250,7 +250,7 @@ public function renderChartPages() { } // dispatch pages - $this->_chart = $chart; + $this->_chart = get_post( $chart_id ); switch ( isset( $_GET['tab'] ) ? $_GET['tab'] : '' ) { case 'settings': // changed by Ash/Upwork @@ -552,7 +552,7 @@ public function exportData() { $chart_id = $success = false; $capable = current_user_can( 'edit_posts' ); if ( $capable ) { - $chart_id = filter_input( INPUT_GET, 'chart', FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => 1 ) ) ); + $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => 1 ) ) ) : ''; if ( $chart_id ) { $chart = get_post( $chart_id ); $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER; @@ -561,7 +561,7 @@ public function exportData() { if ( $success ) { $settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true ); - $filename = $settings['title']; + $filename = isset( $settings['title'] ) ? $settings['title'] : ''; if ( empty( $filename ) ) { $filename = 'export.csv'; } else { @@ -620,6 +620,6 @@ public function exportData() { )); }// End if(). - exit; + wp_die(); } } diff --git a/languages/visualizer.pot b/languages/visualizer.pot index b1382f139..66353195a 100644 --- a/languages/visualizer.pot +++ b/languages/visualizer.pot @@ -4,7 +4,7 @@ msgid "" msgstr "" "Project-Id-Version: Visualizer: Charts and Graphs Lite 2.0.0\n" "Report-Msgid-Bugs-To: https://github.com/Codeinwp/visualizer/issues\n" -"POT-Creation-Date: 2017-03-15 13:57:55+00:00\n" +"POT-Creation-Date: 2017-03-16 05:02:53+00:00\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" diff --git a/tests/test-export.php b/tests/test-export.php new file mode 100644 index 000000000..7348b1ad3 --- /dev/null +++ b/tests/test-export.php @@ -0,0 +1,90 @@ +_setRole( 'administrator' ); + + $_GET = array( + 'library' => 'yes', + 'tab' => 'visualizer', + ); + + // swallow the output + ob_start(); + try { + $this->_handleAjax( 'visualizer-create-chart' ); + } catch ( WPAjaxDieContinueException $e ) { + // We expected this, do nothing. + } catch ( WPAjaxDieStopException $ee ) { + // We expected this, do nothing. + } + ob_end_clean(); + + $query = new WP_Query(array( + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, + 'post_status' => 'auto-draft', + 'numberposts' => 1, + 'fields' => 'ids', + )); + $this->chart = $query->posts[0]; + } + + /** + * Testing export + * + * @access public + */ + public function test_download_export() { + $this->create_chart(); + $this->_setRole( 'administrator' ); + + $_GET = array( + 'security' => wp_create_nonce( Visualizer_Plugin::ACTION_EXPORT_DATA . Visualizer_Plugin::VERSION ), + 'chart' => $this->chart, + ); + + // swallow the output + ob_start(); + try { + $this->_handleAjax( 'visualizer-export-data' ); + } catch ( WPAjaxDieContinueException $e ) { + // We expected this, do nothing. + } catch ( WPAjaxDieStopException $ee) { + // We expected this, do nothing. + } + ob_end_clean(); + + $response = json_decode( $this->_last_response ); + $this->assertInternalType( 'object', $response ); + $this->assertObjectHasAttribute( 'success', $response ); + $this->assertObjectHasAttribute( 'data', $response ); + $this->assertTrue( $response->success ); + } +}