Skip to content

Commit 3374543

Browse files
committed
add tests
1 parent 90f0955 commit 3374543

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

tests/unit/test-class-wc-payment-gateway-wcpay.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3757,6 +3757,92 @@ public function test_get_recommended_payment_method_no_country_code_provided( $i
37573757
remove_filter( 'woocommerce_countries_base_country', $filter_callback );
37583758
}
37593759

3760+
public function test_updating_subscription_for_non_3ds_cards_removes_hook() {
3761+
$_GET['change_payment_method'] = 10;
3762+
WC_Subscriptions::set_wcs_is_subscription(
3763+
function ( $order ) {
3764+
return true;
3765+
}
3766+
);
3767+
3768+
$pi = new Payment_Information( 'pm_test', WC_Helper_Order::create_order(), null, new WC_Payment_Token_CC(), null, null, null, '', 'card' );
3769+
3770+
$request = $this->mock_wcpay_request( Create_And_Confirm_Intention::class );
3771+
$request->expects( $this->once() )
3772+
->method( 'set_payment_methods' )
3773+
->with( [ 'card' ] );
3774+
$request->expects( $this->once() )
3775+
->method( 'format_response' )
3776+
->willReturn( WC_Helper_Intention::create_intention( [ 'status' => 'success' ] ) );
3777+
3778+
add_filter(
3779+
'woocommerce_subscriptions_update_payment_via_pay_shortcode',
3780+
[ $this->card_gateway, 'update_payment_method_for_subscriptions' ],
3781+
10,
3782+
3
3783+
);
3784+
3785+
$this->assertEquals(
3786+
10,
3787+
has_filter( 'woocommerce_subscriptions_update_payment_via_pay_shortcode', [ $this->card_gateway, 'update_payment_method_for_subscriptions' ] ),
3788+
'Hook should be registered before payment processing'
3789+
);
3790+
3791+
$this->card_gateway->process_payment_for_order( WC()->cart, $pi );
3792+
3793+
$this->assertFalse(
3794+
has_filter( 'woocommerce_subscriptions_update_payment_via_pay_shortcode', [ $this->card_gateway, 'update_payment_method_for_subscriptions' ] ),
3795+
'Hook should be removed after processing payment for subscription with non-3DS card'
3796+
);
3797+
}
3798+
3799+
public function test_updating_subscription_for_3ds_cards_sets_delayed_update_payment_method_all() {
3800+
$_GET['change_payment_method'] = 10;
3801+
WC_Subscriptions::set_wcs_is_subscription(
3802+
function ( $order ) {
3803+
return true;
3804+
}
3805+
);
3806+
3807+
$order = WC_Helper_Order::create_order();
3808+
3809+
// Set up POST data including update_all_subscriptions_payment_method.
3810+
$_POST = [
3811+
'payment_method' => 'woocommerce_payments',
3812+
'update_all_subscriptions_payment_method' => '1',
3813+
];
3814+
3815+
$pi = new Payment_Information( 'pm_test', $order, null, new WC_Payment_Token_CC(), null, null, null, '', 'card' );
3816+
3817+
$request = $this->mock_wcpay_request( Create_And_Confirm_Intention::class );
3818+
$request->expects( $this->once() )
3819+
->method( 'set_payment_methods' )
3820+
->with( [ 'card' ] );
3821+
$request->expects( $this->once() )
3822+
->method( 'format_response' )
3823+
->willReturn(
3824+
WC_Helper_Intention::create_intention(
3825+
[
3826+
'status' => 'requires_action',
3827+
'next_action' => [
3828+
'type' => 'use_stripe_sdk',
3829+
],
3830+
]
3831+
)
3832+
);
3833+
3834+
try {
3835+
// The test exits early so we need to handle the exception.
3836+
$this->card_gateway->process_payment_for_order( WC()->cart, $pi );
3837+
} catch ( Exception $e ) {
3838+
$this->assertEquals(
3839+
'woocommerce_payments',
3840+
$order->get_meta( '_delayed_update_payment_method_all' ),
3841+
'Order metadata for delayed payment method update was not set correctly'
3842+
);
3843+
}
3844+
}
3845+
37603846
/**
37613847
* Sets up the expectation for a certain factor for the new payment
37623848
* process to be either set or unset.

tests/unit/test-class-wc-payments-order-service.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,26 @@ public function test_attach_intent_info_to_order() {
12761276
$this->assertEquals( $intent_id, $this->order->get_meta( '_intent_id', true ) );
12771277
}
12781278

1279+
public function test_attach_intent_order_with_allow_update_on_success() {
1280+
$intent = WC_Helper_Intention::create_intention(
1281+
[
1282+
'id' => 'pi_mock',
1283+
'status' => Intent_Status::SUCCEEDED,
1284+
]
1285+
);
1286+
$this->order_service->attach_intent_info_to_order( $this->order, $intent );
1287+
1288+
$another_intent = WC_Helper_Intention::create_intention(
1289+
[
1290+
'id' => 'pi_mock_2',
1291+
'status' => Intent_Status::CANCELED,
1292+
]
1293+
);
1294+
$this->order_service->attach_intent_info_to_order( $this->order, $another_intent, true );
1295+
1296+
$this->assertEquals( Intent_Status::CANCELED, $this->order->get_meta( '_intention_status', true ) );
1297+
}
1298+
12791299
public function test_attach_intent_info_to_order_after_successful_payment() {
12801300
$intent = WC_Helper_Intention::create_intention(
12811301
[

0 commit comments

Comments
 (0)