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

Commit

Permalink
Hook woocommerce_single_product_summary action to `core/post-excerp…
Browse files Browse the repository at this point in the history
…t` block (#11953)

* hook woocommerce_single_product_summary into core/post-excerpt block

* Add E2E tests

* improve E2E test
  • Loading branch information
gigitux authored Dec 4, 2023
1 parent c25161c commit 5a73912
Show file tree
Hide file tree
Showing 3 changed files with 244 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/Templates/SingleProductTemplateCompatibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ function( $hook ) use ( $block_name ) {
* Since that there is a custom logic for the first and last block, we have to inject the hooks manually.
* The first block supports the following hooks:
* woocommerce_before_single_product
* woocommerce_before_single_product_summary
* woocommerce_single_product_summary
*
* The last block supports the following hooks:
* woocommerce_after_single_product
Expand All @@ -69,10 +67,8 @@ function( $hook ) use ( $block_name ) {
private function inject_hook_to_first_and_last_blocks( $block_content, $block, $block_hooks ) {
$first_block_hook = array(
'before' => array(
'woocommerce_before_main_content' => $this->hook_data['woocommerce_before_main_content'],
'woocommerce_before_single_product' => $this->hook_data['woocommerce_before_single_product'],
'woocommerce_before_single_product_summary' => $this->hook_data['woocommerce_before_single_product_summary'],
'woocommerce_single_product_summary' => $this->hook_data['woocommerce_single_product_summary'],
'woocommerce_before_main_content' => $this->hook_data['woocommerce_before_main_content'],
'woocommerce_before_single_product' => $this->hook_data['woocommerce_before_single_product'],
),
'after' => array(),
);
Expand Down Expand Up @@ -199,15 +195,15 @@ protected function set_hook_data() {
),
),
'woocommerce_before_single_product_summary' => array(
'block_names' => array(),
'block_names' => array( 'core/post-excerpt' ),
'position' => 'before',
'hooked' => array(
'woocommerce_show_product_sale_flash' => 10,
'woocommerce_show_product_images' => 20,
),
),
'woocommerce_single_product_summary' => array(
'block_names' => array(),
'block_names' => array( 'core/post-excerpt' ),
'position' => 'before',
'hooked' => array(
'woocommerce_template_single_title' => 5,
Expand Down
115 changes: 115 additions & 0 deletions tests/e2e/tests/single-product-template/compatibility-plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* Plugin Name: Compatibility Layer Plugin
* Description: Adds custom content to the Shop page with Product Collection included
* @package WordPress
*/

add_action(
'woocommerce_before_main_content',
function () {
echo '<p data-testid="woocommerce_before_main_content">
Hook: woocommerce_before_main_content
</p>';
}
);

add_action(
'woocommerce_sidebar',
function () {
echo '<p data-testid="woocommerce_sidebar">
Hook: woocommerce_sidebar
</p>';
}
);

add_action(
'woocommerce_before_single_product',
function () {
echo '<p data-testid="woocommerce_before_single_product">
Hook: woocommerce_before_single_product
</p>';
}
);

add_action(
'woocommerce_before_single_product_summary',
function () {
echo '<p data-testid="woocommerce_before_single_product_summary">
Hook: woocommerce_before_single_product_summary
</p>';
}
);

add_action(
'woocommerce_single_product_summary',
function () {
echo '<p data-testid="woocommerce_single_product_summary">
Hook: woocommerce_single_product_summary
</p>';
}
);

add_action(
'woocommerce_before_add_to_cart_button',
function () {
echo '<p data-testid="woocommerce_before_add_to_cart_button">
Hook: woocommerce_before_add_to_cart_button
</p>';
}
);


add_action(
'woocommerce_product_meta_start',
function () {
echo '<p data-testid="woocommerce_product_meta_start">
Hook: woocommerce_product_meta_start
</p>';
}
);

add_action(
'woocommerce_product_meta_end',
function () {
echo '<p data-testid="woocommerce_product_meta_end">
Hook: woocommerce_product_meta_end
</p>';
}
);

add_action(
'woocommerce_share',
function () {
echo '<p data-testid="woocommerce_share">
Hook: woocommerce_share
</p>';
}
);

add_action(
'woocommerce_after_single_product_summary',
function () {
echo '<p data-testid="woocommerce_after_single_product_summary">
Hook: woocommerce_after_single_product_summary
</p>';
}
);

add_action(
'woocommerce_after_single_product',
function () {
echo '<p data-testid="woocommerce_after_single_product">
Hook: woocommerce_after_single_product
</p>';
}
);

add_action(
'woocommerce_after_main_content',
function () {
echo '<p data-testid="woocommerce_after_main_content">
Hook: woocommerce_after_main_content
</p>';
}
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/**
* External dependencies
*/
import { test, expect } from '@woocommerce/e2e-playwright-utils';
import {
installPluginFromPHPFile,
uninstallPluginFromPHPFile,
} from '@woocommerce/e2e-mocks/custom-plugins';

/**
* Internal dependencies
*/

type Scenario = {
title: string;
dataTestId: string;
content: string;
amount: number;
};

const singleOccurranceScenarios: Scenario[] = [
{
title: 'Before Main Content',
dataTestId: 'woocommerce_before_main_content',
content: 'Hook: woocommerce_before_main_content',
amount: 1,
},
{
title: 'Sidebar',
dataTestId: 'woocommerce_sidebar',
content: 'Hook: woocommerce_sidebar',
amount: 1,
},
{
title: 'Before Single Product',
dataTestId: 'woocommerce_before_single_product',
content: 'Hook: woocommerce_before_single_product',
amount: 1,
},
{
title: 'Before Single Product Summary',
dataTestId: 'woocommerce_before_single_product_summary',
content: 'Hook: woocommerce_before_single_product_summary',
amount: 1,
},
{
title: 'Before Add To Cart Button',
dataTestId: 'woocommerce_before_add_to_cart_button',
content: 'Hook: woocommerce_before_add_to_cart_button',
amount: 1,
},
{
title: 'Single Product Summary',
dataTestId: 'woocommerce_single_product_summary',
content: 'Hook: woocommerce_single_product_summary',
amount: 1,
},
{
title: 'Product Meta Start',
dataTestId: 'woocommerce_product_meta_start',
content: 'Hook: woocommerce_product_meta_start',
amount: 1,
},
{
title: 'Product Meta End',
dataTestId: 'woocommerce_product_meta_end',
content: 'Hook: woocommerce_product_meta_end',
amount: 1,
},
{
title: 'Share',
dataTestId: 'woocommerce_share',
content: 'Hook: woocommerce_share',
amount: 1,
},
{
title: 'After Single Product Summary',
dataTestId: 'woocommerce_after_single_product_summary',
content: 'Hook: woocommerce_after_single_product_summary',
amount: 1,
},
{
title: 'After Single Product',
dataTestId: 'woocommerce_after_single_product',
content: 'Hook: woocommerce_after_single_product',
amount: 1,
},
];

const compatiblityPluginFileName = 'compatibility-plugin.php';

test.describe( 'Compatibility Layer with Product Collection block', () => {
test.beforeAll( async () => {
await installPluginFromPHPFile(
`${ __dirname }/${ compatiblityPluginFileName }`
);
} );

test.describe(
'Product Archive with Product Collection block',
async () => {
test.beforeAll( async ( { page } ) => {
await page.goto( '/product/hoodie/' );
} );

for ( const scenario of singleOccurranceScenarios ) {
test( `${ scenario.title } is attached to the page`, async ( {
page,
} ) => {
const hooks = page.getByTestId( scenario.dataTestId );

await expect( hooks ).toHaveCount( scenario.amount );
await expect( hooks ).toHaveText( scenario.content );
} );
}
}
);
} );

test.afterAll( async ( { requestUtils } ) => {
await uninstallPluginFromPHPFile(
`${ __dirname }/${ compatiblityPluginFileName }`
);
await requestUtils.deleteAllTemplates( 'wp_template' );
} );

0 comments on commit 5a73912

Please sign in to comment.