From 7d25ab2133d6ca17b317753b3c9973066fa815a2 Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Thu, 21 Mar 2024 12:54:35 +0200 Subject: [PATCH 01/16] Add test for Page Cache meta visibility when module is inactive --- .../src/js/features/page-cache/page-cache.tsx | 2 +- projects/plugins/boost/app/lib/class-cli.php | 2 +- .../lib/pages/wp-admin/JetpackBoostPage.js | 5 ++++ .../e2e/specs/page-cache/page-cache.test.js | 23 +++++++++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js diff --git a/projects/plugins/boost/app/assets/src/js/features/page-cache/page-cache.tsx b/projects/plugins/boost/app/assets/src/js/features/page-cache/page-cache.tsx index 93c064f7b9159..3062021653adc 100644 --- a/projects/plugins/boost/app/assets/src/js/features/page-cache/page-cache.tsx +++ b/projects/plugins/boost/app/assets/src/js/features/page-cache/page-cache.tsx @@ -89,7 +89,7 @@ const Meta = () => { return ( pageCache && ( -
+
{ getSummary() }
diff --git a/projects/plugins/boost/app/lib/class-cli.php b/projects/plugins/boost/app/lib/class-cli.php index 720c325398f02..e5b4f396e64d4 100644 --- a/projects/plugins/boost/app/lib/class-cli.php +++ b/projects/plugins/boost/app/lib/class-cli.php @@ -24,7 +24,7 @@ class CLI { */ private $jetpack_boost; - const MAKE_E2E_TESTS_WORK_MODULES = array( 'critical_css', 'render_blocking_js' ); + const MAKE_E2E_TESTS_WORK_MODULES = array( 'critical_css', 'render_blocking_js', 'page_cache' ); /** * CLI constructor. diff --git a/projects/plugins/boost/tests/e2e/lib/pages/wp-admin/JetpackBoostPage.js b/projects/plugins/boost/tests/e2e/lib/pages/wp-admin/JetpackBoostPage.js index 03deb3722394c..70b00ee6ede3a 100644 --- a/projects/plugins/boost/tests/e2e/lib/pages/wp-admin/JetpackBoostPage.js +++ b/projects/plugins/boost/tests/e2e/lib/pages/wp-admin/JetpackBoostPage.js @@ -126,6 +126,11 @@ export default class JetpackBoostPage extends WpPage { return this.waitForElementToBeVisible( selector ); } + async isThePageCacheMetaInformationVisible() { + const selector = '[data-testid="page-cache-meta"]'; + return this.page.isVisible( selector ); + } + async navigateToMainSettingsPage() { await this.page.click( 'text=Go back' ); } diff --git a/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js b/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js new file mode 100644 index 0000000000000..5b41ce292c938 --- /dev/null +++ b/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js @@ -0,0 +1,23 @@ +import { test, expect } from 'jetpack-e2e-commons/fixtures/base-test.js'; +import { boostPrerequisitesBuilder } from '../../lib/env/prerequisites.js'; +import { JetpackBoostPage } from '../../lib/pages/index.js'; +import playwrightConfig from 'jetpack-e2e-commons/playwright.config.mjs'; + +test.describe( 'Cache module', () => { + let page; + + test.beforeAll( async ( { browser } ) => { + page = await browser.newPage( playwrightConfig.use ); + await boostPrerequisitesBuilder( page ).withCleanEnv( true ).withConnection( true ).build(); + } ); + + test( 'No Page Cache meta information should show on the admin when the module is inactive', async () => { + await boostPrerequisitesBuilder( page ).withInactiveModules( [ 'page_cache' ] ).build(); + + const jetpackBoostPage = await JetpackBoostPage.visit( page ); + expect( + await jetpackBoostPage.isThePageCacheMetaInformationVisible(), + 'Page Cache meta information should not be visible' + ).toBeFalsy(); + } ); +} ); From e73627424d5b19d2effc0767a6a214a46790d6f3 Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Thu, 21 Mar 2024 12:55:18 +0200 Subject: [PATCH 02/16] Add test for Page Cache header when module is inactive --- .../tests/e2e/specs/page-cache/page-cache.test.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js b/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js index 5b41ce292c938..59b7e6d39f3ef 100644 --- a/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js +++ b/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js @@ -1,6 +1,7 @@ import { test, expect } from 'jetpack-e2e-commons/fixtures/base-test.js'; import { boostPrerequisitesBuilder } from '../../lib/env/prerequisites.js'; import { JetpackBoostPage } from '../../lib/pages/index.js'; +import { PostFrontendPage } from 'jetpack-e2e-commons/pages/index.js'; import playwrightConfig from 'jetpack-e2e-commons/playwright.config.mjs'; test.describe( 'Cache module', () => { @@ -20,4 +21,17 @@ test.describe( 'Cache module', () => { 'Page Cache meta information should not be visible' ).toBeFalsy(); } ); + + // Make sure there's no cache header when module is disabled. + test( 'Page Cache should not be present when Page Cache module is inactive', async () => { + await boostPrerequisitesBuilder( page ).withInactiveModules( [ 'page_cache' ] ).build(); + await PostFrontendPage.visit( page ); + + page.on( 'response', response => { + expect( + response.headers().hasOwnProperty( 'X-Jetpack-Boost-Cache' ), + 'Page Cache header should not be present' + ).toBeFalsy(); + } ); + } ); } ); From 9e0da04cc0e2d16fa843e67743010e7ff8914b3d Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Thu, 21 Mar 2024 14:15:32 +0200 Subject: [PATCH 03/16] Add test for Page cache meta visibility when module is active --- .../boost/tests/e2e/lib/pages/index.js | 1 + .../lib/pages/wp-admin/JetpackBoostPage.js | 5 +++ .../e2e/lib/pages/wp-admin/PermalinksPage.js | 16 ++++++++++ .../e2e/specs/page-cache/page-cache.test.js | 31 +++++++++++++++++-- 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 projects/plugins/boost/tests/e2e/lib/pages/wp-admin/PermalinksPage.js diff --git a/projects/plugins/boost/tests/e2e/lib/pages/index.js b/projects/plugins/boost/tests/e2e/lib/pages/index.js index 589048da796ce..3775f97ad647b 100644 --- a/projects/plugins/boost/tests/e2e/lib/pages/index.js +++ b/projects/plugins/boost/tests/e2e/lib/pages/index.js @@ -1 +1,2 @@ export { default as JetpackBoostPage } from './wp-admin/JetpackBoostPage.js'; +export { default as PermalinksPage } from './wp-admin/PermalinksPage.js'; diff --git a/projects/plugins/boost/tests/e2e/lib/pages/wp-admin/JetpackBoostPage.js b/projects/plugins/boost/tests/e2e/lib/pages/wp-admin/JetpackBoostPage.js index 70b00ee6ede3a..ed348199f0907 100644 --- a/projects/plugins/boost/tests/e2e/lib/pages/wp-admin/JetpackBoostPage.js +++ b/projects/plugins/boost/tests/e2e/lib/pages/wp-admin/JetpackBoostPage.js @@ -131,6 +131,11 @@ export default class JetpackBoostPage extends WpPage { return this.page.isVisible( selector ); } + async waitForPageCacheMetaInfoVisibility() { + const selector = '[data-testid="page-cache-meta"]'; + return this.waitForElementToBeVisible( selector, 3 * 60 * 1000 ); + } + async navigateToMainSettingsPage() { await this.page.click( 'text=Go back' ); } diff --git a/projects/plugins/boost/tests/e2e/lib/pages/wp-admin/PermalinksPage.js b/projects/plugins/boost/tests/e2e/lib/pages/wp-admin/PermalinksPage.js new file mode 100644 index 0000000000000..84684fa167f7d --- /dev/null +++ b/projects/plugins/boost/tests/e2e/lib/pages/wp-admin/PermalinksPage.js @@ -0,0 +1,16 @@ +import WpPage from 'jetpack-e2e-commons/pages/wp-page.js'; +import { resolveSiteUrl } from 'jetpack-e2e-commons/helpers/utils-helper.js'; + +export default class PermalinksPage extends WpPage { + constructor( page ) { + const url = `${ resolveSiteUrl() }/wp-admin/options-permalink.php`; + super( page, { expectedSelectors: [ '.permalink-structure' ], url } ); + } + + async useDayNameStructure() { + const selector = '[id="permalink-input-day-name"]'; + await this.page.click( selector ); + await this.page.click( '[id="submit"]' ); + await this.waitForLoad(); + } +} diff --git a/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js b/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js index 59b7e6d39f3ef..c69a8bf6ab708 100644 --- a/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js +++ b/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js @@ -1,6 +1,6 @@ import { test, expect } from 'jetpack-e2e-commons/fixtures/base-test.js'; import { boostPrerequisitesBuilder } from '../../lib/env/prerequisites.js'; -import { JetpackBoostPage } from '../../lib/pages/index.js'; +import { JetpackBoostPage, PermalinksPage } from '../../lib/pages/index.js'; import { PostFrontendPage } from 'jetpack-e2e-commons/pages/index.js'; import playwrightConfig from 'jetpack-e2e-commons/playwright.config.mjs'; @@ -9,7 +9,23 @@ test.describe( 'Cache module', () => { test.beforeAll( async ( { browser } ) => { page = await browser.newPage( playwrightConfig.use ); - await boostPrerequisitesBuilder( page ).withCleanEnv( true ).withConnection( true ).build(); + await boostPrerequisitesBuilder( page ) + .withInactiveModules( [ + 'page_cache', // Make sure it's inactive. + ] ) + .withCleanEnv() + .withConnection( true ) + .build(); + + // Page Cache needs a pretty permalink structure to work properly. + const permalinksPage = await PermalinksPage.visit( page ); + await permalinksPage.useDayNameStructure(); + } ); + + test.afterAll( async () => { + // Reset the environment for any other tests. + await boostPrerequisitesBuilder( page ).withCleanEnv().withConnection( true ).build(); + await page.close(); } ); test( 'No Page Cache meta information should show on the admin when the module is inactive', async () => { @@ -34,4 +50,15 @@ test.describe( 'Cache module', () => { ).toBeFalsy(); } ); } ); + + // Make sure Page Cache meta is visible when module is active. + test( 'Page Cache meta information should show on the admin when the module is active', async () => { + await boostPrerequisitesBuilder( page ).withActiveModules( [ 'page_cache' ] ).build(); + + const jetpackBoostPage = await JetpackBoostPage.visit( page ); + expect( + await jetpackBoostPage.waitForPageCacheMetaInfoVisibility(), + 'Page Cache meta information should be visible' + ).toBeTruthy(); + } ); } ); From 5f3567ccd0c3944cfe4c0c959811278c2c835a38 Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Thu, 21 Mar 2024 17:45:24 +0200 Subject: [PATCH 04/16] Add permalink tests for page cache --- .../assets/src/js/features/module/module.tsx | 2 +- .../lib/pages/wp-admin/JetpackBoostPage.js | 5 +++ .../e2e/lib/pages/wp-admin/PermalinksPage.js | 7 ++++ .../e2e/specs/page-cache/page-cache.test.js | 42 ++++++++++++++++++- 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/projects/plugins/boost/app/assets/src/js/features/module/module.tsx b/projects/plugins/boost/app/assets/src/js/features/module/module.tsx index 71d5c97434124..eaf40d209dde2 100644 --- a/projects/plugins/boost/app/assets/src/js/features/module/module.tsx +++ b/projects/plugins/boost/app/assets/src/js/features/module/module.tsx @@ -58,7 +58,7 @@ const Module = ( { } return ( -
+
{ toggle && ( { } ); // Make sure there's no cache header when module is disabled. - test( 'Page Cache should not be present when Page Cache module is inactive', async () => { + test( 'Page Cache header should not be present when module is inactive', async () => { await boostPrerequisitesBuilder( page ).withInactiveModules( [ 'page_cache' ] ).build(); await PostFrontendPage.visit( page ); @@ -61,4 +61,44 @@ test.describe( 'Cache module', () => { 'Page Cache meta information should be visible' ).toBeTruthy(); } ); + + test( 'Page Cache should show error notice when plain permalinks are enabled', async () => { + await boostPrerequisitesBuilder( page ).withInactiveModules( [ 'page_cache' ] ).build(); + + const permalinksPage = await PermalinksPage.visit( page ); + await permalinksPage.usePlainStructure(); + + await boostPrerequisitesBuilder( page ).withActiveModules( [ 'page_cache' ] ).build(); + + const jetpackBoostPage = await JetpackBoostPage.visit( page ); + expect( + await jetpackBoostPage.waitForPageCachePermalinksErrorVisibility(), + 'Page Cache should show permalink error message when using plain permalink structure' + ).toBeTruthy(); + } ); + + // Make sure there's a cache header when module is enabled. + // test ( 'Page Cache header should be present when module is active', async () => { + // await boostPrerequisitesBuilder( page ).withActiveModules( [ 'page_cache' ] ).build(); + // const postFrontendPage = await PostFrontendPage.visit( page ); + // console.log('postFrontendPage - ' + postFrontendPage.url); + // // need a logged out browser context to test the cache header + // await postFrontendPage.logout(); + + // page.on( 'response', response => { + // // Not sure why there's a trailing slash, but it's messing up the test. + // if ( response.url().replace(/\/$/, '') !== postFrontendPage.url ) { + // return; + // } + + // console.log(response.headers()); + + // expect( + // response.headers().hasOwnProperty( 'X-Jetpack-Boost-Cache' ), + // 'Page Cache header should be present' + // ).toBeTruthy(); + // } ); + + // await PostFrontendPage.visit( page ); + // } ); } ); From a46764faafc5d99e1da9c4f79fa3839120d04b9d Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Thu, 21 Mar 2024 17:46:15 +0200 Subject: [PATCH 05/16] add changelog --- projects/plugins/boost/changelog/add-more-e2e-tests | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 projects/plugins/boost/changelog/add-more-e2e-tests diff --git a/projects/plugins/boost/changelog/add-more-e2e-tests b/projects/plugins/boost/changelog/add-more-e2e-tests new file mode 100644 index 0000000000000..3362e8b82349d --- /dev/null +++ b/projects/plugins/boost/changelog/add-more-e2e-tests @@ -0,0 +1,4 @@ +Significance: patch +Type: added + +Add end to end tests for modules. From bb3fdd9dd1e9c97f90a98fc3c8ad71f884ce21af Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Tue, 26 Mar 2024 15:06:41 +0200 Subject: [PATCH 06/16] Add cache header test --- .../e2e/specs/page-cache/page-cache.test.js | 45 +++++++++---------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js b/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js index e05f9b79082e0..aa030929a23a1 100644 --- a/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js +++ b/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js @@ -62,15 +62,15 @@ test.describe( 'Cache module', () => { ).toBeTruthy(); } ); + // Make sure there's an error message when trying to enable Page Cache with plain permalinks. test( 'Page Cache should show error notice when plain permalinks are enabled', async () => { await boostPrerequisitesBuilder( page ).withInactiveModules( [ 'page_cache' ] ).build(); const permalinksPage = await PermalinksPage.visit( page ); await permalinksPage.usePlainStructure(); - await boostPrerequisitesBuilder( page ).withActiveModules( [ 'page_cache' ] ).build(); - const jetpackBoostPage = await JetpackBoostPage.visit( page ); + await jetpackBoostPage.toggleModule( 'page_cache' ); expect( await jetpackBoostPage.waitForPageCachePermalinksErrorVisibility(), 'Page Cache should show permalink error message when using plain permalink structure' @@ -78,27 +78,22 @@ test.describe( 'Cache module', () => { } ); // Make sure there's a cache header when module is enabled. - // test ( 'Page Cache header should be present when module is active', async () => { - // await boostPrerequisitesBuilder( page ).withActiveModules( [ 'page_cache' ] ).build(); - // const postFrontendPage = await PostFrontendPage.visit( page ); - // console.log('postFrontendPage - ' + postFrontendPage.url); - // // need a logged out browser context to test the cache header - // await postFrontendPage.logout(); - - // page.on( 'response', response => { - // // Not sure why there's a trailing slash, but it's messing up the test. - // if ( response.url().replace(/\/$/, '') !== postFrontendPage.url ) { - // return; - // } - - // console.log(response.headers()); - - // expect( - // response.headers().hasOwnProperty( 'X-Jetpack-Boost-Cache' ), - // 'Page Cache header should be present' - // ).toBeTruthy(); - // } ); - - // await PostFrontendPage.visit( page ); - // } ); + test( 'Page Cache header should be present when module is active', async () => { + await boostPrerequisitesBuilder( page ).withActiveModules( [ 'page_cache' ] ).build(); + const postFrontendPage = await PostFrontendPage.visit( page ); + // Cache is only available to logged out users. + await postFrontendPage.logout(); + + page.on( 'response', response => { + // Not sure why there's a trailing slash, but it's messing up the test. + if ( response.url().replace( /\/$/, '' ) !== postFrontendPage.url ) { + return; + } + + expect( + response.headers().hasOwnProperty( 'X-Jetpack-Boost-Cache'.toLowerCase() ), + 'Page Cache header should be present' + ).toBeTruthy(); + } ); + } ); } ); From ad2a73759cc5ea404887fd85f57f6dd42e135457 Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Tue, 26 Mar 2024 17:08:24 +0200 Subject: [PATCH 07/16] Update boost CLI to support page cache --- projects/plugins/boost/app/lib/class-cli.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/projects/plugins/boost/app/lib/class-cli.php b/projects/plugins/boost/app/lib/class-cli.php index e5b4f396e64d4..1221a6be712f2 100644 --- a/projects/plugins/boost/app/lib/class-cli.php +++ b/projects/plugins/boost/app/lib/class-cli.php @@ -11,6 +11,9 @@ use Automattic\Jetpack_Boost\Data_Sync\Getting_Started_Entry; use Automattic\Jetpack_Boost\Jetpack_Boost; +use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Garbage_Collection; +use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Page_Cache_Setup; +use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Pre_WordPress\Boost_Cache_Settings; /** * Control your local Jetpack Boost installation. @@ -113,6 +116,22 @@ public function getting_started( $args ) { private function set_module_status( $module_slug, $status ) { ( new Status( $module_slug ) )->update( $status ); + if ( $module_slug === 'page_cache' && $status ) { + $setup_result = Page_Cache_Setup::run_setup(); + if ( is_wp_error( $setup_result ) ) { + \WP_CLI::error( + sprintf( + /* translators: %s refers to the error code */ + __( 'Setup: %s', 'jetpack-boost' ), + $setup_result->get_error_code() + ) + ); + } + + Garbage_Collection::activate(); + Boost_Cache_Settings::get_instance()->set( array( 'enabled' => true ) ); + } + $status_label = $status ? __( 'activated', 'jetpack-boost' ) : __( 'deactivated', 'jetpack-boost' ); /* translators: The %1$s refers to the module slug, %2$s refers to the module state (either activated or deactivated)*/ From 1f9fd13a15771581b5ad452dd42baf83102152b1 Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Thu, 28 Mar 2024 13:02:55 +0200 Subject: [PATCH 08/16] Fix test --- .../tests/e2e/specs/page-cache/page-cache.test.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js b/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js index aa030929a23a1..1e5aec8e8fcb0 100644 --- a/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js +++ b/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js @@ -41,11 +41,17 @@ test.describe( 'Cache module', () => { // Make sure there's no cache header when module is disabled. test( 'Page Cache header should not be present when module is inactive', async () => { await boostPrerequisitesBuilder( page ).withInactiveModules( [ 'page_cache' ] ).build(); - await PostFrontendPage.visit( page ); + const postFrontendPage = await PostFrontendPage.visit( page ); + // Cache is only available to logged out users. + await postFrontendPage.logout(); page.on( 'response', response => { + if ( response.url().replace( /\/$/, '' ) !== postFrontendPage.url ) { + return; + } + expect( - response.headers().hasOwnProperty( 'X-Jetpack-Boost-Cache' ), + response.headers().hasOwnProperty( 'X-Jetpack-Boost-Cache'.toLowerCase() ), 'Page Cache header should not be present' ).toBeFalsy(); } ); From daf8ad0d8c6ee30c56ba2582a86c113998fdd651 Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Thu, 28 Mar 2024 13:27:50 +0200 Subject: [PATCH 09/16] Update GH actions to include page cache tests --- .github/files/e2e-tests/e2e-matrix.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.github/files/e2e-tests/e2e-matrix.js b/.github/files/e2e-tests/e2e-matrix.js index 2f9ef1cd249fb..73ff717ce4578 100644 --- a/.github/files/e2e-tests/e2e-matrix.js +++ b/.github/files/e2e-tests/e2e-matrix.js @@ -58,6 +58,14 @@ const projects = [ suite: '', buildGroup: 'jetpack-boost', }, + { + project: 'Jetpack Boost - Page Cache', + path: 'projects/plugins/boost/tests/e2e', + testArgs: [ 'specs/page-cache', '--retries=1' ], + targets: [ 'plugins/boost' ], + suite: '', + buildGroup: 'jetpack-boost', + }, { project: 'Search', path: 'projects/plugins/search/tests/e2e', From 185e4578445cdbfc231aad56cec07a6e31b7d27e Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Fri, 29 Mar 2024 12:36:37 +0200 Subject: [PATCH 10/16] Fix page cache tests --- .../e2e/specs/page-cache/page-cache.test.js | 76 ++++++++++++------- 1 file changed, 48 insertions(+), 28 deletions(-) diff --git a/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js b/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js index 1e5aec8e8fcb0..a79d85b0ba942 100644 --- a/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js +++ b/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js @@ -2,7 +2,9 @@ import { test, expect } from 'jetpack-e2e-commons/fixtures/base-test.js'; import { boostPrerequisitesBuilder } from '../../lib/env/prerequisites.js'; import { JetpackBoostPage, PermalinksPage } from '../../lib/pages/index.js'; import { PostFrontendPage } from 'jetpack-e2e-commons/pages/index.js'; +import { WPLoginPage } from 'jetpack-e2e-commons/pages/wp-admin/index.js'; import playwrightConfig from 'jetpack-e2e-commons/playwright.config.mjs'; +import { resolveSiteUrl } from 'jetpack-e2e-commons/helpers/utils-helper.js'; test.describe( 'Cache module', () => { let page; @@ -22,6 +24,12 @@ test.describe( 'Cache module', () => { await permalinksPage.useDayNameStructure(); } ); + // Disabling the module before each test, because each test will decide if + // it needs the module enabled or not. + test.beforeEach( async () => { + await boostPrerequisitesBuilder( page ).withInactiveModules( [ 'page_cache' ] ).build(); + } ); + test.afterAll( async () => { // Reset the environment for any other tests. await boostPrerequisitesBuilder( page ).withCleanEnv().withConnection( true ).build(); @@ -29,8 +37,6 @@ test.describe( 'Cache module', () => { } ); test( 'No Page Cache meta information should show on the admin when the module is inactive', async () => { - await boostPrerequisitesBuilder( page ).withInactiveModules( [ 'page_cache' ] ).build(); - const jetpackBoostPage = await JetpackBoostPage.visit( page ); expect( await jetpackBoostPage.isThePageCacheMetaInformationVisible(), @@ -39,14 +45,15 @@ test.describe( 'Cache module', () => { } ); // Make sure there's no cache header when module is disabled. - test( 'Page Cache header should not be present when module is inactive', async () => { - await boostPrerequisitesBuilder( page ).withInactiveModules( [ 'page_cache' ] ).build(); - const postFrontendPage = await PostFrontendPage.visit( page ); - // Cache is only available to logged out users. - await postFrontendPage.logout(); - - page.on( 'response', response => { - if ( response.url().replace( /\/$/, '' ) !== postFrontendPage.url ) { + test( 'Page Cache header should not be present when module is inactive', async ( { + browser, + } ) => { + const newPage = await browser.newPage( playwrightConfig.use ); + const postFrontPage = await PostFrontendPage.visit( newPage ); + await postFrontPage.logout(); + + newPage.on( 'response', response => { + if ( response.url().replace( /\/$/, '' ) !== resolveSiteUrl().replace( /\/$/, '' ) ) { return; } @@ -55,22 +62,16 @@ test.describe( 'Cache module', () => { 'Page Cache header should not be present' ).toBeFalsy(); } ); - } ); - // Make sure Page Cache meta is visible when module is active. - test( 'Page Cache meta information should show on the admin when the module is active', async () => { - await boostPrerequisitesBuilder( page ).withActiveModules( [ 'page_cache' ] ).build(); + await PostFrontendPage.visit( newPage ); - const jetpackBoostPage = await JetpackBoostPage.visit( page ); - expect( - await jetpackBoostPage.waitForPageCacheMetaInfoVisibility(), - 'Page Cache meta information should be visible' - ).toBeTruthy(); + await newPage.close(); } ); // Make sure there's an error message when trying to enable Page Cache with plain permalinks. - test( 'Page Cache should show error notice when plain permalinks are enabled', async () => { - await boostPrerequisitesBuilder( page ).withInactiveModules( [ 'page_cache' ] ).build(); + test( 'Enabling Page Cache should show error notice when plain permalinks are enabled', async () => { + const loginPage = await WPLoginPage.visit( page ); + await loginPage.login(); const permalinksPage = await PermalinksPage.visit( page ); await permalinksPage.usePlainStructure(); @@ -83,16 +84,31 @@ test.describe( 'Cache module', () => { ).toBeTruthy(); } ); + // Make sure Page Cache meta is visible when module is active. + test( 'Page Cache meta information should show on the admin when the module is active', async () => { + const permalinksPage = await PermalinksPage.visit( page ); + await permalinksPage.useDayNameStructure(); + + // Activate the module. + const jetpackBoostPage = await JetpackBoostPage.visit( page ); + await jetpackBoostPage.toggleModule( 'page_cache' ); + + expect( + await jetpackBoostPage.waitForPageCacheMetaInfoVisibility(), + 'Page Cache meta information should be visible' + ).toBeTruthy(); + } ); + // Make sure there's a cache header when module is enabled. - test( 'Page Cache header should be present when module is active', async () => { + test( 'Page Cache header should be present when module is active', async ( { browser } ) => { await boostPrerequisitesBuilder( page ).withActiveModules( [ 'page_cache' ] ).build(); - const postFrontendPage = await PostFrontendPage.visit( page ); - // Cache is only available to logged out users. - await postFrontendPage.logout(); - page.on( 'response', response => { - // Not sure why there's a trailing slash, but it's messing up the test. - if ( response.url().replace( /\/$/, '' ) !== postFrontendPage.url ) { + const newPage = await browser.newPage( playwrightConfig.use ); + const postFrontPage = await PostFrontendPage.visit( newPage ); + await postFrontPage.logout(); + + newPage.on( 'response', response => { + if ( response.url().replace( /\/$/, '' ) !== resolveSiteUrl().replace( /\/$/, '' ) ) { return; } @@ -101,5 +117,9 @@ test.describe( 'Cache module', () => { 'Page Cache header should be present' ).toBeTruthy(); } ); + + await PostFrontendPage.visit( newPage ); + + await newPage.close(); } ); } ); From 7decab394d9022f3b04d17bfa222b2970532c1c6 Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Fri, 29 Mar 2024 14:41:42 +0200 Subject: [PATCH 11/16] Update file permissions in docker to be writable --- tools/docker/bin/run.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tools/docker/bin/run.sh b/tools/docker/bin/run.sh index 1d02ce843f06f..8038d263e2ea9 100755 --- a/tools/docker/bin/run.sh +++ b/tools/docker/bin/run.sh @@ -15,6 +15,9 @@ group="${APACHE_RUN_GROUP:-www-data}" # Download WordPress [ -f /var/www/html/xmlrpc.php ] || wp --allow-root core download +# Set writable permissions for the wp-content directory +chmod -R 755 /var/www/html/wp-content + # Configure WordPress if [ ! -f /var/www/html/wp-config.php ]; then echo "Creating wp-config.php ..." @@ -61,11 +64,16 @@ if [ ! -f /var/www/html/wp-config.php ]; then wp --allow-root config set JETPACK_DOCKER_ENV true --raw --type=constant fi +chmod 644 /var/www/html/wp-config.php + # Copy single site htaccess if none is present if [ ! -f /var/www/html/.htaccess ]; then cp /var/lib/jetpack-config/htaccess /var/www/html/.htaccess fi +# Set writable permissions for the .htaccess file +chmod 644 /var/www/html/.htaccess + # Clean up old method of including psysh (used from 2019 until 2021) if [[ -e /var/www/html/wp-cli.yml ]] && grep -q '^require: /usr/local/bin/psysh$' /var/www/html/wp-cli.yml; then TMP="$(grep -v '^require: /usr/local/bin/psysh$' /var/www/html/wp-cli.yml || true)" From d5f4227472596d6c6776b39701e4a30da97e0dcb Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Tue, 2 Apr 2024 18:54:09 +0300 Subject: [PATCH 12/16] Add pnpm script to make files used by page cache writable --- projects/plugins/boost/tests/e2e/package.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/projects/plugins/boost/tests/e2e/package.json b/projects/plugins/boost/tests/e2e/package.json index deff81a35bce4..79b6f29d4f572 100644 --- a/projects/plugins/boost/tests/e2e/package.json +++ b/projects/plugins/boost/tests/e2e/package.json @@ -18,14 +18,15 @@ "clean": "rm -rf output", "config:decrypt": "openssl enc -md sha1 -aes-256-cbc -d -pass env:CONFIG_KEY -in ./node_modules/jetpack-e2e-commons/config/encrypted.enc -out ./config/local.cjs", "distclean": "rm -rf node_modules", - "env:up": "e2e-env start --activate-plugins boost", + "env:up": "e2e-env start --activate-plugins boost && npm run prepare:e2e", "env:down": "e2e-env stop", "env:reset": "e2e-env reset --activate-plugins boost", "tunnel:up": "tunnel up", "tunnel:reset": "tunnel reset", "tunnel:down": "tunnel down", "pretest:run": "pnpm run clean", - "test:run": ". ./node_modules/jetpack-e2e-commons/bin/app-password.sh && playwright install && NODE_CONFIG_DIR='./config' ALLURE_RESULTS_DIR=./output/allure-results NODE_PATH=\"$PWD/node_modules\" playwright test --config=./playwright.config.mjs" + "test:run": ". ./node_modules/jetpack-e2e-commons/bin/app-password.sh && playwright install && NODE_CONFIG_DIR='./config' ALLURE_RESULTS_DIR=./output/allure-results NODE_PATH=\"$PWD/node_modules\" playwright test --config=./playwright.config.mjs", + "prepare:e2e": "pnpm jetpack docker --type e2e --name t1 exec -- chown -R www-data .htaccess wp-config.php wp-content" }, "devDependencies": { "@playwright/test": "1.39.0", From d107e6003c04942ae46ecdd010ebd45644c3380a Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Tue, 2 Apr 2024 18:55:06 +0300 Subject: [PATCH 13/16] Revert permission changes --- tools/docker/bin/run.sh | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tools/docker/bin/run.sh b/tools/docker/bin/run.sh index 8038d263e2ea9..1d02ce843f06f 100755 --- a/tools/docker/bin/run.sh +++ b/tools/docker/bin/run.sh @@ -15,9 +15,6 @@ group="${APACHE_RUN_GROUP:-www-data}" # Download WordPress [ -f /var/www/html/xmlrpc.php ] || wp --allow-root core download -# Set writable permissions for the wp-content directory -chmod -R 755 /var/www/html/wp-content - # Configure WordPress if [ ! -f /var/www/html/wp-config.php ]; then echo "Creating wp-config.php ..." @@ -64,16 +61,11 @@ if [ ! -f /var/www/html/wp-config.php ]; then wp --allow-root config set JETPACK_DOCKER_ENV true --raw --type=constant fi -chmod 644 /var/www/html/wp-config.php - # Copy single site htaccess if none is present if [ ! -f /var/www/html/.htaccess ]; then cp /var/lib/jetpack-config/htaccess /var/www/html/.htaccess fi -# Set writable permissions for the .htaccess file -chmod 644 /var/www/html/.htaccess - # Clean up old method of including psysh (used from 2019 until 2021) if [[ -e /var/www/html/wp-cli.yml ]] && grep -q '^require: /usr/local/bin/psysh$' /var/www/html/wp-cli.yml; then TMP="$(grep -v '^require: /usr/local/bin/psysh$' /var/www/html/wp-cli.yml || true)" From f2c7dc23aef73003dfac1a093dbfdd44469ea02b Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Tue, 2 Apr 2024 18:55:45 +0300 Subject: [PATCH 14/16] Fix prepare script --- projects/plugins/boost/tests/e2e/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/plugins/boost/tests/e2e/package.json b/projects/plugins/boost/tests/e2e/package.json index 79b6f29d4f572..3e7365b15b8ae 100644 --- a/projects/plugins/boost/tests/e2e/package.json +++ b/projects/plugins/boost/tests/e2e/package.json @@ -18,7 +18,7 @@ "clean": "rm -rf output", "config:decrypt": "openssl enc -md sha1 -aes-256-cbc -d -pass env:CONFIG_KEY -in ./node_modules/jetpack-e2e-commons/config/encrypted.enc -out ./config/local.cjs", "distclean": "rm -rf node_modules", - "env:up": "e2e-env start --activate-plugins boost && npm run prepare:e2e", + "env:up": "e2e-env start --activate-plugins boost && pnpm run prepare:e2e", "env:down": "e2e-env stop", "env:reset": "e2e-env reset --activate-plugins boost", "tunnel:up": "tunnel up", From 12f40c24e958efbf2f3548063b24e1e2afb2d99b Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Tue, 2 Apr 2024 19:25:46 +0300 Subject: [PATCH 15/16] Update script --- projects/plugins/boost/tests/e2e/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/plugins/boost/tests/e2e/package.json b/projects/plugins/boost/tests/e2e/package.json index 3e7365b15b8ae..f61ad66e688bf 100644 --- a/projects/plugins/boost/tests/e2e/package.json +++ b/projects/plugins/boost/tests/e2e/package.json @@ -26,7 +26,7 @@ "tunnel:down": "tunnel down", "pretest:run": "pnpm run clean", "test:run": ". ./node_modules/jetpack-e2e-commons/bin/app-password.sh && playwright install && NODE_CONFIG_DIR='./config' ALLURE_RESULTS_DIR=./output/allure-results NODE_PATH=\"$PWD/node_modules\" playwright test --config=./playwright.config.mjs", - "prepare:e2e": "pnpm jetpack docker --type e2e --name t1 exec -- chown -R www-data .htaccess wp-config.php wp-content" + "prepare:e2e": "pnpm jetpack docker --type e2e --name t1 exec-silent -- chown -R www-data .htaccess wp-config.php wp-content" }, "devDependencies": { "@playwright/test": "1.39.0", From 2717e85d3958d4a4eede5887052934f6898e3891 Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Wed, 3 Apr 2024 17:15:44 +0300 Subject: [PATCH 16/16] Update page cache test to check header value as well --- .../e2e/specs/page-cache/page-cache.test.js | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js b/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js index a79d85b0ba942..c11a43f319288 100644 --- a/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js +++ b/projects/plugins/boost/tests/e2e/specs/page-cache/page-cache.test.js @@ -107,19 +107,39 @@ test.describe( 'Cache module', () => { const postFrontPage = await PostFrontendPage.visit( newPage ); await postFrontPage.logout(); + let totalVisits = 0; + newPage.on( 'response', response => { if ( response.url().replace( /\/$/, '' ) !== resolveSiteUrl().replace( /\/$/, '' ) ) { return; } - expect( - response.headers().hasOwnProperty( 'X-Jetpack-Boost-Cache'.toLowerCase() ), - 'Page Cache header should be present' - ).toBeTruthy(); + totalVisits++; + + const responseHeaders = response.headers(); + const cacheHeaderName = 'X-Jetpack-Boost-Cache'.toLowerCase(); + + // First visit should always be a miss. + if ( totalVisits === 1 ) { + expect( + responseHeaders.hasOwnProperty( cacheHeaderName ) && + responseHeaders[ cacheHeaderName ] === 'miss', + 'Page Cache header should be set to miss on first visit.' + ).toBeTruthy(); + } else { + expect( + responseHeaders.hasOwnProperty( cacheHeaderName ) && + responseHeaders[ cacheHeaderName ] === 'hit', + 'Page Cache header should be set to hit on second visit.' + ).toBeTruthy(); + } } ); await PostFrontendPage.visit( newPage ); + // Visit again to make sure the cache is hit. + await PostFrontendPage.visit( newPage ); + await newPage.close(); } ); } );