Skip to content

Commit

Permalink
stop testing checkout class in gateway test suite, avoid dependencies…
Browse files Browse the repository at this point in the history
… and increase test coverage
  • Loading branch information
Timur Karimov committed Dec 18, 2023
1 parent 33200e6 commit b3e3b92
Showing 1 changed file with 27 additions and 141 deletions.
168 changes: 27 additions & 141 deletions tests/unit/test-class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
use WCPay\Payment_Methods\UPE_Split_Payment_Gateway;
use WCPay\WooPay\WooPay_Utilities;
use WCPay\Session_Rate_Limiter;
use WCPay\WC_Payments_UPE_Checkout;

// Need to use WC_Mock_Data_Store.
require_once dirname( __FILE__ ) . '/helpers/class-wc-mock-wc-data-store.php';
Expand Down Expand Up @@ -116,12 +115,6 @@ class WC_Payment_Gateway_WCPay_Test extends WCPAY_UnitTestCase {
*/
private $woopay_utilities;

/**
* WC_Payments_UPE_Checkout instance.
* @var WC_Payments_UPE_Checkout
*/
private $payments_checkout;

/**
* Duplicate_Payment_Prevention_Service instance.
* @var Duplicate_Payment_Prevention_Service|MockObject
Expand Down Expand Up @@ -240,8 +233,6 @@ public function set_up() {

$this->woopay_utilities = new WooPay_Utilities();

$this->payments_checkout = new WC_Payments_UPE_Checkout( $this->wcpay_gateway, $this->woopay_utilities, $this->mock_wcpay_account, $this->mock_customer_service, $this->mock_fraud_service );

// Mock the level3 service to always return an empty array.
$mock_level3_service = $this->createMock( Level3Service::class );
$mock_level3_service->expects( $this->any() )
Expand Down Expand Up @@ -394,83 +385,58 @@ public function test_attach_exchange_info_to_order_with_different_order_currency
$this->assertEquals( 0.853, $order->get_meta( '_wcpay_multi_currency_stripe_exchange_rate' ) );
}

public function test_save_card_checkbox_not_displayed_when_saved_cards_disabled() {
$this->wcpay_gateway->update_option( 'saved_cards', 'no' );

$this->refresh_payments_checkout();

public function test_save_payment_method_checkbox_displayed() {
// Use a callback to get and test the output (also suppresses the output buffering being printed to the CLI).
$this->setOutputCallback(
function ( $output ) {
$result = preg_match_all( '/.*<input.*id="wc-woocommerce_payments-new-payment-method".*\/>.*/', $output );
$input_element = preg_match_all( '/.*<input.*id="wc-woocommerce_payments-new-payment-method".*\/>.*/', $output );
$parent_div = preg_match_all( '/<div >.*<\/div>/s', $output );

$this->assertSame( 0, $result );
$this->assertSame( 1, $input_element );
$this->assertSame( 1, $parent_div );
}
);

$this->wcpay_gateway->payment_fields();
$this->wcpay_gateway->save_payment_method_checkbox();
}

