Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?php

namespace Tests\EndToEnd;

use Tests\Support\EndToEndTester;

/**
* Tests for the Settings > Kit > Tools > Import sections for AWeber.
*
* @since 3.1.5
*/
class PluginSettingsToolsImporterAweberCest
{
/**
* Run common actions before running the test functions in this class.
*
* @since 3.1.5
*
* @param EndToEndTester $I Tester.
*/
public function _before(EndToEndTester $I)
{
// Activate Plugins.
$I->activateKitPlugin($I);
}

/**
* Test that AWeber Forms are replaced with Kit Forms when the Tools > AWeber: Migrate Configuration is configured.
*
* @since 3.1.5
*
* @param EndToEndTester $I Tester.
*/
public function testAWeberImport(EndToEndTester $I)
{
// Setup Plugin.
$I->setupKitPlugin($I);
$I->setupKitPluginResources($I);

// Create Aweber Forms.
$aweberFormIDs = $this->_createAWeberForms($I);

// Insert AWeber Form Shortcodes into Pages.
$pageIDs = $this->_createPagesWithAWeberFormShortcodes($I, $aweberFormIDs);

// Navigate to the Tools screen.
$I->loadKitSettingsToolsScreen($I);

// Select the Kit Forms to replace the AWeber Forms.
foreach ($aweberFormIDs as $aweberFormID) {
$I->selectOption('_wp_convertkit_integration_aweber_settings[' . $aweberFormID . ']', $_ENV['CONVERTKIT_API_FORM_ID']);
}

// Click the Migrate button.
$I->click('Migrate');

// Confirm success message displays.
$I->waitForElementVisible('.notice-success');
$I->see('AWeber forms migrated successfully.');

// View the Pages, to confirm Kit Forms now display.
foreach ($pageIDs as $pageID) {
$I->amOnPage('?p=' . $pageID);
$I->seeElementInDOM('form[data-sv-form]');
}
}

/**
* Test that the AWeber: Migrate Configuration section is not displayed when no AWeber Forms exist.
*
* @since 3.1.5
*
* @param EndToEndTester $I Tester.
*/
public function testAWeberImportWhenNoAWeberForms(EndToEndTester $I)
{
// Setup Plugin.
$I->setupKitPlugin($I);
$I->setupKitPluginResources($I);

// Navigate to the Tools screen.
$I->loadKitSettingsToolsScreen($I);

// Confirm no AWeber: Migrate Configuration section is displayed.
$I->dontSeeElementInDOM('#import-aweber');
}

/**
* Test that the AWeber: Migrate Configuration section is not displayed when AWeber Forms exist,
* but no Pages, Posts or Custom Posts contain AWeber Form Shortcodes.
*
* @since 3.1.5
*
* @param EndToEndTester $I Tester.
*/
public function testAWeberImportWhenNoAWeberShortcodesInContent(EndToEndTester $I)
{
// Setup Plugin.
$I->setupKitPlugin($I);
$I->setupKitPluginResources($I);

// Create AWeber Forms.
$aweberFormIDs = $this->_createAWeberForms($I);

// Navigate to the Tools screen.
$I->loadKitSettingsToolsScreen($I);

// Confirm no AWeber: Migrate Configuration section is displayed, as there are no
// AWeber Form Shortcodes in the content.
$I->dontSeeElementInDOM('#import-aweber');
}

/**
* Test that the AWeber: Migrate Configuration section is not displayed when no Kit Forms exist.
*
* @since 3.1.5
*
* @param EndToEndTester $I Tester.
*/
public function testAWeberImportWhenNoKitForms(EndToEndTester $I)
{
// Setup Plugin.
$I->setupKitPluginCredentialsNoData($I);
$I->setupKitPluginResourcesNoData($I);

// Navigate to the Tools screen.
$I->loadKitSettingsToolsScreen($I);

// Confirm no AWeber: Migrate Configuration section is displayed, as there are no
// AWeber Form Shortcodes in the content.
$I->dontSeeElementInDOM('#import-aweber');
}

/**
* Create AWeber Forms.
*
* @since 3.1.5
*
* @return array
*/
private function _createAWeberForms()
{
// AWeber doesn't cache Forms or store them in the database, so we mock the data that would be returned from their API.
return [
'10',
'11',
];
}

/**
* Create Pages with AWeber Form Shortcodes.
*
* @since 3.1.5
*
* @param EndToEndTester $I Tester.
* @param array $aweberFormIDs AWeber Form IDs.
* @return array
*/
private function _createPagesWithAWeberFormShortcodes(EndToEndTester $I, $aweberFormIDs)
{
$pageIDs = array();

foreach ($aweberFormIDs as $aweberFormID) {
$pageIDs[] = $I->havePostInDatabase(
[
'post_type' => 'page',
'post_status' => 'publish',
'post_title' => 'Page with AWeber Form #' . $aweberFormID,
'post_content' => '[aweber formid="' . $aweberFormID . '"]',
'meta_input' => [
'_wp_convertkit_post_meta' => [
'form' => '0',
'landing_page' => '',
'tag' => '',
],
],
]
);
}

return $pageIDs;
}

/**
* Deactivate and reset Plugin(s) after each test, if the test passes.
* We don't use _after, as this would provide a screenshot of the Plugin
* deactivation and not the true test error.
*
* @since 3.1.5
*
* @param EndToEndTester $I Tester.
*/
public function _passed(EndToEndTester $I)
{
$I->deactivateKitPlugin($I);
$I->resetKitPlugin($I);
}
}
130 changes: 130 additions & 0 deletions tests/Integration/ImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,136 @@ public function tearDown(): void
parent::tearDown();
}

