Critical flows: Merchant → Checkout → Can adjust T&S and Privacy Policy options (https://github.com/woocommerce/woocommerce-blocks/pull/6211)

* Add test to check if T&C and Privacy links are visible

* Fix filling wrong type of form error

With a virtual product we shouldn't need to fill the shipping details
We need to fill the billing instead

* Fix undefined 'termsCheckbox' bug

* Fix terms checkbox name

* Activate test case

* Replace static URL with dynamic one

* Convert external plugin into mock

* Fix JS linting issue

Co-authored-by: Saad Tarhi <saad.trh@gmail.com>
This commit is contained in:
Niels Lange 2022-04-25 10:48:54 +02:00 committed by GitHub
parent ce99a47fb4
commit f10bb2da82
4 changed files with 204 additions and 0 deletions

View File

@ -3,6 +3,7 @@
"plugins": [
"https://downloads.wordpress.org/plugin/woocommerce.latest-stable.zip",
"https://github.com/WP-API/Basic-Auth/archive/master.zip",
"./tests/mocks/woo-test-helper",
"."
],
"themes": [

View File

@ -0,0 +1,106 @@
/**
* External dependencies
*/
import {
merchant,
openDocumentSettingsSidebar,
setCheckbox,
unsetCheckbox,
} from '@woocommerce/e2e-utils';
import {
visitBlockPage,
selectBlockByName,
saveOrPublish,
} from '@woocommerce/blocks-test-utils';
/**
* Internal dependencies
*/
import {
shopper,
preventCompatibilityNotice,
reactivateCompatibilityNotice,
} from '../../../utils';
import {
BASE_URL,
BILLING_DETAILS,
SIMPLE_VIRTUAL_PRODUCT_NAME,
} from '../../../utils/constants';
if ( process.env.WOOCOMMERCE_BLOCKS_PHASE < 2 ) {
// eslint-disable-next-line jest/no-focused-tests
test.only( `Skipping checkout tests`, () => {} );
}
describe( 'Merchant → Checkout → Can adjust T&S and Privacy Policy options', () => {
beforeAll( async () => {
await shopper.goToShop();
await page.goto( `${ BASE_URL }/?setup_terms_and_privacy` );
await expect( page ).toMatch( 'Terms & Privacy pages set up.' );
await shopper.block.emptyCart();
} );
afterAll( async () => {
await shopper.block.emptyCart();
await page.goto( `${ BASE_URL }/?teardown_terms_and_privacy` );
await expect( page ).toMatch( 'Terms & Privacy pages teared down.' );
} );
it( 'Merchant can see T&S and Privacy Policy links without checkbox', async () => {
await shopper.goToShop();
await shopper.addToCartFromShopPage( SIMPLE_VIRTUAL_PRODUCT_NAME );
await shopper.block.goToCheckout();
await expect( page ).toMatch(
'By proceeding with your purchase you agree to our Terms and Conditions and Privacy Policy'
);
await shopper.block.placeOrder();
await expect( page ).toMatch( 'Order received' );
} );
it( 'Merchant can see T&S and Privacy Policy links with checkbox', async () => {
// Activate checkboxes for T&S and Privacy Policy links.
await preventCompatibilityNotice();
await merchant.login();
await visitBlockPage( 'Checkout Block' );
await openDocumentSettingsSidebar();
await selectBlockByName( 'woocommerce/checkout-terms-block' );
const [ termsCheckboxLabel ] = await page.$x(
`//label[contains(text(), "Require checkbox") and contains(@class, "components-toggle-control__label")]`
);
const termsCheckboxId = await page.evaluate(
( label ) => `#${ label.getAttribute( 'for' ) }`,
termsCheckboxLabel
);
await setCheckbox( termsCheckboxId );
await saveOrPublish();
await shopper.goToShop();
await shopper.addToCartFromShopPage( SIMPLE_VIRTUAL_PRODUCT_NAME );
await shopper.block.goToCheckout();
await shopper.block.fillBillingDetails( BILLING_DETAILS );
// Placing an order now, must lead to an error.
await page.click( '.wc-block-components-checkout-place-order-button' );
const termsCheckbox = await page.$(
'.wp-block-woocommerce-checkout-terms-block div'
);
const termsCheckboxClassList = (
await ( await termsCheckbox.getProperty( 'className' ) ).jsonValue()
).split( ' ' );
expect( termsCheckboxClassList ).toContain( 'has-error' );
await setCheckbox( '#terms-and-conditions' );
// Placing an order now, must succeed.
await shopper.block.placeOrder();
await expect( page ).toMatch( 'Order received' );
// Deactivate checkboxes for T&S and Privacy Policy links.
await visitBlockPage( 'Checkout Block' );
await openDocumentSettingsSidebar();
await selectBlockByName( 'woocommerce/checkout-terms-block' );
await unsetCheckbox( termsCheckboxId );
await saveOrPublish();
await merchant.logout();
await reactivateCompatibilityNotice();
} );
} );

View File

@ -0,0 +1,96 @@
<?php
/**
* Plugin Name: Woo Test Helper
* Description: A helper plugin to control settings within Woo e2e tests.
* Version: 0.0.1
* Author: Automattic
* Author URI: https://automattic.com
* Text Domain: woo-test-helper
* Requires at least: 5.9
* Requires PHP: 7.3
* WC requires at least: 6.0
* WC tested up to: 6.1
*
* @package WordPress
* @subpackage Woo Test Helper
*/
defined( 'ABSPATH' ) || exit;
/**
* Define URL endpoints for setting up and tearing down the T&C and Privacy Policy pages.
*/
function woocommerce_setup_terms_and_privacy_page() {
// phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['setup_terms_and_privacy'] ) ) {
publish_privacy_page();
publish_terms_page();
exit( 'Terms & Privacy pages set up.' );
}
// phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['teardown_terms_and_privacy'] ) ) {
unpublish_privacy_page();
delete_terms_page();
exit( 'Terms & Privacy pages teared down.' );
}
}
add_action( 'init', 'woocommerce_setup_terms_and_privacy_page' );
/**
* Publish Privacy Policy page.
*/
function publish_privacy_page() {
global $wpdb;
$table = $wpdb->prefix . 'posts';
$data = array( 'post_status' => 'publish' );
$where = array(
'post_title' => 'Privacy Policy',
'post_status' => 'draft',
);
$wpdb->update( $table, $data, $where );
}
/**
* Publish and set Terms & Conditions page.
*/
function publish_terms_page() {
global $wpdb;
$table = $wpdb->prefix . 'posts';
$data = array(
'post_title' => 'Terms & Conditions',
'post_status' => 'publish',
'post_type' => 'page',
'post_author' => 1,
);
$wpdb->replace( $table, $data );
update_option( 'woocommerce_terms_page_id', $wpdb->insert_id );
}
/**
* Unpublish Privacy Policy page.
*/
function unpublish_privacy_page() {
global $wpdb;
$table = $wpdb->prefix . 'posts';
$data = array( 'post_status' => 'draft' );
$where = array(
'post_title' => 'Privacy Policy',
'post_status' => 'publish',
);
$wpdb->update( $table, $data, $where );
}
/**
* Delete Terms & Conditions page.
*/
function delete_terms_page() {
global $wpdb;
$table = $wpdb->prefix . 'posts';
$data = array( 'post_title' => 'Terms & Conditions' );
$wpdb->delete( $table, $data );
}

View File

@ -16,3 +16,4 @@ export const PAYMENT_CHEQUE = 'Check payments';
export const BILLING_DETAILS = config.get( 'addresses.customer.billing' );
export const PERFORMANCE_REPORT_FILENAME = 'reports/e2e-performance.json';
export const SHIPPING_DETAILS = config.get( 'addresses.customer.shipping' );
export const BASE_URL = config.get( 'url' );