Skip to content

Commit 36262f1

Browse files
committed
Merge remote-tracking branch 'origin/MC-20670' into 2.3-develop-com-pr5
2 parents a74a8a8 + 18b2db6 commit 36262f1

File tree

2 files changed

+318
-0
lines changed

2 files changed

+318
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Controller\Adminhtml\Product\Save;
9+
10+
use Magento\Catalog\Api\Data\ProductCustomOptionInterface;
11+
use Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory;
12+
use Magento\Catalog\Api\ProductCustomOptionRepositoryInterface;
13+
use Magento\Catalog\Api\ProductRepositoryInterface;
14+
use Magento\Framework\App\Request\Http as HttpRequest;
15+
use Magento\Framework\Message\MessageInterface;
16+
use Magento\TestFramework\TestCase\AbstractBackendController;
17+
18+
/**
19+
* Base test cases for delete product custom option with type "field".
20+
* Option deleting via product controller action save.
21+
*
22+
* @magentoAppArea adminhtml
23+
* @magentoDbIsolation enabled
24+
*/
25+
class DeleteCustomOptionsTest extends AbstractBackendController
26+
{
27+
/**
28+
* @var ProductRepositoryInterface
29+
*/
30+
private $productRepository;
31+
32+
/**
33+
* @var ProductCustomOptionRepositoryInterface
34+
*/
35+
private $optionRepository;
36+
37+
/**
38+
* @var ProductCustomOptionInterfaceFactory
39+
*/
40+
private $optionRepositoryFactory;
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
protected function setUp()
46+
{
47+
parent::setUp();
48+
49+
$this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
50+
$this->optionRepository = $this->_objectManager->get(ProductCustomOptionRepositoryInterface::class);
51+
$this->optionRepositoryFactory = $this->_objectManager->get(ProductCustomOptionInterfaceFactory::class);
52+
}
53+
54+
/**
55+
* Test delete custom option with type "field".
56+
*
57+
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
58+
*
59+
* @dataProvider \Magento\TestFramework\Catalog\Model\Product\Option\DataProvider\Type\Field::getDataForCreateOptions
60+
*
61+
* @param array $optionData
62+
* @return void
63+
*/
64+
public function testDeleteCustomOptionWithTypeField(array $optionData): void
65+
{
66+
$product = $this->productRepository->get('simple');
67+
/** @var ProductCustomOptionInterface $option */
68+
$option = $this->optionRepositoryFactory->create(['data' => $optionData]);
69+
$option->setProductSku($product->getSku());
70+
$product->setOptions([$option]);
71+
$this->productRepository->save($product);
72+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
73+
$this->dispatch('backend/catalog/product/save/id/' . $product->getEntityId());
74+
$this->assertSessionMessages(
75+
$this->equalTo([(string)__('You saved the product.')]),
76+
MessageInterface::TYPE_SUCCESS
77+
);
78+
$this->assertCount(0, $this->optionRepository->getProductOptions($product));
79+
}
80+
}
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Model\Product;
9+
10+
use Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory;
11+
use Magento\Catalog\Api\Data\ProductCustomOptionValuesInterfaceFactory;
12+
use Magento\Catalog\Api\ProductCustomOptionRepositoryInterface;
13+
use Magento\Catalog\Api\ProductRepositoryInterface;
14+
use Magento\Framework\ObjectManagerInterface;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* Test delete product custom options.
20+
* Testing option types: "Area", "File", "Drop-down", "Radio-Buttons",
21+
* "Checkbox", "Multiple Select", "Date", "Date & Time" and "Time".
22+
*
23+
* @magentoDbIsolation enabled
24+
*/
25+
class DeleteCustomOptionsTest extends TestCase
26+
{
27+
/**
28+
* @var ObjectManagerInterface
29+
*/
30+
private $objectManager;
31+
32+
/**
33+
* @var ProductRepositoryInterface
34+
*/
35+
private $productRepository;
36+
37+
/**
38+
* @var ProductCustomOptionRepositoryInterface
39+
*/
40+
private $optionRepository;
41+
42+
/**
43+
* @var ProductCustomOptionInterfaceFactory
44+
*/
45+
private $customOptionFactory;
46+
47+
/**
48+
* @var ProductCustomOptionValuesInterfaceFactory
49+
*/
50+
private $customOptionValueFactory;
51+
52+
/**
53+
* @inheritdoc
54+
*/
55+
protected function setUp()
56+
{
57+
$this->objectManager = Bootstrap::getObjectManager();
58+
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
59+
$this->optionRepository = $this->objectManager->get(ProductCustomOptionRepositoryInterface::class);
60+
$this->customOptionFactory = $this->objectManager->get(ProductCustomOptionInterfaceFactory::class);
61+
$this->customOptionValueFactory = $this->objectManager
62+
->get(ProductCustomOptionValuesInterfaceFactory::class);
63+
64+
parent::setUp();
65+
}
66+
67+
/**
68+
* Test delete product custom options with type "area".
69+
*
70+
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
71+
*
72+
* @dataProvider \Magento\TestFramework\Catalog\Model\Product\Option\DataProvider\Type\Area::getDataForCreateOptions()
73+
*
74+
* @param array $optionData
75+
* @return void
76+
*/
77+
public function testDeleteAreaCustomOption(array $optionData): void
78+
{
79+
$this->deleteAndAssertNotSelectCustomOptions($optionData);
80+
}
81+
82+
/**
83+
* Test delete product custom options with type "file".
84+
*
85+
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
86+
*
87+
* @dataProvider \Magento\TestFramework\Catalog\Model\Product\Option\DataProvider\Type\File::getDataForCreateOptions()
88+
*
89+
* @param array $optionData
90+
* @return void
91+
*/
92+
public function testDeleteFileCustomOption(array $optionData): void
93+
{
94+
$this->deleteAndAssertNotSelectCustomOptions($optionData);
95+
}
96+
97+
/**
98+
* Test delete product custom options with type "Date".
99+
*
100+
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
101+
*
102+
* @dataProvider \Magento\TestFramework\Catalog\Model\Product\Option\DataProvider\Type\Date::getDataForCreateOptions()
103+
*
104+
* @param array $optionData
105+
* @return void
106+
*/
107+
public function testDeleteDateCustomOption(array $optionData): void
108+
{
109+
$this->deleteAndAssertNotSelectCustomOptions($optionData);
110+
}
111+
112+
/**
113+
* Test delete product custom options with type "Date & Time".
114+
*
115+
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
116+
*
117+
* @dataProvider \Magento\TestFramework\Catalog\Model\Product\Option\DataProvider\Type\DateTime::getDataForCreateOptions()
118+
*
119+
* @param array $optionData
120+
* @return void
121+
*/
122+
public function testDeleteDateTimeCustomOption(array $optionData): void
123+
{
124+
$this->deleteAndAssertNotSelectCustomOptions($optionData);
125+
}
126+
127+
/**
128+
* Test delete product custom options with type "Time".
129+
*
130+
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
131+
*
132+
* @dataProvider \Magento\TestFramework\Catalog\Model\Product\Option\DataProvider\Type\Time::getDataForCreateOptions()
133+
*
134+
* @param array $optionData
135+
* @return void
136+
*/
137+
public function testDeleteTimeCustomOption(array $optionData): void
138+
{
139+
$this->deleteAndAssertNotSelectCustomOptions($optionData);
140+
}
141+
142+
/**
143+
* Test delete product custom options with type "Drop-down".
144+
*
145+
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
146+
*
147+
* @dataProvider \Magento\TestFramework\Catalog\Model\Product\Option\DataProvider\Type\DropDown::getDataForCreateOptions()
148+
*
149+
* @param array $optionData
150+
* @param array $optionValueData
151+
* @return void
152+
*/
153+
public function testDeleteDropDownCustomOption(array $optionData, array $optionValueData): void
154+
{
155+
$this->deleteAndAssertSelectCustomOptions($optionData, $optionValueData);
156+
}
157+
158+
/**
159+
* Test delete product custom options with type "Radio Buttons".
160+
*
161+
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
162+
*
163+
* @dataProvider \Magento\TestFramework\Catalog\Model\Product\Option\DataProvider\Type\RadioButtons::getDataForCreateOptions()
164+
*
165+
* @param array $optionData
166+
* @param array $optionValueData
167+
* @return void
168+
*/
169+
public function testDeleteRadioButtonsCustomOption(array $optionData, array $optionValueData): void
170+
{
171+
$this->deleteAndAssertSelectCustomOptions($optionData, $optionValueData);
172+
}
173+
174+
/**
175+
* Test delete product custom options with type "Checkbox".
176+
*
177+
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
178+
*
179+
* @dataProvider \Magento\TestFramework\Catalog\Model\Product\Option\DataProvider\Type\Checkbox::getDataForCreateOptions()
180+
*
181+
* @param array $optionData
182+
* @param array $optionValueData
183+
* @return void
184+
*/
185+
public function testDeleteCheckboxCustomOption(array $optionData, array $optionValueData): void
186+
{
187+
$this->deleteAndAssertSelectCustomOptions($optionData, $optionValueData);
188+
}
189+
190+
/**
191+
* Test delete product custom options with type "Multiple Select".
192+
*
193+
* @magentoDataFixture Magento/Catalog/_files/product_without_options.php
194+
*
195+
* @dataProvider \Magento\TestFramework\Catalog\Model\Product\Option\DataProvider\Type\MultipleSelect::getDataForCreateOptions()
196+
*
197+
* @param array $optionData
198+
* @param array $optionValueData
199+
* @return void
200+
*/
201+
public function testDeleteMultipleSelectCustomOption(array $optionData, array $optionValueData): void
202+
{
203+
$this->deleteAndAssertSelectCustomOptions($optionData, $optionValueData);
204+
}
205+
206+
/**
207+
* Delete product custom options which are not from "select" group and assert that option was deleted.
208+
*
209+
* @param array $optionData
210+
* @return void
211+
*/
212+
private function deleteAndAssertNotSelectCustomOptions(array $optionData): void
213+
{
214+
$product = $this->productRepository->get('simple');
215+
$createdOption = $this->customOptionFactory->create(['data' => $optionData]);
216+
$createdOption->setProductSku($product->getSku());
217+
$product->setOptions([$createdOption]);
218+
$this->productRepository->save($product);
219+
$this->assertCount(1, $this->optionRepository->getProductOptions($product));
220+
$product->setOptions([]);
221+
$this->productRepository->save($product);
222+
$this->assertCount(0, $this->optionRepository->getProductOptions($product));
223+
}
224+
225+
/**
226+
* Delete product custom options which from "select" group and assert that option was deleted.
227+
*
228+
* @param array $optionData
229+
* @param array $optionValueData
230+
* @return void
231+
*/
232+
private function deleteAndAssertSelectCustomOptions(array $optionData, array $optionValueData): void
233+
{
234+
$optionValue = $this->customOptionValueFactory->create(['data' => $optionValueData]);
235+
$optionData['values'] = [$optionValue];
236+
$this->deleteAndAssertNotSelectCustomOptions($optionData);
237+
}
238+
}

0 commit comments

Comments
 (0)