Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions classes/Visualizer/Module/Abilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,10 @@ private function buildManualSource( $input ) {
return $errors;
}

// REST and MCP requests do not load the admin file helpers.
if ( ! function_exists( 'wp_tempnam' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
$tmpfile = wp_tempnam( Visualizer_Plugin::NAME );
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen
$handle = $tmpfile ? fopen( $tmpfile, 'w' ) : false;
Expand Down
11 changes: 11 additions & 0 deletions tests/e2e/config/mu-plugins/skip-sdk-promotions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* Stop the SDK promotions from loading for requests carrying the test header.
*
* The promotions preload wp-admin/includes/file.php, which hides missing
* includes on sites that show no promotions.
*/

if ( defined( 'TI_E2E_TESTING' ) && isset( $_SERVER['HTTP_X_VISUALIZER_E2E_SKIP_PROMOTIONS'] ) ) {
add_filter( 'themeisle_sdk_ran_promos', '__return_true' );
}
72 changes: 72 additions & 0 deletions tests/e2e/specs/abilities.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* WordPress dependencies
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );

/**
* Internal dependencies
*/
const { deleteAllCharts } = require( '../utils/common' );

const UPSERT_PATH = '/wp-abilities/v1/abilities/visualizer/upsert-chart/run';

// Keeps the admin file helpers unloaded, as on sites without SDK promotions.
const HEADERS = { 'X-Visualizer-E2E-Skip-Promotions': '1' };

const CHART_INPUT = {
type: 'line',
columns: [
{ name: 'Month', type: 'string' },
{ name: 'Sales', type: 'number' },
],
rows: [
[ 'Jan', 10 ],
[ 'Feb', 20 ],
],
};

test.describe( 'Chart abilities over REST', () => {
test.beforeEach( async ( { requestUtils } ) => {
await deleteAllCharts( requestUtils );
} );

test.afterAll( async ( { requestUtils } ) => {
await deleteAllCharts( requestUtils );
} );

test( 'upsert-chart dry run returns the parsed preview from manual data', async ( {
requestUtils,
} ) => {
const result = await requestUtils.rest( {
method: 'POST',
path: UPSERT_PATH,
headers: HEADERS,
data: { input: { ...CHART_INPUT, dry_run: true } },
} );

expect( result.dry_run ).toBe( true );
expect( result.valid ).toBe( true );
expect( result.parsed_preview ).toBeTruthy();
expect( JSON.stringify( result.parsed_preview ) ).toContain( 'Feb' );
} );

test( 'upsert-chart creates a chart from manual data', async ( {
requestUtils,
} ) => {
const result = await requestUtils.rest( {
method: 'POST',
path: UPSERT_PATH,
headers: HEADERS,
data: { input: CHART_INPUT },
} );

expect( result.created ).toBe( true );
expect( result.id ).toBeGreaterThan( 0 );

const chart = await requestUtils.rest( {
path: `/wp/v2/visualizer/${ result.id }`,
params: { context: 'edit' },
} );
expect( chart.status ).toBe( 'publish' );
} );
} );
Loading