Remove subscriber check from newsletter validator

Checking the subscriber limit has nothing to do with the newsletter
itself, so it makes more sense to continue to perform that check as
needed elsewhere in the codebase.

MAILPOET-4236
This commit is contained in:
John Oleksowicz
2022-05-05 14:06:54 -05:00
committed by Veljko V
parent 73c023449b
commit ab609c8867
4 changed files with 14 additions and 29 deletions

View File

@@ -25,6 +25,7 @@ use MailPoet\Newsletter\Scheduler\Scheduler;
use MailPoet\Newsletter\Url as NewsletterUrl; use MailPoet\Newsletter\Url as NewsletterUrl;
use MailPoet\Settings\SettingsController; use MailPoet\Settings\SettingsController;
use MailPoet\UnexpectedValueException; use MailPoet\UnexpectedValueException;
use MailPoet\Util\License\Features\Subscribers as SubscribersFeature;
use MailPoet\Util\Security; use MailPoet\Util\Security;
use MailPoet\WP\Emoji; use MailPoet\WP\Emoji;
use MailPoet\WP\Functions as WPFunctions; use MailPoet\WP\Functions as WPFunctions;
@@ -63,6 +64,9 @@ class Newsletters extends APIEndpoint {
/** @var Emoji */ /** @var Emoji */
private $emoji; private $emoji;
/** @var SubscribersFeature */
private $subscribersFeature;
/** @var SendPreviewController */ /** @var SendPreviewController */
private $sendPreviewController; private $sendPreviewController;
@@ -87,6 +91,7 @@ class Newsletters extends APIEndpoint {
NewsletterListingRepository $newsletterListingRepository, NewsletterListingRepository $newsletterListingRepository,
NewslettersResponseBuilder $newslettersResponseBuilder, NewslettersResponseBuilder $newslettersResponseBuilder,
PostNotificationScheduler $postNotificationScheduler, PostNotificationScheduler $postNotificationScheduler,
SubscribersFeature $subscribersFeature,
Emoji $emoji, Emoji $emoji,
SendPreviewController $sendPreviewController, SendPreviewController $sendPreviewController,
NewsletterSaveController $newsletterSaveController, NewsletterSaveController $newsletterSaveController,
@@ -102,6 +107,7 @@ class Newsletters extends APIEndpoint {
$this->newsletterListingRepository = $newsletterListingRepository; $this->newsletterListingRepository = $newsletterListingRepository;
$this->newslettersResponseBuilder = $newslettersResponseBuilder; $this->newslettersResponseBuilder = $newslettersResponseBuilder;
$this->postNotificationScheduler = $postNotificationScheduler; $this->postNotificationScheduler = $postNotificationScheduler;
$this->subscribersFeature = $subscribersFeature;
$this->emoji = $emoji; $this->emoji = $emoji;
$this->sendPreviewController = $sendPreviewController; $this->sendPreviewController = $sendPreviewController;
$this->newsletterSaveController = $newsletterSaveController; $this->newsletterSaveController = $newsletterSaveController;
@@ -165,6 +171,12 @@ class Newsletters extends APIEndpoint {
]); ]);
} }
if ($status === NewsletterEntity::STATUS_ACTIVE && $this->subscribersFeature->check()) {
return $this->errorResponse([
APIError::FORBIDDEN => __('Subscribers limit reached.', 'mailpoet'),
], [], Response::STATUS_FORBIDDEN);
}
$newsletter = $this->getNewsletter($data); $newsletter = $this->getNewsletter($data);
if ($newsletter === null) { if ($newsletter === null) {
return $this->errorResponse([ return $this->errorResponse([

View File

@@ -5,7 +5,6 @@ namespace MailPoet\Newsletter;
use MailPoet\Entities\NewsletterEntity; use MailPoet\Entities\NewsletterEntity;
use MailPoet\Services\Bridge; use MailPoet\Services\Bridge;
use MailPoet\Settings\TrackingConfig; use MailPoet\Settings\TrackingConfig;
use MailPoet\Util\License\Features\Subscribers as SubscribersFeature;
use MailPoet\Validator\ValidationException; use MailPoet\Validator\ValidationException;
class NewsletterValidator { class NewsletterValidator {
@@ -16,22 +15,16 @@ class NewsletterValidator {
/** @var TrackingConfig */ /** @var TrackingConfig */
private $trackingConfig; private $trackingConfig;
/** @var SubscribersFeature */
private $subscribersFeature;
public function __construct( public function __construct(
Bridge $bridge, Bridge $bridge,
TrackingConfig $trackingConfig, TrackingConfig $trackingConfig
SubscribersFeature $subscribersFeature
) { ) {
$this->bridge = $bridge; $this->bridge = $bridge;
$this->trackingConfig = $trackingConfig; $this->trackingConfig = $trackingConfig;
$this->subscribersFeature = $subscribersFeature;
} }
public function validate(NewsletterEntity $newsletterEntity): ?string { public function validate(NewsletterEntity $newsletterEntity): ?string {
try { try {
$this->validateSubscriberLimit();
$this->validateBody($newsletterEntity); $this->validateBody($newsletterEntity);
$this->validateUnsubscribeRequirements($newsletterEntity); $this->validateUnsubscribeRequirements($newsletterEntity);
$this->validateReEngagementRequirements($newsletterEntity); $this->validateReEngagementRequirements($newsletterEntity);
@@ -95,10 +88,4 @@ class NewsletterValidator {
throw new ValidationException(__('Please add an “Automatic Latest Content” widget to the email from the right sidebar.', 'mailpoet')); throw new ValidationException(__('Please add an “Automatic Latest Content” widget to the email from the right sidebar.', 'mailpoet'));
} }
} }
private function validateSubscriberLimit(): void {
if ($this->subscribersFeature->check()) {
throw new ValidationException(__('Subscribers limit reached.', 'mailpoet'));
}
}
} }

View File

@@ -22,7 +22,6 @@ use MailPoet\Entities\SubscriberSegmentEntity;
use MailPoet\Models\ScheduledTask; use MailPoet\Models\ScheduledTask;
use MailPoet\Models\SendingQueue; use MailPoet\Models\SendingQueue;
use MailPoet\Newsletter\NewslettersRepository; use MailPoet\Newsletter\NewslettersRepository;
use MailPoet\Newsletter\NewsletterValidator;
use MailPoet\Newsletter\Options\NewsletterOptionFieldsRepository; use MailPoet\Newsletter\Options\NewsletterOptionFieldsRepository;
use MailPoet\Newsletter\Options\NewsletterOptionsRepository; use MailPoet\Newsletter\Options\NewsletterOptionsRepository;
use MailPoet\Newsletter\Preview\SendPreviewController; use MailPoet\Newsletter\Preview\SendPreviewController;
@@ -228,9 +227,7 @@ class NewslettersTest extends \MailPoetTest {
public function testItReturnsErrorIfSubscribersLimitReached() { public function testItReturnsErrorIfSubscribersLimitReached() {
$endpoint = $this->createNewslettersEndpointWithMocks([ $endpoint = $this->createNewslettersEndpointWithMocks([
'cronHelper' => $this->cronHelper, 'cronHelper' => $this->cronHelper,
'newsletterValidator' => $this->getServiceWithOverrides(NewsletterValidator::class, [ 'subscribersFeature' => Stub::make(Subscribers::class, ['check' => true])
'subscribersFeature' => Stub::make(Subscribers::class, ['check' => true])
])
]); ]);
$res = $endpoint->setStatus([ $res = $endpoint->setStatus([
'id' => $this->newsletter->getId(), 'id' => $this->newsletter->getId(),

View File

@@ -7,7 +7,6 @@ use MailPoet\Newsletter\NewsletterValidator;
use MailPoet\Services\Bridge; use MailPoet\Services\Bridge;
use MailPoet\Settings\TrackingConfig; use MailPoet\Settings\TrackingConfig;
use MailPoet\Test\DataFactories\Newsletter; use MailPoet\Test\DataFactories\Newsletter;
use MailPoet\Util\License\Features\Subscribers;
class NewsletterValidatorTest extends \MailPoetTest { class NewsletterValidatorTest extends \MailPoetTest {
/** @var NewsletterValidator */ /** @var NewsletterValidator */
@@ -106,14 +105,4 @@ class NewsletterValidatorTest extends \MailPoetTest {
$validationError = $this->newsletterValidator->validate($newsletter); $validationError = $this->newsletterValidator->validate($newsletter);
expect($validationError)->null(); expect($validationError)->null();
} }
public function testItIsNotValidIfSubscriberLimitReached() {
$newsletter = (new Newsletter())->withDefaultBody()->create();
$validator = $this->getServiceWithOverrides(NewsletterValidator::class, [
'subscribersFeature' => Stub::make(Subscribers::class, ['check' => true])
]);
$validationError = $validator->validate($newsletter);
expect($validationError)->equals('Subscribers limit reached.');
}
} }