Skip to content

Commit 72b3c46

Browse files
committed
MAGETWO-71398: Attribute values on store view level not searchable
- Update select query; - Add integration test;
1 parent dba8589 commit 72b3c46

File tree

5 files changed

+134
-80
lines changed

5 files changed

+134
-80
lines changed

app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Action/DataProvider.php

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,7 @@ public function getProductAttributes($storeId, array $productIds, array $attribu
286286
{
287287
$result = [];
288288
$selects = [];
289-
$ifStoreValue = $this->connection->getCheckSql(
290-
'cpe_type.value_id > 0',
291-
'cpe_type.value',
292-
'cpe_type_default.value'
293-
);
289+
$ifStoreValue = $this->connection->getCheckSql('t_store.value_id > 0', 't_store.value', 't_default.value');
294290
$linkField = $this->metadata->getLinkField();
295291
$productLinkFieldsToEntityIdMap = $this->connection->fetchPairs(
296292
$this->connection->select()->from(
@@ -304,43 +300,40 @@ public function getProductAttributes($storeId, array $productIds, array $attribu
304300
foreach ($attributeTypes as $backendType => $attributeIds) {
305301
if ($attributeIds) {
306302
$tableName = $this->getTable('catalog_product_entity_' . $backendType);
307-
$selects[] = $this->connection->select()
308-
->from(
309-
['cpe' => $this->getTable('catalog_product_entity')],
310-
[]
311-
)->joinInner(
312-
['cea' => $this->getTable('catalog_eav_attribute')],
313-
'',
314-
[]
315-
)->joinLeft(
316-
['cpe_type' => $tableName],
317-
$this->connection->quoteInto(
318-
'cpe. ' . $linkField . ' = cpe_type.' . $linkField . ' AND cpe_type.store_id = ? '
319-
. 'AND cea.attribute_id = cpe_type.attribute_id',
320-
$storeId
321-
),
322-
[]
323-
)->joinLeft(
324-
['cpe_type_default' => $tableName],
325-
'cpe.' . $linkField . ' = cpe_type_default.' . $linkField
326-
. ' AND cpe_type_default.store_id = 0'
327-
. ' AND cea.attribute_id = cpe_type_default.attribute_id',
328-
[]
329-
)->where(
330-
'cea.attribute_id IN (?)',
331-
$attributeIds
332-
)->where(
333-
'cpe.' . $linkField . ' IN (?)',
334-
array_keys($productLinkFieldsToEntityIdMap)
335-
)->where(
336-
'cpe_type.attribute_id IS NOT NULL OR cpe_type_default.attribute_id IS NOT NULL'
337-
)->columns(
338-
[
339-
$linkField => 'cpe.' . $linkField,
340-
'attribute_id' => 'cea.attribute_id',
341-
'value' => $this->unifyField($ifStoreValue, $backendType)
342-
]
343-
);
303+
304+
$select = $this->connection->select()->from(
305+
['t' => $tableName],
306+
[
307+
$linkField => 't.' . $linkField,
308+
'attribute_id' => 't.attribute_id',
309+
'value' => $this->unifyField($ifStoreValue, $backendType),
310+
]
311+
)->joinLeft(
312+
['t_store' => $tableName],
313+
$this->connection->quoteInto(
314+
't.' . $linkField . '=t_store.' . $linkField .
315+
' AND t.attribute_id=t_store.attribute_id' .
316+
' AND t_store.store_id = ?',
317+
$storeId
318+
),
319+
[]
320+
)->joinLeft(
321+
['t_default' => $tableName],
322+
$this->connection->quoteInto(
323+
't.' . $linkField . '=t_default.' . $linkField .
324+
' AND t.attribute_id=t_default.attribute_id' .
325+
' AND t_default.store_id = ?',
326+
0
327+
),
328+
[]
329+
)->where(
330+
't.attribute_id IN (?)',
331+
$attributeIds
332+
)->where(
333+
't.' . $linkField . ' IN (?)',
334+
array_keys($productLinkFieldsToEntityIdMap)
335+
)->distinct();
336+
$selects[] = $select;
344337
}
345338
}
346339

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,41 @@
11
<?php
22
/**
3-
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
3+
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
66
namespace Magento\CatalogSearch\Model\Indexer\Fulltext\Action;
77

88
class DataProviderTest extends \PHPUnit_Framework_TestCase
99
{
1010
/**
11-
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
12-
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
11+
* @magentoDataFixture Magento/CatalogSearch/_files/product_for_search.php
12+
* @magentoDbIsolation disabled
1313
*/
14-
public function testRebuildStoreIndexConfigurable()
14+
public function testSearchProductByAttribute()
1515
{
1616
/** @var $objectManager \Magento\TestFramework\ObjectManager */
1717
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
18-
$repository = $objectManager->create(
19-
\Magento\Catalog\Model\ProductRepository::class
20-
);
21-
/** @var \Magento\Store\Model\Store $store */
22-
$store = $objectManager->create(
23-
\Magento\Store\Model\Store::class
24-
);
25-
$globalStoreId = $store->load('admin')->getId();
26-
$secondStoreId = $store->load('fixture_second_store')->getId();
27-
$thirdStoreId = $store->load('fixture_third_store')->getId();
28-
/** @var \Magento\Catalog\Model\Product\Action $productAction */
29-
$productAction = $objectManager->create(
30-
\Magento\Catalog\Model\Product\Action::class
31-
);
32-
33-
$product = $repository->get('simple');
34-
$productId = $product->getId();
35-
$productResource = $product->getResource();
36-
$productAction->updateWebsites([$productId], [$store->load('fixture_second_store')->getWebsiteId()], 'add');
37-
$product->setOrigData();
38-
$product->setStoreId($secondStoreId);
39-
$product->setShortDescription('short description 2 store');
40-
$productResource->save($product);
4118

42-
$this->assertEquals(
43-
'Short description',
44-
$productResource->getAttributeRawValue($productId, 'short_description', $globalStoreId)
45-
);
46-
$this->assertEquals(
47-
'short description 2 store',
48-
$productResource->getAttributeRawValue($productId, 'short_description', $secondStoreId)
49-
);
50-
$this->assertEquals(
51-
'Short description',
52-
$productResource->getAttributeRawValue($productId, 'short_description', $thirdStoreId)
19+
$config = $objectManager->create(\Magento\Framework\Search\Request\Config::class);
20+
/** @var \Magento\Framework\Search\Request\Builder $requestBuilder */
21+
$requestBuilder = $objectManager->create(
22+
\Magento\Framework\Search\Request\Builder::class,
23+
['config' => $config]
5324
);
25+
$requestBuilder->bind('search_term', 'VALUE1');
26+
$requestBuilder->setRequestName('quick_search_container');
27+
$queryRequest = $requestBuilder->create();
28+
/** @var \Magento\Framework\Search\Adapter\Mysql\Adapter $adapter */
29+
$adapter = $objectManager->create(\Magento\Framework\Search\Adapter\Mysql\Adapter::class);
30+
$queryResponse = $adapter->query($queryRequest);
31+
$actualIds = [];
32+
foreach ($queryResponse as $document) {
33+
/** @var \Magento\Framework\Api\Search\Document $document */
34+
$actualIds[] = $document->getId();
35+
}
36+
37+
/** @var \Magento\Catalog\Model\Product $product */
38+
$product = $objectManager->create(\Magento\Catalog\Model\ProductRepository::class)->get('simple');
39+
$this->assertContains($product->getId(), $actualIds, 'Product not found by searchable attribute.');
5440
}
5541
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
require 'searchable_attribute.php';
8+
require __DIR__ . '/../../../Magento/Catalog/_files/product_simple.php';
9+
10+
/** @var $objectManager \Magento\TestFramework\ObjectManager */
11+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
12+
13+
/** @var \Magento\Store\Model\StoreManager $storeManager */
14+
$storeManager = $objectManager->get(\Magento\Store\Model\StoreManager::class);
15+
$storeManager->setIsSingleStoreModeAllowed(false);
16+
/** @var \Magento\Store\Model\Store $store */
17+
$store = $storeManager->getStore('default');
18+
19+
/** @var \Magento\Catalog\Model\Product $product */
20+
$product = $objectManager->create(\Magento\Catalog\Model\ProductRepository::class)->get('simple');
21+
/** @var \Magento\Catalog\Model\Product\Action $productAction */
22+
$productAction = $objectManager->create(\Magento\Catalog\Model\Product\Action::class);
23+
$productAction->updateAttributes([$product->getId()], ['test_searchable_attribute' => 'VALUE1'], $store->getId());
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/** @var $objectManager \Magento\TestFramework\ObjectManager */
8+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
9+
$registry = $objectManager->get(\Magento\Framework\Registry::class);
10+
11+
$registry->unregister('isSecureArea');
12+
$registry->register('isSecureArea', true);
13+
14+
/** @var \Magento\Catalog\Model\Product $product */
15+
$product = $objectManager->create(\Magento\Catalog\Model\ProductRepository::class)->get('simple');
16+
/** @var \Magento\Catalog\Model\ResourceModel\Product $productResource */
17+
$productResource = $objectManager->create(\Magento\Catalog\Model\ResourceModel\Product::class);
18+
$productResource->delete($product);
19+
20+
$eavSetupFactory = $objectManager->create(\Magento\Eav\Setup\EavSetupFactory::class);
21+
/** @var \Magento\Eav\Setup\EavSetup $eavSetup */
22+
$eavSetup = $eavSetupFactory->create();
23+
$eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'test_searchable_attribute');
24+
25+
$registry->unregister('isSecureArea');
26+
$registry->register('isSecureArea', false);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
8+
$eavSetupFactory = $objectManager->create(\Magento\Eav\Setup\EavSetupFactory::class);
9+
/** @var \Magento\Eav\Setup\EavSetup $eavSetup */
10+
$eavSetup = $eavSetupFactory->create();
11+
$eavSetup->addAttribute(
12+
\Magento\Catalog\Model\Product::ENTITY,
13+
'test_searchable_attribute',
14+
[
15+
'label' => 'Test-attribute',
16+
'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
17+
'required' => 0,
18+
'user_defined' => 1,
19+
'searchable' => 1,
20+
'visible_on_front' => 1,
21+
'filterable_in_search' => 1,
22+
'used_in_product_listing' => 1,
23+
'is_used_in_grid' => 1,
24+
'is_filterable_in_grid' => 1,
25+
]
26+
);

0 commit comments

Comments
 (0)