diff --git a/src/js/_enqueues/admin/media-upload-pipeline.js b/src/js/_enqueues/admin/media-upload-pipeline.js index bd25774d48491..4b0fd4f47bae0 100644 --- a/src/js/_enqueues/admin/media-upload-pipeline.js +++ b/src/js/_enqueues/admin/media-upload-pipeline.js @@ -46,7 +46,7 @@ window.wp = window.wp || {}; const IMAGE_SUBTYPES = [ 'jpeg', 'png', 'gif', 'webp', 'avif', 'heic', 'heif' ]; /** - * Recursively appends data to a FormData object, supporting nested objects. + * Recursively appends data to a FormData object, supporting nested objects and arrays. * * Mirrors flattenFormData() in the media-utils package. * @@ -56,9 +56,10 @@ window.wp = window.wp || {}; */ function flattenFormData( formData, key, data ) { if ( - data !== null && - typeof data === 'object' && - Object.getPrototypeOf( data ) === Object.prototype + Array.isArray( data ) || + ( data !== null && + typeof data === 'object' && + Object.getPrototypeOf( data ) === Object.prototype ) ) { Object.keys( data ).forEach( function ( name ) { flattenFormData( formData, key + '[' + name + ']', data[ name ] ); diff --git a/tests/e2e/specs/media-new-client-side-upload.test.js b/tests/e2e/specs/media-new-client-side-upload.test.js index 65e731654a1c9..39b943fb554a0 100644 --- a/tests/e2e/specs/media-new-client-side-upload.test.js +++ b/tests/e2e/specs/media-new-client-side-upload.test.js @@ -1,3 +1,8 @@ +/** + * External dependencies + */ +import { randomUUID } from 'node:crypto'; + /** * Internal dependencies */ @@ -166,6 +171,70 @@ test.describe( 'Add New Media File client-side uploads', () => { expect( thumbnailResponse.status() ).toBe( 200 ); } ); + test( 'creates every image size when two sizes share dimensions', async ( { + page, + admin, + attachments, + requestUtils, + muPlugin, + baseURL, + } ) => { + // Sizes with the same dimensions are sideloaded as one file, with + // their names sent as an array in `image_size`. Other workers share + // the site, so only this test's requests see the extra sizes. + const trigger = `e2e_${ randomUUID().replaceAll( '-', '' ) }`; + await muPlugin.write( + 'shared-image-sizes.php', + ` { + if ( + /\/wp\/v2\/media\/\d+\/sideload/.test( + decodeURIComponent( response.url() ) + ) + ) { + sideloadStatuses.push( response.status() ); + } + } ); + + const ids = trackUploads( page, attachments ); + const fileInput = page.locator( FILE_INPUT_SELECTOR ).first(); + await expect( fileInput ).toBeAttached( { timeout: 30_000 } ); + await fileInput.setInputFiles( TEST_IMAGE_PATH ); + + await expect( editLinks( page ) ).toBeVisible( { timeout: 60_000 } ); + + expect( sideloadStatuses.length ).toBeGreaterThanOrEqual( 1 ); + expect( + sideloadStatuses.filter( ( status ) => status !== 200 ) + ).toEqual( [] ); + + const attachment = await requestUtils.rest( { + path: `/wp/v2/media/${ ids.at( -1 ) }`, + } ); + expect( Object.keys( attachment.media_details.sizes || {} ) ).toEqual( + expect.arrayContaining( [ 'e2e-shared-a', 'e2e-shared-b' ] ) + ); + } ); + test( 'warns before leaving while a pipeline upload is in flight', async ( { page, admin,