|
| 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