Skip to content

Commit 82f040f

Browse files
author
Jakub Winkler
committed
#33486 - fixing tests and applying code changes to indexes and attributes
1 parent 2be10b7 commit 82f040f

File tree

9 files changed

+208
-38
lines changed

9 files changed

+208
-38
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/Source.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ protected function _getIndexableAttributes($multiSelect)
105105
);
106106

107107
if ($multiSelect == true) {
108-
$select->where('ea.backend_type = ?', 'varchar')->where('ea.frontend_input = ?', 'multiselect');
108+
$select->where('ea.backend_type = ?', 'text')->where('ea.frontend_input = ?', 'multiselect');
109109
} else {
110110
$select->where('ea.backend_type = ?', 'int')->where('ea.frontend_input IN( ? )', ['select', 'boolean']);
111111
}
@@ -303,14 +303,14 @@ protected function _prepareMultiselectIndex($entityIds = null, $attributeId = nu
303303
// prepare get multiselect values query
304304
$productValueExpression = $connection->getCheckSql('pvs.value_id > 0', 'pvs.value', 'pvd.value');
305305
$select = $connection->select()->from(
306-
['pvd' => $this->getTable('catalog_product_entity_varchar')],
306+
['pvd' => $this->getTable('catalog_product_entity_text')],
307307
[]
308308
)->join(
309309
['cs' => $this->getTable('store')],
310310
'',
311311
[]
312312
)->joinLeft(
313-
['pvs' => $this->getTable('catalog_product_entity_varchar')],
313+
['pvs' => $this->getTable('catalog_product_entity_text')],
314314
"pvs.{$productIdField} = pvd.{$productIdField} AND pvs.attribute_id = pvd.attribute_id"
315315
. ' AND pvs.store_id=cs.store_id',
316316
[]
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Setup\Patch\Data;
8+
9+
use Magento\Catalog\Model\Product;
10+
use Magento\Eav\Setup\EavSetup;
11+
use Magento\Eav\Setup\EavSetupFactory;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Setup\ModuleDataSetupInterface;
14+
use Magento\Framework\Setup\Patch\DataPatchInterface;
15+
16+
class UpdateMultiselectAttributesBackendTypes implements DataPatchInterface
17+
{
18+
/**
19+
* @var ModuleDataSetupInterface
20+
*/
21+
private $dataSetup;
22+
/**
23+
* @var EavSetupFactory
24+
*/
25+
private $eavSetupFactory;
26+
27+
/**
28+
* MigrateMultiselectAttributesData constructor.
29+
* @param ModuleDataSetupInterface $dataSetup
30+
* @param EavSetupFactory $eavSetupFactory
31+
*/
32+
public function __construct(
33+
ModuleDataSetupInterface $dataSetup,
34+
EavSetupFactory $eavSetupFactory
35+
) {
36+
$this->dataSetup = $dataSetup;
37+
$this->eavSetupFactory = $eavSetupFactory;
38+
}
39+
40+
/**
41+
* @return array
42+
*/
43+
public static function getDependencies()
44+
{
45+
return [];
46+
}
47+
48+
/**
49+
* @return array
50+
*/
51+
public function getAliases()
52+
{
53+
return [];
54+
}
55+
56+
/**
57+
* @return UpdateMultiselectAttributesBackendTypes
58+
* @throws LocalizedException
59+
*/
60+
public function apply()
61+
{
62+
$this->dataSetup->startSetup();
63+
64+
$connection = $this->dataSetup->getConnection();
65+
$attributeTable = $connection->getTableName('eav_attribute');
66+
/** @var EavSetup $eavSetup */
67+
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->dataSetup]);
68+
$entityTypeId = $eavSetup->getEntityTypeId(Product::ENTITY);
69+
$attributesToMigrate = $connection->fetchCol(
70+
$connection
71+
->select()
72+
->from($attributeTable, ['attribute_id'])
73+
->where('entity_type_id = ?', $entityTypeId)
74+
->where('backend_type = ?', 'varchar')
75+
->where('frontend_input = ?', 'multiselect')
76+
);
77+
78+
$varcharTable = $connection->getTableName('catalog_product_entity_varchar');
79+
$textTable = $connection->getTableName('catalog_product_entity_text');
80+
$varcharTableDataSql = $connection
81+
->select()
82+
->from($varcharTable)
83+
->where('attribute_id in (?)', $attributesToMigrate);
84+
$dataToMigrate = array_map(static function ($row) {
85+
$row['value_id'] = null;
86+
return $row;
87+
}, $connection->fetchAll($varcharTableDataSql));
88+
89+
foreach (array_chunk($dataToMigrate, 2000) as $dataChunk) {
90+
$connection->insertMultiple($textTable, $dataChunk);
91+
}
92+
93+
$connection->query($connection->deleteFromSelect($varcharTableDataSql, $varcharTable));
94+
95+
foreach ($attributesToMigrate as $attributeId) {
96+
$eavSetup->updateAttribute($entityTypeId, $attributeId, 'backend_type', 'text');
97+
}
98+
99+
$this->dataSetup->endSetup();
100+
101+
return $this;
102+
}
103+
}

