diff --git a/changelog/fix-9787-woopay-enable-state-settings b/changelog/fix-9787-woopay-enable-state-settings new file mode 100644 index 00000000000..cee183680df --- /dev/null +++ b/changelog/fix-9787-woopay-enable-state-settings @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Consider WooPay eligibility when retrieving WooPay enable state in the settings. diff --git a/includes/admin/class-wc-rest-payments-settings-controller.php b/includes/admin/class-wc-rest-payments-settings-controller.php index 012604733b9..dfd6e76f005 100644 --- a/includes/admin/class-wc-rest-payments-settings-controller.php +++ b/includes/admin/class-wc-rest-payments-settings-controller.php @@ -513,7 +513,7 @@ public function get_settings(): WP_REST_Response { 'payment_request_button_border_radius' => $this->wcpay_gateway->get_option( 'payment_request_button_border_radius', WC_Payments_Express_Checkout_Button_Handler::DEFAULT_BORDER_RADIUS_IN_PX ), 'is_saved_cards_enabled' => $this->wcpay_gateway->is_saved_cards_enabled(), 'is_card_present_eligible' => $this->wcpay_gateway->is_card_present_eligible() && isset( WC()->payment_gateways()->get_available_payment_gateways()['cod'] ), - 'is_woopay_enabled' => 'yes' === $this->wcpay_gateway->get_option( 'platform_checkout' ), + 'is_woopay_enabled' => WC_Payments_Features::is_woopay_eligible() && 'yes' === $this->wcpay_gateway->get_option( 'platform_checkout' ), 'show_woopay_incompatibility_notice' => get_option( 'woopay_invalid_extension_found', false ), 'woopay_custom_message' => $this->wcpay_gateway->get_option( 'platform_checkout_custom_message' ), 'woopay_store_logo' => $this->wcpay_gateway->get_option( 'platform_checkout_store_logo' ), diff --git a/tests/unit/admin/test-class-wc-rest-payments-settings-controller.php b/tests/unit/admin/test-class-wc-rest-payments-settings-controller.php index d68c5c1f82e..459a6a7bf08 100644 --- a/tests/unit/admin/test-class-wc-rest-payments-settings-controller.php +++ b/tests/unit/admin/test-class-wc-rest-payments-settings-controller.php @@ -73,7 +73,7 @@ class WC_REST_Payments_Settings_Controller_Test extends WCPAY_UnitTestCase { /** * @var Database_Cache|MockObject */ - private $mock_db_cache; + private $mock_cache; /** * WC_Payments_Localization_Service instance. @@ -117,15 +117,19 @@ public function set_up() { // Set the user so that we can pass the authentication. wp_set_current_user( 1 ); + // Mock the main class's cache service. + $this->_cache = WC_Payments::get_database_cache(); + $this->mock_cache = $this->createMock( Database_Cache::class ); + WC_Payments::set_database_cache( $this->mock_cache ); + $this->mock_api_client = $this->getMockBuilder( WC_Payments_API_Client::class ) ->disableOriginalConstructor() ->getMock(); $this->mock_wcpay_account = $this->createMock( WC_Payments_Account::class ); - $this->mock_db_cache = $this->createMock( Database_Cache::class ); $this->mock_session_service = $this->createMock( WC_Payments_Session_Service::class ); $order_service = new WC_Payments_Order_Service( $this->mock_api_client ); - $customer_service = new WC_Payments_Customer_Service( $this->mock_api_client, $this->mock_wcpay_account, $this->mock_db_cache, $this->mock_session_service, $order_service ); + $customer_service = new WC_Payments_Customer_Service( $this->mock_api_client, $this->mock_wcpay_account, $this->mock_cache, $this->mock_session_service, $order_service ); $token_service = new WC_Payments_Token_Service( $this->mock_api_client, $customer_service ); $compatibility_service = new Compatibility_Service( $this->mock_api_client ); $action_scheduler_service = new WC_Payments_Action_Scheduler_Service( $this->mock_api_client, $order_service, $compatibility_service ); @@ -205,6 +209,8 @@ public function set_up() { public function tear_down() { parent::tear_down(); WC_Blocks_REST_API_Registration_Preventer::stop_preventing(); + // Restore the cache service in the main class. + WC_Payments::set_database_cache( $this->_cache ); } public function test_get_settings_request_returns_status_code_200() { @@ -745,6 +751,32 @@ public function test_get_settings_domestic_currency_fallbacks_to_default_currenc $this->assertSame( $this->domestic_currency, $response->get_data()['account_domestic_currency'] ); } + public function test_get_settings_is_woopay_enabled_returns_true(): void { + $current_platform_checkout = $this->gateway->get_option( 'platform_checkout' ); + + $this->gateway->update_option( 'platform_checkout', 'yes' ); + $this->mock_cache->method( 'get' )->willReturn( [ 'platform_checkout_eligible' => true ] ); + + $response = $this->controller->get_settings(); + + $this->assertArrayHasKey( 'is_woopay_enabled', $response->get_data() ); + $this->assertTrue( $response->get_data()['is_woopay_enabled'] ); + $this->gateway->update_option( 'platform_checkout', $current_platform_checkout ); + } + + public function test_get_settings_is_woopay_enabled_returns_false_if_it_is_not_eligible(): void { + $current_platform_checkout = $this->gateway->get_option( 'platform_checkout' ); + + $this->gateway->update_option( 'platform_checkout', 'yes' ); + $this->mock_cache->method( 'get' )->willReturn( [ 'platform_checkout_eligible' => false ] ); + + $response = $this->controller->get_settings(); + + $this->assertArrayHasKey( 'is_woopay_enabled', $response->get_data() ); + $this->assertFalse( $response->get_data()['is_woopay_enabled'] ); + $this->gateway->update_option( 'platform_checkout', $current_platform_checkout ); + } + /** * Tests account business support address validator *