Skip to content

Commit

Permalink
MC-20670: Delete custom options of simple product
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanPletnyov committed Nov 5, 2019
1 parent d5104d5 commit 3b8c827
Show file tree
Hide file tree
Showing 2 changed files with 313 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Controller\Adminhtml\Product\Save;

use Magento\Catalog\Api\Data\ProductCustomOptionInterface;
use Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory;
use Magento\Catalog\Api\ProductCustomOptionRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product\Option;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\TestFramework\TestCase\AbstractBackendController;

/**
* Base test cases for delete product custom option with type "field".
* Option deleting via product controller action save.
*
* @magentoAppArea adminhtml
* @magentoDbIsolation enabled
*/
class DeleteCustomOptionsTest extends AbstractBackendController
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @var ProductCustomOptionRepositoryInterface
*/
private $optionRepository;

/**
* @var ProductCustomOptionInterfaceFactory
*/
private $optionRepositoryFactory;

/**
* @inheritdoc
*/
protected function setUp()
{
parent::setUp();

$this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
$this->optionRepository = $this->_objectManager->get(ProductCustomOptionRepositoryInterface::class);
$this->optionRepositoryFactory = $this->_objectManager->get(ProductCustomOptionInterfaceFactory::class);
}

/**
* Test delete custom option with type "field".
*
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
*
* @dataProvider \Magento\TestFramework\Catalog\Model\Product\Option\DataProvider\Type\Field::getDataForCreateOptions
*
* @param array $optionData
* @return void
*/
public function testDeleteCustomOptionWithTypeField(array $optionData): void
{
$product = $this->productRepository->get('simple');
/** @var ProductCustomOptionInterface|Option $option */
$option = $this->optionRepositoryFactory->create(['data' => $optionData]);
$option->setProductSku($product->getSku());
$product->setOptions([$option]);
$this->productRepository->save($product);
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$this->dispatch('backend/catalog/product/save/id/' . $product->getEntityId());
$this->assertCount(0, $this->optionRepository->getProductOptions($product));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\Product;

use Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory;
use Magento\Catalog\Api\Data\ProductCustomOptionValuesInterfaceFactory;
use Magento\Catalog\Api\ProductCustomOptionRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* Test delete product custom options.
* Testing option types: "Area", "File", "Drop-down", "Radio-Buttons",
* "Checkbox", "Multiple Select", "Date", "Date & Time" and "Time".
*
* @magentoDbIsolation enabled
*/
class DeleteCustomOptionsTest extends TestCase
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @var ProductCustomOptionRepositoryInterface
*/
private $optionRepository;

/**
* @var ProductCustomOptionInterfaceFactory
*/
private $customOptionFactory;

/**
* @var ProductCustomOptionValuesInterfaceFactory
*/
private $customOptionValueFactory;

/**
* @inheritdoc
*/
protected function setUp()
{
$this->objectManager = Bootstrap::getObjectManager();
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$this->optionRepository = $this->objectManager->get(ProductCustomOptionRepositoryInterface::class);
$this->customOptionFactory = $this->objectManager->get(ProductCustomOptionInterfaceFactory::class);
$this->customOptionValueFactory = $this->objectManager
->get(ProductCustomOptionValuesInterfaceFactory::class);

parent::setUp();
}

/**
* Test delete product custom options with type "area".
*
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
*
* @dataProvider \Magento\TestFramework\Catalog\Model\Product\Option\DataProvider\Type\Area::getDataForCreateOptions()
*
* @param array $optionData
* @return void
*/
public function testDeleteAreaCustomOption(array $optionData): void
{
$this->deleteAndAssertNotSelectCustomOptions($optionData);
}

/**
* Test delete product custom options with type "file".
*
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
*
* @dataProvider \Magento\TestFramework\Catalog\Model\Product\Option\DataProvider\Type\File::getDataForCreateOptions()
*
* @param array $optionData
* @return void
*/
public function testDeleteFileCustomOption(array $optionData): void
{
$this->deleteAndAssertNotSelectCustomOptions($optionData);
}

/**
* Test delete product custom options with type "Date".
*
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
*
* @dataProvider \Magento\TestFramework\Catalog\Model\Product\Option\DataProvider\Type\Date::getDataForCreateOptions()
*
* @param array $optionData
* @return void
*/
public function testDeleteDateCustomOption(array $optionData): void
{
$this->deleteAndAssertNotSelectCustomOptions($optionData);
}

/**
* Test delete product custom options with type "Date & Time".
*
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
*
* @dataProvider \Magento\TestFramework\Catalog\Model\Product\Option\DataProvider\Type\DateTime::getDataForCreateOptions()
*
* @param array $optionData
* @return void
*/
public function testDeleteDateTimeCustomOption(array $optionData): void
{
$this->deleteAndAssertNotSelectCustomOptions($optionData);
}

/**
* Test delete product custom options with type "Time".
*
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
*
* @dataProvider \Magento\TestFramework\Catalog\Model\Product\Option\DataProvider\Type\Time::getDataForCreateOptions()
*
* @param array $optionData
* @return void
*/
public function testDeleteTimeCustomOption(array $optionData): void
{
$this->deleteAndAssertNotSelectCustomOptions($optionData);
}

/**
* Test delete product custom options with type "Drop-down".
*
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
*
* @dataProvider \Magento\TestFramework\Catalog\Model\Product\Option\DataProvider\Type\DropDown::getDataForCreateOptions()
*
* @param array $optionData
* @param array $optionValueData
* @return void
*/
public function testDeleteDropDownCustomOption(array $optionData, array $optionValueData): void
{
$this->deleteAndAssertSelectCustomOptions($optionData, $optionValueData);
}

/**
* Test delete product custom options with type "Radio Buttons".
*
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
*
* @dataProvider \Magento\TestFramework\Catalog\Model\Product\Option\DataProvider\Type\RadioButtons::getDataForCreateOptions()
*
* @param array $optionData
* @param array $optionValueData
* @return void
*/
public function testDeleteRadioButtonsCustomOption(array $optionData, array $optionValueData): void
{
$this->deleteAndAssertSelectCustomOptions($optionData, $optionValueData);
}

/**
* Test delete product custom options with type "Checkbox".
*
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
*
* @dataProvider \Magento\TestFramework\Catalog\Model\Product\Option\DataProvider\Type\Checkbox::getDataForCreateOptions()
*
* @param array $optionData
* @param array $optionValueData
* @return void
*/
public function testDeleteCheckboxCustomOption(array $optionData, array $optionValueData): void
{
$this->deleteAndAssertSelectCustomOptions($optionData, $optionValueData);
}

/**
* Test delete product custom options with type "Multiple Select".
*
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
*
* @dataProvider \Magento\TestFramework\Catalog\Model\Product\Option\DataProvider\Type\MultipleSelect::getDataForCreateOptions()
*
* @param array $optionData
* @param array $optionValueData
* @return void
*/
public function testDeleteMultipleSelectCustomOption(array $optionData, array $optionValueData): void
{
$this->deleteAndAssertSelectCustomOptions($optionData, $optionValueData);
}

/**
* Delete product custom options which are not from "select" group and assert that option was deleted.
*
* @param array $optionData
* @return void
*/
private function deleteAndAssertNotSelectCustomOptions(array $optionData): void
{
$product = $this->productRepository->get('simple');
$createdOption = $this->customOptionFactory->create(['data' => $optionData]);
$createdOption->setProductSku($product->getSku());
$product->setOptions([$createdOption]);
$this->productRepository->save($product);
$product->setOptions([]);
$this->productRepository->save($product);
$this->assertCount(0, $this->optionRepository->getProductOptions($product));
}

/**
* Delete product custom options which from "select" group and assert that option was deleted.
*
* @param array $optionData
* @param array $optionValueData
* @return void
*/
private function deleteAndAssertSelectCustomOptions(array $optionData, array $optionValueData): void
{
$optionValue = $this->customOptionValueFactory->create(['data' => $optionValueData]);
$optionData['values'] = [$optionValue];
$this->deleteAndAssertNotSelectCustomOptions($optionData);
}
}

0 comments on commit 3b8c827

Please sign in to comment.