From 6b749da9f45f7f7ef21921c59728e62af620e093 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 8 Nov 2024 11:19:17 +0800 Subject: [PATCH 1/3] Use Kit Account ID when storing connection in WPForms MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This ensures that individual Forms configured with Kit have their settings retained should the user disconnect and reconnect the same Kit Account at Settings > Integrations. It’s likely they’ll do this with the v4 OAuth when using the same creator account on multiple sites, hitting a race condition where an access token expires. --- .env.dist.testing | 2 + .env.example | 2 + .../class-integrate-convertkit-wpforms.php | 3 +- tests/_support/Helper/Acceptance/WPForms.php | 28 +++++++-- tests/acceptance/forms/FormCest.php | 57 ++++++++++++++++++- .../recommendations/RecommendationsCest.php | 2 +- 6 files changed, 83 insertions(+), 11 deletions(-) diff --git a/.env.dist.testing b/.env.dist.testing index d9287dc..478f863 100644 --- a/.env.dist.testing +++ b/.env.dist.testing @@ -17,6 +17,8 @@ TEST_TABLE_PREFIX=wp_ TEST_SITE_WP_URL=http://127.0.0.1 TEST_SITE_WP_DOMAIN=127.0.0.1 TEST_SITE_ADMIN_EMAIL=wordpress@convertkit.local +CONVERTKIT_API_ACCOUNT_ID=841415 +CONVERTKIT_API_ACCOUNT_ID_NO_DATA=753065 CONVERTKIT_API_FORM_NAME="WPForms Form" CONVERTKIT_API_FORM_ID="3753745" CONVERTKIT_API_LEGACY_FORM_NAME="Legacy Form" diff --git a/.env.example b/.env.example index 19a4f6a..432cfda 100644 --- a/.env.example +++ b/.env.example @@ -21,6 +21,8 @@ CONVERTKIT_API_KEY_NO_DATA= CONVERTKIT_API_SECRET_NO_DATA= CONVERTKIT_API_KEY= CONVERTKIT_API_SECRET= +CONVERTKIT_API_ACCOUNT_ID=841415 +CONVERTKIT_API_ACCOUNT_ID_NO_DATA=753065 CONVERTKIT_API_FORM_NAME="WPForms Form" CONVERTKIT_API_FORM_ID="3753745" CONVERTKIT_API_LEGACY_FORM_NAME="Legacy Form" diff --git a/includes/class-integrate-convertkit-wpforms.php b/includes/class-integrate-convertkit-wpforms.php index efe30e4..12f8311 100644 --- a/includes/class-integrate-convertkit-wpforms.php +++ b/includes/class-integrate-convertkit-wpforms.php @@ -717,7 +717,6 @@ public function maybe_get_and_store_access_token() { } // Update the provider's settings and return its unique ID. - $id = uniqid(); wpforms_update_providers_options( $this->slug, array( @@ -727,7 +726,7 @@ public function maybe_get_and_store_access_token() { 'label' => $account['account']['name'], 'date' => time(), ), - $id + 'kit-' . $account['account']['id'] ); // Reload the integrations screen, which will now show the connection. diff --git a/tests/_support/Helper/Acceptance/WPForms.php b/tests/_support/Helper/Acceptance/WPForms.php index 4a50c86..e27a8cb 100644 --- a/tests/_support/Helper/Acceptance/WPForms.php +++ b/tests/_support/Helper/Acceptance/WPForms.php @@ -17,11 +17,13 @@ class WPForms extends \Codeception\Module * @param AcceptanceTester $I AcceptanceTester. * @param bool|string $accessToken Access Token (if not specified, CONVERTKIT_OAUTH_ACCESS_TOKEN is used). * @param bool|string $refreshToken Refresh Token (if not specified, CONVERTKIT_OAUTH_REFRESH_TOKEN is used). + * @param string $accountID Kit Account ID. + * @return string Account ID in WPForms. */ - public function setupWPFormsIntegration($I, $accessToken = false, $refreshToken = false) + public function setupWPFormsIntegration($I, $accessToken = false, $refreshToken = false, $accountID = false) { - // Define a random account ID key for this test. - $accountID = rand(63000, 64000) . 'bdcceea3'; // phpcs:ignore WordPress.WP.AlternativeFunctions + $accountID = 'kit-' . ( $accountID ? $accountID : $_ENV['CONVERTKIT_API_ACCOUNT_ID'] ); + $I->haveOptionInDatabase( 'wpforms_providers', [ @@ -47,11 +49,13 @@ public function setupWPFormsIntegration($I, $accessToken = false, $refreshToken * @param AcceptanceTester $I AcceptanceTester. * @param bool|string $apiKey API Key (if not specified, CONVERTKIT_API_KEY is used). * @param bool|string $apiSecret API Secret (if not specified, CONVERTKIT_API_SECRET is used). + * @param string $accountID Kit Account ID. + * @return string Account ID in WPForms. */ - public function setupWPFormsIntegrationWithAPIKeyAndSecret($I, $apiKey = false, $apiSecret = false) + public function setupWPFormsIntegrationWithAPIKeyAndSecret($I, $apiKey = false, $apiSecret = false, $accountID = false) { - // Define a random account ID key for this test. - $accountID = rand(63000, 64000) . 'bdcceea3'; // phpcs:ignore WordPress.WP.AlternativeFunctions + $accountID = 'kit-' . ( $accountID ? $accountID : $_ENV['CONVERTKIT_API_ACCOUNT_ID'] ); + $I->haveOptionInDatabase( 'wpforms_providers', [ @@ -91,6 +95,18 @@ public function checkWPFormsIntegrationExists($I, $accessToken, $refreshToken) return false; } + /** + * Helper method to delete the ConvertKit integration in WPForms. + * + * @since 1.7.8 + * + * @param AcceptanceTester $I AcceptanceTester. + */ + public function deleteWPFormsIntegration($I) + { + $I->dontHaveOptionInDatabase('wpforms_providers'); + } + /** * Creates a WPForms Form with ConvertKit Settings, as if it were created * in 1.4.1 or older. diff --git a/tests/acceptance/forms/FormCest.php b/tests/acceptance/forms/FormCest.php index 98f7bd9..0bc18a9 100644 --- a/tests/acceptance/forms/FormCest.php +++ b/tests/acceptance/forms/FormCest.php @@ -61,6 +61,39 @@ public function testCreateFormToConvertKitFormMapping(AcceptanceTester $I) $I->apiCheckSubscriberExists($I, $emailAddress, 'First'); } + /** + * Tests that the connection configured when editing a WPForms Form at Marketing > Kit is retained when: + * - Connects to Kit at Settings > Integrations, + * - Configures the connection at WPForms Form > Marketing > Kit, + * - Disconnects from Kit at Settings > Integrations, + * - Connects (again) to the same Kit at Settings > Integrations + * - Observe the connection at WPForms Form > Marketing > Kit is retained. + * + * @since 1.7.8 + * + * @param AcceptanceTester $I Tester. + */ + public function testConnectionRetainedWhenAccountReconnected(AcceptanceTester $I) + { + // Setup WPForms Form and configuration for this test. + $formID = $this->_wpFormsSetupFormOnly( + $I, + $_ENV['CONVERTKIT_API_FORM_NAME'] + ); + + // Disconnect from Kit at Settings > Integrations. + $I->deleteWPFormsIntegration($I); + + // Connect to Kit at Settings > Integrations. + $I->setupWPFormsIntegration($I); + + // Edit the WPForms Form, confirming the connection still exists. + $I->amOnAdminPage('admin.php?page=wpforms-builder&view=providers&form_id=' . $formID); + $I->waitForElementVisible('div[data-provider="convertkit"]'); + $I->see('Select Account'); + $I->see('Kit Form'); + } + /** * Test that the Plugin works when: * - Creating a WPForms Form, @@ -515,6 +548,27 @@ public function testCreateFormWithCustomField(AcceptanceTester $I) * @return int Page ID */ private function _wpFormsSetupForm(AcceptanceTester $I, $optionName, $tags = false, $customFields = false) + { + // Create Form. + $wpFormsID = $this->_wpFormsSetupFormOnly($I, $optionName, $tags, $customFields); + + // Create a Page with the WPForms shortcode as its content. + return $I->createPageWithWPFormsShortcode($I, $wpFormsID); + } + + /** + * Maps the given resource name to the created WPForms Form, + * embeds the shortcode on a new Page, returning the Form ID. + * + * @since 1.7.8 + * + * @param AcceptanceTester $I Tester. + * @param string $optionName