Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions .env.dist.testing
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ CONVERTKIT_API_SUBSCRIBER_ID="1579118532"
CONVERTKIT_API_SUBSCRIBER_ID_NO_ACCESS="1632998602"
CONVERTKIT_API_FORM_NAME="Page Form [inline]"
CONVERTKIT_API_FORM_ID="2765139"
CONVERTKIT_API_FORM_SINGLE_OPTIN_NAME="Auto Confirm Form [inline]"
CONVERTKIT_API_FORM_SINGLE_OPTIN_ID="3059218"
CONVERTKIT_API_FORM_DOUBLE_OPTIN_NAME="Double Optin Form [inline]"
CONVERTKIT_API_FORM_DOUBLE_OPTIN_ID="2765143"
CONVERTKIT_API_FORM_FORMAT_MODAL_NAME="Modal Form [modal]"
CONVERTKIT_API_FORM_FORMAT_MODAL_NAME_ONLY="Modal Form"
CONVERTKIT_API_FORM_FORMAT_MODAL_ID="2780977"
Expand Down
26 changes: 20 additions & 6 deletions includes/blocks/class-convertkit-block-form-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,16 @@ public function maybe_subscribe() {
'block_form_builder'
);

// Determine the subscriber state.
// If a Form is specified, mark the subscriber as inactive, so the form's double optin is honored.
// If a Tag or Sequence is specified, mark the subscriber as active, as there's no double optin for tags or sequences.
$subscriber_state = $form_id !== false ? 'inactive' : 'active';

// Create subscriber.
$result = $api->create_subscriber(
sanitize_email( $form_data['email'] ),
array_key_exists( 'first_name', $form_data ) ? $form_data['first_name'] : '',
'active',
$subscriber_state,
$custom_fields
);

