-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MC-31752: Admin: Unsubscribe/subscribe newsletter email
- Loading branch information
Showing
4 changed files
with
141 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,104 +3,175 @@ | |
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Newsletter\Model; | ||
|
||
use Magento\Customer\Api\CustomerRepositoryInterface; | ||
use Magento\Framework\ObjectManagerInterface; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\TestFramework\Mail\Template\TransportBuilderMock; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* \Magento\Newsletter\Model\Subscriber tests | ||
* Class checks subscription behavior. | ||
* | ||
* @see \Magento\Newsletter\Model\Subscriber | ||
*/ | ||
class SubscriberTest extends \PHPUnit\Framework\TestCase | ||
class SubscriberTest extends TestCase | ||
{ | ||
/** @var ObjectManagerInterface */ | ||
private $objectManager; | ||
|
||
/** @var SubscriberFactory */ | ||
private $subscriberFactory; | ||
|
||
/** @var TransportBuilderMock */ | ||
private $transportBuilder; | ||
|
||
/** @var CustomerRepositoryInterface */ | ||
private $customerRepository; | ||
|
||
/** | ||
* @var Subscriber | ||
* @inheritdoc | ||
*/ | ||
private $model; | ||
|
||
protected function setUp() | ||
{ | ||
$this->model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( | ||
\Magento\Newsletter\Model\Subscriber::class | ||
); | ||
$this->objectManager = Bootstrap::getObjectManager(); | ||
$this->subscriberFactory = $this->objectManager->get(SubscriberFactory::class); | ||
$this->transportBuilder = $this->objectManager->get(TransportBuilderMock::class); | ||
$this->customerRepository = $this->objectManager->get(CustomerRepositoryInterface::class); | ||
} | ||
|
||
/** | ||
* @magentoDataFixture Magento/Newsletter/_files/subscribers.php | ||
* @magentoConfigFixture current_store newsletter/subscription/confirm 1 | ||
* | ||
* @magentoDataFixture Magento/Newsletter/_files/subscribers.php | ||
* | ||
* @return void | ||
*/ | ||
public function testEmailConfirmation() | ||
public function testEmailConfirmation(): void | ||
{ | ||
$this->model->subscribe('[email protected]'); | ||
/** @var TransportBuilderMock $transportBuilder */ | ||
$transportBuilder = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() | ||
->get(\Magento\TestFramework\Mail\Template\TransportBuilderMock::class); | ||
$subscriber = $this->subscriberFactory->create(); | ||
$subscriber->subscribe('[email protected]'); | ||
// confirmationCode 'ysayquyajua23iq29gxwu2eax2qb6gvy' is taken from fixture | ||
$this->assertContains( | ||
'/newsletter/subscriber/confirm/id/' . $this->model->getSubscriberId() | ||
'/newsletter/subscriber/confirm/id/' . $subscriber->getSubscriberId() | ||
. '/code/ysayquyajua23iq29gxwu2eax2qb6gvy', | ||
$transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent() | ||
$this->transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent() | ||
); | ||
$this->assertEquals(Subscriber::STATUS_NOT_ACTIVE, $this->model->getSubscriberStatus()); | ||
$this->assertEquals(Subscriber::STATUS_NOT_ACTIVE, $subscriber->getSubscriberStatus()); | ||
} | ||
|
||
/** | ||
* @magentoDataFixture Magento/Newsletter/_files/subscribers.php | ||
* | ||
* @return void | ||
*/ | ||
public function testLoadByCustomerId() | ||
public function testLoadByCustomerId(): void | ||
{ | ||
$this->assertSame($this->model, $this->model->loadByCustomerId(1)); | ||
$this->assertEquals('[email protected]', $this->model->getSubscriberEmail()); | ||
$subscriber = $this->subscriberFactory->create(); | ||
$this->assertSame($subscriber, $subscriber->loadByCustomerId(1)); | ||
$this->assertEquals('[email protected]', $subscriber->getSubscriberEmail()); | ||
} | ||
|
||
/** | ||
* @magentoDataFixture Magento/Newsletter/_files/subscribers.php | ||
* @magentoAppArea frontend | ||
* | ||
* @magentoAppArea frontend | ||
* | ||
* @return void | ||
*/ | ||
public function testUnsubscribeSubscribe() | ||
public function testUnsubscribeSubscribe(): void | ||
{ | ||
// Unsubscribe and verify | ||
$this->assertSame($this->model, $this->model->loadByCustomerId(1)); | ||
$this->assertEquals($this->model, $this->model->unsubscribe()); | ||
$this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $this->model->getSubscriberStatus()); | ||
|
||
$subscriber = $this->subscriberFactory->create(); | ||
$this->assertSame($subscriber, $subscriber->loadByCustomerId(1)); | ||
$this->assertEquals($subscriber, $subscriber->unsubscribe()); | ||
$this->assertContains( | ||
'You have been unsubscribed from the newsletter.', | ||
$this->transportBuilder->getSentMessage()->getRawMessage() | ||
); | ||
$this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $subscriber->getSubscriberStatus()); | ||
// Subscribe and verify | ||
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $this->model->subscribe('[email protected]')); | ||
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $this->model->getSubscriberStatus()); | ||
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->subscribe('[email protected]')); | ||
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->getSubscriberStatus()); | ||
$this->assertContains( | ||
'You have been successfully subscribed to our newsletter.', | ||
$this->transportBuilder->getSentMessage()->getRawMessage() | ||
); | ||
} | ||
|
||
/** | ||
* @magentoDataFixture Magento/Newsletter/_files/subscribers.php | ||
* @magentoAppArea frontend | ||
* | ||
* @magentoAppArea frontend | ||
* | ||
* @return void | ||
*/ | ||
public function testUnsubscribeSubscribeByCustomerId() | ||
public function testUnsubscribeSubscribeByCustomerId(): void | ||
{ | ||
$subscriber = $this->subscriberFactory->create(); | ||
// Unsubscribe and verify | ||
$this->assertSame($this->model, $this->model->unsubscribeCustomerById(1)); | ||
$this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $this->model->getSubscriberStatus()); | ||
|
||
$this->assertSame($subscriber, $subscriber->unsubscribeCustomerById(1)); | ||
$this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $subscriber->getSubscriberStatus()); | ||
$this->assertContains( | ||
'You have been unsubscribed from the newsletter.', | ||
$this->transportBuilder->getSentMessage()->getRawMessage() | ||
); | ||
// Subscribe and verify | ||
$this->assertSame($this->model, $this->model->subscribeCustomerById(1)); | ||
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $this->model->getSubscriberStatus()); | ||
$this->assertSame($subscriber, $subscriber->subscribeCustomerById(1)); | ||
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->getSubscriberStatus()); | ||
$this->assertContains( | ||
'You have been successfully subscribed to our newsletter.', | ||
$this->transportBuilder->getSentMessage()->getRawMessage() | ||
); | ||
} | ||
|
||
/** | ||
* @magentoDataFixture Magento/Newsletter/_files/subscribers.php | ||
* @magentoConfigFixture current_store newsletter/subscription/confirm 1 | ||
* | ||
* @magentoDataFixture Magento/Newsletter/_files/subscribers.php | ||
* | ||
* @return void | ||
*/ | ||
public function testConfirm() | ||
public function testConfirm(): void | ||
{ | ||
$subscriber = $this->subscriberFactory->create(); | ||
$customerEmail = '[email protected]'; | ||
$this->model->subscribe($customerEmail); | ||
$this->model->loadByEmail($customerEmail); | ||
$this->model->confirm($this->model->getSubscriberConfirmCode()); | ||
|
||
$transportBuilder = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( | ||
\Magento\TestFramework\Mail\Template\TransportBuilderMock::class | ||
); | ||
|
||
$subscriber->subscribe($customerEmail); | ||
$subscriber->loadByEmail($customerEmail); | ||
$subscriber->confirm($subscriber->getSubscriberConfirmCode()); | ||
$this->assertContains( | ||
'You have been successfully subscribed to our newsletter.', | ||
$transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent() | ||
$this->transportBuilder->getSentMessage()->getRawMessage() | ||
); | ||
} | ||
|
||
/** | ||
* @magentoDataFixture Magento/Customer/_files/customer_confirmation_config_enable.php | ||
* @magentoDataFixture Magento/Newsletter/_files/newsletter_unconfirmed_customer.php | ||
* | ||
* @return void | ||
*/ | ||
public function testSubscribeUnconfirmedCustomerWithSubscription(): void | ||
{ | ||
$customer = $this->customerRepository->get('[email protected]'); | ||
$subscriber = $this->subscriberFactory->create(); | ||
$subscriber->subscribeCustomerById($customer->getId()); | ||
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->getStatus()); | ||
} | ||
|
||
/** | ||
* @magentoDataFixture Magento/Customer/_files/customer_confirmation_config_enable.php | ||
* @magentoDataFixture Magento/Customer/_files/unconfirmed_customer.php | ||
* | ||
* @return void | ||
*/ | ||
public function testSubscribeUnconfirmedCustomerWithoutSubscription(): void | ||
{ | ||
$customer = $this->customerRepository->get('[email protected]'); | ||
$subscriber = $this->subscriberFactory->create(); | ||
$subscriber->subscribeCustomerById($customer->getId()); | ||
$this->assertEquals(Subscriber::STATUS_UNCONFIRMED, $subscriber->getStatus()); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...tests/integration/testsuite/Magento/Newsletter/_files/newsletter_unconfirmed_customer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
use Magento\Newsletter\Model\SubscriberFactory; | ||
|
||
require __DIR__ . '/../../../Magento/Customer/_files/unconfirmed_customer.php'; | ||
|
||
/** @var SubscriberFactory $subscriberFactory */ | ||
$subscriberFactory = $objectManager->get(SubscriberFactory::class); | ||
$subscriberFactory->create()->subscribe('[email protected]'); |
8 changes: 8 additions & 0 deletions
8
...egration/testsuite/Magento/Newsletter/_files/newsletter_unconfirmed_customer_rollback.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
require __DIR__ . '/../../../Magento/Customer/_files/unconfirmed_customer_rollback.php'; |