Skip to content

Commit

Permalink
Completed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
n7studios committed Dec 11, 2024
1 parent 5fccd4e commit 3aeec1c
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 36 deletions.
49 changes: 28 additions & 21 deletions includes/class-convertkit-preview-output.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function preview_form( $form_id ) {
}

/**
* Adds a non-inline form for display if the request is from a logged in user who has clicked a preview link
* Adds non-inline form(s) for display if the request is from a logged in user who has clicked a preview link
* and is viewing the home page.
*
* @since 2.3.3
Expand All @@ -99,33 +99,40 @@ public function preview_non_inline_form() {
return;
}

// Determine the form to preview.
$preview_form_id = (int) ( isset( $_REQUEST['convertkit_form_id'] ) ? sanitize_text_field( $_REQUEST['convertkit_form_id'] ) : 0 );
// Bail if no form(s) to preview.
if ( ! isset( $_REQUEST['convertkit_form_id'] ) ) {
return;
}

// Get form.
// Determine form ID(s) to preview.
$convertkit_forms = new ConvertKit_Resource_Forms();
$form = $convertkit_forms->get_by_id( $preview_form_id );
$preview_form_ids = explode( ',', sanitize_text_field( $_REQUEST['convertkit_form_id'] ) );

// Bail if the Form doesn't exist (this shouldn't happen, but you never know).
if ( ! $form ) {
return;
}
foreach ( $preview_form_ids as $preview_form_id ) {
// Get form.
$form = $convertkit_forms->get_by_id( (int) $preview_form_id );

// Add the form to the scripts array so it is included in the preview.
add_filter(
'convertkit_output_scripts_footer',
function ( $scripts ) use ( $form ) {
// Bail if the Form doesn't exist (this shouldn't happen, but you never know).
if ( ! $form ) {
continue;
}

$scripts[] = array(
'async' => true,
'data-uid' => $form['uid'],
'src' => $form['embed_js'],
);
// Add the form to the scripts array so it is included in the preview.
add_filter(
'convertkit_output_scripts_footer',
function ( $scripts ) use ( $form ) {

return $scripts;
$scripts[] = array(
'async' => true,
'data-uid' => $form['uid'],
'src' => $form['embed_js'],
);

}
);
return $scripts;

}
);
}

}

Expand Down
6 changes: 3 additions & 3 deletions includes/class-convertkit-resource-forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function get_select_field_all( $name, $id, $css_classes, $selected_option
* @param string $name Name.
* @param string $id ID.
* @param bool|array $css_classes <select> CSS class(es).
* @param string $selected_options <option> values to mark as selected.
* @param array $selected_options <option> values to mark as selected.
* @param bool|array $prepend_options <option> elements to prepend before resources.
* @param bool|array $attributes <select> attributes.
* @param bool|string|array $description Description.
Expand Down Expand Up @@ -305,7 +305,7 @@ private function get_multi_select_field( $forms, $name, $id, $css_classes, $sele
$html .= sprintf(
'<option value="%s" data-preserve-on-refresh="1"%s>%s</option>',
esc_attr( $value ),
( in_array( $value, $selected_options, false ) ? ' selected' : '' ),
( in_array( $value, $selected_options, true ) ? ' selected' : '' ),
esc_attr( $label )
);
}
Expand All @@ -318,7 +318,7 @@ private function get_multi_select_field( $forms, $name, $id, $css_classes, $sele
$html .= sprintf(
'<option value="%s"%s>%s [%s]</option>',
esc_attr( $form['id'] ),
( in_array( $form['id'], $selected_options, false ) ? ' selected' : '' ),
( in_array( $form['id'], $selected_options, true ) ? ' selected' : '' ),
esc_attr( $form['name'] ),
( ! empty( $form['format'] ) ? esc_attr( $form['format'] ) : 'inline' )
);
Expand Down
9 changes: 5 additions & 4 deletions includes/class-convertkit-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,11 @@ public function get_non_inline_form() {

// 2.6.8 and earlier stored a single Form ID in a string.
if ( is_string( $this->settings['non_inline_form'] ) ) {
return array( $this->settings['non_inline_form'] );
return array( (int) $this->settings['non_inline_form'] );
}

return $this->settings['non_inline_form'];
// Cast values to integers and return.
return array_map( 'intval', $this->settings['non_inline_form'] );

}

Expand All @@ -383,13 +384,13 @@ public function has_non_inline_form() {
// 2.6.8 and earlier stored a single Form ID in a string.
if ( is_string( $this->settings['non_inline_form'] ) ) {
if ( ! empty( $this->settings['non_inline_form'] ) ) {
return array( $this->settings['non_inline_form'] );
return true;
}

return false;
}

return ( count( $this->settings['non_inline_form'] ) );
return ( count( $this->settings['non_inline_form'] ) > 0 ? true : false );

}

Expand Down
19 changes: 19 additions & 0 deletions tests/_support/Helper/Acceptance/Select2.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,23 @@ public function fillSelect2Field($I, $container, $value, $ariaAttributeName = 'a
$I->waitForElementVisible('ul#select2-' . $fieldName . '-results li.select2-results__option--highlighted');
$I->pressKey('.select2-search__field[' . $ariaAttributeName . '="select2-' . $fieldName . '-results"]', \Facebook\WebDriver\WebDriverKeys::ENTER);
}

/**
* Helper method to enter text into a jQuery Select2 multiple selection field, selecting the option that appears.
*
* @since 2.6.9
*
* @param AcceptanceTester $I Tester.
* @param string $container Field CSS Class / ID.
* @param string $value Field Value.
* @param string $ariaAttributeName Aria Attribute Name (aria-controls|aria-owns).
*/
public function fillSelect2MultipleField($I, $container, $value, $ariaAttributeName = 'aria-describedby')
{
$fieldID = $I->grabAttributeFrom($container, 'id');
$fieldName = str_replace('-container', '', str_replace('select2-', '', $fieldID));
$I->fillField('.select2-search__field[' . $ariaAttributeName . '="select2-' . $fieldName . '-container"]', $value);
$I->waitForElementVisible('ul#select2-' . $fieldName . '-results li.select2-results__option--highlighted');
$I->pressKey('.select2-search__field[' . $ariaAttributeName . '="select2-' . $fieldName . '-container"]', \Facebook\WebDriver\WebDriverKeys::ENTER);
}
}
73 changes: 65 additions & 8 deletions tests/acceptance/general/PluginSettingsGeneralCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,38 @@ public function testChangeDefaultFormSettingAndPreviewFormLinks(AcceptanceTester
// Close newly opened tab.
$I->closeTab();

// Select a non-inline form for the Default non-inline form setting.
$I->fillSelect2Field($I, '#select2-_wp_convertkit_settings_non_inline_form-container', $_ENV['CONVERTKIT_API_FORM_FORMAT_STICKY_BAR_NAME']);
// Click the Save Changes button.
$I->click('Save Changes');

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

// Check the value of the fields match the inputs provided.
$I->seeInField('_wp_convertkit_settings[page_form]', $_ENV['CONVERTKIT_API_FORM_NAME']);
$I->seeInField('_wp_convertkit_settings[page_form_position]', 'Before Page content');
$I->seeInField('_wp_convertkit_settings[post_form]', $_ENV['CONVERTKIT_API_FORM_NAME']);
$I->seeInField('_wp_convertkit_settings[post_form_position]', 'After Post content');
}

