Skip to content

Commit

Permalink
cleanup settings controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Timur Karimov committed Dec 27, 2023
1 parent 3fa1a93 commit 2122fd9
Showing 1 changed file with 11 additions and 159 deletions.
170 changes: 11 additions & 159 deletions tests/unit/admin/test-class-wc-rest-payments-settings-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use WCPay\Database_Cache;
use WCPay\Duplicate_Payment_Prevention_Service;
use WCPay\Payment_Methods\Eps_Payment_Method;
use WCPay\Payment_Methods\UPE_Payment_Gateway;
use WCPay\Payment_Methods\CC_Payment_Method;
use WCPay\Payment_Methods\Bancontact_Payment_Method;
use WCPay\Payment_Methods\Becs_Payment_Method;
Expand Down Expand Up @@ -65,34 +64,6 @@ class WC_REST_Payments_Settings_Controller_Test extends WCPAY_UnitTestCase {
*/
private $mock_db_cache;

/**
* An array of mocked split UPE payment gateways mapped to payment method ID.
*
* @var UPE_Payment_Gateway
*/
private $mock_upe_payment_gateway;

/**
* An array of mocked split UPE payment gateways mapped to payment method ID.
*
* @var UPE_Payment_Gateway
*/
private $mock_split_upe_payment_gateway;

/**
* UPE system under test.
*
* @var WC_REST_Payments_Settings_Controller
*/
private $upe_controller;

/**
* UPE system under test.
*
* @var WC_REST_Payments_Settings_Controller
*/
private $upe_split_controller;

/**
* WC_Payments_Localization_Service instance.
*
Expand Down Expand Up @@ -186,40 +157,6 @@ public function set_up() {
);
$this->controller = new WC_REST_Payments_Settings_Controller( $this->mock_api_client, $this->gateway, $this->mock_wcpay_account );

$this->mock_upe_payment_gateway = new WC_Payment_Gateway_WCPay(
$this->mock_api_client,
$this->mock_wcpay_account,
$customer_service,
$token_service,
$action_scheduler_service,
$mock_payment_method,
$mock_payment_methods,
$mock_rate_limiter,
$order_service,
$mock_dpps,
$this->mock_localization_service,
$this->mock_fraud_service
);

$this->upe_controller = new WC_REST_Payments_Settings_Controller( $this->mock_api_client, $this->mock_upe_payment_gateway, $this->mock_wcpay_account );

$this->mock_split_upe_payment_gateway = new WC_Payment_Gateway_WCPay(
$this->mock_api_client,
$this->mock_wcpay_account,
$customer_service,
$token_service,
$action_scheduler_service,
$mock_payment_methods['card'],
$mock_payment_methods,
$mock_rate_limiter,
$order_service,
$mock_dpps,
$this->mock_localization_service,
$this->mock_fraud_service
);

$this->upe_split_controller = new WC_REST_Payments_Settings_Controller( $this->mock_api_client, $this->mock_split_upe_payment_gateway, $this->mock_wcpay_account );

$this->mock_api_client
->method( 'is_server_connected' )
->willReturn( true );
Expand Down Expand Up @@ -269,7 +206,7 @@ public function test_get_settings_returns_enabled_payment_method_ids() {
);
}

public function test_upe_get_settings_returns_available_payment_method_ids() {
public function test_get_settings_returns_available_payment_method_ids() {
$this->mock_localization_service->method( 'get_country_locale_data' )->willReturn(
[
'currency_code' => 'usd',
Expand All @@ -295,32 +232,6 @@ public function test_upe_get_settings_returns_available_payment_method_ids() {
);
}

public function test_split_upe_get_settings_returns_available_payment_method_ids() {
$this->mock_localization_service->method( 'get_country_locale_data' )->willReturn(
[
'currency_code' => 'usd',
]
);
$response = $this->upe_split_controller->get_settings();
$enabled_method_ids = $response->get_data()['available_payment_method_ids'];

$this->assertEquals(
[
Payment_Method::CARD,
Payment_Method::BECS,
Payment_Method::BANCONTACT,
Payment_Method::EPS,
Payment_Method::GIROPAY,
Payment_Method::IDEAL,
Payment_Method::SOFORT,
Payment_Method::SEPA,
Payment_Method::P24,
Payment_Method::LINK,
],
$enabled_method_ids
);
}

public function test_get_settings_request_returns_test_mode_flag() {
$this->mock_localization_service->method( 'get_country_locale_data' )->willReturn(
[
Expand Down Expand Up @@ -438,26 +349,15 @@ public function test_update_settings_returns_error_on_non_bool_is_wcpay_enabled_
$this->assertEquals( 400, $response->get_status() );
}

public function test_upe_update_settings_saves_enabled_payment_methods() {
$this->mock_upe_payment_gateway->update_option( 'upe_enabled_payment_method_ids', [ Payment_Method::CARD ] );
public function test_update_settings_saves_enabled_payment_methods() {
$this->gateway->update_option( 'upe_enabled_payment_method_ids', [ Payment_Method::CARD ] );

$request = new WP_REST_Request();
$request->set_param( 'enabled_payment_method_ids', [ Payment_Method::CARD, Payment_Method::GIROPAY ] );

$this->upe_controller->update_settings( $request );

$this->assertEquals( [ Payment_Method::CARD, Payment_Method::GIROPAY ], $this->mock_upe_payment_gateway->get_option( 'upe_enabled_payment_method_ids' ) );
}

public function test_upe_split_update_settings_saves_enabled_payment_methods() {
$this->mock_split_upe_payment_gateway->update_option( 'upe_enabled_payment_method_ids', [ Payment_Method::CARD ] );

$request = new WP_REST_Request();
$request->set_param( 'enabled_payment_method_ids', [ Payment_Method::CARD, Payment_Method::GIROPAY ] );

$this->upe_split_controller->update_settings( $request );
$this->controller->update_settings( $request );

$this->assertEquals( [ Payment_Method::CARD, Payment_Method::GIROPAY ], $this->mock_split_upe_payment_gateway->get_option( 'upe_enabled_payment_method_ids' ) );
$this->assertEquals( [ Payment_Method::CARD, Payment_Method::GIROPAY ], $this->gateway->get_option( 'upe_enabled_payment_method_ids' ) );
}

public function test_update_settings_fails_if_user_cannot_manage_woocommerce() {
Expand Down Expand Up @@ -771,7 +671,7 @@ public function deregister_wc_blocks_rest_api() {
}
}

public function test_upe_get_settings_card_eligible_flag(): void {
public function test_get_settings_card_eligible_flag(): void {
// Enable Cash on Delivery gateway for the purpose of this test.
$cod_gateway = WC()->payment_gateways()->payment_gateways()['cod'];
$cod_gateway->enabled = 'yes';
Expand All @@ -782,26 +682,7 @@ public function test_upe_get_settings_card_eligible_flag(): void {
]
);

$response = $this->upe_controller->get_settings();

$this->assertArrayHasKey( 'is_card_present_eligible', $response->get_data() );
$this->assertTrue( $response->get_data()['is_card_present_eligible'] );

// Disable Cash on Delivery gateway.
$cod_gateway->enabled = 'no';
}

public function test_upe_split_get_settings_card_eligible_flag(): void {
// Enable Cash on Delivery gateway for the purpose of this test.
$cod_gateway = WC()->payment_gateways()->payment_gateways()['cod'];
$cod_gateway->enabled = 'yes';

$this->mock_localization_service->method( 'get_country_locale_data' )->willReturn(
[
'currency_code' => 'usd',
]
);
$response = $this->upe_split_controller->get_settings();
$response = $this->controller->get_settings();

$this->assertArrayHasKey( 'is_card_present_eligible', $response->get_data() );
$this->assertTrue( $response->get_data()['is_card_present_eligible'] );
Expand All @@ -810,7 +691,7 @@ public function test_upe_split_get_settings_card_eligible_flag(): void {
$cod_gateway->enabled = 'no';
}

public function test_upe_get_settings_domestic_currency(): void {
public function test_get_settings_domestic_currency(): void {
$mock_domestic_currency = 'usd';
$this->mock_localization_service->method( 'get_country_locale_data' )->willReturn(
[
Expand All @@ -821,49 +702,20 @@ public function test_upe_get_settings_domestic_currency(): void {
->expects( $this->never() )
->method( 'get_account_default_currency' );

$response = $this->upe_controller->get_settings();

$this->assertArrayHasKey( 'account_domestic_currency', $response->get_data() );
$this->assertSame( $mock_domestic_currency, $response->get_data()['account_domestic_currency'] );
}

public function test_upe_get_settings_domestic_currency_fallbacks_to_default_currency(): void {
$mock_domestic_currency = 'usd';
$this->mock_localization_service->method( 'get_country_locale_data' )->willReturn( [] );
$this->mock_wcpay_account
->expects( $this->once() )
->method( 'get_account_default_currency' )
->willReturn( $mock_domestic_currency );
$response = $this->upe_controller->get_settings();

$this->assertArrayHasKey( 'account_domestic_currency', $response->get_data() );
$this->assertSame( $mock_domestic_currency, $response->get_data()['account_domestic_currency'] );
}

public function test_upe_split_get_settings_domestic_currency(): void {
$mock_domestic_currency = 'usd';
$this->mock_localization_service->method( 'get_country_locale_data' )->willReturn(
[
'currency_code' => $mock_domestic_currency,
]
);
$this->mock_wcpay_account
->expects( $this->never() )
->method( 'get_account_default_currency' );
$response = $this->upe_split_controller->get_settings();
$response = $this->controller->get_settings();

$this->assertArrayHasKey( 'account_domestic_currency', $response->get_data() );
$this->assertSame( $mock_domestic_currency, $response->get_data()['account_domestic_currency'] );
}

public function test_upe_split_get_settings_domestic_currency_fallbacks_to_default_currency(): void {
public function test_get_settings_domestic_currency_fallbacks_to_default_currency(): void {
$mock_domestic_currency = 'usd';
$this->mock_localization_service->method( 'get_country_locale_data' )->willReturn( [] );
$this->mock_wcpay_account
->expects( $this->once() )
->method( 'get_account_default_currency' )
->willReturn( $mock_domestic_currency );
$response = $this->upe_split_controller->get_settings();
$response = $this->controller->get_settings();

$this->assertArrayHasKey( 'account_domestic_currency', $response->get_data() );
$this->assertSame( $mock_domestic_currency, $response->get_data()['account_domestic_currency'] );
Expand Down

0 comments on commit 2122fd9

Please sign in to comment.