|
| 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\Bundle\Controller\Adminhtml\Product; |
| 9 | + |
| 10 | +use Magento\Bundle\Model\Product\Type; |
| 11 | +use Magento\Bundle\Model\ResourceModel\Option\Collection as OptionCollection; |
| 12 | +use Magento\Bundle\Model\ResourceModel\Selection\Collection as SelectionCollection; |
| 13 | +use Magento\Catalog\Api\Data\ProductAttributeInterface; |
| 14 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 15 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 16 | +use Magento\Catalog\Model\ResourceModel\Product as ProductResource; |
| 17 | +use Magento\Eav\Model\Config; |
| 18 | +use Magento\Framework\App\Request\Http as HttpRequest; |
| 19 | +use Magento\TestFramework\TestCase\AbstractBackendController; |
| 20 | + |
| 21 | +/** |
| 22 | + * Class determine basic logic for bundle product save tests |
| 23 | + */ |
| 24 | +abstract class AbstractBundleProductSaveTest extends AbstractBackendController |
| 25 | +{ |
| 26 | + /** @var string */ |
| 27 | + protected $productToDelete; |
| 28 | + |
| 29 | + /** @var ProductRepositoryInterface */ |
| 30 | + protected $productRepository; |
| 31 | + |
| 32 | + /** @var Config */ |
| 33 | + private $eavConfig; |
| 34 | + |
| 35 | + /** @var ProductResource */ |
| 36 | + private $productResource; |
| 37 | + |
| 38 | + /** |
| 39 | + * @inheritdoc |
| 40 | + */ |
| 41 | + protected function setUp() |
| 42 | + { |
| 43 | + parent::setUp(); |
| 44 | + |
| 45 | + $this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class); |
| 46 | + $this->eavConfig = $this->_objectManager->get(Config::class); |
| 47 | + $this->productResource = $this->_objectManager->get(ProductResource::class); |
| 48 | + $this->productToDelete = $this->getStaticProductData()['sku']; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @inheritdoc |
| 53 | + */ |
| 54 | + protected function tearDown() |
| 55 | + { |
| 56 | + if ($this->productToDelete) { |
| 57 | + $this->productRepository->deleteById($this->productToDelete); |
| 58 | + } |
| 59 | + |
| 60 | + parent::tearDown(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Retrieve default product attribute set id. |
| 65 | + * |
| 66 | + * @return int |
| 67 | + */ |
| 68 | + protected function getDefaultAttributeSetId(): int |
| 69 | + { |
| 70 | + return (int)$this->eavConfig->getEntityType(ProductAttributeInterface::ENTITY_TYPE_CODE) |
| 71 | + ->getDefaultAttributeSetId(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Prepare request |
| 76 | + * |
| 77 | + * @param array $post |
| 78 | + * @param int|null $id |
| 79 | + * @return array |
| 80 | + */ |
| 81 | + protected function prepareRequestData(array $post, ?int $id = null): array |
| 82 | + { |
| 83 | + $post = $this->preparePostParams($post); |
| 84 | + $this->setRequestparams($post, $id); |
| 85 | + |
| 86 | + return $post; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Prepare and assert bundle options |
| 91 | + * |
| 92 | + * @param array $bundleOptions |
| 93 | + * @return void |
| 94 | + */ |
| 95 | + protected function assertBundleOptions(array $bundleOptions): void |
| 96 | + { |
| 97 | + $mainProduct = $this->productRepository->get($this->getStaticProductData()['sku'], false, null, true); |
| 98 | + $optionsCollection = $mainProduct->getTypeInstance()->getOptionsCollection($mainProduct); |
| 99 | + $selectionCollection = $mainProduct->getTypeInstance() |
| 100 | + ->getSelectionsCollection($optionsCollection->getAllIds(), $mainProduct); |
| 101 | + $this->assertOptionsData($bundleOptions, $optionsCollection, $selectionCollection); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Prepare post params before dispatch |
| 106 | + * |
| 107 | + * @param array $post |
| 108 | + * @return array |
| 109 | + */ |
| 110 | + private function preparePostParams(array $post): array |
| 111 | + { |
| 112 | + $post['product'] = $this->getStaticProductData(); |
| 113 | + foreach ($post['bundle_options']['bundle_options'] as &$bundleOption) { |
| 114 | + $bundleOption = $this->prepareOptionByType($bundleOption['type'], $bundleOption); |
| 115 | + $productIdsBySkus = $this->productResource->getProductsIdsBySkus( |
| 116 | + array_column($bundleOption['bundle_selections'], 'sku') |
| 117 | + ); |
| 118 | + foreach ($bundleOption['bundle_selections'] as &$bundleSelection) { |
| 119 | + $bundleSelection = $this->prepareSelection($productIdsBySkus, $bundleSelection); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return $post; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Prepare option params |
| 128 | + * |
| 129 | + * @param string $type |
| 130 | + * @param array $option |
| 131 | + * @return array |
| 132 | + */ |
| 133 | + private function prepareOptionByType(string $type, array $option): array |
| 134 | + { |
| 135 | + $option['required'] = '1'; |
| 136 | + $option['delete'] = ''; |
| 137 | + $option['title'] = $option['title'] ?? $type . ' Option Title'; |
| 138 | + |
| 139 | + return $option; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Prepare selection params |
| 144 | + * |
| 145 | + * @param array $productIdsBySkus |
| 146 | + * @param array $selection |
| 147 | + * @return array |
| 148 | + */ |
| 149 | + private function prepareSelection(array $productIdsBySkus, array $selection): array |
| 150 | + { |
| 151 | + $staticData = [ |
| 152 | + 'price' => '10', |
| 153 | + 'selection_qty' => '5', |
| 154 | + 'selection_can_change_qty' => '0' |
| 155 | + ]; |
| 156 | + $selection['product_id'] = $productIdsBySkus[$selection['sku']]; |
| 157 | + $selection = array_merge($selection, $staticData); |
| 158 | + |
| 159 | + return $selection; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Assert bundle options data |
| 164 | + * |
| 165 | + * @param array $expectedOptions |
| 166 | + * @param OptionCollection $actualOptions |
| 167 | + * @param SelectionCollection $selectionCollection |
| 168 | + * @return void |
| 169 | + */ |
| 170 | + private function assertOptionsData( |
| 171 | + array $expectedOptions, |
| 172 | + OptionCollection $actualOptions, |
| 173 | + SelectionCollection $selectionCollection |
| 174 | + ): void { |
| 175 | + $this->assertCount(count($expectedOptions['bundle_options']), $actualOptions); |
| 176 | + foreach ($expectedOptions['bundle_options'] as $expectedOption) { |
| 177 | + $optionToCheck = $actualOptions->getItemByColumnValue('title', $expectedOption['title']); |
| 178 | + $this->assertNotNull($optionToCheck->getId()); |
| 179 | + $selectionToCheck = $selectionCollection->getItemsByColumnValue('option_id', $optionToCheck->getId()); |
| 180 | + $this->assertCount(count($expectedOption['bundle_selections']), $selectionToCheck); |
| 181 | + $this->assertSelections($expectedOption['bundle_selections'], $selectionToCheck); |
| 182 | + unset($expectedOption['delete'], $expectedOption['bundle_selections']); |
| 183 | + foreach ($expectedOption as $key => $value) { |
| 184 | + $this->assertEquals($value, $optionToCheck->getData($key)); |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Assert selections data |
| 191 | + * |
| 192 | + * @param array $expectedSelections |
| 193 | + * @param array $actualSelections |
| 194 | + * @return void |
| 195 | + */ |
| 196 | + private function assertSelections(array $expectedSelections, array $actualSelections): void |
| 197 | + { |
| 198 | + foreach ($expectedSelections as $expectedSelection) { |
| 199 | + $actualSelectionToCheck = $this->getSelectionByProductSku($expectedSelection['sku'], $actualSelections); |
| 200 | + $this->assertNotNull($actualSelectionToCheck); |
| 201 | + foreach ($expectedSelection as $key => $value) { |
| 202 | + $this->assertEquals($value, $actualSelectionToCheck->getData($key)); |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Get selection by product sku |
| 209 | + * |
| 210 | + * @param string $sku |
| 211 | + * @param array $actualSelections |
| 212 | + * @return ProductInterface |
| 213 | + */ |
| 214 | + private function getSelectionByProductSku(string $sku, array $actualSelections): ProductInterface |
| 215 | + { |
| 216 | + $item = null; |
| 217 | + foreach ($actualSelections as $selection) { |
| 218 | + if ($selection->getSku() === $sku) { |
| 219 | + $item = $selection; |
| 220 | + break; |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + return $item; |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Set request parameters |
| 229 | + * |
| 230 | + * @param array $post |
| 231 | + * @param int|null $id |
| 232 | + * @return void |
| 233 | + */ |
| 234 | + private function setRequestParams(array $post, ?int $id): void |
| 235 | + { |
| 236 | + $this->getRequest()->setMethod(HttpRequest::METHOD_POST); |
| 237 | + $params = ['type' => Type::TYPE_CODE, 'set' => $this->getDefaultAttributeSetId()]; |
| 238 | + if ($id) { |
| 239 | + $params['id'] = $id; |
| 240 | + } |
| 241 | + $this->getRequest()->setParams($params); |
| 242 | + $this->getRequest()->setPostValue('product', $post['product']); |
| 243 | + $this->getRequest()->setPostValue('bundle_options', $post['bundle_options']); |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * Get main product data |
| 248 | + * |
| 249 | + * @return array |
| 250 | + */ |
| 251 | + abstract protected function getStaticProductData(): array; |
| 252 | +} |
0 commit comments