/**
* Test that the get_form_ids_from_content() method returns third party form shortcode Form IDs.
*
* @since 3.1.5
*/
public function testGetAWeberFormIDsFromContent()
{
// Initialize the class we want to test.
$this->importer = new \ConvertKit_Admin_Importer_AWeber();

// Confirm initialization didn't result in an error.
$this->assertNotInstanceOf(\WP_Error::class, $this->importer);

// Define the content to test.
$content = '[aweber formid="10"] some content [aweber formid="11"] some other content';

// Extract form IDs from content.
$form_ids = $this->importer->get_form_ids_from_content( $content );

// Assert the correct number of form IDs are returned.
$this->assertEquals( 2, count( $form_ids ) );
$this->assertEquals( 10, $form_ids[0] );
$this->assertEquals( 11, $form_ids[1] );
}

/**
* Test that the replace_shortcodes_in_content() method replaces the third party form shortcode with the Kit form shortcode.
*
* @since 3.1.5
*/
public function testAWeberReplaceShortcodesInContent()
{
// Initialize the class we want to test.
$this->importer = new \ConvertKit_Admin_Importer_AWeber();

// Confirm initialization didn't result in an error.
$this->assertNotInstanceOf(\WP_Error::class, $this->importer);

// Define the shortcodes to test.
$shortcodes = [
'[aweber formid=10]',
'[aweber formid="10"]',
'[aweber formid=10 listid=11]',
'[aweber formid="10" listid="11"]',
'[aweber formid=10 listid=11 formtype=webform]',
'[aweber formid="10" listid="11" formtype="webform"]',
'[aweber listid=11 formid=10]',
'[aweber listid="11" formid="10"]',
'[aweber listid=11 formid=10 formtype=webform]',
'[aweber listid="11" formid="10" formtype="webform"]',
'[aweber formtype=webform listid=11 formid=10]',
'[aweber formtype="webform" listid="11" formid="10"]',
];

// Test each shortcode is replaced with the Kit form shortcode.
foreach ( $shortcodes as $shortcode ) {
$this->assertEquals(
'[convertkit_form id="' . $_ENV['CONVERTKIT_API_FORM_ID'] . '"]',
$this->importer->replace_shortcodes_in_content( $shortcode, 10, $_ENV['CONVERTKIT_API_FORM_ID'] )
);

// Prepend and append some content.
$content = 'Some content before the shortcode: ' . $shortcode . ' Some content after the shortcode.';
$this->assertEquals(
'Some content before the shortcode: [convertkit_form id="' . $_ENV['CONVERTKIT_API_FORM_ID'] . '"] Some content after the shortcode.',
$this->importer->replace_shortcodes_in_content( $content, 10, $_ENV['CONVERTKIT_API_FORM_ID'] )
);

// Prepend and append some content and duplicate the shortcode.
$content = 'Some content before the shortcode: ' . $shortcode . ' Some content after the shortcode: ' . $shortcode;
$this->assertEquals(
'Some content before the shortcode: [convertkit_form id="' . $_ENV['CONVERTKIT_API_FORM_ID'] . '"] Some content after the shortcode: [convertkit_form id="' . $_ENV['CONVERTKIT_API_FORM_ID'] . '"]',
$this->importer->replace_shortcodes_in_content( $content, 10, $_ENV['CONVERTKIT_API_FORM_ID'] )
);
}
}

/**
* Test that the replace_shortcodes_in_content() method ignores non-AWeber shortcodes.
*
* @since 3.1.5
*/
public function testAWeberReplaceShortcodesInContentIgnoringOtherShortcodes()
{
// Initialize the class we want to test.
$this->importer = new \ConvertKit_Admin_Importer_AWeber();

// Confirm initialization didn't result in an error.
$this->assertNotInstanceOf(\WP_Error::class, $this->importer);

// Define the shortcodes to test.
$shortcodes = [
'[convertkit_form id="' . $_ENV['CONVERTKIT_API_FORM_ID'] . '"]',
'[a_random_shortcode]',
];

// Test each shortcode is ignored.
foreach ( $shortcodes as $shortcode ) {
$this->assertEquals(
$shortcode,
$this->importer->replace_shortcodes_in_content( $shortcode, 10, $_ENV['CONVERTKIT_API_FORM_ID'] )
);
}
}

/**
* Test that the get_form_ids_from_content() method returns third party form shortcode Form IDs.
*
* @since 3.1.5
*/
public function testGetMC4WPFormIDsFromContent()
{
// Initialize the class we want to test.
$this->importer = new \ConvertKit_Admin_Importer_MC4WP();

// Confirm initialization didn't result in an error.
$this->assertNotInstanceOf(\WP_Error::class, $this->importer);

// Define the content to test.
$content = '[mc4wp_form id="10"] some content [mc4wp_form id="11"] some other content';

// Extract form IDs from content.
$form_ids = $this->importer->get_form_ids_from_content( $content );

// Assert the correct number of form IDs are returned.
$this->assertEquals( 2, count( $form_ids ) );
$this->assertEquals( 10, $form_ids[0] );
$this->assertEquals( 11, $form_ids[1] );
}

/**
* Test that the replace_shortcodes_in_content() method replaces the third party form shortcode with the Kit form shortcode.
*
Expand Down
Loading