init
This commit is contained in:
+176
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\SendFriend\Controller\Product;
|
||||
|
||||
use Magento\Captcha\Model\DefaultModel;
|
||||
use Magento\TestFramework\TestCase\AbstractController;
|
||||
use Magento\Framework\Data\Form\FormKey;
|
||||
use Magento\Customer\Api\AccountManagementInterface;
|
||||
use Magento\Customer\Model\Session;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Message\MessageInterface;
|
||||
use Magento\Captcha\Helper\Data as CaptchaHelper;
|
||||
|
||||
/**
|
||||
* Class CustomerSendmailTest
|
||||
*/
|
||||
class CustomerSendmailTest extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @var AccountManagementInterface
|
||||
*/
|
||||
private $accountManagement;
|
||||
|
||||
/**
|
||||
* @var FormKey
|
||||
*/
|
||||
private $formKey;
|
||||
|
||||
/**
|
||||
* @var Session
|
||||
*/
|
||||
private $session;
|
||||
|
||||
/**
|
||||
* @var CaptchaHelper
|
||||
*/
|
||||
private $captchaHelper;
|
||||
|
||||
/**
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->accountManagement = $this->_objectManager->create(AccountManagementInterface::class);
|
||||
$this->formKey = $this->_objectManager->create(FormKey::class);
|
||||
$logger = $this->getMockForAbstractClass(LoggerInterface::class);
|
||||
$this->session = $this->_objectManager->create(
|
||||
Session::class,
|
||||
[$logger]
|
||||
);
|
||||
$this->captchaHelper = $this->_objectManager->create(CaptchaHelper::class);
|
||||
$customer = $this->accountManagement->authenticate('customer@example.com', 'password');
|
||||
$this->session->setCustomerDataAsLoggedIn($customer);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default_store sendfriend/email/enabled 1
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
|
||||
*/
|
||||
public function testExecute()
|
||||
{
|
||||
$this->getRequest()
|
||||
->setMethod('POST')
|
||||
->setPostValue(
|
||||
[
|
||||
'form_key' => $this->formKey->getFormKey(),
|
||||
'sender' => [
|
||||
'name' => 'customer',
|
||||
'email' => 'customer@example.com',
|
||||
'message' => 'example message'
|
||||
],
|
||||
'id' => 1,
|
||||
'recipients' => [
|
||||
'name' => ['John'],
|
||||
'email' => ['example1@gmail.com']
|
||||
]
|
||||
|
||||
]
|
||||
);
|
||||
|
||||
$this->dispatch('sendfriend/product/sendmail');
|
||||
$this->assertSessionMessages(
|
||||
$this->equalTo(['The link to a friend was sent.']),
|
||||
MessageInterface::TYPE_SUCCESS
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default_store customer/captcha/enable 1
|
||||
* @magentoConfigFixture default_store customer/captcha/failed_attempts_login 0
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
|
||||
* @magentoConfigFixture default_store customer/captcha/forms product_sendtofriend_form
|
||||
* @magentoConfigFixture default_store sendfriend/email/enabled 1
|
||||
*/
|
||||
public function testWithCaptchaFailed()
|
||||
{
|
||||
$this->getRequest()
|
||||
->setMethod('POST')
|
||||
->setPostValue(
|
||||
[
|
||||
'form_key' => $this->formKey->getFormKey(),
|
||||
'sender' => [
|
||||
'name' => 'customer',
|
||||
'email' => 'customer@example.com',
|
||||
'message' => 'example message'
|
||||
],
|
||||
'id' => 1,
|
||||
'captcha' => [
|
||||
'product_sendtofriend_form' => 'test'
|
||||
],
|
||||
'recipients' => [
|
||||
'name' => ['John'],
|
||||
'email' => ['example1@gmail.com']
|
||||
]
|
||||
|
||||
]
|
||||
);
|
||||
|
||||
$this->dispatch('sendfriend/product/sendmail');
|
||||
$this->assertSessionMessages(
|
||||
$this->equalTo(['Incorrect CAPTCHA']),
|
||||
MessageInterface::TYPE_ERROR
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default_store customer/captcha/enable 1
|
||||
* @magentoConfigFixture default_store customer/captcha/failed_attempts_login 0
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
|
||||
* @magentoConfigFixture default_store customer/captcha/forms product_sendtofriend_form
|
||||
* @magentoConfigFixture default_store sendfriend/email/enabled 1
|
||||
*/
|
||||
public function testWithCaptchaSuccess()
|
||||
{
|
||||
/** @var DefaultModel $captchaModel */
|
||||
$captchaModel = $this->captchaHelper->getCaptcha('product_sendtofriend_form');
|
||||
$captchaModel->generate();
|
||||
$word = $captchaModel->getWord();
|
||||
$this->getRequest()
|
||||
->setMethod('POST')
|
||||
->setPostValue(
|
||||
[
|
||||
'form_key' => $this->formKey->getFormKey(),
|
||||
'sender' => [
|
||||
'name' => 'customer',
|
||||
'email' => 'customer@example.com',
|
||||
'message' => 'example message'
|
||||
],
|
||||
'id' => 1,
|
||||
'captcha' => [
|
||||
'product_sendtofriend_form' => $word
|
||||
],
|
||||
'recipients' => [
|
||||
'name' => ['John'],
|
||||
'email' => ['example1@gmail.com']
|
||||
]
|
||||
]
|
||||
);
|
||||
|
||||
$this->dispatch('sendfriend/product/sendmail');
|
||||
$this->assertSessionMessages(
|
||||
$this->equalTo(['The link to a friend was sent.']),
|
||||
MessageInterface::TYPE_SUCCESS
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\SendFriend\Controller;
|
||||
|
||||
use Magento\Customer\Model\Session;
|
||||
use Magento\SendFriend\Model\SendFriend;
|
||||
use Magento\TestFramework\Response;
|
||||
use Magento\TestFramework\TestCase\AbstractController;
|
||||
|
||||
/**
|
||||
* Class for send friend send action
|
||||
*
|
||||
* @magentoAppArea frontend
|
||||
*
|
||||
* @see \Magento\SendFriend\Controller\Product\Send
|
||||
*/
|
||||
class SendTest extends AbstractController
|
||||
{
|
||||
/** @var Session */
|
||||
private $customerSession;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->customerSession = $this->_objectManager->get(Session::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
|
||||
$this->customerSession->logout();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store sendfriend/email/enabled 0
|
||||
*
|
||||
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSendMailNotAllowed(): void
|
||||
{
|
||||
$this->dispatchWithProductIdParam(6);
|
||||
$this->assert404NotFound();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store sendfriend/email/enabled 1
|
||||
* @magentoConfigFixture current_store sendfriend/email/allow_guest 0
|
||||
*
|
||||
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGuestSendMailNotAllowed(): void
|
||||
{
|
||||
$this->dispatchWithProductIdParam(6);
|
||||
$this->assertRedirect($this->stringContains('customer/account/login'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store sendfriend/email/enabled 1
|
||||
* @magentoConfigFixture current_store sendfriend/email/allow_guest 1
|
||||
*
|
||||
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGuestSendMailAllowed(): void
|
||||
{
|
||||
$this->dispatchWithProductIdParam(6);
|
||||
$this->assertEquals(Response::STATUS_CODE_200, $this->getResponse()->getHttpResponseCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store sendfriend/email/enabled 1
|
||||
*
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testLoggedInCustomer(): void
|
||||
{
|
||||
$this->customerSession->loginById(1);
|
||||
$this->dispatchWithProductIdParam(6);
|
||||
$this->assertEquals(Response::STATUS_CODE_200, $this->getResponse()->getHttpResponseCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store sendfriend/email/enabled 1
|
||||
*
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testWithoutProductId(): void
|
||||
{
|
||||
$this->customerSession->loginById(1);
|
||||
$this->dispatch('sendfriend/product/send/');
|
||||
$this->assert404NotFound();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store sendfriend/email/enabled 1
|
||||
* @magentoConfigFixture current_store sendfriend/email/max_per_hour 1
|
||||
*
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testToMachSendRequests(): void
|
||||
{
|
||||
$this->createSendFriendMock();
|
||||
$this->customerSession->loginById(1);
|
||||
$this->dispatchWithProductIdParam(6);
|
||||
$this->assertSessionMessages(
|
||||
$this->equalTo([(string)__('You can\'t send messages more than 5 times an hour.')])
|
||||
);
|
||||
$this->assertEquals(Response::STATUS_CODE_200, $this->getResponse()->getHttpResponseCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set product id parameter and dispatch controller
|
||||
*
|
||||
* @param int $productId
|
||||
* @return void
|
||||
*/
|
||||
private function dispatchWithProductIdParam(int $productId): void
|
||||
{
|
||||
$this->getRequest()->setParam('id', $productId);
|
||||
$this->dispatch('sendfriend/product/send/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create mock to imitate to mach send requests
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function createSendFriendMock(): void
|
||||
{
|
||||
$mock = $this->createMock(SendFriend::class);
|
||||
$mock->method('isExceedLimit')->willReturn(true);
|
||||
$mock->method('getMaxSendsToFriend')->willReturn(5);
|
||||
$this->_objectManager->addSharedInstance($mock, SendFriend::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\SendFriend\Controller;
|
||||
|
||||
use Magento\Catalog\Api\Data\ProductInterface;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Customer\Model\Session;
|
||||
use Magento\Framework\Message\MessageInterface;
|
||||
use Magento\TestFramework\Helper\Xpath;
|
||||
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
|
||||
use Magento\TestFramework\Request;
|
||||
use Magento\TestFramework\TestCase\AbstractController;
|
||||
|
||||
/**
|
||||
* Class checks send mail action
|
||||
*
|
||||
* @see \Magento\SendFriend\Controller\Product\Sendmail
|
||||
*
|
||||
* @magentoAppArea frontend
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
class SendmailTest extends AbstractController
|
||||
{
|
||||
private const MESSAGE_PRODUCT_LINK_XPATH = "//a[contains(@href, '%s') and contains(text(), '%s')]";
|
||||
|
||||
/** @var Session */
|
||||
private $session;
|
||||
|
||||
/** @var ProductRepositoryInterface */
|
||||
private $productRepository;
|
||||
|
||||
/** @var TransportBuilderMock */
|
||||
private $transportBuilder;
|
||||
|
||||
/** @var array */
|
||||
private $staticData = [
|
||||
'sender' => [
|
||||
'name' => 'Test',
|
||||
'email' => 'test@example.com',
|
||||
'message' => 'Message',
|
||||
],
|
||||
'recipients' => [
|
||||
'name' => [
|
||||
'Recipient 1',
|
||||
'Recipient 2'
|
||||
],
|
||||
'email' => [
|
||||
'r1@example.com',
|
||||
'r2@example.com'
|
||||
]
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->session = $this->_objectManager->get(Session::class);
|
||||
$this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
|
||||
$this->transportBuilder = $this->_objectManager->get(TransportBuilderMock::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
|
||||
$this->session->logout();
|
||||
}
|
||||
|
||||
/**
|
||||
* Share the product to friend as logged in customer
|
||||
*
|
||||
* @magentoConfigFixture default_store sendfriend/email/allow_guest 0
|
||||
* @magentoConfigFixture default_store sendfriend/email/enabled 1
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/products.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSendActionAsLoggedIn(): void
|
||||
{
|
||||
$product = $this->productRepository->get('custom-design-simple-product');
|
||||
$this->session->loginById(1);
|
||||
$this->prepareRequestData();
|
||||
$this->dispatch('sendfriend/product/sendmail/id/' . $product->getId());
|
||||
$this->checkSuccess($product);
|
||||
}
|
||||
|
||||
/**
|
||||
* Share the product to friend as guest customer
|
||||
*
|
||||
* @magentoConfigFixture default_store sendfriend/email/enabled 1
|
||||
* @magentoConfigFixture default_store sendfriend/email/allow_guest 1
|
||||
* @magentoDataFixture Magento/Catalog/_files/products.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSendActionAsGuest(): void
|
||||
{
|
||||
$product = $this->productRepository->get('custom-design-simple-product');
|
||||
$this->prepareRequestData();
|
||||
$this->dispatch('sendfriend/product/sendmail/id/' . $product->getId());
|
||||
$this->checkSuccess($product);
|
||||
}
|
||||
|
||||
/**
|
||||
* Share the product to friend as guest customer with invalid post data
|
||||
*
|
||||
* @magentoConfigFixture default_store sendfriend/email/enabled 1
|
||||
* @magentoConfigFixture default_store sendfriend/email/allow_guest 1
|
||||
* @magentoDataFixture Magento/Catalog/_files/products.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSendActionAsGuestWithInvalidData(): void
|
||||
{
|
||||
$product = $this->productRepository->get('custom-design-simple-product');
|
||||
unset($this->staticData['sender']['email']);
|
||||
$this->prepareRequestData();
|
||||
$this->dispatch('sendfriend/product/sendmail/id/' . $product->getId());
|
||||
$this->assertSessionMessages(
|
||||
$this->equalTo([(string)__('Invalid Sender Email')]),
|
||||
MessageInterface::TYPE_ERROR
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Share the product invisible in catalog to friend as guest customer
|
||||
*
|
||||
* @magentoConfigFixture default_store sendfriend/email/enabled 1
|
||||
* @magentoConfigFixture default_store sendfriend/email/allow_guest 1
|
||||
* @magentoDataFixture Magento/Catalog/_files/simple_products_not_visible_individually.php
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSendInvisibleProduct(): void
|
||||
{
|
||||
$product = $this->productRepository->get('simple_not_visible_1');
|
||||
$this->prepareRequestData();
|
||||
$this->dispatch('sendfriend/product/sendmail/id/' . $product->getId());
|
||||
$this->assert404NotFound();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check success session message and email content
|
||||
*
|
||||
* @param ProductInterface $product
|
||||
* @return void
|
||||
*/
|
||||
private function checkSuccess(ProductInterface $product): void
|
||||
{
|
||||
$this->assertSessionMessages(
|
||||
$this->equalTo([(string)__('The link to a friend was sent.')]),
|
||||
MessageInterface::TYPE_SUCCESS
|
||||
);
|
||||
$message = $this->transportBuilder->getSentMessage();
|
||||
$this->assertNotNull($message, 'The message was not sent');
|
||||
$content = $message->getBody()->getParts()[0]->getRawContent();
|
||||
$this->assertEquals(
|
||||
1,
|
||||
Xpath::getElementsCountForXpath(
|
||||
sprintf(self::MESSAGE_PRODUCT_LINK_XPATH, $product->getUrlKey(), $product->getName()),
|
||||
$content
|
||||
),
|
||||
'Sent message does not contain product link'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare request before dispatch
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function prepareRequestData(): void
|
||||
{
|
||||
$this->getRequest()->setMethod(Request::METHOD_POST);
|
||||
$this->getRequest()->setPostValue($this->staticData);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user