/**
* Test that no PHP errors or notices are displayed on the Plugin's Setting screen,
* when the Default Forms (Site Wide) setting is changed, and that the preview links
* work when one or more Default Forms are chosen.
*
* @since 2.6.9
*
* @param AcceptanceTester $I Tester.
*/
public function testChangeDefaultSiteWideFormsSettingAndPreviewFormLinks(AcceptanceTester $I)
{
// Setup Plugin, without defining default Forms.
$I->setupConvertKitPluginNoDefaultForms($I);

// Go to the Plugin's Settings Screen.
$I->loadConvertKitSettingsGeneralScreen($I);

// Select the Sticky Bar Form for the Site Wide option.
$I->fillSelect2MultipleField($I, '#select2-_wp_convertkit_settings_non_inline_form-container', $_ENV['CONVERTKIT_API_FORM_FORMAT_STICKY_BAR_NAME']);

// Open preview.
$I->click('a#convertkit-preview-non-inline-form');
Expand All @@ -291,25 +321,52 @@ public function testChangeDefaultFormSettingAndPreviewFormLinks(AcceptanceTester
// Switch to newly opened tab.
$I->switchToNextTab();

// Confirm that one ConvertKit Form is output in the DOM.
// Confirm that the preview is the Home Page.
$I->seeElementInDOM('body.home');

// Confirm that one Kit Form is output in the DOM.
// This confirms that there is only one script on the page for this form, which renders the form.
$I->seeNumberOfElementsInDOM('form[data-sv-form="' . $_ENV['CONVERTKIT_API_FORM_FORMAT_STICKY_BAR_ID'] . '"]', 1);

// Close newly opened tab.
$I->closeTab();

// Select a second Modal Form for the Site Wide option.
$I->fillSelect2MultipleField($I, '#select2-_wp_convertkit_settings_non_inline_form-container', $_ENV['CONVERTKIT_API_FORM_FORMAT_MODAL_NAME']);

// Open preview.
$I->click('a#convertkit-preview-non-inline-form');
$I->wait(2); // Required, otherwise switchToNextTab fails.

// Switch to newly opened tab.
$I->switchToNextTab();

// Confirm that the preview is the Home Page.
$I->seeElementInDOM('body.home');

// Confirm that two Kit Forms are output in the DOM.
$I->seeNumberOfElementsInDOM('form[data-sv-form="' . $_ENV['CONVERTKIT_API_FORM_FORMAT_STICKY_BAR_ID'] . '"]', 1);
$I->seeNumberOfElementsInDOM('form[data-sv-form="' . $_ENV['CONVERTKIT_API_FORM_FORMAT_MODAL_ID'] . '"]', 1);

// Close newly opened tab.
$I->closeTab();

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

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

// Check the value of the fields match the inputs provided.
$I->seeInField('_wp_convertkit_settings[page_form]', $_ENV['CONVERTKIT_API_FORM_NAME']);
$I->seeInField('_wp_convertkit_settings[page_form_position]', 'Before Page content');
$I->seeInField('_wp_convertkit_settings[post_form]', $_ENV['CONVERTKIT_API_FORM_NAME']);
$I->seeInField('_wp_convertkit_settings[post_form_position]', 'After Post content');
$I->seeInField('_wp_convertkit_settings[non_inline_form]', $_ENV['CONVERTKIT_API_FORM_FORMAT_STICKY_BAR_NAME']);
$I->seeInFormFields(
'form',
[
'_wp_convertkit_settings[non_inline_form][]' => [
$_ENV['CONVERTKIT_API_FORM_FORMAT_STICKY_BAR_NAME'],
$_ENV['CONVERTKIT_API_FORM_FORMAT_MODAL_NAME'],
],
]
);
}

/**
Expand Down

0 comments on commit 3aeec1c

Please sign in to comment.