Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions .wp-env.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"."
],
"themes": [],
"mappings": {
"wp-content/mu-plugins/visualizer-e2e-enable-database-source.php": "./tests/e2e/config/enable-database-source.php"
},
"config": {
"WP_DEBUG": true,
"WP_DEBUG_LOG": true,
Expand Down
14 changes: 14 additions & 0 deletions js/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,20 @@

init_db_import_component();

$( '#settings-button' ).on( 'click', function( event ){
$('body').trigger('visualizer:db:query:update', {});
if( $( '#db-chart-button' ).attr( 'data-current' ) !== 'filter' || $( '.visualizer-db-query' ).val().length === 0 ){
return;
}

event.preventDefault();
event.stopImmediatePropagation();
$( '#thehole' ).one( 'load', function(){
$( '#settings-button' ).trigger( 'click' );
} );
$( '#db-chart-button' ).trigger( 'click' );
} );

$('#visualizer-query-fetch').on('click', function(e){

$('body').trigger('visualizer:db:query:update', {});
Expand Down
34 changes: 34 additions & 0 deletions tests/e2e/config/enable-database-source.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Enable database-source E2E tests for requests carrying the test cookie.
*/

defined( 'VISUALIZER_PRO_VERSION' ) || define( 'VISUALIZER_PRO_VERSION', '2.0.1' );

if ( ! class_exists( 'Visualizer_Pro' ) ) {
class Visualizer_Pro {
const ACTION_FETCH_DATA = 'visualizer-fetch-data';
const CF_PERMISSIONS = 'visualizer-permissions';
}
}
Comment thread
Copilot marked this conversation as resolved.
Outdated

function visualizer_e2e_database_source_enabled() {
return defined( 'TI_E2E_TESTING' ) && isset( $_COOKIE['visualizer_e2e_database_source'] );
}

add_filter(
'visualizer_is_pro',
function ( $enabled ) {
return visualizer_e2e_database_source_enabled() ? true : $enabled;
},
PHP_INT_MAX
);

add_filter(
'visualizer_pro_upsell_class',
function ( $class, $feature = '' ) {
return visualizer_e2e_database_source_enabled() && 'db-query' === $feature ? '' : $class;
},
PHP_INT_MAX,
2
);
182 changes: 182 additions & 0 deletions tests/e2e/specs/database-source.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/**
* WordPress dependencies
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );

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

async function showQueryResults( page, chartEditor, expectedValue ) {
const [ response ] = await Promise.all( [
page.waitForResponse( ( candidate ) => {
const request = candidate.request();

return (
candidate.url().includes( '/wp-admin/admin-ajax.php' ) &&
request.method() === 'POST' &&
request
.postData()
?.includes( 'action=visualizer-fetch-db-data' )
);
} ),
chartEditor.getByRole( 'button', { name: 'Show Results' } ).click(),
] );

expect( response.ok() ).toBe( true );
await expect( chartEditor.locator( '.db-wizard-results' ) ).toContainText(
expectedValue
);
}

test.describe( 'Database source', () => {
const postIds = [];

test.beforeEach( async ( { page, requestUtils } ) => {
await deleteAllCharts( requestUtils );
page.setDefaultTimeout( 20000 );
} );

test.afterEach( async ( { requestUtils } ) => {
await Promise.all(
postIds.splice( 0 ).map( ( postId ) =>
requestUtils
.rest( {
method: 'DELETE',
path: `/wp/v2/posts/${ postId }`,
params: { force: true },
} )
.catch( () => {} )
)
);
} );

test( 'saves previewed one-time data when saving the chart', async ( {
admin,
context,
page,
requestUtils,
}, testInfo ) => {
test.setTimeout( 180000 );

await context.addCookies( [
{
name: 'visualizer_e2e_database_source',
value: '1',
url: testInfo.project.use.baseURL || 'http://localhost:8889',
},
] );

const sourcePost = await requestUtils.createPost( {
title: 'ISSUE_1329_OLD',
status: 'publish',
} );
postIds.push( sourcePost.id );

await admin.visitAdminPage(
'admin.php?page=visualizer&vaction=addnew'
);
await page
.getByRole( 'button', { name: 'Classic Builder Step-by-step' } )
.click();

const chartEditor = page.frameLocator( 'iframe' );
await chartEditor
.locator( 'div#type-picker' )
.waitFor( { state: 'visible', timeout: 20000 } );
await selectChartAdmin( chartEditor, CHART_JS_LABELS.table );

await chartEditor
.locator( '#visualizer-chart-id' )
.waitFor( { state: 'attached', timeout: 20000 } );
const chartId = await chartEditor
.locator( '#visualizer-chart-id' )
.getAttribute( 'data-id' );
expect( chartId ).toMatch( /^\d+$/ );

await chartEditor
.getByRole( 'button', { name: /Import from database/ } )
.click();
await chartEditor
.locator( '#db-chart-button' )
.evaluate( ( button ) => button.click() );

const query = `SELECT post_title AS value FROM wp_posts WHERE ID = ${ sourcePost.id }`;
await chartEditor
.locator( '.CodeMirror' )
.evaluate(

Check failure on line 112 in tests/e2e/specs/database-source.spec.js

View workflow job for this annotation

GitHub Actions / Playwright E2E tests

[chromium] › tests/e2e/specs/database-source.spec.js:59:2 › Database source › saves previewed one-time data when saving the chart

3) [chromium] › tests/e2e/specs/database-source.spec.js:59:2 › Database source › saves previewed one-time data when saving the chart Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: locator.evaluate: Error: strict mode violation: locator('.CodeMirror') resolved to 2 elements: 1) <div translate="no" class="CodeMirror cm-s-default CodeMirror-wrap">…</div> aka locator('div').filter({ hasText: '/* List Posts published per.' }).nth(4) 2) <div translate="no" class="CodeMirror cm-s-default CodeMirror-wrap">…</div> aka locator('div').filter({ hasText: 'xxxxxxxxxx' }).nth(5) Call log: - waiting for locator('iframe').contentFrame().locator('.CodeMirror') 110 | await chartEditor 111 | .locator( '.CodeMirror' ) > 112 | .evaluate( | ^ 113 | ( editor, sql ) => editor.CodeMirror.setValue( sql ), 114 | query 115 | ); at /home/runner/work/visualizer/visualizer/tests/e2e/specs/database-source.spec.js:112:5

Check failure on line 112 in tests/e2e/specs/database-source.spec.js

View workflow job for this annotation

GitHub Actions / Playwright E2E tests

[chromium] › tests/e2e/specs/database-source.spec.js:59:2 › Database source › saves previewed one-time data when saving the chart

3) [chromium] › tests/e2e/specs/database-source.spec.js:59:2 › Database source › saves previewed one-time data when saving the chart Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: locator.evaluate: Error: strict mode violation: locator('.CodeMirror') resolved to 2 elements: 1) <div translate="no" class="CodeMirror cm-s-default CodeMirror-wrap">…</div> aka locator('div').filter({ hasText: '/* List Posts published per.' }).nth(4) 2) <div translate="no" class="CodeMirror cm-s-default CodeMirror-wrap">…</div> aka locator('div').filter({ hasText: 'xxxxxxxxxx' }).nth(5) Call log: - waiting for locator('iframe').contentFrame().locator('.CodeMirror') 110 | await chartEditor 111 | .locator( '.CodeMirror' ) > 112 | .evaluate( | ^ 113 | ( editor, sql ) => editor.CodeMirror.setValue( sql ), 114 | query 115 | ); at /home/runner/work/visualizer/visualizer/tests/e2e/specs/database-source.spec.js:112:5

Check failure on line 112 in tests/e2e/specs/database-source.spec.js

View workflow job for this annotation

GitHub Actions / Playwright E2E tests

[chromium] › tests/e2e/specs/database-source.spec.js:59:2 › Database source › saves previewed one-time data when saving the chart

3) [chromium] › tests/e2e/specs/database-source.spec.js:59:2 › Database source › saves previewed one-time data when saving the chart Error: locator.evaluate: Error: strict mode violation: locator('.CodeMirror') resolved to 2 elements: 1) <div translate="no" class="CodeMirror cm-s-default CodeMirror-wrap">…</div> aka locator('div').filter({ hasText: '/* List Posts published per.' }).nth(4) 2) <div translate="no" class="CodeMirror cm-s-default CodeMirror-wrap">…</div> aka locator('div').filter({ hasText: 'xxxxxxxxxx' }).nth(5) Call log: - waiting for locator('iframe').contentFrame().locator('.CodeMirror') 110 | await chartEditor 111 | .locator( '.CodeMirror' ) > 112 | .evaluate( | ^ 113 | ( editor, sql ) => editor.CodeMirror.setValue( sql ), 114 | query 115 | ); at /home/runner/work/visualizer/visualizer/tests/e2e/specs/database-source.spec.js:112:5
( editor, sql ) => editor.CodeMirror.setValue( sql ),
query
);

await showQueryResults( page, chartEditor, 'ISSUE_1329_OLD' );

const [ saveResponse ] = await Promise.all( [
page.waitForResponse(
( candidate ) =>
candidate
.url()
.includes( 'action=visualizer-save-db-query' ) &&
candidate.request().method() === 'POST'
),
chartEditor
.locator( '#db-chart-button' )
.evaluate( ( button ) => button.click() ),
] );
expect( saveResponse.ok() ).toBe( true );
await expect( chartEditor.locator( '#canvas' ) ).toContainText(
'ISSUE_1329_OLD',
{ timeout: 20000 }
);
await chartEditor
.getByRole( 'button', { name: 'Create Chart' } )
.click();
await expect(
page.getByRole( 'heading', { name: 'Visualizer Library' } )
).toBeVisible( { timeout: 20000 } );

const chartPost = await requestUtils.createPost( {
title: 'Issue 1329 chart',
content: `[visualizer id="${ chartId }"]`,
status: 'publish',
} );
postIds.push( chartPost.id );

await page.goto( chartPost.link );
await page.mouse.wheel( 0, 500 );
await expect(
page.getByRole( 'cell', { name: 'ISSUE_1329_OLD' } )
).toBeVisible();

await requestUtils.rest( {
method: 'POST',
path: `/wp/v2/posts/${ sourcePost.id }`,
data: { title: 'ISSUE_1329_NEW' },
} );

await admin.visitAdminPage( 'admin.php?page=visualizer' );
await page
.locator( `.visualizer-chart-edit[data-chart="${ chartId }"]` )
.click();
await chartEditor
.locator( '#db-chart-button' )
.evaluate( ( button ) => button.click() );
await showQueryResults( page, chartEditor, 'ISSUE_1329_NEW' );

await chartEditor.getByRole( 'button', { name: 'Save Chart' } ).click();
await expect(
page.getByRole( 'dialog', { name: 'Visualizer' } )
).toHaveCount( 0, { timeout: 30000 } );

await page.goto( chartPost.link );
await page.mouse.wheel( 0, 500 );
await expect(
page.getByRole( 'cell', { name: 'ISSUE_1329_NEW' } )
).toBeVisible();
} );
} );
Loading