Skip to content
Open
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
54 changes: 45 additions & 9 deletions src/js/_enqueues/admin/site-icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add tests for the changes here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 4c1329d: tests/e2e/specs/site-icon.test.js (there is no QUnit coverage for site-icon.js). It covers a 512×512 image, a 1024×1024 image using "Skip cropping", and an 800×600 image that is not offered "Skip cropping", and checks the cropped copy has site_icon-32, -64, -180, -192 and -270. Locally all 3 pass with this PR; with trunk's site-icon.js the first two fail.

var realWidth = attachment.get( 'width' ),
realHeight = attachment.get( 'height' ),
xInit = 512,
Expand All @@ -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;
Expand Down Expand Up @@ -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' );
}
Expand Down
Binary file added tests/e2e/assets/site-icon-1024x1024.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/e2e/assets/site-icon-512x512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/e2e/assets/site-icon-800x600.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
146 changes: 146 additions & 0 deletions tests/e2e/specs/site-icon.test.js
Original file line number Diff line number Diff line change
@@ -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<number>} 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<Object>} 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();

Check failure on line 56 in tests/e2e/specs/site-icon.test.js

View workflow job for this annotation

GitHub Actions / E2E Tests / parallel, SCRIPT_DEBUG enabled

[parallel] › tests/e2e/specs/site-icon.test.js:106:2 › Site Icon in General Settings › lets a larger square image skip cropping and creates the site icon sizes

3) [parallel] › tests/e2e/specs/site-icon.test.js:106:2 › Site Icon in General Settings › lets a larger square image skip cropping and creates the site icon sizes Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── TimeoutError: locator.click: Timeout 10000ms exceeded. Call log: - waiting for locator('.media-modal').locator('li.attachment[data-id="117"]') 54 | 55 | const modal = page.locator( '.media-modal' ); > 56 | await modal.locator( `li.attachment[data-id="${ mediaId }"]` ).click(); | ^ 57 | await modal.locator( '.media-button-select' ).click(); 58 | 59 | return modal; at selectSiteIcon (/home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:56:66) at /home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:116:17

Check failure on line 56 in tests/e2e/specs/site-icon.test.js

View workflow job for this annotation

GitHub Actions / E2E Tests / parallel, SCRIPT_DEBUG enabled

[parallel] › tests/e2e/specs/site-icon.test.js:106:2 › Site Icon in General Settings › lets a larger square image skip cropping and creates the site icon sizes

3) [parallel] › tests/e2e/specs/site-icon.test.js:106:2 › Site Icon in General Settings › lets a larger square image skip cropping and creates the site icon sizes TimeoutError: locator.click: Timeout 10000ms exceeded. Call log: - waiting for locator('.media-modal').locator('li.attachment[data-id="116"]') 54 | 55 | const modal = page.locator( '.media-modal' ); > 56 | await modal.locator( `li.attachment[data-id="${ mediaId }"]` ).click(); | ^ 57 | await modal.locator( '.media-button-select' ).click(); 58 | 59 | return modal; at selectSiteIcon (/home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:56:66) at /home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:116:17

Check failure on line 56 in tests/e2e/specs/site-icon.test.js

View workflow job for this annotation

GitHub Actions / E2E Tests / parallel, SCRIPT_DEBUG enabled

[parallel] › tests/e2e/specs/site-icon.test.js:127:2 › Site Icon in General Settings › does not offer to skip cropping for an image that is not square

2) [parallel] › tests/e2e/specs/site-icon.test.js:127:2 › Site Icon in General Settings › does not offer to skip cropping for an image that is not square Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── TimeoutError: locator.click: Timeout 10000ms exceeded. Call log: - waiting for locator('.media-modal').locator('li.attachment[data-id="115"]') 54 | 55 | const modal = page.locator( '.media-modal' ); > 56 | await modal.locator( `li.attachment[data-id="${ mediaId }"]` ).click(); | ^ 57 | await modal.locator( '.media-button-select' ).click(); 58 | 59 | return modal; at selectSiteIcon (/home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:56:66) at /home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:137:17

Check failure on line 56 in tests/e2e/specs/site-icon.test.js

View workflow job for this annotation

GitHub Actions / E2E Tests / parallel, SCRIPT_DEBUG enabled

[parallel] › tests/e2e/specs/site-icon.test.js:127:2 › Site Icon in General Settings › does not offer to skip cropping for an image that is not square

