From 6853eb4f7d37117ae14f9b5a4cf34b33aa0f7175 Mon Sep 17 00:00:00 2001 From: abhishek kumar Date: Tue, 10 Nov 2026 00:16:55 +0530 Subject: [PATCH] Media: Show uploads after resetting the date filter to "All dates". Choosing a month and then "All dates" set `monthnum` and `year` to `false` instead of `null`. The query kept both keys, stopped observing the upload queue, and new uploads did not appear in the grid. Use `null`, as the other "all" filters do. The toolbar now also refreshes when an attachment in the selection changes its `uploading` state, so the insert/select button is enabled once an upload finishes, even when the current query does not show it. Client-side uploads clear `uploading` silently before refetching the attachment, so they now trigger `change:uploading` once the refetch completes, as classic uploads do. See #54839. --- src/js/_enqueues/admin/media-frame-upload.js | 5 + src/js/media/views/attachment-filters/date.js | 4 +- src/js/media/views/toolbar.js | 2 +- .../specs/media-date-filter-upload.test.js | 228 ++++++++++++++++++ tests/qunit/index.html | 1 + .../wp-includes/js/media/test-toolbar-view.js | 65 +++++ 6 files changed, 302 insertions(+), 3 deletions(-) create mode 100644 tests/e2e/specs/media-date-filter-upload.test.js create mode 100644 tests/qunit/wp-includes/js/media/test-toolbar-view.js diff --git a/src/js/_enqueues/admin/media-frame-upload.js b/src/js/_enqueues/admin/media-frame-upload.js index f5af17d7991d2..14e3a931ecc6f 100644 --- a/src/js/_enqueues/admin/media-frame-upload.js +++ b/src/js/_enqueues/admin/media-frame-upload.js @@ -94,6 +94,11 @@ model.set( attachment ); } ) .always( function () { + // The silent set above fired no event. Announce the finished + // upload now, as wp-plupload.js does, so listeners such as the + // toolbar can update, even when the query does not show it. + model.trigger( 'change:uploading', model, false, {} ); + maybeResetQueue(); // Parity with wp-plupload.js, which exposes this callback so diff --git a/src/js/media/views/attachment-filters/date.js b/src/js/media/views/attachment-filters/date.js index 4d1b8f93692fb..c232a83d1bdfb 100644 --- a/src/js/media/views/attachment-filters/date.js +++ b/src/js/media/views/attachment-filters/date.js @@ -29,8 +29,8 @@ DateFilter = wp.media.view.AttachmentFilters.extend(/** @lends wp.media.view.Att filters.all = { text: l10n.allDates, props: { - monthnum: false, - year: false + monthnum: null, + year: null }, priority: 10 }; diff --git a/src/js/media/views/toolbar.js b/src/js/media/views/toolbar.js index 68d58d93faffc..ce5053a883ca4 100644 --- a/src/js/media/views/toolbar.js +++ b/src/js/media/views/toolbar.js @@ -47,7 +47,7 @@ Toolbar = View.extend(/** @lends wp.media.view.Toolbar.prototype */{ } if ( selection ) { - selection.on( 'add remove reset', this.refresh, this ); + selection.on( 'add remove reset change:uploading', this.refresh, this ); } if ( library ) { diff --git a/tests/e2e/specs/media-date-filter-upload.test.js b/tests/e2e/specs/media-date-filter-upload.test.js new file mode 100644 index 0000000000000..7eacdc411bf16 --- /dev/null +++ b/tests/e2e/specs/media-date-filter-upload.test.js @@ -0,0 +1,228 @@ +/** + * External dependencies + */ +import { randomUUID } from 'node:crypto'; +import fs from 'node:fs'; + +/** + * WordPress dependencies + */ +import { test, expect } from '@wordpress/e2e-test-utils-playwright'; + +/** + * Internal dependencies + */ +import { TEST_IMAGE_PATH, tile } from '../utils/media-pipeline'; + +// The backdated attachment puts this month in the date filter. +const OLD_DATE = '2020-01-15T12:00:00'; +const OLD_MONTH = 'January 2020'; + +/* + * Uploads take the client-side pipeline when the screen is cross-origin + * isolated, and classic plupload otherwise. Removing the + * Document-Isolation-Policy header reproduces a browser without isolation. + */ +const UPLOAD_PATHS = [ 'client-side', 'classic' ]; + +/** + * Removes the Document-Isolation-Policy header from a screen for classic uploads. + * + * @param {import('@playwright/test').Page} page The page. + * @param {string} uploadPath 'client-side' or 'classic'. + * @param {string} screen The admin screen, e.g. '/wp-admin/upload.php'. + */ +async function useUploadPath( page, uploadPath, screen ) { + if ( 'classic' !== uploadPath ) { + return; + } + await page.route( + ( url ) => url.pathname.endsWith( screen ), + async ( route ) => { + const response = await route.fetch(); + const headers = { ...response.headers() }; + delete headers[ 'document-isolation-policy' ]; + await route.fulfill( { response, headers } ); + } + ); +} + +/** + * Checks the loaded screen uses the upload path the test asked for. + * + * @param {import('@playwright/test').Page} page The page. + * @param {string} uploadPath 'client-side' or 'classic'. + */ +async function expectUploadPath( page, uploadPath ) { + const isolated = await page.evaluate( () => + Boolean( window.crossOriginIsolated ) + ); + if ( 'classic' === uploadPath ) { + expect( isolated ).toBe( false ); + return; + } + test.skip( + ! isolated, + 'The client-side pipeline requires a cross-origin isolated context' + ); +} + +const dateFilter = ( scope ) => + scope.getByRole( 'combobox', { name: 'Filter by date', exact: true } ); + +test.describe( 'Media uploads with the date filter set', () => { + let prefix; + + test.beforeEach( async ( { requestUtils } ) => { + prefix = `e2e-54839-${ randomUUID() }`; + + const old = await requestUtils.uploadMedia( TEST_IMAGE_PATH ); + await requestUtils.rest( { + method: 'POST', + path: `/wp/v2/media/${ old.id }`, + data: { title: `${ prefix }-old`, date: OLD_DATE }, + } ); + } ); + + test.afterEach( async ( { requestUtils } ) => { + const items = await requestUtils.rest( { + path: '/wp/v2/media', + params: { search: prefix, per_page: 100 }, + } ); + for ( const item of items ) { + await requestUtils.deleteMedia( item.id ); + } + } ); + + /** + * Uploads a uniquely named copy of the test image and waits for the + * upload queue to be reset, which both upload paths do once every + * queued file has finished. + * + * @param {import('@playwright/test').Page} page The page. + * @param {string} selector The plupload file input. + */ + async function upload( page, selector ) { + await page.evaluate( () => { + window.e2eUploadDone = new Promise( ( resolve ) => + window.wp.Uploader.queue.once( 'reset', () => resolve() ) + ); + } ); + await page.locator( selector ).first().setInputFiles( { + name: `${ prefix }-new.jpg`, + mimeType: 'image/jpeg', + buffer: fs.readFileSync( TEST_IMAGE_PATH ), + } ); + await page.evaluate( () => window.e2eUploadDone ); + } + + for ( const uploadPath of UPLOAD_PATHS ) { + test.describe( `${ uploadPath } uploads`, () => { + test.describe( 'Media Library grid', () => { + const FILE_INPUT = '.moxie-shim-html5 input[type="file"]'; + + test.beforeEach( async ( { admin, page } ) => { + await useUploadPath( page, uploadPath, '/wp-admin/upload.php' ); + await admin.visitAdminPage( 'upload.php', 'mode=grid' ); + await expectUploadPath( page, uploadPath ); + } ); + + test( 'shows the upload when the date filter is untouched', async ( { + page, + } ) => { + await upload( page, FILE_INPUT ); + + await expect( tile( page, `${ prefix }-new` ) ).toBeVisible(); + } ); + + test( 'shows the upload after going back to All dates', async ( { + page, + } ) => { + await dateFilter( page ).selectOption( { label: OLD_MONTH } ); + await dateFilter( page ).selectOption( { label: 'All dates' } ); + await expect( tile( page, `${ prefix }-old` ) ).toBeVisible(); + + await upload( page, FILE_INPUT ); + + await expect( tile( page, `${ prefix }-new` ) ).toBeVisible(); + } ); + } ); + + test.describe( 'Media modal', () => { + // The plupload HTML5 runtime creates this hidden file input + // over the modal's "Select Files" button. + const FILE_INPUT = + '.media-modal .moxie-shim-html5 input[type="file"]'; + + let modal; + const selectButton = () => + modal.getByRole( 'button', { name: 'Select', exact: true } ); + + test.beforeEach( async ( { admin, editor, page } ) => { + await useUploadPath( page, uploadPath, '/wp-admin/post-new.php' ); + await admin.createNewPost(); + await expectUploadPath( page, uploadPath ); + + // Blocks inserted before the editor sets up the post are reset away. + await expect( + editor.canvas.getByRole( 'textbox', { name: 'Add title' } ) + ).toBeVisible(); + await editor.insertBlock( { name: 'core/image' } ); + await editor.canvas + .getByRole( 'button', { name: 'Media Library' } ) + .click(); + + modal = page.getByRole( 'dialog', { + name: 'Select or Upload Media', + exact: true, + } ); + await modal + .getByRole( 'tab', { name: 'Media Library', exact: true } ) + .click(); + } ); + + test( 'shows and allows selecting the upload when the date filter is untouched', async ( { + page, + } ) => { + await upload( page, FILE_INPUT ); + + await expect( tile( modal, `${ prefix }-new` ) ).toBeVisible(); + await expect( selectButton() ).toBeEnabled(); + } ); + + test( 'shows and allows selecting the upload after going back to All dates', async ( { + page, + } ) => { + await dateFilter( modal ).selectOption( { label: OLD_MONTH } ); + await dateFilter( modal ).selectOption( { label: 'All dates' } ); + await expect( tile( modal, `${ prefix }-old` ) ).toBeVisible(); + + await upload( page, FILE_INPUT ); + + await expect( tile( modal, `${ prefix }-new` ) ).toBeVisible(); + await expect( selectButton() ).toBeEnabled(); + } ); + + test( 'allows selecting the upload while an older month is selected', async ( { + page, + editor, + } ) => { + await dateFilter( modal ).selectOption( { label: OLD_MONTH } ); + await expect( tile( modal, `${ prefix }-old` ) ).toBeVisible(); + + await upload( page, FILE_INPUT ); + + // Whether the grid should show an upload outside the selected + // month is not settled (#54839); only the selection is checked. + await expect( selectButton() ).toBeEnabled(); + await selectButton().click(); + await expect( + editor.canvas + .getByRole( 'document', { name: 'Block: Image' } ) + .locator( 'img' ) + ).toHaveAttribute( 'src', new RegExp( `${ prefix }-new` ) ); + } ); + } ); + } ); + } +} ); diff --git a/tests/qunit/index.html b/tests/qunit/index.html index f9033a776151c..c5ba752852ca6 100644 --- a/tests/qunit/index.html +++ b/tests/qunit/index.html @@ -205,6 +205,7 @@ +