-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathglobal-setup.js
More file actions
93 lines (80 loc) · 2.9 KB
/
Copy pathglobal-setup.js
File metadata and controls
93 lines (80 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* eslint-disable no-console */
/* global globalThis */
const { runCLI } = require( '@wp-playground/cli' );
const { chromium } = require( '@playwright/test' );
const fs = require( 'node:fs' );
const path = require( 'node:path' );
const crypto = require( 'node:crypto' );
/**
* Deterministic port from cwd hash so each git worktree gets its own
* Playground instance. Override with WP_PLAYGROUND_PORT (e.g., in CI).
* Range: 9400–9499.
*
* @return {number} Port to run Playground on.
*/
function resolvePort() {
if ( process.env.WP_PLAYGROUND_PORT ) {
return Number( process.env.WP_PLAYGROUND_PORT );
}
const hash = crypto.createHash( 'sha1' ).update( process.cwd() ).digest();
return 9400 + ( hash.readUInt16BE( 0 ) % 100 );
}
module.exports = async () => {
// WP_BASE_URL means someone else is running Playground (e.g. via
// `npm run playground:start`). Use theirs rather than booting a second.
if ( process.env.WP_BASE_URL ) {
return;
}
const blueprintPath = process.env.WP_BLUEPRINT_PATH
? path.resolve( process.env.WP_BLUEPRINT_PATH )
: path.resolve( process.cwd(), 'blueprint.json' );
const blueprint = JSON.parse( fs.readFileSync( blueprintPath, 'utf8' ) );
const port = resolvePort();
console.log( `Starting WordPress Playground on port ${ port }...` );
const cli = await runCLI( {
command: 'server',
port,
php: process.env.WP_PLAYGROUND_PHP || undefined,
wp: process.env.WP_PLAYGROUND_WP || undefined,
mount: [
{
hostPath: process.cwd(),
vfsPath: '/wordpress/wp-content/plugins/query-filter',
},
],
blueprint,
} );
// runCLI returns { playground, server } — derive the URL from the socket
// rather than the requested port, which may have been taken.
const addr = cli.server.address();
const serverUrl = `http://127.0.0.1:${ addr.port }`;
process.env.WP_BASE_URL = serverUrl;
globalThis.__wpPlayground = cli;
console.log( `Playground running at ${ serverUrl }` );
// Log in once and save the authentication state for all tests. The filter
// blocks are front end, but the storage state keeps admin-side checks and
// any future editor tests cheap.
const browser = await chromium.launch();
const page = await browser.newPage( {
baseURL: serverUrl,
extraHTTPHeaders: { 'Accept-Encoding': 'identity' },
} );
await page.goto( `${ serverUrl }/wp-login.php` );
await page.fill( '#user_login', 'admin' );
await page.fill( '#user_pass', 'password' );
await page.click( '#wp-submit' );
try {
await Promise.race( [
page.waitForURL( '**/wp-admin/', { timeout: 30000 } ),
page.waitForSelector( '#wpadminbar', { timeout: 30000 } ),
] );
} catch ( error ) {
console.error( 'Failed to log in to WordPress Playground' );
await browser.close();
throw error;
}
fs.mkdirSync( '.auth', { recursive: true } );
await page.context().storageState( { path: '.auth/wordpress.json' } );
await browser.close();
console.log( 'WordPress authentication state saved.' );
};