|
| 1 | +/** |
| 2 | + * External dependencies |
| 3 | + */ |
| 4 | +import { mkdir, rm, writeFile } from 'node:fs/promises'; |
| 5 | +import { join } from 'node:path'; |
| 6 | + |
| 7 | +/** |
| 8 | + * WordPress dependencies |
| 9 | + */ |
| 10 | +import { expect } from '@wordpress/e2e-test-utils-playwright'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Internal dependencies |
| 14 | + */ |
| 15 | +import { wpCli } from '../utils/wp-cli'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Writes options, then reads options, through WP-CLI. |
| 19 | + * |
| 20 | + * @param {string} baseURL The URL of the site. |
| 21 | + * @param {string[]} names The names of the options to read. |
| 22 | + * @param {Object} values The option values to write. A value of `false` deletes the option. |
| 23 | + * @return {Object} The stored values of the options in `names`. |
| 24 | + */ |
| 25 | +function syncOptions( baseURL, names, values = {} ) { |
| 26 | + const input = Buffer.from( JSON.stringify( { names, values } ) ).toString( |
| 27 | + 'base64' |
| 28 | + ); |
| 29 | + |
| 30 | + return JSON.parse( |
| 31 | + wpCli( [ |
| 32 | + `--url=${ baseURL }`, |
| 33 | + 'eval', |
| 34 | + ` |
| 35 | + // Read and restore the stored offset, not the value calculated for a named timezone. |
| 36 | + remove_filter( 'pre_option_gmt_offset', 'wp_timezone_override_offset' ); |
| 37 | + $input = json_decode( base64_decode( '${ input }' ), true ); |
| 38 | + foreach ( $input['values'] as $option => $value ) { |
| 39 | + // get_option() returns false for an option that does not exist. |
| 40 | + false === $value ? delete_option( $option ) : update_option( $option, $value ); |
| 41 | + } |
| 42 | + $stored = array(); |
| 43 | + foreach ( $input['names'] as $option ) { |
| 44 | + $stored[ $option ] = get_option( $option ); |
| 45 | + } |
| 46 | + echo wp_json_encode( (object) $stored ); |
| 47 | + `, |
| 48 | + ] ) |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Waits for all restores to finish, so that one failure does not stop the others. |
| 54 | + * |
| 55 | + * @param {Promise[]} restores The restores in progress. |
| 56 | + * @throws {AggregateError} If a restore failed. |
| 57 | + */ |
| 58 | +async function settleRestores( restores ) { |
| 59 | + const errors = ( await Promise.allSettled( restores ) ) |
| 60 | + .filter( ( { status } ) => status === 'rejected' ) |
| 61 | + .map( ( { reason } ) => reason ); |
| 62 | + |
| 63 | + if ( errors.length > 0 ) { |
| 64 | + throw new AggregateError( |
| 65 | + errors, |
| 66 | + errors.map( ( { message } ) => message ).join( '\n' ) |
| 67 | + ); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Reads and changes site settings through REST. Teardown restores the keys passed to `set`. |
| 73 | + */ |
| 74 | +export const siteSettings = async ( { requestUtils }, use ) => { |
| 75 | + const originals = {}; |
| 76 | + const get = () => requestUtils.rest( { path: '/wp/v2/settings' } ); |
| 77 | + const post = ( data ) => |
| 78 | + requestUtils.rest( { method: 'POST', path: '/wp/v2/settings', data } ); |
| 79 | + |
| 80 | + await use( { |
| 81 | + get, |
| 82 | + set: async ( patch ) => { |
| 83 | + const current = await get(); |
| 84 | + for ( const key of Object.keys( patch ) ) { |
| 85 | + if ( ! Object.hasOwn( originals, key ) ) { |
| 86 | + originals[ key ] = current[ key ]; |
| 87 | + } |
| 88 | + } |
| 89 | + await post( patch ); |
| 90 | + }, |
| 91 | + } ); |
| 92 | + |
| 93 | + await settleRestores( |
| 94 | + Object.entries( originals ).map( async ( [ key, value ] ) => { |
| 95 | + const original = { [ key ]: value }; |
| 96 | + expect( await post( original ) ).toMatchObject( original ); |
| 97 | + } ) |
| 98 | + ); |
| 99 | +}; |
| 100 | + |
| 101 | +/** |
| 102 | + * Reads and changes site options through WP-CLI. Teardown restores the keys passed to `set`. |
| 103 | + */ |
| 104 | +export const siteOptions = async ( { baseURL }, use ) => { |
| 105 | + let originals = {}; |
| 106 | + |
| 107 | + await use( { |
| 108 | + get: async ( names ) => syncOptions( baseURL, names ), |
| 109 | + set: async ( values ) => { |
| 110 | + originals = { |
| 111 | + ...syncOptions( baseURL, Object.keys( values ) ), |
| 112 | + ...originals, |
| 113 | + }; |
| 114 | + syncOptions( baseURL, [], values ); |
| 115 | + }, |
| 116 | + } ); |
| 117 | + |
| 118 | + const names = Object.keys( originals ); |
| 119 | + if ( names.length > 0 ) { |
| 120 | + expect( syncOptions( baseURL, names, originals ) ).toEqual( originals ); |
| 121 | + } |
| 122 | +}; |
| 123 | + |
| 124 | +/** |
| 125 | + * Writes must-use plugins. Teardown deletes each file that `write` wrote. |
| 126 | + */ |
| 127 | +export const muPlugin = async ( {}, use ) => { |
| 128 | + const directory = join( |
| 129 | + process.cwd(), |
| 130 | + process.env.LOCAL_DIR ?? 'src', |
| 131 | + 'wp-content/mu-plugins' |
| 132 | + ); |
| 133 | + const files = new Set(); |
| 134 | + |
| 135 | + await use( { |
| 136 | + write: async ( fileName, php ) => { |
| 137 | + const file = join( directory, fileName ); |
| 138 | + files.add( file ); |
| 139 | + await mkdir( directory, { recursive: true } ); |
| 140 | + await writeFile( file, php ); |
| 141 | + return file; |
| 142 | + }, |
| 143 | + } ); |
| 144 | + |
| 145 | + await settleRestores( |
| 146 | + [ ...files ].map( ( file ) => rm( file, { force: true } ) ) |
| 147 | + ); |
| 148 | +}; |
0 commit comments