public function test_save_card_checkbox_not_displayed_for_non_logged_in_users() {
$this->wcpay_gateway->update_option( 'saved_cards', 'yes' );
wp_set_current_user( 0 );

$this->refresh_payments_checkout();

// Use a callback to get and test the output (also suppresses the output buffering being printed to the CLI).
public function test_save_payment_method_checkbox_not_displayed_when_force_checked() {
$this->setOutputCallback(
function ( $output ) {
$result = preg_match_all( '/.*<input.*id="wc-woocommerce_payments-new-payment-method".*\/>.*/', $output );
$input_element = preg_match_all( '/.*<input.*id="wc-woocommerce_payments-new-payment-method".*\/>.*/', $output );
$parent_div = preg_match_all( '/<div style="display:none;">.*<\/div>/s', $output );

$this->assertSame( 0, $result );
$this->assertSame( 1, $input_element );
$this->assertSame( 1, $parent_div );
}
);

$this->wcpay_gateway->payment_fields();
$this->wcpay_gateway->save_payment_method_checkbox( true );
}

public function test_save_card_checkbox_displayed_for_logged_in_users() {
$this->wcpay_gateway->update_option( 'saved_cards', 'yes' );
wp_set_current_user( 1 );

$this->refresh_payments_checkout();
public function test_save_payment_method_checkbox_not_displayed_when_stripe_platform_account_used() {
// Setup the test so that should_use_stripe_platform_on_checkout_page returns true.
$this->mock_cache->method( 'get' )->willReturn( [ 'platform_checkout_eligible' => true ] );
$this->wcpay_gateway->update_option( 'platform_checkout', 'yes' );
add_filter( 'woocommerce_is_checkout', '__return_true' );
WC()->session->init();
WC()->cart->add_to_cart( WC_Helper_Product::create_simple_product()->get_id(), 1 );
WC()->cart->calculate_totals();

// Use a callback to get and test the output (also suppresses the output buffering being printed to the CLI).
$this->setOutputCallback(
function ( $output ) {
$result = preg_match_all( '/.*<input.*id="wc-woocommerce_payments-new-payment-method".*\/>.*/', $output );
$input_element = preg_match_all( '/.*<input.*id="wc-woocommerce_payments-new-payment-method".*\/>.*/', $output );
$parent_div = preg_match_all( '/<div style="display:none;">.*<\/div>/s', $output );

$this->assertSame( 1, $result );
$this->assertSame( 1, $input_element );
$this->assertSame( 1, $parent_div );
}
);

$this->wcpay_gateway->payment_fields();
}

public function test_fraud_prevention_token_added_when_enabled() {
$token_value = 'test-token';
$fraud_prevention_service_mock = $this->get_fraud_prevention_service_mock();
$fraud_prevention_service_mock
->expects( $this->once() )
->method( 'is_enabled' )
->willReturn( true );
$fraud_prevention_service_mock
->expects( $this->once() )
->method( 'get_token' )
->willReturn( $token_value );

$this->refresh_payments_checkout();
$this->wcpay_gateway->save_payment_method_checkbox( false );

// Use a callback to get and test the output (also suppresses the output buffering being printed to the CLI).
$this->setOutputCallback(
function ( $output ) use ( $token_value ) {
$result = preg_match_all( '/.*<input.*type="hidden".*name="wcpay-fraud-prevention-token".*value="' . $token_value . '".*\/>.*/', $output );

$this->assertSame( 0, $result );
}
);

$this->wcpay_gateway->payment_fields();
remove_filter( 'woocommerce_is_checkout', '__return_true' );
WC()->cart->empty_cart();
}

public function test_capture_charge_success() {
Expand Down Expand Up @@ -1859,7 +1825,6 @@ public function test_is_woopay_enabled_returns_true() {
add_filter( 'woocommerce_is_checkout', '__return_true' );

$this->assertTrue( $this->woopay_utilities->should_enable_woopay( $this->wcpay_gateway ) );
$this->assertTrue( $this->payments_checkout->get_payment_fields_js_config()['isWooPayEnabled'] );

remove_filter( 'woocommerce_is_checkout', '__return_true' );
}
Expand All @@ -1876,79 +1841,6 @@ public function test_should_use_stripe_platform_on_checkout_page_not_woopay() {
$this->assertFalse( $this->wcpay_gateway->should_use_stripe_platform_on_checkout_page() );
}

public function test_force_network_saved_cards_is_returned_as_true_if_should_use_stripe_platform() {
$mock_wcpay_gateway = $this->get_partial_mock_for_gateway( [ 'should_use_stripe_platform_on_checkout_page' ] );

$mock_wcpay_gateway
->expects( $this->once() )
->method( 'should_use_stripe_platform_on_checkout_page' )
->willReturn( true );

$registered_card_gateway = WC_Payments::get_registered_card_gateway();
WC_Payments::set_registered_card_gateway( $mock_wcpay_gateway );

$payments_checkout = new WC_Payments_UPE_Checkout(
$mock_wcpay_gateway,
$this->woopay_utilities,
$this->mock_wcpay_account,
$this->mock_customer_service,
$this->mock_fraud_service
);
$payments_checkout->init_hooks();

$this->assertTrue( $payments_checkout->get_payment_fields_js_config()['forceNetworkSavedCards'] );
WC_Payments::set_registered_card_gateway( $registered_card_gateway );
}

public function test_registered_gateway_replaced_by_gateway_if_not_initialized() {
// Set the registered gateway to null to simulate the case where the gateway is not initialized and thus should be replaced by $mock_wcpay_gateway.
WC_Payments::set_registered_card_gateway( null );

$mock_wcpay_gateway = $this->get_partial_mock_for_gateway( [ 'should_use_stripe_platform_on_checkout_page' ] );

$mock_wcpay_gateway
->expects( $this->once() )
->method( 'should_use_stripe_platform_on_checkout_page' )
->willReturn( true );

$payments_checkout = new WC_Payments_UPE_Checkout( $mock_wcpay_gateway, $this->woopay_utilities, $this->mock_wcpay_account, $this->mock_customer_service, $this->mock_fraud_service );

$this->assertTrue( $payments_checkout->get_payment_fields_js_config()['forceNetworkSavedCards'] );
}

public function test_force_network_saved_cards_is_returned_as_false_if_should_not_use_stripe_platform() {
$mock_wcpay_gateway = $this->get_partial_mock_for_gateway( [ 'should_use_stripe_platform_on_checkout_page' ] );

$mock_wcpay_gateway
->expects( $this->once() )
->method( 'should_use_stripe_platform_on_checkout_page' )
->willReturn( false );

$registered_card_gateway = WC_Payments::get_registered_card_gateway();
WC_Payments::set_registered_card_gateway( $mock_wcpay_gateway );

$payments_checkout = new WC_Payments_UPE_Checkout( $mock_wcpay_gateway, $this->woopay_utilities, $this->mock_wcpay_account, $this->mock_customer_service, $this->mock_fraud_service );

$this->assertFalse( $payments_checkout->get_payment_fields_js_config()['forceNetworkSavedCards'] );
WC_Payments::set_registered_card_gateway( $registered_card_gateway );
}

public function test_is_woopay_enabled_returns_false_if_ineligible() {
$this->mock_cache->method( 'get' )->willReturn( [ 'platform_checkout_eligible' => false ] );
$this->assertFalse( $this->payments_checkout->get_payment_fields_js_config()['isWooPayEnabled'] );
}

public function test_is_woopay_enabled_returns_false_if_ineligible_and_enabled() {
$this->wcpay_gateway->update_option( 'platform_checkout', 'yes' );
$this->assertFalse( $this->payments_checkout->get_payment_fields_js_config()['isWooPayEnabled'] );
}

public function test_timur_testing() {
$returned_icon = $this->payments_checkout->get_payment_fields_js_config()['icon'];
$this->assertNotNull( $returned_icon );
$this->assertStringContainsString( 'assets/images/payment-methods/cc.svg', $returned_icon );
}

public function is_woopay_falsy_value_provider() {
return [
[ '0' ],
Expand Down Expand Up @@ -2356,12 +2248,6 @@ private function create_charge_object() {
return new WC_Payments_API_Charge( $this->mock_charge_id, 1500, $created );
}

private function refresh_payments_checkout() {
remove_all_actions( 'wc_payments_add_payment_fields' );

$this->payments_checkout = new WC_Payments_UPE_Checkout( $this->wcpay_gateway, $this->woopay_utilities, $this->mock_wcpay_account, $this->mock_customer_service, $this->mock_fraud_service );
}

private function create_gateway_with( $payment_method ) {
return new UPE_Split_Payment_Gateway(
$this->mock_api_client,
Expand Down

0 comments on commit b3e3b92

Please sign in to comment.