Skip to content

Commit

Permalink
Merge pull request #747 from Kit/fix-tests-doing-it-wrong-notice
Browse files Browse the repository at this point in the history
Tests: Fix `_load_textdomain_just_in_time` PHP Notice
  • Loading branch information
n7studios authored Nov 27, 2024
2 parents 2730e8f + 4e69f84 commit 5d38edf
Show file tree
Hide file tree
Showing 16 changed files with 210 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ jobs:
DB_USER: root
DB_PASS: root
DB_HOST: localhost
INSTALL_PLUGINS: "admin-menu-editor autoptimize beaver-builder-lite-version contact-form-7 classic-editor custom-post-type-ui elementor forminator jetpack-boost woocommerce wordpress-seo wpforms-lite litespeed-cache wp-crontrol wp-super-cache w3-total-cache wp-fastest-cache wp-optimize sg-cachepress" # Don't include this repository's Plugin here.
INSTALL_PLUGINS_URLS: "https://downloads.wordpress.org/plugin/convertkit-for-woocommerce.1.6.4.zip http://cktestplugins.wpengine.com/wp-content/uploads/2024/01/convertkit-action-filter-tests.zip" # URLs to specific third party Plugins
INSTALL_PLUGINS: "admin-menu-editor autoptimize beaver-builder-lite-version block-visibility contact-form-7 classic-editor custom-post-type-ui elementor forminator jetpack-boost woocommerce wordpress-seo wpforms-lite litespeed-cache wp-crontrol wp-super-cache w3-total-cache wp-fastest-cache wp-optimize sg-cachepress" # Don't include this repository's Plugin here.
INSTALL_PLUGINS_URLS: "https://downloads.wordpress.org/plugin/convertkit-for-woocommerce.1.6.4.zip http://cktestplugins.wpengine.com/wp-content/uploads/2024/01/convertkit-action-filter-tests.zip http://cktestplugins.wpengine.com/wp-content/uploads/2024/11/disable-doing-it-wrong-notices.zip" # URLs to specific third party Plugins
CONVERTKIT_API_KEY: ${{ secrets.CONVERTKIT_API_KEY }} # ConvertKit API Key, stored in the repository's Settings > Secrets
CONVERTKIT_API_SECRET: ${{ secrets.CONVERTKIT_API_SECRET }} # ConvertKit API Secret, stored in the repository's Settings > Secrets
CONVERTKIT_API_KEY_NO_DATA: ${{ secrets.CONVERTKIT_API_KEY_NO_DATA }} # ConvertKit API Key for ConvertKit account with no data, stored in the repository's Settings > Secrets
Expand Down
7 changes: 7 additions & 0 deletions includes/blocks/class-convertkit-block-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,13 @@ public function get_default_values() {
*/
public function render( $atts ) {

// Check if the Block Visibility Plugin permits displaying this block.
if ( ! $this->is_block_visible( $atts ) ) {
// Block should not be displayed due to Block Visibility Plugin conditions.
// Return a blank string now.
return '';
}

// Parse shortcode attributes, defining fallback defaults if required.
$atts = shortcode_atts(
$this->get_default_values(),
Expand Down
45 changes: 45 additions & 0 deletions includes/blocks/class-convertkit-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,49 @@ public function is_block_editor_request() {

}

/**
* If the Block Visiblity Plugin is active, run the block through its conditions now.
* We don't wait for Block Visibility to do this, as it performs this on the
* `render_block` filter, by which time the code in this method has fully executed,
* meaning any non-inline Forms will have had their scripts added to the
* `convertkit_output_scripts_footer` hook.
* As a result, the non-inline Form will always display, regardless of whether
* Block Visibility's conditions are met.
* We deliberately don't output non-inline Forms in their block, instead deferring
* to the `convertkit_output_scripts_footer` hook, to ensure the non-inline Forms
* styling are not constrained by the Theme's width, layout or other properties.
*
* @since 2.6.6
*
* @param array $atts Block Attributes.
* @return bool Display Block
*/
public function is_block_visible( $atts ) {

// Display the block if the Block Visibility Plugin isn't active.
if ( ! function_exists( '\BlockVisibility\Frontend\render_with_visibility' ) ) {
return true;
}

// Determine whether the block should display.
$display_block = \BlockVisibility\Frontend\render_with_visibility(
'block',
array(
'blockName' => 'convertkit-' . $this->get_name(),
'attrs' => $atts,
)
);

// If the content returned is a blank string, conditions on this block set
// by the user in the Block Visibility Plugin resulted in the block not displaying.
// Don't display it.
if ( empty( $display_block ) ) {
return false;
}

// If here, the block can be displayed.
return true;

}

}
9 changes: 9 additions & 0 deletions includes/class-convertkit-output.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,15 @@ public function append_form_to_content( $content ) {
*/
private function inject_form_after_element( $content, $tag, $index, $form ) {

// Wrap content in <html>, <head> and <body> tags now, so we can inject the UTF-8 Content-Type meta tag.
$content = '<html><head></head><body>' . $content . '</body></html>';

// Forcibly tell DOMDocument that this HTML uses the UTF-8 charset.
// <meta charset="utf-8"> isn't enough, as DOMDocument still interprets the HTML as ISO-8859, which breaks character encoding
// Use of mb_convert_encoding() with HTML-ENTITIES is deprecated in PHP 8.2, so we have to use this method.
// If we don't, special characters render incorrectly.
$content = str_replace( '<head>', '<head>' . "\n" . '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">', $content );

// Load Page / Post content into DOMDocument.
libxml_use_internal_errors( true );
$html = new DOMDocument();
Expand Down
13 changes: 11 additions & 2 deletions tests/_support/Helper/Acceptance/WPCron.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
*/
class WPCron extends \Codeception\Module
{
/**
* Holds the admin UI URL for WP Crontrol.
*
* @since 2.6.6
*
* @var string
*/
private $adminURL = 'tools.php?page=wp-crontrol';

/**
* Asserts if the given event name is scheduled in WordPress' Cron.
*
Expand Down Expand Up @@ -49,7 +58,7 @@ public function dontSeeCronEvent($I, $name)
public function runCronEvent($I, $name)
{
// List cron event in WP-Crontrol Plugin.
$I->amOnAdminPage('tools.php?page=crontrol_admin_manage_page&s=' . $name);
$I->amOnAdminPage($this->adminURL . '&s=' . $name);

// Hover mouse over event's name.
$I->moveMouseOver('#the-list tr');
Expand All @@ -74,7 +83,7 @@ public function runCronEvent($I, $name)
public function deleteCronEvent($I, $name)
{
// List cron event in WP-Crontrol Plugin.
$I->amOnAdminPage('tools.php?page=crontrol_admin_manage_page&s=' . $name);
$I->amOnAdminPage($this->adminURL . '&s=' . $name);

// Hover mouse over event's name.
$I->moveMouseOver('#the-list tr');
Expand Down
2 changes: 1 addition & 1 deletion tests/_support/Helper/Acceptance/WPGutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ public function addGutenbergPageToDatabase($I, $postType = 'page', $title = 'Gut
<!-- /wp:heading -->
<!-- wp:paragraph -->
<p>Item #2</p>
<p>Item #2: Adhaésionés altéram improbis mi pariendarum sit stulti triarium</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column -->
Expand Down
49 changes: 49 additions & 0 deletions tests/acceptance/forms/blocks-shortcodes/PageBlockFormCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,55 @@ public function testFormBlockWithWPRocketPluginMinifyJS(AcceptanceTester $I)
$I->deactivateThirdPartyPlugin($I, 'wp-rocket');
}

/**
* Test that a non-inline Form is not displayed when specified in the Form Block and the
* Block Visibility Plugin sets the block to not display.
*
* @since 2.6.6
*
* @param AcceptanceTester $I Tester.
*/
public function testFormBlockWithNonInlineFormAndBlockVisibilityPlugin(AcceptanceTester $I)
{
// Setup Plugin and Resources.
$I->setupConvertKitPlugin($I);
$I->setupConvertKitPluginResources($I);

// Activate Block Visibility Plugin.
$I->activateThirdPartyPlugin($I, 'block-visibility');

// Create a Page as if it were create in Gutenberg with the Form block
// set to display a non-inline Form, and the Block Visibility Plugin
// set to hide the block.
$pageID = $I->havePostInDatabase(
[
'post_type' => 'page',
'post_title' => 'Kit: Page: Form: Block: Block Visibility',
'post_content' => '<!-- wp:convertkit/form {"form":"' . $_ENV['CONVERTKIT_API_FORM_FORMAT_STICKY_BAR_ID'] . '","blockVisibility":{"controlSets":[{"id":1,"enable":true,"controls":{"location":{"ruleSets":[{"enable":true,"rules":[{"field":"postType","operator":"any","value":["page"]}]}],"hideOnRuleSets":true}}}]}} /-->',
'meta_input' => [
// Configure ConvertKit Plugin to not display a default Form.
'_wp_convertkit_post_meta' => [
'form' => '0',
'landing_page' => '',
'tag' => '',
],
],
]
);

// Load Page.
$I->amOnPage('?p=' . $pageID);

// Check that no PHP warnings or notices were output.
$I->checkNoWarningsAndNoticesOnScreen($I);

// Confirm that no ConvertKit Form is displayed.
$I->dontSeeElementInDOM('form[data-sv-form]');

// Deactivate Block Visibility Plugin.
$I->deactivateThirdPartyPlugin($I, 'block-visibility');
}

/**
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ public function testFormShortcodeWithPerfmattersPlugin(AcceptanceTester $I)
$I->setupConvertKitPluginResources($I);

// Activate Perfmatters Plugin.
$I->activateThirdPartyPlugin($I, 'disable-_load_textdomain_just_in_time-doing_it_wrong-notice');
$I->activateThirdPartyPlugin($I, 'perfmatters');

// Enable Defer and Delay JavaScript.
Expand Down Expand Up @@ -700,6 +701,7 @@ public function testFormShortcodeWithPerfmattersPlugin(AcceptanceTester $I)

// Deactivate Perfmatters Plugin.
$I->deactivateThirdPartyPlugin($I, 'perfmatters');
$I->deactivateThirdPartyPlugin($I, 'disable-_load_textdomain_just_in_time-doing_it_wrong-notice');
}

/**
Expand Down Expand Up @@ -770,6 +772,7 @@ public function testFormShortcodeWithWPRocketPlugin(AcceptanceTester $I)
$I->setupConvertKitPluginResources($I);

// Activate WP Rocket Plugin.
$I->activateThirdPartyPlugin($I, 'disable-_load_textdomain_just_in_time-doing_it_wrong-notice');
$I->activateThirdPartyPlugin($I, 'wp-rocket');

// Configure WP Rocket.
Expand Down Expand Up @@ -806,6 +809,7 @@ public function testFormShortcodeWithWPRocketPlugin(AcceptanceTester $I)

// Deactivate WP Rocket Plugin.
$I->deactivateThirdPartyPlugin($I, 'wp-rocket');
$I->deactivateThirdPartyPlugin($I, 'disable-_load_textdomain_just_in_time-doing_it_wrong-notice');
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/acceptance/forms/post-types/CPTFormCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ public function testAddNewCPTUsingDefaultFormAfterParagraphElement(AcceptanceTes

// Confirm that one ConvertKit Form is output in the DOM after the third paragraph.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_element', 'p', 3);

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down Expand Up @@ -350,6 +353,9 @@ public function testAddNewCPTUsingDefaultFormAfterHeadingElement(AcceptanceTeste

// Confirm that one ConvertKit Form is output in the DOM after the second <h2> element.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_element', 'h2', 2);

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down Expand Up @@ -386,6 +392,9 @@ public function testAddNewCPTUsingDefaultFormAfterImageElement(AcceptanceTester

// Confirm that one ConvertKit Form is output in the DOM after the second <img> element.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_element', 'img', 2);

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down Expand Up @@ -423,6 +432,9 @@ public function testAddNewCPTUsingDefaultFormAfterOutOfBoundsElement(AcceptanceT
// Confirm that one ConvertKit Form is output in the DOM after the content, as
// the number of paragraphs is less than the position.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_content');

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down
48 changes: 48 additions & 0 deletions tests/acceptance/forms/post-types/PageFormCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,48 @@ public function testAddNewPageUsingDefaultFormAfterParagraphElement(AcceptanceTe

// Confirm that one ConvertKit Form is output in the DOM after the third paragraph.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_element', 'p', 3);

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
* Test that the Default Form specified in the Plugin Settings works when
* creating and viewing a new WordPress Page, and its position is set
* to after the 2nd <h2> element.
*
* @since 2.6.6
*
* @param AcceptanceTester $I Tester.
*/
public function testAddNewPageUsingDefaultFormAfterHeadingElement(AcceptanceTester $I)
{
// Setup ConvertKit plugin with Default Form for Pages set to be output after the 2nd <h2> of content.
$I->setupConvertKitPlugin(
$I,
[
'page_form' => $_ENV['CONVERTKIT_API_FORM_ID'],
'page_form_position' => 'after_element',
'page_form_position_element' => 'h2',
'page_form_position_element_index' => 2,
]
);
$I->setupConvertKitPluginResources($I);

// Setup Page with placeholder content.
$pageID = $I->addGutenbergPageToDatabase($I, 'page', 'Kit: Page: Form: Default: After 2nd H2 Element');

// View the Page on the frontend site.
$I->amOnPage('?p=' . $pageID);

// Check that no PHP warnings or notices were output.
$I->checkNoWarningsAndNoticesOnScreen($I);

// Confirm that one ConvertKit Form is output in the DOM after the second <h2> element.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_element', 'h2', 2);

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down Expand Up @@ -274,6 +316,9 @@ public function testAddNewPageUsingDefaultFormAfterImageElement(AcceptanceTester

// Confirm that one ConvertKit Form is output in the DOM after the second <img> element.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_element', 'img', 2);

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down Expand Up @@ -311,6 +356,9 @@ public function testAddNewPageUsingDefaultFormAfterOutOfBoundsElement(Acceptance
// Confirm that one ConvertKit Form is output in the DOM after the content, as
// the number of paragraphs is less than the position.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_content');

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/acceptance/forms/post-types/PostFormCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ public function testAddNewPostUsingDefaultFormAfterParagraphElement(AcceptanceTe

// Confirm that one ConvertKit Form is output in the DOM after the third paragraph.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_element', 'p', 3);

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down Expand Up @@ -273,6 +276,9 @@ public function testAddNewPostUsingDefaultFormAfterHeadingElement(AcceptanceTest

// Confirm that one ConvertKit Form is output in the DOM after the second <h2> element.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_element', 'h2', 2);

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down Expand Up @@ -309,6 +315,9 @@ public function testAddNewPostUsingDefaultFormAfterImageElement(AcceptanceTester

// Confirm that one ConvertKit Form is output in the DOM after the second <img> element.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_element', 'img', 2);

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down Expand Up @@ -346,6 +355,9 @@ public function testAddNewPostUsingDefaultFormAfterOutOfBoundsElement(Acceptance
// Confirm that one ConvertKit Form is output in the DOM after the content, as
// the number of paragraphs is less than the position.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_content');

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/acceptance/integrations/other/DiviBroadcastsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class DiviBroadcastsCest
public function _before(AcceptanceTester $I)
{
$I->activateConvertKitPlugin($I);
$I->activateThirdPartyPlugin($I, 'disable-_load_textdomain_just_in_time-doing_it_wrong-notice');
$I->activateThirdPartyPlugin($I, 'divi-builder');
}

Expand Down Expand Up @@ -173,6 +174,7 @@ public function testBroadcastsModuleInFrontendEditorWhenNoBroadcasts(AcceptanceT
public function _passed(AcceptanceTester $I)
{
$I->deactivateThirdPartyPlugin($I, 'divi-builder');
$I->deactivateThirdPartyPlugin($I, 'disable-_load_textdomain_just_in_time-doing_it_wrong-notice');
$I->deactivateConvertKitPlugin($I);
$I->resetConvertKitPlugin($I);
}
Expand Down
Loading

0 comments on commit 5d38edf

Please sign in to comment.