From cbb602c07c53ed705f7eb026b9f6301024cf7b47 Mon Sep 17 00:00:00 2001 From: Anne Mirasol Date: Thu, 23 Jan 2025 14:26:06 -0600 Subject: [PATCH] Add unit tests --- .../class-wc-stripe-upe-payment-gateway.php | 3 +- ...st-class-wc-stripe-upe-payment-gateway.php | 156 ++++++++++++++---- 2 files changed, 125 insertions(+), 34 deletions(-) diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 460e93d99..80e6ef002 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -955,6 +955,8 @@ private function process_payment_with_confirmation_token( int $order_id ) { try { $payment_information = $this->prepare_payment_information_from_request( $order ); + $this->validate_selected_payment_method_type( $payment_information, $order->get_billing_country() ); + $this->set_customer_id_for_order( $order, $payment_information['customer'] ); if ( $this->is_payment_needed( $order->get_id() ) ) { @@ -969,7 +971,6 @@ private function process_payment_with_confirmation_token( int $order_id ) { $selected_payment_type = $payment_information['selected_payment_type']; - // TODO: We want to set the payment method title to 'Amazon Pay', if applicable. $this->set_payment_method_title_for_order( $order, $selected_payment_type ); $return_url = $this->get_return_url( $order ); diff --git a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php index a3ba397e8..07db38eba 100644 --- a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php +++ b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php @@ -358,8 +358,10 @@ public function test_process_payment_returns_valid_response() { /** * Test basic checkout process_payment flow with deferred intent. + * + * @dataProvider provide_process_payment_deferred_intent_returns_valid_response */ - public function test_process_payment_deferred_intent_returns_valid_response() { + public function test_process_payment_deferred_intent_returns_valid_response( $post_vars ) { $customer_id = 'cus_mock'; $order = WC_Helper_Order::create_order(); $currency = $order->get_currency(); @@ -382,11 +384,7 @@ public function test_process_payment_deferred_intent_returns_valid_response() { ); // Set the appropriate POST flag to trigger a deferred intent request. - $_POST = [ - 'payment_method' => 'stripe', - 'wc-stripe-payment-method' => 'pm_mock', - 'wc-stripe-is-deferred-intent' => '1', - ]; + $_POST = $post_vars; $this->mock_gateway->intent_controller ->expects( $this->once() ) @@ -408,6 +406,30 @@ public function test_process_payment_deferred_intent_returns_valid_response() { $this->assertEquals( self::MOCK_RETURN_URL, $response['redirect'] ); } + /** + * Provider for `test_process_payment_deferred_intent_returns_valid_response`. + */ + public function provide_process_payment_deferred_intent_returns_valid_response() { + return [ + 'with-payment-method' => [ + [ + 'payment_method' => 'stripe', + 'wc-stripe-payment-method' => 'pm_mock', + 'wc-stripe-confirmation-token' => '', + 'wc-stripe-is-deferred-intent' => '1', + ], + ], + 'with-confirmation-token' => [ + [ + 'payment_method' => 'stripe', + 'wc-stripe-payment-method' => '', + 'wc-stripe-confirmation-token' => 'ctoken_mock', + 'wc-stripe-is-deferred-intent' => '1', + ], + ], + ]; + } + /** * Test SCA/3DS checkout process_payment flow with deferred intent. */ @@ -589,8 +611,10 @@ public function provide_process_payment_deferred_intent_with_required_action_for /** * Exception handling of the process_payment flow with deferred intent. + * + * @dataProvider provide_process_payment_deferred_intent_handles_exception */ - public function test_process_payment_deferred_intent_handles_exception() { + public function test_process_payment_deferred_intent_handles_exception( $post_vars ) { $payment_intent_id = 'pi_mock'; $customer_id = 'cus_mock'; $order = WC_Helper_Order::create_order(); @@ -609,11 +633,7 @@ public function test_process_payment_deferred_intent_handles_exception() { ], ]; - $_POST = [ - 'payment_method' => 'stripe', - 'wc-stripe-payment-method' => 'pm_mock', - 'wc-stripe-is-deferred-intent' => '1', - ]; + $_POST = $post_vars; $this->mock_gateway->intent_controller ->expects( $this->once() ) @@ -637,7 +657,34 @@ public function test_process_payment_deferred_intent_handles_exception() { $this->assertEquals( 'failed', $processed_order->get_status() ); } - public function test_process_payment_deferred_intent_bails_with_empty_payment_type() { + /** + * Provider for `test_process_payment_deferred_intent_handles_exception`. + */ + public function provide_process_payment_deferred_intent_handles_exception() { + return [ + 'with-payment-method' => [ + [ + 'payment_method' => 'stripe', + 'wc-stripe-payment-method' => 'pm_mock', + 'wc-stripe-confirmation-token' => '', + 'wc-stripe-is-deferred-intent' => '1', + ], + ], + 'with-confirmation-token' => [ + [ + 'payment_method' => 'stripe', + 'wc-stripe-payment-method' => '', + 'wc-stripe-confirmation-token' => 'ctoken_mock', + 'wc-stripe-is-deferred-intent' => '1', + ], + ], + ]; + } + + /** + * @dataProvider provide_process_payment_deferred_intent_bails_with_empty_payment_type + */ + public function test_process_payment_deferred_intent_bails_with_empty_payment_type( $post_vars ) { $payment_intent_id = 'pi_mock'; $customer_id = 'cus_mock'; $order = WC_Helper_Order::create_order(); @@ -656,11 +703,7 @@ public function test_process_payment_deferred_intent_bails_with_empty_payment_ty ], ]; - $_POST = [ - 'payment_method' => '', - 'wc-stripe-payment-method' => 'pm_mock', - 'wc-stripe-is-deferred-intent' => '1', - ]; + $_POST = $post_vars; $this->mock_gateway->intent_controller ->expects( $this->never() ) @@ -683,7 +726,34 @@ public function test_process_payment_deferred_intent_bails_with_empty_payment_ty $this->assertEquals( 'failed', $processed_order->get_status() ); } - public function test_process_payment_deferred_intent_bails_with_invalid_payment_type() { + /** + * Provider for `test_process_payment_deferred_intent_bails_with_empty_payment_type`. + */ + public function provide_process_payment_deferred_intent_bails_with_empty_payment_type() { + return [ + 'with-payment-method' => [ + [ + 'payment_method' => '', + 'wc-stripe-payment-method' => 'pm_mock', + 'wc-stripe-confirmation-token' => '', + 'wc-stripe-is-deferred-intent' => '1', + ], + ], + 'with-confirmation-token' => [ + [ + 'payment_method' => '', + 'wc-stripe-payment-method' => '', + 'wc-stripe-confirmation-token' => 'ctoken_mock', + 'wc-stripe-is-deferred-intent' => '1', + ], + ], + ]; + } + + /** + * @dataProvider provide_process_payment_deferred_intent_bails_with_invalid_payment_type + */ + public function test_process_payment_deferred_intent_bails_with_invalid_payment_type( $post_vars ) { $payment_intent_id = 'pi_mock'; $customer_id = 'cus_mock'; $order = WC_Helper_Order::create_order(); @@ -702,11 +772,7 @@ public function test_process_payment_deferred_intent_bails_with_invalid_payment_ ], ]; - $_POST = [ - 'payment_method' => 'some_invalid_type', - 'wc-stripe-payment-method' => 'pm_mock', - 'wc-stripe-is-deferred-intent' => '1', - ]; + $_POST = $post_vars; $this->mock_gateway->intent_controller ->expects( $this->never() ) @@ -729,6 +795,30 @@ public function test_process_payment_deferred_intent_bails_with_invalid_payment_ $this->assertEquals( 'failed', $processed_order->get_status() ); } + /** + * Provider for `test_process_payment_deferred_intent_bails_with_invalid_payment_type`. + */ + public function provide_process_payment_deferred_intent_bails_with_invalid_payment_type() { + return [ + 'with-payment-method' => [ + [ + 'payment_method' => 'some_invalid_type', + 'wc-stripe-payment-method' => 'pm_mock', + 'wc-stripe-confirmation-token' => '', + 'wc-stripe-is-deferred-intent' => '1', + ], + ], + 'with-confirmation-token' => [ + [ + 'payment_method' => 'some_invalid_type', + 'wc-stripe-payment-method' => '', + 'wc-stripe-confirmation-token' => 'ctoken_mock', + 'wc-stripe-is-deferred-intent' => '1', + ], + ], + ]; + } + /** * Test basic redirect payment processed correctly. */ @@ -1026,7 +1116,7 @@ public function test_checkout_saves_sepa_generated_payment_method_to_order() { 'captured' => true, 'status' => 'succeeded', 'payment_method_details' => [ - 'type' => WC_Stripe_Payment_Methods::BANCONTACT, + 'type' => WC_Stripe_Payment_Methods::BANCONTACT, WC_Stripe_Payment_Methods::BANCONTACT => [ 'generated_sepa_debit' => $generated_payment_method_id, ], @@ -2283,11 +2373,11 @@ public function test_process_payment_creates_new_intent_when_existing_intent_fai // Create a mock failed payment intent that would be attached to the order $mock_failed_intent = (object) wp_parse_args( [ - 'id' => 'pi_mock_failed', - 'payment_method' => 'pm_mock', - 'status' => WC_Stripe_Intent_Status::CANCELED, + 'id' => 'pi_mock_failed', + 'payment_method' => 'pm_mock', + 'status' => WC_Stripe_Intent_Status::CANCELED, 'payment_method_types' => [ WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID ], - 'charges' => (object) [ + 'charges' => (object) [ 'data' => [], ], ], @@ -2297,11 +2387,11 @@ public function test_process_payment_creates_new_intent_when_existing_intent_fai // Create a mock successful payment intent that will be created $mock_success_intent = (object) wp_parse_args( [ - 'id' => 'pi_mock_new', - 'payment_method' => 'pm_mock', - 'status' => WC_Stripe_Intent_Status::SUCCEEDED, + 'id' => 'pi_mock_new', + 'payment_method' => 'pm_mock', + 'status' => WC_Stripe_Intent_Status::SUCCEEDED, 'payment_method_types' => [ WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID ], - 'charges' => (object) [ + 'charges' => (object) [ 'data' => [ (object) [ 'id' => 'ch_mock',