Files
wp-graphql-woocommerce/tests/wpunit/WooCommerceEmailTemplatesTest.php
Geoff TaylorandGitHub 460b101b47 fix: HPOS order mutation data loss, COT cursor pagination, email tests, checkout auth (#1003)
* devops: WC email template tests, COT cursor HPOS fix, checkout account auth

WC Email Template Tests:
- Add WooCommerceEmailTemplatesTest verifying WC email templates are used
  for registerCustomer, checkout with account creation, and password reset
- Use MockPHPMailer to capture emails and verify HTML content type
- Disable deferred transactional emails during tests via GRAPHQL_TESTING flag

COT Cursor HPOS Fix:
- Fix COT_Cursor::compare_with to resolve orderby aliases and legacy meta
  keys (_order_total, _date_completed, etc.) to COT column names
- Add resolve_orderby_alias() mapping short aliases and meta keys to columns
- Add DB_Hooks::clean_query_vars to translate post_* and meta_key orderby
  to COT-compatible equivalents via woocommerce_order_query_args filter

Checkout Account Authentication:
- Add authenticate field to CreateAccountInput type
- Gate wc_set_customer_auth_cookie() behind authenticate flag in checkout
  mutation so account creation doesn't auto-authenticate by default

Closes #882

* devops: codeception.dist.yml updated

* fix: Functional test cleanup and CI coverage condition

Test fixes:
- Enable authorizing URL fields in ProtectedRouterCest and
  DownloadableItemAuthCest via setWooGraphQLSetting
- Add stale data cleanup (sessions, users, products, orders) to
  GraphQLE2E _setupStore/getCatalog/setupStoreAndUsers
- Fix CartTransactionQueueCest and CartQueriesTest for test isolation

CI:
- Only run coverage job when at least one upstream job succeeds

* chore: Linter compliances met

* fix: HPOS order mutation data loss and CI coverage condition

Refactor order create/update mutations to set all props on a single
WC_Order instance before saving, mirroring the WC REST API pattern.
Previously, separate add_order_meta() and add_items() calls each loaded
their own order instance and saved independently, causing HPOS data loss
for payment method, addresses, and other fields.

Also fix CI coverage job to only run when all upstream jobs succeed,
and correct test assertions for RAW format line item totals.

Closes #591

* chore: Linter compliances met

* chore: Remove dead code from Order_Mutation after prepare_order refactor

Removes add_items() and update_address() which are no longer called
after the prepare_order() consolidation.

* chore: Remove dead code add_order_meta and update_item_meta_data
2026-03-30 21:42:00 -04:00

183 lines
5.0 KiB
PHP

<?php
class WooCommerceEmailTemplatesTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraphQLTestCase {
public function setUp(): void {
parent::setUp();
// Reset captured emails from previous tests.
reset_phpmailer_instance();
// Enable bacs payment gateway.
$gateways = \WC()->payment_gateways->payment_gateways();
$bacs_gateway = $gateways['bacs'];
$bacs_gateway->settings['enabled'] = 'yes';
update_option( $bacs_gateway->get_option_key(), $bacs_gateway->settings );
\WC()->payment_gateways->init();
}
public function tearDown(): void {
// Reset WC state to prevent test contamination.
\WC()->customer = null;
\WC()->session = null;
\WC()->cart = null;
\WC()->initialize_session();
\WC()->initialize_cart();
$this->loginAs( 0 );
parent::tearDown();
}
/**
* Find a sent email by recipient address in the mock mailer.
*
* @param string $to_address Recipient email address.
*
* @return object|null
*/
private function find_sent_email( $to_address ) {
$mailer = tests_retrieve_phpmailer_instance();
if ( ! $mailer ) {
return null;
}
foreach ( $mailer->mock_sent as $index => $sent ) {
$recipient = $mailer->get_recipient( 'to', $index );
if ( $recipient && $to_address === $recipient->address ) {
return $mailer->get_sent( $index );
}
}
return null;
}
public function testRegisterCustomerSendsWooCommerceNewAccountEmail() {
$query = '
mutation ($input: RegisterCustomerInput!) {
registerCustomer(input: $input) {
customer {
databaseId
email
}
}
}
';
$variables = [
'input' => [
'email' => 'newcustomer@example.com',
'username' => 'newcustomer',
'password' => 'testpassword123',
'authenticate' => true,
],
];
$response = $this->graphql( compact( 'query', 'variables' ) );
$this->assertQuerySuccessful(
$response,
[
$this->expectedField( 'registerCustomer.customer.databaseId', static::NOT_FALSY ),
$this->expectedField( 'registerCustomer.customer.email', 'newcustomer@example.com' ),
]
);
$sent = $this->find_sent_email( 'newcustomer@example.com' );
$this->assertNotNull( $sent, 'WC new account email should be sent to the registered customer.' );
$this->assertStringContainsString( 'text/html', $sent->header, 'New account email should use HTML content type from WooCommerce template.' );
}
public function testCheckoutWithAccountCreationSendsWooCommerceNewAccountEmail() {
// Enable guest checkout and account creation.
update_option( 'woocommerce_enable_guest_checkout', 'yes' );
update_option( 'woocommerce_enable_signup_and_login_from_checkout', 'yes' );
$product_id = $this->factory->product->createSimple( [ 'virtual' => true ] );
WC()->cart->add_to_cart( $product_id, 1 );
$query = '
mutation ($input: CheckoutInput!) {
checkout(input: $input) {
order {
databaseId
}
customer {
databaseId
email
}
}
}
';
$variables = [
'input' => [
'paymentMethod' => 'bacs',
'isPaid' => true,
'billing' => [
'firstName' => 'John',
'lastName' => 'Doe',
'email' => 'checkoutcustomer@example.com',
'address1' => '123 Test St',
'city' => 'Testville',
'state' => 'CA',
'postcode' => '90210',
'country' => 'US',
],
'account' => [
'username' => 'checkoutcustomer',
'password' => 'testpassword123',
'authenticate' => true,
],
],
];
$response = $this->graphql( compact( 'query', 'variables' ) );
$this->assertQuerySuccessful(
$response,
[
$this->expectedField( 'checkout.order.databaseId', static::NOT_FALSY ),
$this->expectedField( 'checkout.customer.databaseId', static::NOT_FALSY ),
$this->expectedField( 'checkout.customer.email', 'checkoutcustomer@example.com' ),
]
);
$sent = $this->find_sent_email( 'checkoutcustomer@example.com' );
$this->assertNotNull( $sent, 'WC new account email should be sent when creating account during checkout.' );
$this->assertStringContainsString( 'text/html', $sent->header, 'Checkout account creation email should use HTML content type from WooCommerce template.' );
}
public function testResetPasswordSendsWooCommerceEmail() {
$this->factory->customer->create(
[ 'email' => 'resetuser@example.com' ]
);
$query = '
mutation ($input: SendPasswordResetEmailInput!) {
sendPasswordResetEmail(input: $input) {
success
}
}
';
$variables = [
'input' => [
'username' => 'resetuser@example.com',
],
];
$response = $this->graphql( compact( 'query', 'variables' ) );
$this->assertQuerySuccessful(
$response,
[
$this->expectedField( 'sendPasswordResetEmail.success', true ),
]
);
$sent = $this->find_sent_email( 'resetuser@example.com' );
$this->assertNotNull( $sent, 'A password reset email should be sent.' );
$this->assertStringContainsString( 'text/html', $sent->header, 'Password reset email should use HTML content type from WooCommerce template.' );
}
}