From 78dfd76cf385760cce78929a24cb51de00734635 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 19 Jan 2026 16:07:53 +0800 Subject: [PATCH 1/7] Importer: Convert Third Party Form Blocks to Kit Form Block --- ...class-convertkit-admin-importer-aweber.php | 18 ++ .../class-convertkit-admin-importer-mc4wp.php | 18 ++ .../class-convertkit-admin-importer.php | 285 ++++++++++++++---- .../class-convertkit-admin-section-tools.php | 2 + 4 files changed, 258 insertions(+), 65 deletions(-) diff --git a/admin/importers/class-convertkit-admin-importer-aweber.php b/admin/importers/class-convertkit-admin-importer-aweber.php index fd982f283..2df10614e 100644 --- a/admin/importers/class-convertkit-admin-importer-aweber.php +++ b/admin/importers/class-convertkit-admin-importer-aweber.php @@ -32,6 +32,24 @@ class ConvertKit_Admin_Importer_AWeber extends ConvertKit_Admin_Importer { */ public $shortcode_id_attribute = 'formid'; + /** + * Holds the block name for AWeber forms. + * + * @since 3.1.6 + * + * @var string + */ + public $block_name = 'aweber-signupform-block/aweber-shortcode'; + + /** + * Holds the ID attribute name for AWeber forms. + * + * @since 3.1.6 + * + * @var string + */ + public $block_id_attribute = 'selectedShortCode'; + /** * Returns an array of AWeber form IDs and titles. * diff --git a/admin/importers/class-convertkit-admin-importer-mc4wp.php b/admin/importers/class-convertkit-admin-importer-mc4wp.php index 84ed6e071..ad9239bb7 100644 --- a/admin/importers/class-convertkit-admin-importer-mc4wp.php +++ b/admin/importers/class-convertkit-admin-importer-mc4wp.php @@ -32,6 +32,24 @@ class ConvertKit_Admin_Importer_MC4WP extends ConvertKit_Admin_Importer { */ public $shortcode_id_attribute = 'id'; + /** + * Holds the block name for MC4WP forms. + * + * @since 3.1.6 + * + * @var string + */ + public $block_name = 'mailchimp-for-wp/form'; + + /** + * Holds the ID attribute name for MC4WP forms. + * + * @since 3.1.6 + * + * @var string + */ + public $block_id_attribute = 'id'; + /** * Returns an array of MC4WP form IDs and titles. * diff --git a/admin/importers/class-convertkit-admin-importer.php b/admin/importers/class-convertkit-admin-importer.php index c3f437f20..8bfff6d68 100644 --- a/admin/importers/class-convertkit-admin-importer.php +++ b/admin/importers/class-convertkit-admin-importer.php @@ -32,6 +32,15 @@ abstract class ConvertKit_Admin_Importer { */ public $shortcode_id_attribute = ''; + /** + * Holds the block name for the third party Form plugin. + * + * @since 3.1.6 + * + * @var string + */ + public $block_name = ''; + /** * Returns an array of third party form IDs and titles. * @@ -42,7 +51,7 @@ abstract class ConvertKit_Admin_Importer { abstract public function get_forms(); /** - * Returns an array of post IDs that contain the third partyform shortcode. + * Returns an array of post IDs that contain the third party form shortcode or block. * * @since 3.1.5 * @@ -52,17 +61,21 @@ public function get_forms_in_posts() { global $wpdb; - // Search post_content for the third party form shortcode and return array of post IDs. + // Search post_content for the third party form shortcode or block and return array of post IDs. $results = $wpdb->get_col( $wpdb->prepare( " SELECT ID FROM {$wpdb->posts} WHERE post_status = %s - AND post_content LIKE %s + AND ( + post_content LIKE %s + OR post_content LIKE %s + ) ", 'publish', - '%[' . $this->shortcode_name . '%' + '%[' . $this->shortcode_name . '%', + '% +
[aweber listid=6924484 formid=289586845 formtype=webform]
+'; + + // Test the block is replaced with the Kit form block. + $this->assertEquals( + '', + $this->importer->replace_blocks_in_content( $content, 289586845, $_ENV['CONVERTKIT_API_FORM_ID'] ) + ); + } + + /** + * Test that the replace_blocks_in_content() method ignores non-AWeber blocks. + * + * @since 3.1.5 + */ + public function testAWeberReplaceBlocksInContentIgnoringOtherBlocks() + { + // 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 blocks to test. + $html_block = '
Some content
'; + $content = ' +
[aweber listid=6924484 formid=289586845 formtype=webform]
+' . $html_block; + + // Test the block is replaced with the Kit form block. + $this->assertEquals( + '' . $html_block, + $this->importer->replace_blocks_in_content( $content, 289586845, $_ENV['CONVERTKIT_API_FORM_ID'] ) + ); + } + /** * Test that the get_form_ids_from_content() method returns MC4WP form shortcode Form IDs * ignoring any other shortcodes. @@ -256,4 +307,51 @@ public function testMC4WPReplaceShortcodesInContentIgnoringOtherShortcodes() ); } } + + /** + * Test that the replace_blocks_in_content() method replaces the third party form block with the Kit form block. + * + * @since 3.1.5 + */ + public function testMC4WPReplaceBlocksInContent() + { + // 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 blocks to test. + $content = ''; + + // Test the block is replaced with the Kit form block. + $this->assertEquals( + '', + $this->importer->replace_blocks_in_content( $content, 4410, $_ENV['CONVERTKIT_API_FORM_ID'] ) + ); + } + + /** + * Test that the replace_blocks_in_content() method ignores non-MC4WP blocks. + * + * @since 3.1.5 + */ + public function testMC4WPReplaceBlocksInContentIgnoringOtherBlocks() + { + // 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 blocks to test. + $html_block = '
Some content
'; + $content = '' . $html_block; + + // Test the block is replaced with the Kit form block. + $this->assertEquals( + '' . $html_block, + $this->importer->replace_blocks_in_content( $content, 4410, $_ENV['CONVERTKIT_API_FORM_ID'] ) + ); + } } From 3a63e872c8714ef3ee22aa5c33e683b4797d731f Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 19 Jan 2026 16:45:07 +0800 Subject: [PATCH 5/7] Added End to End Tests --- .../PluginSettingsToolsImporterAweberCest.php | 79 ++++++++++++++++++- .../PluginSettingsToolsImporterMC4WPCest.php | 72 ++++++++++++++++- 2 files changed, 148 insertions(+), 3 deletions(-) diff --git a/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterAweberCest.php b/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterAweberCest.php index 27bdfca60..0df53fb63 100644 --- a/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterAweberCest.php +++ b/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterAweberCest.php @@ -31,7 +31,7 @@ public function _before(EndToEndTester $I) * * @param EndToEndTester $I Tester. */ - public function testAWeberImport(EndToEndTester $I) + public function testAWeberImportWithShortcodes(EndToEndTester $I) { // Setup Plugin. $I->setupKitPlugin($I); @@ -65,6 +65,47 @@ public function testAWeberImport(EndToEndTester $I) } } + /** + * Test that AWeber Blocks are replaced with Kit Blocks when the Tools > AWeber: Migrate Configuration is configured. + * + * @since 3.1.5 + * + * @param EndToEndTester $I Tester. + */ + public function testAWeberImportWithBlocks(EndToEndTester $I) + { + // Setup Plugin. + $I->setupKitPlugin($I); + $I->setupKitPluginResources($I); + + // Create Aweber Forms. + $aweberFormIDs = $this->_createAWeberForms($I); + + // Insert AWeber Blocks into Pages. + $pageIDs = $this->_createPagesWithAWeberBlocks($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. * @@ -181,6 +222,42 @@ private function _createPagesWithAWeberFormShortcodes(EndToEndTester $I, $aweber return $pageIDs; } + /** + * Create Pages with AWeber Blocks. + * + * @since 3.1.6 + * + * @param EndToEndTester $I Tester. + * @param array $aweberFormIDs AWeber Form IDs. + * @return array + */ + private function _createPagesWithAWeberBlocks(EndToEndTester $I, $aweberFormIDs) + { + $pageIDs = array(); + + foreach ($aweberFormIDs as $aweberFormID) { + $pageIDs[] = $I->havePostInDatabase( + [ + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_title' => 'Page with AWeber Block #' . $aweberFormID, + 'post_content' => ' +
[aweber listid=6924484 formid=' . $aweberFormID . ' formtype=webform]
+', + '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 diff --git a/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterMC4WPCest.php b/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterMC4WPCest.php index 569fd0049..2336c6d23 100644 --- a/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterMC4WPCest.php +++ b/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterMC4WPCest.php @@ -27,13 +27,13 @@ public function _before(EndToEndTester $I) } /** - * Test that Mailchimp Forms are replaced with Kit Forms when the Tools > MC4WP: Migrate Configuration is configured. + * Test that Mailchimp Form Shortcodes are replaced with Kit Form Shortcodes when the Tools > MC4WP: Migrate Configuration is configured. * * @since 3.1.0 * * @param EndToEndTester $I Tester. */ - public function testMC4WPImport(EndToEndTester $I) + public function testMC4WPImportWithShortcodes(EndToEndTester $I) { // Setup Plugin. $I->setupKitPlugin($I); @@ -67,6 +67,47 @@ public function testMC4WPImport(EndToEndTester $I) } } + /** + * Test that Mailchimp Blocks are replaced with Kit Blocks when the Tools > MC4WP: Migrate Configuration is configured. + * + * @since 3.1.0 + * + * @param EndToEndTester $I Tester. + */ + public function testMC4WPImportWithBlocks(EndToEndTester $I) + { + // Setup Plugin. + $I->setupKitPlugin($I); + $I->setupKitPluginResources($I); + + // Create Mailchimp Forms. + $mailchimpFormIDs = $this->_createMailchimpForms($I); + + // Insert Mailchimp Blocks into Pages. + $pageIDs = $this->_createPagesWithMC4WPBlocks($I, $mailchimpFormIDs); + + // Navigate to the Tools screen. + $I->loadKitSettingsToolsScreen($I); + + // Select the Kit Forms to replace the Mailchimp Forms. + foreach ($mailchimpFormIDs as $mailchimpFormID) { + $I->selectOption('_wp_convertkit_integration_mc4wp_settings[' . $mailchimpFormID . ']', $_ENV['CONVERTKIT_API_FORM_ID']); + } + + // Click the Migrate button. + $I->click('Migrate'); + + // Confirm success message displays. + $I->waitForElementVisible('.notice-success'); + $I->see('MC4WP 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 MC4WP: Migrate Configuration section is not displayed when no Mailchimp Forms exist. * @@ -197,6 +238,33 @@ private function _createPagesWithMailchimpFormShortcodes(EndToEndTester $I, $mai return $pageIDs; } + /** + * Create Pages with MC4WP Blocks. + * + * @since 3.1.6 + * + * @param EndToEndTester $I Tester. + * @param array $mc4wpFormIDs MC4WP Form IDs. + * @return array + */ + private function _createPagesWithMC4WPBlocks(EndToEndTester $I, $mc4wpFormIDs) + { + $pageIDs = array(); + + foreach ($mc4wpFormIDs as $mc4wpFormID) { + $pageIDs[] = $I->havePostInDatabase( + [ + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_title' => 'Page with MC4WP Block #' . $mc4wpFormID, + 'post_content' => '', + ] + ); + } + + 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 From 7d7baa4373664758b90d697a5791c29c9983fd0d Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 19 Jan 2026 16:57:48 +0800 Subject: [PATCH 6/7] PHPStan compat. --- .../class-convertkit-admin-importer.php | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/admin/importers/class-convertkit-admin-importer.php b/admin/importers/class-convertkit-admin-importer.php index 3858fc869..50de5785d 100644 --- a/admin/importers/class-convertkit-admin-importer.php +++ b/admin/importers/class-convertkit-admin-importer.php @@ -275,11 +275,8 @@ public function replace_blocks_in_posts( $third_party_form_id, $form_id ) { // Fetch Blocks from Content. $blocks = parse_blocks( $post_content ); - // Bail if no Blocks exist (i.e. we're not using Gutenberg). - if ( ! is_array( $blocks ) ) { - continue; - } - if ( ! count( $blocks ) ) { + // If a single block was returned with blockName null, this content was not created using the block editor. + if ( count( $blocks ) === 1 && is_null( $blocks[0]['blockName'] ) ) { continue; } @@ -316,11 +313,8 @@ public function replace_blocks_in_content( $content, $third_party_form_id, $form // Fetch Blocks from Content. $blocks = parse_blocks( $content ); - // Bail if no Blocks exist (i.e. we're not using Gutenberg). - if ( ! is_array( $blocks ) ) { - return $content; - } - if ( ! count( $blocks ) ) { + // If a single block was returned with blockName null, this content was not created using the block editor. + if ( count( $blocks ) === 1 && is_null( $blocks[0]['blockName'] ) ) { return $content; } @@ -367,7 +361,7 @@ private function recursively_convert_blocks( $blocks, $third_party_form_id, $for } // Skip if the third party form ID doesn't exist within the third party form block's attribute. - if ( stripos( $block['attrs'][ $this->block_id_attribute ], $third_party_form_id ) === false ) { + if ( stripos( $block['attrs'][ $this->block_id_attribute ], (string) $third_party_form_id ) === false ) { continue; } From a6285b7b5f232a624ca79968672910654bb6893d Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Tue, 20 Jan 2026 15:49:19 +0800 Subject: [PATCH 7/7] Improve test coverage --- .../class-convertkit-admin-importer.php | 90 +++++++++-------- .../PluginSettingsToolsImporterAweberCest.php | 13 ++- .../PluginSettingsToolsImporterMC4WPCest.php | 20 +++- tests/Integration/ImporterTest.php | 99 +++++++++++++------ 4 files changed, 144 insertions(+), 78 deletions(-) diff --git a/admin/importers/class-convertkit-admin-importer.php b/admin/importers/class-convertkit-admin-importer.php index 50de5785d..17e86d60c 100644 --- a/admin/importers/class-convertkit-admin-importer.php +++ b/admin/importers/class-convertkit-admin-importer.php @@ -24,7 +24,7 @@ abstract class ConvertKit_Admin_Importer { public $shortcode_name = ''; /** - * Holds the ID attribute name for the third party Form plugin. + * Holds the shortcode ID attribute name for the third party Form plugin. * * @since 3.1.0 * @@ -42,7 +42,7 @@ abstract class ConvertKit_Admin_Importer { public $block_name = ''; /** - * Holds the ID attribute name for the third party Form plugin. + * Holds the block ID attribute name for the third party Form plugin. * * @since 3.1.6 * @@ -60,7 +60,7 @@ abstract class ConvertKit_Admin_Importer { abstract public function get_forms(); /** - * Returns an array of post IDs that contain the third partyform shortcode. + * Returns an array of post IDs that contain the third party form block or shortcode. * * @since 3.1.5 * @@ -70,7 +70,7 @@ public function get_forms_in_posts() { global $wpdb; - // Search post_content for the third party form shortcode and return array of post IDs. + // Search post_content for the third party form block or shortcode and return array of post IDs. $results = $wpdb->get_col( $wpdb->prepare( " @@ -123,8 +123,8 @@ public function has_forms_in_posts() { * * @since 3.1.0 * - * @param int $third_party_form_id The ID of the third party form. - * @param int $form_id The ID of the Kit form. + * @param int $third_party_form_id Third Party Form ID. + * @param int $form_id Kit Form ID. */ public function replace_shortcodes_in_posts( $third_party_form_id, $form_id ) { @@ -254,8 +254,8 @@ public function get_form_ids_from_content( $content ) { * * @since 3.1.6 * - * @param int $third_party_form_id The ID of the third party form. - * @param int $form_id The ID of the Kit form. + * @param int $third_party_form_id Third Party Form ID. + * @param int $form_id Kit Form ID. */ public function replace_blocks_in_posts( $third_party_form_id, $form_id ) { @@ -269,32 +269,50 @@ public function replace_blocks_in_posts( $third_party_form_id, $form_id ) { // Iterate through Posts and replace the third party Form Block with the Kit Form Block. foreach ( $posts as $post_id ) { - // Get Post content. - $post_content = get_post_field( 'post_content', $post_id ); + $this->replace_blocks_in_post( $post_id, $third_party_form_id, $form_id ); + } - // Fetch Blocks from Content. - $blocks = parse_blocks( $post_content ); + } - // If a single block was returned with blockName null, this content was not created using the block editor. - if ( count( $blocks ) === 1 && is_null( $blocks[0]['blockName'] ) ) { - continue; - } + /** + * Replaces the third party form block with the Kit form block in the given post. + * + * @since 3.1.6 + * + * @param int $post_id Post ID. + * @param int $third_party_form_id Third Party Form ID. + * @param int $form_id Kit Form ID. + */ + public function replace_blocks_in_post( $post_id, $third_party_form_id, $form_id ) { - // Replace the third party Form Block with the Kit Form Block. - $post_content = $this->replace_blocks_in_content( $post_content, $third_party_form_id, $form_id ); + // Get Post content. + $post_content = get_post_field( 'post_content', $post_id ); - // Update the Post content. - wp_update_post( - array( - 'ID' => $post_id, - 'post_content' => $post_content, - ), - false, - false // Don't fire after action hooks. - ); + // Fetch Blocks from Content. + $blocks = parse_blocks( $post_content ); + // If a single block was returned with blockName null, this content was not created using the block editor. + if ( count( $blocks ) === 1 && is_null( $blocks[0]['blockName'] ) ) { + return; } + // Replace the third party Form Block with the Kit Form Block. + $post_content = $this->replace_blocks_in_content( $blocks, $third_party_form_id, $form_id ); + + // Double escape backslashes so that wp_update_post doesn't remove them. + // When content contains a single backslash (\), wp_update_post will strip it unless we double escape it (\\). + $post_content = str_replace( '\\', '\\\\', $post_content ); + + // Update the Post content. + wp_update_post( + array( + 'ID' => $post_id, + 'post_content' => $post_content, + ), + false, + false // Don't fire after action hooks. + ); + } /** @@ -302,21 +320,13 @@ public function replace_blocks_in_posts( $third_party_form_id, $form_id ) { * * @since 3.1.6 * - * @param string $content Content containing third party Form Blocks. - * @param int $third_party_form_id Third Party Form ID. - * @param int $form_id Kit Form ID. - + * @param array $blocks Blocks. + * @param int $third_party_form_id Third Party Form ID. + * @param int $form_id Kit Form ID. + * * @return string */ - public function replace_blocks_in_content( $content, $third_party_form_id, $form_id ) { - - // Fetch Blocks from Content. - $blocks = parse_blocks( $content ); - - // If a single block was returned with blockName null, this content was not created using the block editor. - if ( count( $blocks ) === 1 && is_null( $blocks[0]['blockName'] ) ) { - return $content; - } + public function replace_blocks_in_content( $blocks, $third_party_form_id, $form_id ) { // Recursively convert blocks. $blocks = $this->recursively_convert_blocks( $blocks, $third_party_form_id, $form_id ); diff --git a/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterAweberCest.php b/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterAweberCest.php index 0df53fb63..9c9046a67 100644 --- a/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterAweberCest.php +++ b/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterAweberCest.php @@ -68,7 +68,7 @@ public function testAWeberImportWithShortcodes(EndToEndTester $I) /** * Test that AWeber Blocks are replaced with Kit Blocks when the Tools > AWeber: Migrate Configuration is configured. * - * @since 3.1.5 + * @since 3.1.6 * * @param EndToEndTester $I Tester. */ @@ -99,10 +99,15 @@ public function testAWeberImportWithBlocks(EndToEndTester $I) $I->waitForElementVisible('.notice-success'); $I->see('AWeber forms migrated successfully.'); - // View the Pages, to confirm Kit Forms now display. + // Test each Page. foreach ($pageIDs as $pageID) { $I->amOnPage('?p=' . $pageID); + + // Check Kit Form block is displayed. $I->seeElementInDOM('form[data-sv-form]'); + + // Confirm special characters have not been stripped. + $I->seeInSource('!@£$%^&*()_+~!@£$%^&*()_+\\'); } } @@ -208,6 +213,8 @@ private function _createPagesWithAWeberFormShortcodes(EndToEndTester $I, $aweber 'post_status' => 'publish', 'post_title' => 'Page with AWeber Form #' . $aweberFormID, 'post_content' => '[aweber formid="' . $aweberFormID . '"]', + + // Configure Kit Plugin to not display a default Form, so we test against the Kit Form in the content. 'meta_input' => [ '_wp_convertkit_post_meta' => [ 'form' => '0', @@ -243,7 +250,7 @@ private function _createPagesWithAWeberBlocks(EndToEndTester $I, $aweberFormIDs) 'post_title' => 'Page with AWeber Block #' . $aweberFormID, 'post_content' => '
[aweber listid=6924484 formid=' . $aweberFormID . ' formtype=webform]
-', +
Some content with characters !@£$%^&*()_+~!@£$%^&*()_+\\\
', 'meta_input' => [ '_wp_convertkit_post_meta' => [ 'form' => '0', diff --git a/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterMC4WPCest.php b/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterMC4WPCest.php index 2336c6d23..580ac359f 100644 --- a/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterMC4WPCest.php +++ b/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterMC4WPCest.php @@ -70,7 +70,7 @@ public function testMC4WPImportWithShortcodes(EndToEndTester $I) /** * Test that Mailchimp Blocks are replaced with Kit Blocks when the Tools > MC4WP: Migrate Configuration is configured. * - * @since 3.1.0 + * @since 3.1.6 * * @param EndToEndTester $I Tester. */ @@ -101,10 +101,15 @@ public function testMC4WPImportWithBlocks(EndToEndTester $I) $I->waitForElementVisible('.notice-success'); $I->see('MC4WP forms migrated successfully.'); - // View the Pages, to confirm Kit Forms now display. + // Test each Page. foreach ($pageIDs as $pageID) { $I->amOnPage('?p=' . $pageID); + + // Check Kit Form block is displayed. $I->seeElementInDOM('form[data-sv-form]'); + + // Confirm special characters have not been stripped. + $I->seeInSource('!@£$%^&*()_+~!@£$%^&*()_+\\'); } } @@ -257,7 +262,16 @@ private function _createPagesWithMC4WPBlocks(EndToEndTester $I, $mc4wpFormIDs) 'post_type' => 'page', 'post_status' => 'publish', 'post_title' => 'Page with MC4WP Block #' . $mc4wpFormID, - 'post_content' => '', + 'post_content' => '
Some content with characters !@£$%^&*()_+~!@£$%^&*()_+\\\
', + + // Configure Kit Plugin to not display a default Form, so we test against the Kit Form in the content. + 'meta_input' => [ + '_wp_convertkit_post_meta' => [ + 'form' => '0', + 'landing_page' => '', + 'tag' => '', + ], + ], ] ); } diff --git a/tests/Integration/ImporterTest.php b/tests/Integration/ImporterTest.php index 6ce2175ee..886c2a761 100644 --- a/tests/Integration/ImporterTest.php +++ b/tests/Integration/ImporterTest.php @@ -18,6 +18,15 @@ class ImporterTest extends WPTestCase */ protected $tester; + /** + * Holds the HTML block to test, which includes special characters. + * + * @since 3.1.6 + * + * @var string + */ + private $html_block = '
Some content with characters !@£$%^&*()_+~!@£$%^&*()_+\
'; + /** * Performs actions before each test. * @@ -154,11 +163,12 @@ public function testAWeberReplaceShortcodesInContentIgnoringOtherShortcodes() } /** - * Test that the replace_blocks_in_content() method replaces the third party form block with the Kit form block. + * Test that the replace_blocks_in_post() method replaces the third party form block with the Kit form block, + * and special characters are not stripped when the Post is saved. * - * @since 3.1.5 + * @since 3.1.6 */ - public function testAWeberReplaceBlocksInContent() + public function testAWeberReplaceBlocksInPost() { // Initialize the class we want to test. $this->importer = new \ConvertKit_Admin_Importer_AWeber(); @@ -166,24 +176,39 @@ public function testAWeberReplaceBlocksInContent() // Confirm initialization didn't result in an error. $this->assertNotInstanceOf(\WP_Error::class, $this->importer); - // Define the blocks to test. - $content = ' + // Create a Post with an AWeber form block and HTML block, as if the user already created this post. + $postID = $this->factory->post->create( + [ + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_title' => 'AWeber: Replace Blocks in Post', + 'post_content' => str_replace( + '\\', + '\\\\', + '
[aweber listid=6924484 formid=289586845 formtype=webform]
-'; +' . $this->html_block + ), + ] + ); - // Test the block is replaced with the Kit form block. + // Replace the blocks in the post. + $this->importer->replace_blocks_in_post( $postID, 289586845, $_ENV['CONVERTKIT_API_FORM_ID'] ); + + // Test the block is replaced with the Kit form block, and special characters are not stripped. $this->assertEquals( - '', - $this->importer->replace_blocks_in_content( $content, 289586845, $_ENV['CONVERTKIT_API_FORM_ID'] ) + '' . $this->html_block, + get_post_field( 'post_content', $postID ) ); } /** - * Test that the replace_blocks_in_content() method ignores non-AWeber blocks. + * Test that the replace_blocks_in_content() method replaces the third party form block with the Kit form block, + * and special characters are not stripped. * - * @since 3.1.5 + * @since 3.1.6 */ - public function testAWeberReplaceBlocksInContentIgnoringOtherBlocks() + public function testAWeberReplaceBlocksInContent() { // Initialize the class we want to test. $this->importer = new \ConvertKit_Admin_Importer_AWeber(); @@ -192,15 +217,14 @@ public function testAWeberReplaceBlocksInContentIgnoringOtherBlocks() $this->assertNotInstanceOf(\WP_Error::class, $this->importer); // Define the blocks to test. - $html_block = '
Some content
'; - $content = ' + $content = '
[aweber listid=6924484 formid=289586845 formtype=webform]
-' . $html_block; +' . $this->html_block; // Test the block is replaced with the Kit form block. $this->assertEquals( - '' . $html_block, - $this->importer->replace_blocks_in_content( $content, 289586845, $_ENV['CONVERTKIT_API_FORM_ID'] ) + '' . $this->html_block, + $this->importer->replace_blocks_in_content( parse_blocks( $content ), 289586845, $_ENV['CONVERTKIT_API_FORM_ID'] ) ); } @@ -309,11 +333,12 @@ public function testMC4WPReplaceShortcodesInContentIgnoringOtherShortcodes() } /** - * Test that the replace_blocks_in_content() method replaces the third party form block with the Kit form block. + * Test that the replace_blocks_in_post() method replaces the third party form block with the Kit form block, + * and special characters are not stripped when the Post is saved. * - * @since 3.1.5 + * @since 3.1.6 */ - public function testMC4WPReplaceBlocksInContent() + public function testMC4WPReplaceBlocksInPost() { // Initialize the class we want to test. $this->importer = new \ConvertKit_Admin_Importer_MC4WP(); @@ -321,22 +346,33 @@ public function testMC4WPReplaceBlocksInContent() // Confirm initialization didn't result in an error. $this->assertNotInstanceOf(\WP_Error::class, $this->importer); - // Define the blocks to test. - $content = ''; + // Create a Post with a MC4WP form block and HTML block, as if the user already created this post. + $postID = $this->factory->post->create( + [ + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_title' => 'Mailchimp 4 WP: Replace Blocks in Post', + 'post_content' => str_replace( '\\', '\\\\', '' . $this->html_block ), + ] + ); - // Test the block is replaced with the Kit form block. + // Replace the blocks in the post. + $this->importer->replace_blocks_in_post( $postID, 4410, $_ENV['CONVERTKIT_API_FORM_ID'] ); + + // Test the block is replaced with the Kit form block, and special characters are not stripped. $this->assertEquals( - '', - $this->importer->replace_blocks_in_content( $content, 4410, $_ENV['CONVERTKIT_API_FORM_ID'] ) + '' . $this->html_block, + get_post_field( 'post_content', $postID ) ); } /** - * Test that the replace_blocks_in_content() method ignores non-MC4WP blocks. + * Test that the replace_blocks_in_content() method replaces the third party form block with the Kit form block, + * and special characters are not stripped. * - * @since 3.1.5 + * @since 3.1.6 */ - public function testMC4WPReplaceBlocksInContentIgnoringOtherBlocks() + public function testMC4WPReplaceBlocksInContent() { // Initialize the class we want to test. $this->importer = new \ConvertKit_Admin_Importer_MC4WP(); @@ -345,13 +381,12 @@ public function testMC4WPReplaceBlocksInContentIgnoringOtherBlocks() $this->assertNotInstanceOf(\WP_Error::class, $this->importer); // Define the blocks to test. - $html_block = '
Some content
'; - $content = '' . $html_block; + $content = '' . $this->html_block; // Test the block is replaced with the Kit form block. $this->assertEquals( - '' . $html_block, - $this->importer->replace_blocks_in_content( $content, 4410, $_ENV['CONVERTKIT_API_FORM_ID'] ) + '' . $this->html_block, + $this->importer->replace_blocks_in_content( parse_blocks( $content ), 4410, $_ENV['CONVERTKIT_API_FORM_ID'] ) ); } }