Replace old Subscriber and SubscriberSegment models with Doctrine code in SubscribersTest

[MAILPOET-4378]
This commit is contained in:
Rodrigo Primo
2022-09-19 15:25:14 -03:00
committed by Oluwaseun Olorunsola
parent 0677dbc3b0
commit 4157a864e5

View File

@@ -26,8 +26,6 @@ use MailPoet\Form\Util\FieldNameObfuscator;
use MailPoet\Listing\Handler; use MailPoet\Listing\Handler;
use MailPoet\Models\Segment; use MailPoet\Models\Segment;
use MailPoet\Models\SendingQueue; use MailPoet\Models\SendingQueue;
use MailPoet\Models\Subscriber;
use MailPoet\Models\SubscriberSegment;
use MailPoet\Segments\SegmentsRepository; use MailPoet\Segments\SegmentsRepository;
use MailPoet\Settings\SettingsController; use MailPoet\Settings\SettingsController;
use MailPoet\Settings\SettingsRepository; use MailPoet\Settings\SettingsRepository;
@@ -80,6 +78,9 @@ class SubscribersTest extends \MailPoetTest {
/** @var SubscribersResponseBuilder */ /** @var SubscribersResponseBuilder */
private $responseBuilder; private $responseBuilder;
/** @var SubscribersRepository */
private $subscribersRepository;
public function _before() { public function _before() {
parent::_before(); parent::_before();
$this->cleanup(); $this->cleanup();
@@ -155,6 +156,8 @@ class SubscribersTest extends \MailPoetTest {
'name' => 'Sender', 'name' => 'Sender',
]); ]);
$this->entityManager->flush(); $this->entityManager->flush();
$this->subscribersRepository = $this->diContainer->get(SubscribersRepository::class);
} }
public function testItCanGetASubscriber() { public function testItCanGetASubscriber() {
@@ -259,7 +262,7 @@ class SubscribersTest extends \MailPoetTest {
'email' => 'jane@mailpoet.com', 'email' => 'jane@mailpoet.com',
'first_name' => 'Super Jane', 'first_name' => 'Super Jane',
'last_name' => 'Doe', 'last_name' => 'Doe',
'status' => Subscriber::STATUS_SUBSCRIBED, 'status' => SubscriberEntity::STATUS_SUBSCRIBED,
'segments' => [$this->segment1->getId()], 'segments' => [$this->segment1->getId()],
'source' => Source::API, 'source' => Source::API,
]; ];
@@ -301,7 +304,7 @@ class SubscribersTest extends \MailPoetTest {
'email' => 'jane@mailpoet.com', 'email' => 'jane@mailpoet.com',
'first_name' => 'Super Jane', 'first_name' => 'Super Jane',
'last_name' => 'Doe', 'last_name' => 'Doe',
'status' => Subscriber::STATUS_SUBSCRIBED, 'status' => SubscriberEntity::STATUS_SUBSCRIBED,
'source' => Source::API, 'source' => Source::API,
]; ];
@@ -444,7 +447,7 @@ class SubscribersTest extends \MailPoetTest {
public function testItCanGroupListing() { public function testItCanGroupListing() {
$subscribedGroup = $this->endpoint->listing([ $subscribedGroup = $this->endpoint->listing([
'group' => Subscriber::STATUS_SUBSCRIBED, 'group' => SubscriberEntity::STATUS_SUBSCRIBED,
]); ]);
expect($subscribedGroup->meta['count'])->equals(1); expect($subscribedGroup->meta['count'])->equals(1);
expect($subscribedGroup->data[0]['email'])->equals( expect($subscribedGroup->data[0]['email'])->equals(
@@ -452,12 +455,12 @@ class SubscribersTest extends \MailPoetTest {
); );
$unsubscribedGroup = $this->endpoint->listing([ $unsubscribedGroup = $this->endpoint->listing([
'group' => Subscriber::STATUS_UNSUBSCRIBED, 'group' => SubscriberEntity::STATUS_UNSUBSCRIBED,
]); ]);
expect($unsubscribedGroup->meta['count'])->equals(0); expect($unsubscribedGroup->meta['count'])->equals(0);
$unconfirmedGroup = $this->endpoint->listing([ $unconfirmedGroup = $this->endpoint->listing([
'group' => Subscriber::STATUS_UNCONFIRMED, 'group' => SubscriberEntity::STATUS_UNCONFIRMED,
]); ]);
expect($unconfirmedGroup->meta['count'])->equals(1); expect($unconfirmedGroup->meta['count'])->equals(1);
expect($unconfirmedGroup->data[0]['email'])->equals( expect($unconfirmedGroup->data[0]['email'])->equals(
@@ -483,27 +486,26 @@ class SubscribersTest extends \MailPoetTest {
} }
public function testItCorrectSubscriptionStatus() { public function testItCorrectSubscriptionStatus() {
$segment = Segment::createOrUpdate(['name' => 'Segment185245']); $segment = (new SegmentFactory())->create();
$subscriber = Subscriber::createOrUpdate([ $subscriber = (new SubscriberFactory())
'email' => 'third@example.com', ->withEmail('third@example.com')
'status' => Subscriber::STATUS_SUBSCRIBED, ->withStatus(SubscriberEntity::STATUS_SUBSCRIBED)
'segments' => [ ->withSegments([$segment])
$segment->id, ->withSource(Source::API)
], ->create();
'source' => Source::API, $subscriberSegment = $subscriber->getSubscriberSegments()->first();
]); $this->assertInstanceOf(SubscriberSegmentEntity::class, $subscriberSegment);
SubscriberSegment::createOrUpdate([ $subscriberSegment->setStatus(SubscriberEntity::STATUS_UNSUBSCRIBED);
'subscriber_id' => $subscriber->id, $this->entityManager->persist($subscriberSegment);
'segment_id' => $segment->id, $this->entityManager->flush();
'status' => Subscriber::STATUS_UNSUBSCRIBED,
]);
$response = $this->endpoint->listing([ $response = $this->endpoint->listing([
'filter' => [ 'filter' => [
'segment' => $segment->id, 'segment' => (string)$segment->getId(),
], ],
]); ]);
expect($response->data[0]['status'])->equals(Subscriber::STATUS_UNSUBSCRIBED); expect($response->data[0]['status'])->equals(SubscriberEntity::STATUS_UNSUBSCRIBED);
} }
public function testItCanSortAndLimitListing() { public function testItCanSortAndLimitListing() {
@@ -536,23 +538,18 @@ class SubscribersTest extends \MailPoetTest {
} }
public function testItCanFilterSubscribersWithoutSegment() { public function testItCanFilterSubscribersWithoutSegment() {
$subscriber = Subscriber::createOrUpdate( $subscriber = (new SubscriberFactory())
[ ->withEmail('no-segment@example.com')
'email' => 'no-segment@example.com', ->withStatus(SubscriberEntity::STATUS_SUBSCRIBED)
'status' => Subscriber::STATUS_SUBSCRIBED, ->withSource(Source::API)
'segments' => [], ->create();
'source' => Source::API,
] $trashedSubscriber = (new SubscriberFactory())
); ->withEmail('no-segment-in-trash@example.com')
$trashedSubscriber = Subscriber::createOrUpdate( ->withStatus(SubscriberEntity::STATUS_SUBSCRIBED)
[ ->withSource(Source::API)
'email' => 'no-segment-in-trash@example.com', ->withDeletedAt(new Carbon())
'status' => Subscriber::STATUS_SUBSCRIBED, ->create();
'segments' => [],
'source' => Source::API,
]
);
$trashedSubscriber->trash()->save();
$result = $this->endpoint->listing( $result = $this->endpoint->listing(
[ [
@@ -573,7 +570,7 @@ class SubscribersTest extends \MailPoetTest {
}, $data['data'] }, $data['data']
); );
self::assertTrue(in_array((int)$this->subscriber1->getId(), $foundSubscriberIds, true), 'Subscriber 1 was not found.'); self::assertTrue(in_array((int)$this->subscriber1->getId(), $foundSubscriberIds, true), 'Subscriber 1 was not found.');
self::assertTrue(in_array((int)$subscriber->id(), $foundSubscriberIds, true), 'New subscriber without list was not found.'); self::assertTrue(in_array((int)$subscriber->getId(), $foundSubscriberIds, true), 'New subscriber without list was not found.');
$result = $this->endpoint->listing( $result = $this->endpoint->listing(
[ [
@@ -588,17 +585,17 @@ class SubscribersTest extends \MailPoetTest {
self::assertEquals(1, $meta['count'], "Did not find exactly one trashed subscriber without list."); self::assertEquals(1, $meta['count'], "Did not find exactly one trashed subscriber without list.");
self::assertCount(1, $data['data'], "Did not return exactly one trashed subscriber without list."); self::assertCount(1, $data['data'], "Did not return exactly one trashed subscriber without list.");
self::assertEquals($trashedSubscriber->id(), $data['data'][0]['id'], "Did not return the trashed subscriber without list."); self::assertEquals($trashedSubscriber->getId(), $data['data'][0]['id'], "Did not return the trashed subscriber without list.");
} }
public function testItCanBulkDeleteSelectionOfSubscribers() { public function testItCanBulkDeleteSelectionOfSubscribers() {
$deletableSubscriber = Subscriber::createOrUpdate([ $deletableSubscriber = (new SubscriberFactory())
'email' => 'to.be.removed@mailpoet.com', ->withEmail('to.be.removed@mailpoet.com')
]); ->create();
$selectionIds = [ $selectionIds = [
$this->subscriber1->getId(), $this->subscriber1->getId(),
$deletableSubscriber->id, $deletableSubscriber->getId(),
]; ];
$response = $this->endpoint->bulkAction([ $response = $this->endpoint->bulkAction([
@@ -611,11 +608,13 @@ class SubscribersTest extends \MailPoetTest {
expect($response->data)->isEmpty(); expect($response->data)->isEmpty();
expect($response->meta['count'])->equals(count($selectionIds)); expect($response->meta['count'])->equals(count($selectionIds));
$this->entityManager->clear();
$isSubscriber1Deleted = ( $isSubscriber1Deleted = (
Subscriber::findOne($this->subscriber1->getId()) === false $this->subscribersRepository->findOneById($this->subscriber1->getId()) === null
); );
$isDeletableSubscriberDeleted = ( $isDeletableSubscriberDeleted = (
Subscriber::findOne($deletableSubscriber->id) === false $this->subscribersRepository->findOneById($deletableSubscriber->getId()) === null
); );
expect($isSubscriber1Deleted)->true(); expect($isSubscriber1Deleted)->true();
@@ -742,10 +741,10 @@ class SubscribersTest extends \MailPoetTest {
public function testItCannotSubscribeWithoutBuiltInCaptchaWhenEnabled() { public function testItCannotSubscribeWithoutBuiltInCaptchaWhenEnabled() {
$this->settings->set('captcha', ['type' => Captcha::TYPE_BUILTIN]); $this->settings->set('captcha', ['type' => Captcha::TYPE_BUILTIN]);
$email = 'toto@mailpoet.com'; $email = 'toto@mailpoet.com';
$subscriber = Subscriber::create(); (new SubscriberFactory())
$subscriber->email = $email; ->withEmail($email)
$subscriber->countConfirmations = 1; ->withCountConfirmations(1)
$subscriber->save(); ->create();
$response = $this->endpoint->subscribe([ $response = $this->endpoint->subscribe([
$this->obfuscatedEmail => $email, $this->obfuscatedEmail => $email,
'form_id' => $this->form->getId(), 'form_id' => $this->form->getId(),
@@ -759,10 +758,10 @@ class SubscribersTest extends \MailPoetTest {
public function testItCanSubscribeWithBuiltInCaptchaWhenEnabled() { public function testItCanSubscribeWithBuiltInCaptchaWhenEnabled() {
$this->settings->set('captcha', ['type' => Captcha::TYPE_BUILTIN]); $this->settings->set('captcha', ['type' => Captcha::TYPE_BUILTIN]);
$email = 'toto@mailpoet.com'; $email = 'toto@mailpoet.com';
$subscriber = Subscriber::create(); (new SubscriberFactory())
$subscriber->email = $email; ->withEmail($email)
$subscriber->countConfirmations = 1; ->withCountConfirmations(1)
$subscriber->save(); ->create();
$captchaValue = 'ihG5W'; $captchaValue = 'ihG5W';
$captchaSessionId = 'abcdfgh'; $captchaSessionId = 'abcdfgh';
$this->captchaSession->init($captchaSessionId); $this->captchaSession->init($captchaSessionId);
@@ -871,13 +870,15 @@ class SubscribersTest extends \MailPoetTest {
]); ]);
// Try to resubscribe an existing subscriber that was updated just now // Try to resubscribe an existing subscriber that was updated just now
$subscriber = Subscriber::where('email', 'toto@mailpoet.com')->findOne(); $subscriber = $this->subscribersRepository->findOneBy(['email' => 'toto@mailpoet.com']);
$subscriber->createdAt = Carbon::yesterday(); $this->assertInstanceOf(SubscriberEntity::class, $subscriber);
$subscriber->updatedAt = Carbon::now(); $subscriber->setCreatedAt(Carbon::yesterday());
$subscriber->save(); $subscriber->setUpdatedAt(Carbon::now());
$this->subscribersRepository->persist($subscriber);
$this->subscribersRepository->flush();
$response = $this->endpoint->subscribe([ $response = $this->endpoint->subscribe([
$this->obfuscatedEmail => $subscriber->email, $this->obfuscatedEmail => $subscriber->getEmail(),
'form_id' => $this->form->getId(), 'form_id' => $this->form->getId(),
$this->obfuscatedSegments => [$this->segment1->getId(), $this->segment2->getId()], $this->obfuscatedSegments => [$this->segment1->getId(), $this->segment2->getId()],
]); ]);
@@ -903,8 +904,8 @@ class SubscribersTest extends \MailPoetTest {
$this->obfuscatedSegments => [$this->segment1->getId(), $this->segment2->getId()], $this->obfuscatedSegments => [$this->segment1->getId(), $this->segment2->getId()],
]); ]);
$didSubscribe = Subscriber::where('email', 'toto@mailpoet.com')->findOne(); $didSubscribe = $this->subscribersRepository->findOneBy(['email' => 'toto@mailpoet.com']);
expect($didSubscribe)->equals(false); expect($didSubscribe)->null();
expect($response)->isInstanceOf(ErrorResponse::class); expect($response)->isInstanceOf(ErrorResponse::class);
expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST); expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST);
expect($response->errors[0]['message'])->equals($expectedErrorMessage); expect($response->errors[0]['message'])->equals($expectedErrorMessage);
@@ -947,18 +948,17 @@ class SubscribersTest extends \MailPoetTest {
public function testItDoesNotSchedulesWelcomeEmailNotificationWhenNoNewSegmentIsAdded() { public function testItDoesNotSchedulesWelcomeEmailNotificationWhenNoNewSegmentIsAdded() {
$this->_createWelcomeNewsletter(); $this->_createWelcomeNewsletter();
$subscriber = Subscriber::createOrUpdate([ $subscriber = (new SubscriberFactory())
'email' => 'raul.doe@mailpoet.com', ->withEmail('raul.doe@mailpoet.com')
'first_name' => 'Jane', ->withFirstName('Jane')
'last_name' => 'Doe', ->withLastName('Doe')
'status' => Subscriber::STATUS_SUBSCRIBED, ->withStatus(SubscriberEntity::STATUS_SUBSCRIBED)
'segments' => [ ->withSegments([$this->segment1])
$this->segment1->getId(), ->withSource(Source::IMPORTED)
], ->create();
'source' => Source::IMPORTED,
]);
$subscriberData = [ $subscriberData = [
'id' => $subscriber->id(), 'id' => $subscriber->getId(),
'email' => 'raul.doe@mailpoet.com', 'email' => 'raul.doe@mailpoet.com',
'first_name' => 'Raul', 'first_name' => 'Raul',
'last_name' => 'Doe', 'last_name' => 'Doe',
@@ -993,22 +993,19 @@ class SubscribersTest extends \MailPoetTest {
} }
public function testItKeepsSpecialSegmentsUnchangedAfterSaving() { public function testItKeepsSpecialSegmentsUnchangedAfterSaving() {
$wcSegment = Segment::createOrUpdate([ $wcSegment = (new SegmentFactory())
'name' => 'WooCommerce Users', ->withName('WooCommerce Users')
'type' => Segment::TYPE_WC_USERS, ->withType(Segment::TYPE_WC_USERS)
]); ->create();
$subscriber = Subscriber::createOrUpdate([ $subscriber = (new SubscriberFactory())
'email' => 'woo@commerce.com', ->withEmail('woo@commerce.com')
'first_name' => 'Woo', ->withFirstName('Woo')
'last_name' => 'Commerce', ->withLastName('Commerce')
'status' => Subscriber::STATUS_SUBSCRIBED, ->withStatus(SubscriberEntity::STATUS_SUBSCRIBED)
'segments' => [ ->withSegments([$this->segment1, $wcSegment])
$this->segment1->getId(), ->create();
$wcSegment->id,
],
]);
$subscriberData = [ $subscriberData = [
'id' => $subscriber->id(), 'id' => $subscriber->getId(),
'email' => 'woo@commerce.com', 'email' => 'woo@commerce.com',
'first_name' => 'Woo', 'first_name' => 'Woo',
'last_name' => 'Commerce', 'last_name' => 'Commerce',
@@ -1018,12 +1015,9 @@ class SubscribersTest extends \MailPoetTest {
]; ];
$this->endpoint->save($subscriberData); $this->endpoint->save($subscriberData);
$subscriberRepository = $this->diContainer->get(SubscribersRepository::class);
$subscriber = $subscriberRepository->findOneById($subscriber->id);
$this->assertInstanceOf(SubscriberEntity::class, $subscriber);
$segments = $subscriber->getSegments(); $segments = $subscriber->getSegments();
expect($segments->get(0)->getId())->equals($this->segment1->getId()); expect($segments->get(0)->getId())->equals($this->segment1->getId());
expect($segments->get(1)->getId())->equals($wcSegment->id); expect($segments->get(1)->getId())->equals($wcSegment->getId());
} }
private function _createWelcomeNewsletter(): void { private function _createWelcomeNewsletter(): void {