Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Blueprints] setSiteLanguage step – download the latest RC translations for Nightly and Beta builds of WordPress #1987

Merged
merged 14 commits into from
Nov 19, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { MinifiedWordPressVersions } from '@wp-playground/wordpress-builds';
import { getWordPressTranslationUrl } from './set-site-language';

describe('getTranslationUrl()', () => {
it('should return a major.minor translation URL for a major.minor version', () => {
expect(getWordPressTranslationUrl('6.6', 'en_US')).toBe(
'https://downloads.wordpress.org/translation/core/6.6/en_US.zip'
);
});

it('should return a major.minor.patch translation URL for a major.minor.patch version', () => {
expect(getWordPressTranslationUrl('6.5.1', 'es_ES')).toBe(
'https://downloads.wordpress.org/translation/core/6.5.1/es_ES.zip'
);
});

[
{
version: '6.6-RC1',
description:
'should return the latest RC translation URL for a RC version',
},
{
version: '6.6-beta2',
description:
'should return the latest RC translation URL for a beta version',
},
{
version: '6.6-nightly',
description:
'should return the latest RC translation URL for a nightly version',
},
{
version: '6.8-alpha-59408',
description:
'should return the latest RC translation URL for an alpha version',
},
].forEach(({ version, description }) => {
it(description, () => {
const latestBetaVersion =
MinifiedWordPressVersions['beta'].split('-')[0];
expect(getWordPressTranslationUrl(version, 'en_US')).toBe(
`https://downloads.wordpress.org/translation/core/${latestBetaVersion}-RC/en_US.zip`
);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { StepHandler } from '.';
import { unzipFile } from '@wp-playground/common';
import { logger } from '@php-wasm/logger';
import {
LatestMinifiedWordPressVersion,
MinifiedWordPressVersions,
} from '@wp-playground/wordpress-builds';

/**
* @inheritDoc setSiteLanguage
* @hasRunnableExample
Expand All @@ -19,6 +24,48 @@ export interface SetSiteLanguageStep {
language: string;
}

/**
* Returns the URL to download a WordPress translation package.
*
* If the WordPress version doesn't have a translation package,
* the latest "RC" version will be used instead.
*/
export const getWordPressTranslationUrl = (
wpVersion: string,
language: string
) => {
/**
* The translation API provides translations for all WordPress releases
* including patch releases.
*
* RC and beta versions don't have individual translation packages.
* They all share the same "RC" translation package.
*
* Nightly versions don't have a "nightly" translation package.
* So, the best we can do is download the RC translation package,
* because it contains the latest available translations.
*
* The WordPress.org translation API uses "RC" instead of
* "RC1", "RC2", "BETA1", "BETA2", etc.
*
* For example translations for WordPress 6.6-BETA1 or 6.6-RC1 are found under
* https://downloads.wordpress.org/translation/core/6.6-RC/en_GB.zip
*/
if (wpVersion.match(/(\d.\d(.\d)?)-(alpha|beta|nightly|rc).*$/i)) {
wpVersion = MinifiedWordPressVersions['beta'].replace(
/(rc|beta).*$/i,
'RC'
);
} else if (!wpVersion.match(/^(\d+\.\d+)(?:\.\d+)?$/)) {
/**
* If the WordPress version string isn't a major.minor or major.minor.patch,
* the latest available WordPress build version will be used instead.
*/
wpVersion = LatestMinifiedWordPressVersion;
}
return `https://downloads.wordpress.org/translation/core/${wpVersion}/${language}.zip`;
};

/**
* Sets the site language and download translations.
*/
Expand All @@ -44,7 +91,7 @@ export const setSiteLanguage: StepHandler<SetSiteLanguageStep> = async (

const translations = [
{
url: `https://downloads.wordpress.org/translation/core/${wpVersion}/${language}.zip`,
url: getWordPressTranslationUrl(wpVersion, language),
type: 'core',
},
];
Expand Down
18 changes: 18 additions & 0 deletions packages/playground/website/playwright/e2e/blueprints.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,21 @@ test('should correctly redirect to a multisite wp-admin url', async ({
await website.goto(`./#${encodedBlueprint}`);
await expect(wordpress.locator('body')).toContainText('General Settings');
});

['latest', 'nightly', 'beta'].forEach((version) => {
test(`should translate WP-admin to Spanish for the ${version} WordPress build`, async ({
website,
wordpress,
}) => {
const blueprint: Blueprint = {
landingPage: '/wp-admin/',
preferredVersions: {
wp: version,
},
steps: [{ step: 'setSiteLanguage', language: 'es_ES' }],
};
const encodedBlueprint = JSON.stringify(blueprint);
await website.goto(`./#${encodedBlueprint}`);
await expect(wordpress.locator('body')).toContainText('Escritorio');
});
});
8 changes: 8 additions & 0 deletions packages/playground/website/playwright/e2e/query-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ test('should not login the user in if the login query parameter is set to no', a
});
});

test('should translate WP-admin to Spanish using the language query parameter', async ({
website,
wordpress,
}) => {
await website.goto('./?language=es_ES&url=/wp-admin/');
await expect(wordpress.locator('body')).toContainText('Escritorio');
});

/**
* There is no reason to remove encoded control characters from the URL.
* For example, the html-api-debugger accepts markup with newlines encoded
Expand Down
16 changes: 16 additions & 0 deletions packages/playground/wordpress/src/version-detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ export async function getLoadedWordPressVersion(
return versionStringToLoadedWordPressVersion(versionString);
}

/**
* Returns a WordPress build version string, for a given WordPress version string.
*
* You can find the full list of supported build version strings in
* packages/playground/wordpress-builds/src/wordpress/wp-versions.json
*
* Each released version will be converted to the major.minor format.
* For example 6.6.1 will be converted to 6.6.
*
* Release candidates (RC) and beta releases are converted to "beta".
*
* Nightly releases are converted to "nightly".
*
* @param wpVersionString - A WordPress version string.
* @returns A Playground WordPress build version.
*/
export function versionStringToLoadedWordPressVersion(
wpVersionString: string
): string {
Expand Down