Skip to content

Commit a514d7f

Browse files
MC-29126: Admin: Create/update/delete configurable product
1 parent c396c2d commit a514d7f

File tree

3 files changed

+54
-17
lines changed

3 files changed

+54
-17
lines changed

dev/tests/integration/testsuite/Magento/ConfigurableProduct/Controller/Adminhtml/ProductTest.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@
1717
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
1818
use Magento\Eav\Model\Config;
1919
use Magento\Framework\App\Request\Http as HttpRequest;
20+
use Magento\Framework\Exception\NoSuchEntityException;
2021
use Magento\Framework\Message\MessageInterface;
2122
use Magento\Framework\Registry;
22-
use Magento\Framework\Serialize\Serializer\Json;
23+
use Magento\Framework\Serialize\SerializerInterface;
2324
use Magento\TestFramework\TestCase\AbstractBackendController;
2425

2526
/**
2627
* Tests for configurable product admin save.
2728
*
2829
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2930
* @magentoAppArea adminhtml
30-
* @magentoDbIsolation disabled
31+
* @magentoDbIsolation enabled
3132
*/
3233
class ProductTest extends AbstractBackendController
3334
{
@@ -39,15 +40,15 @@ class ProductTest extends AbstractBackendController
3940
/**
4041
* @var ProductAttributeRepositoryInterface
4142
*/
42-
private $attributeRepository;
43+
private $productAttributeRepository;
4344

4445
/**
4546
* @var Registry
4647
*/
4748
private $registry;
4849

4950
/**
50-
* @var Json
51+
* @var SerializerInterface
5152
*/
5253
private $jsonSerializer;
5354

@@ -63,9 +64,9 @@ protected function setUp()
6364
{
6465
parent::setUp();
6566
$this->productRepository = $this->_objectManager->create(ProductRepositoryInterface::class);
66-
$this->attributeRepository = $this->_objectManager->create(ProductAttributeRepositoryInterface::class);
67+
$this->productAttributeRepository = $this->_objectManager->create(ProductAttributeRepositoryInterface::class);
6768
$this->registry = $this->_objectManager->get(Registry::class);
68-
$this->jsonSerializer = $this->_objectManager->get(Json::class);
69+
$this->jsonSerializer = $this->_objectManager->get(SerializerInterface::class);
6970
$this->eavConfig = $this->_objectManager->get(Config::class);
7071
}
7172

@@ -86,7 +87,7 @@ public function testSaveActionAssociatedProductIds(): void
8687
]
8788
);
8889
$this->dispatch('backend/catalog/product/save');
89-
$this->assertSessionMessages($this->equalTo(['You saved the product.']), MessageInterface::TYPE_SUCCESS);
90+
$this->assertSessionMessages($this->equalTo([__('You saved the product.')]), MessageInterface::TYPE_SUCCESS);
9091
$this->assertRegistryConfigurableLinks($associatedProductIds);
9192
$this->assertConfigurableLinks('configurable', $associatedProductIds);
9293
}
@@ -101,10 +102,11 @@ public function testSaveNewProduct(array $childProducts): void
101102
{
102103
$this->serRequestParams($childProducts);
103104
$this->dispatch('backend/catalog/product/save');
104-
$this->assertSessionMessages($this->equalTo(['You saved the product.']), MessageInterface::TYPE_SUCCESS);
105+
$this->assertSessionMessages($this->equalTo([__('You saved the product.')]), MessageInterface::TYPE_SUCCESS);
105106
$this->assertChildProducts($childProducts);
106107
$this->assertConfigurableOptions('configurable', $childProducts);
107108
$this->assertConfigurableLinks('configurable', $this->getProductIds(array_keys($childProducts)));
109+
$this->deleteProducts(array_merge(array_keys($childProducts), ['configurable']));
108110
}
109111

110112
/**
@@ -174,6 +176,7 @@ public function testSaveExistProduct(array $childProducts, array $associatedProd
174176
'configurable',
175177
$this->getProductIds(array_merge($associatedProducts, array_keys($childProducts)))
176178
);
179+
$this->deleteProducts(array_merge($associatedProducts, array_keys($childProducts)));
177180
}
178181

179182
/**
@@ -483,11 +486,11 @@ private function getDefaultAttributeSetId(): int
483486
*/
484487
private function getAttribute(string $attributeCode): ProductAttributeInterface
485488
{
486-
return $this->attributeRepository->get($attributeCode);
489+
return $this->productAttributeRepository->get($attributeCode);
487490
}
488491

489492
/**
490-
* Returns product ids by sku list.
493+
* Returns products by sku list.
491494
*
492495
* @param array $skuList
493496
* @return ProductInterface[]
@@ -517,4 +520,25 @@ private function getProductIds(array $skuList): array
517520

518521
return $associatedProductIds;
519522
}
523+
524+
/**
525+
* @param array $skuList
526+
* @return void
527+
*/
528+
private function deleteProducts(array $skuList): void
529+
{
530+
$this->registry->unregister('isSecureArea');
531+
$this->registry->register('isSecureArea', true);
532+
533+
foreach ($skuList as $sku) {
534+
try {
535+
$product = $this->productRepository->get($sku, false, null, true);
536+
$this->productRepository->delete($product);
537+
} catch (NoSuchEntityException $e) {
538+
//Product already removed
539+
}
540+
}
541+
$this->registry->unregister('isSecureArea');
542+
$this->registry->register('isSecureArea', false);
543+
}
520544
}

