ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\ProductAlert\Model;
|
||||
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Customer\Api\AccountManagementInterface;
|
||||
use Magento\Customer\Api\CustomerRepositoryInterface;
|
||||
use Magento\Customer\Helper\View;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\MailException;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\Store\Model\Website;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test for Magento\ProductAlert\Model\Email class.
|
||||
*
|
||||
* @magentoAppIsolation enabled
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class EmailTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Email
|
||||
*/
|
||||
protected $_emailModel;
|
||||
|
||||
/**
|
||||
* @var ObjectManager
|
||||
*/
|
||||
protected $_objectManager;
|
||||
|
||||
/**
|
||||
* @var AccountManagementInterface
|
||||
*/
|
||||
protected $customerAccountManagement;
|
||||
|
||||
/**
|
||||
* @var View
|
||||
*/
|
||||
protected $_customerViewHelper;
|
||||
|
||||
/**
|
||||
* @var TransportBuilderMock
|
||||
*/
|
||||
private $transportBuilder;
|
||||
|
||||
/**
|
||||
* @var ProductRepositoryInterface
|
||||
*/
|
||||
private $productRepository;
|
||||
|
||||
/**
|
||||
* @var CustomerRepositoryInterface
|
||||
*/
|
||||
private $customerRepository;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->_objectManager = Bootstrap::getObjectManager();
|
||||
$this->customerAccountManagement = $this->_objectManager->create(
|
||||
AccountManagementInterface::class
|
||||
);
|
||||
$this->_customerViewHelper = $this->_objectManager->create(View::class);
|
||||
$this->transportBuilder = $this->_objectManager->get(TransportBuilderMock::class);
|
||||
$this->customerRepository = $this->_objectManager->create(CustomerRepositoryInterface::class);
|
||||
$this->productRepository = $this->_objectManager->create(ProductRepositoryInterface::class);
|
||||
|
||||
$this->_emailModel = $this->_objectManager->create(Email::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppArea frontend
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
|
||||
* @dataProvider customerFunctionDataProvider
|
||||
*
|
||||
* @param bool isCustomerIdUsed
|
||||
* @throws LocalizedException
|
||||
* @throws MailException
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
public function testSend($isCustomerIdUsed)
|
||||
{
|
||||
/** @var Website $website */
|
||||
$website = $this->_objectManager->create(Website::class);
|
||||
$website->load(1);
|
||||
$this->_emailModel->setWebsite($website);
|
||||
|
||||
$customer = $this->customerRepository->getById(1);
|
||||
|
||||
if ($isCustomerIdUsed) {
|
||||
$this->_emailModel->setCustomerId(1);
|
||||
} else {
|
||||
$this->_emailModel->setCustomerData($customer);
|
||||
}
|
||||
|
||||
/** @var Product $product */
|
||||
$product = $this->productRepository->getById(1);
|
||||
|
||||
$this->_emailModel->addPriceProduct($product);
|
||||
$this->_emailModel->send();
|
||||
|
||||
$this->assertStringContainsString(
|
||||
'John Smith,',
|
||||
$this->transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent()
|
||||
);
|
||||
}
|
||||
|
||||
public function customerFunctionDataProvider()
|
||||
{
|
||||
return [
|
||||
[true],
|
||||
[false],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that product price shown correct in email for customers with different customer groups.
|
||||
*
|
||||
* @magentoAppArea frontend
|
||||
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_wholesale_tier_price.php
|
||||
* @magentoDataFixture Magento/Customer/_files/two_customers_with_different_customer_groups.php
|
||||
*
|
||||
* @return void
|
||||
* @throws LocalizedException
|
||||
* @throws MailException
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
public function testEmailForDifferentCustomers(): void
|
||||
{
|
||||
$customerGeneral = $this->customerRepository->get('customer@example.com');
|
||||
$customerWholesale = $this->customerRepository->get('customer_two@example.com');
|
||||
$product = $this->productRepository->get('simple');
|
||||
|
||||
/** @var Website $website */
|
||||
$website = $this->_objectManager->create(Website::class);
|
||||
$website->load(1);
|
||||
|
||||
$data = [
|
||||
$customerGeneral->getId() => '10',
|
||||
$customerWholesale->getId() => '5',
|
||||
];
|
||||
|
||||
foreach ($data as $customerId => $expectedPrice) {
|
||||
$this->_emailModel->clean();
|
||||
$this->_emailModel->setCustomerId($customerId);
|
||||
$this->_emailModel->setWebsite($website);
|
||||
$this->_emailModel->addStockProduct($product);
|
||||
$this->_emailModel->setType('stock');
|
||||
$this->_emailModel->send();
|
||||
|
||||
$expectedPriceBox = '<span id="product-price-' . $product->getId() . '" data-price-amount="'
|
||||
. $expectedPrice . '" data-price-type="finalPrice" '
|
||||
. 'class="price-wrapper "><span class="price">$' . $expectedPrice . '.00</span></span>';
|
||||
|
||||
$this->assertStringContainsString(
|
||||
$expectedPriceBox,
|
||||
$this->transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppArea frontend
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
|
||||
* @magentoDataFixture Magento/Store/_files/second_store_with_second_identity.php
|
||||
*/
|
||||
public function testScopedMessageIdentity()
|
||||
{
|
||||
/** @var Website $website */
|
||||
$website = $this->_objectManager->create(Website::class);
|
||||
$website->load(1);
|
||||
$this->_emailModel->setWebsite($website);
|
||||
|
||||
/** @var StoreManagerInterface $storeManager */
|
||||
$storeManager = $this->_objectManager->create(StoreManagerInterface::class);
|
||||
$store = $storeManager->getStore('fixture_second_store');
|
||||
$this->_emailModel->setStoreId($store->getId());
|
||||
|
||||
$customer = $this->customerRepository->getById(1);
|
||||
$this->_emailModel->setCustomerData($customer);
|
||||
|
||||
/** @var Product $product */
|
||||
$product = $this->productRepository->getById(1);
|
||||
|
||||
$this->_emailModel->addPriceProduct($product);
|
||||
$this->_emailModel->send();
|
||||
|
||||
$from = $this->transportBuilder->getSentMessage()->getFrom()[0];
|
||||
$this->assertEquals('Fixture Store Owner', $from->getName());
|
||||
$this->assertEquals('fixture.store.owner@example.com', $from->getEmail());
|
||||
}
|
||||
}
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\ProductAlert\Model\Mailing;
|
||||
|
||||
use Magento\Customer\Api\AccountManagementInterface;
|
||||
use Magento\Customer\Model\Session;
|
||||
use Magento\Framework\App\Area;
|
||||
use Magento\Framework\Locale\Resolver;
|
||||
use Magento\Framework\Module\Dir\Reader;
|
||||
use Magento\Framework\Phrase;
|
||||
use Magento\Framework\Phrase\Renderer\Translate as PhraseRendererTranslate;
|
||||
use Magento\Framework\Phrase\RendererInterface;
|
||||
use Magento\Framework\Translate;
|
||||
use Magento\Store\Model\StoreRepository;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test for Product Alert observer
|
||||
*
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoAppArea frontend
|
||||
*/
|
||||
class AlertProcessorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var ObjectManager
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var Publisher
|
||||
*/
|
||||
private $publisher;
|
||||
|
||||
/**
|
||||
* @var AlertProcessor
|
||||
*/
|
||||
private $alertProcessor;
|
||||
|
||||
/**
|
||||
* @var TransportBuilderMock
|
||||
*/
|
||||
private $transportBuilder;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->publisher = $this->objectManager->get(Publisher::class);
|
||||
$this->alertProcessor = $this->objectManager->get(AlertProcessor::class);
|
||||
|
||||
$this->transportBuilder = $this->objectManager->get(TransportBuilderMock::class);
|
||||
$service = $this->objectManager->create(AccountManagementInterface::class);
|
||||
$customer = $service->authenticate('customer@example.com', 'password');
|
||||
$customerSession = $this->objectManager->get(Session::class);
|
||||
$customerSession->setCustomerDataAsLoggedIn($customer);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store catalog/productalert/allow_price 1
|
||||
* @magentoDataFixture Magento/ProductAlert/_files/product_alert.php
|
||||
*/
|
||||
public function testProcess()
|
||||
{
|
||||
$this->processAlerts();
|
||||
|
||||
/** Checking is the email was sent */
|
||||
$this->assertStringContainsString(
|
||||
'John Smith,',
|
||||
$this->transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check translations for product alerts
|
||||
*
|
||||
* @magentoDbIsolation disabled
|
||||
* @magentoDataFixture Magento/Catalog/_files/category.php
|
||||
* @magentoConfigFixture current_store catalog/productalert/allow_price 1
|
||||
* @magentoDataFixture Magento/Store/_files/second_store.php
|
||||
* @magentoConfigFixture fixture_second_store_store general/locale/code pt_BR
|
||||
* @magentoDataFixture Magento/ProductAlert/_files/product_alert_with_store.php
|
||||
*/
|
||||
public function testProcessPortuguese()
|
||||
{
|
||||
// get second store
|
||||
$storeRepository = $this->objectManager->create(StoreRepository::class);
|
||||
$secondStore = $storeRepository->get('fixture_second_store');
|
||||
|
||||
// check if Portuguese language is specified for the second store
|
||||
$storeResolver = $this->objectManager->get(Resolver::class);
|
||||
$storeResolver->emulate($secondStore->getId());
|
||||
$this->assertEquals('pt_BR', $storeResolver->getLocale());
|
||||
|
||||
// set translation data and check it
|
||||
$modulesReader = $this->createPartialMock(Reader::class, ['getModuleDir']);
|
||||
$modulesReader->method('getModuleDir')
|
||||
->willReturn(dirname(__DIR__) . '/../_files/i18n');
|
||||
/** @var Translate $translator */
|
||||
$translator = $this->objectManager->create(Translate::class, ['modulesReader' => $modulesReader]);
|
||||
$translation = [
|
||||
'Price change alert! We wanted you to know that prices have changed for these products:' =>
|
||||
'Alerta de mudanca de preco! Queriamos que voce soubesse que os precos mudaram para esses produtos:'
|
||||
];
|
||||
$translator->loadData();
|
||||
$this->assertEquals($translation, $translator->getData());
|
||||
$this->objectManager->addSharedInstance($translator, Translate::class);
|
||||
$this->objectManager->removeSharedInstance(PhraseRendererTranslate::class);
|
||||
Phrase::setRenderer($this->objectManager->create(RendererInterface::class));
|
||||
|
||||
// dispatch process() method and check sent message
|
||||
$this->processAlerts();
|
||||
$message = $this->transportBuilder->getSentMessage();
|
||||
$messageContent = $message->getBody()->getParts()[0]->getRawContent();
|
||||
$expectedText = array_shift($translation);
|
||||
$this->assertStringContainsString('/frontend/Magento/luma/pt_BR/', $messageContent);
|
||||
$this->assertStringContainsString(substr($expectedText, 0, 50), $messageContent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process price alerts
|
||||
*/
|
||||
private function processAlerts(): void
|
||||
{
|
||||
$alertType = AlertProcessor::ALERT_TYPE_PRICE;
|
||||
$customerId = 1;
|
||||
$websiteId = 1;
|
||||
|
||||
$this->publisher->execute($alertType, [$customerId], $websiteId);
|
||||
$this->alertProcessor->process($alertType, [$customerId], $websiteId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\ProductAlert\Model;
|
||||
|
||||
use Magento\Framework\DB\Select;
|
||||
use Magento\ProductAlert\Model\ResourceModel\Price as PriceResource;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\MessageQueue\EnvironmentPreconditionException;
|
||||
use Magento\TestFramework\MessageQueue\PreconditionFailedException;
|
||||
use Magento\TestFramework\MessageQueue\PublisherConsumerController;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test Product Alert observer
|
||||
*
|
||||
* @magentoDbIsolation disabled
|
||||
*/
|
||||
class ObserverTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Observer
|
||||
*/
|
||||
private $observer;
|
||||
|
||||
/**
|
||||
* @var PriceResource
|
||||
*/
|
||||
private $priceResource;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$this->observer = $objectManager->get(Observer::class);
|
||||
$this->priceResource = $objectManager->create(PriceResource::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test process() method
|
||||
*
|
||||
* @magentoConfigFixture current_store catalog/productalert/allow_price 1
|
||||
* @magentoDataFixture Magento/ProductAlert/_files/product_alert.php
|
||||
*/
|
||||
public function testProcess()
|
||||
{
|
||||
$this->observer->process();
|
||||
$this->assertProcessAlertByConsumer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Waiting for execute consumer
|
||||
*
|
||||
* @return void
|
||||
* @throws PreconditionFailedException
|
||||
*/
|
||||
private function assertProcessAlertByConsumer(): void
|
||||
{
|
||||
/** @var PublisherConsumerController $publisherConsumerController */
|
||||
$publisherConsumerController = Bootstrap::getObjectManager()->create(
|
||||
PublisherConsumerController::class,
|
||||
[
|
||||
'consumers' => ['product_alert'],
|
||||
'logFilePath' => TESTS_TEMP_DIR . "/MessageQueueTestLog.txt",
|
||||
'maxMessages' => 1,
|
||||
'appInitParams' => Bootstrap::getInstance()->getAppInitParams()
|
||||
]
|
||||
);
|
||||
try {
|
||||
$publisherConsumerController->startConsumers();
|
||||
} catch (EnvironmentPreconditionException $e) {
|
||||
$this->markTestSkipped($e->getMessage());
|
||||
} catch (PreconditionFailedException $e) {
|
||||
$this->fail(
|
||||
$e->getMessage()
|
||||
);
|
||||
}
|
||||
|
||||
sleep(15); // timeout to processing Magento queue
|
||||
|
||||
$publisherConsumerController->waitForAsynchronousResult(
|
||||
function () {
|
||||
return $this->loadLastPriceAlertStatus();
|
||||
},
|
||||
[]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load last created price alert
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function loadLastPriceAlertStatus(): bool
|
||||
{
|
||||
$select = $this->priceResource->getConnection()->select();
|
||||
$select->from($this->priceResource->getMainTable(), ['status'])
|
||||
->order($this->priceResource->getIdFieldName() . ' ' . Select::SQL_DESC)
|
||||
->limit(1);
|
||||
|
||||
return (bool)$this->priceResource->getConnection()->fetchOne($select);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user