From f7e4cf5753f1a54292ba12beb1824597d709772a Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 24 Jan 2025 18:20:55 +0800 Subject: [PATCH] Added tests --- .../Acceptance/ConvertKitRestrictContent.php | 60 ++++++++------- .../general/RestrictContentSettingsCest.php | 19 +++-- .../general/RestrictContentTagCest.php | 73 +++++++++++++++++-- 3 files changed, 109 insertions(+), 43 deletions(-) diff --git a/tests/_support/Helper/Acceptance/ConvertKitRestrictContent.php b/tests/_support/Helper/Acceptance/ConvertKitRestrictContent.php index 22384803..c04b20db 100644 --- a/tests/_support/Helper/Acceptance/ConvertKitRestrictContent.php +++ b/tests/_support/Helper/Acceptance/ConvertKitRestrictContent.php @@ -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 @@ -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. * @@ -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); @@ -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); @@ -464,32 +461,41 @@ public function testRestrictedContentByTagOnFrontendUsingSignedSubscriberID($I, $I->see($options['text_items']['subscribe_text_tag']); $I->seeInSource('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('
' . $options['text_items']['no_access_text'] . '
'); - $I->seeInSource('
'); + // 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('

' . $options['text_items']['email_check_heading'] . '

'); + $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('
The entered code is invalid. Please try again, or click the link sent in the email.
'); + $I->seeInSource('
'); + + // 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); } /** diff --git a/tests/acceptance/restrict-content/general/RestrictContentSettingsCest.php b/tests/acceptance/restrict-content/general/RestrictContentSettingsCest.php index 03bd1238..7f695ed5 100644 --- a/tests/acceptance/restrict-content/general/RestrictContentSettingsCest.php +++ b/tests/acceptance/restrict-content/general/RestrictContentSettingsCest.php @@ -84,11 +84,6 @@ 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' => '', @@ -96,6 +91,10 @@ public function testSaveBlankSettings(AcceptanceTester $I) // 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' => '', @@ -142,11 +141,6 @@ 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', @@ -154,6 +148,10 @@ public function testSaveSettings(AcceptanceTester $I) // 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', @@ -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 { diff --git a/tests/acceptance/restrict-content/general/RestrictContentTagCest.php b/tests/acceptance/restrict-content/general/RestrictContentTagCest.php index 36dcb6d1..e53df887 100644 --- a/tests/acceptance/restrict-content/general/RestrictContentTagCest.php +++ b/tests/acceptance/restrict-content/general/RestrictContentTagCest.php @@ -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( @@ -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); } /**