Skip to content

Commit 4180b0b

Browse files
authored
Fix Optimized Checkout inconsistencies (#4445)
* Fix OC source of truth * Fix tests * Improving tests * Improving tests * Improving tests * Improving tests * Improving tests * New helper to test OC related methods * Remove unused import * Renaming some variables to keep consistency * Fix tests * Changelog and reame entries * Update changelog.txt * Update readme.txt
1 parent 33d11d6 commit 4180b0b

9 files changed

+130
-61
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*** Changelog ***
22

33
= 9.6.0 - xxxx-xx-xx =
4+
* Fix - Fixes some inconsistencies related to the Optimized Checkout feature and improves its unit tests
45
* Fix - Throws a specific exception on an edge case where a saved payment method could not be found when processing an order in the new checkout experience
56
* Fix - Checks if the store has other BNPL extensions installed before displaying the promotional banner
67
* Fix - Correctly notifies customers and merchants of a failed refund and reverts the refunded status

includes/abstracts/abstract-wc-stripe-payment-gateway.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,7 @@ public function payment_icons() {
367367
'cards' => '<img src="' . WC_STRIPE_PLUGIN_URL . '/assets/images/cards.svg" class="stripe-cards-icon stripe-icon" alt="' . __( 'Credit / Debit Card', 'woocommerce-gateway-stripe' ) . '" />',
368368
WC_Stripe_Payment_Methods::CASHAPP_PAY => '<img src="' . WC_STRIPE_PLUGIN_URL . '/assets/images/cashapp.svg" class="stripe-cashapp-icon stripe-icon" alt="Cash App Pay" />',
369369
];
370-
$settings = WC_Stripe_Helper::get_stripe_settings();
371-
$oc_setting = $settings['optimized_checkout_element'] ?? null;
372-
if ( 'yes' === $oc_setting ) {
370+
if ( 'yes' === $this->get_option( 'optimized_checkout_element' ) ) {
373371
$icon_list['cards'] = '';
374372
}
375373
return apply_filters( 'wc_stripe_payment_icons', $icon_list );

includes/payment-methods/class-wc-stripe-upe-payment-gateway.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ public function payment_fields() {
791791

792792
<?php
793793
if ( $this->testmode ) :
794-
if ( $this->spe_enabled ) :
794+
if ( $this->oc_enabled ) :
795795
echo wp_kses_post( self::get_testing_instructions_for_optimized_checkout() );
796796
else :
797797
?>
@@ -1972,7 +1972,7 @@ public function is_sepa_tokens_for_other_methods_enabled() {
19721972
* @deprecated 9.5.0 Use is_oc_enabled() instead.
19731973
*/
19741974
public function is_spe_enabled() {
1975-
return $this->spe_enabled;
1975+
return $this->oc_enabled;
19761976
}
19771977

19781978
/**

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
112112

113113
= 9.6.0 - xxxx-xx-xx =
114114

115+
* Fix - Fixes some inconsistencies related to the Optimized Checkout feature and improves its unit tests
115116
* Fix - Throws a specific exception on an edge case where a saved payment method could not be found when processing an order in the new checkout experience
116117
* Fix - Checks if the store has other BNPL extensions installed before displaying the promotional banner
117118
* Fix - Correctly notifies customers and merchants of a failed refund and reverts the refunded status.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace WooCommerce\Stripe\Tests\Helpers;
4+
5+
use WC_Stripe_Feature_Flags;
6+
use WC_Stripe_Helper;
7+
8+
/**
9+
* Provides useful methods to test logic related to the Optimized Checkout.
10+
*/
11+
class OC_Test_Helper {
12+
/**
13+
* Enables the Optimized Checkout feature flag and sets the corresponding setting.
14+
*
15+
* @return void
16+
*/
17+
public static function enable_oc() {
18+
update_option( WC_Stripe_Feature_Flags::OC_FEATURE_FLAG_NAME, 'yes' );
19+
20+
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
21+
$stripe_settings['optimized_checkout_element'] = 'yes';
22+
WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings );
23+
}
24+
25+
/**
26+
* Disables the Optimized Checkout feature flag and sets the corresponding setting.
27+
*
28+
* @return void
29+
*/
30+
public static function disable_oc() {
31+
update_option( WC_Stripe_Feature_Flags::OC_FEATURE_FLAG_NAME, 'no' );
32+
33+
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
34+
$stripe_settings['optimized_checkout_element'] = 'no';
35+
WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings );
36+
}
37+
}

tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Automattic\WooCommerce\Enums\OrderStatus;
66
use Exception;
7+
use WooCommerce\Stripe\Tests\Helpers\OC_Test_Helper;
78
use WooCommerce\Stripe\Tests\Helpers\UPE_Test_Helper;
89
use WC_Data_Exception;
910
use WC_Order;
@@ -2892,24 +2893,42 @@ public function payment_method_titles_provider() {
28922893
/**
28932894
* Test that a failed payment intent is not reused and a new one is created instead.
28942895
*
2896+
* @param bool $setting_enabled Whether the optimized checkout setting is enabled.
2897+
* @param bool $expected The expected result of the `is_oc_enabled` method.
28952898
* @return void
2899+
*
2900+
* @dataProvider provide_test_is_oc_enabled
28962901
*/
2897-
public function test_is_oc_enabled() {
2898-
// Disabled
2899-
update_option( WC_Stripe_Feature_Flags::OC_FEATURE_FLAG_NAME, 'no' );
2902+
public function test_is_oc_enabled( $setting_enabled, $expected ) {
2903+
if ( $setting_enabled ) {
2904+
OC_Test_Helper::enable_oc();
2905+
}
29002906

29012907
$gateway = new WC_Stripe_UPE_Payment_Gateway();
2902-
$this->assertFalse( $gateway->is_oc_enabled() );
2908+
$actual = $gateway->is_oc_enabled();
29032909

2904-
// Enabled
2905-
update_option( WC_Stripe_Feature_Flags::OC_FEATURE_FLAG_NAME, 'yes' );
2910+
// Clean up
2911+
OC_Test_Helper::disable_oc();
29062912

2907-
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
2908-
$stripe_settings['optimized_checkout_element'] = 'yes';
2909-
WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings );
2913+
$this->assertSame( $expected, $actual );
2914+
}
29102915

2911-
$gateway = new WC_Stripe_UPE_Payment_Gateway();
2912-
$this->assertTrue( $gateway->is_oc_enabled() );
2916+
/**
2917+
* Data provider for `test_is_oc_enabled`.
2918+
*
2919+
* @return array[]
2920+
*/
2921+
public function provide_test_is_oc_enabled() {
2922+
return [
2923+
'Disabled' => [
2924+
'setting value' => false,
2925+
'expected' => false,
2926+
],
2927+
'Enabled' => [
2928+
'setting value' => true,
2929+
'expected' => true,
2930+
],
2931+
];
29132932
}
29142933

29152934
/**

tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Method_CC_Test.php

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use WC_Stripe_Helper;
77
use WC_Stripe_Payment_Methods;
88
use WC_Stripe_UPE_Payment_Method_CC;
9+
use WooCommerce\Stripe\Tests\Helpers\OC_Test_Helper;
910
use WP_UnitTestCase;
1011

1112
/**
@@ -17,19 +18,17 @@ class WC_Stripe_UPE_Payment_Method_CC_Test extends WP_UnitTestCase {
1718
*
1819
* @param array $settings Settings.
1920
* @param array|bool $payment_details Payment details.
20-
* @param bool $optimized_checkout_flag Optimized Checkout flag.
21+
* @param bool $optimized_checkout_setting Optimized Checkout flag.
2122
* @param array $query_params Query parameters.
2223
* @param string $expected Expected title.
2324
* @return void
2425
*
2526
* @dataProvider provide_test_get_title
2627
*/
27-
public function test_get_title( $settings, $payment_details, $optimized_checkout_flag, $query_params, $expected ) {
28-
update_option( WC_Stripe_Feature_Flags::OC_FEATURE_FLAG_NAME, $optimized_checkout_flag ? 'yes' : 'no' );
29-
30-
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
31-
$stripe_settings['optimized_checkout_element'] = $optimized_checkout_flag ? 'yes' : 'no';
32-
WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings );
28+
public function test_get_title( $settings, $payment_details, $optimized_checkout_setting, $query_params, $expected ) {
29+
if ( $optimized_checkout_setting ) {
30+
OC_Test_Helper::enable_oc();
31+
}
3332

3433
if ( is_array( $payment_details ) ) {
3534
$payment_details = json_decode( wp_json_encode( $payment_details ) );
@@ -42,8 +41,12 @@ public function test_get_title( $settings, $payment_details, $optimized_checkout
4241
}
4342

4443
$payment_method = new WC_Stripe_UPE_Payment_Method_CC();
44+
$actual = $payment_method->get_title( $payment_details );
4545

46-
$this->assertEquals( $expected, $payment_method->get_title( $payment_details ) );
46+
// Clean up.
47+
OC_Test_Helper::disable_oc();
48+
49+
$this->assertEquals( $expected, $actual );
4750
}
4851

4952
/**
@@ -109,22 +112,24 @@ public function provide_test_get_title() {
109112
/**
110113
* Test for `get_testing_instructions`.
111114
*
112-
* @param bool $optimized_checkout_flag Optimized Checkout flag.
115+
* @param bool $optimized_checkout_setting Optimized Checkout setting.
113116
* @param string $expected Expected instructions.
114117
* @return void
115118
*
116119
* @dataProvider provide_test_get_testing_instructions
117120
*/
118-
public function test_get_testing_instructions( $optimized_checkout_flag, $expected ) {
119-
update_option( WC_Stripe_Feature_Flags::OC_FEATURE_FLAG_NAME, $optimized_checkout_flag ? 'yes' : 'no' );
120-
121-
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
122-
$stripe_settings['optimized_checkout_element'] = $optimized_checkout_flag ? 'yes' : 'no';
123-
WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings );
121+
public function test_get_testing_instructions( $optimized_checkout_setting, $expected ) {
122+
if ( $optimized_checkout_setting ) {
123+
OC_Test_Helper::enable_oc();
124+
}
124125

125126
$payment_method = new WC_Stripe_UPE_Payment_Method_CC();
127+
$actual = $payment_method->get_testing_instructions();
128+
129+
// Clean up
130+
OC_Test_Helper::disable_oc();
126131

127-
$this->assertEquals( $expected, $payment_method->get_testing_instructions() );
132+
$this->assertEquals( $expected, $actual );
128133
}
129134

130135
/**
@@ -135,12 +140,12 @@ public function test_get_testing_instructions( $optimized_checkout_flag, $expect
135140
public function provide_test_get_testing_instructions() {
136141
return [
137142
'Optimized Checkout enabled' => [
138-
'optimized checkout flag' => true,
139-
'expected' => '<div id="wc-stripe-payment-method-instructions-card" class="wc-stripe-payment-method-instruction" style="display: none;"><strong>Test mode:</strong> use the test VISA card 4242424242424242 with any expiry date and CVC. Other payment methods may redirect to a Stripe test page to authorize payment. More test card numbers are listed <a href="https://docs.stripe.com/testing" target="_blank">here</a>.</div><div id="wc-stripe-payment-method-instructions-blik" class="wc-stripe-payment-method-instruction" style="display: none;"><strong>Test mode:</strong> use any 6-digit number to authorize payment.</div><div id="wc-stripe-payment-method-instructions-sepa_debit" class="wc-stripe-payment-method-instruction" style="display: none;"><strong>Test mode:</strong> use the test account number AT611904300234573201. Other payment methods may redirect to a Stripe test page to authorize payment. More test card numbers are listed <a href="https://docs.stripe.com/testing?payment-method=sepa-direct-debit#non-card-payments" target="_blank">here</a>.</div>',
143+
'optimized checkout setting' => true,
144+
'expected' => '<div id="wc-stripe-payment-method-instructions-card" class="wc-stripe-payment-method-instruction" style="display: none;"><strong>Test mode:</strong> use the test VISA card 4242424242424242 with any expiry date and CVC. Other payment methods may redirect to a Stripe test page to authorize payment. More test card numbers are listed <a href="https://docs.stripe.com/testing" target="_blank">here</a>.</div><div id="wc-stripe-payment-method-instructions-blik" class="wc-stripe-payment-method-instruction" style="display: none;"><strong>Test mode:</strong> use any 6-digit number to authorize payment.</div><div id="wc-stripe-payment-method-instructions-sepa_debit" class="wc-stripe-payment-method-instruction" style="display: none;"><strong>Test mode:</strong> use the test account number AT611904300234573201. Other payment methods may redirect to a Stripe test page to authorize payment. More test card numbers are listed <a href="https://docs.stripe.com/testing?payment-method=sepa-direct-debit#non-card-payments" target="_blank">here</a>.</div>',
140145
],
141146
'Optimized Checkout disabled' => [
142-
'optimized checkout flag' => false,
143-
'expected' => '<strong>Test mode:</strong> use the test VISA card 4242424242424242 with any expiry date and CVC. Other payment methods may redirect to a Stripe test page to authorize payment. More test card numbers are listed <a href="https://docs.stripe.com/testing" target="_blank">here</a>.',
147+
'optimized checkout setting' => false,
148+
'expected' => '<strong>Test mode:</strong> use the test VISA card 4242424242424242 with any expiry date and CVC. Other payment methods may redirect to a Stripe test page to authorize payment. More test card numbers are listed <a href="https://docs.stripe.com/testing" target="_blank">here</a>.',
144149
],
145150
];
146151
}

tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Method_Test.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use WC_Stripe_UPE_Payment_Method_CC;
2828
use WC_Stripe_UPE_Payment_Method_Link;
2929
use WC_Stripe_UPE_Payment_Method_Wechat_Pay;
30+
use WooCommerce\Stripe\Tests\Helpers\OC_Test_Helper;
3031
use WooCommerce\Stripe\Tests\WC_Mock_Stripe_API_Unit_Test_Case;
3132

3233
/**
@@ -153,9 +154,9 @@ class WC_Stripe_UPE_Payment_Method_Test extends WC_Mock_Stripe_API_Unit_Test_Cas
153154
* Base template for Stripe AU BECS Debit Pay payment method.
154155
*/
155156
const MOCK_BECS_DEBIT_PAYMENT_METHOD_TEMPLATE = [
156-
'id' => 'pm_mock_payment_method_id',
157-
'type' => WC_Stripe_Payment_Methods::BECS_DEBIT,
158-
WC_Stripe_Payment_Methods::BECS_DEBIT => [
157+
'id' => 'pm_mock_payment_method_id',
158+
'type' => WC_Stripe_Payment_Methods::BECS_DEBIT,
159+
WC_Stripe_Payment_Methods::BECS_DEBIT => [
159160
'last4' => '4321',
160161
'fingerprint' => 'F1ng3rpr1n7',
161162
],
@@ -856,12 +857,8 @@ public function test_payment_methods_support_custom_name_and_description() {
856857
update_option( 'woocommerce_stripe_' . $payment_method_id . '_settings', $original_payment_settings );
857858
}
858859

859-
// Test custom description when SPE is enabled. Should be always empty.
860-
update_option( WC_Stripe_Feature_Flags::OC_FEATURE_FLAG_NAME, 'yes' );
861-
862-
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
863-
$stripe_settings['optimized_checkout_element'] = 'yes';
864-
WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings );
860+
// Test custom description when OC is enabled. Should be always empty.
861+
OC_Test_Helper::enable_oc();
865862

866863
$payment_method_id = WC_Stripe_Payment_Methods::CARD;
867864
$custom_description = 'Custom description for ' . $payment_method_id;
@@ -882,7 +879,13 @@ public function test_payment_methods_support_custom_name_and_description() {
882879
)
883880
->getMock();
884881

885-
$this->assertEmpty( $mocked_payment_method->get_description() );
882+
$actual = $mocked_payment_method->get_description();
883+
884+
// Clean up.
885+
OC_Test_Helper::disable_oc();
886+
update_option( 'woocommerce_stripe_' . $payment_method_id . '_settings', $original_payment_settings );
887+
888+
$this->assertEmpty( $actual );
886889
}
887890

888891
/**

tests/phpunit/WC_Stripe_Payment_Gateway_Test.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
use WC_Gateway_Stripe_Giropay;
99
use WC_Stripe_Customer;
1010
use WC_Stripe_Exception;
11-
use WC_Stripe_Feature_Flags;
1211
use WC_Stripe_Helper;
12+
use WooCommerce\Stripe\Tests\Helpers\OC_Test_Helper;
13+
use WooCommerce\Stripe\Tests\Helpers\OCTest_Helper;
1314
use WooCommerce\Stripe\Tests\Helpers\WC_Helper_Order;
1415
use WP_Error;
1516
use WP_UnitTestCase;
@@ -838,19 +839,23 @@ public function test_process_refund_voids_pre_auth_on_cancel() {
838839
* @dataProvider provide_test_payment_icons
839840
*/
840841
public function test_payment_icons( $optimized_checkout_enabled, $filter, $expected ) {
841-
update_option( WC_Stripe_Feature_Flags::OC_FEATURE_FLAG_NAME, $optimized_checkout_enabled ? 'yes' : 'no' );
842-
843-
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
844-
$stripe_settings['optimized_checkout_element'] = $optimized_checkout_enabled ? 'yes' : 'no';
845-
WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings );
842+
if ( $optimized_checkout_enabled ) {
843+
OC_Test_Helper::enable_oc();
844+
}
846845

847846
if ( $filter ) {
848847
add_filter( 'wc_stripe_payment_icons', $filter );
849-
} else {
850-
remove_filter( 'wc_stripe_payment_icons', [] );
851848
}
852849

853-
$this->assertSame( $expected, $this->gateway->payment_icons() );
850+
$gateway = new WC_Gateway_Stripe();
851+
$actual = $gateway->payment_icons();
852+
// Clean up
853+
OC_Test_Helper::disable_oc();
854+
if ( $filter ) {
855+
remove_filter( 'wc_stripe_payment_icons', $filter );
856+
}
857+
858+
$this->assertSame( $expected, $actual );
854859
}
855860

856861
/**
@@ -864,7 +869,7 @@ public function provide_test_payment_icons() {
864869
};
865870

866871
return [
867-
'default' => [
872+
'default' => [
868873
'optimized checkout enabled' => false,
869874
'filter' => null,
870875
'expected' => [
@@ -892,8 +897,8 @@ public function provide_test_payment_icons() {
892897
],
893898
'Optimized Checkout enabled' => [
894899
'optimized checkout enabled' => true,
895-
'filter' => null,
896-
'expected' => [
900+
'filter' => null,
901+
'expected' => [
897902
'us_bank_account' => '<img src="' . WC_STRIPE_PLUGIN_URL . '/assets/images/bank-debit.svg" class="stripe-ach-icon stripe-icon" alt="ACH" />',
898903
'acss_debit' => '<img src="' . WC_STRIPE_PLUGIN_URL . '/assets/images/bank-debit.svg" class="stripe-ach-icon stripe-icon" alt="Pre-Authorized Debit" />',
899904
'alipay' => '<img src="' . WC_STRIPE_PLUGIN_URL . '/assets/images/alipay.svg" class="stripe-alipay-icon stripe-icon" alt="Alipay" />',
@@ -916,10 +921,10 @@ public function provide_test_payment_icons() {
916921
'cashapp' => '<img src="' . WC_STRIPE_PLUGIN_URL . '/assets/images/cashapp.svg" class="stripe-cashapp-icon stripe-icon" alt="Cash App Pay" />',
917922
],
918923
],
919-
'filter applied' => [
924+
'filter applied' => [
920925
'optimized checkout enabled' => false,
921-
'filter' => $mocked_filter,
922-
'expected' => [],
926+
'filter' => $mocked_filter,
927+
'expected' => [],
923928
],
924929
];
925930
}

0 commit comments

Comments
 (0)