Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 5 additions & 4 deletions src/js/_enqueues/admin/media-upload-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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 ] );
Expand Down
69 changes: 69 additions & 0 deletions tests/e2e/specs/media-new-client-side-upload.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import { randomUUID } from 'node:crypto';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -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',
`<?php
if ( isset( $_COOKIE['${ trigger }'] ) ) {
add_action(
'after_setup_theme',
function () {
add_image_size( 'e2e-shared-a', 320, 320, true );
add_image_size( 'e2e-shared-b', 320, 320, true );
}
);
}`
);
await page
.context()
.addCookies( [ { name: trigger, value: '1', url: baseURL } ] );

await admin.visitAdminPage( 'media-new.php' );

await expectCrossOriginIsolated( page );

const sideloadStatuses = [];
page.on( 'response', ( response ) => {
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,
Expand Down
Loading