|
| 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\ProductCustomOptionRepositoryInterface; |
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\Framework\App\Request\Http as HttpRequest; |
| 13 | +use Magento\Framework\Message\MessageInterface; |
| 14 | +use Magento\TestFramework\TestCase\AbstractBackendController; |
| 15 | + |
| 16 | +/** |
| 17 | + * Base test cases for product custom options with type "field". |
| 18 | + * Option add via dispatch product controller action save with options data in POST data. |
| 19 | + * |
| 20 | + * @magentoAppArea adminhtml |
| 21 | + * @magentoDbIsolation enabled |
| 22 | + */ |
| 23 | +class CreateCustomOptionsTest extends AbstractBackendController |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var ProductRepositoryInterface |
| 27 | + */ |
| 28 | + private $productRepository; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var ProductCustomOptionRepositoryInterface |
| 32 | + */ |
| 33 | + private $optionRepository; |
| 34 | + |
| 35 | + /** |
| 36 | + * @inheritDoc |
| 37 | + */ |
| 38 | + protected function setUp() |
| 39 | + { |
| 40 | + parent::setUp(); |
| 41 | + |
| 42 | + $this->productRepository = $this->_objectManager->create(ProductRepositoryInterface::class); |
| 43 | + $this->optionRepository = $this->_objectManager->create(ProductCustomOptionRepositoryInterface::class); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Test add to product custom option with type "field". |
| 48 | + * |
| 49 | + * @magentoDataFixture Magento/Catalog/_files/product_without_options.php |
| 50 | + * |
| 51 | + * @dataProvider productWithNewOptionsDataProvider |
| 52 | + * |
| 53 | + * @param array $productPostData |
| 54 | + */ |
| 55 | + public function testSaveCustomOptionWithTypeField(array $productPostData): void |
| 56 | + { |
| 57 | + $this->getRequest()->setPostValue($productPostData); |
| 58 | + $product = $this->productRepository->get('simple'); |
| 59 | + $this->getRequest()->setMethod(HttpRequest::METHOD_POST); |
| 60 | + $this->dispatch('backend/catalog/product/save/id/' . $product->getEntityId()); |
| 61 | + $this->assertSessionMessages( |
| 62 | + $this->contains('You saved the product.'), |
| 63 | + MessageInterface::TYPE_SUCCESS |
| 64 | + ); |
| 65 | + $productOptions = $this->optionRepository->getProductOptions($product); |
| 66 | + $this->assertCount(2, $productOptions); |
| 67 | + foreach ($productOptions as $customOption) { |
| 68 | + $postOptionData = $productPostData['product']['options'][$customOption->getTitle()] ?? null; |
| 69 | + $this->assertNotNull($postOptionData); |
| 70 | + $this->assertEquals($postOptionData['title'], $customOption->getTitle()); |
| 71 | + $this->assertEquals($postOptionData['type'], $customOption->getType()); |
| 72 | + $this->assertEquals($postOptionData['is_require'], $customOption->getIsRequire()); |
| 73 | + $this->assertEquals($postOptionData['sku'], $customOption->getSku()); |
| 74 | + $this->assertEquals($postOptionData['price'], $customOption->getPrice()); |
| 75 | + $this->assertEquals($postOptionData['price_type'], $customOption->getPriceType()); |
| 76 | + $maxCharacters = $postOptionData['max_characters'] ?? 0; |
| 77 | + $this->assertEquals($maxCharacters, $customOption->getMaxCharacters()); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Return all data for add option to product for all cases. |
| 83 | + * |
| 84 | + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
| 85 | + * |
| 86 | + * @return array |
| 87 | + */ |
| 88 | + public function productWithNewOptionsDataProvider(): array |
| 89 | + { |
| 90 | + return [ |
| 91 | + 'required_options' => [ |
| 92 | + [ |
| 93 | + 'product' => [ |
| 94 | + 'options' => [ |
| 95 | + 'Test option title 1' => [ |
| 96 | + 'record_id' => 0, |
| 97 | + 'sort_order' => 1, |
| 98 | + 'is_require' => 1, |
| 99 | + 'sku' => 'test-option-title-1', |
| 100 | + 'max_characters' => 50, |
| 101 | + 'title' => 'Test option title 1', |
| 102 | + 'type' => 'field', |
| 103 | + 'price' => 10, |
| 104 | + 'price_type' => 'fixed', |
| 105 | + ], |
| 106 | + 'Test option title 2' => [ |
| 107 | + 'record_id' => 1, |
| 108 | + 'sort_order' => 2, |
| 109 | + 'is_require' => 1, |
| 110 | + 'sku' => 'test-option-title-2', |
| 111 | + 'max_characters' => 50, |
| 112 | + 'title' => 'Test option title 2', |
| 113 | + 'type' => 'field', |
| 114 | + 'price' => 10, |
| 115 | + 'price_type' => 'fixed', |
| 116 | + ], |
| 117 | + ], |
| 118 | + ], |
| 119 | + ], |
| 120 | + ], |
| 121 | + 'not_required_options' => [ |
| 122 | + [ |
| 123 | + 'product' => [ |
| 124 | + 'options' => [ |
| 125 | + 'Test option title 1' => [ |
| 126 | + 'record_id' => 0, |
| 127 | + 'sort_order' => 1, |
| 128 | + 'is_require' => 0, |
| 129 | + 'sku' => 'test-option-title-1', |
| 130 | + 'max_characters' => 50, |
| 131 | + 'title' => 'Test option title 1', |
| 132 | + 'type' => 'field', |
| 133 | + 'price' => 10, |
| 134 | + 'price_type' => 'fixed', |
| 135 | + ], |
| 136 | + 'Test option title 2' => [ |
| 137 | + 'record_id' => 1, |
| 138 | + 'sort_order' => 2, |
| 139 | + 'is_require' => 0, |
| 140 | + 'sku' => 'test-option-title-2', |
| 141 | + 'max_characters' => 50, |
| 142 | + 'title' => 'Test option title 2', |
| 143 | + 'type' => 'field', |
| 144 | + 'price' => 10, |
| 145 | + 'price_type' => 'fixed', |
| 146 | + ], |
| 147 | + ], |
| 148 | + ], |
| 149 | + ], |
| 150 | + ], |
| 151 | + 'options_with_fixed_price' => [ |
| 152 | + [ |
| 153 | + 'product' => [ |
| 154 | + 'options' => [ |
| 155 | + 'Test option title 1' => [ |
| 156 | + 'record_id' => 0, |
| 157 | + 'sort_order' => 1, |
| 158 | + 'is_require' => 1, |
| 159 | + 'sku' => 'test-option-title-1', |
| 160 | + 'max_characters' => 50, |
| 161 | + 'title' => 'Test option title 1', |
| 162 | + 'type' => 'field', |
| 163 | + 'price' => 10, |
| 164 | + 'price_type' => 'fixed', |
| 165 | + ], |
| 166 | + 'Test option title 2' => [ |
| 167 | + 'record_id' => 1, |
| 168 | + 'sort_order' => 2, |
| 169 | + 'is_require' => 1, |
| 170 | + 'sku' => 'test-option-title-2', |
| 171 | + 'max_characters' => 50, |
| 172 | + 'title' => 'Test option title 2', |
| 173 | + 'type' => 'field', |
| 174 | + 'price' => 10, |
| 175 | + 'price_type' => 'percent', |
| 176 | + ], |
| 177 | + ], |
| 178 | + ], |
| 179 | + ], |
| 180 | + ], |
| 181 | + 'options_with_percent_price' => [ |
| 182 | + [ |
| 183 | + 'product' => [ |
| 184 | + 'options' => [ |
| 185 | + 'Test option title 1' => [ |
| 186 | + 'record_id' => 0, |
| 187 | + 'sort_order' => 1, |
| 188 | + 'is_require' => 1, |
| 189 | + 'sku' => 'test-option-title-1', |
| 190 | + 'max_characters' => 50, |
| 191 | + 'title' => 'Test option title 1', |
| 192 | + 'type' => 'field', |
| 193 | + 'price' => 10, |
| 194 | + 'price_type' => 'fixed', |
| 195 | + ], |
| 196 | + 'Test option title 2' => [ |
| 197 | + 'record_id' => 1, |
| 198 | + 'sort_order' => 2, |
| 199 | + 'is_require' => 1, |
| 200 | + 'sku' => 'test-option-title-2', |
| 201 | + 'max_characters' => 50, |
| 202 | + 'title' => 'Test option title 2', |
| 203 | + 'type' => 'field', |
| 204 | + 'price' => 20, |
| 205 | + 'price_type' => 'percent', |
| 206 | + ], |
| 207 | + ], |
| 208 | + ], |
| 209 | + ], |
| 210 | + ], |
| 211 | + 'options_with_max_charters_configuration' => [ |
| 212 | + [ |
| 213 | + 'product' => [ |
| 214 | + 'options' => [ |
| 215 | + 'Test option title 1' => [ |
| 216 | + 'record_id' => 0, |
| 217 | + 'sort_order' => 1, |
| 218 | + 'is_require' => 1, |
| 219 | + 'sku' => 'test-option-title-1', |
| 220 | + 'max_characters' => 30, |
| 221 | + 'title' => 'Test option title 1', |
| 222 | + 'type' => 'field', |
| 223 | + 'price' => 10, |
| 224 | + 'price_type' => 'fixed', |
| 225 | + ], |
| 226 | + 'Test option title 2' => [ |
| 227 | + 'record_id' => 1, |
| 228 | + 'sort_order' => 2, |
| 229 | + 'is_require' => 1, |
| 230 | + 'sku' => 'test-option-title-2', |
| 231 | + 'max_characters' => 50, |
| 232 | + 'title' => 'Test option title 2', |
| 233 | + 'type' => 'field', |
| 234 | + 'price' => 10, |
| 235 | + 'price_type' => 'fixed', |
| 236 | + ], |
| 237 | + ], |
| 238 | + ], |
| 239 | + ], |
| 240 | + ], |
| 241 | + 'options_without_max_charters_configuration' => [ |
| 242 | + [ |
| 243 | + 'product' => [ |
| 244 | + 'options' => [ |
| 245 | + 'Test option title 1' => [ |
| 246 | + 'record_id' => 0, |
| 247 | + 'sort_order' => 1, |
| 248 | + 'is_require' => 1, |
| 249 | + 'sku' => 'test-option-title-1', |
| 250 | + 'title' => 'Test option title 1', |
| 251 | + 'type' => 'field', |
| 252 | + 'price' => 10, |
| 253 | + 'price_type' => 'fixed', |
| 254 | + ], |
| 255 | + 'Test option title 2' => [ |
| 256 | + 'record_id' => 1, |
| 257 | + 'sort_order' => 2, |
| 258 | + 'is_require' => 1, |
| 259 | + 'sku' => 'test-option-title-2', |
| 260 | + 'title' => 'Test option title 2', |
| 261 | + 'type' => 'field', |
| 262 | + 'price' => 10, |
| 263 | + 'price_type' => 'fixed', |
| 264 | + ], |
| 265 | + ], |
| 266 | + ], |
| 267 | + ], |
| 268 | + ], |
| 269 | + ]; |
| 270 | + } |
| 271 | +} |
0 commit comments