ltsecurityinc安防产品在线商城代码
This commit is contained in:
+134
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Newsletter\Controller\Adminhtml;
|
||||
|
||||
use Magento\Framework\App\Request\Http as HttpRequest;
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class NewsletterQueueTest extends \Magento\TestFramework\TestCase\AbstractBackendController
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Newsletter\Model\Template
|
||||
*/
|
||||
protected $_model;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Newsletter\Model\Template::class
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
/**
|
||||
* Unset messages
|
||||
*/
|
||||
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Backend\Model\Session::class
|
||||
)->getMessages(
|
||||
true
|
||||
);
|
||||
$this->_model = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Newsletter/_files/newsletter_sample.php
|
||||
* @dataProvider postValuesForRequest
|
||||
*/
|
||||
public function testSaveActionQueueTemplateAndVerifySuccessMessage(array $postForQueue)
|
||||
{
|
||||
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
|
||||
$this->getRequest()->setPostValue($postForQueue);
|
||||
|
||||
// Loading by code, since ID will vary. template_code is not actually used to load anywhere else.
|
||||
$this->_model->load('some_unique_code', 'template_code');
|
||||
|
||||
// Ensure that template is actually loaded so as to prevent a false positive on saving a *new* template
|
||||
// instead of existing one.
|
||||
$this->assertEquals('some_unique_code', $this->_model->getTemplateCode());
|
||||
|
||||
$this->getRequest()->setParam('template_id', $this->_model->getId());
|
||||
$this->dispatch('backend/newsletter/queue/save');
|
||||
|
||||
/**
|
||||
* Check that errors was generated and set to session
|
||||
*/
|
||||
$this->assertSessionMessages($this->isEmpty(), \Magento\Framework\Message\MessageInterface::TYPE_ERROR);
|
||||
|
||||
/**
|
||||
* Check that success message is set
|
||||
*/
|
||||
$this->assertSessionMessages(
|
||||
$this->equalTo(['You saved the newsletter queue.']),
|
||||
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The data provider for possible request values of the start_at.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function postValuesForRequest(): array
|
||||
{
|
||||
return [
|
||||
'start_at_value_is_integer_zero' => [
|
||||
[
|
||||
'sender_email' => 'johndoe_gieee@unknown-domain.com',
|
||||
'sender_name' => 'john doe',
|
||||
'subject' => 'test subject',
|
||||
'text' => 'newsletter text',
|
||||
'start_at' => 0
|
||||
]
|
||||
],
|
||||
'start_at_value_is_string_zero' => [
|
||||
[
|
||||
'sender_email' => 'johndoe_gieee@unknown-domain.com',
|
||||
'sender_name' => 'john doe',
|
||||
'subject' => 'test subject',
|
||||
'text' => 'newsletter text',
|
||||
'start_at' => '0'
|
||||
]
|
||||
],
|
||||
'start_at_value_is_empty_string' => [
|
||||
[
|
||||
'sender_email' => 'johndoe_gieee@unknown-domain.com',
|
||||
'sender_name' => 'john doe',
|
||||
'subject' => 'test subject',
|
||||
'text' => 'newsletter text',
|
||||
'start_at' => ''
|
||||
]
|
||||
],
|
||||
'start_at_value_not_provided' => [
|
||||
[
|
||||
'sender_email' => 'johndoe_gieee@unknown-domain.com',
|
||||
'sender_name' => 'john doe',
|
||||
'subject' => 'test subject',
|
||||
'text' => 'newsletter text'
|
||||
]
|
||||
],
|
||||
'start_at_value_is_date_time_string' => [
|
||||
[
|
||||
'sender_email' => 'johndoe_gieee@unknown-domain.com',
|
||||
'sender_name' => 'john doe',
|
||||
'subject' => 'test subject',
|
||||
'text' => 'newsletter text',
|
||||
'start_at' => date('Y-m-d H:i:s')
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Newsletter\Controller\Adminhtml;
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class NewsletterTemplateTest extends \Magento\TestFramework\TestCase\AbstractBackendController
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $formKey;
|
||||
|
||||
/**
|
||||
* @var \Magento\Newsletter\Model\Template
|
||||
*/
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$formKey = $this->_objectManager->get(\Magento\Framework\Data\Form\FormKey::class);
|
||||
$this->formKey = $formKey->getFormKey();
|
||||
$post = [
|
||||
'code' => 'test data',
|
||||
'subject' => 'test data2',
|
||||
'sender_email' => 'sender@email.com',
|
||||
'sender_name' => 'Test Sender Name',
|
||||
'text' => 'Template Content',
|
||||
'form_key' => $this->formKey,
|
||||
];
|
||||
$this->getRequest()->setPostValue($post)->setMethod(\Laminas\Http\Request::METHOD_POST);
|
||||
$this->model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Newsletter\Model\Template::class
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
/**
|
||||
* Unset messages
|
||||
*/
|
||||
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Backend\Model\Session::class)
|
||||
->destroy();
|
||||
$this->model = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
public function testSaveActionCreateNewTemplateAndVerifySuccessMessage()
|
||||
{
|
||||
$this->getRequest()->setParam('id', $this->model->getId());
|
||||
|
||||
$this->dispatch('backend/newsletter/template/save');
|
||||
|
||||
/**
|
||||
* Check that errors was generated and set to session
|
||||
*/
|
||||
$this->assertSessionMessages($this->isEmpty(), \Magento\Framework\Message\MessageInterface::TYPE_ERROR);
|
||||
|
||||
$this->model->load($this->getRequest()->getPostValue('code'), 'template_code');
|
||||
|
||||
$this->assertEquals(0, $this->model->getIsLegacy());
|
||||
|
||||
/**
|
||||
* Check that success message is set
|
||||
*/
|
||||
$this->assertSessionMessages(
|
||||
$this->equalTo(['The newsletter template has been saved.']),
|
||||
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDataFixture Magento/Newsletter/_files/newsletter_sample.php
|
||||
*/
|
||||
public function testSaveActionEditTemplateAndVerifySuccessMessage()
|
||||
{
|
||||
// Loading by code, since ID will vary. template_code is not actually used to load anywhere else.
|
||||
$this->model->load('some_unique_code', 'template_code');
|
||||
|
||||
// Ensure that template is actually loaded so as to prevent a false positive on saving a *new* template
|
||||
// instead of existing one.
|
||||
$this->assertEquals('some_unique_code', $this->model->getTemplateCode());
|
||||
|
||||
$this->getRequest()->setParam('id', $this->model->getId());
|
||||
|
||||
$this->dispatch('backend/newsletter/template/save');
|
||||
|
||||
/**
|
||||
* Check that errors was generated and set to session
|
||||
*/
|
||||
$this->assertSessionMessages($this->isEmpty(), \Magento\Framework\Message\MessageInterface::TYPE_ERROR);
|
||||
|
||||
$this->model->load($this->getRequest()->getPostValue('code'), 'template_code');
|
||||
|
||||
$this->assertEquals(0, $this->model->getIsLegacy());
|
||||
|
||||
/**
|
||||
* Check that success message is set
|
||||
*/
|
||||
$this->assertSessionMessages(
|
||||
$this->equalTo(['The newsletter template has been saved.']),
|
||||
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDataFixture Magento/Newsletter/_files/newsletter_sample.php
|
||||
*/
|
||||
public function testDeleteActionTemplateAndVerifySuccessMessage()
|
||||
{
|
||||
// Loading by code, since ID will vary. template_code is not actually used to load anywhere else.
|
||||
$this->model->load('some_unique_code', 'template_code');
|
||||
|
||||
$this->getRequest()->setParam('id', $this->model->getId());
|
||||
$this->dispatch('backend/newsletter/template/delete');
|
||||
|
||||
/**
|
||||
* Check that errors was generated and set to session
|
||||
*/
|
||||
$this->assertSessionMessages($this->isEmpty(), \Magento\Framework\Message\MessageInterface::TYPE_ERROR);
|
||||
|
||||
/**
|
||||
* Check that success message is set
|
||||
*/
|
||||
$this->assertSessionMessages(
|
||||
$this->equalTo(['The newsletter template has been deleted.']),
|
||||
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDataFixture Magento/Newsletter/_files/newsletter_sample.php
|
||||
*/
|
||||
public function testSaveActionTemplateWithGetAndVerifyRedirect()
|
||||
{
|
||||
// Loading by code, since ID will vary. template_code is not actually used to load anywhere else.
|
||||
$this->model->load('some_unique_code', 'template_code');
|
||||
|
||||
$this->getRequest()->setMethod(\Laminas\Http\Request::METHOD_GET)->setParam('id', $this->model->getId());
|
||||
$this->dispatch('backend/newsletter/template/save');
|
||||
|
||||
$this->assertEquals(404, $this->getResponse()->getStatusCode());
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Newsletter\Controller\Adminhtml\Template;
|
||||
|
||||
use Magento\Framework\App\Request\Http as HttpRequest;
|
||||
use Magento\Framework\Data\Form\FormKey;
|
||||
use Magento\Newsletter\Model\Template;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\TestFramework\TestCase\AbstractBackendController;
|
||||
|
||||
class DropTest extends AbstractBackendController
|
||||
{
|
||||
public function testDefaultTemplateAction()
|
||||
{
|
||||
$website = $this->_objectManager
|
||||
->get(StoreManagerInterface::class)
|
||||
->getWebsite();
|
||||
|
||||
$storeId = $website->getDefaultStore()
|
||||
->getId();
|
||||
|
||||
/** @var $formKey FormKey */
|
||||
$formKey = $this->_objectManager->get(FormKey::class);
|
||||
$post = [
|
||||
'form_key' => $formKey->getFormKey(),
|
||||
'type' => Template::TYPE_HTML,
|
||||
'preview_store_id' => $storeId,
|
||||
'text' => 'Template {{var this.template_id}}:{{var this.getData(template_id)}} Text'
|
||||
];
|
||||
$this->getRequest()->setPostValue($post);
|
||||
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
|
||||
$this->dispatch('backend/newsletter/template/drop');
|
||||
$this->assertStringContainsString(
|
||||
'Template : Text',
|
||||
$this->getResponse()->getBody()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Newsletter\Controller\Ajax;
|
||||
|
||||
use Magento\Framework\Serialize\SerializerInterface;
|
||||
use Magento\TestFramework\TestCase\AbstractController;
|
||||
|
||||
/**
|
||||
* Test Subscriber status ajax
|
||||
*/
|
||||
class StatusTest extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @var SerializerInterface
|
||||
*/
|
||||
private $json;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->json = $this->_objectManager->get(SerializerInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check newsletter subscription status verification
|
||||
*
|
||||
* @magentoDataFixture Magento/Newsletter/_files/subscribers.php
|
||||
* @dataProvider ajaxSubscriberDataProvider
|
||||
* @param bool $expStatus
|
||||
* @param string $email
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testExecute(bool $expStatus, string $email): void
|
||||
{
|
||||
$this->getRequest()->setParam('email', $email);
|
||||
$this->dispatch('newsletter/ajax/status');
|
||||
$actual = $this->json->unserialize($this->getResponse()->getBody());
|
||||
|
||||
$this->assertEquals($expStatus, $actual['subscribed']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides data and Expected Result
|
||||
*
|
||||
* @param void
|
||||
* @return array
|
||||
*/
|
||||
public function ajaxSubscriberDataProvider(): array
|
||||
{
|
||||
return [
|
||||
'empty_email_parameter' => [false, ''],
|
||||
'invalid_email' => [false, 'invalid_email.com'],
|
||||
'unsubscribed_email' => [false, 'sample@email.com'],
|
||||
'subscribed_guest_email' => [true, 'customer_two@example.com'],
|
||||
'subscribed_registered_customer_email' => [false, 'customer@example.com'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Newsletter\Controller\Manage;
|
||||
|
||||
use Magento\Customer\Api\CustomerRepositoryInterface;
|
||||
use Magento\Customer\Model\CustomerRegistry;
|
||||
use Magento\Customer\Model\Session;
|
||||
use Magento\Framework\Data\Form\FormKey;
|
||||
use Magento\Framework\Message\MessageInterface;
|
||||
use Magento\Newsletter\Model\CustomerSubscriberCache;
|
||||
use Magento\Newsletter\Model\Plugin\CustomerPlugin;
|
||||
use Magento\TestFramework\TestCase\AbstractController;
|
||||
|
||||
/**
|
||||
* Class checks customer subscription
|
||||
*
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
class SaveTest extends AbstractController
|
||||
{
|
||||
/** @var Session */
|
||||
protected $customerSession;
|
||||
|
||||
/** @var CustomerRepositoryInterface */
|
||||
private $customerRepository;
|
||||
|
||||
/** @var FormKey */
|
||||
private $formKey;
|
||||
|
||||
/** @var CustomerRegistry */
|
||||
private $customerRegistry;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->customerSession = $this->_objectManager->get(Session::class);
|
||||
$this->customerRepository = $this->_objectManager->get(CustomerRepositoryInterface::class);
|
||||
$this->formKey = $this->_objectManager->get(FormKey::class);
|
||||
$this->customerRegistry = $this->_objectManager->get(CustomerRegistry::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->customerSession->logout();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Customer/_files/new_customer.php
|
||||
*
|
||||
* @dataProvider subscriptionDataProvider
|
||||
*
|
||||
* @param bool $isSubscribed
|
||||
* @param string $expectedMessage
|
||||
* @return void
|
||||
*/
|
||||
public function testSaveAction(bool $isSubscribed, string $expectedMessage): void
|
||||
{
|
||||
$this->loginCustomer('new_customer@example.com');
|
||||
$this->dispatchSaveAction($isSubscribed);
|
||||
$this->assertSuccessSubscription($expectedMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function subscriptionDataProvider(): array
|
||||
{
|
||||
return [
|
||||
'subscribe_customer' => [
|
||||
'is_subscribed' => true,
|
||||
'expected_message' => 'We have saved your subscription.',
|
||||
],
|
||||
'unsubscribe_customer' => [
|
||||
'is_subscribed' => false,
|
||||
'expected_message' => 'We have updated your subscription.',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Customer/_files/customer_confirmation_config_enable.php
|
||||
* @magentoDataFixture Magento/Customer/_files/new_customer.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSubscribeWithEnabledConfirmation(): void
|
||||
{
|
||||
$this->loginCustomer('new_customer@example.com');
|
||||
$this->dispatchSaveAction(true);
|
||||
$this->assertSuccessSubscription('A confirmation request has been sent.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Newsletter/_files/customer_with_subscription.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testUnsubscribeSubscribedCustomer(): void
|
||||
{
|
||||
$this->loginCustomer('new_customer@example.com');
|
||||
$this->dispatchSaveAction(false);
|
||||
$this->assertSuccessSubscription('We have removed your newsletter subscription.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch save action with parameters
|
||||
*
|
||||
* @param string $isSubscribed
|
||||
* @return void
|
||||
*/
|
||||
private function dispatchSaveAction(bool $isSubscribed): void
|
||||
{
|
||||
$this->_objectManager->removeSharedInstance(CustomerPlugin::class);
|
||||
$this->_objectManager->removeSharedInstance(CustomerSubscriberCache::class);
|
||||
$this->getRequest()->setParam('form_key', $this->formKey->getFormKey())
|
||||
->setParam('is_subscribed', $isSubscribed);
|
||||
$this->dispatch('newsletter/manage/save');
|
||||
}
|
||||
|
||||
/**
|
||||
* Login customer by email
|
||||
*
|
||||
* @param string $email
|
||||
* @return void
|
||||
*/
|
||||
private function loginCustomer(string $email): void
|
||||
{
|
||||
$customer = $this->customerRepository->get($email);
|
||||
$this->customerSession->loginById($customer->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that action was successfully done
|
||||
*
|
||||
* @param string $expectedMessage
|
||||
* @return void
|
||||
*/
|
||||
private function assertSuccessSubscription(string $expectedMessage): void
|
||||
{
|
||||
$this->assertRedirect($this->stringContains('customer/account/'));
|
||||
$this->assertSessionMessages($this->equalTo([(string)__($expectedMessage)]), MessageInterface::TYPE_SUCCESS);
|
||||
}
|
||||
}
|
||||
+285
@@ -0,0 +1,285 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Newsletter\Controller\Subscriber;
|
||||
|
||||
use Magento\Customer\Api\CustomerRepositoryInterface;
|
||||
use Magento\Customer\Model\AccountManagement;
|
||||
use Magento\Customer\Model\Session;
|
||||
use Magento\Customer\Model\Url;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\App\Request\Http as HttpRequest;
|
||||
use Magento\Newsletter\Model\ResourceModel\Subscriber as SubscriberResource;
|
||||
use Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory;
|
||||
use Magento\Newsletter\Model\ResourceModel\Subscriber\Grid\Collection as GridCollection;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Magento\TestFramework\TestCase\AbstractController;
|
||||
use Laminas\Stdlib\Parameters;
|
||||
|
||||
/**
|
||||
* Class checks subscription behaviour from frontend
|
||||
*
|
||||
* @magentoDbIsolation enabled
|
||||
* @see \Magento\Newsletter\Controller\Subscriber\NewAction
|
||||
*/
|
||||
class NewActionTest extends AbstractController
|
||||
{
|
||||
/** @var Session */
|
||||
private $session;
|
||||
|
||||
/** @var CollectionFactory */
|
||||
private $subscriberCollectionFactory;
|
||||
|
||||
/** @var SubscriberResource */
|
||||
private $subscriberResource;
|
||||
|
||||
/** @var string|null */
|
||||
private $subscriberToDelete;
|
||||
|
||||
/** @var CustomerRepositoryInterface */
|
||||
private $customerRepository;
|
||||
|
||||
/** @var Url */
|
||||
private $customerUrl;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->session = $this->_objectManager->get(Session::class);
|
||||
$this->subscriberCollectionFactory = $this->_objectManager->get(CollectionFactory::class);
|
||||
$this->subscriberResource = $this->_objectManager->get(SubscriberResource::class);
|
||||
$this->customerRepository = $this->_objectManager->get(CustomerRepositoryInterface::class);
|
||||
$this->customerUrl = $this->_objectManager->get(Url::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
if ($this->subscriberToDelete) {
|
||||
$this->deleteSubscriber($this->subscriberToDelete);
|
||||
}
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider subscribersDataProvider
|
||||
*
|
||||
* @param string $email
|
||||
* @param string $expectedMessage
|
||||
* @return void
|
||||
*/
|
||||
public function testNewAction(string $email, string $expectedMessage): void
|
||||
{
|
||||
$this->subscriberToDelete = $email ? $email : null;
|
||||
$this->prepareRequest($email);
|
||||
$this->dispatch('newsletter/subscriber/new');
|
||||
|
||||
$this->performAsserts($expectedMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function subscribersDataProvider(): array
|
||||
{
|
||||
return [
|
||||
'without_email' => [
|
||||
'email' => '',
|
||||
'message' => '',
|
||||
],
|
||||
'with_unused_email' => [
|
||||
'email' => 'not_used@example.com',
|
||||
'message' => 'Thank you for your subscription.',
|
||||
],
|
||||
'with_invalid_email' => [
|
||||
'email' => 'invalid_email.com',
|
||||
'message' => 'Please enter a valid email address.'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Customer/_files/new_customer.php
|
||||
* @dataProvider emailAndStatusDataProvider
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNewActionUsedEmail($email, $subscriptionType): void
|
||||
{
|
||||
$this->prepareRequest($email);
|
||||
$this->dispatch('newsletter/subscriber/new');
|
||||
|
||||
/** @var GridCollection $gridCollection */
|
||||
$gridCollection = $this->_objectManager->create(GridCollection::class);
|
||||
$item = $gridCollection->getFirstItem();
|
||||
self::assertEquals($subscriptionType, (int)$item->getType());
|
||||
$this->performAsserts('Thank you for your subscription.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function emailAndStatusDataProvider()
|
||||
{
|
||||
return [
|
||||
'customer' => ['new_customer@example.com', 2],
|
||||
'not_a_customer' => ['not_a_customer@gmail.com', 1],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Customer/_files/new_customer.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNewActionOwnerEmail(): void
|
||||
{
|
||||
$this->prepareRequest('new_customer@example.com');
|
||||
$this->session->loginById(1);
|
||||
$this->dispatch('newsletter/subscriber/new');
|
||||
|
||||
$this->performAsserts('Thank you for your subscription.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Newsletter/_files/customer_with_subscription.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAlreadyExistEmail(): void
|
||||
{
|
||||
$this->prepareRequest('new_customer@example.com');
|
||||
$this->dispatch('newsletter/subscriber/new');
|
||||
|
||||
$this->performAsserts('This email address is already subscribed.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store newsletter/subscription/allow_guest_subscribe 0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testWithNotAllowedGuestSubscription(): void
|
||||
{
|
||||
$message = sprintf(
|
||||
'Sorry, but the administrator denied subscription for guests. Please <a href="%s">register</a>.',
|
||||
$this->customerUrl->getRegisterUrl()
|
||||
);
|
||||
$this->subscriberToDelete = 'guest@example.com';
|
||||
$this->prepareRequest('guest@example.com');
|
||||
$this->dispatch('newsletter/subscriber/new');
|
||||
|
||||
$this->performAsserts($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store newsletter/subscription/allow_guest_subscribe 0
|
||||
*
|
||||
* @magentoDataFixture Magento/Customer/_files/new_customer.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCustomerSubscribeUnrelatedEmailWithNotAllowedGuestSubscription(): void
|
||||
{
|
||||
$this->markTestSkipped('Blocked by MC-31662');
|
||||
$this->subscriberToDelete = 'guest@example.com';
|
||||
$this->session->loginById($this->customerRepository->get('new_customer@example.com')->getId());
|
||||
$this->prepareRequest('guest@example.com');
|
||||
$this->dispatch('newsletter/subscriber/new');
|
||||
//ToDo message need to be specified after bug MC-31662 fixing
|
||||
$this->performAsserts('');
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store newsletter/subscription/confirm 1
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testWithRequiredConfirmation(): void
|
||||
{
|
||||
$this->subscriberToDelete = 'guest@example.com';
|
||||
$this->prepareRequest('guest@example.com');
|
||||
$this->dispatch('newsletter/subscriber/new');
|
||||
|
||||
$this->performAsserts('The confirmation request has been sent.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Newsletter/_files/three_subscribers.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testWithEmailAssignedToAnotherCustomer(): void
|
||||
{
|
||||
$this->session->loginById(1);
|
||||
$this->prepareRequest('customer2@search.example.com');
|
||||
$this->dispatch('newsletter/subscriber/new');
|
||||
$scopeConfig = $this->_objectManager->get(ScopeConfigInterface::class);
|
||||
$guestLoginConfig = $scopeConfig->getValue(
|
||||
AccountManagement::GUEST_CHECKOUT_LOGIN_OPTION_SYS_CONFIG,
|
||||
ScopeInterface::SCOPE_WEBSITE,
|
||||
1
|
||||
);
|
||||
|
||||
if ($guestLoginConfig) {
|
||||
$this->performAsserts('This email address is already assigned to another user.');
|
||||
} else {
|
||||
$this->performAsserts('This email address is already subscribed.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare request
|
||||
*
|
||||
* @param string $email
|
||||
* @return void
|
||||
*/
|
||||
private function prepareRequest(string $email): void
|
||||
{
|
||||
$parameters = $this->_objectManager->create(Parameters::class);
|
||||
$parameters->set('HTTP_REFERER', 'http://localhost/testRedirect');
|
||||
$this->getRequest()->setServer($parameters);
|
||||
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
|
||||
$this->getRequest()->setPostValue(['email' => $email]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert session message and expected redirect
|
||||
*
|
||||
* @param string $message
|
||||
* @return void
|
||||
*/
|
||||
private function performAsserts(string $message): void
|
||||
{
|
||||
if ($message) {
|
||||
$this->assertSessionMessages($this->equalTo([(string)__($message)]));
|
||||
}
|
||||
$this->assertRedirect($this->equalTo('http://localhost/testRedirect'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete subscribers by email
|
||||
*
|
||||
* @param string $email
|
||||
* @return void
|
||||
*/
|
||||
private function deleteSubscriber(string $email): void
|
||||
{
|
||||
$collection = $this->subscriberCollectionFactory->create();
|
||||
$item = $collection->addFieldToFilter('subscriber_email', $email)->setPageSize(1)->getFirstItem();
|
||||
if ($item->getId()) {
|
||||
$this->subscriberResource->delete($item);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Newsletter\Controller;
|
||||
|
||||
use Magento\Customer\Api\CustomerRepositoryInterface;
|
||||
use Magento\Customer\Model\AccountConfirmation;
|
||||
use Magento\Framework\App\Config\MutableScopeConfigInterface;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Framework\Data\Form\FormKey;
|
||||
use Magento\Newsletter\Model\ResourceModel\Subscriber as SubscriberLoader;
|
||||
use Magento\Newsletter\Model\Subscriber;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\TestCase\AbstractController;
|
||||
|
||||
/**
|
||||
* Test Subscriber
|
||||
*/
|
||||
class SubscriberTest extends AbstractController
|
||||
{
|
||||
/**
|
||||
* Check that Customer still subscribed for newsletters emails after registration.
|
||||
*
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
public function testCreatePosWithSubscribeEmailAction()
|
||||
{
|
||||
$this->markTestSkipped('Skip until failed. MAGETWO-96420');
|
||||
|
||||
$config = Bootstrap::getObjectManager()->get(MutableScopeConfigInterface::class);
|
||||
$accountConfirmationRequired = $config->getValue(
|
||||
AccountConfirmation::XML_PATH_IS_CONFIRM,
|
||||
ScopeInterface::SCOPE_WEBSITES
|
||||
);
|
||||
$config->setValue(AccountConfirmation::XML_PATH_IS_CONFIRM, 1, ScopeInterface::SCOPE_WEBSITES);
|
||||
|
||||
$subscriber = Bootstrap::getObjectManager()->create(Subscriber::class);
|
||||
$customerEmail = 'subscribeemail@example.com';
|
||||
// Subscribe by email
|
||||
$subscriber->subscribe($customerEmail);
|
||||
$subscriber->loadByEmail($customerEmail);
|
||||
$subscriber->confirm($subscriber->getSubscriberConfirmCode());
|
||||
|
||||
// Create customer
|
||||
$this->fillRequestWithAccountDataAndFormKey($customerEmail);
|
||||
$this->dispatch('customer/account/createPost');
|
||||
$this->dispatch('customer/account/confirm');
|
||||
|
||||
$customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
|
||||
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
|
||||
$customer = $customerRepository->get($customerEmail);
|
||||
$subscriberResource = Bootstrap::getObjectManager()
|
||||
->create(SubscriberLoader::class);
|
||||
|
||||
// check customer subscribed to newsletter
|
||||
$this->assertTrue($subscriberResource->loadByCustomerData($customer)['subscriber_status'] === "1");
|
||||
|
||||
$config->setValue(
|
||||
AccountConfirmation::XML_PATH_IS_CONFIRM,
|
||||
$accountConfirmationRequired,
|
||||
ScopeInterface::SCOPE_WEBSITES
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Customer Data.
|
||||
*
|
||||
* @param string $email
|
||||
* @return void
|
||||
*/
|
||||
private function fillRequestWithAccountDataAndFormKey($email)
|
||||
{
|
||||
Bootstrap::getObjectManager()->get(RequestInterface::class)
|
||||
->setMethod('POST')
|
||||
->setParam('firstname', 'firstname1')
|
||||
->setParam('lastname', 'lastname1')
|
||||
->setParam('company', '')
|
||||
->setParam('email', $email)
|
||||
->setParam('password', '_Password1')
|
||||
->setParam('password_confirmation', '_Password1')
|
||||
->setParam('telephone', '5123334444')
|
||||
->setParam('street', ['1234 fake street', ''])
|
||||
->setParam('city', 'Austin')
|
||||
->setParam('region_id', 57)
|
||||
->setParam('region', '')
|
||||
->setParam('postcode', '78701')
|
||||
->setParam('country_id', 'US')
|
||||
->setParam('default_billing', '1')
|
||||
->setParam('default_shipping', '1')
|
||||
->setParam('is_subscribed', '0')
|
||||
->setPostValue('create_address', true)
|
||||
->setParam('form_key', Bootstrap::getObjectManager()->get(FormKey::class)->getFormKey());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user