2) [parallel] › tests/e2e/specs/site-icon.test.js:127:2 › Site Icon in General Settings › does not offer to skip cropping for an image that is not square TimeoutError: locator.click: Timeout 10000ms exceeded. Call log: - waiting for locator('.media-modal').locator('li.attachment[data-id="113"]') 54 | 55 | const modal = page.locator( '.media-modal' ); > 56 | await modal.locator( `li.attachment[data-id="${ mediaId }"]` ).click(); | ^ 57 | await modal.locator( '.media-button-select' ).click(); 58 | 59 | return modal; at selectSiteIcon (/home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:56:66) at /home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:137:17

Check failure on line 56 in tests/e2e/specs/site-icon.test.js

View workflow job for this annotation

GitHub Actions / E2E Tests / parallel, SCRIPT_DEBUG enabled

[parallel] › tests/e2e/specs/site-icon.test.js:89:2 › Site Icon in General Settings › creates the site icon sizes for an image that is exactly 512 by 512

1) [parallel] › tests/e2e/specs/site-icon.test.js:89:2 › Site Icon in General Settings › creates the site icon sizes for an image that is exactly 512 by 512 Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── TimeoutError: locator.click: Timeout 10000ms exceeded. Call log: - waiting for locator('.media-modal').locator('li.attachment[data-id="114"]') 54 | 55 | const modal = page.locator( '.media-modal' ); > 56 | await modal.locator( `li.attachment[data-id="${ mediaId }"]` ).click(); | ^ 57 | await modal.locator( '.media-button-select' ).click(); 58 | 59 | return modal; at selectSiteIcon (/home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:56:66) at /home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:99:17

Check failure on line 56 in tests/e2e/specs/site-icon.test.js

View workflow job for this annotation

GitHub Actions / E2E Tests / parallel, SCRIPT_DEBUG enabled

[parallel] › tests/e2e/specs/site-icon.test.js:89:2 › Site Icon in General Settings › creates the site icon sizes for an image that is exactly 512 by 512

1) [parallel] › tests/e2e/specs/site-icon.test.js:89:2 › Site Icon in General Settings › creates the site icon sizes for an image that is exactly 512 by 512 TimeoutError: locator.click: Timeout 10000ms exceeded. Call log: - waiting for locator('.media-modal').locator('li.attachment[data-id="112"]') 54 | 55 | const modal = page.locator( '.media-modal' ); > 56 | await modal.locator( `li.attachment[data-id="${ mediaId }"]` ).click(); | ^ 57 | await modal.locator( '.media-button-select' ).click(); 58 | 59 | return modal; at selectSiteIcon (/home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:56:66) at /home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:99:17

Check failure on line 56 in tests/e2e/specs/site-icon.test.js

View workflow job for this annotation

GitHub Actions / E2E Tests / parallel, SCRIPT_DEBUG disabled

[parallel] › tests/e2e/specs/site-icon.test.js:106:2 › Site Icon in General Settings › lets a larger square image skip cropping and creates the site icon sizes

3) [parallel] › tests/e2e/specs/site-icon.test.js:106:2 › Site Icon in General Settings › lets a larger square image skip cropping and creates the site icon sizes Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── TimeoutError: locator.click: Timeout 10000ms exceeded. Call log: - waiting for locator('.media-modal').locator('li.attachment[data-id="117"]') 54 | 55 | const modal = page.locator( '.media-modal' ); > 56 | await modal.locator( `li.attachment[data-id="${ mediaId }"]` ).click(); | ^ 57 | await modal.locator( '.media-button-select' ).click(); 58 | 59 | return modal; at selectSiteIcon (/home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:56:66) at /home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:116:17

Check failure on line 56 in tests/e2e/specs/site-icon.test.js

View workflow job for this annotation

GitHub Actions / E2E Tests / parallel, SCRIPT_DEBUG disabled

[parallel] › tests/e2e/specs/site-icon.test.js:106:2 › Site Icon in General Settings › lets a larger square image skip cropping and creates the site icon sizes

3) [parallel] › tests/e2e/specs/site-icon.test.js:106:2 › Site Icon in General Settings › lets a larger square image skip cropping and creates the site icon sizes TimeoutError: locator.click: Timeout 10000ms exceeded. Call log: - waiting for locator('.media-modal').locator('li.attachment[data-id="116"]') 54 | 55 | const modal = page.locator( '.media-modal' ); > 56 | await modal.locator( `li.attachment[data-id="${ mediaId }"]` ).click(); | ^ 57 | await modal.locator( '.media-button-select' ).click(); 58 | 59 | return modal; at selectSiteIcon (/home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:56:66) at /home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:116:17

