|
| 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\Swatches\Model\Plugin; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductAttributeRepositoryInterface; |
| 11 | +use Magento\Catalog\Model\ResourceModel\Eav\Attribute; |
| 12 | +use Magento\Framework\ObjectManagerInterface; |
| 13 | +use Magento\Swatches\Model\ResourceModel\Swatch as SwatchResource; |
| 14 | +use Magento\Swatches\Model\Swatch; |
| 15 | +use Magento\TestFramework\Helper\Bootstrap; |
| 16 | +use Magento\TestFramework\Interception\PluginList; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +/** |
| 20 | + * Checks swatches attribute save behaviour |
| 21 | + * |
| 22 | + * @see \Magento\Swatches\Model\Plugin\EavAttribute |
| 23 | + * |
| 24 | + * @magentoAppArea adminhtml |
| 25 | + * @magentoDbIsolation disabled |
| 26 | + */ |
| 27 | +class EavAttributeTest extends TestCase |
| 28 | +{ |
| 29 | + /** @var ObjectManagerInterface */ |
| 30 | + private $objectManager; |
| 31 | + |
| 32 | + /** @var ProductAttributeRepositoryInterface */ |
| 33 | + private $productAttributeRepository; |
| 34 | + |
| 35 | + /** @var SwatchResource */ |
| 36 | + private $swatchResource; |
| 37 | + |
| 38 | + /** |
| 39 | + * @inheritdoc |
| 40 | + */ |
| 41 | + protected function setUp(): void |
| 42 | + { |
| 43 | + parent::setUp(); |
| 44 | + |
| 45 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 46 | + $this->productAttributeRepository = $this->objectManager->get(ProductAttributeRepositoryInterface::class); |
| 47 | + $this->swatchResource = $this->objectManager->get(SwatchResource::class); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @return void |
| 52 | + */ |
| 53 | + public function testEavAttributePluginIsRegistered(): void |
| 54 | + { |
| 55 | + $pluginInfo = $this->objectManager->get(PluginList::class)->get(Attribute::class); |
| 56 | + $this->assertSame(EavAttribute::class, $pluginInfo['save_swatches_option_params']['instance']); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @magentoDataFixture Magento/Swatches/_files/text_swatch_attribute.php |
| 61 | + * |
| 62 | + * @return void |
| 63 | + */ |
| 64 | + public function testChangeAttributeToDropdown(): void |
| 65 | + { |
| 66 | + $attribute = $this->productAttributeRepository->get('test_configurable'); |
| 67 | + $options = $attribute->getOptions(); |
| 68 | + unset($options[0]); |
| 69 | + $optionsIds = $this->collectOptionsIds($options); |
| 70 | + $attribute->addData($this->prepareOptions($options)); |
| 71 | + $attribute->setData(Swatch::SWATCH_INPUT_TYPE_KEY, Swatch::SWATCH_INPUT_TYPE_DROPDOWN); |
| 72 | + $attribute->beforeSave(); |
| 73 | + $this->assertEmpty($this->fetchSwatchOptions($optionsIds), 'Swatch options were not deleted'); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Prepare options |
| 78 | + * |
| 79 | + * @param array $options |
| 80 | + * @return array |
| 81 | + */ |
| 82 | + private function prepareOptions(array $options): array |
| 83 | + { |
| 84 | + foreach ($options as $key => $option) { |
| 85 | + $preparedOptions['option']['order'][$option->getValue()] = $key; |
| 86 | + $preparedOptions['option']['value'][$option->getValue()] = [$option->getLabel()]; |
| 87 | + $preparedOptions['option']['delete'][$option->getValue()] = ''; |
| 88 | + } |
| 89 | + |
| 90 | + return $preparedOptions ?? []; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Collect options ids |
| 95 | + * |
| 96 | + * @param array $options |
| 97 | + * @return array |
| 98 | + */ |
| 99 | + private function collectOptionsIds(array $options): array |
| 100 | + { |
| 101 | + foreach ($options as $option) { |
| 102 | + $optionsIds[] = $option->getValue(); |
| 103 | + } |
| 104 | + |
| 105 | + return $optionsIds ?? []; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Fetch related to provided ids records from eav_attribute_option_swatch table |
| 110 | + * |
| 111 | + * @param $optionsIds |
| 112 | + * @return array |
| 113 | + */ |
| 114 | + private function fetchSwatchOptions(array $optionsIds): array |
| 115 | + { |
| 116 | + $connection = $this->swatchResource->getConnection(); |
| 117 | + $select = $connection->select()->from(['main_table' => $this->swatchResource->getMainTable()]) |
| 118 | + ->where('main_table.option_id IN (?)', $optionsIds); |
| 119 | + |
| 120 | + return $connection->fetchAll($select); |
| 121 | + } |
| 122 | +} |
0 commit comments