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

Add E2E tests for WooCommerce #2923

Merged
merged 17 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from 13 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
1 change: 1 addition & 0 deletions .wp-env.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"wp-content/plugins/unsupported-elasticsearch-version.php": "./tests/cypress/wordpress-files/test-plugins/unsupported-elasticsearch-version.php",
"wp-content/plugins/shorten-autosave.php": "./tests/cypress/wordpress-files/test-plugins/shorten-autosave.php",
"wp-content/plugins/fake-log-messages.php": "./tests/cypress/wordpress-files/test-plugins/fake-log-messages.php",
"wp-content/plugins/enable-debug-bar.php": "./tests/cypress/wordpress-files/test-plugins/enable-debug-bar.php",
"wp-content/uploads/content-example.xml": "./tests/cypress/wordpress-files/test-docs/content-example.xml"
}
}
Expand Down
131 changes: 131 additions & 0 deletions tests/cypress/integration/features/woocommerce.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
describe('WooCommerce Feature', () => {
const userData = {
username: 'testuser',
email: 'testuser@example.com',
firstName: 'John',
lastName: 'Doe',
address: '123 Main St',
city: 'Culver City',
postCode: '90230',
phoneNumber: '1234567890',
};

before(() => {
cy.deactivatePlugin('woocommerce', 'wpCli');
});
Expand Down Expand Up @@ -45,6 +56,9 @@ describe('WooCommerce Feature', () => {
cy.maybeEnableFeature('protected_content');
cy.maybeEnableFeature('woocommerce');

// this is required to sync the orders to Elasticsearch.
cy.wpCli('elasticpress index --setup --yes');

cy.visitAdminPage('edit.php?post_type=shop_order');
cy.get('#debug-menu-target-EP_Debug_Bar_ElasticPress .ep-query-debug').should(
'contain.text',
Expand Down Expand Up @@ -88,4 +102,121 @@ describe('WooCommerce Feature', () => {
'Query Response Code: HTTP 200',
);
});

it('Can not display other users orders on the My Account Order page', () => {
cy.login();

cy.activatePlugin('woocommerce');
cy.activatePlugin('enable-debug-bar');

cy.maybeEnableFeature('protected_content');
cy.maybeEnableFeature('woocommerce');

// enable payment gateway.
cy.visitAdminPage('admin.php?page=wc-settings&tab=checkout&section=cod');
cy.get('#woocommerce_cod_enabled').check();
cy.get('.button-primary.woocommerce-save-button').click();

cy.logout();

// create new user.
cy.createUser({
username: userData.username,
email: userData.email,
login: true,
});

// add product to cart.
cy.visit('product/fantastic-silk-knife');
cy.get('.single_add_to_cart_button').click();

// checkout and place order.
cy.visit('checkout');
cy.get('#billing_first_name').type(userData.firstName);
cy.get('#billing_last_name').type(userData.lastName);
cy.get('#billing_address_1').type(userData.address);
cy.get('#billing_city').type(userData.city);
cy.get('#billing_postcode').type(userData.postCode);
cy.get('#billing_phone').type(userData.phoneNumber);
cy.get('#place_order').click();

// ensure order is placed.
cy.url().should('include', '/checkout/order-received');

// ensure order is visible to user.
cy.visit('my-account/orders');
cy.get('.woocommerce-orders-table tbody tr').should('have.length', 1);

// Test orderby parameter set to `date` in query.
cy.get('#debug-menu-target-EP_Debug_Bar_ElasticPress .ep-query-debug').should(
'contain.text',
"'orderby' => 'date'",
);
felipeelia marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure we are not capturing an autosuggest template query or similar, can we check for shop_order in this query?


cy.logout();

cy.createUser({
username: 'testuser2',
email: 'testuser2@example.com',
login: true,
});

// ensure no order is show.
cy.visit('my-account/orders');
cy.get('.woocommerce-orders-table tbody tr').should('have.length', 0);

cy.get('#debug-menu-target-EP_Debug_Bar_ElasticPress .ep-query-debug').should(
'contain.text',
'Query Response Code: HTTP 200',
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, can we check for shop_order on this one as well?

});

it('Can search orders from ElasticPress in WP Dashboard', () => {
cy.login();

cy.activatePlugin('woocommerce');
cy.maybeEnableFeature('protected_content');
cy.maybeEnableFeature('woocommerce');

cy.visitAdminPage('edit.php?post_type=shop_order');

// search order by user's name.
cy.get('#post-search-input')
.clear()
.type(`${userData.firstName} ${userData.lastName}{enter}`);

cy.get('#debug-menu-target-EP_Debug_Bar_ElasticPress .ep-query-debug').should(
'contain.text',
'Query Response Code: HTTP 200',
);

cy.get('.order_number .order-view').should(
'contain.text',
`${userData.firstName} ${userData.lastName}`,
);

// search order by user's address.
cy.get('#post-search-input').clear().type(`${userData.address}{enter}`);
cy.get('#debug-menu-target-EP_Debug_Bar_ElasticPress .ep-query-debug').should(
'contain.text',
'Query Response Code: HTTP 200',
);

cy.get('.order_number .order-view').should(
'contain.text',
`${userData.firstName} ${userData.lastName}`,
);

// search order by product.
cy.get('#post-search-input').clear().type(`fantastic-silk-knife{enter}`);
cy.get('#debug-menu-target-EP_Debug_Bar_ElasticPress .ep-query-debug').should(
'contain.text',
'Query Response Code: HTTP 200',
);

cy.get('.order_number .order-view').should(
'contain.text',
`${userData.firstName} ${userData.lastName}`,
);
});
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@burhandodhy can we bring to this file the test we have for Product variations skus?

35 changes: 35 additions & 0 deletions tests/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,38 @@ Cypress.Commands.add('createAutosavePost', (postData) => {
cy.wait(5000);
cy.deactivatePlugin('shorten-autosave', 'wpCli');
});

Cypress.Commands.add('logout', () => {
cy.visit('/wp-admin');
cy.get('body').then(($body) => {
if ($body.find('#wpadminbar').length !== 0) {
cy.get('#wp-admin-bar-my-account').invoke('addClass', 'hover');
cy.get('#wp-admin-bar-logout > a').click();
}
});
});

Cypress.Commands.add('createUser', (userData) => {
const newUserDate = {
username: 'testuser',
password: 'password',
email: 'testuser@example.com',
role: 'subscriber',
login: false,
...userData,
};

// delete the user.
cy.wpCli(`wp user delete ${newUserDate.username} --yes --network`, true);

// create the user
cy.wpCli(
`wp user create ${newUserDate.username} ${newUserDate.email} --user_pass=${newUserDate.password} --role=${newUserDate.role}`,
);

if (newUserDate.login) {
cy.visit('wp-login.php');
cy.get('#user_login').clear().type(newUserDate.username);
cy.get('#user_pass').clear().type(`${newUserDate.password}{enter}`);
}
});
11 changes: 11 additions & 0 deletions tests/cypress/wordpress-files/test-plugins/enable-debug-bar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* Plugin Name: Enable Debug bar
* Description: Enable debug bar for all users
* Version: 1.0.0
* Author: 10up Inc.
* License: GPLv2 or later
*/

add_filter( 'debug_bar_enable', '__return_true' );
add_filter( 'show_admin_bar', '__return_true', 99 );