Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
n7studios committed Dec 6, 2024
1 parent e33d721 commit f4ce290
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 52 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ CONVERTKIT_API_KEY=
CONVERTKIT_API_SECRET=
CONVERTKIT_API_SIGNED_SUBSCRIBER_ID=
CONVERTKIT_API_SIGNED_SUBSCRIBER_ID_NO_ACCESS=
CONVERTKIT_API_RECAPTCHA_SITE_KEY=
CONVERTKIT_API_RECAPTCHA_SECRET_KEY=
CONVERTKIT_API_FORM_NAME="Page Form [inline]"
CONVERTKIT_API_FORM_ID="2765139"
CONVERTKIT_API_FORM_FORMAT_MODAL_NAME="Modal Form [modal]"
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ jobs:
KIT_OAUTH_REDIRECT_URI: ${{ secrets.KIT_OAUTH_REDIRECT_URI }}
CONVERTKIT_API_SIGNED_SUBSCRIBER_ID: ${{ secrets.CONVERTKIT_API_SIGNED_SUBSCRIBER_ID }} # ConvertKit API Signed Subscriber ID, stored in the repository's Settings > Secrets
CONVERTKIT_API_SIGNED_SUBSCRIBER_ID_NO_ACCESS: ${{ secrets.CONVERTKIT_API_SIGNED_SUBSCRIBER_ID_NO_ACCESS }} # ConvertKit API Signed Subscriber ID with no access to Products, stored in the repository's Settings > Secrets
CONVERTKIT_API_RECAPTCHA_SITE_KEY: ${{ secrets.CONVERTKIT_API_RECAPTCHA_SITE_KEY }} # Google reCAPTCHA v3 Site Key, stored in the repository's Settings > Secrets
CONVERTKIT_API_RECAPTCHA_SECRET_KEY: ${{ secrets.CONVERTKIT_API_RECAPTCHA_SECRET_KEY }} # Google reCAPTCHA v3 Secret Key, stored in the repository's Settings > Secrets

# Defines the WordPress and PHP Versions matrix to run tests on
# WooCommerce 5.9.0 requires WordPress 5.6 or greater, so we do not test on earlier versions
Expand Down Expand Up @@ -218,6 +220,9 @@ jobs:
KIT_OAUTH_REDIRECT_URI=${{ env.KIT_OAUTH_REDIRECT_URI }}
CONVERTKIT_API_SIGNED_SUBSCRIBER_ID=${{ env.CONVERTKIT_API_SIGNED_SUBSCRIBER_ID }}
CONVERTKIT_API_SIGNED_SUBSCRIBER_ID_NO_ACCESS=${{ env.CONVERTKIT_API_SIGNED_SUBSCRIBER_ID_NO_ACCESS }}
CONVERTKIT_API_RECAPTCHA_SITE_KEY=${{ env.CONVERTKIT_API_RECAPTCHA_SITE_KEY }}
CONVERTKIT_API_RECAPTCHA_SECRET_KEY=${{ env.CONVERTKIT_API_RECAPTCHA_SECRET_KEY }}
write-mode: append

# Installs wp-browser, Codeception, PHP CodeSniffer and anything else needed to run tests.
Expand Down
42 changes: 27 additions & 15 deletions tests/_support/Helper/Acceptance/ConvertKitRestrictContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,27 @@ public function getRestrictedContentDefaultSettings()
{
return array(
// Permit Crawlers.
'permit_crawlers' => '',
'permit_crawlers' => '',
'recaptcha_site_key' => '',
'recaptcha_secret_key' => '',
'recaptcha_minimum_score' => '0.5',

// Restrict by Product.
'subscribe_heading' => 'Read this post with a premium subscription',
'subscribe_text' => 'This post is only available to premium subscribers. Join today to get access to all posts.',
'subscribe_heading' => 'Read this post with a premium subscription',
'subscribe_text' => 'This post is only available to premium subscribers. Join today to get access to all posts.',

// Restrict by Tag.
'subscribe_heading_tag' => 'Subscribe to keep reading',
'subscribe_text_tag' => 'This post is free to read but only available to subscribers. Join today to get access to all posts.',
'subscribe_heading_tag' => 'Subscribe to keep reading',
'subscribe_text_tag' => 'This post is free to read but only available to subscribers. Join today to get access to all posts.',

// All.
'subscribe_button_label' => 'Subscribe',
'email_text' => 'Already subscribed?',
'email_button_label' => 'Log in',
'email_description_text' => 'We\'ll email you a magic code to log you in without a password.',
'email_check_heading' => 'We just emailed you a log in code',
'email_check_text' => 'Enter the code below to finish logging in',
'no_access_text' => 'Your account does not have access to this content. Please use the button above to purchase, or enter the email address you used to purchase the product.',
'subscribe_button_label' => 'Subscribe',
'email_text' => 'Already subscribed?',
'email_button_label' => 'Log in',
'email_description_text' => 'We\'ll email you a magic code to log you in without a password.',
'email_check_heading' => 'We just emailed you a log in code',
'email_check_text' => 'Enter the code below to finish logging in',
'no_access_text' => 'Your account does not have access to this content. Please use the button above to purchase, or enter the email address you used to purchase the product.',
);
}

