Documentation
-
To get started with Visualizer, we recommend you first bookmark our main documentation page here.
+
To get started with Visualizer, we recommend you first bookmark our main documentation page here.
- Notably, you could take a look at this first introductory tutorial: How to create my first chart.
+ Notably, you could take a look at this first introductory tutorial: How to create my first chart.
If you prefer learning through video, this could prove useful. It is a little dated however.
diff --git a/tests/e2e/specs/admin.spec.js b/tests/e2e/specs/admin.spec.js
index 3534105ac..3a6b9488a 100644
--- a/tests/e2e/specs/admin.spec.js
+++ b/tests/e2e/specs/admin.spec.js
@@ -211,6 +211,41 @@ test.describe( 'Chart Library', () => {
await expect( page.getByText(backendName) ).toBeVisible();
await expect( page.locator('g').filter({ hasText: 'Test Chart Name' }).locator('rect') ).toBeVisible();
} );
+
+ // frame.js rewrites the Google API link for the chart type. It must leave the
+ // "Common Snippets" docs link and its UTMs alone, on both libraries.
+ for ( const library of [ 'GoogleCharts', 'ChartJS' ] ) {
+ test( `manual configuration links (${ library })`, async ( { admin, page } ) => {
+ await admin.visitAdminPage( 'admin.php?page=visualizer&vaction=addnew' );
+ await page.waitForURL( '**/admin.php?page=visualizer&vaction=addnew' );
+ await expect( page.getByRole('button', { name: 'Classic Builder Step-by-step' }) ).toBeVisible({ timeout: 5000 });
+ await page.getByRole('button', { name: 'Classic Builder Step-by-step' }).click();
+ await page.waitForSelector('h1:text("Visualizer")');
+
+ const frame = page.frameLocator('iframe');
+ await frame.getByRole('combobox').selectOption({ value: library });
+ await selectChartAdmin( frame, CHART_JS_LABELS.pie );
+ await expect( frame.getByRole('button', { name: 'Create Chart' }) ).toBeVisible({ timeout: 5000 });
+
+ const snippets = frame.locator('span.viz-gvlink a').filter({ hasText: 'Common Snippets' });
+ await expect( snippets ).toHaveAttribute(
+ 'href',
+ 'https://docs.themeisle.com/visualizer-charts-and-graphs/manual-configuration?utm_source=wpadmin&utm_medium=chart-editor&utm_campaign=manual-configuration&utm_content=docsvisualizer-charts-and-graphsmanual-configuration'
+ );
+
+ if ( library === 'GoogleCharts' ) {
+ await expect( frame.locator('span.viz-gvlink a').filter({ hasText: 'Google Visualization API' }) ).toHaveAttribute(
+ 'href',
+ 'https://developers.google.com/chart/interactive/docs/gallery/piechart#configuration-options'
+ );
+ // The data filter box keeps its own text and link.
+ await expect( frame.locator('span.viz-gvlink a').filter({ hasText: 'Chart Controls API' }) ).toHaveAttribute(
+ 'href',
+ 'https://developers.google.com/chart/interactive/docs/gallery/controls#controls-gallery'
+ );
+ }
+ } );
+ }
} );
test.describe( 'Support', () => {
diff --git a/tests/js/render-datatables-settings.test.js b/tests/js/render-datatables-settings.test.js
new file mode 100644
index 000000000..394e56a4a
--- /dev/null
+++ b/tests/js/render-datatables-settings.test.js
@@ -0,0 +1,100 @@
+const assert = require( 'node:assert/strict' );
+const path = require( 'path' );
+const { afterEach, beforeEach, describe, it } = require( 'node:test' );
+
+describe( 'DataTables nested settings', () => {
+ let handlers;
+ let capturedSettings;
+ let alertCalls;
+
+ beforeEach( () => {
+ handlers = {};
+ capturedSettings = null;
+ alertCalls = 0;
+
+ global.window = {};
+ global.document = {
+ getElementById: () => ( {} ),
+ };
+ global.alert = () => {
+ alertCalls++;
+ };
+
+ const collection = {
+ length: 0,
+ hasClass: () => false,
+ children: () => ( { length: 0 } ),
+ find: () => ( { length: 0 } ),
+ empty() {
+ return this;
+ },
+ append() {
+ return this;
+ },
+ css: () => '400px',
+ addClass() {
+ return this;
+ },
+ remove() {
+ return this;
+ },
+ on( event, callback ) {
+ handlers[ event ] = callback;
+ return this;
+ },
+ trigger() {
+ return this;
+ },
+ DataTable( settings ) {
+ capturedSettings = settings;
+ return this;
+ },
+ };
+
+ const jquery = () => collection;
+ jquery.extend = ( target, ...sources ) => Object.assign( target, ...sources );
+ jquery.fn = {
+ dataTable: {
+ render: {
+ number: () => null,
+ },
+ },
+ };
+ global.jQuery = jquery;
+
+ const renderer = path.resolve( __dirname, '../../js/render-datatables.js' );
+ delete require.cache[ renderer ];
+ require( renderer );
+ } );
+
+ afterEach( () => {
+ delete global.alert;
+ delete global.document;
+ delete global.jQuery;
+ delete global.window;
+ } );
+
+ function renderWithSettings( settings ) {
+ handlers[ 'visualizer:render:specificchart:start' ]( {}, {
+ id: 'chart',
+ chart: {
+ library: 'datatables',
+ settings,
+ series: [ { label: 'Value', type: 'string' } ],
+ data: [ [ 'Example' ] ],
+ },
+ v: { page_type: 'frontend' },
+ } );
+ }
+
+ it( 'does not execute an unsupported nested setting name', () => {
+ assert.doesNotThrow( () => renderWithSettings( { 'alert(1)_x': 'value' } ) );
+ assert.equal( alertCalls, 0 );
+ } );
+
+ it( 'continues to apply the supported select nested setting', () => {
+ renderWithSettings( { select_info: 'true' } );
+ assert.equal( capturedSettings.select.info, true );
+ assert.equal( capturedSettings.info, true );
+ } );
+} );
diff --git a/tests/php/static-analysis-stubs/symbols.php b/tests/php/static-analysis-stubs/symbols.php
index 0e17329a9..9df272ecc 100644
--- a/tests/php/static-analysis-stubs/symbols.php
+++ b/tests/php/static-analysis-stubs/symbols.php
@@ -31,11 +31,11 @@
define( 'VISUALIZER_ENABLE_BETA_FEATURES', '' );
// the link to pre-build queries.
-define( 'VISUALIZER_DB_QUERY_DOC_URL', 'https://docs.themeisle.com/article/970-visualizer-sample-queries-to-generate-charts' );
-define( 'VISUALIZER_MAIN_DOC', 'https://docs.themeisle.com/category/657-visualizer' );
+define( 'VISUALIZER_DB_QUERY_DOC_URL', 'https://docs.themeisle.com/visualizer-charts-and-graphs/visualizer-sample-queries-to-generate-charts' );
+define( 'VISUALIZER_MAIN_DOC', 'https://docs.themeisle.com/visualizer-charts-and-graphs/' );
define( 'VISUALIZER_DOC_COLLECTION', 'https://docs.themeisle.com/search?collectionId=561ec249c69791452ed4bceb&query=#+visualizer' );
define( 'VISUALIZER_DEMO_URL', 'https://demo.themeisle.com/visualizer/#' );
-define( 'VISUALIZER_CODE_SNIPPETS_URL', 'https://docs.themeisle.com/category/726-visualizer' );
+define( 'VISUALIZER_CODE_SNIPPETS_URL', 'https://docs.themeisle.com/visualizer-charts-and-graphs/' );
define( 'VISUALIZER_SUBSCRIBE_API', 'https://api.themeisle.com/tracking/subscribe' );
// to redirect all themeisle_log_event to error log.
diff --git a/tests/test-ajax.php b/tests/test-ajax.php
index 20a0ed8c4..60fdda461 100644
--- a/tests/test-ajax.php
+++ b/tests/test-ajax.php
@@ -1122,6 +1122,375 @@ public function test_json_schedule_denied_for_chart_user_cannot_edit() {
$this->assertSame( '', get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE, true ) );
}
+ /**
+ * A chart editor without report access cannot schedule a WooCommerce report import.
+ */
+ public function test_woocommerce_json_schedule_denied_without_report_capability() {
+ $this->enable_pro();
+ $chart_id = $this->create_chart_for_user( $this->contibutor_user_id );
+ wp_set_current_user( $this->contibutor_user_id );
+ $_POST = array(
+ 'chart' => $chart_id,
+ 'time' => 12,
+ 'is_woocommerce_report' => 'true',
+ 'security' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE . Visualizer_Plugin::VERSION ),
+ );
+
+ try {
+ $this->_handleAjax( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE );
+ } catch ( WPAjaxDieContinueException $e ) {
+ // Expected after the JSON response.
+ }
+
+ $response = json_decode( $this->_last_response );
+ $this->assertFalse( $response->success );
+ $this->assertSame( '', get_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, true ) );
+ }
+
+ /**
+ * A report-capable chart editor can authorize future scheduled imports.
+ */
+ public function test_woocommerce_json_schedule_records_authorizing_user() {
+ $this->enable_pro();
+ $chart_id = $this->create_chart_for_user( $this->admin_user_id );
+ wp_set_current_user( $this->admin_user_id );
+ wp_get_current_user()->add_cap( 'view_woocommerce_reports' );
+ $this->assertTrue( current_user_can( 'view_woocommerce_reports' ) );
+ $_POST = array(
+ 'chart' => $chart_id,
+ 'time' => 12,
+ 'is_woocommerce_report' => 'true',
+ 'security' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE . Visualizer_Plugin::VERSION ),
+ );
+
+ try {
+ $this->_handleAjax( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE );
+ } catch ( WPAjaxDieContinueException $e ) {
+ // Expected after the JSON response.
+ }
+
+ $response = json_decode( $this->_last_response );
+ $this->assertTrue( $response->success, $this->_last_response );
+ $this->assertSame( $this->admin_user_id, (int) get_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, true ) );
+ }
+
+ /**
+ * Switching a chart to another JSON source keeps the recorded report user.
+ */
+ public function test_json_schedule_for_other_source_keeps_report_user() {
+ $this->enable_pro();
+ $chart_id = $this->create_chart_for_user( $this->admin_user_id );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_IS_WOOCOMMERCE_SOURCE, true );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, $this->admin_user_id );
+ wp_set_current_user( $this->admin_user_id );
+ $_POST = array(
+ 'chart' => $chart_id,
+ 'time' => 12,
+ 'is_woocommerce_report' => 'false',
+ 'security' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE . Visualizer_Plugin::VERSION ),
+ );
+
+ try {
+ $this->_handleAjax( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE );
+ } catch ( WPAjaxDieContinueException $e ) {
+ // Expected after the JSON response.
+ }
+
+ $this->assertSame( '', get_post_meta( $chart_id, Visualizer_Plugin::CF_IS_WOOCOMMERCE_SOURCE, true ) );
+ $this->assertSame( $this->admin_user_id, (int) get_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, true ) );
+ }
+
+ /**
+ * Saving a new JSON endpoint makes the saving user the chart's report user.
+ */
+ public function test_json_source_change_records_saving_user() {
+ $chart_id = $this->create_chart_for_user( $this->admin_user_id );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, 'https://example.com/old.json' );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, $this->admin_user_id );
+ $editor_id = $this->factory->user->create( array( 'role' => 'editor' ) );
+ wp_set_current_user( $editor_id );
+
+ $this->handle_json_set_data( $chart_id, array( 'url' => 'https://example.com/new.json' ) );
+
+ $this->assertSame( 'https://example.com/new.json', get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, true ) );
+ $this->assertSame( $editor_id, (int) get_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, true ) );
+ }
+
+ /**
+ * Saving the same JSON endpoint again keeps the recorded report user.
+ */
+ public function test_json_source_resave_keeps_report_user() {
+ $chart_id = $this->create_chart_for_user( $this->admin_user_id );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, 'https://example.com/data.json' );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, $this->admin_user_id );
+ wp_set_current_user( $this->factory->user->create( array( 'role' => 'editor' ) ) );
+
+ $this->handle_json_set_data( $chart_id, array( 'url' => 'https://example.com/data.json' ) );
+
+ $this->assertSame( $this->admin_user_id, (int) get_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, true ) );
+ }
+
+ /**
+ * Restoring a revision keeps the recorded report user.
+ */
+ public function test_revision_restore_keeps_report_user() {
+ $chart_id = $this->create_chart_for_user( $this->admin_user_id );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, 'https://example.com/data.json' );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, $this->admin_user_id );
+ $revision_id = $this->factory->post->create(
+ array(
+ 'post_type' => 'revision',
+ 'post_parent' => $chart_id,
+ 'post_status' => 'inherit',
+ )
+ );
+ add_metadata( 'post', $revision_id, Visualizer_Plugin::CF_JSON_URL, 'https://example.com/data.json' );
+ add_metadata( 'post', $revision_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, $this->admin_user_id );
+ $editor_id = $this->factory->user->create( array( 'role' => 'editor' ) );
+ wp_set_current_user( $editor_id );
+
+ ( new Visualizer_Module_Admin( Visualizer_Plugin::instance() ) )->restoreRevision( $chart_id, $revision_id );
+
+ $this->assertSame( $this->admin_user_id, (int) get_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, true ) );
+ }
+
+ /**
+ * A cloned chart does not carry the original chart's report user.
+ */
+ public function test_clone_does_not_copy_report_user() {
+ $chart_id = $this->create_chart_for_user( $this->admin_user_id );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, 'https://example.com/data.json' );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, $this->admin_user_id );
+ $editor_id = $this->factory->user->create( array( 'role' => 'editor' ) );
+ wp_set_current_user( $editor_id );
+ $_GET = array(
+ 'nonce' => wp_create_nonce( Visualizer_Plugin::ACTION_CLONE_CHART ),
+ 'chart' => $chart_id,
+ );
+
+ try {
+ $this->_handleAjax( Visualizer_Plugin::ACTION_CLONE_CHART );
+ } catch ( WPAjaxDieContinueException $e ) {
+ // Expected after the redirect.
+ } catch ( WPAjaxDieStopException $e ) {
+ // Expected after the redirect.
+ }
+
+ $clones = get_posts(
+ array(
+ 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
+ 'post_status' => 'any',
+ 'author' => $editor_id,
+ 'fields' => 'ids',
+ )
+ );
+ $this->assertCount( 1, $clones );
+ $this->assertSame( 'https://example.com/data.json', get_post_meta( $clones[0], Visualizer_Plugin::CF_JSON_URL, true ) );
+ $this->assertSame( '', get_post_meta( $clones[0], Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, true ) );
+ }
+
+ /**
+ * A translated copy of a chart does not carry the original chart's report user.
+ */
+ public function test_translation_copy_does_not_copy_report_user() {
+ $chart_id = $this->create_chart_for_user( $this->admin_user_id );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, 'https://example.com/data.json' );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, $this->admin_user_id );
+ $editor_id = $this->factory->user->create( array( 'role' => 'editor' ) );
+ wp_set_current_user( $editor_id );
+ $_GET = array(
+ 'lang' => 'de',
+ 'parent_chart_id' => $chart_id,
+ );
+
+ try {
+ $this->_handleAjax( Visualizer_Plugin::ACTION_CREATE_CHART );
+ } catch ( WPAjaxDieContinueException $e ) {
+ // Expected after the redirect.
+ } catch ( WPAjaxDieStopException $e ) {
+ // Expected after the redirect.
+ }
+
+ $copies = get_posts(
+ array(
+ 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
+ 'post_status' => 'any',
+ 'author' => $editor_id,
+ 'fields' => 'ids',
+ )
+ );
+ $this->assertCount( 1, $copies );
+ $this->assertSame( 'https://example.com/data.json', get_post_meta( $copies[0], Visualizer_Plugin::CF_JSON_URL, true ) );
+ $this->assertSame( '', get_post_meta( $copies[0], Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, true ) );
+ }
+
+ /**
+ * The AI builder edits the stored cell text.
+ */
+ public function test_ai_builder_fetch_returns_stored_cells() {
+ wp_set_current_user( $this->admin_user_id );
+ $chart_id = $this->factory->post->create(
+ array(
+ 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
+ 'post_author' => $this->admin_user_id,
+ 'post_content' => wp_slash( serialize( array( array( 'Q1
Q4' ) ) ) ),
+ )
+ );
+ $_POST = array(
+ 'nonce' => wp_create_nonce( 'visualizer-ai-builder' ),
+ 'chart_id' => $chart_id,
+ );
+
+ try {
+ $this->_handleAjax( 'visualizer-ai-fetch' );
+ } catch ( WPAjaxDieContinueException $e ) {
+ // Expected after the JSON response.
+ }
+
+ $response = json_decode( $this->_last_response );
+ $this->assertTrue( $response->success, $this->_last_response );
+ $this->assertSame( 'Q1Q4', $response->data->data[0][0] );
+ }
+
+ /**
+ * The JSON import preview prints endpoint keys as text.
+ */
+ public function test_json_preview_escapes_endpoint_keys() {
+ $chart_id = $this->create_chart_for_user( $this->admin_user_id );
+ wp_set_current_user( $this->admin_user_id );
+ $filter = $this->mock_json_endpoint( array( array( '
' => 'a' ) ) );
+
+ $_GET['security'] = wp_create_nonce( Visualizer_Plugin::ACTION_JSON_GET_DATA . Visualizer_Plugin::VERSION );
+ $_POST['params'] = array(
+ 'url' => 'https://example.com/data.json',
+ 'method' => 'GET',
+ 'chart' => $chart_id,
+ 'root' => 'root',
+ );
+ try {
+ $this->_handleAjax( Visualizer_Plugin::ACTION_JSON_GET_DATA );
+ } catch ( WPAjaxDieContinueException $e ) {
+ // Expected after the JSON response.
+ } finally {
+ remove_filter( 'pre_http_request', $filter );
+ }
+
+ $response = json_decode( $this->_last_response );
+ $this->assertTrue( $response->success, $this->_last_response );
+ $this->assertStringContainsString( '<img', $response->data->table );
+ $this->assertStringNotContainsString( '
data->table );
+ }
+
+ /**
+ * The JSON import preview table carries no event attributes from the endpoint.
+ */
+ public function test_json_preview_removes_event_attributes() {
+ $chart_id = $this->create_chart_for_user( $this->admin_user_id );
+ wp_set_current_user( $this->admin_user_id );
+ $filter = $this->mock_json_endpoint( array( array( 'name' => '
' ) ) );
+
+ $_GET['security'] = wp_create_nonce( Visualizer_Plugin::ACTION_JSON_GET_DATA . Visualizer_Plugin::VERSION );
+ $_POST['params'] = array(
+ 'url' => 'https://example.com/data.json',
+ 'method' => 'GET',
+ 'chart' => $chart_id,
+ 'root' => 'root',
+ );
+ try {
+ $this->_handleAjax( Visualizer_Plugin::ACTION_JSON_GET_DATA );
+ } catch ( WPAjaxDieContinueException $e ) {
+ // Expected after the JSON response.
+ } finally {
+ remove_filter( 'pre_http_request', $filter );
+ }
+
+ $response = json_decode( $this->_last_response );
+ $this->assertTrue( $response->success, $this->_last_response );
+ $this->assertStringContainsString( '
data->table );
+ $this->assertStringNotContainsString( 'onerror', $response->data->table );
+ }
+
+ /**
+ * The preview rendered after saving a JSON import carries no event attributes.
+ */
+ public function test_json_save_preview_removes_event_attributes() {
+ $chart_id = $this->create_chart_for_user( $this->admin_user_id );
+ wp_set_current_user( $this->admin_user_id );
+
+ $this->handle_json_set_data(
+ $chart_id,
+ array(
+ 'header' => array( 'name' ),
+ 'type' => array( 'name' => 'string' ),
+ ),
+ array( array( 'name' => '
' ) )
+ );
+
+ $this->assertStringContainsString( 'canvas.data', $this->_last_response );
+ $this->assertStringNotContainsString( 'onerror', $this->_last_response );
+ }
+
+ /**
+ * The preview rendered after a CSV upload carries no event attributes.
+ */
+ public function test_csv_upload_preview_removes_event_attributes() {
+ $chart_id = $this->create_chart_for_user( $this->admin_user_id );
+ wp_set_current_user( $this->admin_user_id );
+ $file = wp_tempnam( 'visualizer.csv' );
+ file_put_contents( $file, "Name,Value\nstring,number\n\"
\",1\n" );
+ $_FILES = array(
+ 'local_data' => array(
+ 'tmp_name' => $file,
+ 'name' => 'data.csv',
+ 'error' => 0,
+ ),
+ );
+ $_GET = array(
+ 'nonce' => wp_create_nonce( 'visualizer-upload-data' ),
+ 'chart' => $chart_id,
+ );
+
+ try {
+ $this->_handleAjax( Visualizer_Plugin::ACTION_UPLOAD_DATA );
+ } catch ( WPAjaxDieContinueException $e ) {
+ // Expected once the update page has rendered.
+ } catch ( WPAjaxDieStopException $e ) {
+ // Expected when the handler stops after rendering.
+ } finally {
+ unlink( $file );
+ }
+
+ $this->assertStringContainsString( 'canvas.data', $this->_last_response );
+ $this->assertStringNotContainsString( 'onerror', $this->_last_response );
+ }
+
+ /**
+ * The database query preview table carries no event attributes from the results.
+ */
+ public function test_query_preview_removes_event_attributes() {
+ $this->_setRole( 'administrator' );
+ $this->enable_pro();
+ $post_id = $this->factory->post->create( array( 'post_title' => '
' ) );
+ $this->assertStringContainsString( 'onerror', get_post_field( 'post_title', $post_id ) );
+
+ global $wpdb;
+ $_GET['security'] = wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION );
+ $_POST['params'] = array(
+ 'query' => 'SELECT post_title FROM ' . $wpdb->prefix . 'posts WHERE ID = ' . $post_id,
+ 'chart_id' => 1,
+ );
+ try {
+ $this->_handleAjax( Visualizer_Plugin::ACTION_FETCH_DB_DATA );
+ } catch ( WPAjaxDieContinueException $e ) {
+ // Expected after the JSON response.
+ }
+
+ $response = json_decode( $this->_last_response );
+ $this->assertTrue( $response->success, $this->_last_response );
+ $this->assertStringContainsString( '
data->table );
+ $this->assertStringNotContainsString( 'onerror', $response->data->table );
+ }
+
/**
* A user cannot replace another user's JSON chart data.
*/
@@ -1151,13 +1520,14 @@ public function test_json_set_data_denied_for_chart_user_cannot_edit() {
/**
* Mock the JSON endpoint so the set-data handler does not hit the network.
*
+ * @param array> $rows The rows the endpoint returns.
* @return callable The filter callback, for removal.
*/
- private function mock_json_endpoint() {
- $filter = function () {
+ private function mock_json_endpoint( array $rows = array( array( 'name' => 'a', 'value' => 1 ) ) ) {
+ $filter = function () use ( $rows ) {
return array(
'headers' => array(),
- 'body' => wp_json_encode( array( array( 'name' => 'a', 'value' => 1 ) ) ),
+ 'body' => wp_json_encode( $rows ),
'response' => array( 'code' => 200, 'message' => '' ),
'cookies' => array(),
'filename' => null,
@@ -1170,10 +1540,11 @@ private function mock_json_endpoint() {
/**
* Run the JSON set-data handler as the current user.
*
- * @param int $chart_id The chart being saved.
- * @param array $post The POST fields to send on top of the defaults.
+ * @param int $chart_id The chart being saved.
+ * @param array $post The POST fields to send on top of the defaults.
+ * @param array> $rows The rows the endpoint returns.
*/
- private function handle_json_set_data( $chart_id, array $post ) {
+ private function handle_json_set_data( $chart_id, array $post, array $rows = array( array( 'name' => 'a', 'value' => 1 ) ) ) {
$_GET = array(
'chart' => $chart_id,
'security' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_SET_DATA . Visualizer_Plugin::VERSION ),
@@ -1190,7 +1561,7 @@ private function handle_json_set_data( $chart_id, array $post ) {
$post
);
- $filter = $this->mock_json_endpoint();
+ $filter = $this->mock_json_endpoint( $rows );
try {
$this->_handleAjax( Visualizer_Plugin::ACTION_JSON_SET_DATA );
} catch ( WPAjaxDieContinueException $e ) {
diff --git a/tests/test-chart-data-sanitization.php b/tests/test-chart-data-sanitization.php
new file mode 100644
index 000000000..9de74f186
--- /dev/null
+++ b/tests/test-chart-data-sanitization.php
@@ -0,0 +1,167 @@
+ 123,
+ 'post_content' => serialize(
+ array(
+ array(
+ 'Revenue',
+ '
',
+ '',
+ 'Research & Development',
+ '3 < 5',
+ '
assertSame( 'Revenue', $result[0][0] );
+ $this->assertStringNotContainsString( 'onerror', $result[0][1] );
+ $this->assertStringNotContainsString( '' ) ) );
+ $filter = array( $this, 'add_script_cell' );
+ add_filter( Visualizer_Plugin::FILTER_GET_CHART_DATA, $filter );
+
+ $result = Visualizer_Gutenberg_Block::get_instance()->get_visualizer_data( array( 'id' => $chart_id ) );
+
+ remove_filter( Visualizer_Plugin::FILTER_GET_CHART_DATA, $filter );
+ $this->assertSame( 'Revenue', $result['visualizer-data'][0][0] );
+ $this->assertStringNotContainsString( '' );
+ return $data;
+ }
+
+ /**
+ * Creates a line chart with the given rows, stored as an administrator would.
+ *
+ * @param array> $rows The chart rows.
+ * @return int The chart ID.
+ */
+ private function create_chart( $rows ) {
+ $author_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
+ // Post content filtering leaves the serialized data intact for an administrator.
+ wp_set_current_user( $author_id );
+ $chart_id = self::factory()->post->create(
+ array(
+ 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
+ 'post_status' => 'publish',
+ 'post_author' => $author_id,
+ 'post_content' => wp_slash( serialize( $rows ) ),
+ )
+ );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, 'line' );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, array() );
+ update_post_meta(
+ $chart_id,
+ Visualizer_Plugin::CF_SERIES,
+ array(
+ array(
+ 'label' => 'Label',
+ 'type' => 'string',
+ ),
+ array(
+ 'label' => 'Value',
+ 'type' => 'string',
+ ),
+ )
+ );
+ return $chart_id;
+ }
+}
diff --git a/tests/test-remote-fetch.php b/tests/test-remote-fetch.php
index 308ffc403..a5ee5a7cd 100644
--- a/tests/test-remote-fetch.php
+++ b/tests/test-remote-fetch.php
@@ -680,6 +680,196 @@ public function test_download_blocks_non_public_destination() {
$this->assertSame( 0, $requests );
}
+ /**
+ * Chart editors without WooCommerce report access must not receive the internal token.
+ */
+ public function test_json_source_does_not_mint_woocommerce_token_for_contributor() {
+ $user_id = self::factory()->user->create( array( 'role' => 'contributor' ) );
+ wp_set_current_user( $user_id );
+
+ $headers = $this->capture_woocommerce_request_headers();
+
+ $this->assertArrayNotHasKey( 'X-Visualizer-Token', $headers );
+ }
+
+ /**
+ * Shop users with report access retain the WooCommerce chart source feature.
+ */
+ public function test_json_source_mints_woocommerce_token_for_authorized_user() {
+ $user_id = self::factory()->user->create( array( 'role' => 'contributor' ) );
+ $user = new WP_User( $user_id );
+ $user->add_cap( 'view_woocommerce_reports' );
+ wp_set_current_user( $user_id );
+
+ $headers = $this->capture_woocommerce_request_headers();
+
+ $this->assertArrayHasKey( 'X-Visualizer-Token', $headers );
+ $this->assertNotSame( '', $headers['X-Visualizer-Token'] );
+ delete_transient( 'visualizer_wc_token_' . $headers['X-Visualizer-Token'] );
+ }
+
+ /**
+ * The report access filter can grant the token to a user without the capability.
+ */
+ public function test_report_access_filter_can_grant_token() {
+ $user_id = self::factory()->user->create( array( 'role' => 'contributor' ) );
+ wp_set_current_user( $user_id );
+ add_filter( 'visualizer_can_fetch_woocommerce_reports', '__return_true' );
+
+ $headers = $this->capture_woocommerce_request_headers();
+
+ remove_filter( 'visualizer_can_fetch_woocommerce_reports', '__return_true' );
+ $this->assertArrayHasKey( 'X-Visualizer-Token', $headers );
+ delete_transient( 'visualizer_wc_token_' . $headers['X-Visualizer-Token'] );
+ }
+
+ /**
+ * The report access filter can withhold the token from a user with the capability.
+ */
+ public function test_report_access_filter_can_withhold_token() {
+ $user_id = self::factory()->user->create( array( 'role' => 'contributor' ) );
+ $user = new WP_User( $user_id );
+ $user->add_cap( 'view_woocommerce_reports' );
+ wp_set_current_user( $user_id );
+ add_filter( 'visualizer_can_fetch_woocommerce_reports', '__return_false' );
+
+ $headers = $this->capture_woocommerce_request_headers();
+
+ remove_filter( 'visualizer_can_fetch_woocommerce_reports', '__return_false' );
+ $this->assertArrayNotHasKey( 'X-Visualizer-Token', $headers );
+ }
+
+ /**
+ * A failed WooCommerce report fetch is reported as a failure.
+ */
+ public function test_failed_woocommerce_fetch_returns_false() {
+ $filter = function () {
+ return $this->response( 401, array(), '{"code":"woocommerce_rest_cannot_view"}' );
+ };
+ add_filter( 'pre_http_request', $filter );
+
+ $source = new Visualizer_Source_Json(
+ array(
+ 'url' => home_url( '/wp-json/wc/v3/reports/sales' ),
+ 'root' => 'root',
+ )
+ );
+ $result = $source->fetch();
+
+ remove_filter( 'pre_http_request', $filter );
+ $this->assertFalse( $result );
+ $this->assertNotEmpty( $source->get_error() );
+ }
+
+ /**
+ * A visitor's live refresh of a WooCommerce report chart keeps the stored data when the fetch fails.
+ */
+ public function test_visitor_refresh_keeps_stored_woocommerce_data() {
+ $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
+ wp_set_current_user( $admin_id );
+ $content = serialize( array( array( '10.00' ) ) );
+ $chart_id = self::factory()->post->create(
+ array(
+ 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
+ 'post_status' => 'publish',
+ 'post_content' => wp_slash( $content ),
+ )
+ );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, 'Visualizer_Source_Json' );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, home_url( '/wp-json/wc/v3/reports/sales' ) );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_ROOT, 'root' );
+ update_post_meta(
+ $chart_id,
+ Visualizer_Plugin::CF_SERIES,
+ array(
+ array(
+ 'label' => 'total_sales',
+ 'type' => 'number',
+ ),
+ )
+ );
+ wp_set_current_user( 0 );
+ $filter = function () {
+ return $this->response( 401, array(), '{"code":"woocommerce_rest_cannot_view"}' );
+ };
+ add_filter( 'pre_http_request', $filter );
+
+ apply_filters( 'visualizer_schedule_refresh_chart', get_post( $chart_id ), $chart_id, false );
+
+ remove_filter( 'pre_http_request', $filter );
+ $this->assertSame( $content, get_post_field( 'post_content', $chart_id, 'raw' ) );
+ $this->assertNotEmpty( get_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR, true ) );
+ }
+
+ /**
+ * An editable-table chart keeps its stored data when the second WooCommerce read fails.
+ */
+ public function test_refresh_keeps_stored_data_when_second_woocommerce_read_fails() {
+ $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
+ wp_set_current_user( $admin_id );
+ $content = serialize( array( array( '10.00' ) ) );
+ $chart_id = self::factory()->post->create(
+ array(
+ 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
+ 'post_status' => 'publish',
+ 'post_content' => wp_slash( $content ),
+ )
+ );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, 'Visualizer_Source_Json' );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, home_url( '/wp-json/wc/v3/reports/sales' ) );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_ROOT, 'root' );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true );
+ update_post_meta(
+ $chart_id,
+ Visualizer_Plugin::CF_SERIES,
+ array(
+ array(
+ 'label' => 'total_sales',
+ 'type' => 'number',
+ ),
+ )
+ );
+ $requests = 0;
+ $filter = function () use ( &$requests ) {
+ ++$requests;
+ return 1 === $requests
+ ? $this->response( 200, array(), '[{"total_sales":"20.00"}]' )
+ : $this->response( 401, array(), '{"code":"woocommerce_rest_cannot_view"}' );
+ };
+ add_filter( 'pre_http_request', $filter );
+
+ apply_filters( 'visualizer_schedule_refresh_chart', get_post( $chart_id ), $chart_id, false );
+
+ remove_filter( 'pre_http_request', $filter );
+ $this->assertSame( 2, $requests );
+ $this->assertSame( $content, get_post_field( 'post_content', $chart_id, 'raw' ) );
+ }
+
+ /**
+ * Captures the headers generated for a same-site WooCommerce reports request.
+ *
+ * @return array
+ */
+ private function capture_woocommerce_request_headers() {
+ $headers = array();
+ $filter = function ( $preempt, $args ) use ( &$headers ) {
+ $headers = $args['headers'];
+ return $this->response( 200, array(), '[{"total_sales":"10.00"}]' );
+ };
+ add_filter( 'pre_http_request', $filter, 10, 2 );
+
+ $source = new Visualizer_Source_Json(
+ array(
+ 'url' => home_url( '/wp-json/wc/v3/reports/sales' ),
+ 'root' => 'root',
+ )
+ );
+ $source->fetchRoots();
+
+ remove_filter( 'pre_http_request', $filter, 10 );
+ return $headers;
+ }
+
/**
* Builds a WordPress HTTP response fixture.
*
diff --git a/tests/test-support-hub.php b/tests/test-support-hub.php
new file mode 100644
index 000000000..312ca80e8
--- /dev/null
+++ b/tests/test-support-hub.php
@@ -0,0 +1,84 @@
+assertNotFalse( has_filter( 'visualizer_support_hub_metadata', 'visualizer_support_hub_metadata' ) );
+
+ $data = apply_filters( 'visualizer_support_hub_metadata', array() );
+
+ $this->assertIsArray( $data );
+ $this->assertArrayHasKey( 'is_pro', $data );
+ $this->assertIsBool( $data['is_pro'] );
+ $this->assertArrayNotHasKey( 'setup_complete', $data, 'The setup nudge stays off for now.' );
+ $this->assertArrayNotHasKey( 'pages', $data, 'Screens are declared through themeisle_internal_page, not pages.' );
+ $this->assertArrayNotHasKey( 'primary_color', $data, 'The hub keeps the shared SDK accent.' );
+ }
+
+ /**
+ * Every footer link has an icon, a text and a URL.
+ */
+ public function test_links_are_complete() {
+ $data = apply_filters( 'visualizer_support_hub_metadata', array() );
+
+ $this->assertNotEmpty( $data['links'] );
+ $this->assertLessThanOrEqual( 6, count( $data['links'] ) );
+ foreach ( $data['links'] as $link ) {
+ $this->assertNotEmpty( $link['icon'] );
+ $this->assertNotEmpty( $link['text'] );
+ $this->assertNotFalse( filter_var( $link['url'], FILTER_VALIDATE_URL ) );
+ }
+ $this->assertSame( VISUALIZER_MAIN_DOC, $data['links'][0]['url'] );
+ }
+
+ /**
+ * `is_pro` follows the `visualizer_is_pro` filter Pro answers from its license.
+ */
+ public function test_is_pro_follows_the_license_filter() {
+ $this->assertFalse( apply_filters( 'visualizer_support_hub_metadata', array() )['is_pro'] );
+
+ add_filter( 'visualizer_is_pro', '__return_true' );
+ $this->assertTrue( apply_filters( 'visualizer_support_hub_metadata', array() )['is_pro'] );
+ remove_filter( 'visualizer_is_pro', '__return_true' );
+ }
+
+ /**
+ * The Settings screen declares itself a product screen like the Library and Support screens.
+ */
+ public function test_settings_screen_fires_internal_page() {
+ wp_set_current_user( 1 );
+ set_current_screen( 'dashboard' );
+
+ $admin = Visualizer_Plugin::instance()->getModule( Visualizer_Module_Admin::NAME );
+ $admin->registerAdminMenu();
+
+ $fired = array();
+ add_action(
+ 'themeisle_internal_page',
+ function ( $product, $page ) use ( &$fired ) {
+ $fired[] = array( $product, $page );
+ },
+ 10,
+ 2
+ );
+
+ // The suffix add_submenu_page() returned, whatever menu state earlier tests left behind.
+ $admin->enqueueSettingsScripts( get_plugin_page_hookname( 'viz-settings', Visualizer_Plugin::NAME ) );
+ $admin->enqueueSettingsScripts( 'edit-post' );
+
+ $this->assertSame( array( array( VISUALIZER_DIRNAME, 'settings' ) ), $fired );
+ }
+}