app/code/Magento/CatalogWidget/Model/Rule/Condition/Product.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ protected function addGlobalAttribute(
182182
$linkField = $attribute->getEntity()->getLinkField();
183183

184184
$collection->getSelect()->join(
185-
[$alias => $collection->getTable('catalog_product_entity_varchar')],
185+
[$alias => $collection->getTable($attribute->getBackendTable())],
186186
"($alias.$linkField = e.$linkField) AND ($alias.store_id = $storeId)" .
187187
" AND ($alias.attribute_id = {$attribute->getId()})",
188188
[]

dev/tests/integration/framework/Magento/TestFramework/Eav/Model/Attribute/DataProvider/MultipleSelect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ protected function getUpdateExpectedData(): array
8989
'frontend_class' => null,
9090
'used_for_sort_by' => '0',
9191
'is_user_defined' => '1',
92-
'backend_type' => 'varchar',
92+
'backend_type' => 'text',
9393
]
9494
);
9595
}

dev/tests/integration/testsuite/Magento/Catalog/Model/ProductGettersTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ public function testGetAttributeText()
207207
$this->assertEquals('Enabled', $this->_model->getAttributeText('status'));
208208
}
209209

210+
210211
/**
211212
* @magentoDataFixture Magento/Catalog/_files/products_with_multiselect_attribute.php
212213
*/
@@ -215,20 +216,21 @@ public function testGetAttributeTextArray()
215216
$product = $this->productRepository->get('simple_ms_2');
216217
$product->getAttributeText('multiselect_attribute');
217218
$expected = [
218-
'Multiselect option 2',
219-
'Multiselect option 3',
220-
'Multiselect option 4'
219+
'Option 2',
220+
'Option 3',
221+
'Option 4 "!@#$%^&*'
221222
];
222223
self::assertEquals(
223224
$expected,
224225
$product->getAttributeText('multiselect_attribute')
225226
);
226227
}
227228

229+
228230
/**
229231
* @magentoDataFixture Magento/Catalog/_files/products_with_multiselect_attribute.php
230232
*/
231-
public function testMultipleMultiselectValues()
233+
public function testMultipleMultiselectTextValues()
232234
{
233235
$expectedArray = [];
234236

@@ -240,7 +242,7 @@ public function testMultipleMultiselectValues()
240242

241243
self::assertEquals(
242244
$expectedArray,
243-
$product->getAttributeText('multiselect_attribute')
245+
$product->getAttributeText('multiselect_attribute_text')
244246
);
245247
}
246248

dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/SourceTest.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,26 +138,23 @@ public function testReindexMultiselectAttribute()
138138
/** @var $options \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection */
139139
$options = $objectManager->create(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class);
140140
$options->setAttributeFilter($attr->getId());
141-
$optionIds = $options->getAllIds();
142-
$product1Id = $optionIds[0] * 10;
143-
$product2Id = $optionIds[1] * 10;
144141

145142
/** @var \Magento\Catalog\Model\Product $product1 **/
146-
$product1 = $productRepository->getById($product1Id);
143+
$product1 = $productRepository->get('simple_ms_1');
147144
$product1->setSpecialFromDate(date('Y-m-d H:i:s'));
148145
$product1->setNewsFromDate(date('Y-m-d H:i:s'));
149146
$productRepository->save($product1);
150147

151148
/** @var \Magento\Catalog\Model\Product $product2 **/
152-
$product2 = $productRepository->getById($product2Id);
153-
$product1->setSpecialFromDate(date('Y-m-d H:i:s'));
154-
$product1->setNewsFromDate(date('Y-m-d H:i:s'));
149+
$product2 = $productRepository->get('simple_ms_2');
150+
$product2->setSpecialFromDate(date('Y-m-d H:i:s'));
151+
$product2->setNewsFromDate(date('Y-m-d H:i:s'));
155152
$productRepository->save($product2);
156153

157154
$this->_eavIndexerProcessor->reindexAll();
158155
$connection = $this->productResource->getConnection();
159156
$select = $connection->select()->from($this->productResource->getTable('catalog_product_index_eav'))
160-
->where('entity_id in (?)', [$product1Id, $product2Id])
157+
->where('entity_id in (?)', [$product1->getId(), $product2->getId()])
161158
->where('attribute_id = ?', $attr->getId());
162159

