Skip used segments when bulk trashing segments
[MAILPOET-3463]
This commit is contained in:
committed by
Veljko V
parent
b17236b88c
commit
a264951904
@@ -8,6 +8,7 @@ use MailPoet\Entities\DynamicSegmentFilterData;
|
|||||||
use MailPoet\Entities\DynamicSegmentFilterEntity;
|
use MailPoet\Entities\DynamicSegmentFilterEntity;
|
||||||
use MailPoet\Entities\SegmentEntity;
|
use MailPoet\Entities\SegmentEntity;
|
||||||
use MailPoet\Entities\SubscriberSegmentEntity;
|
use MailPoet\Entities\SubscriberSegmentEntity;
|
||||||
|
use MailPoet\Newsletter\Segment\NewsletterSegmentRepository;
|
||||||
use MailPoet\NotFoundException;
|
use MailPoet\NotFoundException;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
use MailPoetVendor\Carbon\Carbon;
|
use MailPoetVendor\Carbon\Carbon;
|
||||||
@@ -18,6 +19,18 @@ use MailPoetVendor\Doctrine\ORM\EntityManager;
|
|||||||
* @extends Repository<SegmentEntity>
|
* @extends Repository<SegmentEntity>
|
||||||
*/
|
*/
|
||||||
class SegmentsRepository extends Repository {
|
class SegmentsRepository extends Repository {
|
||||||
|
|
||||||
|
/** @var NewsletterSegmentRepository */
|
||||||
|
private $newsletterSegmentRepository;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
EntityManager $entityManager,
|
||||||
|
NewsletterSegmentRepository $newsletterSegmentRepository
|
||||||
|
) {
|
||||||
|
parent::__construct($entityManager);
|
||||||
|
$this->newsletterSegmentRepository = $newsletterSegmentRepository;
|
||||||
|
}
|
||||||
|
|
||||||
protected function getEntityClassName() {
|
protected function getEntityClassName() {
|
||||||
return SegmentEntity::class;
|
return SegmentEntity::class;
|
||||||
}
|
}
|
||||||
@@ -146,6 +159,8 @@ class SegmentsRepository extends Repository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function bulkTrash(array $ids, string $type = SegmentEntity::TYPE_DEFAULT): int {
|
public function bulkTrash(array $ids, string $type = SegmentEntity::TYPE_DEFAULT): int {
|
||||||
|
$activelyUsed = $this->newsletterSegmentRepository->getSubjectsOfActivelyUsedEmailsForSegments($ids);
|
||||||
|
$ids = array_diff($ids, array_keys($activelyUsed));
|
||||||
return $this->updateDeletedAt($ids, new Carbon(), $type);
|
return $this->updateDeletedAt($ids, new Carbon(), $type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -4,6 +4,8 @@ namespace MailPoet\Segments;
|
|||||||
|
|
||||||
use MailPoet\Entities\DynamicSegmentFilterData;
|
use MailPoet\Entities\DynamicSegmentFilterData;
|
||||||
use MailPoet\Entities\DynamicSegmentFilterEntity;
|
use MailPoet\Entities\DynamicSegmentFilterEntity;
|
||||||
|
use MailPoet\Entities\NewsletterEntity;
|
||||||
|
use MailPoet\Entities\NewsletterSegmentEntity;
|
||||||
use MailPoet\Entities\SegmentEntity;
|
use MailPoet\Entities\SegmentEntity;
|
||||||
|
|
||||||
class SegmentsRepositoryTest extends \MailPoetTest {
|
class SegmentsRepositoryTest extends \MailPoetTest {
|
||||||
@@ -40,6 +42,32 @@ class SegmentsRepositoryTest extends \MailPoetTest {
|
|||||||
expect($segment2->getDeletedAt())->isInstanceOf(\DateTimeInterface::class);
|
expect($segment2->getDeletedAt())->isInstanceOf(\DateTimeInterface::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testItSkipTrashingForActivelyUsedDefaultSegments() {
|
||||||
|
$segment1 = $this->createDefaultSegment('Segment 1');
|
||||||
|
$segment2 = $this->createDefaultSegment('Segment 2');
|
||||||
|
$this->addActiveNewsletterToSegment($segment1);
|
||||||
|
$this->entityManager->flush();
|
||||||
|
$result = $this->segmentsRepository->bulkTrash([$segment1->getId(), $segment2->getId()]);
|
||||||
|
$this->entityManager->refresh($segment1);
|
||||||
|
$this->entityManager->refresh($segment2);
|
||||||
|
expect($result)->equals(1);
|
||||||
|
expect($segment1->getDeletedAt())->null();
|
||||||
|
expect($segment2->getDeletedAt())->isInstanceOf(\DateTimeInterface::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItSkipTrashingForActivelyUsedDynamicSegments() {
|
||||||
|
$segment1 = $this->createDynamicSegmentEntityForEditorUsers();
|
||||||
|
$segment2 = $this->createDynamicSegmentEntityForEditorUsers();
|
||||||
|
$this->addActiveNewsletterToSegment($segment2);
|
||||||
|
$this->entityManager->flush();
|
||||||
|
$result = $this->segmentsRepository->bulkTrash([$segment1->getId(), $segment2->getId()], SegmentEntity::TYPE_DYNAMIC);
|
||||||
|
$this->entityManager->refresh($segment1);
|
||||||
|
$this->entityManager->refresh($segment2);
|
||||||
|
expect($result)->equals(1);
|
||||||
|
expect($segment1->getDeletedAt())->isInstanceOf(\DateTimeInterface::class);
|
||||||
|
expect($segment2->getDeletedAt())->null();
|
||||||
|
}
|
||||||
|
|
||||||
private function createDefaultSegment(string $name): SegmentEntity {
|
private function createDefaultSegment(string $name): SegmentEntity {
|
||||||
$segment = new SegmentEntity($name, SegmentEntity::TYPE_DEFAULT, 'description');
|
$segment = new SegmentEntity($name, SegmentEntity::TYPE_DEFAULT, 'description');
|
||||||
$this->entityManager->persist($segment);
|
$this->entityManager->persist($segment);
|
||||||
@@ -58,9 +86,21 @@ class SegmentsRepositoryTest extends \MailPoetTest {
|
|||||||
return $segment;
|
return $segment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function addActiveNewsletterToSegment(SegmentEntity $segmentEntity) {
|
||||||
|
$newsletter = new NewsletterEntity();
|
||||||
|
$newsletter->setSubject('Subject');
|
||||||
|
$newsletter->setType(NewsletterEntity::TYPE_WELCOME);
|
||||||
|
$newsletter->setStatus(NewsletterEntity::STATUS_ACTIVE);
|
||||||
|
$newsletterSegment = new NewsletterSegmentEntity($newsletter, $segmentEntity);
|
||||||
|
$this->entityManager->persist($newsletter);
|
||||||
|
$this->entityManager->persist($newsletterSegment);
|
||||||
|
}
|
||||||
|
|
||||||
private function cleanup() {
|
private function cleanup() {
|
||||||
$this->truncateEntity(SegmentEntity::class);
|
$this->truncateEntity(SegmentEntity::class);
|
||||||
$this->truncateEntity(DynamicSegmentFilterEntity::class);
|
$this->truncateEntity(DynamicSegmentFilterEntity::class);
|
||||||
|
$this->truncateEntity(NewsletterEntity::class);
|
||||||
|
$this->truncateEntity(NewsletterSegmentEntity::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function _after() {
|
public function _after() {
|
||||||
|
Reference in New Issue
Block a user