Skip to content

Commit

Permalink
Add client data to compatibility data hook
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoumpierre committed Nov 13, 2024
1 parent 7271454 commit e3855c4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
14 changes: 10 additions & 4 deletions includes/class-compatibility-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -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(),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
52 changes: 49 additions & 3 deletions tests/unit/test-class-compatibility-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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' => [],
]
Expand Down Expand Up @@ -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',
]
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit e3855c4

Please sign in to comment.