Commit 46cb8eb53a9 for woocommerce
commit 46cb8eb53a9dabd9ea36881f3f5a7af2b2cce63c
Author: Luigi Teschio <gigitux@gmail.com>
Date: Mon Nov 23 13:03:27 2026 +0100
Require a browser session to get the WooPayments connection link (#71254)
Forward-port of Automattic/woocommerce#619.
Co-authored-by: Daniel Mallory <daniel.mallory@automattic.com>
diff --git a/plugins/woocommerce/src/Internal/Admin/Settings/PaymentsProviders/WooPayments/WooPaymentsRestController.php b/plugins/woocommerce/src/Internal/Admin/Settings/PaymentsProviders/WooPayments/WooPaymentsRestController.php
index 31239458b06..9261c14bdc3 100644
--- a/plugins/woocommerce/src/Internal/Admin/Settings/PaymentsProviders/WooPayments/WooPaymentsRestController.php
+++ b/plugins/woocommerce/src/Internal/Admin/Settings/PaymentsProviders/WooPayments/WooPaymentsRestController.php
@@ -5,6 +5,7 @@ namespace Automattic\WooCommerce\Internal\Admin\Settings\PaymentsProviders\WooPa
use Automattic\WooCommerce\Internal\Admin\Settings\Exceptions\ApiException;
use Automattic\WooCommerce\Internal\Admin\Settings\Payments;
+use Automattic\WooCommerce\Internal\RestApi\InteractiveSessionCheck;
use Automattic\WooCommerce\Internal\RestApiControllerBase;
use Automattic\WooCommerce\Internal\Utilities\ArrayUtil;
use Exception;
@@ -223,7 +224,7 @@ class WooPaymentsRestController extends RestApiControllerBase {
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => fn( $request ) => $this->run( $request, 'handle_onboarding_wpcom_connection_authorize' ),
- 'permission_callback' => fn( $request ) => $this->check_permissions( $request ),
+ 'permission_callback' => fn( $request ) => $this->check_wpcom_connection_authorize_permissions( $request ),
'args' => array(
'source' => array(
'description' => esc_html__( 'The upmost entry point from where the merchant entered the onboarding flow.', 'woocommerce' ),
@@ -528,8 +529,11 @@ class WooPaymentsRestController extends RestApiControllerBase {
$source = $request->get_param( 'source' );
+ // The WPCOM authorization URL is only returned to an interactive admin session.
+ $is_interactive_session = ! is_wp_error( InteractiveSessionCheck::require_interactive_session( $request ) );
+
try {
- $onboarding_details = $this->woopayments->get_onboarding_details( $location, $this->get_rest_url_path( 'onboarding' ), $source );
+ $onboarding_details = $this->woopayments->get_onboarding_details( $location, $this->get_rest_url_path( 'onboarding' ), $source, $is_interactive_session );
} catch ( ApiException $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
} catch ( Exception $e ) {
@@ -984,6 +988,25 @@ class WooPaymentsRestController extends RestApiControllerBase {
);
}
+ /**
+ * Permissions check for the WPCOM connection authorize endpoint.
+ *
+ * Requires an interactive admin session on top of the general permissions.
+ *
+ * @param WP_REST_Request $request The request for which the permission is checked.
+ * @phpstan-param WP_REST_Request<array<string, mixed>> $request
+ *
+ * @return bool|WP_Error True if the request is allowed, otherwise an error or False if no error is available for the request method.
+ */
+ private function check_wpcom_connection_authorize_permissions( WP_REST_Request $request ) {
+ $session = InteractiveSessionCheck::require_interactive_session( $request );
+ if ( is_wp_error( $session ) ) {
+ return $session;
+ }
+
+ return $this->check_permissions( $request );
+ }
+
/**
* Validate the location argument.
*
diff --git a/plugins/woocommerce/src/Internal/Admin/Settings/PaymentsProviders/WooPayments/WooPaymentsService.php b/plugins/woocommerce/src/Internal/Admin/Settings/PaymentsProviders/WooPayments/WooPaymentsService.php
index df19802ecf5..33ee16fb045 100644
--- a/plugins/woocommerce/src/Internal/Admin/Settings/PaymentsProviders/WooPayments/WooPaymentsService.php
+++ b/plugins/woocommerce/src/Internal/Admin/Settings/PaymentsProviders/WooPayments/WooPaymentsService.php
@@ -163,16 +163,18 @@ class WooPaymentsService {
/**
* Get the onboarding details for the Payments settings page.
*
- * @param string $location The location for which we are onboarding.
- * This is an ISO 3166-1 alpha-2 country code.
- * @param string $rest_path The REST API path to use for constructing REST API URLs.
- * @param string|null $source Optional. The source for the onboarding flow.
+ * @param string $location The location for which we are onboarding.
+ * This is an ISO 3166-1 alpha-2 country code.
+ * @param string $rest_path The REST API path to use for constructing REST API URLs.
+ * @param string|null $source Optional. The source for the onboarding flow.
+ * @param bool $include_wpcom_authorization_url Optional. Whether to include the WPCOM connection authorization URL.
+ * Only pass true for the current user's interactive admin session.
*
* @return array The onboarding details.
* @throws ApiException If the onboarding action can not be performed due to the current state of the site.
* @throws Exception If there were errors when generating the onboarding details.
*/
- public function get_onboarding_details( string $location, string $rest_path, ?string $source = null ): array {
+ public function get_onboarding_details( string $location, string $rest_path, ?string $source = null, bool $include_wpcom_authorization_url = false ): array {
// Since getting the onboarding details is not idempotent, we will check it as an action.
$this->check_if_onboarding_action_is_acceptable();
@@ -199,7 +201,7 @@ class WooPaymentsService {
'messages' => array(
'not_supported' => ! $onboarding_supported ? $this->provider->get_onboarding_not_supported_message( $gateway, $location ) : null,
),
- 'steps' => $this->get_onboarding_steps( $location, trailingslashit( $rest_path ) . 'step', $source ),
+ 'steps' => $this->get_onboarding_steps( $location, trailingslashit( $rest_path ) . 'step', $source, $include_wpcom_authorization_url ),
'context' => array(
'urls' => array(
'overview_page' => $this->get_overview_page_url(),
@@ -2192,15 +2194,16 @@ class WooPaymentsService {
/**
* Get the onboarding details for each step.
*
- * @param string $location The location for which we are onboarding.
- * This is an ISO 3166-1 alpha-2 country code.
- * @param string $rest_path The REST API path to use for constructing REST API URLs.
- * @param string|null $source Optional. The source for the onboarding flow.
+ * @param string $location The location for which we are onboarding.
+ * This is an ISO 3166-1 alpha-2 country code.
+ * @param string $rest_path The REST API path to use for constructing REST API URLs.
+ * @param string|null $source Optional. The source for the onboarding flow.
+ * @param bool $include_wpcom_authorization_url Optional. Whether to include the WPCOM connection authorization URL.
*
* @return array[] The list of onboarding steps details.
* @throws Exception If there was an error generating the onboarding steps details.
*/
- private function get_onboarding_steps( string $location, string $rest_path, ?string $source = self::SESSION_ENTRY_DEFAULT ): array {
+ private function get_onboarding_steps( string $location, string $rest_path, ?string $source = self::SESSION_ENTRY_DEFAULT, bool $include_wpcom_authorization_url = false ): array {
$steps = array();
// Add the payment methods onboarding step details, but only if we have recommended payment methods.
@@ -2255,16 +2258,19 @@ class WooPaymentsService {
);
if ( $this->is_store_registered_with_wpcom() ) {
- // The site is already registered, so generating the authorization URL will not register it.
- $wpcom_connection = $this->get_wpcom_connection_authorization( $this->get_wpcom_connection_return_url( $source ) );
- if ( ! $wpcom_connection['success'] ) {
- // In case of errors, make sure we work with a list of error messages.
- $wpcom_step['errors'] = array_values( (array) ( $wpcom_connection['errors'] ?? array() ) );
+ // The authorization URL is only generated for callers that asked for it.
+ if ( $include_wpcom_authorization_url ) {
+ // The site is already registered, so generating the authorization URL will not register it.
+ $wpcom_connection = $this->get_wpcom_connection_authorization( $this->get_wpcom_connection_return_url( $source ) );
+ if ( ! $wpcom_connection['success'] ) {
+ // In case of errors, make sure we work with a list of error messages.
+ $wpcom_step['errors'] = array_values( (array) ( $wpcom_connection['errors'] ?? array() ) );
+ }
+ $wpcom_step['actions']['auth'] = array(
+ 'type' => self::ACTION_TYPE_REDIRECT,
+ 'href' => $wpcom_connection['url'],
+ );
}
- $wpcom_step['actions']['auth'] = array(
- 'type' => self::ACTION_TYPE_REDIRECT,
- 'href' => $wpcom_connection['url'],
- );
} else {
// Registering the site is a slow WPCOM request, so it waits for the merchant's click
// instead of running every time something reads the onboarding details.
diff --git a/plugins/woocommerce/tests/php/src/Internal/Admin/Settings/PaymentsProviders/WooPayments/WooPaymentsRestControllerIntegrationTest.php b/plugins/woocommerce/tests/php/src/Internal/Admin/Settings/PaymentsProviders/WooPayments/WooPaymentsRestControllerIntegrationTest.php
index e09e006b4a8..c4dbb549fb8 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Admin/Settings/PaymentsProviders/WooPayments/WooPaymentsRestControllerIntegrationTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Admin/Settings/PaymentsProviders/WooPayments/WooPaymentsRestControllerIntegrationTest.php
@@ -328,6 +328,7 @@ class WooPaymentsRestControllerIntegrationTest extends WC_Unit_Test_Case {
$this->run_cleanup_step( fn() => $this->clear_rest_server(), $cleanup_errors );
$this->run_cleanup_step( fn() => parent::tearDown(), $cleanup_errors );
$this->run_cleanup_step( fn() => self::invalidate_test_option_caches(), $cleanup_errors );
+ unset( $GLOBALS['wp_rest_auth_cookie'] );
if ( array() !== $cleanup_errors ) {
throw $cleanup_errors[0];
@@ -1251,6 +1252,7 @@ class WooPaymentsRestControllerIntegrationTest extends WC_Unit_Test_Case {
// Act.
$request = new WP_REST_Request( 'POST', self::ENDPOINT . '/onboarding/step/' . WooPaymentsService::ONBOARDING_STEP_WPCOM_CONNECTION . '/authorize' );
$request->set_param( 'source', 'test_source' );
+ $this->add_interactive_session( $request );
$response = $this->server->dispatch( $request );
// Assert.
@@ -1270,7 +1272,8 @@ class WooPaymentsRestControllerIntegrationTest extends WC_Unit_Test_Case {
->expects( $this->never() )
->method( 'try_registration' );
- $request = new WP_REST_Request( 'POST', self::ENDPOINT . '/onboarding/step/' . WooPaymentsService::ONBOARDING_STEP_WPCOM_CONNECTION . '/authorize' );
+ $request = new WP_REST_Request( 'POST', self::ENDPOINT . '/onboarding/step/' . WooPaymentsService::ONBOARDING_STEP_WPCOM_CONNECTION . '/authorize' );
+ $this->add_interactive_session( $request );
$response = $this->server->dispatch( $request );
$this->assertSame( \WP_Http::CONFLICT, $response->get_status() );
@@ -1311,6 +1314,7 @@ class WooPaymentsRestControllerIntegrationTest extends WC_Unit_Test_Case {
if ( null !== $source ) {
$request->set_param( 'source', $source );
}
+ $this->add_interactive_session( $request );
$response = $this->server->dispatch( $request );
$this->assertSame( \WP_Http::OK, $response->get_status() );
@@ -1333,7 +1337,8 @@ class WooPaymentsRestControllerIntegrationTest extends WC_Unit_Test_Case {
->willReturn( new \WP_Error( 'siteurl_private_ip', 'The site URL is a private IP address.' ) );
// Act.
- $request = new WP_REST_Request( 'POST', self::ENDPOINT . '/onboarding/step/' . WooPaymentsService::ONBOARDING_STEP_WPCOM_CONNECTION . '/authorize' );
+ $request = new WP_REST_Request( 'POST', self::ENDPOINT . '/onboarding/step/' . WooPaymentsService::ONBOARDING_STEP_WPCOM_CONNECTION . '/authorize' );
+ $this->add_interactive_session( $request );
$response = $this->server->dispatch( $request );
// Assert.
@@ -1350,7 +1355,8 @@ class WooPaymentsRestControllerIntegrationTest extends WC_Unit_Test_Case {
->expects( $this->never() )
->method( 'try_registration' );
- $request = new WP_REST_Request( 'POST', self::ENDPOINT . '/onboarding/step/' . WooPaymentsService::ONBOARDING_STEP_WPCOM_CONNECTION . '/authorize' );
+ $request = new WP_REST_Request( 'POST', self::ENDPOINT . '/onboarding/step/' . WooPaymentsService::ONBOARDING_STEP_WPCOM_CONNECTION . '/authorize' );
+ $this->add_interactive_session( $request );
$response = $this->server->dispatch( $request );
$this->assertSame( \WP_Http::FORBIDDEN, $response->get_status() );
@@ -1381,7 +1387,8 @@ class WooPaymentsRestControllerIntegrationTest extends WC_Unit_Test_Case {
)
);
- $request = new WP_REST_Request( 'POST', self::ENDPOINT . '/onboarding/step/' . WooPaymentsService::ONBOARDING_STEP_WPCOM_CONNECTION . '/authorize' );
+ $request = new WP_REST_Request( 'POST', self::ENDPOINT . '/onboarding/step/' . WooPaymentsService::ONBOARDING_STEP_WPCOM_CONNECTION . '/authorize' );
+ $this->add_interactive_session( $request );
$response = $this->server->dispatch( $request );
$this->assertSame( \WP_Http::INTERNAL_SERVER_ERROR, $response->get_status() );
@@ -1409,7 +1416,8 @@ class WooPaymentsRestControllerIntegrationTest extends WC_Unit_Test_Case {
)
);
- $request = new WP_REST_Request( 'POST', self::ENDPOINT . '/onboarding/step/' . WooPaymentsService::ONBOARDING_STEP_WPCOM_CONNECTION . '/authorize' );
+ $request = new WP_REST_Request( 'POST', self::ENDPOINT . '/onboarding/step/' . WooPaymentsService::ONBOARDING_STEP_WPCOM_CONNECTION . '/authorize' );
+ $this->add_interactive_session( $request );
$response = $this->server->dispatch( $request );
$this->assertSame( \WP_Http::INTERNAL_SERVER_ERROR, $response->get_status() );
@@ -1440,7 +1448,8 @@ class WooPaymentsRestControllerIntegrationTest extends WC_Unit_Test_Case {
)
);
- $request = new WP_REST_Request( 'POST', self::ENDPOINT . '/onboarding/step/' . WooPaymentsService::ONBOARDING_STEP_WPCOM_CONNECTION . '/authorize' );
+ $request = new WP_REST_Request( 'POST', self::ENDPOINT . '/onboarding/step/' . WooPaymentsService::ONBOARDING_STEP_WPCOM_CONNECTION . '/authorize' );
+ $this->add_interactive_session( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
@@ -1450,6 +1459,104 @@ class WooPaymentsRestControllerIntegrationTest extends WC_Unit_Test_Case {
$this->assertSame( 'Diagnostic data', $data['data']['detail'] ?? null );
}
+ /**
+ * @testdox Rejects authorization, without registering the store or generating the URL, when the request has no interactive session.
+ */
+ public function test_handle_onboarding_wpcom_connection_authorize_requires_interactive_session(): void {
+ $this->mock_wpcom_connection_manager->method( 'is_connected' )->willReturn( false );
+ $this->mock_wpcom_connection_manager
+ ->expects( $this->never() )
+ ->method( 'try_registration' );
+ $authorization_requested = false;
+ $this->mockable_proxy->register_static_mocks(
+ array(
+ Utils::class => array(
+ 'get_wpcom_connection_authorization' => function () use ( &$authorization_requested ) {
+ $authorization_requested = true;
+ return array(
+ 'success' => true,
+ 'url' => 'https://wordpress.com/auth',
+ );
+ },
+ ),
+ )
+ );
+
+ // The current user can manage WooCommerce, but nothing ties the request to their browser session.
+ $request = new WP_REST_Request( 'POST', self::ENDPOINT . '/onboarding/step/' . WooPaymentsService::ONBOARDING_STEP_WPCOM_CONNECTION . '/authorize' );
+ $response = $this->server->dispatch( $request );
+
+ $this->assertSame( \WP_Http::FORBIDDEN, $response->get_status() );
+ $this->assertSame( 'woocommerce_rest_missing_session', $response->get_data()['code'] );
+ $this->assertFalse( $authorization_requested, 'The authorization URL should not be generated without an interactive session' );
+ }
+
+ /**
+ * @testdox Rejects authorization from an interactive session of a user who cannot manage WooCommerce.
+ */
+ public function test_handle_onboarding_wpcom_connection_authorize_requires_capability(): void {
+ wp_set_current_user( $this->factory->user->create( array( 'role' => 'customer' ) ) );
+ $this->mock_wpcom_connection_manager->method( 'is_connected' )->willReturn( false );
+ $this->mock_wpcom_connection_manager
+ ->expects( $this->never() )
+ ->method( 'try_registration' );
+
+ $request = new WP_REST_Request( 'POST', self::ENDPOINT . '/onboarding/step/' . WooPaymentsService::ONBOARDING_STEP_WPCOM_CONNECTION . '/authorize' );
+ $this->add_interactive_session( $request );
+ $response = $this->server->dispatch( $request );
+
+ $this->assertSame( \WP_Http::FORBIDDEN, $response->get_status() );
+ $this->assertSame( 'woocommerce_rest_cannot_create', $response->get_data()['code'] );
+ }
+
+ /**
+ * @testdox Includes the WPCOM authorization URL in the onboarding details only for an interactive session.
+ * @testWith [true]
+ * [false]
+ *
+ * @param bool $interactive_session Whether the request comes from an interactive session.
+ */
+ public function test_get_onboarding_details_wpcom_authorization_url_requires_interactive_session( bool $interactive_session ): void {
+ // The store is registered with WPCOM, but no user has connected their account yet.
+ $this->mock_wpcom_connection_manager->method( 'is_connected' )->willReturn( true );
+ $this->mock_wpcom_connection_manager->method( 'has_connected_owner' )->willReturn( false );
+ $authorization_requested = false;
+ $this->mockable_proxy->register_static_mocks(
+ array(
+ Utils::class => array(
+ 'get_wpcom_connection_authorization' => function () use ( &$authorization_requested ) {
+ $authorization_requested = true;
+ return array(
+ 'success' => true,
+ 'errors' => array(),
+ 'url' => 'https://wordpress.com/auth?secret=some_secret',
+ );
+ },
+ ),
+ )
+ );
+
+ $request = new WP_REST_Request( 'POST', self::ENDPOINT . '/onboarding' );
+ $request->set_param( 'location', 'US' );
+ if ( $interactive_session ) {
+ $this->add_interactive_session( $request );
+ }
+ $response = $this->server->dispatch( $request );
+
+ $this->assertSame( \WP_Http::OK, $response->get_status() );
+ $steps = array_column( $response->get_data()['steps'], null, 'id' );
+ $wpcom_step = $steps[ WooPaymentsService::ONBOARDING_STEP_WPCOM_CONNECTION ];
+ $this->assertSame( WooPaymentsService::ONBOARDING_STEP_STATUS_NOT_STARTED, $wpcom_step['status'] );
+ $this->assertArrayHasKey( 'start', $wpcom_step['actions'], 'The other step actions should not depend on the session' );
+ $this->assertSame( $interactive_session, $authorization_requested );
+ if ( $interactive_session ) {
+ $this->assertSame( 'https://wordpress.com/auth?secret=some_secret', $wpcom_step['actions']['auth']['href'] );
+ } else {
+ $this->assertArrayNotHasKey( 'auth', $wpcom_step['actions'] );
+ $this->assertStringNotContainsString( 'some_secret', (string) wp_json_encode( $response->get_data() ) );
+ }
+ }
+
/**
* Test onboarding reset.
*/
@@ -1833,4 +1940,19 @@ class WooPaymentsRestControllerIntegrationTest extends WC_Unit_Test_Case {
'PR' => 'Puerto Rico',
);
}
+
+ /**
+ * Make the request look like it comes from the current user's browser session.
+ *
+ * @param WP_REST_Request $request The request to add the session to.
+ */
+ private function add_interactive_session( WP_REST_Request $request ): void {
+ $user_id = get_current_user_id();
+ $expiration = time() + DAY_IN_SECONDS;
+ $token = \WP_Session_Tokens::get_instance( $user_id )->create( $expiration );
+
+ // Validating the cookie fires auth_cookie_valid, which core records in $wp_rest_auth_cookie.
+ wp_validate_auth_cookie( wp_generate_auth_cookie( $user_id, $expiration, 'logged_in', $token ), 'logged_in' );
+ $request->set_header( 'X-WP-Nonce', wp_create_nonce( 'wp_rest' ) );
+ }
}
diff --git a/plugins/woocommerce/tests/php/src/Internal/Admin/Settings/PaymentsProviders/WooPayments/WooPaymentsServiceTest.php b/plugins/woocommerce/tests/php/src/Internal/Admin/Settings/PaymentsProviders/WooPayments/WooPaymentsServiceTest.php
index e7d2d11c7e1..e00655f5fa3 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Admin/Settings/PaymentsProviders/WooPayments/WooPaymentsServiceTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Admin/Settings/PaymentsProviders/WooPayments/WooPaymentsServiceTest.php
@@ -1031,7 +1031,7 @@ class WooPaymentsServiceTest extends WC_Unit_Test_Case {
);
// Act.
- $result = $this->sut->get_onboarding_details( $location, $rest_path );
+ $result = $this->sut->get_onboarding_details( $location, $rest_path, null, true );
// Assert.
$this->assertIsArray( $result );
@@ -1040,6 +1040,44 @@ class WooPaymentsServiceTest extends WC_Unit_Test_Case {
$this->assertEquals( $expected_steps, $result['steps'] );
}
+ /**
+ * @testdox Should leave the WPCOM authorization URL out of the onboarding details unless asked to include it.
+ */
+ public function test_get_onboarding_details_steps_without_wpcom_authorization_url(): void {
+ // The store is registered with WPCOM, but no user has connected their account yet.
+ $this->mock_wpcom_connection_manager->method( 'is_connected' )->willReturn( true );
+ $this->mock_wpcom_connection_manager->method( 'has_connected_owner' )->willReturn( false );
+ $authorization_requested = false;
+ $this->mockable_proxy->register_static_mocks(
+ array(
+ Utils::class => array(
+ 'get_wpcom_connection_authorization' => function () use ( &$authorization_requested ) {
+ $authorization_requested = true;
+ return array(
+ 'success' => true,
+ 'errors' => array(),
+ 'url' => 'https://wordpress.com/auth?query=some_query',
+ );
+ },
+ ),
+ )
+ );
+ $rest_path = '/rest/path/to/onboarding/';
+
+ $result = $this->sut->get_onboarding_details( 'US', $rest_path );
+
+ $steps = array_column( $result['steps'], null, 'id' );
+ $wpcom_step = $steps[ WooPaymentsService::ONBOARDING_STEP_WPCOM_CONNECTION ];
+ $this->assertSame( WooPaymentsService::ONBOARDING_STEP_STATUS_NOT_STARTED, $wpcom_step['status'] );
+ $this->assertSame( array(), $wpcom_step['errors'] );
+ $this->assertSame(
+ array( 'start', 'check', 'clean' ),
+ array_keys( $wpcom_step['actions'] ),
+ 'Only the authorization URL action should be left out'
+ );
+ $this->assertFalse( $authorization_requested, 'The authorization URL should not be generated when it is not included' );
+ }
+
/**
* Data provider for test_get_onboarding_details_steps.
*
diff --git a/plugins/woocommerce/tests/php/src/Internal/Admin/Settings/PaymentsRestControllerIntegrationTest.php b/plugins/woocommerce/tests/php/src/Internal/Admin/Settings/PaymentsRestControllerIntegrationTest.php
index 5eb92598179..cb733866a03 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Admin/Settings/PaymentsRestControllerIntegrationTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Admin/Settings/PaymentsRestControllerIntegrationTest.php
@@ -1312,6 +1312,52 @@ class PaymentsRestControllerIntegrationTest extends WC_Unit_Test_Case {
$this->assertTrue( $provider['state']['enabled'] );
}
+ /**
+ * @testdox Leaves the WPCOM authorization URL out of the WooPayments onboarding steps.
+ */
+ public function test_get_payment_providers_leaves_out_woopayments_wpcom_authorization_url(): void {
+ // The store is registered with WPCOM, but no user has connected their account yet.
+ $this->mock_wpcom_connection_manager = $this->getMockBuilder( WPCOM_Connection_Manager::class )
+ ->onlyMethods( array( 'is_connected', 'has_connected_owner', 'is_connection_owner' ) )
+ ->getMock();
+ $this->mock_wpcom_connection_manager->method( 'is_connected' )->willReturn( true );
+ $this->mock_wpcom_connection_manager->method( 'has_connected_owner' )->willReturn( false );
+ $this->setup_legacy_proxy_mocks();
+ $this->reinitialize_services_and_controller();
+
+ $authorization_requested = false;
+ $this->mockable_proxy->register_static_mocks(
+ array(
+ Utils::class => array(
+ 'get_wpcom_connection_authorization' => function () use ( &$authorization_requested ) {
+ $authorization_requested = true;
+ return array(
+ 'success' => true,
+ 'errors' => array(),
+ 'url' => 'https://wordpress.com/auth?secret=some_secret',
+ );
+ },
+ ),
+ )
+ );
+ $this->mock_payment_gateways();
+ Constants::set_constant( 'WCPAY_VERSION_NUMBER', WooPaymentsService::EXTENSION_MINIMUM_VERSION );
+
+ $request = new WP_REST_Request( 'POST', self::ENDPOINT . '/providers' );
+ $request->set_param( 'location', 'US' );
+ $response = $this->server->dispatch( $request );
+
+ $this->assertSame( 200, $response->get_status() );
+ $providers = array_column( $response->get_data()['providers'], null, 'id' );
+ $steps = array_column( $providers['woocommerce_payments']['onboarding']['steps'], null, 'id' );
+ $wpcom_step = $steps[ WooPaymentsService::ONBOARDING_STEP_WPCOM_CONNECTION ];
+ $this->assertSame( WooPaymentsService::ONBOARDING_STEP_STATUS_NOT_STARTED, $wpcom_step['status'] );
+ $this->assertArrayHasKey( 'start', $wpcom_step['actions'] );
+ $this->assertArrayNotHasKey( 'auth', $wpcom_step['actions'] );
+ $this->assertFalse( $authorization_requested, 'The providers list should not generate the authorization URL' );
+ $this->assertStringNotContainsString( 'some_secret', (string) wp_json_encode( $response->get_data() ) );
+ }
+
/**
* Test setting the country.
*/