Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
n7studios committed Jan 24, 2025
1 parent 245ba94 commit f7e4cf5
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 43 deletions.
60 changes: 33 additions & 27 deletions tests/_support/Helper/Acceptance/ConvertKitRestrictContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public function testRestrictedContentModalByProductOnFrontend($I, $urlOrPageID,
}

/**
* Run frontend tests for restricted content by ConvertKit Product, to confirm that visible and member's content
* Run frontend tests for restricted content by Kit Tag, to confirm that visible and member's content
* is / is not displayed when logging in with valid and invalid subscriber email addresses.
*
* @since 2.1.0
Expand Down Expand Up @@ -416,13 +416,14 @@ public function testRestrictedContentByTagOnFrontend($I, $urlOrPageID, $emailAdd
}

/**
* Run frontend tests for restricted content by ConvertKit Product, to confirm that visible and member's content
* Run frontend tests for restricted content by Kit Tag, to confirm that visible and member's content
* is / is not displayed when using signed subscriber IDs that do / do not have access to the content.
*
* @since 2.7.1
*
* @param AcceptanceTester $I Tester.
* @param string|int $urlOrPageID URL or ID of Restricted Content Page.
* @param string $emailAddress Email Address.
* @param bool|array $options {
* Optional. An array of settings.
*
Expand All @@ -432,7 +433,7 @@ public function testRestrictedContentByTagOnFrontend($I, $urlOrPageID, $emailAdd
* }
* @param bool $recaptchaEnabled Whether the reCAPTCHA settings are enabled in the Plugin settings.
*/
public function testRestrictedContentByTagOnFrontendUsingSignedSubscriberID($I, $urlOrPageID, $options = false, $recaptchaEnabled = false)
public function testRestrictedContentByTagOnFrontendUsingSignedSubscriberID($I, $urlOrPageID, $emailAddress, $options = false, $recaptchaEnabled = false)
{
// Merge options with defaults.
$options = $this->_getRestrictedContentOptionsWithDefaultsMerged($options);
Expand All @@ -444,10 +445,6 @@ public function testRestrictedContentByTagOnFrontendUsingSignedSubscriberID($I,
$I->amOnUrl($urlOrPageID);
}

// Clear any existing cookie from a previous test and reload.
$I->resetCookie('ck_subscriber_id');
$I->reloadPage();

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

Expand All @@ -464,32 +461,41 @@ public function testRestrictedContentByTagOnFrontendUsingSignedSubscriberID($I,
$I->see($options['text_items']['subscribe_text_tag']);
$I->seeInSource('<input type="submit" class="wp-block-button__link wp-block-button__link' . ( $recaptchaEnabled ? ' g-recaptcha' : '' ) . '" value="' . $options['text_items']['subscribe_button_label'] . '"');

// Set cookie with signed subscriber ID that does not have access to the tag, and reload the restricted content page.
$I->setCookie('ck_subscriber_id', $_ENV['CONVERTKIT_API_SIGNED_SUBSCRIBER_ID_NO_ACCESS']);
if ( is_numeric( $urlOrPageID ) ) {
$I->amOnPage('?p=' . $urlOrPageID . '&ck-cache-bust=' . microtime() );
} else {
$I->amOnUrl($urlOrPageID . '?ck-cache-bust=' . microtime() );
}
// Signup.
$I->waitForElementVisible('input#convertkit_email');
$I->fillField('convertkit_email', $emailAddress);
$I->click('input.wp-block-button__link');

// Confirm an inline error message is displayed.
$I->seeInSource('<div class="convertkit-restrict-content-notice convertkit-restrict-content-notice-error">' . $options['text_items']['no_access_text'] . '</div>');
$I->seeInSource('<div id="convertkit-restrict-content-email-field" class="convertkit-restrict-content-error">');
// Wait for reCAPTCHA to fully load.
if ( $recaptchaEnabled ) {
$I->wait(3);
}

// Confirm that confirmation an email has been sent is displayed.
// Confirm that the visible text displays, hidden text does not display and the CTA displays.
$I->see($options['visible_content']);
if ( ! empty($options['visible_content'])) {
$I->see($options['visible_content']);
}
$I->dontSee($options['member_content']);

// Set cookie with signed subscriber ID that does have access to the tag, and reload the restricted content page.
$I->setCookie('ck_subscriber_id', $_ENV['CONVERTKIT_API_SIGNED_SUBSCRIBER_ID']);
if ( is_numeric( $urlOrPageID ) ) {
$I->amOnPage('?p=' . $urlOrPageID . '&ck-cache-bust=' . microtime() );
} else {
$I->amOnUrl($urlOrPageID . '?ck-cache-bust=' . microtime() );
}
// Confirm that the CTA displays with the expected text.
$I->seeElementInDOM('#convertkit-restrict-content');
$I->seeInSource('<h4>' . $options['text_items']['email_check_heading'] . '</h4>');
$I->see($options['text_items']['email_check_text']);
$I->seeElementInDOM('input#convertkit_subscriber_code');
$I->seeElementInDOM('input.wp-block-button__link');

// Confirm that the restricted content is now displayed.
$I->testRestrictContentDisplaysContent($I, $options);
// Enter an invalid code.
$I->fillField('subscriber_code', '999999');
$I->click('Verify');

// Confirm an inline error message is displayed.
$I->seeInSource('<div class="convertkit-restrict-content-notice convertkit-restrict-content-notice-error">The entered code is invalid. Please try again, or click the link sent in the email.</div>');
$I->seeInSource('<div id="convertkit-subscriber-code-container" class="convertkit-restrict-content-error">');

// Test that the restricted content displays when a valid signed subscriber ID is used,
// as if we entered the code sent in the email.
$this->testRestrictedContentShowsContentWithValidSubscriberID($I, $urlOrPageID, $options);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,17 @@ public function testSaveBlankSettings(AcceptanceTester $I)
// Permit Crawlers.
'permit_crawlers' => '',

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

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

// Restrict by Tag.
'subscribe_heading_tag' => '',
'subscribe_text_tag' => '',
'require_tag_login' => '',
'recaptcha_site_key' => '',
'recaptcha_secret_key' => '',
'recaptcha_minimum_score' => '',

// All.
'subscribe_button_label' => '',
Expand Down Expand Up @@ -142,18 +141,17 @@ public function testSaveSettings(AcceptanceTester $I)
// Permit Crawlers.
'permit_crawlers' => true,

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

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

// Restrict by Tag.
'subscribe_heading_tag' => 'Subscribe Heading Tag',
'subscribe_text_tag' => 'Subscribe Text Tag',
'require_tag_login' => 'on',
'recaptcha_site_key' => 'reCAPTCHASiteKey',
'recaptcha_secret_key' => 'reCAPTCHASecretKey',
'recaptcha_minimum_score' => '0.8',

// All.
'subscribe_button_label' => 'Subscribe Button Label',
Expand Down Expand Up @@ -236,6 +234,7 @@ public function _setupConvertKitPluginRestrictContent($I, $settings = false)
foreach ( $settings as $key => $value ) {
switch ( $key ) {
case 'permit_crawlers':
case 'require_tag_login':
if ( $value ) {
$I->checkOption('_wp_convertkit_settings_restrict_content[' . $key . ']');
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,78 @@ public function testRestrictContentByTag(AcceptanceTester $I)
}

/**
* Test that restricting content by a Tag specified in the Page Settings works when
* creating and viewing a new WordPress Page and the subscriber uses a signed subscriber ID.
* Test that restricting content by a Tag specified in the Page Settings works when:
* - the Plugin is set to Require Login,
* - creating a viewing a new WordPress Page,
* - entering an email address displays the code verification screen
* - using a signed subscriber ID that has access to the Tag displays the content.
*
* @since 2.7.1
* @since 2.7.2
*
* @param AcceptanceTester $I Tester.
*/
public function testRestrictContentByTagUsingSignedSubscriberID(AcceptanceTester $I)
public function testRestrictContentByTagWithRequireLoginEnabled(AcceptanceTester $I)
{
// Setup Restrict Content functionality with Require Login enabled.
$I->setupConvertKitPluginRestrictContent(
$I,
[
'require_tag_login' => 'on',
]
);

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

// 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->testRestrictedContentByTagOnFrontendUsingSignedSubscriberID($I, $url, $I->generateEmailAddress());
}

/**
* Test that restricting content by a Tag specified in the Page Settings works when:
* - the Plugin is set to Require Login,
* - the Plugin has its Recaptcha settings defined,
* - creating a viewing a new WordPress Page,
* - entering an email address displays the code verification screen
* - using a signed subscriber ID that has access to the Tag displays the content.
*
* @since 2.7.2
*
* @param AcceptanceTester $I Tester.
*/
public function testRestrictContentByTagWithRecaptchaAndRequireLoginEnabled(AcceptanceTester $I)
{
// Setup Restrict Content functionality with Require Login and reCAPTCHA enabled.
$I->setupConvertKitPluginRestrictContent(
$I,
[
'require_tag_login' => 'on',
'recaptcha_site_key' => $_ENV['CONVERTKIT_API_RECAPTCHA_SITE_KEY'],
'recaptcha_secret_key' => $_ENV['CONVERTKIT_API_RECAPTCHA_SECRET_KEY'],
'recaptcha_minimum_score' => '0.01', // Set a low score to ensure reCAPTCHA passes the subscriber.
]
);

// Add a Page using the Gutenberg editor.
$I->addGutenbergPage($I, 'page', 'Kit: Page: Restrict Content: Tag by Signed Subscriber ID');
$I->addGutenbergPage($I, 'page', 'Kit: Page: Restrict Content: Tag: Recaptcha and Require Login');

// Configure metabox's Restrict Content setting = Tag name.
$I->configureMetaboxSettings(
Expand All @@ -87,7 +148,7 @@ public function testRestrictContentByTagUsingSignedSubscriberID(AcceptanceTester
$url = $I->publishGutenbergPage($I);

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

/**
Expand Down

0 comments on commit f7e4cf5

Please sign in to comment.