2023-05-05 11:03:36 +00:00
|
|
|
/* eslint-disable no-console */
|
2024-02-23 10:17:46 +00:00
|
|
|
|
2023-04-17 12:01:41 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2024-02-23 10:17:46 +00:00
|
|
|
import { chromium, request } from '@playwright/test';
|
2023-05-05 11:03:36 +00:00
|
|
|
import { RequestUtils } from '@wordpress/e2e-test-utils-playwright';
|
2024-05-13 12:58:26 +00:00
|
|
|
import {
|
|
|
|
BASE_URL,
|
|
|
|
adminFile,
|
2024-05-28 15:09:28 +00:00
|
|
|
wpCLI,
|
2024-05-13 12:58:26 +00:00
|
|
|
customerFile,
|
|
|
|
BLOCK_THEME_SLUG,
|
|
|
|
DB_EXPORT_FILE,
|
|
|
|
} from '@woocommerce/e2e-utils';
|
2023-04-17 12:01:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2023-06-01 14:14:50 +00:00
|
|
|
import { customer, admin } from './test-data/data/data';
|
2023-04-17 12:01:41 +00:00
|
|
|
|
2024-02-23 10:17:46 +00:00
|
|
|
const prepareAttributes = async () => {
|
2023-12-08 16:44:59 +00:00
|
|
|
const browser = await chromium.launch();
|
2024-02-23 10:17:46 +00:00
|
|
|
const context = await browser.newContext( {
|
|
|
|
baseURL: BASE_URL,
|
|
|
|
storageState: adminFile,
|
|
|
|
} );
|
2023-06-01 14:14:50 +00:00
|
|
|
|
|
|
|
const page = await context.newPage();
|
|
|
|
|
2024-02-23 10:17:46 +00:00
|
|
|
// Intercept the dialog event. This is needed because when the regenerate
|
|
|
|
// button is clicked, a dialog is shown.
|
2024-05-14 08:23:17 +00:00
|
|
|
page.on( 'dialog', ( dialog ) => {
|
|
|
|
void dialog.accept();
|
2023-06-01 14:14:50 +00:00
|
|
|
} );
|
|
|
|
|
2024-02-23 10:17:46 +00:00
|
|
|
await page.goto( '/wp-admin/admin.php?page=wc-status&tab=tools' );
|
2023-06-01 14:14:50 +00:00
|
|
|
|
2024-02-23 10:17:46 +00:00
|
|
|
// Attributes regeneration should be doable via a CLI command, e.g.:
|
|
|
|
// "wp wc tool run regenerate_product_attributes_lookup_table --user=1"
|
|
|
|
// It doesn't seem to be working correctly ATM so we need to do it via
|
|
|
|
// browser actions.
|
|
|
|
// See: https://github.com/woocommerce/woocommerce/issues/32831
|
|
|
|
await page
|
|
|
|
.getByRole( 'row', {
|
|
|
|
name: /Regenerate the product attributes lookup table/,
|
|
|
|
} )
|
|
|
|
.getByRole( 'button' )
|
|
|
|
.click();
|
2023-06-01 14:14:50 +00:00
|
|
|
|
|
|
|
await context.close();
|
|
|
|
await browser.close();
|
|
|
|
|
2024-02-23 10:17:46 +00:00
|
|
|
// Note that the two commands below are intentionally duplicated as we need
|
|
|
|
// to run the cron task twice as we need to process more than 1 batch of
|
|
|
|
// items.
|
2024-05-28 15:09:28 +00:00
|
|
|
const cronTask =
|
|
|
|
'action-scheduler run --hooks="woocommerce_run_product_attribute_lookup_regeneration_callback"';
|
|
|
|
await wpCLI( cronTask );
|
|
|
|
await wpCLI( cronTask );
|
2023-06-01 14:14:50 +00:00
|
|
|
};
|
|
|
|
|
2024-02-23 10:17:46 +00:00
|
|
|
async function globalSetup() {
|
2024-05-13 12:58:26 +00:00
|
|
|
console.log( 'Running global setup:' );
|
|
|
|
console.time( '└ Total time' );
|
2024-02-23 10:17:46 +00:00
|
|
|
|
2024-05-13 12:58:26 +00:00
|
|
|
let databaseImported = false;
|
|
|
|
|
|
|
|
try {
|
2024-05-28 15:09:28 +00:00
|
|
|
await wpCLI( `db import ${ DB_EXPORT_FILE }` );
|
|
|
|
console.log( '├ Database snapshot imported, running basic setup…' );
|
2024-05-13 12:58:26 +00:00
|
|
|
databaseImported = true;
|
2024-05-28 15:09:28 +00:00
|
|
|
} catch ( error ) {
|
|
|
|
if (
|
|
|
|
error instanceof Error &&
|
|
|
|
! error.message.includes( 'Import file missing' )
|
|
|
|
) {
|
|
|
|
// Throw if the error is not related to the import file missing.
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log( '├ Database snapshot not found, running full setup…' );
|
2024-05-13 12:58:26 +00:00
|
|
|
}
|
2023-05-05 11:03:36 +00:00
|
|
|
|
|
|
|
const requestContext = await request.newContext( {
|
2024-02-23 10:17:46 +00:00
|
|
|
baseURL: BASE_URL,
|
2023-05-05 11:03:36 +00:00
|
|
|
} );
|
|
|
|
|
2024-05-13 12:58:26 +00:00
|
|
|
console.log( '├ Pre-authenticating users…' );
|
2024-02-23 10:17:46 +00:00
|
|
|
await new RequestUtils( requestContext, {
|
|
|
|
user: customer,
|
|
|
|
storageStatePath: customerFile,
|
|
|
|
} ).setupRest();
|
2024-05-13 12:58:26 +00:00
|
|
|
const requestUtils = new RequestUtils( requestContext, {
|
|
|
|
user: admin,
|
|
|
|
storageStatePath: adminFile,
|
|
|
|
} );
|
|
|
|
await requestUtils.setupRest();
|
|
|
|
|
|
|
|
if ( ! databaseImported ) {
|
|
|
|
console.log( '├ Activating default theme…' );
|
|
|
|
await requestUtils.activateTheme( BLOCK_THEME_SLUG );
|
|
|
|
|
|
|
|
console.log( '├ Preparing product attributes…' );
|
|
|
|
await prepareAttributes();
|
|
|
|
}
|
2024-02-23 10:17:46 +00:00
|
|
|
|
2024-05-28 15:09:28 +00:00
|
|
|
console.log( '├ Exporting database snapshot…' );
|
|
|
|
await wpCLI( `db export ${ DB_EXPORT_FILE }` );
|
2023-05-05 11:03:36 +00:00
|
|
|
|
|
|
|
await requestContext.dispose();
|
2024-05-13 12:58:26 +00:00
|
|
|
console.timeEnd( '└ Total time' );
|
2023-05-05 11:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default globalSetup;
|