Expand Down Expand Up @@ -189,11 +194,20 @@ public function maybe_subscribe() {

// If a form was specified, add the subscriber to the form.
if ( $form_id ) {
$result = $api->add_subscriber_to_form(
$form_id,
$result['subscriber']['id'],
get_permalink( absint( $form_data['post_id'] ) )
);
// For Legacy Forms, a different endpoint is used.
$forms = new ConvertKit_Resource_Forms();
if ( $forms->is_legacy( $form_id ) ) {
$result = $api->add_subscriber_to_legacy_form(
$form_id,
$result['subscriber']['id']
);
} else {
$result = $api->add_subscriber_to_form(
$form_id,
$result['subscriber']['id'],
get_permalink( absint( $form_data['post_id'] ) )
);
}

if ( $form_data['store_entries'] ) {
$entries->upsert(
Expand Down
220 changes: 215 additions & 5 deletions tests/EndToEnd/forms/blocks-shortcodes/PageBlockFormBuilderCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,123 @@ public function testFormBuilderBlockWithTextCustomization(EndToEndTester $I)
}

/**
* Test the Form Builder block works when added and a Form is specified
* to subscribe the subscriber to.
* Test the Form Builder block works when added and that the subscriber's state
* is active when a single optin Form is specified.
*
* @since 3.0.4
*
* @param EndToEndTester $I Tester.
*/
public function testFormBuilderBlockWithSingleOptinFormEnabled(EndToEndTester $I)
{
// Setup Plugin and Resources.
$I->setupKitPlugin($I);
$I->setupKitPluginResources($I);

// Add a Page using the Gutenberg editor.
$I->addGutenbergPage(
$I,
title: 'Kit: Page: Form Builder: Block: Form Enabled'
);

// Configure metabox's Form setting = None, ensuring we only test the block in Gutenberg.
$I->configurePluginSidebarSettings(
$I,
form: 'None'
);

// Add block to Page.
$I->addGutenbergBlock(
$I,
blockName: 'Kit Form Builder',
blockProgrammaticName: 'convertkit-form-builder',
blockConfiguration: [
'form_id' => [ 'select', $_ENV['CONVERTKIT_API_FORM_SINGLE_OPTIN_NAME'] ],
]
);

// Confirm the block template was used as the default.
$this->seeFormBuilderBlock($I);
$this->seeFormBuilderButtonBlock($I);
$this->seeFormBuilderField(
$I,
fieldType: 'text',
fieldName: 'first_name',
fieldID: 'first_name',
label: 'First name',
container: 'div[data-type="convertkit/form-builder"]'
);
$this->seeFormBuilderField(
$I,
fieldType: 'email',
fieldName: 'email',
fieldID: 'email',
label: 'Email address',
container: 'div[data-type="convertkit/form-builder"]'
);

// Publish and view the Page on the frontend site.
$I->publishAndViewGutenbergPage($I);

// Confirm that the Form is output in the DOM.
$this->seeFormBuilderField(
$I,
fieldType: 'text',
fieldName: 'first_name',
fieldID: 'first_name',
label: 'First name',
container: 'div.wp-block-convertkit-form-builder',
isFrontend: true
);
$this->seeFormBuilderField(
$I,
fieldType: 'email',
fieldName: 'email',
fieldID: 'email',
label: 'Email address',
container: 'div.wp-block-convertkit-form-builder',
isFrontend: true
);

// Generate email address for this test.
$emailAddress = $I->generateEmailAddress();

// Submit form.
$I->fillField('input[name="convertkit[first_name]"]', 'First');
$I->fillField('input[name="convertkit[email]"]', $emailAddress);
$I->click('div.wp-block-convertkit-form-builder button[type="submit"]');

// Confirm that the email address was added to Kit.
$I->waitForElementVisible('.convertkit-form-builder-subscribed-message');
$I->wait(3);
$subscriber = $I->apiCheckSubscriberExists(
$I,
emailAddress: $emailAddress,
firstName: 'First'
);

// Confirm that the subscriber is active, as a form was used.
// This honors a Form's single optin setting.
$I->assertEquals('active', $subscriber['state']);

// Confirm that the subscriber has the form.
$I->apiCheckSubscriberHasForm(
$I,
subscriberID: $subscriber['id'],
formID: $_ENV['CONVERTKIT_API_FORM_SINGLE_OPTIN_ID'],
referrer: $_ENV['WORDPRESS_URL'] . $I->grabFromCurrentUrl()
);
}

/**
* Test the Form Builder block works when added and that the subscriber's state
* is inactive when a double optin Form is specified.
*
* @since 3.0.4
*
* @param EndToEndTester $I Tester.
*/
public function testFormBuilderBlockWithFormEnabled(EndToEndTester $I)
public function testFormBuilderBlockWithDoubleOptinFormEnabled(EndToEndTester $I)
{
// Setup Plugin and Resources.
$I->setupKitPlugin($I);
Expand All @@ -309,7 +418,7 @@ public function testFormBuilderBlockWithFormEnabled(EndToEndTester $I)
blockName: 'Kit Form Builder',
blockProgrammaticName: 'convertkit-form-builder',
blockConfiguration: [
'form_id' => [ 'select', $_ENV['CONVERTKIT_API_FORM_NAME'] ],
'form_id' => [ 'select', $_ENV['CONVERTKIT_API_FORM_DOUBLE_OPTIN_NAME'] ],
]
);

Expand Down Expand Up @@ -373,15 +482,116 @@ public function testFormBuilderBlockWithFormEnabled(EndToEndTester $I)
firstName: 'First'
);

// Confirm that the subscriber is inactive, as a form was used.
// This honors a Form's double optin setting.
$I->assertEquals('inactive', $subscriber['state']);

// Confirm that the subscriber has the form.
$I->apiCheckSubscriberHasForm(
$I,
subscriberID: $subscriber['id'],
formID: $_ENV['CONVERTKIT_API_FORM_ID'],
formID: $_ENV['CONVERTKIT_API_FORM_DOUBLE_OPTIN_ID'],
referrer: $_ENV['WORDPRESS_URL'] . $I->grabFromCurrentUrl()
);
}

/**
* Test the Form Builder block works when added and a Legacy Form is specified
* to subscribe the subscriber to.
*
* @since 3.3.2
*
* @param EndToEndTester $I Tester.
*/
public function testFormBuilderBlockWithLegacyFormEnabled(EndToEndTester $I)
{
// Setup Plugin and Resources.
$I->setupKitPlugin($I);
$I->setupKitPluginResources($I);

// Add a Page using the Gutenberg editor.
$I->addGutenbergPage(
$I,
title: 'Kit: Page: Form Builder: Block: Form Enabled'
);

// Configure metabox's Form setting = None, ensuring we only test the block in Gutenberg.
$I->configurePluginSidebarSettings(
$I,
form: 'None'
);

// Add block to Page.
$I->addGutenbergBlock(
$I,
blockName: 'Kit Form Builder',
blockProgrammaticName: 'convertkit-form-builder',
blockConfiguration: [
'form_id' => [ 'select', $_ENV['CONVERTKIT_API_LEGACY_FORM_NAME'] ],
]
);

// Confirm the block template was used as the default.
$this->seeFormBuilderBlock($I);
$this->seeFormBuilderButtonBlock($I);
$this->seeFormBuilderField(
$I,
fieldType: 'text',
fieldName: 'first_name',
fieldID: 'first_name',
label: 'First name',
container: 'div[data-type="convertkit/form-builder"]'
);
$this->seeFormBuilderField(
$I,
fieldType: 'email',
fieldName: 'email',
fieldID: 'email',
label: 'Email address',
container: 'div[data-type="convertkit/form-builder"]'
);

// Publish and view the Page on the frontend site.
$I->publishAndViewGutenbergPage($I);

// Confirm that the Form is output in the DOM.
$this->seeFormBuilderField(
$I,
fieldType: 'text',
fieldName: 'first_name',
fieldID: 'first_name',
label: 'First name',
container: 'div.wp-block-convertkit-form-builder',
isFrontend: true
);
$this->seeFormBuilderField(
$I,
fieldType: 'email',
fieldName: 'email',
fieldID: 'email',
label: 'Email address',
container: 'div.wp-block-convertkit-form-builder',
isFrontend: true
);

// Generate email address for this test.
$emailAddress = $I->generateEmailAddress();

// Submit form.
$I->fillField('input[name="convertkit[first_name]"]', 'First');
$I->fillField('input[name="convertkit[email]"]', $emailAddress);
$I->click('div.wp-block-convertkit-form-builder button[type="submit"]');

// Confirm that the email address was added to Kit.
$I->waitForElementVisible('.convertkit-form-builder-subscribed-message');
$I->wait(3);
$I->apiCheckSubscriberExists(
$I,
emailAddress: $emailAddress,
firstName: 'First'
);
}

/**
* Test the Form Builder block works when added and a Tag is specified
* to subscribe the subscriber to.
Expand Down
60 changes: 55 additions & 5 deletions tests/EndToEnd/integrations/other/ContactForm7FormCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,64 @@ public function testSettingsContactForm7WhenNoCredentials(EndToEndTester $I)
}

/**
* Test that saving a Contact Form 7 to Kit Form Mapping works.
* Test the Contact Form 7 Form integration works and that the subscriber's state is active
* when a single optin Form is specified.
*
* @since 1.9.6
* @since 3.3.2
*
* @param EndToEndTester $I Tester.
*/
public function testSettingsContactForm7ToKitSingleOptinFormMapping(EndToEndTester $I)
{
// Setup Contact form 7 Form and configuration for this test.
$pageID = $this->_contactForm7SetupForm(
$I,
$_ENV['CONVERTKIT_API_FORM_SINGLE_OPTIN_NAME']
);

// Define email address for this test.
$emailAddress = $I->generateEmailAddress();

// Complete and submit Contact Form 7 Form.
$this->_contactForm7CompleteAndSubmitForm(
$I,
pageID: $pageID,
emailAddress: $emailAddress
);

// Wait for the API to update.
$I->wait(2);

// Confirm that the email address was added to Kit.
$subscriber = $I->apiCheckSubscriberExists($I, $emailAddress);

// Confirm that the subscriber is active, as a form was used.
// This honors a Form's single optin setting.
$I->assertEquals('active', $subscriber['state']);

// Check that the subscriber has the expected form and referrer value set.
$I->apiCheckSubscriberHasForm(
$I,
subscriberID: $subscriber['id'],
formID: $_ENV['CONVERTKIT_API_FORM_SINGLE_OPTIN_ID'],
referrer: $_ENV['WORDPRESS_URL'] . $I->grabFromCurrentUrl()
);
}

/**
* Test the Contact Form 7 Form integration works and that the subscriber's state is inactive
* when a double optin Form is specified.
*
* @since 3.3.2
*
* @param EndToEndTester $I Tester.
*/
public function testSettingsContactForm7ToKitFormMapping(EndToEndTester $I)
public function testSettingsContactForm7ToKitDoubleOptinFormMapping(EndToEndTester $I)
{
// Setup Contact form 7 Form and configuration for this test.
$pageID = $this->_contactForm7SetupForm(
$I,
$_ENV['CONVERTKIT_API_THIRD_PARTY_INTEGRATIONS_FORM_NAME']
$_ENV['CONVERTKIT_API_FORM_DOUBLE_OPTIN_NAME']
);

// Define email address for this test.
Expand All @@ -72,11 +118,15 @@ public function testSettingsContactForm7ToKitFormMapping(EndToEndTester $I)
// Confirm that the email address was added to Kit.
$subscriber = $I->apiCheckSubscriberExists($I, $emailAddress);

// Confirm that the subscriber is inactive, as a form was used.
// This honors a Form's double optin setting.
$I->assertEquals('inactive', $subscriber['state']);

// Check that the subscriber has the expected form and referrer value set.
$I->apiCheckSubscriberHasForm(
$I,
subscriberID: $subscriber['id'],
formID: $_ENV['CONVERTKIT_API_THIRD_PARTY_INTEGRATIONS_FORM_ID'],
formID: $_ENV['CONVERTKIT_API_FORM_DOUBLE_OPTIN_ID'],
referrer: $_ENV['WORDPRESS_URL'] . $I->grabFromCurrentUrl()
);
}
Expand Down
Loading