Skip to content

Commit 4af49d1

Browse files
authored
Disable OC when PMC is disabled (#4444)
* Disable OC when PMC is disabled * Unit test * Moving PMC enabled condition out of the feature flags class * Revert test changes * Fix tests * Improving test readability * 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 * Unit tests * Fix tests * New test helper for PMC * Moving the PMC check inside the feature flag method again * Removing unnecessary import * Fix tests * Fix changelog entries * Fix tests
1 parent c9b8091 commit 4af49d1

File tree

8 files changed

+105
-9
lines changed

8 files changed

+105
-9
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
* Add - Introduces a new marketing note to promote BNPLs (Buy Now Pay Later) payment methods (Klarna and Affirm) on WooCommerce admin home page
56
* Fix - Fixes some inconsistencies related to the Optimized Checkout feature and improves its unit tests
67
* 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

includes/class-wc-stripe-feature-flags.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,14 @@ public static function is_spe_available() {
192192
* @return bool
193193
*/
194194
public static function is_oc_available() {
195-
$default_value = self::get_option_with_default( self::OC_FEATURE_FLAG_NAME );
195+
$default_value = self::get_option_with_default( self::OC_FEATURE_FLAG_NAME );
196+
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
197+
$pmc_enabled = $stripe_settings['pmc_enabled'] ?? 'no';
196198
return apply_filters(
197199
'wc_stripe_is_optimized_checkout_available',
198-
'yes' === $default_value,
199-
$default_value
200+
'yes' === $default_value && 'yes' === $pmc_enabled,
201+
$default_value,
202+
$pmc_enabled
200203
);
201204
}
202205
}

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
* Add - Introduces a new marketing note to promote BNPLs (Buy Now Pay Later) payment methods (Klarna and Affirm) on WooCommerce admin home page
116117
* Fix - Fixes some inconsistencies related to the Optimized Checkout feature and improves its unit tests
117118
* 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

tests/e2e/bin/setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ echo " - Activating plugin"
135135
redirect_output cli wp plugin activate woocommerce-gateway-stripe
136136

137137
echo " - Updating WooCommerce Gateway Stripe settings"
138-
redirect_output cli wp option update woocommerce_stripe_settings --format=json "{\"enabled\":\"yes\",\"title\":\"Credit Card (Stripe)\",\"description\":\"Pay with your credit card via Stripe.\",\"api_credentials\":\"\",\"testmode\":\"yes\",\"test_publishable_key\":\"${STRIPE_PUB_KEY}\",\"test_secret_key\":\"${STRIPE_SECRET_KEY}\",\"publishable_key\":\"\",\"secret_key\":\"\",\"webhook\":\"\",\"test_webhook_secret\":\"\",\"webhook_secret\":\"\",\"inline_cc_form\":\"no\",\"statement_descriptor\":\"\",\"short_statement_descriptor\":\"\",\"capture\":\"yes\",\"payment_request\":\"yes\",\"payment_request_button_type\":\"buy\",\"payment_request_button_theme\":\"dark\",\"payment_request_button_locations\":[\"product\",\"cart\",\"checkout\"],\"payment_request_button_size\":\"default\",\"saved_cards\":\"yes\",\"logging\":\"no\",\"upe_checkout_experience_enabled\":\"yes\"}"
138+
redirect_output cli wp option update woocommerce_stripe_settings --format=json "{\"enabled\":\"yes\",\"title\":\"Credit Card (Stripe)\",\"description\":\"Pay with your credit card via Stripe.\",\"api_credentials\":\"\",\"testmode\":\"yes\",\"test_publishable_key\":\"${STRIPE_PUB_KEY}\",\"test_secret_key\":\"${STRIPE_SECRET_KEY}\",\"publishable_key\":\"\",\"secret_key\":\"\",\"webhook\":\"\",\"test_webhook_secret\":\"\",\"webhook_secret\":\"\",\"inline_cc_form\":\"no\",\"statement_descriptor\":\"\",\"short_statement_descriptor\":\"\",\"capture\":\"yes\",\"payment_request\":\"yes\",\"payment_request_button_type\":\"buy\",\"payment_request_button_theme\":\"dark\",\"payment_request_button_locations\":[\"product\",\"cart\",\"checkout\"],\"payment_request_button_size\":\"default\",\"saved_cards\":\"yes\",\"logging\":\"no\",\"upe_checkout_experience_enabled\":\"yes\",\"test_connection_type\":\"connect\"}"
139139

140140
echo " - Enabling the ACH feature flag"
141141
redirect_output cli wp option update _wcstripe_feature_lpm_ach 'yes'
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace WooCommerce\Stripe\Tests\Helpers;
4+
5+
use WC_Stripe_Database_Cache;
6+
use WC_Stripe_Helper;
7+
use WC_Stripe_Payment_Method_Configurations;
8+
use WC_Stripe_Payment_Methods;
9+
10+
/**
11+
* Provides useful methods to test logic related to the Payment Method Configuration API.
12+
*/
13+
class PMC_Test_Helper {
14+
/**
15+
* Enables the Payment Method Configuration API for testing purposes.
16+
*
17+
* @return void
18+
*/
19+
public static function enable_pmc() {
20+
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
21+
$stripe_settings['pmc_enabled'] = 'yes';
22+
WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings );
23+
}
24+
25+
/**
26+
* Disables the Payment Method Configuration API for testing purposes.
27+
*
28+
* @return void
29+
*/
30+
public static function disable_pmc() {
31+
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
32+
$stripe_settings['pmc_enabled'] = 'no';
33+
WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings );
34+
}
35+
36+
/**
37+
* Caches a mocked payment method configuration for testing purposes.
38+
*
39+
* @return void
40+
*/
41+
public static function cache_mocked_configuration() {
42+
$payment_method_configuration = [
43+
'id' => 'pmc_abcdef',
44+
'object' => 'payment_method_configuration',
45+
'active' => true,
46+
'parent' => WC_Stripe_Payment_Method_Configurations::TEST_MODE_CONFIGURATION_PARENT_ID,
47+
'livemode' => false,
48+
WC_Stripe_Payment_Methods::CARD => (object) [
49+
'display_preference' => (object) [ 'value' => 'on' ],
50+
],
51+
];
52+
WC_Stripe_Database_Cache::set( WC_Stripe_Payment_Method_Configurations::CONFIGURATION_CACHE_KEY, $payment_method_configuration );
53+
}
54+
55+
/**
56+
* Deletes the cached payment method configuration for testing purposes.
57+
*
58+
* @return void
59+
*/
60+
public static function delete_cached_configuration() {
61+
WC_Stripe_Database_Cache::delete( WC_Stripe_Payment_Method_Configurations::CONFIGURATION_CACHE_KEY );
62+
}
63+
}

tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use Automattic\WooCommerce\Enums\OrderStatus;
66
use Exception;
77
use WooCommerce\Stripe\Tests\Helpers\OC_Test_Helper;
8+
use WC_Stripe_Database_Cache;
9+
use WC_Stripe_Payment_Method_Configurations;
10+
use WooCommerce\Stripe\Tests\Helpers\PMC_Test_Helper;
811
use WooCommerce\Stripe\Tests\Helpers\UPE_Test_Helper;
912
use WC_Data_Exception;
1013
use WC_Order;
@@ -2893,13 +2896,21 @@ public function payment_method_titles_provider() {
28932896
/**
28942897
* Test that a failed payment intent is not reused and a new one is created instead.
28952898
*
2899+
* @param bool $pmc_enabled Whether the payment method configurations are enabled.
28962900
* @param bool $setting_enabled Whether the optimized checkout setting is enabled.
28972901
* @param bool $expected The expected result of the `is_oc_enabled` method.
28982902
* @return void
28992903
*
29002904
* @dataProvider provide_test_is_oc_enabled
29012905
*/
2902-
public function test_is_oc_enabled( $setting_enabled, $expected ) {
2906+
public function test_is_oc_enabled( $pmc_enabled, $setting_enabled, $expected ) {
2907+
if ( $pmc_enabled ) {
2908+
PMC_Test_Helper::enable_pmc();
2909+
2910+
// Mock the payment method configuration for the test, to avoid it being disabled by default.
2911+
PMC_Test_Helper::cache_mocked_configuration();
2912+
}
2913+
29032914
if ( $setting_enabled ) {
29042915
OC_Test_Helper::enable_oc();
29052916
}
@@ -2908,6 +2919,8 @@ public function test_is_oc_enabled( $setting_enabled, $expected ) {
29082919
$actual = $gateway->is_oc_enabled();
29092920

29102921
// Clean up
2922+
PMC_Test_Helper::disable_pmc();
2923+
PMC_Test_Helper::delete_cached_configuration();
29112924
OC_Test_Helper::disable_oc();
29122925

29132926
$this->assertSame( $expected, $actual );
@@ -2920,13 +2933,20 @@ public function test_is_oc_enabled( $setting_enabled, $expected ) {
29202933
*/
29212934
public function provide_test_is_oc_enabled() {
29222935
return [
2923-
'Disabled' => [
2936+
'Disabled (all disabled)' => [
2937+
'pmc enabled' => false,
2938+
'setting value' => false,
2939+
'expected' => false,
2940+
],
2941+
'Disabled (pmc enabled)' => [
2942+
'pmc enabled' => true,
29242943
'setting value' => false,
29252944
'expected' => false,
29262945
],
2927-
'Enabled' => [
2946+
'Enabled' => [
2947+
'pmc enabled' => true,
29282948
'setting value' => true,
2929-
'expected' => true,
2949+
'expected' => true,
29302950
],
29312951
];
29322952
}

tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Method_Test.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
use WC_Payment_Token_SEPA;
1414
use WC_Stripe;
1515
use WC_Stripe_Currency_Code;
16+
use WC_Stripe_Database_Cache;
1617
use WC_Stripe_Feature_Flags;
1718
use WC_Stripe_Helper;
19+
use WC_Stripe_Payment_Method_Configurations;
1820
use WC_Stripe_Payment_Methods;
1921
use WC_Stripe_Payment_Token_CC;
2022
use WC_Stripe_UPE_Payment_Gateway;
@@ -28,6 +30,7 @@
2830
use WC_Stripe_UPE_Payment_Method_Link;
2931
use WC_Stripe_UPE_Payment_Method_Wechat_Pay;
3032
use WooCommerce\Stripe\Tests\Helpers\OC_Test_Helper;
33+
use WooCommerce\Stripe\Tests\Helpers\PMC_Test_Helper;
3134
use WooCommerce\Stripe\Tests\WC_Mock_Stripe_API_Unit_Test_Case;
3235

3336
/**
@@ -867,6 +870,10 @@ public function test_payment_methods_support_custom_name_and_description() {
867870
$updated_payment_settings['description'] = $custom_description;
868871
update_option( 'woocommerce_stripe_' . $payment_method_id . '_settings', $updated_payment_settings );
869872

873+
PMC_Test_Helper::enable_pmc();
874+
// Mock the payment method configuration for the test, to avoid it being disabled by default.
875+
PMC_Test_Helper::cache_mocked_configuration();
876+
870877
$mocked_payment_method = $this->getMockBuilder( WC_Stripe_UPE_Payment_Method_CC::class )
871878
->setMethods(
872879
[
@@ -883,6 +890,8 @@ public function test_payment_methods_support_custom_name_and_description() {
883890

884891
// Clean up.
885892
OC_Test_Helper::disable_oc();
893+
PMC_Test_Helper::disable_pmc();
894+
PMC_Test_Helper::delete_cached_configuration();
886895
update_option( 'woocommerce_stripe_' . $payment_method_id . '_settings', $original_payment_settings );
887896

888897
$this->assertEmpty( $actual );

tests/phpunit/WC_Stripe_Payment_Gateway_Test.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use WC_Stripe_Exception;
1111
use WC_Stripe_Helper;
1212
use WooCommerce\Stripe\Tests\Helpers\OC_Test_Helper;
13-
use WooCommerce\Stripe\Tests\Helpers\OCTest_Helper;
1413
use WooCommerce\Stripe\Tests\Helpers\WC_Helper_Order;
1514
use WP_Error;
1615
use WP_UnitTestCase;

0 commit comments

Comments
 (0)