dev/tests/integration/testsuite/Magento/ConfigurableProduct/_files/product_configurable_with_one_simple.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,36 @@
55
*/
66
declare(strict_types=1);
77

8+
use Magento\Catalog\Api\Data\ProductExtensionInterfaceFactory;
89
use Magento\Catalog\Api\ProductRepositoryInterface;
910
use Magento\Catalog\Model\Product\Attribute\Source\Status;
1011
use Magento\Catalog\Model\Product\Type;
1112
use Magento\Catalog\Model\Product\Visibility;
1213
use Magento\Catalog\Model\ProductFactory;
1314
use Magento\ConfigurableProduct\Helper\Product\Options\Factory;
1415
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
16+
use Magento\Store\Api\WebsiteRepositoryInterface;
1517
use Magento\TestFramework\Helper\Bootstrap;
1618

1719
require __DIR__ . '/configurable_attribute.php';
1820
$objectManager = Bootstrap::getObjectManager();
21+
/** @var ProductRepositoryInterface $productRepository */
1922
$productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class);
20-
$productFactory = $objectManager->create(ProductFactory::class);
21-
$optionsFactory = $objectManager->create(Factory::class);
23+
/** @var ProductFactory $productFactory */
24+
$productFactory = $objectManager->get(ProductFactory::class);
25+
/** @var Factory $optionsFactory */
26+
$optionsFactory = $objectManager->get(Factory::class);
27+
/** @var ProductExtensionInterfaceFactory $productExtensionAttributes */
28+
$productExtensionAttributesFactory = $objectManager->get(ProductExtensionInterfaceFactory::class);
29+
/** @var WebsiteRepositoryInterface $websiteRepository */
30+
$websiteRepository = $objectManager->create(WebsiteRepositoryInterface::class);
31+
$defaultWebsiteId = $websiteRepository->get('base')->getId();
2232

2333
$option = $attribute->getSource()->getOptionId('Option 1');
2434
$product = $productFactory->create();
2535
$product->setTypeId(Type::TYPE_SIMPLE)
2636
->setAttributeSetId($product->getDefaultAttributeSetId())
37+
->setWebsiteIds([$defaultWebsiteId])
2738
->setName('Configurable Option 1')
2839
->setSku('simple_1')
2940
->setPrice(10.00)
@@ -44,15 +55,15 @@
4455
],
4556
]
4657
);
47-
$extensionConfigurableAttributes = $product->getExtensionAttributes();
58+
$extensionConfigurableAttributes = $product->getExtensionAttributes() ?: $productExtensionAttributesFactory->create();
4859
$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
4960
$extensionConfigurableAttributes->setConfigurableProductLinks([$product->getId()]);
5061

5162
$configurableProduct = $productFactory->create();
5263
$configurableProduct->setExtensionAttributes($extensionConfigurableAttributes);
5364
$configurableProduct->setTypeId(Configurable::TYPE_CODE)
5465
->setAttributeSetId($configurableProduct->getDefaultAttributeSetId())
55-
->setWebsiteIds([1])
66+
->setWebsiteIds([$defaultWebsiteId])
5667
->setName('Configurable Product')
5768
->setSku('configurable')
5869
->setVisibility(Visibility::VISIBILITY_BOTH)

dev/tests/integration/testsuite/Magento/ConfigurableProduct/_files/product_configurable_with_one_simple_rollback.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,22 @@
1111
use Magento\TestFramework\Helper\Bootstrap;
1212

1313
$objectManager = Bootstrap::getObjectManager();
14+
/** @var Registry $registry */
1415
$registry = $objectManager->get(Registry::class);
16+
/** @var ProductRepositoryInterface $productRepository */
1517
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
1618
$registry->unregister('isSecureArea');
1719
$registry->register('isSecureArea', true);
1820

19-
foreach (['simple_1','simple_2','simple_1_1','simple_1_2', 'simple_2_1','simple_2_2', 'configurable'] as $sku) {
21+
foreach (['simple_1', 'configurable'] as $sku) {
2022
try {
21-
$product = $productRepository->get($sku, false, null, true);
23+
$product = $productRepository->get($sku);
2224
$productRepository->delete($product);
2325
} catch (NoSuchEntityException $e) {
2426
//Product already removed
2527
}
2628
}
2729

28-
require __DIR__ . '/configurable_attribute_rollback.php';
2930
$registry->unregister('isSecureArea');
3031
$registry->register('isSecureArea', false);
32+
require __DIR__ . '/configurable_attribute_rollback.php';

0 commit comments

Comments
 (0)