Check failure on line 56 in tests/e2e/specs/site-icon.test.js

View workflow job for this annotation

GitHub Actions / E2E Tests / parallel, SCRIPT_DEBUG disabled

[parallel] › tests/e2e/specs/site-icon.test.js:127:2 › Site Icon in General Settings › does not offer to skip cropping for an image that is not square

2) [parallel] › tests/e2e/specs/site-icon.test.js:127:2 › Site Icon in General Settings › does not offer to skip cropping for an image that is not square Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── TimeoutError: locator.click: Timeout 10000ms exceeded. Call log: - waiting for locator('.media-modal').locator('li.attachment[data-id="115"]') 54 | 55 | const modal = page.locator( '.media-modal' ); > 56 | await modal.locator( `li.attachment[data-id="${ mediaId }"]` ).click(); | ^ 57 | await modal.locator( '.media-button-select' ).click(); 58 | 59 | return modal; at selectSiteIcon (/home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:56:66) at /home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:137:17

Check failure on line 56 in tests/e2e/specs/site-icon.test.js

View workflow job for this annotation

GitHub Actions / E2E Tests / parallel, SCRIPT_DEBUG disabled

[parallel] › tests/e2e/specs/site-icon.test.js:127:2 › Site Icon in General Settings › does not offer to skip cropping for an image that is not square

2) [parallel] › tests/e2e/specs/site-icon.test.js:127:2 › Site Icon in General Settings › does not offer to skip cropping for an image that is not square TimeoutError: locator.click: Timeout 10000ms exceeded. Call log: - waiting for locator('.media-modal').locator('li.attachment[data-id="113"]') 54 | 55 | const modal = page.locator( '.media-modal' ); > 56 | await modal.locator( `li.attachment[data-id="${ mediaId }"]` ).click(); | ^ 57 | await modal.locator( '.media-button-select' ).click(); 58 | 59 | return modal; at selectSiteIcon (/home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:56:66) at /home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:137:17

Check failure on line 56 in tests/e2e/specs/site-icon.test.js

View workflow job for this annotation

GitHub Actions / E2E Tests / parallel, SCRIPT_DEBUG disabled

[parallel] › tests/e2e/specs/site-icon.test.js:89:2 › Site Icon in General Settings › creates the site icon sizes for an image that is exactly 512 by 512

1) [parallel] › tests/e2e/specs/site-icon.test.js:89:2 › Site Icon in General Settings › creates the site icon sizes for an image that is exactly 512 by 512 Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── TimeoutError: locator.click: Timeout 10000ms exceeded. Call log: - waiting for locator('.media-modal').locator('li.attachment[data-id="114"]') 54 | 55 | const modal = page.locator( '.media-modal' ); > 56 | await modal.locator( `li.attachment[data-id="${ mediaId }"]` ).click(); | ^ 57 | await modal.locator( '.media-button-select' ).click(); 58 | 59 | return modal; at selectSiteIcon (/home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:56:66) at /home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:99:17

Check failure on line 56 in tests/e2e/specs/site-icon.test.js

View workflow job for this annotation

GitHub Actions / E2E Tests / parallel, SCRIPT_DEBUG disabled

[parallel] › tests/e2e/specs/site-icon.test.js:89:2 › Site Icon in General Settings › creates the site icon sizes for an image that is exactly 512 by 512

1) [parallel] › tests/e2e/specs/site-icon.test.js:89:2 › Site Icon in General Settings › creates the site icon sizes for an image that is exactly 512 by 512 TimeoutError: locator.click: Timeout 10000ms exceeded. Call log: - waiting for locator('.media-modal').locator('li.attachment[data-id="112"]') 54 | 55 | const modal = page.locator( '.media-modal' ); > 56 | await modal.locator( `li.attachment[data-id="${ mediaId }"]` ).click(); | ^ 57 | await modal.locator( '.media-button-select' ).click(); 58 | 59 | return modal; at selectSiteIcon (/home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:56:66) at /home/runner/work/wordpress-develop/wordpress-develop/tests/e2e/specs/site-icon.test.js:99:17
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 );
} );
} );
Loading