Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Kit Account ID as WPForms Provider Key #69

Merged
merged 4 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.dist.testing
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 1 addition & 2 deletions includes/class-integrate-convertkit-wpforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions tests/_support/Helper/Acceptance/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Plugin extends \Codeception\Module
*/
public function activateConvertKitPlugin($I)
{
$I->activateThirdPartyPlugin($I, 'kit-formerly-convertkit-for-wpforms');
$I->activateThirdPartyPlugin($I, 'integrate-convertkit-wpforms');
}

/**
Expand All @@ -32,7 +32,7 @@ public function activateConvertKitPlugin($I)
*/
public function deactivateConvertKitPlugin($I)
{
$I->deactivateThirdPartyPlugin($I, 'kit-formerly-convertkit-for-wpforms');
$I->deactivateThirdPartyPlugin($I, 'integrate-convertkit-wpforms');
}

/**
Expand Down
30 changes: 23 additions & 7 deletions tests/_support/Helper/Acceptance/WPForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
[
Expand All @@ -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',
[
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -368,7 +384,7 @@ public function configureConvertKitSettingsOnForm($I, $wpFormID, $formName, $nam
$connectionID = $I->grabAttributeFrom('.wpforms-provider-connections .wpforms-provider-connection', 'data-connection_id');

// Specify field values.
$I->waitForElementVisible('div[data-connection_id="' . $connectionID . '"] .wpforms-provider-fields');
$I->waitForElementVisible('div[data-connection_id="' . $connectionID . '"] .wpforms-provider-fields', 30);

if ($formName) {
// Select Form.
Expand Down
57 changes: 55 additions & 2 deletions tests/acceptance/forms/FormCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 <select> option name.
* @param bool|array $tags Values to use for tags.
* @param bool|array $customFields Custom field key / value pairs.
* @return int Form ID
*/
private function _wpFormsSetupFormOnly(AcceptanceTester $I, $optionName, $tags = false, $customFields = false)
{
// Define connection with valid API credentials.
$this->accountID = $I->setupWPFormsIntegration($I);
Expand All @@ -539,8 +593,7 @@ private function _wpFormsSetupForm(AcceptanceTester $I, $optionName, $tags = fal
// Check that the resources are cached with the correct key.
$I->seeCachedResourcesInDatabase($I, $this->accountID);

// Create a Page with the WPForms shortcode as its content.
return $I->createPageWithWPFormsShortcode($I, $wpFormsID);
return $wpFormsID;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/recommendations/RecommendationsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function testCreatorNetworkRecommendationsOptionWhenInvalidCredentials(Ac
public function testCreatorNetworkRecommendationsOptionWhenDisabledOnConvertKitAccount(AcceptanceTester $I)
{
// Setup Plugin with API Key and Secret for ConvertKit Account that does not have the Creator Network enabled.
$accountID = $I->setupWPFormsIntegration($I, $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA']);
$accountID = $I->setupWPFormsIntegration($I, $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA'], $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA'], $_ENV['CONVERTKIT_API_ACCOUNT_ID_NO_DATA']);

// Create Form.
$wpFormsID = $I->createWPFormsForm($I);
Expand Down
Loading