Skip to content

Commit 949c31f

Browse files
author
Valeriy Nayda
committed
MAGETWO-51716: [Performance Bug] Build all layered nav filters on every search query regardless of result set
- fix integration tests
1 parent 368ed07 commit 949c31f

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

app/code/Magento/CatalogSearch/Model/Adapter/Aggregation/AggregationResolver.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Framework\Api\SearchCriteriaBuilder;
1111
use Magento\Framework\Search\Adapter\Aggregation\AggregationResolverInterface;
1212
use Magento\Framework\Search\Request\BucketInterface;
13+
use Magento\Framework\Search\Request\Config;
1314
use Magento\Framework\Search\RequestInterface;
1415

1516
class AggregationResolver implements AggregationResolverInterface
@@ -28,36 +29,50 @@ class AggregationResolver implements AggregationResolverInterface
2829
* @var SearchCriteriaBuilder
2930
*/
3031
private $searchCriteriaBuilder;
32+
33+
/**
34+
* @var Config
35+
*/
36+
private $config;
3137

3238
/**
3339
* AggregationResolver constructor
3440
*
3541
* @param AttributeSetFinderInterface $attributeSetFinder
3642
* @param ProductAttributeRepositoryInterface $productAttributeRepository
3743
* @param SearchCriteriaBuilder $searchCriteriaBuilder
44+
* @param Config $config
3845
*/
3946
public function __construct(
4047
AttributeSetFinderInterface $attributeSetFinder,
4148
ProductAttributeRepositoryInterface $productAttributeRepository,
42-
SearchCriteriaBuilder $searchCriteriaBuilder
49+
SearchCriteriaBuilder $searchCriteriaBuilder,
50+
Config $config
4351
) {
4452
$this->attributeSetFinder = $attributeSetFinder;
4553
$this->productAttributeRepository = $productAttributeRepository;
4654
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
55+
$this->config = $config;
4756
}
4857

4958
/**
5059
* {@inheritdoc}
5160
*/
5261
public function resolve(RequestInterface $request, array $documentIds)
5362
{
63+
$data = $this->config->get($request->getName());
64+
65+
$bucketKeys = isset($data['aggregations']) ? array_keys($data['aggregations']) : [];
5466
$attributeCodes = $this->getApplicableAttributeCodes($documentIds);
5567

56-
$resolvedAggregation = array_filter($request->getAggregation(), function ($bucket) use ($attributeCodes) {
57-
/** @var BucketInterface $bucket */
58-
return in_array($bucket->getField(), $attributeCodes);
59-
});
60-
return $resolvedAggregation;
68+
$resolvedAggregation = array_filter(
69+
$request->getAggregation(),
70+
function ($bucket) use ($attributeCodes, $bucketKeys) {
71+
/** @var BucketInterface $bucket */
72+
return in_array($bucket->getField(), $attributeCodes) || in_array($bucket->getName(), $bucketKeys);
73+
}
74+
);
75+
return array_values($resolvedAggregation);
6176
}
6277

6378
/**

app/code/Magento/CatalogSearch/Test/Unit/Model/Adapter/Aggregation/AggregationResolverTest.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\Api\SearchCriteriaBuilder;
1414
use Magento\Framework\Api\SearchCriteriaInterface;
1515
use Magento\Framework\Search\Request\BucketInterface;
16+
use Magento\Framework\Search\Request\Config;
1617
use Magento\Framework\Search\RequestInterface;
1718
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1819

@@ -38,6 +39,11 @@ class AggregationResolverTest extends \PHPUnit_Framework_TestCase
3839
*/
3940
private $request;
4041

42+
/**
43+
* @var Config|\PHPUnit_Framework_MockObject_MockObject
44+
*/
45+
private $config;
46+
4147
/**
4248
* @var AggregationResolver
4349
*/
@@ -51,13 +57,15 @@ protected function setUp()
5157
->disableOriginalConstructor()
5258
->getMock();
5359
$this->request = $this->getMock(RequestInterface::class);
60+
$this->config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
5461

5562
$this->aggregationResolver = (new ObjectManager($this))->getObject(
5663
AggregationResolver::class,
5764
[
5865
'attributeSetFinder' => $this->attributeSetFinder,
5966
'productAttributeRepository' => $this->productAttributeRepository,
6067
'searchCriteriaBuilder' => $this->searchCriteriaBuilder,
68+
'config' => $this->config,
6169
]
6270
);
6371
}
@@ -66,6 +74,7 @@ public function testResolve()
6674
{
6775
$documentIds = [1, 2, 3];
6876
$attributeSetIds = [4, 5];
77+
$requestName = 'request_name';
6978

7079
$this->attributeSetFinder
7180
->expects($this->once())
@@ -113,11 +122,28 @@ public function testResolve()
113122
$bucketSecond->expects($this->once())
114123
->method('getField')
115124
->willReturn('some_another_code');
125+
$bucketThird = $this->getMock(BucketInterface::class);
126+
$bucketThird->expects($this->once())
127+
->method('getName')
128+
->willReturn('custom_not_attribute_field');
116129

117130
$this->request->expects($this->once())
118131
->method('getAggregation')
119-
->willReturn([$bucketFirst, $bucketSecond]);
120-
121-
$this->assertEquals([$bucketFirst], $this->aggregationResolver->resolve($this->request, $documentIds));
132+
->willReturn([$bucketFirst, $bucketSecond, $bucketThird]);
133+
$this->request->expects($this->once())
134+
->method('getName')
135+
->willReturn($requestName);
136+
137+
$this->config->expects($this->once())
138+
->method('get')
139+
->with($requestName)
140+
->willReturn([
141+
'aggregations' => ['custom_not_attribute_field' => []],
142+
]);
143+
144+
$this->assertEquals(
145+
[$bucketFirst, $bucketThird],
146+
$this->aggregationResolver->resolve($this->request, $documentIds)
147+
);
122148
}
123149
}

0 commit comments

Comments
 (0)