Skip to content

Commit 56f7795

Browse files
committed
MC-39706: Create automated test for: "Change a text swatch attribute to dropdown attribute"
1 parent 800acfa commit 56f7795

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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\TestFramework\Helper\Bootstrap;
15+
use Magento\TestFramework\Interception\PluginList;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* Checks swatches attribute save behaviour
20+
*
21+
* @see \Magento\Swatches\Model\Plugin\EavAttribute
22+
*
23+
* @magentoAppArea adminhtml
24+
* @magentoDbIsolation disabled
25+
*/
26+
class EavAttributeTest extends TestCase
27+
{
28+
/** @var ObjectManagerInterface */
29+
private $objectManager;
30+
31+
/** @var ProductAttributeRepositoryInterface */
32+
private $productAttributeRepository;
33+
34+
/** @var SwatchResource */
35+
private $swatchResource;
36+
37+
/**
38+
* @inheritdoc
39+
*/
40+
protected function setUp(): void
41+
{
42+
parent::setUp();
43+
44+
$this->objectManager = Bootstrap::getObjectManager();
45+
$this->productAttributeRepository = $this->objectManager->get(ProductAttributeRepositoryInterface::class);
46+
$this->swatchResource = $this->objectManager->get(SwatchResource::class);
47+
}
48+
49+
/**
50+
* @return void
51+
*/
52+
public function testEavAttributePluginIsRegistered()
53+
{
54+
$pluginInfo = $this->objectManager->get(PluginList::class)->get(Attribute::class);
55+
$this->assertSame(EavAttribute::class, $pluginInfo['save_swatches_option_params']['instance']);
56+
}
57+
58+
/**
59+
* @magentoDataFixture Magento/Swatches/_files/text_swatch_attribute.php
60+
*
61+
* @return void
62+
*/
63+
public function testChangeAttributeToDropdown(): void
64+
{
65+
$attribute = $this->productAttributeRepository->get('test_configurable');
66+
$options = $attribute->getOptions();
67+
unset($options[0]);
68+
$optionsIds = $this->collectOptionsIds($options);
69+
$attribute->addData($this->prepareOptions($options));
70+
$attribute->setData('swatch_input_type', 'dropdown');
71+
$attribute->beforeSave();
72+
$this->assertEmpty($this->fetchSwatchOptions($optionsIds), 'Swatch options were not deleted');
73+
}
74+
75+
/**
76+
* Prepare options
77+
*
78+
* @param array $options
79+
* @return array
80+
*/
81+
private function prepareOptions(array $options): array
82+
{
83+
unset($options[0]);
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

Comments
 (0)