ltsecurityinc安防产品在线商城代码

This commit is contained in:
2026-09-14 17:47:10 +08:00
parent 57d8444c15
commit 6a3c9c5bd8
14539 changed files with 1775310 additions and 0 deletions
@@ -0,0 +1,104 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\SendFriend\Block;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Block\Product\View;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\LayoutInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Class checks send friend link visibility
*
* @magentoAppArea frontend
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
*/
class ProductViewTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Registry */
private $registry;
/** @var LayoutInterface */
private $layout;
/** @var View */
private $block;
/** @var ProductRepositoryInterface */
private $productRepository;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->layout = $this->objectManager->get(LayoutInterface::class);
$this->block = $this->layout->createBlock(View::class);
$this->block->setTemplate('Magento_Catalog::product/view/mailto.phtml');
$this->registry = $this->objectManager->get(Registry::class);
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$this->productRepository->cleanCache();
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
parent::tearDown();
$this->registry->unregister('product');
}
/**
* @return void
*/
public function testSendFriendLinkDisabled(): void
{
$this->registerProduct('simple2');
$this->assertEmpty($this->block->toHtml());
}
/**
* @magentoConfigFixture current_store sendfriend/email/enabled 1
*
* @return void
*/
public function testSendFriendLinkEnabled(): void
{
$product = $this->registerProduct('simple2');
$html = $this->block->toHtml();
$this->assertStringContainsString('sendfriend/product/send/id/' . $product->getId(), $html);
$this->assertEquals('Email', trim(strip_tags($html)));
}
/**
* Register product by sku
*
* @param string $sku
* @return ProductInterface
*/
private function registerProduct(string $sku): ProductInterface
{
$product = $this->productRepository->get($sku);
$this->registry->unregister('product');
$this->registry->register('product', $product);
return $product;
}
}
@@ -0,0 +1,165 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\SendFriend\Block;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Model\Session;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use PHPUnit\Framework\TestCase;
/**
* Class checks send friend email block
*
* @see \Magento\SendFriend\Block\Send
*
* @magentoAppArea frontend
*/
class SendTest extends TestCase
{
/** @var array */
private $elementsXpath = [
'sender name field' => "//input[@name='sender[name]']",
'sender email field' => "//input[@name='sender[email]']",
'sender message field' => "//textarea[@name='sender[message]']",
'recipient name field' => "//input[contains(@name, 'recipients[name]')]",
'recipient email field' => "//input[contains(@name, 'recipients[email]')]",
'submit button' => "//button[@type='submit']/span[contains(text(), 'Send Email')]",
'notice massage' => "//div[@id='max-recipient-message']"
. "/span[contains(text(), 'Maximum 1 email addresses allowed.')]"
];
/** @var ObjectManagerInterface */
private $objectManager;
/** @var LayoutInterface */
private $layout;
/** @var Send */
private $block;
/** @var Session */
private $session;
/** @var AccountManagementInterface */
private $accountManagement;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->layout = $this->objectManager->get(LayoutInterface::class);
$this->block = $this->layout->createBlock(Send::class);
$this->session = $this->objectManager->get(Session::class);
$this->accountManagement = $this->objectManager->get(AccountManagementInterface::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
parent::tearDown();
$this->session->logout();
}
/**
* @dataProvider formDataProvider
*
* @param string $field
* @param string $value
* @return void
*/
public function testGetCustomerFieldFromFormData(string $field, string $value): void
{
$formData = ['sender' => [$field => $value]];
$this->block->setFormData($formData);
$this->assertEquals(trim($value), $this->_callBlockMethod($field));
}
/**
* @return array
*/
public function formDataProvider(): array
{
return [
['name', 'Customer Form Name'],
['email', 'customer_form_email@example.com']
];
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
*
* @dataProvider customerSessionDataProvider
*
* @magentoAppIsolation enabled
*
* @param string $field
* @param string $value
* @return void
*/
public function testGetCustomerFieldFromSession(string $field, string $value): void
{
$customer = $this->accountManagement->authenticate('customer@example.com', 'password');
$this->session->setCustomerDataAsLoggedIn($customer);
$this->assertEquals($value, $this->_callBlockMethod($field));
}
/**
* @return array
*/
public function customerSessionDataProvider(): array
{
return [
['name', 'John Smith'],
['email', 'customer@example.com']
];
}
/**
* @magentoConfigFixture current_store sendfriend/email/max_recipients 1
*
* @return void
*/
public function testBlockAppearance(): void
{
$this->block->setTemplate('Magento_SendFriend::send.phtml');
$html = preg_replace('#<script(.*?)>#i', '', $this->block->toHtml());
foreach ($this->elementsXpath as $key => $xpath) {
$this->assertEquals(
1,
Xpath::getElementsCountForXpath($xpath, $html),
sprintf('The %s field is not found on the page', $key)
);
}
}
/**
* Call block method based on form field
*
* @param string $field
* @return null|string
*/
protected function _callBlockMethod(string $field): ?string
{
switch ($field) {
case 'name':
return $this->block->getUserName();
case 'email':
return $this->block->getEmail();
default:
return null;
}
}
}
@@ -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);
}
}
@@ -0,0 +1,26 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Config\Model\Config;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
$objectManager = Bootstrap::getObjectManager();
$configData = [
'sendfriend/email/max_per_hour' => 1,
'sendfriend/email/check_by' => 1,
];
$objectManager = Bootstrap::getObjectManager();
$defConfig = $objectManager->create(Config::class);
$defConfig->setScope(ScopeConfigInterface::SCOPE_TYPE_DEFAULT);
foreach ($configData as $key => $value) {
$defConfig->setDataByPath($key, $value);
$defConfig->save();
}
@@ -0,0 +1,23 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
$objectManager = Bootstrap::getObjectManager();
$configData = [
'sendfriend/email/max_per_hour',
'sendfriend/email/check_by'
];
/** @var WriterInterface $configWriter */
$configWriter = $objectManager->get(WriterInterface::class);
foreach ($configData as $path) {
$configWriter->delete($path, ScopeConfigInterface::SCOPE_TYPE_DEFAULT, 0);
}
@@ -0,0 +1,245 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\SendFriend\Model;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Stdlib\CookieManagerInterface;
use Magento\SendFriend\Helper\Data as SendFriendHelper;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
use Laminas\Stdlib\Parameters;
/**
* Class checks send friend model behavior
*
* @see \Magento\SendFriend\Model\SendFriend
*/
class SendFriendTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var SendFriend */
private $sendFriend;
/** @var ResourceModel\SendFriend */
private $sendFriendResource;
/** @var CookieManagerInterface */
private $cookieManager;
/** @var RequestInterface */
private $request;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->sendFriend = $this->objectManager->get(SendFriendFactory::class)->create();
$this->sendFriendResource = $this->objectManager->get(ResourceModel\SendFriend::class);
$this->cookieManager = $this->objectManager->get(CookieManagerInterface::class);
$this->request = $this->objectManager->get(RequestInterface::class);
}
/**
* @magentoConfigFixture current_store sendfriend/email/max_recipients 1
*
* @dataProvider validateDataProvider
*
* @param array $sender
* @param array $recipients
* @param string|bool $expectedResult
*
* @return void
*/
public function testValidate(array $sender, array $recipients, $expectedResult): void
{
$this->prepareData($sender, $recipients);
$this->checkResult($expectedResult, $this->sendFriend->validate());
}
/**
* @return array
*/
public function validateDataProvider(): array
{
return [
'valid_data' => [
'sender' => [
'name' => 'Sender Name',
'email' => 'm1111ytest@mail.com',
'message' => 'test message',
],
'recipients' => [
'name' => [
'recipient_name',
],
'email' => [
'recipient_email@example.com',
],
],
'expected_result' => true,
],
'empty_message' => [
'sender' => [
'name' => 'Sender Name',
'email' => 'm1111ytest@mail.com',
'message' => '',
],
'recipients' => [
'name' => [
'recipient name',
],
'email' => [
'recipient_email@example.com',
],
],
'expected_result' => 'Please enter a message.',
],
'empty_sender_name' => [
'sender' => [
'name' => '',
'email' => 'customer_email@example.com',
'message' => 'test message',
],
'recipients' => [
'name' => [
'recipient name',
],
'email' => [
'recipient_email@example.com',
],
],
'expected_result' => 'Please enter a sender name.',
],
'empty_recipients' => [
'sender' => [
'name' => 'Sender Name',
'email' => 'm1111ytest@mail.com',
'message' => 'test message',
],
'recipients' => [
'name' => [],
'email' => [],
],
'expected_result' => 'Please specify at least one recipient.',
],
'wrong_recipient_email' => [
'sender' => [
'name' => 'Sender Name',
'email' => 'm1111ytest@mail.com',
'message' => 'test message',
],
'recipients' => [
'name' => [
'recipient name',
],
'email' => [
'123123',
],
],
'expected_result' => 'Please enter a correct recipient email address.',
],
'to_much_recipients' => [
'sender' => [
'name' => 'Sender Name',
'email' => 'm1111ytest@mail.com',
'message' => 'test message',
],
'recipients' => [
'name' => [
'recipient name',
'second name',
],
'email' => [
'recipient_email@example.com',
'recipient2_email@example.com',
],
],
'expected_result' => 'No more than 1 emails can be sent at a time.',
],
];
}
/**
* @magentoConfigFixture current_store sendfriend/email/check_by 0
* @magentoConfigFixture current_store sendfriend/email/max_per_hour 1
*
* @return void
*/
public function testisExceedLimitByCookies(): void
{
$this->cookieManager->setPublicCookie(SendFriendHelper::COOKIE_NAME, (string)time());
$this->assertTrue($this->sendFriend->isExceedLimit());
}
/**
* @magentoConfigFixture current_store sendfriend/email/check_by 1
* @magentoConfigFixture current_store sendfriend/email/max_per_hour 1
*
* @magentoDataFixture Magento/SendFriend/_files/sendfriend_log_record_half_hour_before.php
*
* @magentoDbIsolation disabled
*
* @return void
*/
public function testisExceedLimitByIp(): void
{
$remoteAddr = '127.0.0.1';
$parameters = $this->objectManager->create(Parameters::class);
$parameters->set('REMOTE_ADDR', $remoteAddr);
$this->request->setServer($parameters);
$this->assertTrue($this->sendFriend->isExceedLimit());
// Verify that ip is saved correctly as integer value
$this->assertEquals(
1,
(int)$this->sendFriendResource->getSendCount(
null,
ip2long($remoteAddr),
time() - (60 * 60 * 24 * 365),
1
)
);
}
/**
* Check test result
*
* @param array|bool $expectedResult
* @param array|bool $result
*
* @return void
*/
private function checkResult($expectedResult, $result): void
{
if ($expectedResult === true) {
$this->assertTrue($result);
} else {
$this->assertEquals($expectedResult, (string)reset($result) ?? '');
}
}
/**
* Prepare sender and recipient data
*
* @param array $sender
* @param array $recipients
*
* @return void
*/
private function prepareData(array $sender, array $recipients): void
{
$this->sendFriend->setSender($sender);
$this->sendFriend->setRecipients($recipients);
}
}
@@ -0,0 +1,20 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\SendFriend\Model\ResourceModel\SendFriend as SendFriendResource;
use Magento\Store\Api\WebsiteRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;
$objectManager = Bootstrap::getObjectManager();
/** @var WebsiteRepositoryInterface $websiteRepository */
$websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class);
$baseWebsiteId = $websiteRepository->get('base')->getId();
$ip = ip2long('127.0.0.1');
$updateDatetime = new \DateTime('-0.5 hours');
/** @var SendFriendResource $sendFriendResource */
$sendFriendResource = $objectManager->get(SendFriendResource::class);
$sendFriendResource->addSendItem($ip, $updateDatetime->getTimestamp(), $baseWebsiteId);
@@ -0,0 +1,13 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\SendFriend\Model\DeleteLogRowsByIp;
/** @var DeleteLogRowsByIp $deleteLogRowsByIp */
$deleteLogRowsByIp = Bootstrap::getObjectManager()->get(DeleteLogRowsByIp::class);
$deleteLogRowsByIp->execute('127.0.0.1');