Expand All @@ -95,6 +98,14 @@ public function checkRestrictContentSettings($I, $settings)
}
break;

case 'recaptcha_minimum_score':
if ( $value ) {
$I->seeInField('_wp_convertkit_settings_restrict_content[' . $key . ']', $value);
} else {
$I->seeInField('_wp_convertkit_settings_restrict_content[' . $key . ']', '0.5');
}
break;

default:
$I->seeInField('_wp_convertkit_settings_restrict_content[' . $key . ']', $value);
break;
Expand Down Expand Up @@ -341,8 +352,9 @@ public function testRestrictedContentModalByProductOnFrontend($I, $urlOrPageID,
* @type string $member_content Content that should only be available to authenticated subscribers.
* @type array $text_items Expected text for subscribe text, subscribe button label, email text etc. If not defined, uses expected defaults.
* }
* @param bool $recaptchaEnabled Whether the reCAPTCHA settings are enabled in the Plugin settings.
*/
public function testRestrictedContentByTagOnFrontend($I, $urlOrPageID, $emailAddress, $options = false)
public function testRestrictedContentByTagOnFrontend($I, $urlOrPageID, $emailAddress, $options = false, $recaptchaEnabled = false)
{
// Merge options with defaults.
$options = $this->_getRestrictedContentOptionsWithDefaultsMerged($options);
Expand Down Expand Up @@ -372,7 +384,7 @@ public function testRestrictedContentByTagOnFrontend($I, $urlOrPageID, $emailAdd
$I->seeElementInDOM('#convertkit-restrict-content');
$I->seeInSource('<h3>' . $options['text_items']['subscribe_heading_tag'] . '</h3>');
$I->see($options['text_items']['subscribe_text_tag']);
$I->seeInSource('<input type="submit" class="wp-block-button__link wp-block-button__link" value="' . $options['text_items']['subscribe_button_label'] . '">');
$I->seeInSource('<input type="submit" class="wp-block-button__link wp-block-button__link' . ( $recaptchaEnabled ? ' g-recaptcha' : '' ) . '" value="' . $options['text_items']['subscribe_button_label'] . '"');

// Enter the email address and submit the form.
$I->fillField('convertkit_email', $emailAddress);
Expand Down Expand Up @@ -462,7 +474,7 @@ public function testRestrictContentByProductHidesContentWithCTA($I, $options = f
$I->seeInSource('<a href="' . $_ENV['CONVERTKIT_API_PRODUCT_URL'] . '" class="wp-block-button__link');

$I->see($options['text_items']['email_text']);
$I->seeInSource('<input type="submit" class="wp-block-button__link wp-block-button__link" value="' . $options['text_items']['email_button_label'] . '">');
$I->seeInSource('<input type="submit" class="wp-block-button__link wp-block-button__link" value="' . $options['text_items']['email_button_label'] . '"');
$I->seeInSource('<small>' . $options['text_items']['email_description_text'] . '</small>');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,30 @@ public function testSaveBlankSettings(AcceptanceTester $I)
// Define settings.
$settings = array(
// Permit Crawlers.
'permit_crawlers' => '',
'permit_crawlers' => '',

// Google reCAPTCHA.
'recaptcha_site_key' => '',
'recaptcha_secret_key' => '',
'recaptcha_site_key' => '',
'recaptcha_secret_key' => '',
'recaptcha_minimum_score' => '',

// Restrict by Product.
'subscribe_heading' => '',
'subscribe_text' => '',
'subscribe_heading' => '',
'subscribe_text' => '',

// Restrict by Tag.
'subscribe_heading_tag' => '',
'subscribe_text_tag' => '',
'subscribe_heading_tag' => '',
'subscribe_text_tag' => '',

// All.
'subscribe_button_label' => '',
'email_text' => '',
'email_button_label' => '',
'email_heading' => '',
'email_description_text' => '',
'email_check_heading' => '',
'email_check_text' => '',
'no_access_text' => '',
'subscribe_button_label' => '',
'email_text' => '',
'email_button_label' => '',
'email_heading' => '',
'email_description_text' => '',
'email_check_heading' => '',
'email_check_text' => '',
'no_access_text' => '',
);

// Save settings.
Expand Down Expand Up @@ -139,29 +140,30 @@ public function testSaveSettings(AcceptanceTester $I)
// Define settings.
$settings = array(
// Permit Crawlers.
'permit_crawlers' => true,
'permit_crawlers' => true,

// Google reCAPTCHA.
'recaptcha_site_key' => 'reCAPTCHASiteKey',
'recaptcha_secret_key' => 'reCAPTCHASecretKey',
'recaptcha_site_key' => 'reCAPTCHASiteKey',
'recaptcha_secret_key' => 'reCAPTCHASecretKey',
'recaptcha_minimum_score' => '0.8',

// Restrict by Product.
'subscribe_heading' => 'Subscribe Heading',
'subscribe_text' => 'Subscribe Text',
'subscribe_heading' => 'Subscribe Heading',
'subscribe_text' => 'Subscribe Text',

// Restrict by Tag.
'subscribe_heading_tag' => 'Subscribe Heading Tag',
'subscribe_text_tag' => 'Subscribe Text Tag',
'subscribe_heading_tag' => 'Subscribe Heading Tag',
'subscribe_text_tag' => 'Subscribe Text Tag',

// All.
'subscribe_button_label' => 'Subscribe Button Label',
'email_text' => 'Email Text',
'email_button_label' => 'Email Button Label',
'email_heading' => 'Email Heading',
'email_description_text' => 'Email Description Text',
'email_check_heading' => 'Email Check Heading',
'email_check_text' => 'Email Check Text',
'no_access_text' => 'No Access Text',
'subscribe_button_label' => 'Subscribe Button Label',
'email_text' => 'Email Text',
'email_button_label' => 'Email Button Label',
'email_heading' => 'Email Heading',
'email_description_text' => 'Email Description Text',
'email_check_heading' => 'Email Check Heading',
'email_check_text' => 'Email Check Text',
'no_access_text' => 'No Access Text',
);

// Save settings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,104 @@ public function testRestrictContentByInvalidTag(AcceptanceTester $I)
$I->testRestrictContentDisplaysContent($I);
}

/**
* Test that restricting content by a Tag specified in the Page Settings works when
* creating and viewing a new WordPress Page, with Google's reCAPTCHA enabled.
*
* @since 2.6.8
*
* @param AcceptanceTester $I Tester.
*/
public function testRestrictContentByTagWithRecaptchaEnabled(AcceptanceTester $I)
{
// Setup Restrict Content functionality with reCAPTCHA enabled.
$I->setupConvertKitPluginRestrictContent(
$I,
[
'recaptcha_site_key' => $_ENV['CONVERTKIT_API_RECAPTCHA_SITE_KEY'],
'recaptcha_secret_key' => $_ENV['CONVERTKIT_API_RECAPTCHA_SECRET_KEY'],
'recaptcha_minimum_score' => '0.5',
]
);

// Add a Page using the Gutenberg editor.
$I->addGutenbergPage($I, 'page', 'Kit: Page: Restrict Content: Tag: reCAPTCHA');

// Configure metabox's Restrict Content setting = Tag name.
$I->configureMetaboxSettings(
$I,
'wp-convertkit-meta-box',
[
'form' => [ 'select2', 'None' ],
'restrict_content' => [ 'select2', $_ENV['CONVERTKIT_API_TAG_NAME'] ],
]
);

// Add blocks.
$I->addGutenbergParagraphBlock($I, 'Visible content.');
$I->addGutenbergBlock($I, 'More', 'more');
$I->addGutenbergParagraphBlock($I, 'Member-only content.');

// Publish Page.
$url = $I->publishGutenbergPage($I);

// Test Restrict Content functionality.
$I->testRestrictedContentByTagOnFrontend($I, $url, $I->generateEmailAddress(), false, true);
}

/**
* Test that restricting content by a Tag specified in the Page Settings works when
* creating and viewing a new WordPress Page, with Google's reCAPTCHA enabled.
*
* @since 2.6.8
*
* @param AcceptanceTester $I Tester.
*/
public function testRestrictContentByTagWithRecaptchaEnabledWithHighMinimumScore(AcceptanceTester $I)
{
// Setup Restrict Content functionality with reCAPTCHA enabled.
$I->setupConvertKitPluginRestrictContent(
$I,
[
'recaptcha_site_key' => $_ENV['CONVERTKIT_API_RECAPTCHA_SITE_KEY'],
'recaptcha_secret_key' => $_ENV['CONVERTKIT_API_RECAPTCHA_SECRET_KEY'],
'recaptcha_minimum_score' => '0.99', // Set a high score to ensure reCAPTCHA blocks the subscriber.
]
);

// Add a Page using the Gutenberg editor.
$I->addGutenbergPage($I, 'page', 'Kit: Page: Restrict Content: Tag: reCAPTCHA High Min Score');

// Configure metabox's Restrict Content setting = Tag name.
$I->configureMetaboxSettings(
$I,
'wp-convertkit-meta-box',
[
'form' => [ 'select2', 'None' ],
'restrict_content' => [ 'select2', $_ENV['CONVERTKIT_API_TAG_NAME'] ],
]
);

// Add blocks.
$I->addGutenbergParagraphBlock($I, 'Visible content.');
$I->addGutenbergBlock($I, 'More', 'more');
$I->addGutenbergParagraphBlock($I, 'Member-only content.');

// Publish Page.
$url = $I->publishGutenbergPage($I);

// Load page.
$I->amOnUrl($url);

// Enter the email address and submit the form.
$I->fillField('convertkit_email', $I->generateEmailAddress());
$I->click('input.wp-block-button__link');

// Confirm an error message is displayed.
$I->waitForElementVisible('#convertkit-restrict-content');
$I->seeInSource('<div class="convertkit-restrict-content-notice convertkit-restrict-content-notice-error">Google reCAPTCHA failed</div>');
}

/**
* Test that restricting content by a Tag specified in the Page Settings works when
* using the Quick Edit functionality.
Expand Down
10 changes: 3 additions & 7 deletions views/frontend/restrict-content/tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,14 @@
// Output submit button, depending on whether Google reCAPTCHA is enabled.
if ( $this->restrict_content_settings->has_recaptcha_site_and_secret_keys() ) {
?>
<input type="submit"
class="wp-block-button__link wp-block-button__link g-recaptcha"
<input type="submit" class="wp-block-button__link wp-block-button__link g-recaptcha" value="<?php echo esc_attr( $this->restrict_content_settings->get_by_key( 'subscribe_button_label' ) ); ?>"
data-sitekey="<?php echo esc_attr( $this->restrict_content_settings->get_recaptcha_site_key() ); ?>"
data-callback="convertKitRestrictContentTagFormSubmit"
data-action="convertkit_restrict_content_tag"
value="<?php echo esc_attr( $this->restrict_content_settings->get_by_key( 'subscribe_button_label' ) ); ?>" />
data-action="convertkit_restrict_content_tag" />
<?php
} else {
?>
<input type="submit"
class="wp-block-button__link wp-block-button__link"
value="<?php echo esc_attr( $this->restrict_content_settings->get_by_key( 'subscribe_button_label' ) ); ?>" />
<input type="submit" class="wp-block-button__link wp-block-button__link" value="<?php echo esc_attr( $this->restrict_content_settings->get_by_key( 'subscribe_button_label' ) ); ?>" />
<?php
}
?>
Expand Down

0 comments on commit f4ce290

Please sign in to comment.