init
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\BundleImportExport\Model;
|
||||
|
||||
use Magento\CatalogImportExport\Model\AbstractProductExportImportTestCase;
|
||||
|
||||
class BundleTest extends AbstractProductExportImportTestCase
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function exportImportDataProvider(): array
|
||||
{
|
||||
return [
|
||||
// @todo uncomment after MAGETWO-49677 resolved
|
||||
/*
|
||||
'bundle-product' => [
|
||||
[
|
||||
'Magento/Bundle/_files/product.php'
|
||||
],
|
||||
[
|
||||
'bundle-product',
|
||||
]
|
||||
],
|
||||
*/
|
||||
'bundle-product-multi-options' => [
|
||||
[
|
||||
'Magento/Bundle/_files/product_with_multiple_options.php'
|
||||
],
|
||||
[
|
||||
'bundle-product',
|
||||
]
|
||||
],
|
||||
// @todo uncomment after MAGETWO-49677 resolved
|
||||
/*
|
||||
'bundle-product-tier-pricing' => [
|
||||
[
|
||||
'Magento/Bundle/_files/product_with_tier_pricing.php'
|
||||
],
|
||||
[
|
||||
'bundle-product',
|
||||
]
|
||||
]
|
||||
*/
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Run import/export tests.
|
||||
*
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoDbIsolation disabled
|
||||
* @magentoAppIsolation enabled
|
||||
*
|
||||
* @param array $fixtures
|
||||
* @param string[] $skus
|
||||
* @param string[] $skippedAttributes
|
||||
* @return void
|
||||
* @dataProvider exportImportDataProvider
|
||||
*/
|
||||
public function testImportExport(array $fixtures, array $skus, array $skippedAttributes = []): void
|
||||
{
|
||||
$rollbacks = [];
|
||||
foreach ($fixtures as $fixture) {
|
||||
$rollbacks[] = str_replace('.php', '_rollback.php', $fixture);
|
||||
}
|
||||
$this->fixtures = $fixtures;
|
||||
$this->executeFixtures($fixtures);
|
||||
$this->modifyData($skus);
|
||||
$skippedAttributes = array_merge(self::$skippedAttributes, $skippedAttributes);
|
||||
$csvFile = $this->executeExportTest($skus, $skippedAttributes);
|
||||
$this->executeImportReplaceTest($skus, $skippedAttributes, false, $csvFile);
|
||||
$this->executeImportDeleteTest($skus, $csvFile);
|
||||
$this->executeFixtures($rollbacks);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function assertEqualsSpecificAttributes(
|
||||
\Magento\Catalog\Model\Product $expectedProduct,
|
||||
\Magento\Catalog\Model\Product $actualProduct
|
||||
): void {
|
||||
$expectedBundleProductOptions = $expectedProduct->getExtensionAttributes()->getBundleProductOptions();
|
||||
$actualBundleProductOptions = $actualProduct->getExtensionAttributes()->getBundleProductOptions();
|
||||
|
||||
$this->assertEquals(count($expectedBundleProductOptions), count($actualBundleProductOptions));
|
||||
|
||||
$expectedBundleProductOptionsToCompare = [];
|
||||
foreach ($expectedBundleProductOptions as $expectedBundleProductOption) {
|
||||
$expectedBundleProductOptionsToCompare[$expectedBundleProductOption->getTitle()]['type']
|
||||
= $expectedBundleProductOption->getType();
|
||||
foreach ($expectedBundleProductOption->getProductLinks() as $productLink) {
|
||||
$expectedBundleProductOptionsToCompare[$expectedBundleProductOption->getTitle()]['product_links'][]
|
||||
= $productLink->getSku();
|
||||
}
|
||||
}
|
||||
|
||||
$actualBundleProductOptionsToCompare = [];
|
||||
foreach ($actualBundleProductOptions as $actualBundleProductOption) {
|
||||
$actualBundleProductOptionsToCompare[$actualBundleProductOption->getTitle()]['type']
|
||||
= $actualBundleProductOption->getType();
|
||||
foreach ($actualBundleProductOption->getProductLinks() as $productLink) {
|
||||
$actualBundleProductOptionsToCompare[$actualBundleProductOption->getTitle()]['product_links'][]
|
||||
= $productLink->getSku();
|
||||
}
|
||||
}
|
||||
|
||||
$this->assertEquals(count($expectedBundleProductOptions), count($actualBundleProductOptions));
|
||||
|
||||
foreach ($expectedBundleProductOptionsToCompare as $key => $expectedBundleProductOption) {
|
||||
$this->assertEquals(
|
||||
$expectedBundleProductOption['type'],
|
||||
$actualBundleProductOptionsToCompare[$key]['type']
|
||||
);
|
||||
|
||||
$expectedProductLinks = $expectedBundleProductOption['product_links'];
|
||||
$actualProductLinks = $actualBundleProductOptionsToCompare[$key]['product_links'];
|
||||
|
||||
sort($expectedProductLinks);
|
||||
sort($actualProductLinks);
|
||||
|
||||
$this->assertEquals($expectedProductLinks, $actualProductLinks);
|
||||
}
|
||||
}
|
||||
}
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\BundleImportExport\Model\Export;
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class RowCustomizerTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\BundleImportExport\Model\Export\RowCustomizer
|
||||
*/
|
||||
private $model;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->model = $this->objectManager->create(
|
||||
\Magento\BundleImportExport\Model\Export\RowCustomizer::class
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Bundle/_files/product.php
|
||||
* @magentoDbIsolation disabled
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testPrepareData(): void
|
||||
{
|
||||
$parsedAdditionalAttributes = 'text_attribute=!@#$%^&*()_+1234567890-=|\\:;"\'<,>.?/'
|
||||
. ',text_attribute2=,';
|
||||
$allAdditionalAttributes = $parsedAdditionalAttributes . ',weight_type=0,price_type=1';
|
||||
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
|
||||
$collection = $this->objectManager->get(\Magento\Catalog\Model\ResourceModel\Product\Collection::class);
|
||||
$select = $collection->getConnection()->select()
|
||||
->from(['p' => $collection->getTable('catalog_product_entity')], ['sku', 'entity_id'])
|
||||
->where('sku IN(?)', ['simple', 'custom-design-simple-product', 'bundle-product']);
|
||||
$ids = $collection->getConnection()->fetchPairs($select);
|
||||
$select = (string)$collection->getSelect();
|
||||
$this->model->prepareData($collection, array_values($ids));
|
||||
$this->assertEquals($select, (string)$collection->getSelect());
|
||||
$result = $this->model->addData(['additional_attributes' => $allAdditionalAttributes], $ids['bundle-product']);
|
||||
$this->assertArrayHasKey('bundle_price_type', $result);
|
||||
$this->assertArrayHasKey('bundle_shipment_type', $result);
|
||||
$this->assertArrayHasKey('bundle_sku_type', $result);
|
||||
$this->assertArrayHasKey('bundle_price_view', $result);
|
||||
$this->assertArrayHasKey('bundle_weight_type', $result);
|
||||
$this->assertArrayHasKey('bundle_values', $result);
|
||||
$this->assertStringContainsString('sku=simple,', $result['bundle_values']);
|
||||
$this->assertEquals([], $this->model->addData([], $ids['simple']));
|
||||
$this->assertEquals($parsedAdditionalAttributes, $result['additional_attributes']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Store/_files/second_store.php
|
||||
* @magentoDataFixture Magento/Bundle/_files/product.php
|
||||
* @magentoDbIsolation disabled
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testPrepareDataWithDifferentStoreValues(): void
|
||||
{
|
||||
$storeCode = 'default';
|
||||
$expectedNames = [
|
||||
'name' => 'Bundle Product Items',
|
||||
'name_' . $storeCode => 'Bundle Product Items_' . $storeCode,
|
||||
];
|
||||
$parsedAdditionalAttributes = 'text_attribute=!@#$%^&*()_+1234567890-=|\\:;"\'<,>.?/'
|
||||
. ',text_attribute2=,';
|
||||
$allAdditionalAttributes = $parsedAdditionalAttributes . ',weight_type=0,price_type=1';
|
||||
$collection = $this->objectManager->get(\Magento\Catalog\Model\ResourceModel\Product\Collection::class);
|
||||
/** @var \Magento\Store\Model\Store $store */
|
||||
$store = $this->objectManager->create(\Magento\Store\Model\Store::class);
|
||||
$store->load($storeCode, 'code');
|
||||
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
|
||||
$productRepository = $this->objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
|
||||
$product = $productRepository->get('bundle-product', 1, $store->getId());
|
||||
|
||||
$extension = $product->getExtensionAttributes();
|
||||
$options = $extension->getBundleProductOptions();
|
||||
|
||||
foreach ($options as $productOption) {
|
||||
$productOption->setTitle($productOption->getTitle() . '_' . $store->getCode());
|
||||
}
|
||||
$extension->setBundleProductOptions($options);
|
||||
$product->setExtensionAttributes($extension);
|
||||
$productRepository->save($product);
|
||||
$this->model->prepareData($collection, [$product->getId()]);
|
||||
$result = $this->model->addData(['additional_attributes' => $allAdditionalAttributes], $product->getId());
|
||||
$bundleValues = array_map(
|
||||
function ($input) {
|
||||
$data = explode('=', $input);
|
||||
|
||||
return [$data[0] => $data[1]];
|
||||
},
|
||||
explode(',', $result['bundle_values'])
|
||||
);
|
||||
$actualNames = [
|
||||
'name' => array_column($bundleValues, 'name')[0],
|
||||
'name' . '_' . $store->getCode() => array_column($bundleValues, 'name' . '_' . $store->getCode())[0],
|
||||
];
|
||||
|
||||
self::assertSame($expectedNames, $actualNames);
|
||||
}
|
||||
}
|
||||
+434
@@ -0,0 +1,434 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\BundleImportExport\Model\Import\Product\Type;
|
||||
|
||||
use Magento\Catalog\Api\Data\ProductInterface;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
|
||||
use Magento\CatalogInventory\Api\Data\StockItemInterface;
|
||||
use Magento\CatalogInventory\Api\StockConfigurationInterface;
|
||||
use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory;
|
||||
use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\Filesystem;
|
||||
use Magento\ImportExport\Model\Import;
|
||||
use Magento\ImportExport\Model\Import\Adapter as ImportAdapter;
|
||||
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
|
||||
use Magento\ImportExport\Model\Import\Source\Csv;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\Framework\App\Filesystem\DirectoryList;
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoAppIsolation enabled
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class BundleTest extends \Magento\TestFramework\Indexer\TestCase
|
||||
{
|
||||
/**
|
||||
* Bundle product test Name
|
||||
*/
|
||||
private const TEST_PRODUCT_NAME = 'Bundle 1';
|
||||
|
||||
/**
|
||||
* Bundle product test Type
|
||||
*/
|
||||
private const TEST_PRODUCT_TYPE = 'bundle';
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\ObjectManagerInterface
|
||||
*/
|
||||
protected $objectManager;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $importedProductSkus;
|
||||
|
||||
/**
|
||||
* List of Bundle options SKU
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $optionSkuList = ['Simple 1', 'Simple 2', 'Simple 3'];
|
||||
|
||||
public static function setUpBeforeClass(): void
|
||||
{
|
||||
$db = Bootstrap::getInstance()->getBootstrap()
|
||||
->getApplication()
|
||||
->getDbInstance();
|
||||
if (!$db->isDbDumpExists()) {
|
||||
throw new \LogicException('DB dump does not exist.');
|
||||
}
|
||||
$db->restoreFromDbDump();
|
||||
|
||||
parent::setUpBeforeClass();
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testBundleImport()
|
||||
{
|
||||
// import data from CSV file
|
||||
$pathToFile = __DIR__ . '/../../_files/import_bundle.csv';
|
||||
$errors = $this->doImport($pathToFile, Import::BEHAVIOR_APPEND);
|
||||
$this->assertEquals(0, $errors->getErrorsCount());
|
||||
|
||||
$resource = $this->objectManager->get(ProductResource::class);
|
||||
$productId = $resource->getIdBySku(self::TEST_PRODUCT_NAME);
|
||||
$this->assertIsNumeric($productId);
|
||||
/** @var Product $product */
|
||||
$product = $this->objectManager->create(Product::class);
|
||||
$product->load($productId);
|
||||
|
||||
$this->assertFalse($product->isObjectNew());
|
||||
$this->assertEquals(self::TEST_PRODUCT_NAME, $product->getName());
|
||||
$this->assertEquals(self::TEST_PRODUCT_TYPE, $product->getTypeId());
|
||||
$this->assertEquals(1, $product->getShipmentType());
|
||||
|
||||
$optionIdList = $resource->getProductsIdsBySkus($this->optionSkuList);
|
||||
$bundleOptionCollection = $product->getExtensionAttributes()->getBundleProductOptions();
|
||||
$this->assertCount(2, $bundleOptionCollection);
|
||||
foreach ($bundleOptionCollection as $optionKey => $option) {
|
||||
$this->assertEquals('checkbox', $option->getData('type'));
|
||||
$this->assertEquals('Option ' . ($optionKey + 1), $option->getData('title'));
|
||||
$this->assertEquals(self::TEST_PRODUCT_NAME, $option->getData('sku'));
|
||||
$this->assertEquals($optionKey + 1, count($option->getData('product_links')));
|
||||
foreach ($option->getData('product_links') as $linkKey => $productLink) {
|
||||
$optionSku = 'Simple ' . ($optionKey + 1 + $linkKey);
|
||||
$this->assertEquals($optionIdList[$optionSku], $productLink->getData('entity_id'));
|
||||
$this->assertEquals($optionSku, $productLink->getData('sku'));
|
||||
|
||||
switch ($optionKey + 1 + $linkKey) {
|
||||
case 1:
|
||||
$this->assertEquals(1, (int) $productLink->getCanChangeQuantity());
|
||||
break;
|
||||
case 2:
|
||||
$this->assertEquals(0, (int) $productLink->getCanChangeQuantity());
|
||||
break;
|
||||
case 3:
|
||||
$this->assertEquals(1, (int) $productLink->getCanChangeQuantity());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->importedProductSkus = ['Simple 1', 'Simple 2', 'Simple 3', 'Bundle 1'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that Bundle options are updated correctly by import
|
||||
*
|
||||
* @dataProvider valuesDataProvider
|
||||
*
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoAppIsolation enabled
|
||||
* @param array $expectedValues
|
||||
* @return void
|
||||
*/
|
||||
public function testBundleImportUpdateValues(array $expectedValues): void
|
||||
{
|
||||
// import data from CSV file
|
||||
$pathToFile = __DIR__ . '/../../_files/import_bundle.csv';
|
||||
$errors = $this->doImport($pathToFile, Import::BEHAVIOR_APPEND);
|
||||
$this->assertEquals(0, $errors->getErrorsCount());
|
||||
|
||||
// import data from CSV file to update values
|
||||
$pathToFile2 = __DIR__ . '/../../_files/import_bundle_update_values.csv';
|
||||
$errors = $this->doImport($pathToFile2, Import::BEHAVIOR_APPEND);
|
||||
$this->assertEquals(0, $errors->getErrorsCount());
|
||||
|
||||
$resource = $this->objectManager->get(ProductResource::class);
|
||||
$productId = $resource->getIdBySku(self::TEST_PRODUCT_NAME);
|
||||
$this->assertIsNumeric($productId);
|
||||
/** @var Product $product */
|
||||
$product = $this->objectManager->create(Product::class);
|
||||
$product->load($productId);
|
||||
|
||||
$this->assertFalse($product->isObjectNew());
|
||||
$this->assertEquals(self::TEST_PRODUCT_NAME, $product->getName());
|
||||
$this->assertEquals(self::TEST_PRODUCT_TYPE, $product->getTypeId());
|
||||
$this->assertEquals(1, $product->getShipmentType());
|
||||
|
||||
$optionIdList = $resource->getProductsIdsBySkus($this->optionSkuList);
|
||||
$bundleOptionCollection = $product->getExtensionAttributes()->getBundleProductOptions();
|
||||
$this->assertCount(3, $bundleOptionCollection);
|
||||
foreach ($bundleOptionCollection as $optionKey => $option) {
|
||||
$this->assertEquals('checkbox', $option->getData('type'));
|
||||
$this->assertEquals($expectedValues[$optionKey]['title'], $option->getData('title'));
|
||||
$this->assertEquals(self::TEST_PRODUCT_NAME, $option->getData('sku'));
|
||||
foreach ($option->getData('product_links') as $linkKey => $productLink) {
|
||||
$optionSku = $expectedValues[$optionKey]['product_links'][$linkKey];
|
||||
$this->assertEquals($optionIdList[$optionSku], $productLink->getData('entity_id'));
|
||||
$this->assertEquals($optionSku, $productLink->getData('sku'));
|
||||
}
|
||||
}
|
||||
$this->importedProductSkus = ['Simple 1', 'Simple 2', 'Simple 3', 'Bundle 1'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that Bundle options with question mark are updated correctly by import
|
||||
*
|
||||
*
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoAppIsolation enabled
|
||||
* @return void
|
||||
*/
|
||||
public function testBundleImportUpdateValuesWithQuestionMark(): void
|
||||
{
|
||||
// import data from CSV file
|
||||
$pathToFile = __DIR__ . '/../../_files/import_bundle_with_question_mark.csv';
|
||||
$errors = $this->doImport($pathToFile, Import::BEHAVIOR_APPEND);
|
||||
$this->assertEquals(0, $errors->getErrorsCount());
|
||||
|
||||
// import data from CSV file to update values
|
||||
$pathToFile2 = __DIR__ . '/../../_files/import_bundle_with_question_mark.csv';
|
||||
$errors = $this->doImport($pathToFile2, Import::BEHAVIOR_APPEND);
|
||||
$this->assertEquals(0, $errors->getErrorsCount());
|
||||
|
||||
$resource = $this->objectManager->get(ProductResource::class);
|
||||
$productId = $resource->getIdBySku(self::TEST_PRODUCT_NAME);
|
||||
$this->assertIsNumeric($productId);
|
||||
/** @var Product $product */
|
||||
$product = $this->objectManager->create(ProductRepositoryInterface::class);
|
||||
$ProductRepository = $product->get(self::TEST_PRODUCT_NAME);
|
||||
|
||||
$this->assertEquals(self::TEST_PRODUCT_NAME, $ProductRepository->getName());
|
||||
$this->assertEquals(self::TEST_PRODUCT_TYPE, $ProductRepository->getTypeId());
|
||||
$this->assertEquals(1, $ProductRepository->getShipmentType());
|
||||
|
||||
$bundleOptionCollection = $ProductRepository->getExtensionAttributes()->getBundleProductOptions();
|
||||
$this->assertCount(1, $bundleOptionCollection);
|
||||
|
||||
$this->importedProductSkus = ['Simple 1', 'Bundle 1'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Store/_files/second_store.php
|
||||
* @magentoDbIsolation disabled
|
||||
* @magentoAppArea adminhtml
|
||||
* @return void
|
||||
*/
|
||||
public function testBundleImportWithMultipleStoreViews(): void
|
||||
{
|
||||
// import data from CSV file
|
||||
$pathToFile = __DIR__ . '/../../_files/import_bundle_multiple_store_views.csv';
|
||||
$errors = $this->doImport($pathToFile, Import::BEHAVIOR_APPEND);
|
||||
$this->assertEquals(0, $errors->getErrorsCount());
|
||||
$resource = $this->objectManager->get(ProductResource::class);
|
||||
$productId = $resource->getIdBySku(self::TEST_PRODUCT_NAME);
|
||||
$this->assertIsNumeric($productId);
|
||||
/** @var Product $product */
|
||||
$product = $this->objectManager->create(Product::class);
|
||||
$product->load($productId);
|
||||
$this->assertFalse($product->isObjectNew());
|
||||
$this->assertEquals(self::TEST_PRODUCT_NAME, $product->getName());
|
||||
$this->assertEquals(self::TEST_PRODUCT_TYPE, $product->getTypeId());
|
||||
$this->assertEquals(1, $product->getShipmentType());
|
||||
$optionIdList = $resource->getProductsIdsBySkus($this->optionSkuList);
|
||||
/** @var ProductRepositoryInterface $productRepository */
|
||||
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
|
||||
$i = 0;
|
||||
foreach ($product->getStoreIds() as $storeId) {
|
||||
$bundleOptionCollection = $productRepository->get(self::TEST_PRODUCT_NAME, false, $storeId)
|
||||
->getExtensionAttributes()->getBundleProductOptions();
|
||||
$this->assertCount(2, $bundleOptionCollection);
|
||||
$i++;
|
||||
foreach ($bundleOptionCollection as $optionKey => $option) {
|
||||
$this->assertEquals('checkbox', $option->getData('type'));
|
||||
$this->assertEquals('Option ' . $i . ' ' . ($optionKey + 1), $option->getData('title'));
|
||||
$this->assertEquals(self::TEST_PRODUCT_NAME, $option->getData('sku'));
|
||||
$this->assertEquals($optionKey + 1, count($option->getData('product_links')));
|
||||
foreach ($option->getData('product_links') as $linkKey => $productLink) {
|
||||
$optionSku = 'Simple ' . ($optionKey + 1 + $linkKey);
|
||||
$this->assertEquals($optionIdList[$optionSku], $productLink->getData('entity_id'));
|
||||
$this->assertEquals($optionSku, $productLink->getData('sku'));
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->importedProductSkus = ['Simple 1', 'Simple 2', 'Simple 3', 'Bundle 1'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provider for testBundleImportUpdateValues
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function valuesDataProvider(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
[
|
||||
0 => [
|
||||
'title' => 'Option 1',
|
||||
'product_links' => ['Simple 1'],
|
||||
],
|
||||
1 => [
|
||||
'title' => 'Option 1 new',
|
||||
'product_links' => ['Simple 1'],
|
||||
],
|
||||
2 => [
|
||||
'title' => 'Option 2',
|
||||
'product_links' => ['Simple 2', 'Simple 3'],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDbIsolation enabled
|
||||
* @dataProvider shouldUpdateBundleStockStatusIfChildProductsStockStatusChangedDataProvider
|
||||
* @param bool $isOption1Required
|
||||
* @param bool $isOption2Required
|
||||
* @param string $outOfStockImportFile
|
||||
* @param string $inStockImportFile
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
public function testShouldUpdateBundleStockStatusIfChildProductsStockStatusChanged(
|
||||
bool $isOption1Required,
|
||||
bool $isOption2Required,
|
||||
string $outOfStockImportFile,
|
||||
string $inStockImportFile
|
||||
): void {
|
||||
// import data from CSV file
|
||||
$pathToFile = __DIR__ . '/../../_files/import_bundle.csv';
|
||||
$errors = $this->doImport($pathToFile, Import::BEHAVIOR_APPEND);
|
||||
$this->assertEquals(0, $errors->getErrorsCount());
|
||||
$this->importedProductSkus = ['Simple 1', 'Simple 2', 'Simple 3', 'Bundle 1'];
|
||||
$sku = 'Bundle 1';
|
||||
/** @var ProductRepositoryInterface $productRepository */
|
||||
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
|
||||
/** @var ProductInterface $product */
|
||||
$product = $productRepository->get($sku, true, null, true);
|
||||
$options = $product->getExtensionAttributes()->getBundleProductOptions();
|
||||
$options[0]->setRequired($isOption1Required);
|
||||
$options[1]->setRequired($isOption2Required);
|
||||
$extension = $product->getExtensionAttributes();
|
||||
$extension->setBundleProductOptions($options);
|
||||
$product->setExtensionAttributes($extension);
|
||||
$productRepository->save($product);
|
||||
|
||||
$stockItem = $this->getStockItem((int) $product->getId());
|
||||
$this->assertNotNull($stockItem);
|
||||
$this->assertTrue($stockItem->getIsInStock());
|
||||
|
||||
$errors = $this->doImport(__DIR__ . '/../../_files/' . $outOfStockImportFile);
|
||||
$this->assertEquals(0, $errors->getErrorsCount());
|
||||
|
||||
$stockItem = $this->getStockItem((int) $product->getId());
|
||||
$this->assertNotNull($stockItem);
|
||||
$this->assertFalse($stockItem->getIsInStock());
|
||||
|
||||
$errors = $this->doImport(__DIR__ . '/../../_files/' . $inStockImportFile);
|
||||
$this->assertEquals(0, $errors->getErrorsCount());
|
||||
|
||||
$stockItem = $this->getStockItem((int) $product->getId());
|
||||
$this->assertNotNull($stockItem);
|
||||
$this->assertTrue($stockItem->getIsInStock());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function shouldUpdateBundleStockStatusIfChildProductsStockStatusChangedDataProvider(): array
|
||||
{
|
||||
return [
|
||||
'all options are required' => [
|
||||
true,
|
||||
true,
|
||||
'out-of-stock' => 'import_bundle_set_option1_products_out_of_stock.csv',
|
||||
'in-stock' => 'import_bundle_set_option1_products_in_stock.csv'
|
||||
],
|
||||
'all options are optional' => [
|
||||
false,
|
||||
false,
|
||||
'out-of-stock' => 'import_bundle_set_all_products_out_of_stock.csv',
|
||||
'in-stock' => 'import_bundle_set_option1_products_in_stock.csv'
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $productId
|
||||
* @return StockItemInterface|null
|
||||
*/
|
||||
private function getStockItem(int $productId): ?StockItemInterface
|
||||
{
|
||||
$criteriaFactory = $this->objectManager->create(StockItemCriteriaInterfaceFactory::class);
|
||||
$stockItemRepository = $this->objectManager->create(StockItemRepositoryInterface::class);
|
||||
$stockConfiguration = $this->objectManager->create(StockConfigurationInterface::class);
|
||||
$criteria = $criteriaFactory->create();
|
||||
$criteria->setScopeFilter($stockConfiguration->getDefaultScopeId());
|
||||
$criteria->setProductsFilter($productId);
|
||||
$stockItemCollection = $stockItemRepository->getList($criteria);
|
||||
$stockItems = $stockItemCollection->getItems();
|
||||
return reset($stockItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $file
|
||||
* @param string $behavior
|
||||
* @param bool $validateOnly
|
||||
* @return ProcessingErrorAggregatorInterface
|
||||
*/
|
||||
private function doImport(
|
||||
string $file,
|
||||
string $behavior = Import::BEHAVIOR_ADD_UPDATE,
|
||||
bool $validateOnly = false
|
||||
): ProcessingErrorAggregatorInterface {
|
||||
/** @var Filesystem $filesystem */
|
||||
$filesystem = $this->objectManager->create(Filesystem::class);
|
||||
$directoryWrite = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
|
||||
$source = ImportAdapter::findAdapterFor($file, $directoryWrite);
|
||||
$model = $this->objectManager->create(\Magento\CatalogImportExport\Model\Import\Product::class);
|
||||
$model->setParameters(['behavior' => $behavior, 'entity' => 'catalog_product']);
|
||||
$model->setSource($source);
|
||||
$errors = $model->validateData();
|
||||
if (!$validateOnly && !$errors->getAllErrors()) {
|
||||
$model->importData();
|
||||
}
|
||||
return $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* teardown
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
if (!empty($this->importedProductSkus)) {
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
/** @var ProductRepositoryInterface $productRepository */
|
||||
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
|
||||
$registry = $objectManager->get(\Magento\Framework\Registry::class);
|
||||
/** @var ProductRepositoryInterface $productRepository */
|
||||
$registry->unregister('isSecureArea');
|
||||
$registry->register('isSecureArea', true);
|
||||
|
||||
foreach ($this->importedProductSkus as $sku) {
|
||||
try {
|
||||
$productRepository->deleteById($sku);
|
||||
} catch (NoSuchEntityException $e) {
|
||||
// product already deleted
|
||||
}
|
||||
}
|
||||
$registry->unregister('isSecureArea');
|
||||
$registry->register('isSecureArea', false);
|
||||
}
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
sku,store_view_code,attribute_set_code,product_type,product_websites,name,product_online,price,additional_attributes,qty,out_of_stock_qty,website_id,bundle_price_type,bundle_sku_type,bundle_price_view,bundle_weight_type,bundle_values
|
||||
Simple 1,,Default,simple,base,Simple 1,1,100,,1000,0,1,,,,,
|
||||
Simple 2,,Default,simple,base,Simple 2,1,200,,1000,0,1,,,,,
|
||||
Simple 3,,Default,simple,base,Simple 3,1,300,,1000,0,1,,,,,
|
||||
Bundle 1,,Default,bundle,base,Bundle 1,1,,shipment_type=separately,0,0,1,dynamic,dynamic,Price range,dynamic,"name=Option 1,type=checkbox,required=1,sku=Simple 1,price=0.0000,default=0,default_qty=1.0000,price_type=fixed,can_change_qty=1|name=Option 2,type=checkbox,required=1,sku=Simple 2,price=0.0000,default=0,default_qty=1.0000,price_type=fixed,can_change_qty=0|name=Option 2,type=checkbox,required=1,sku=Simple 3,price=0.0000,default=0,default_qty=1.0000,price_type=fixed"
|
||||
|
+5
@@ -0,0 +1,5 @@
|
||||
sku,store_view_code,attribute_set_code,product_type,product_websites,name,product_online,price,additional_attributes,qty,out_of_stock_qty,website_id,bundle_price_type,bundle_sku_type,bundle_price_view,bundle_weight_type,bundle_values
|
||||
Simple 1,,Default,simple,base,Simple 1,1,100,,1000,0,1,,,,,
|
||||
Simple 2,,Default,simple,base,Simple 2,1,200,,1000,0,1,,,,,
|
||||
Simple 3,,Default,simple,base,Simple 3,1,300,,1000,0,1,,,,,
|
||||
Bundle 1,,Default,bundle,base,Bundle 1,1,,shipment_type=separately,0,0,1,dynamic,dynamic,Price range,dynamic,"name=Option 1,name_default=Option 1 1,name_fixture_second_store=Option 2 1,type=checkbox,required=1,sku=Simple 1,price=0.0000,default=0,default_qty=1.0000,price_type=fixed|name=Option 2,name_default=Option 1 2,name_fixture_second_store=Option 2 2,type=checkbox,required=1,sku=Simple 2,price=0.0000,default=0,default_qty=1.0000,price_type=fixed|name=Option 2,type=checkbox,required=1,sku=Simple 3,price=0.0000,default=0,default_qty=1.0000,price_type=fixed"
|
||||
|
+4
@@ -0,0 +1,4 @@
|
||||
"sku","qty","is_in_stock"
|
||||
"Simple 1","0","0"
|
||||
"Simple 2","0","0"
|
||||
"Simple 3","0","0"
|
||||
|
+2
@@ -0,0 +1,2 @@
|
||||
"sku","qty","is_in_stock"
|
||||
"Simple 1","100","1"
|
||||
|
+2
@@ -0,0 +1,2 @@
|
||||
"sku","qty","is_in_stock"
|
||||
"Simple 1","0","0"
|
||||
|
+2
@@ -0,0 +1,2 @@
|
||||
sku,store_view_code,attribute_set_code,product_type,product_websites,name,product_online,price,additional_attributes,qty,out_of_stock_qty,website_id,bundle_price_type,bundle_sku_type,bundle_price_view,bundle_weight_type,bundle_values
|
||||
Bundle 1,,Default,bundle,base,Bundle 1,1,,shipment_type=separately,0,0,1,dynamic,dynamic,Price range,dynamic,"name=Option 1 new,type=checkbox,required=1,sku=Simple 1,price=0.0000,default=0,default_qty=1.0000,price_type=fixed,can_change_qty=1"
|
||||
|
+3
@@ -0,0 +1,3 @@
|
||||
sku,store_view_code,attribute_set_code,product_type,product_websites,name,product_online,price,additional_attributes,qty,out_of_stock_qty,website_id,bundle_price_type,bundle_sku_type,bundle_price_view,bundle_weight_type,bundle_values
|
||||
Simple 1,,Default,simple,base,Simple 1,1,100,,1000,0,1,,,,,
|
||||
Bundle 1,,Default,bundle,base,Bundle 1,1,,shipment_type=separately,0,0,1,dynamic,dynamic,Price range,dynamic,"name=Option?,type=checkbox,required=1,sku=Simple 1,price=0.0000,default=0,default_qty=1.0000,price_type=fixed,can_change_qty=1"
|
||||
|
Reference in New Issue
Block a user