From 4dbc8aa74b7c23335f5b276a6a89b8ac2618e9f7 Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Wed, 28 Aug 2024 10:44:28 +0800 Subject: [PATCH] Refactor WooCommercePayments task to use woo payment gateway from`WC()->payment_gateways->payment_gateways()` (#50953) * Fake woopayments gateway is_account_partially_onboarded return false * Refactor WooCommercePayments class to use get_woo_payments_gateway() function * Add changefile(s) from automation for the following project(s): woocommerce-beta-tester, woocommerce * Remove changelog * Add unit tests * Update changelog --------- Co-authored-by: github-actions --- .../api/tools/class-fake-wcpayments.php | 11 ++ .../changelog/update-woo-pay-task | 4 + .../changelog/50953-update-woo-pay-task | 4 + .../Tasks/WooCommercePayments.php | 29 +-- .../tasks/woocommerce-payments.php | 169 ++++++++++++++++++ 5 files changed, 207 insertions(+), 10 deletions(-) create mode 100644 plugins/woocommerce-beta-tester/changelog/update-woo-pay-task create mode 100644 plugins/woocommerce/changelog/50953-update-woo-pay-task create mode 100644 plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/features/onboarding-tasks/tasks/woocommerce-payments.php diff --git a/plugins/woocommerce-beta-tester/api/tools/class-fake-wcpayments.php b/plugins/woocommerce-beta-tester/api/tools/class-fake-wcpayments.php index 01fa658c3a4..7292cc43e74 100644 --- a/plugins/woocommerce-beta-tester/api/tools/class-fake-wcpayments.php +++ b/plugins/woocommerce-beta-tester/api/tools/class-fake-wcpayments.php @@ -59,4 +59,15 @@ class Fake_WCPayments extends WC_Payment_Gateway_WCPay { public function is_available() { return true; } + + /** + * Checks if the account has not completed onboarding due to users abandoning the process half way. + * Also used by WC Core to complete the task "Set up WooPayments". + * Called directly by WooCommerce Core. + * + * @return bool + */ + public function is_account_partially_onboarded(): bool { + return false; + } } diff --git a/plugins/woocommerce-beta-tester/changelog/update-woo-pay-task b/plugins/woocommerce-beta-tester/changelog/update-woo-pay-task new file mode 100644 index 00000000000..8a1b90226ac --- /dev/null +++ b/plugins/woocommerce-beta-tester/changelog/update-woo-pay-task @@ -0,0 +1,4 @@ +Significance: patch +Type: update + +Update Fake_WCPayments is_account_partially_onboarded to return false diff --git a/plugins/woocommerce/changelog/50953-update-woo-pay-task b/plugins/woocommerce/changelog/50953-update-woo-pay-task new file mode 100644 index 00000000000..48e96069d4e --- /dev/null +++ b/plugins/woocommerce/changelog/50953-update-woo-pay-task @@ -0,0 +1,4 @@ +Significance: patch +Type: update + +Refactor WooCommercePayments task to use woo payment gateway from `WC()->payment_gateways->payment_gateways()` diff --git a/plugins/woocommerce/src/Admin/Features/OnboardingTasks/Tasks/WooCommercePayments.php b/plugins/woocommerce/src/Admin/Features/OnboardingTasks/Tasks/WooCommercePayments.php index 5a269ca620f..5c6d45255f6 100644 --- a/plugins/woocommerce/src/Admin/Features/OnboardingTasks/Tasks/WooCommercePayments.php +++ b/plugins/woocommerce/src/Admin/Features/OnboardingTasks/Tasks/WooCommercePayments.php @@ -147,11 +147,9 @@ class WooCommercePayments extends Task { * @return bool */ public static function is_connected() { - if ( class_exists( '\WC_Payments' ) ) { - $wc_payments_gateway = \WC_Payments::get_gateway(); - return method_exists( $wc_payments_gateway, 'is_connected' ) - ? $wc_payments_gateway->is_connected() - : false; + $wc_payments_gateway = self::get_woo_payments_gateway(); + if ( $wc_payments_gateway && method_exists( $wc_payments_gateway, 'is_connected' ) ) { + return $wc_payments_gateway->is_connected(); } return false; @@ -164,11 +162,9 @@ class WooCommercePayments extends Task { * @return bool */ public static function is_account_partially_onboarded() { - if ( class_exists( '\WC_Payments' ) ) { - $wc_payments_gateway = \WC_Payments::get_gateway(); - return method_exists( $wc_payments_gateway, 'is_account_partially_onboarded' ) - ? $wc_payments_gateway->is_account_partially_onboarded() - : false; + $wc_payments_gateway = self::get_woo_payments_gateway(); + if ( $wc_payments_gateway && method_exists( $wc_payments_gateway, 'is_account_partially_onboarded' ) ) { + return $wc_payments_gateway->is_account_partially_onboarded(); } return false; @@ -195,4 +191,17 @@ class WooCommercePayments extends Task { } return false; } + + /** + * Get the WooPayments gateway. + * + * @return \WC_Payments|null + */ + private static function get_woo_payments_gateway() { + $payment_gateways = WC()->payment_gateways->payment_gateways(); + if ( isset( $payment_gateways['woocommerce_payments'] ) ) { + return $payment_gateways['woocommerce_payments']; + } + return null; + } } diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/features/onboarding-tasks/tasks/woocommerce-payments.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/features/onboarding-tasks/tasks/woocommerce-payments.php new file mode 100644 index 00000000000..dc4e1e3e28b --- /dev/null +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/features/onboarding-tasks/tasks/woocommerce-payments.php @@ -0,0 +1,169 @@ +task = new WooCommercePayments( new TaskList() ); + $this->fake_gateway = new Fake_WC_Payments_Gateway(); + } + + /** + * Teardown after each test. + */ + public function tearDown(): void { + parent::tearDown(); + remove_filter( 'woocommerce_payment_gateways', array( $this, 'inject_fake_gateway' ) ); + } + + /** + * Test is_connected method when WooCommerce Payments is connected. + */ + public function test_is_connected_when_wcpay_is_connected() { + $this->mock_wc_payments_gateway( true ); + $this->assertTrue( WooCommercePayments::is_connected() ); + } + + /** + * Test is_connected method when WooCommerce Payments is not connected. + */ + public function test_is_connected_when_wcpay_is_not_connected() { + $this->mock_wc_payments_gateway( false ); + $this->assertFalse( WooCommercePayments::is_connected() ); + } + + /** + * Test is_account_partially_onboarded method when account is partially onboarded. + */ + public function test_is_account_partially_onboarded_when_true() { + $this->mock_wc_payments_gateway( true, true ); + $this->assertTrue( WooCommercePayments::is_account_partially_onboarded() ); + } + + /** + * Test is_account_partially_onboarded method when account is fully onboarded. + */ + public function test_is_account_partially_onboarded_when_false() { + $this->mock_wc_payments_gateway( true, false ); + $this->assertFalse( WooCommercePayments::is_account_partially_onboarded() ); + } + + /** + * Mock the WC_Payments gateway using filters. + * + * @param bool $is_connected Whether the gateway is connected. + * @param bool $is_partially_onboarded Whether the account is partially onboarded. + */ + private function mock_wc_payments_gateway( $is_connected, $is_partially_onboarded = false ) { + $this->fake_gateway->set_connected( $is_connected ); + $this->fake_gateway->set_partially_onboarded( $is_partially_onboarded ); + add_filter( 'woocommerce_payment_gateways', array( $this, 'inject_fake_gateway' ) ); + WC()->payment_gateways()->init(); + } + + /** + * Inject fake gateway filter callback. + * + * @param array $gateways Existing gateways. + * @return array Modified gateways. + */ + public function inject_fake_gateway( $gateways ) { + return array( 'woocommerce_payments' => $this->fake_gateway ); + } +} + +// phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound + +/** + * Fake WooCommerce Payments Gateway class for testing. + */ +class Fake_WC_Payments_Gateway extends WC_Payment_Gateway { + /** + * Whether the gateway is connected. + * + * @var bool + */ + private $connected = false; + + /** + * Whether the account is partially onboarded. + * + * @var bool + */ + private $partially_onboarded = false; + + /** + * Constructor. + */ + public function __construct() { + $this->id = 'woocommerce_payments'; + } + + /** + * Check if the gateway is connected. + * + * @return bool + */ + public function is_connected() { + return $this->connected; + } + + /** + * Check if the account is partially onboarded. + * + * @return bool + */ + public function is_account_partially_onboarded() { + return $this->partially_onboarded; + } + + /** + * Set the connected status. + * + * @param bool $connected Whether the gateway is connected. + */ + public function set_connected( $connected ) { + $this->connected = $connected; + } + + /** + * Set the partially onboarded status. + * + * @param bool $partially_onboarded Whether the account is partially onboarded. + */ + public function set_partially_onboarded( $partially_onboarded ) { + $this->partially_onboarded = $partially_onboarded; + } +}