Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Enable blockified templates (#9551)
Browse files Browse the repository at this point in the history
* Enable blockified templates

* add html template

* fix function

* WIP

* Migrate Classic Block E2E tests to Playwright (#9575)

* Add E2E tests

* add comment

* set worker to 1

* try now

* add beforeAll and afterAll hook

* restore woocommerce-gutenberg-products-block.php

* enable plugin with .wp-env configuration

* remove waitForNetworkIdle

* Products block: Add e2e tests (#9577)

* migrate classic block to Playwright

* fix can be inserted more than once tests

* migrate classic block to Playwright

* fix command

* remove old tests

* improve E2E tests

* skip test

* Products block: add E2E pw tests

* Add E2E tests

* fix E2E test

* test now

* try now

* rename path

* set one worker

* try now

* try now

* try now

* set 1 worker
  • Loading branch information
gigitux authored Jun 1, 2023
1 parent 8295737 commit f85b97c
Show file tree
Hide file tree
Showing 31 changed files with 478 additions and 1,200 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ jobs:
- uses: actions/upload-artifact@v3
with:
name: playwright-report
path: ./tests/e2e-pw/artifacts/test-results
path: playwright-report
3 changes: 2 additions & 1 deletion .wp-env.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"https://downloads.wordpress.org/plugin/woocommerce.latest-stable.zip",
"https://github.com/WP-API/Basic-Auth/archive/master.zip",
"https://downloads.wordpress.org/plugin/wordpress-importer.0.8.zip",
"./tests/mocks/woo-test-helper"
"./tests/mocks/woo-test-helper",
"."
],
"env": {
"tests": {
Expand Down
9 changes: 6 additions & 3 deletions docs/contributors/e2e-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ The first folder is named "e2e" and it contains all the E2E tests that were crea

#### Structure

There are two Playwright projects configuration:
There are three Playwright projects configuration:

- blockTheme
- classicTheme
- blockTheme
- blockThemeWithGlobalSideEffects
- classicTheme

The blockTheme project runs the tests with the suffix _block_theme_. In this case, the theme is a block theme. The block theme is the default WordPress theme. Currently, it is Twenty-Twenty Three. You should use this configuration if you want test the block with the Site Editor.

The blockThemeWithGlobalSideEffects project runs the tests with the suffix _block_theme.side_effects_. These tests have side effects that can potentially impact other end-to-end (E2E) tests. Due to the nature of these tests and their potential impact, they are not executed in parallel with other tests.

The classicTheme project runs the tests with the suffix _classic_theme_. In this case, the theme is a Twenty Twenty-One. You should use this configuration if you want test the block with a classic theme.

Each block should have a dedicated folder with a scoped util file if you want share some logic related to the block.
Expand Down
18 changes: 5 additions & 13 deletions src/BlockTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ protected function init() {
add_filter( 'current_theme_supports-block-templates', array( $this, 'remove_block_template_support_for_shop_page' ) );
add_filter( 'taxonomy_template_hierarchy', array( $this, 'add_archive_product_to_eligible_for_fallback_templates' ), 10, 1 );
add_filter( 'post_type_archive_title', array( $this, 'update_product_archive_title' ), 10, 2 );

if ( $this->package->is_experimental_build() ) {
add_action( 'after_switch_theme', array( $this, 'check_should_use_blockified_product_grid_templates' ), 10, 2 );
}
add_action( 'after_switch_theme', array( $this, 'check_should_use_blockified_product_grid_templates' ), 10, 2 );
}

/**
Expand Down Expand Up @@ -373,11 +370,7 @@ public function get_block_templates_from_woocommerce( $slugs, $already_found_tem

foreach ( $template_files as $template_file ) {
// Skip the template if it's blockified, and we should only use classic ones.
// Until the blockified Product Grid Block is implemented, we need to always skip the blockified templates.
// phpcs:ignore Squiz.PHP.CommentedOutCode
if ( // $this->package->is_experimental_build() &&
// ! BlockTemplateUtils::should_use_blockified_product_grid_templates() &&
strpos( $template_file, 'blockified' ) !== false ) {
if ( ! BlockTemplateUtils::should_use_blockified_product_grid_templates() && strpos( $template_file, 'blockified' ) !== false ) {
continue;
}

Expand Down Expand Up @@ -466,10 +459,9 @@ protected function get_templates_directory( $template_type = 'wp_template' ) {
return $this->template_parts_directory;
}

// When the blockified Product Grid Block will be implemented, we need to use the blockified templates.
// if ( $this->package->is_experimental_build() && BlockTemplateUtils::should_use_blockified_product_grid_templates() ) {
// return $this->templates_directory . '/blockified';
// }.
if ( BlockTemplateUtils::should_use_blockified_product_grid_templates() ) {
return $this->templates_directory . '/blockified';
}

return $this->templates_directory;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected function init() {
$this->register_dependencies();
$this->register_payment_methods();

if ( $this->package->is_experimental_build() && is_admin() ) {
if ( is_admin() ) {
if ( $this->package->get_version() !== $this->package->get_version_stored_on_db() ) {
$this->migration->run_migrations();
$this->package->set_version_stored_on_db();
Expand Down
17 changes: 11 additions & 6 deletions src/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* @since 2.5.0
*/
class Migration {

/**
* DB updates and callbacks that need to be run per version.
*
Expand All @@ -17,10 +16,9 @@ class Migration {
* @var array
*/
private $db_upgrades = array(
// We don't need to do the following migration yet, but we'll keep it here for future use.
// '7.10.0' => array(
// 'wc_blocks_update_710_blockified_product_grid_block',
// ).
'10.3.0' => array(
'wc_blocks_update_1030_blockified_product_grid_block',
),
);

/**
Expand All @@ -31,7 +29,14 @@ class Migration {
public function run_migrations() {
$current_db_version = get_option( Options::WC_BLOCK_VERSION, '' );

// This check is necessary because the version was not being set in the database until 10.3.0.
// We can remove this check in the next months.
if ( ! empty( get_option( 'wc_blocks_db_schema_version', '' ) ) ) {
$this->wc_blocks_update_1030_blockified_product_grid_block();
}

if ( empty( $current_db_version ) ) {
// This is a fresh install, so we don't need to run any migrations.
return;
}

Expand All @@ -47,7 +52,7 @@ public function run_migrations() {
/**
* Set a flag to indicate if the blockified Product Grid Block should be rendered by default.
*/
public static function wc_blocks_update_710_blockified_product_grid_block() {
public static function wc_blocks_update_1030_blockified_product_grid_block() {
update_option( Options::WC_BLOCK_USE_BLOCKIFIED_PRODUCT_GRID_BLOCK_AS_TEMPLATE, wc_bool_to_string( false ) );
}
}
5 changes: 1 addition & 4 deletions templates/templates/blockified/taxonomy-product_tag.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,4 @@
<!-- /wp:query-no-results -->
</div>
<!-- /wp:query -->
</div>
<!-- /wp:group -->

<!-- wp:template-part {"slug":"footer"} /-->
</div>
10 changes: 9 additions & 1 deletion tests/e2e-pw/playwright-utils/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
RequestUtils,
} from '@wordpress/e2e-test-utils-playwright';

import { TemplateApiUtils, STORAGE_STATE_PATH } from '@woocommerce/e2e-utils';
import {
TemplateApiUtils,
STORAGE_STATE_PATH,
EditorUtils,
} from '@woocommerce/e2e-utils';

/**
* Set of console logging types observed to protect against unexpected yet
Expand Down Expand Up @@ -100,6 +104,7 @@ const test = base.extend<
editor: Editor;
pageUtils: PageUtils;
templateApiUtils: TemplateApiUtils;
editorUtils: EditorUtils;
snapshotConfig: void;
},
{
Expand Down Expand Up @@ -129,6 +134,9 @@ const test = base.extend<
},
templateApiUtils: async ( {}, use ) =>
await use( new TemplateApiUtils( request ) ),
editorUtils: async ( { editor }, use ) => {
await use( new EditorUtils( editor ) );
},
requestUtils: [
async ( {}, use, workerInfo ) => {
const requestUtils = await RequestUtils.setup( {
Expand Down
15 changes: 11 additions & 4 deletions tests/e2e-pw/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ const config: ExtendedPlaywrightTestConfig = {
globalTeardown: require.resolve( './global-teardown' ),
testDir: 'tests',
retries: CI ? 2 : 0,
workers: 4,
fullyParallel: true,
reporter: process.env.CI ? [ [ 'github' ], [ 'list' ] ] : 'list',
workers: 1,
reporter: process.env.CI
? [ [ 'github' ], [ 'list' ], [ 'html' ] ]
: 'list',
maxFailures: E2E_MAX_FAILURES ? Number( E2E_MAX_FAILURES ) : 0,
use: {
baseURL: BASE_URL,
Expand All @@ -50,10 +51,16 @@ const config: ExtendedPlaywrightTestConfig = {
testMatch: /.*.block_theme.spec.ts/,
dependencies: [ 'blockThemeConfiguration' ],
},
{
name: 'blockThemeWithGlobalSideEffects',
testMatch: /.*.block_theme.side_effects.spec.ts/,
dependencies: [ 'blockTheme' ],
fullyParallel: false,
},
{
name: 'classicThemeConfiguration',
testMatch: /block-theme.setup.ts/,
dependencies: [ 'blockTheme' ],
dependencies: [ 'blockThemeWithGlobalSideEffects' ],
},
{
name: 'classicTheme',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* External dependencies
*/
import { BlockData } from '@woocommerce/e2e-types';
import { test, expect } from '@woocommerce/e2e-playwright-utils';
import { cli } from '@woocommerce/e2e-utils';

/**
* Internal dependencies
*/

const blockData: BlockData = {
name: 'woocommerce/legacy-template',
mainClass: '.wc-block-price-filter',
selectors: {
frontend: {},
editor: {},
},
};

const templates = {
'single-product': {
templateTitle: 'Single Product',
slug: 'single-product',
frontendPage: '/product/single/',
},
'taxonomy-product_attribute': {
templateTitle: 'Product Attribute',
slug: 'taxonomy-product_attribute',
frontendPage: '/product-attribute/color/',
},

'taxonomy-product_cat': {
templateTitle: 'Product Category',
slug: 'taxonomy-product_cat',
frontendPage: '/product-category/music/',
},
// We don't have products with tags in the test site. Uncomment this when we have products with tags.
// 'taxonomy-product_tag': {
// templateTitle: 'Product Tag',
// slug: 'taxonomy-product_tag',
// frontendPage: '/product-tag/hoodie/',
// },
'archive-product': {
templateTitle: 'Product Catalog',
slug: 'archive-product',
frontendPage: '/shop/',
},
'product-search-results': {
templateTitle: 'Product Search Results',
slug: 'product-search-results',
frontendPage: '/?s=single&post_type=product',
},
};

for ( const { templateTitle, slug, frontendPage } of Object.values(
templates
) ) {
test.describe( `${ blockData.name } Block `, () => {
test.beforeAll( async () => {
await cli(
'npm run wp-env run tests-cli "wp option update wc_blocks_use_blockified_product_grid_block_as_template false"'
);
} );

test( `is rendered on ${ templateTitle } template`, async ( {
admin,
editorUtils,
editor,
} ) => {
await admin.visitSiteEditor( {
postId: `woocommerce/woocommerce//${ slug }`,
postType: 'wp_template',
} );

await editor.canvas.click( 'body' );

const block = await editorUtils.getBlockByName( blockData.name );
expect( block ).not.toBeNull();
} );

test( `is rendered on ${ templateTitle } template - frontend side`, async ( {
admin,
editor,
page,
} ) => {
await admin.visitSiteEditor( {
postId: `woocommerce/woocommerce//${ slug }`,
postType: 'wp_template',
} );

await editor.canvas.click( 'body' );

await editor.insertBlock( {
name: 'core/paragraph',
attributes: { content: 'Hello World' },
} );

await editor.saveSiteEditorEntities();

await page.goto( frontendPage );

const helloWorldText = await page.getByText( 'Hello World' );

expect( helloWorldText ).not.toBeNull();
} );

test.afterAll( async ( { requestUtils } ) => {
await cli(
'npm run wp-env run tests-cli "wp option delete wc_blocks_use_blockified_product_grid_block_as_template"'
);

await requestUtils.deleteAllTemplates( 'wp_template' );
await requestUtils.deleteAllTemplates( 'wp_template_part' );
} );
} );
}
9 changes: 9 additions & 0 deletions tests/e2e-pw/tests/classic-template/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const WOOCOMMERCE_ID = 'woocommerce/woocommerce';

export const getDefaultTemplateProps = ( templateTitle: string ) => {
return {
templateTitle,
addedBy: WOOCOMMERCE_ID,
hasActions: false,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { BlockData } from '@woocommerce/e2e-types';
import { test, expect } from '@woocommerce/e2e-playwright-utils';
import { BASE_URL, getBlockByName } from '@woocommerce/e2e-utils';
import { BASE_URL, cli, getBlockByName } from '@woocommerce/e2e-utils';

/**
* Internal dependencies
Expand Down Expand Up @@ -105,6 +105,11 @@ test.describe( `${ blockData.name } Block - with All products Block`, () => {
} );

test.describe( `${ blockData.name } Block - with PHP classic template`, () => {
test.beforeAll( async () => {
await cli(
'npm run wp-env run tests-cli "wp option update wc_blocks_use_blockified_product_grid_block_as_template false"'
);
} );
test.beforeEach( async ( { admin, page, editor } ) => {
await admin.visitSiteEditor( {
postId: 'woocommerce/woocommerce//archive-product',
Expand Down Expand Up @@ -178,4 +183,10 @@ test.describe( `${ blockData.name } Block - with PHP classic template`, () => {

expect( products ).toHaveLength( 1 );
} );

test.afterAll( async () => {
await cli(
'npm run wp-env run tests-cli "wp option delete wc_blocks_use_blockified_product_grid_block_as_template"'
);
} );
} );
Loading

0 comments on commit f85b97c

Please sign in to comment.