From f3ad7ac0b430c234ac8b4ae935c8fff41e1abf5b Mon Sep 17 00:00:00 2001 From: Brezo Cordero <8002881+brezocordero@users.noreply.github.com> Date: Thu, 9 Sep 2021 16:23:15 -0500 Subject: [PATCH] Show CAPTCHA from first signup [MAILPOET-3788] --- lib/Subscription/Captcha.php | 10 ++++--- .../integration/Subscription/CaptchaTest.php | 27 +++++++++++++++---- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/lib/Subscription/Captcha.php b/lib/Subscription/Captcha.php index cd7b2c59ab..7de7e2ffc6 100644 --- a/lib/Subscription/Captcha.php +++ b/lib/Subscription/Captcha.php @@ -43,12 +43,16 @@ class Captcha { } public function isRequired($subscriberEmail = null) { - if ($this->isUserExemptFromCaptcha()) { + if ($this->isUserExemptFromCaptcha() ) { return false; } - // Check limits per recipient - $subscriptionCaptchaRecipientLimit = $this->wp->applyFilters('mailpoet_subscription_captcha_recipient_limit', 1); + $subscriptionCaptchaRecipientLimit = $this->wp->applyFilters('mailpoet_subscription_captcha_recipient_limit', 0); + if ($subscriptionCaptchaRecipientLimit === 0) { + return true; + } + + // Check limits per recipient if enabled if ($subscriberEmail) { $subscriber = Subscriber::where('email', $subscriberEmail)->findOne(); if ($subscriber instanceof Subscriber diff --git a/tests/integration/Subscription/CaptchaTest.php b/tests/integration/Subscription/CaptchaTest.php index 382dd084e0..6084550f9d 100644 --- a/tests/integration/Subscription/CaptchaTest.php +++ b/tests/integration/Subscription/CaptchaTest.php @@ -33,22 +33,37 @@ class CaptchaTest extends \MailPoetTest { $this->captchaSession->reset(); } - public function testItDoesNotRequireCaptchaForTheFirstSubscription() { + public function testItRequiresCaptchaForFirstSubscription() { + $email = 'non-existent-subscriber@example.com'; + $result = $this->captcha->isRequired($email); + expect($result)->equals(true); + } + + public function testItRequiresCaptchaForUnrepeatedIPAddress() { + $result = $this->captcha->isRequired(); + expect($result)->equals(true); + } + + public function testItTakesFilterIntoAccountToDisableCaptcha() { + $wp = new WPFunctions; + $filter = function() { + return 1; + }; + $wp->addFilter('mailpoet_subscription_captcha_recipient_limit', $filter); $email = 'non-existent-subscriber@example.com'; $result = $this->captcha->isRequired($email); expect($result)->equals(false); - } - public function testItRequiresCaptchaForRepeatedRecipient() { + $result = $this->captcha->isRequired(); + expect($result)->equals(false); + $subscriber = Subscriber::create(); $subscriber->hydrate(Fixtures::get('subscriber_template')); $subscriber->countConfirmations = 1; $subscriber->save(); $result = $this->captcha->isRequired($subscriber->email); expect($result)->equals(true); - } - public function testItRequiresCaptchaForRepeatedIPAddress() { $ip = new SubscriberIPEntity('127.0.0.1'); $ip->setCreatedAt(Carbon::now()->subMinutes(1)); $this->entityManager->persist($ip); @@ -56,6 +71,8 @@ class CaptchaTest extends \MailPoetTest { $email = 'non-existent-subscriber@example.com'; $result = $this->captcha->isRequired($email); expect($result)->equals(true); + + $wp->removeFilter('mailpoet_subscription_captcha_recipient_limit', $filter); } public function testItRendersImageAndStoresHashToSession() {