From e3855c4872aa69e92ea08420b490d6503081346d Mon Sep 17 00:00:00 2001 From: Eduardo Umpierre Date: Wed, 13 Nov 2024 18:19:19 -0300 Subject: [PATCH] Add client data to compatibility data hook --- includes/class-compatibility-service.php | 14 +++-- ...s-wc-rest-payments-settings-controller.php | 2 +- .../unit/test-class-compatibility-service.php | 52 +++++++++++++++++-- 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/includes/class-compatibility-service.php b/includes/class-compatibility-service.php index 7f0fd6f7162..2bc83904a8f 100644 --- a/includes/class-compatibility-service.php +++ b/includes/class-compatibility-service.php @@ -74,7 +74,16 @@ public function update_compatibility_data() { * @return void */ public function update_compatibility_data_hook() { - $this->payments_api_client->update_compatibility_data( $this->get_compatibility_data() ); + $compatibility_data = array_merge( + $this->get_compatibility_data(), + [ + 'sift_session_id' => $this->session_service->get_sift_session_id(), + 'ip_address' => \WC_Geolocation::get_ip_address(), + 'browser' => $this->get_browser_info(), + ], + ); + + $this->payments_api_client->update_compatibility_data( $compatibility_data ); } /** @@ -125,9 +134,6 @@ private function get_compatibility_data(): array { 'blog_theme' => get_stylesheet(), 'active_plugins' => $active_plugins, 'post_types_count' => $post_types_count, - 'sift_session_id' => $this->session_service->get_sift_session_id(), - 'ip_address' => \WC_Geolocation::get_ip_address(), - 'browser' => $this->get_browser_info(), ]; } 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..ca3dd061a68 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 @@ -127,7 +127,7 @@ public function set_up() { $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 ); $token_service = new WC_Payments_Token_Service( $this->mock_api_client, $customer_service ); - $compatibility_service = new Compatibility_Service( $this->mock_api_client ); + $compatibility_service = new Compatibility_Service( $this->mock_api_client, $this->mock_session_service ); $action_scheduler_service = new WC_Payments_Action_Scheduler_Service( $this->mock_api_client, $order_service, $compatibility_service ); $mock_rate_limiter = $this->createMock( Session_Rate_Limiter::class ); $mock_dpps = $this->createMock( Duplicate_Payment_Prevention_Service::class ); diff --git a/tests/unit/test-class-compatibility-service.php b/tests/unit/test-class-compatibility-service.php index 6217b1ce710..44b664c16a5 100644 --- a/tests/unit/test-class-compatibility-service.php +++ b/tests/unit/test-class-compatibility-service.php @@ -19,6 +19,13 @@ class Compatibility_Service_Test extends WCPAY_UnitTestCase { */ private $mock_api_client; + /** + * Mock WC_Payments_Session_Service. + * + * @var WC_Payments_Session_Service|MockObject + */ + private $mock_session_service; + /** * Compatibility_Service. * @@ -69,7 +76,8 @@ public function set_up() { parent::set_up(); $this->mock_api_client = $this->createMock( WC_Payments_API_Client::class ); - $this->compatibility_service = new Compatibility_Service( $this->mock_api_client ); + $this->mock_session_service = $this->createMock( WC_Payments_Session_Service::class ); + $this->compatibility_service = new Compatibility_Service( $this->mock_api_client, $this->mock_session_service ); $this->compatibility_service->init_hooks(); $this->add_stylesheet_filter(); @@ -167,7 +175,7 @@ public function test_update_compatibility_data_adds_a_single_scheduled_job() { public function test_update_compatibility_data_hook_active_plugins_false() { // Arrange: Create the expected value to be passed to update_compatibility_data. - $expected = $this->get_mock_compatibility_data( + $expected = $this->get_mock_compatibility_hook_data( [ 'active_plugins' => [], ] @@ -196,7 +204,7 @@ public function test_update_compatibility_data_hook_active_plugins_false() { */ public function test_update_compatibility_data_hook_permalinks_not_set( $page_name ) { // Arrange: Create the expected value to be passed to update_compatibility_data. - $expected = $this->get_mock_compatibility_data( + $expected = $this->get_mock_compatibility_hook_data( [ 'woocommerce_' . $page_name => 'Not set', ] @@ -255,6 +263,44 @@ private function get_mock_compatibility_data( array $args = [] ): array { ); } + /** + * Returns the mock compatibility hook data. + * + * @param array $args If any values need to be overridden, the values can be added here. + * + * @return array + */ + private function get_mock_compatibility_hook_data( array $args = [] ): array { + $user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36'; + $accept_language = 'en-US'; + $content_language = 'en-US'; + $sift_session_id = '12345'; + + wp_set_current_user( 1 ); + wp_get_current_user()->locale = $content_language; + + $_SERVER['HTTP_USER_AGENT'] = $user_agent; + $_SERVER['HTTP_ACCEPT_LANGUAGE'] = $accept_language; + + $this->mock_session_service + ->method( 'get_sift_session_id' ) + ->willReturn( $sift_session_id ); + + return array_merge( + $this->get_mock_compatibility_data(), + [ + 'sift_session_id' => $sift_session_id, + 'ip_address' => '127.0.0.1', + 'browser' => [ + 'user_agent' => $user_agent, + 'accept_language' => $accept_language, + 'content_language' => $content_language, + ], + ], + $args, + ); + } + /** * Adds a filter for the theme/stylesheet name. * Will use the default defined in the test class if no params passed.