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() {