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,142 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\AsynchronousOperations\Model;
use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Framework\ObjectManagerInterface;
use Magento\AsynchronousOperations\Api\Data\OperationInterface;
use Magento\Framework\MessageQueue\BulkPublisherInterface;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\EntityManager\EntityManager;
use Magento\AsynchronousOperations\Model\ResourceModel\Operation\CollectionFactory;
use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterfaceFactory;
use Magento\AsynchronousOperations\Api\Data\OperationInterfaceFactory;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class BulkManagementTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \PHPUnit\Framework\MockObject_MockObject
*/
private $publisherMock;
/**
* @var BulkManagement
*/
private $model;
/**
* @var ObjectManagerInterface
*/
private $objectManager;
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->publisherMock = $this->getMockForAbstractClass(BulkPublisherInterface::class);
$this->model = $this->objectManager->create(
BulkManagement::class,
[
'publisher' => $this->publisherMock
]
);
}
public function testScheduleBulk()
{
// general bulk information
$bulkUuid = '5a12c1bd-a8b5-41d4-8c00-3f5bcaa6d3c7';
$bulkDescription = 'Bulk General Information';
$topicName = 'example.topic.name';
$userId = 1;
// generate bulk operations that must be saved
$operationCount = 100;
$operations = [];
$operationFactory = $this->objectManager->get(OperationInterfaceFactory::class);
for ($index = 0; $index < $operationCount; $index++) {
/** @var OperationInterface $operation */
$operation = $operationFactory->create();
$operation->setBulkUuid($bulkUuid);
$operation->setTopicName($topicName);
$operation->setSerializedData(json_encode(['entity_id' => $index]));
$operations[] = $operation;
}
$this->publisherMock->expects($this->once())
->method('publish')
->with($topicName, $operations);
// schedule bulk
$this->assertTrue($this->model->scheduleBulk($bulkUuid, $operations, $bulkDescription, $userId));
$storedData = $this->getStoredOperationData();
// No operations should be saved to database during bulk creation
$this->assertCount(0, $storedData);
}
/**
* @magentoDataFixture Magento/AsynchronousOperations/_files/bulk.php
*/
public function testRetryBulk()
{
$bulkUuid = 'bulk-uuid-5';
$topicName = 'topic-4';
$errorCodes = [1111, 2222];
$operations = $this->objectManager->get(CollectionFactory::class)
->create()
->addFieldToFilter('bulk_uuid', ['eq' => $bulkUuid])
->getItems();
$this->publisherMock->expects($this->once())
->method('publish')
->with($topicName, array_values($operations));
$this->assertEquals(2, $this->model->retryBulk($bulkUuid, $errorCodes));
$operations = $this->objectManager->get(CollectionFactory::class)
->create()
->addFieldToFilter('bulk_uuid', ['eq' => $bulkUuid])
->getItems();
// Failed operations should be removed from database during bulk retry
$this->assertCount(0, $operations);
}
/**
* @magentoDataFixture Magento/AsynchronousOperations/_files/bulk.php
*/
public function testDeleteBulk()
{
$this->model->deleteBulk('bulk-uuid-1');
/** @var EntityManager $entityManager */
$entityManager = $this->objectManager->get(EntityManager::class);
$bulkSummaryFactory = $this->objectManager->get(BulkSummaryInterfaceFactory::class);
/** @var BulkSummaryInterface $bulkSummary */
$bulkSummary = $entityManager->load($bulkSummaryFactory->create(), 'bulk-uuid-1');
$this->assertNull($bulkSummary->getBulkId());
}
/**
* Retrieve stored operation data
*
* @return array
* @throws \Exception
*/
private function getStoredOperationData()
{
/** @var MetadataPool $metadataPool */
$metadataPool = $this->objectManager->get(MetadataPool::class);
$operationMetadata = $metadataPool->getMetadata(OperationInterface::class);
/** @var ResourceConnection $resourceConnection */
$resourceConnection = $this->objectManager->get(ResourceConnection::class);
$connection = $resourceConnection->getConnectionByName($operationMetadata->getEntityConnectionName());
return $connection->fetchAll($connection->select()->from($operationMetadata->getEntityTable()));
}
}
@@ -0,0 +1,81 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\AsynchronousOperations\Model;
use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Framework\ObjectManagerInterface;
class BulkNotificationManagementTest extends \PHPUnit\Framework\TestCase
{
/**
* @var BulkNotificationManagement
*/
private $model;
/**
* @var ObjectManagerInterface
*/
private $objectManager;
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->model = $this->objectManager->create(
BulkNotificationManagement::class
);
}
/**
* @magentoDataFixture Magento/AsynchronousOperations/_files/bulk.php
*/
public function testAcknowledgeBulks()
{
$this->model->acknowledgeBulks(['bulk-uuid-5']);
$acknowledgedBulks = $this->model->getAcknowledgedBulksByUser(1);
$this->assertCount(1, $acknowledgedBulks);
/** @var BulkSummaryInterface $acknowledgedBulk */
$acknowledgedBulk = array_pop($acknowledgedBulks);
$this->assertEquals('bulk-uuid-5', $acknowledgedBulk->getBulkId());
}
/**
* @magentoDataFixture Magento/AsynchronousOperations/_files/acknowledged_bulk.php
*/
public function testIgnoreBulks()
{
// 'bulk-uuid-5' and 'bulk-uuid-4' are marked as acknowledged in fixture
$this->model->ignoreBulks(['bulk-uuid-5']);
$acknowledgedBulks = $this->model->getAcknowledgedBulksByUser(1);
$this->assertCount(1, $acknowledgedBulks);
/** @var BulkSummaryInterface $acknowledgedBulk */
$acknowledgedBulk = array_pop($acknowledgedBulks);
$this->assertEquals('bulk-uuid-4', $acknowledgedBulk->getBulkId());
}
/**
* @magentoDataFixture Magento/AsynchronousOperations/_files/acknowledged_bulk.php
*/
public function testGetAcknowledgedBulks()
{
// 'bulk-uuid-5' and 'bulk-uuid-4' are marked as acknowledged in fixture
$acknowledgedBulks = $this->model->getAcknowledgedBulksByUser(1);
$this->assertCount(2, $acknowledgedBulks);
}
/**
* @magentoDataFixture Magento/AsynchronousOperations/_files/acknowledged_bulk.php
*/
public function testGetIgnoredBulks()
{
// 'bulk-uuid-5' and 'bulk-uuid-4' are marked as acknowledged in fixture. Fixture creates 5 bulks in total.
$ignoredBulks = $this->model->getIgnoredBulksByUser(1);
$this->assertCount(3, $ignoredBulks);
}
}
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\AsynchronousOperations\Model;
use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface;
class BulkStatusTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\AsynchronousOperations\Model\BulkStatus
*/
private $model;
protected function setUp(): void
{
$this->model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\AsynchronousOperations\Model\BulkStatus::class
);
}
/**
* @magentoDataFixture Magento/AsynchronousOperations/_files/bulk.php
*/
public function testGetBulkStatus()
{
$this->assertEquals(BulkSummaryInterface::NOT_STARTED, $this->model->getBulkStatus('bulk-uuid-1'));
$this->assertEquals(BulkSummaryInterface::IN_PROGRESS, $this->model->getBulkStatus('bulk-uuid-2'));
$this->assertEquals(BulkSummaryInterface::FINISHED_SUCCESSFULLY, $this->model->getBulkStatus('bulk-uuid-4'));
$this->assertEquals(BulkSummaryInterface::FINISHED_WITH_FAILURE, $this->model->getBulkStatus('bulk-uuid-5'));
}
/**
* @magentoDataFixture Magento/AsynchronousOperations/_files/bulk.php
*/
public function testGetBulksByUser()
{
/** @var \Magento\AsynchronousOperations\Model\BulkSummary[] $bulks */
$bulksUuidArray = ['bulk-uuid-1', 'bulk-uuid-2', 'bulk-uuid-3', 'bulk-uuid-4', 'bulk-uuid-5'];
$bulks = $this->model->getBulksByUser(1);
$this->assertCount(5, $bulks);
foreach ($bulks as $bulk) {
$this->assertTrue(in_array($bulk->getBulkId(), $bulksUuidArray));
}
}
/**
* @magentoDataFixture Magento/AsynchronousOperations/_files/bulk.php
*/
public function testGetFailedOperationsByBulkId()
{
/** @var \Magento\AsynchronousOperations\Api\Data\OperationInterface[] $operations */
$operations = $this->model->getFailedOperationsByBulkId('bulk-uuid-1');
$this->assertEquals([], $operations);
$operations = $this->model->getFailedOperationsByBulkId('bulk-uuid-5', 3);
foreach ($operations as $operation) {
$this->assertEquals(1111, $operation->getErrorCode());
}
$operations = $this->model->getFailedOperationsByBulkId('bulk-uuid-5', 2);
foreach ($operations as $operation) {
$this->assertEquals(2222, $operation->getErrorCode());
}
}
}
@@ -0,0 +1,154 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\AsynchronousOperations\Model;
use Magento\AsynchronousOperations\Api\Data\OperationInterface;
use Magento\AsynchronousOperations\Api\SaveMultipleOperationsInterface;
use Magento\AsynchronousOperations\Model\ConfigInterface as AsyncConfig;
use Magento\AsynchronousOperations\Model\ResourceModel\Operation\Collection;
use Magento\AsynchronousOperations\Model\ResourceModel\Operation\CollectionFactory;
use Magento\Catalog\Model\Product;
use Magento\Framework\Bulk\BulkManagementInterface;
use Magento\Framework\MessageQueue\ConsumerConfigurationInterface;
use Magento\Framework\MessageQueue\EnvelopeInterface;
use Magento\Framework\MessageQueue\Lock\ReaderInterface;
use Magento\Framework\MessageQueue\LockInterfaceFactory;
use Magento\Framework\MessageQueue\MessageEncoder;
use Magento\Framework\MessageQueue\QueueInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Monolog\Test\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
/**
* Test for MassConsumerEnvelopeCallback
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class MassConsumerEnvelopeCallbackTest extends TestCase
{
/**
* @var MassConsumerEnvelopeCallback
*/
private $model;
/**
* @var QueueInterface|MockObject
*/
private $queue;
/**
* @var ConsumerConfigurationInterface|MockObject
*/
private $configuration;
/**
* @var MessageEncoder
*/
private $messageEncoder;
/**
* @var OperationRepositoryInterface
*/
private $operationRepository;
/**
* @var BulkManagementInterface
*/
private $bulkManagement;
/**
* @var SaveMultipleOperationsInterface
*/
private $saveMultipleOperations;
/**
* @var CollectionFactory
*/
private $operationCollectionFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$objectManager = Bootstrap::getObjectManager();
$this->queue = $this->createMock(QueueInterface::class);
$this->configuration = $this->createMock(ConsumerConfigurationInterface::class);
$this->messageEncoder = $objectManager->get(MessageEncoder::class);
$this->operationRepository = $objectManager->get(OperationRepositoryInterface::class);
$operationProcessor = $this->createMock(OperationProcessor::class);
$this->operationCollectionFactory = $objectManager->get(CollectionFactory::class);
$this->bulkManagement = $objectManager->get(BulkManagementInterface::class);
$this->saveMultipleOperations = $objectManager->get(SaveMultipleOperationsInterface::class);
$operationProcessorFactory = $this->createMock(OperationProcessorFactory::class);
$operationProcessorFactory->method('create')
->willReturn($operationProcessor);
$this->model = Bootstrap::getObjectManager()->create(
MassConsumerEnvelopeCallback::class,
[
'queue' => $this->queue,
'configuration' => $this->configuration,
'operationProcessorFactory' => $operationProcessorFactory
]
);
}
/**
* @magentoDbIsolation enabled
*/
public function testMessageLock(): void
{
$objectManager = Bootstrap::getObjectManager();
$product = $objectManager->create(Product::class);
$product->setSku('simple');
$product->setName('random name update');
$message = [
'product' => $product
];
$topicName = 'async.magento.catalog.api.productrepositoryinterface.save.post';
$buuid = uniqid('bulk-');
$messageProps = [
'message_id' => uniqid('msg-'),
'topic_name' => $topicName
];
$consumerName = 'async.operations.all';
$this->bulkManagement->scheduleBulk($buuid, [], 'test bulk');
$operation = $this->operationRepository->create($topicName, $message, $buuid, 0);
$this->saveMultipleOperations->execute([$operation]);
$envelope = $this->createMock(EnvelopeInterface::class);
$envelope->method('getBody')
->willReturn($this->messageEncoder->encode(AsyncConfig::SYSTEM_TOPIC_NAME, $operation));
$envelope->method('getProperties')
->willReturn($messageProps);
$this->configuration
->method('getConsumerName')
->willReturn($consumerName);
$this->configuration
->method('getTopicNames')
->willReturn([$topicName]);
$this->model->execute($envelope);
/** @var Collection $collection */
$collection = $this->operationCollectionFactory->create();
$collection->addFieldToFilter(OperationInterface::BULK_ID, ['eq' => $buuid]);
$this->assertEquals(1, $collection->count());
$operation = $collection->getFirstItem();
$this->assertNotEmpty($operation->getData('started_at'));
// md5() here is not for cryptographic use.
// phpcs:ignore Magento2.Security.InsecureFunction
$code = md5($consumerName . '-' . $messageProps['message_id']);
$lockFactory = $objectManager->get(LockInterfaceFactory::class);
$lockReader = $objectManager->get(ReaderInterface::class);
$lock = $lockFactory->create();
$lockReader->read($lock, $code);
$this->assertNotEmpty($lock->getId());
}
}
@@ -0,0 +1,358 @@
<?php
/**
* Test services for name collisions.
*
* Let we have two service interfaces called Foo\Bar\Service\SomeBazV1Interface and Foo\Bar\Service\Some\BazV1Interface.
* Given current name generation logic both are going to be translated to BarSomeBazV1. This test checks such things
* are not going to happen.
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\AsynchronousOperations\Model;
use Magento\AsynchronousOperations\Model\ResourceModel\Bulk\Collection as BulkCollection;
use Magento\Framework\MessageQueue\BulkPublisherInterface;
use Magento\Framework\Exception\BulkException;
use Magento\Framework\Phrase;
use Magento\Framework\Registry;
use Magento\Framework\Webapi\Exception;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\TestFramework\MessageQueue\PublisherConsumerController;
use Magento\TestFramework\MessageQueue\EnvironmentPreconditionException;
use Magento\TestFramework\MessageQueue\PreconditionFailedException;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\ObjectManagerInterface;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*
* @magentoDbIsolation disabled
*/
class MassScheduleTest extends \PHPUnit\Framework\TestCase
{
/**
* @var string[]
*/
protected $consumers = ['async.operations.all'];
/**
* @var ObjectManagerInterface
*/
protected $objectManager;
/**
* @var MassSchedule
*/
private $massSchedule;
/**
* @var Collection
*/
private $collection;
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var PublisherConsumerController
*/
private $publisherConsumerController;
/**
* @var array
*/
private $skus = [];
/** @var string */
private $logFilePath;
/**
* @var Registry
*/
private $registry;
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->registry = $this->objectManager->get(Registry::class);
$this->massSchedule = $this->objectManager->create(MassSchedule::class);
$this->logFilePath = TESTS_TEMP_DIR . "/MessageQueueTestLog.txt";
$this->collection = $this->objectManager->create(Collection::class);
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
/** @var PublisherConsumerController publisherConsumerController */
$this->publisherConsumerController = $this->objectManager->create(PublisherConsumerController::class, [
'consumers' => $this->consumers,
'logFilePath' => $this->logFilePath,
'appInitParams' => \Magento\TestFramework\Helper\Bootstrap::getInstance()->getAppInitParams()
]);
try {
$this->publisherConsumerController->initialize();
} catch (EnvironmentPreconditionException $e) {
$this->markTestSkipped($e->getMessage());
} catch (PreconditionFailedException $e) {
$this->fail($e->getMessage());
}
parent::setUp();
}
/**
* @dataProvider productDataProvider
* @param ProductInterface[] $products
*/
public function testScheduleMass($products)
{
try {
$this->sendBulk($products);
} catch (BulkException $bulkException) {
$this->fail('Bulk was not accepted in full');
}
//assert all products are created
try {
$this->publisherConsumerController->waitForAsynchronousResult(
[$this, 'assertProductExists'],
[$this->skus, count($this->skus)]
);
} catch (PreconditionFailedException $e) {
$this->fail("Not all products were created");
}
}
/**
* @dataProvider publisherExceptionDataProvider
* @param \Exception $exception
*/
public function testScheduleMassWithExceptionDuringPublishing(\Exception $exception)
{
$products = [
['product' => $this->getProduct()],
];
$publisher = $this->createMock(BulkPublisherInterface::class);
$publisher->expects($this->atLeastOnce())
->method('publish')
->willThrowException($exception);
$bulkManagement = $this->objectManager->create(
BulkManagement::class,
['publisher' => $publisher]
);
$this->massSchedule = $this->objectManager->create(
MassSchedule::class,
['bulkManagement' => $bulkManagement]
);
$bulkCollection = $this->objectManager->create(BulkCollection::class);
$bulksCount = $bulkCollection->getSize();
try {
$this->massSchedule->publishMass(
'async.magento.catalog.api.productrepositoryinterface.save.post',
$products
);
$this->fail('Publish is not failed when operations are not published.');
} catch (\Exception $e) {
$bulkCollection = $this->objectManager->create(BulkCollection::class);
$this->assertEquals($bulksCount, $bulkCollection->getSize());
}
}
/**
* @return array
*/
public function publisherExceptionDataProvider(): array
{
return [
[new \InvalidArgumentException('Unknown publisher type async')],
[new \LogicException('Could not find an implementation type for type "async" and connection "amqp".')],
];
}
public function sendBulk($products)
{
$this->skus = [];
foreach ($products as $data) {
if (isset($data['product'])) {
$this->skus[] = $data['product']->getSku();
}
}
$this->clearProducts();
$result = $this->massSchedule->publishMass(
'async.magento.catalog.api.productrepositoryinterface.save.post',
$products
);
//assert bulk accepted with no errors
$this->assertFalse($result->isErrors());
//assert number of products sent to queue
$this->assertCount(count($this->skus), $result->getRequestItems());
}
protected function tearDown(): void
{
$this->publisherConsumerController->stopConsumers();
$this->clearProducts();
parent::tearDown();
}
private function clearProducts()
{
$size = $this->objectManager->create(Collection::class)
->addAttributeToFilter('sku', ['in' => $this->skus])
->load()
->getSize();
if ($size == 0) {
return;
}
$this->registry->unregister('isSecureArea');
$this->registry->register('isSecureArea', true);
try {
foreach ($this->skus as $sku) {
$this->productRepository->deleteById($sku);
}
} catch (\Exception $e) {
//nothing to delete
}
$this->registry->unregister('isSecureArea');
$size = $this->objectManager->create(Collection::class)
->addAttributeToFilter('sku', ['in' => $this->skus])
->load()
->getSize();
if ($size > 0) {
throw new Exception(new Phrase("Collection size after clearing the products: %size", ['size' => $size]));
}
}
public function assertProductExists($productsSkus, $count)
{
$collection = $this->objectManager->create(Collection::class)
->addAttributeToFilter('sku', ['in' => $productsSkus])
->load();
$size = $collection->getSize();
return $size == $count;
}
/**
* @dataProvider productExceptionDataProvider
* @param ProductInterface[] $products
*/
public function testScheduleMassOneEntityFailure($products)
{
try {
$this->sendBulk($products);
} catch (BulkException $e) {
$this->assertCount(1, $e->getErrors());
$errors = $e->getErrors();
$this->assertInstanceOf(\Magento\Framework\Exception\LocalizedException::class, $errors[0]);
$this->assertEquals("Error processing 1 element of input data", $errors[0]->getMessage());
$reasonException = $errors[0]->getPrevious();
$expectedErrorMessage = "Data item corresponding to \"product\" " .
"must be specified in the message with topic " .
"\"async.magento.catalog.api.productrepositoryinterface.save.post\".";
$this->assertEquals(
$expectedErrorMessage,
$reasonException->getMessage()
);
/** @var \Magento\WebapiAsync\Model\AsyncResponse $bulkStatus */
$bulkStatus = $e->getData();
$this->assertTrue($bulkStatus->isErrors());
/** @var ItemStatus[] $items */
$items = $bulkStatus->getRequestItems();
$this->assertCount(2, $items);
$this->assertEquals(ItemStatus::STATUS_ACCEPTED, $items[0]->getStatus());
$this->assertEquals(0, $items[0]->getId());
$this->assertEquals(ItemStatus::STATUS_REJECTED, $items[1]->getStatus());
$this->assertEquals(1, $items[1]->getId());
$this->assertEquals($expectedErrorMessage, $items[1]->getErrorMessage());
}
//assert one products is created
try {
$this->publisherConsumerController->waitForAsynchronousResult(
[$this, 'assertProductExists'],
[$this->skus, count($this->skus)]
);
} catch (PreconditionFailedException $e) {
$this->fail("Not all products were created");
}
}
private function getProduct()
{
/** @var $product \Magento\Catalog\Model\Product */
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->create(ProductInterface::class);
$product
->setTypeId('simple')
->setAttributeSetId(4)
->setWebsiteIds([1])
->setName('Simple Product 1')
->setSku('unique-simple-product1')
->setPrice(10)
->setMetaTitle('meta title')
->setMetaKeyword('meta keyword')
->setMetaDescription('meta description')
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
->setStockData(['use_config_manage_stock' => 0]);
return $product;
}
public function productDataProvider()
{
return [
'single_product' => [
[['product' => $this->getProduct()]],
],
'multiple_products' => [
[
['product' => $this->getProduct()
->setName('Simple Product 3')
->setSku('unique-simple-product3')
->setMetaTitle('meta title 3')
],
['product' => $this->getProduct()
->setName('Simple Product 2')
->setSku('unique-simple-product2')
->setMetaTitle('meta title 2')
]
]
],
];
}
public function productExceptionDataProvider()
{
return [
'single_product' => [
[['product' => $this->getProduct()]],
],
'multiple_products' => [
[
['product' => $this->getProduct()],
['customer' => $this->getProduct()]
]
],
];
}
}
@@ -0,0 +1,84 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\AsynchronousOperations\Model;
use Magento\AsynchronousOperations\Api\Data\OperationInterface;
use Magento\AsynchronousOperations\Api\Data\OperationInterfaceFactory;
use Magento\AsynchronousOperations\Model\BulkStatus;
use Magento\AsynchronousOperations\Model\OperationManagement;
use Magento\Framework\EntityManager\EntityManager;
use Magento\Framework\App\ResourceConnection;
use Magento\TestFramework\Helper\Bootstrap;
class OperationManagementTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\AsynchronousOperations\Model\OperationManagement
*/
private $model;
/**
* @var \Magento\AsynchronousOperations\Model\BulkStatus
*/
private $bulkStatusManagement;
/**
* @var OperationInterfaceFactory
*/
private $operationFactory;
/**
* @var EntityManager
*/
private $entityManager;
/**
* @var ResourceConnection
*/
private $connection;
protected function setUp(): void
{
$this->connection = Bootstrap::getObjectManager()->get(ResourceConnection::class);
$this->model = Bootstrap::getObjectManager()->get(OperationManagement::class);
$this->bulkStatusManagement = Bootstrap::getObjectManager()->get(BulkStatus::class);
$this->operationFactory = Bootstrap::getObjectManager()->get(OperationInterfaceFactory::class);
$this->entityManager = Bootstrap::getObjectManager()->get(EntityManager::class);
}
/**
* @magentoDataFixture Magento/AsynchronousOperations/_files/bulk.php
*/
public function testGetBulkStatus()
{
$operations = $this->bulkStatusManagement->getFailedOperationsByBulkId('bulk-uuid-5', 3);
if (empty($operations)) {
$this->fail('Operation doesn\'t exist');
}
/** @var OperationInterface $operation */
$operation = array_shift($operations);
$operationId = $operation->getId();
$this->assertTrue($this->model->changeOperationStatus(
'bulk-uuid-5',
$operationId,
OperationInterface::STATUS_TYPE_OPEN
));
$table = $this->connection->getTableName('magento_operation');
$connection = $this->connection->getConnection();
$select = $connection->select()
->from($table)
->where("bulk_uuid = ?", 'bulk-uuid-5')
->where("operation_key = ?", $operationId);
$updatedOperation = $connection->fetchRow($select);
$this->assertEquals(OperationInterface::STATUS_TYPE_OPEN, $updatedOperation['status']);
$this->assertNull($updatedOperation['result_message']);
$this->assertNull($updatedOperation['serialized_data']);
}
}
@@ -0,0 +1,117 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\AsynchronousOperations\Model;
use Magento\AsynchronousOperations\Api\Data\OperationInterface;
use Magento\AsynchronousOperations\Api\Data\OperationInterfaceFactory;
use Magento\AsynchronousOperations\Api\SaveMultipleOperationsInterface;
use Magento\AsynchronousOperations\Model\BulkStatus;
use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface;
use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterfaceFactory;
use Magento\Framework\EntityManager\EntityManager;
class SaveMultipleOperationsTest extends \PHPUnit\Framework\TestCase
{
private const BULK_UUID = "bulk-uuid-multiple-0";
/**
* @var BulkStatus
*/
private $bulkStatusManagement;
/**
* @var OperationInterfaceFactory
*/
private $operationFactory;
/**
* @var SaveMultipleOperationsInterface
*/
private $saveMultipleOperationsInterface;
/**
* @var EntityManager
*/
private $entityManager;
/**
* @var BulkSummaryInterfaceFactory
*/
private $bulkSummaryFactory;
/**
* Set Up the test
*/
protected function setUp(): void
{
$this->saveMultipleOperationsInterface = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
SaveMultipleOperationsInterface::class
);
$this->operationFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
OperationInterfaceFactory::class
);
$this->bulkStatusManagement = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
BulkStatus::class
);
$this->bulkSummaryFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
BulkSummaryInterfaceFactory::class
);
$this->entityManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
EntityManager::class
);
}
/**
* Test execute() of SaveMultipleOperations
*/
public function testExecute()
{
$operation = $this->createOperation();
$operations = [$operation, $operation, $operation];
$bulkSummary = $this->bulkSummaryFactory->create();
$this->entityManager->load($bulkSummary, self::BULK_UUID);
$bulkSummary->setBulkId(self::BULK_UUID);
$bulkSummary->setDescription("Test Bulk");
$bulkSummary->setUserId(1);
$bulkSummary->setUserType(1);
$bulkSummary->setOperationCount(count($operations));
$this->entityManager->save($bulkSummary);
$this->saveMultipleOperationsInterface->execute($operations);
$operationsCount = $this->bulkStatusManagement
->getOperationsCountByBulkIdAndStatus(self::BULK_UUID, OperationInterface::STATUS_TYPE_OPEN);
$this->assertEquals($operationsCount, 3);
}
/**
* Create Operation object and pre-fill with test data
* @return OperationInterface
*/
public function createOperation()
{
$serializedData = [
'entity_id' => null,
'entity_link' => '',
'meta_information' => json_encode([
'entity_id' => 5,
'meta_information' => 'Test'
])
];
$data = [
'data' => [
OperationInterface::BULK_ID => self::BULK_UUID,
OperationInterface::TOPIC_NAME => "topic-4",
OperationInterface::SERIALIZED_DATA => json_encode($serializedData),
OperationInterface::STATUS => OperationInterface::STATUS_TYPE_OPEN,
],
];
return $this->operationFactory->create($data);
}
}