From f78ea33ed1ae4c80a57950d7d222eac3b1bead7c Mon Sep 17 00:00:00 2001 From: Igor Rozum Date: Mon, 23 Nov 2026 12:03:56 -0500 Subject: [PATCH] Themes: Key the Add Themes favorites query by the saved username --- src/js/_enqueues/wp/theme.js | 18 ++- .../e2e/specs/theme-install-favorites.test.js | 107 ++++++++++++++++++ 2 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 tests/e2e/specs/theme-install-favorites.test.js diff --git a/src/js/_enqueues/wp/theme.js b/src/js/_enqueues/wp/theme.js index dabd64cc6c6ba..2ec6ea49438d0 100644 --- a/src/js/_enqueues/wp/theme.js +++ b/src/js/_enqueues/wp/theme.js @@ -1705,6 +1705,7 @@ themes.view.Installer = themes.view.Appearance.extend({ this.uploader(); this.collection = new themes.Collection(); + this.favoritesUser = $( '#wporg-username-input' ).val(); // Bump `collection.currentQuery.page` and request more themes if we hit the end of the page. this.listenTo( this, 'theme:end', function() { @@ -1766,6 +1767,8 @@ themes.view.Installer = themes.view.Appearance.extend({ // Get the themes by sending Ajax POST request to api.wordpress.org/themes // or searching the local cache. this.collection.query( { tag: 'full-site-editing' } ); + } else if ( 'favorites' === section ) { + this.collection.query( this.favoritesRequest() ); } else { this.collection.query( { browse: section } ); } @@ -1890,7 +1893,6 @@ themes.view.Installer = themes.view.Appearance.extend({ saveUsername: function ( event ) { var username = $( '#wporg-username-input' ).val(), nonce = $( '#wporg-username-nonce' ).val(), - request = { browse: 'favorites', user: username }, that = this; if ( event ) { @@ -1910,11 +1912,23 @@ themes.view.Installer = themes.view.Appearance.extend({ success: function () { // Get the themes by sending Ajax POST request to api.wordpress.org/themes // or searching the local cache. - that.collection.query( request ); + that.favoritesUser = username; + that.collection.query( that.favoritesRequest() ); } } ); }, + // Keyed by username so the query cache can't serve another user's favorites. + favoritesRequest: function() { + var request = { browse: 'favorites' }; + + if ( this.favoritesUser ) { + request.user = this.favoritesUser; + } + + return request; + }, + /** * Get the checked filters. * diff --git a/tests/e2e/specs/theme-install-favorites.test.js b/tests/e2e/specs/theme-install-favorites.test.js new file mode 100644 index 0000000000000..f40880c13da16 --- /dev/null +++ b/tests/e2e/specs/theme-install-favorites.test.js @@ -0,0 +1,107 @@ +/** + * WordPress dependencies + */ +import { test, expect } from '@wordpress/e2e-test-utils-playwright'; + +const ALICE_THEME_NAME = 'Alice Favorite Theme'; + +const aliceTheme = { + name: ALICE_THEME_NAME, + slug: 'alice-favorite-theme', + version: '1.0.0', + preview_url: 'https://wp-themes.com/alice-favorite-theme/', + author: { user_nicename: 'alice', display_name: 'Alice' }, + screenshot_url: '//ts.w.org/wp-content/themes/alice-favorite-theme/screenshot.png', + rating: 100, + num_ratings: 10, + homepage: 'https://wordpress.org/themes/alice-favorite-theme/', + description: 'A theme favorited by Alice.', + installed: false, + compatible_wp: true, + compatible_php: true, + requires: false, + requires_php: false, +}; + +function queryThemesResponse( themes ) { + return { + success: true, + data: { + info: { page: 1, pages: 1, results: themes.length }, + themes, + }, + }; +} + +// Alice is the saved username, so a favorites request without `user` is hers too. +function respondToThemesQuery( params ) { + const isFavorites = params.get( 'request[browse]' ) === 'favorites'; + const user = params.get( 'request[user]' ); + + if ( isFavorites && ( user === null || user === 'alice' ) ) { + return queryThemesResponse( [ aliceTheme ] ); + } + + return queryThemesResponse( [] ); +} + +test.describe( 'Add Themes: Favorites tab', () => { + test.beforeEach( async ( { page } ) => { + await page.route( '**/wp-admin/admin-ajax.php', async ( route ) => { + const params = new URLSearchParams( route.request().postData() ); + const action = params.get( 'action' ); + + if ( action === 'query-themes' ) { + return route.fulfill( { json: respondToThemesQuery( params ) } ); + } + + if ( action === 'save-wporg-username' ) { + return route.fulfill( { json: { success: true, data: true } } ); + } + + return route.continue(); + } ); + } ); + + async function submitUsername( page, username ) { + await page.getByLabel( 'Your WordPress.org username:' ).fill( username ); + await page.getByRole( 'button', { name: 'Get Favorites' } ).click(); + } + + async function switchToBlockThemesAndBack( page ) { + await page.getByRole( 'link', { name: 'Block Themes' } ).click(); + await page.getByRole( 'link', { name: 'Favorites' } ).click(); + } + + test( "shows the current username's favorites after switching tabs", async ( { + admin, + page, + } ) => { + await admin.visitAdminPage( 'theme-install.php', 'browse=favorites' ); + await expect( page.getByText( ALICE_THEME_NAME ).first() ).toBeVisible(); + + await submitUsername( page, 'bob' ); + await expect( page.locator( 'p.no-themes' ) ).toBeVisible(); + + await switchToBlockThemesAndBack( page ); + + await expect( page.getByText( ALICE_THEME_NAME ) ).toHaveCount( 0 ); + await expect( page.locator( 'p.no-themes' ) ).toBeVisible(); + } ); + + test( "shows an earlier username's favorites again after re-submitting it", async ( { + admin, + page, + } ) => { + await admin.visitAdminPage( 'theme-install.php', 'browse=favorites' ); + await expect( page.getByText( ALICE_THEME_NAME ).first() ).toBeVisible(); + + await submitUsername( page, 'bob' ); + await expect( page.locator( 'p.no-themes' ) ).toBeVisible(); + + await submitUsername( page, 'alice' ); + await switchToBlockThemesAndBack( page ); + + await expect( page.getByText( ALICE_THEME_NAME ).first() ).toBeVisible(); + } ); +} );