diff --git a/src/js/_enqueues/admin/site-icon.js b/src/js/_enqueues/admin/site-icon.js index 59a098dc08aac..a1c663cbb185a 100644 --- a/src/js/_enqueues/admin/site-icon.js +++ b/src/js/_enqueues/admin/site-icon.js @@ -22,11 +22,13 @@ * Calculate image selection options based on the attachment dimensions. * * @since 6.5.0 + * @since 7.3.0 Added the `controller` parameter, to let square images skip cropping. * * @param {Object} attachment The attachment object representing the image. + * @param {Object} controller The cropper controller. * @return {Object} The image selection options. */ - function calculateImageSelectOptions( attachment ) { + function calculateImageSelectOptions( attachment, controller ) { var realWidth = attachment.get( 'width' ), realHeight = attachment.get( 'height' ), xInit = 512, @@ -38,6 +40,20 @@ y1, imgSelectOptions; + /* + * Square images can skip cropping. "Skip Cropping" still sends the whole + * image through the crop request, so the site icon sizes are created. + */ + controller.set( 'hasRequiredAspectRatio', realWidth === realHeight ); + controller.set( 'suggestedCropSize', { + width: realWidth, + height: realHeight, + x1: 0, + y1: 0, + x2: xInit, + y2: yInit, + } ); + if ( realWidth / realHeight > ratio ) { yInit = realHeight; xInit = yInit * ratio; @@ -117,17 +133,37 @@ // When an image is selected, run a callback. frame.on( 'select', function () { // Grab the selected attachment. - var attachment = frame.state().get( 'selection' ).first(); + var attachment = frame.state().get( 'selection' ).first(), + size = $el.data( 'size' ); if ( - attachment.attributes.height === $el.data( 'size' ) && - $el.data( 'size' ) === attachment.attributes.width + attachment.attributes.height === size && + size === attachment.attributes.width ) { - switchToUpdate( attachment.attributes ); - frame.close(); - - // Set the value of the hidden input to the attachment id. - $hiddenDataField.val( attachment.id ); + /* + * Since 7.3.0, an image that doesn't need cropping still goes through + * the crop request, so that the site icon and its additional sizes are created. + */ + wp.ajax + .post( 'crop-image', { + nonce: attachment.get( 'nonces' ).edit, + id: attachment.id, + context: 'site-icon', + cropDetails: { + x1: 0, + y1: 0, + width: size, + height: size, + dst_width: size, + dst_height: size, + }, + } ) + .done( function ( croppedImage ) { + frame.trigger( 'cropped', croppedImage ); + } ) + .fail( function () { + frame.trigger( 'content:error:crop' ); + } ); } else { frame.setState( 'cropper' ); } diff --git a/tests/e2e/assets/site-icon-1024x1024.png b/tests/e2e/assets/site-icon-1024x1024.png new file mode 100644 index 0000000000000..6dc6a9ba6b02c Binary files /dev/null and b/tests/e2e/assets/site-icon-1024x1024.png differ diff --git a/tests/e2e/assets/site-icon-512x512.png b/tests/e2e/assets/site-icon-512x512.png new file mode 100644 index 0000000000000..0a5a7b30201d5 Binary files /dev/null and b/tests/e2e/assets/site-icon-512x512.png differ diff --git a/tests/e2e/assets/site-icon-800x600.png b/tests/e2e/assets/site-icon-800x600.png new file mode 100644 index 0000000000000..d3acdf06f2c93 Binary files /dev/null and b/tests/e2e/assets/site-icon-800x600.png differ diff --git a/tests/e2e/specs/site-icon.test.js b/tests/e2e/specs/site-icon.test.js new file mode 100644 index 0000000000000..e9b65e44c2bc4 --- /dev/null +++ b/tests/e2e/specs/site-icon.test.js @@ -0,0 +1,146 @@ +/** + * External dependencies + */ +import path from 'path'; + +/** + * WordPress dependencies + */ +import { test, expect } from '@wordpress/e2e-test-utils-playwright'; + +const SITE_ICON_SIZES = [ + 'site_icon-32', + 'site_icon-64', + 'site_icon-180', + 'site_icon-192', + 'site_icon-270', +]; + +test.describe( 'Site Icon in General Settings', () => { + const mediaIds = []; + + test.afterAll( async ( { requestUtils } ) => { + for ( const id of mediaIds ) { + await requestUtils.deleteMedia( id ); + } + } ); + + /** + * Uploads an image from the e2e assets folder. + * + * @param {Object} requestUtils The request utilities. + * @param {string} filename The image file name. + * @return {Promise} The attachment ID. + */ + async function uploadImage( requestUtils, filename ) { + const media = await requestUtils.uploadMedia( + path.join( __dirname, '../assets', filename ) + ); + mediaIds.push( media.id ); + return media.id; + } + + /** + * Selects an image for the Site Icon in Settings > General. + * + * @param {Object} page The page. + * @param {Object} admin The admin utilities. + * @param {number} mediaId The attachment ID. + * @return {Promise} The media modal locator. + */ + async function selectSiteIcon( page, admin, mediaId ) { + await admin.visitAdminPage( '/options-general.php' ); + await page.locator( '#choose-from-library-button' ).click(); + + const modal = page.locator( '.media-modal' ); + await modal.locator( `li.attachment[data-id="${ mediaId }"]` ).click(); + await modal.locator( '.media-button-select' ).click(); + + return modal; + } + + /** + * Checks that the selected Site Icon is a new cropped copy with all site icon sizes. + * + * @param {Object} page The page. + * @param {Object} requestUtils The request utilities. + * @param {number} originalId The ID of the image that was selected. + */ + async function expectCroppedSiteIcon( page, requestUtils, originalId ) { + const field = page.locator( '#site_icon_hidden_field' ); + await expect( field ).not.toHaveValue( '' ); + await expect( field ).not.toHaveValue( String( originalId ) ); + + const croppedId = Number( await field.inputValue() ); + mediaIds.push( croppedId ); + + const cropped = await requestUtils.rest( { + path: `/wp/v2/media/${ croppedId }`, + params: { context: 'edit' }, + } ); + + expect( cropped.media_details.width ).toBe( 512 ); + expect( cropped.media_details.height ).toBe( 512 ); + expect( Object.keys( cropped.media_details.sizes ) ).toEqual( + expect.arrayContaining( SITE_ICON_SIZES ) + ); + } + + test( 'creates the site icon sizes for an image that is exactly 512 by 512', async ( { + page, + admin, + requestUtils, + } ) => { + const mediaId = await uploadImage( + requestUtils, + 'site-icon-512x512.png' + ); + + const modal = await selectSiteIcon( page, admin, mediaId ); + + // The image does not need cropping, so the cropper is not shown. + await expect( modal ).toBeHidden(); + await expectCroppedSiteIcon( page, requestUtils, mediaId ); + } ); + + test( 'lets a larger square image skip cropping and creates the site icon sizes', async ( { + page, + admin, + requestUtils, + } ) => { + const mediaId = await uploadImage( + requestUtils, + 'site-icon-1024x1024.png' + ); + + const modal = await selectSiteIcon( page, admin, mediaId ); + + await expect( + modal.getByRole( 'button', { name: 'Crop image' } ) + ).toBeVisible(); + await modal.getByRole( 'button', { name: 'Skip cropping' } ).click(); + + await expect( modal ).toBeHidden(); + await expectCroppedSiteIcon( page, requestUtils, mediaId ); + } ); + + test( 'does not offer to skip cropping for an image that is not square', async ( { + page, + admin, + requestUtils, + } ) => { + const mediaId = await uploadImage( + requestUtils, + 'site-icon-800x600.png' + ); + + const modal = await selectSiteIcon( page, admin, mediaId ); + + await expect( + modal.getByRole( 'button', { name: 'Crop image' } ) + ).toBeVisible(); + await expect( + modal.getByRole( 'button', { name: 'Skip cropping' } ) + ).toHaveCount( 0 ); + } ); +} );