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
18 changes: 16 additions & 2 deletions src/js/_enqueues/wp/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 } );
}
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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.
*
Expand Down
107 changes: 107 additions & 0 deletions tests/e2e/specs/theme-install-favorites.test.js
Original file line number Diff line number Diff line change
@@ -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();
} );
} );
Loading