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\Settings\SettingsController;
use MailPoet\UnexpectedValueException;
use MailPoet\Util\License\Features\Subscribers as SubscribersFeature;
use MailPoet\Util\Security;
use MailPoet\WP\Emoji;
use MailPoet\WP\Functions as WPFunctions;
@@ -63,6 +64,9 @@ class Newsletters extends APIEndpoint {
/** @var Emoji */
private $emoji;
/** @var SubscribersFeature */
private $subscribersFeature;
/** @var SendPreviewController */
private $sendPreviewController;
@@ -87,6 +91,7 @@ class Newsletters extends APIEndpoint {
NewsletterListingRepository $newsletterListingRepository,
NewslettersResponseBuilder $newslettersResponseBuilder,
PostNotificationScheduler $postNotificationScheduler,
SubscribersFeature $subscribersFeature,
Emoji $emoji,
SendPreviewController $sendPreviewController,
NewsletterSaveController $newsletterSaveController,
@@ -102,6 +107,7 @@ class Newsletters extends APIEndpoint {
$this->newsletterListingRepository = $newsletterListingRepository;
$this->newslettersResponseBuilder = $newslettersResponseBuilder;
$this->postNotificationScheduler = $postNotificationScheduler;
$this->subscribersFeature = $subscribersFeature;
$this->emoji = $emoji;
$this->sendPreviewController = $sendPreviewController;
$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);
if ($newsletter === null) {
return $this->errorResponse([

View File

@@ -5,7 +5,6 @@ namespace MailPoet\Newsletter;
use MailPoet\Entities\NewsletterEntity;
use MailPoet\Services\Bridge;
use MailPoet\Settings\TrackingConfig;
use MailPoet\Util\License\Features\Subscribers as SubscribersFeature;
use MailPoet\Validator\ValidationException;
class NewsletterValidator {
@@ -16,22 +15,16 @@ class NewsletterValidator {
/** @var TrackingConfig */
private $trackingConfig;
/** @var SubscribersFeature */
private $subscribersFeature;
public function __construct(
Bridge $bridge,
TrackingConfig $trackingConfig,
SubscribersFeature $subscribersFeature
TrackingConfig $trackingConfig
) {
$this->bridge = $bridge;
$this->trackingConfig = $trackingConfig;
$this->subscribersFeature = $subscribersFeature;
}
public function validate(NewsletterEntity $newsletterEntity): ?string {
try {
$this->validateSubscriberLimit();
$this->validateBody($newsletterEntity);
$this->validateUnsubscribeRequirements($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'));
}
}
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\SendingQueue;
use MailPoet\Newsletter\NewslettersRepository;
use MailPoet\Newsletter\NewsletterValidator;
use MailPoet\Newsletter\Options\NewsletterOptionFieldsRepository;
use MailPoet\Newsletter\Options\NewsletterOptionsRepository;
use MailPoet\Newsletter\Preview\SendPreviewController;
@@ -228,9 +227,7 @@ class NewslettersTest extends \MailPoetTest {
public function testItReturnsErrorIfSubscribersLimitReached() {
$endpoint = $this->createNewslettersEndpointWithMocks([
'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([
'id' => $this->newsletter->getId(),

View File

@@ -7,7 +7,6 @@ use MailPoet\Newsletter\NewsletterValidator;
use MailPoet\Services\Bridge;
use MailPoet\Settings\TrackingConfig;
use MailPoet\Test\DataFactories\Newsletter;
use MailPoet\Util\License\Features\Subscribers;
class NewsletterValidatorTest extends \MailPoetTest {
/** @var NewsletterValidator */
@@ -106,14 +105,4 @@ class NewsletterValidatorTest extends \MailPoetTest {
$validationError = $this->newsletterValidator->validate($newsletter);
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.');
}
}