163160
$result = $connection->fetchAll($select);
@@ -219,6 +216,7 @@ public function testReindexMultiselectAttributeWithSourceModel()
219216
$sourceModel = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
220217
MultiselectSourceMock::class
221218
);
219+
222220
$options = $sourceModel->getAllOptions();
223221
$product1Id = $options[0]['value'] * 10;
224222
$product2Id = $options[1]['value'] * 10;
@@ -231,8 +229,8 @@ public function testReindexMultiselectAttributeWithSourceModel()
231229

232230
/** @var \Magento\Catalog\Model\Product $product2 **/
233231
$product2 = $productRepository->getById($product2Id);
234-
$product1->setSpecialFromDate(date('Y-m-d H:i:s'));
235-
$product1->setNewsFromDate(date('Y-m-d H:i:s'));
232+
$product2->setSpecialFromDate(date('Y-m-d H:i:s'));
233+
$product2->setNewsFromDate(date('Y-m-d H:i:s'));
236234
$productRepository->save($product2);
237235

238236
$this->_eavIndexerProcessor->reindexAll();

dev/tests/integration/testsuite/Magento/Catalog/_files/multiselect_attribute.php

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
$installer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
99
\Magento\Catalog\Setup\CategorySetup::class
1010
);
11-
/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
12-
$attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
11+
/** @var $attributeMultiselect \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
12+
$attributeMultiselect = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
13+
\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
14+
);
15+
16+
/** @var $attributeMultiselectText \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
17+
$attributeMultiselectText = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
1318
\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
1419
);
1520

@@ -22,12 +27,59 @@
2227
}
2328

2429
$entityType = $installer->getEntityTypeId('catalog_product');
25-
if (!$attribute->loadByCode($entityType, 'multiselect_attribute')->getAttributeId()) {
26-
$attribute->setData(
30+
if (!$attributeMultiselect->loadByCode($entityType, 'multiselect_attribute')->getAttributeId()) {
31+
$attributeMultiselect->setData(
2732
[
2833
'attribute_code' => 'multiselect_attribute',
2934
'entity_type_id' => $entityType,
3035
'is_global' => 1,
36+
'is_user_defined' => 1,
37+
'frontend_input' => 'multiselect',
38+
'is_unique' => 0,
39+
'is_required' => 0,
40+
'is_searchable' => 0,
41+
'is_visible_in_advanced_search' => 0,
42+
'is_comparable' => 0,
43+
'is_filterable' => 1,
44+
'is_filterable_in_search' => 0,
45+
'is_used_for_promo_rules' => 0,
46+
'is_html_allowed_on_front' => 1,
47+
'is_visible_on_front' => 0,
48+
'used_in_product_listing' => 0,
49+
'used_for_sort_by' => 0,
50+
'frontend_label' => ['Multiselect Attribute'],
51+
'backend_type' => 'text',
52+
'backend_model' => \Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend::class,
53+
'option' => [
54+
'value' => [
55+
'option_1' => ['Option 1'],
56+
'option_2' => ['Option 2'],
57+
'option_3' => ['Option 3'],
58+
'option_4' => ['Option 4 "!@#$%^&*']
59+
],
60+
'order' => [
61+
'option_1' => 1,
62+
'option_2' => 2,
63+
'option_3' => 3,
64+
'option_4' => 4,
65+
],
66+
],
67+
]
68+
);
69+
$attributeMultiselect->save();
70+
71+
/* Assign attribute to attribute set */
72+
$installer->addAttributeToGroup('catalog_product', 'Default', 'General', $attributeMultiselect->getId());
73+
}
74+
75+
76+
if (!$attributeMultiselectText->loadByCode($entityType, 'multiselect_attribute_text')->getAttributeId()) {
77+
$attributeMultiselectText->setData(
78+
[
79+
'attribute_code' => 'multiselect_attribute_text',
80+
'entity_type_id' => $entityType,
81+
'is_global' => 1,
82+
3183
'is_user_defined' => 1,
3284
'frontend_input' => 'multiselect',
3385
'is_unique' => 0,
@@ -51,8 +103,8 @@
51103
],
52104
]
53105
);
54-
$attribute->save();
106+
$attributeMultiselectText->save();
55107

56108
/* Assign attribute to attribute set */
57-
$installer->addAttributeToGroup('catalog_product', 'Default', 'General', $attribute->getId());
109+
$installer->addAttributeToGroup('catalog_product', 'Default', 'General', $attributeMultiselectText->getId());
58110
}

dev/tests/integration/testsuite/Magento/Catalog/_files/multiselect_attribute_rollback.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@
1414
$attribute->load('multiselect_attribute', 'attribute_code');
1515
$attribute->delete();
1616

17+
$attribute->load('multiselect_attribute_text', 'attribute_code');
18+
$attribute->delete();
19+
1720
$registry->unregister('isSecureArea');
1821
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)