Skip to content

Commit f2a66a4

Browse files
author
Bomko, Alex(abomko)
committed
Merge pull request #616 from magento-dragons/PR-5
[Dragons] Bugfixes
2 parents 2f91db8 + c2d8b3f commit f2a66a4

File tree

17 files changed

+668
-22
lines changed

17 files changed

+668
-22
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © 2016 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\Catalog\Api;
8+
9+
interface AttributeSetFinderInterface
10+
{
11+
/**
12+
* Get attribute set ids by product ids
13+
*
14+
* @param array $productIds
15+
* @return array
16+
*/
17+
public function findAttributeSetIdsByProductIds(array $productIds);
18+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Model\Product\Attribute;
7+
8+
use Magento\Catalog\Api\Data\ProductInterface;
9+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
10+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
11+
use Magento\Catalog\Api\AttributeSetFinderInterface;
12+
use Magento\Framework\DB\Select;
13+
14+
class AttributeSetFinder implements AttributeSetFinderInterface
15+
{
16+
/**
17+
* @var CollectionFactory
18+
*/
19+
private $productCollectionFactory;
20+
21+
/**
22+
* @param CollectionFactory $productCollectionFactory
23+
*/
24+
public function __construct(CollectionFactory $productCollectionFactory)
25+
{
26+
$this->productCollectionFactory = $productCollectionFactory;
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function findAttributeSetIdsByProductIds(array $productIds)
33+
{
34+
/** @var $collection Collection */
35+
$collection = $this->productCollectionFactory->create();
36+
$select = $collection
37+
->getSelect()
38+
->reset(Select::COLUMNS)
39+
->columns(ProductInterface::ATTRIBUTE_SET_ID)
40+
->where('entity_id IN (?)', $productIds)
41+
->group(ProductInterface::ATTRIBUTE_SET_ID);
42+
$result = $collection->getConnection()->fetchCol($select);
43+
return $result;
44+
}
45+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © 2016 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\Catalog\Test\Unit\Model\Product\Attribute;
8+
9+
use Magento\Catalog\Api\Data\ProductInterface;
10+
use Magento\Catalog\Model\Product\Attribute\AttributeSetFinder;
11+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
12+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
13+
use Magento\Framework\DB\Adapter\AdapterInterface;
14+
use Magento\Framework\DB\Select;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
16+
17+
class AttributeSetFinderTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @var Collection|\PHPUnit_Framework_MockObject_MockObject
21+
*/
22+
protected $productCollection;
23+
24+
/**
25+
* @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
26+
*/
27+
protected $productCollectionFactory;
28+
29+
/**
30+
* @var AttributeSetFinder
31+
*/
32+
protected $attributeSetFinder;
33+
34+
protected function setUp()
35+
{
36+
$this->productCollection = $this->getMockBuilder(Collection::class)
37+
->disableOriginalConstructor()
38+
->getMock();
39+
$this->productCollectionFactory = $this->getMockBuilder(CollectionFactory::class)
40+
->setMethods(['create'])
41+
->disableOriginalConstructor()
42+
->getMock();
43+
$this->productCollectionFactory->expects($this->once())->method('create')->willReturn($this->productCollection);
44+
45+
$this->attributeSetFinder = (new ObjectManager($this))->getObject(
46+
AttributeSetFinder::class,
47+
[
48+
'productCollectionFactory' => $this->productCollectionFactory,
49+
]
50+
);
51+
}
52+
53+
public function testFindAttributeIdsByProductIds()
54+
{
55+
$productIds = [1, 2, 3];
56+
$attributeSetIds = [3, 4, 6];
57+
58+
$select = $this->getMockBuilder(Select::class)
59+
->disableOriginalConstructor()
60+
->getMock();
61+
$select->expects($this->once())->method('reset')->with(Select::COLUMNS)->willReturnSelf();
62+
$select->expects($this->once())->method('columns')->with(ProductInterface::ATTRIBUTE_SET_ID)->willReturnSelf();
63+
$select->expects($this->once())->method('where')->with('entity_id IN (?)', $productIds)->willReturnSelf();
64+
$select->expects($this->once())->method('group')->with(ProductInterface::ATTRIBUTE_SET_ID)->willReturnSelf();
65+
66+
$connection = $this->getMock(AdapterInterface::class);
67+
$connection->expects($this->once())->method('fetchCol')->with($select)->willReturn($attributeSetIds);
68+
69+
$this->productCollection->expects($this->once())->method('getSelect')->willReturn($select);
70+
$this->productCollection->expects($this->once())->method('getConnection')->willReturn($connection);
71+
72+
$this->assertEquals($attributeSetIds, $this->attributeSetFinder->findAttributeSetIdsByProductIds($productIds));
73+
}
74+
}

app/code/Magento/Catalog/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<preference for="Magento\Catalog\Api\AttributeSetManagementInterface" type="Magento\Catalog\Model\Product\Attribute\SetManagement" />
4444
<preference for="Magento\Catalog\Api\AttributeSetRepositoryInterface" type="Magento\Catalog\Model\Product\Attribute\SetRepository" />
4545
<preference for="Magento\Catalog\Api\ProductManagementInterface" type="Magento\Catalog\Model\ProductManagement" />
46+
<preference for="Magento\Catalog\Api\AttributeSetFinderInterface" type="Magento\Catalog\Model\Product\Attribute\AttributeSetFinder" />
4647
<type name="Magento\Customer\Model\ResourceModel\Visitor">
4748
<plugin name="catalogLog" type="Magento\Catalog\Model\Plugin\Log" />
4849
</type>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogSearch\Model\Adapter\Aggregation;
7+
8+
use Magento\Catalog\Api\AttributeSetFinderInterface;
9+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
10+
use Magento\Framework\Api\SearchCriteriaBuilder;
11+
use Magento\Framework\Search\Adapter\Aggregation\AggregationResolverInterface;
12+
use Magento\Framework\Search\Request\BucketInterface;
13+
use Magento\Framework\Search\Request\Config;
14+
use Magento\Framework\Search\RequestInterface;
15+
16+
class AggregationResolver implements AggregationResolverInterface
17+
{
18+
/**
19+
* @var AttributeSetFinderInterface
20+
*/
21+
private $attributeSetFinder;
22+
23+
/**
24+
* @var ProductAttributeRepositoryInterface
25+
*/
26+
private $productAttributeRepository;
27+
28+
/**
29+
* @var SearchCriteriaBuilder
30+
*/
31+
private $searchCriteriaBuilder;
32+
33+
/**
34+
* @var Config
35+
*/
36+
private $config;
37+
38+
/**
39+
* AggregationResolver constructor
40+
*
41+
* @param AttributeSetFinderInterface $attributeSetFinder
42+
* @param ProductAttributeRepositoryInterface $productAttributeRepository
43+
* @param SearchCriteriaBuilder $searchCriteriaBuilder
44+
* @param Config $config
45+
*/
46+
public function __construct(
47+
AttributeSetFinderInterface $attributeSetFinder,
48+
ProductAttributeRepositoryInterface $productAttributeRepository,
49+
SearchCriteriaBuilder $searchCriteriaBuilder,
50+
Config $config
51+
) {
52+
$this->attributeSetFinder = $attributeSetFinder;
53+
$this->productAttributeRepository = $productAttributeRepository;
54+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
55+
$this->config = $config;
56+
}
57+
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
public function resolve(RequestInterface $request, array $documentIds)
62+
{
63+
$data = $this->config->get($request->getName());
64+
65+
$bucketKeys = isset($data['aggregations']) ? array_keys($data['aggregations']) : [];
66+
$attributeCodes = $this->getApplicableAttributeCodes($documentIds);
67+
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);
76+
}
77+
78+
/**
79+
* Get applicable attributes
80+
*
81+
* @param array $documentIds
82+
* @return array
83+
*/
84+
private function getApplicableAttributeCodes(array $documentIds)
85+
{
86+
$attributeSetIds = $this->attributeSetFinder->findAttributeSetIdsByProductIds($documentIds);
87+
88+
$searchCriteria = $this->searchCriteriaBuilder
89+
->addFilter('attribute_set_id', $attributeSetIds, 'in')
90+
->create();
91+
$result = $this->productAttributeRepository->getList($searchCriteria);
92+
93+
$attributeCodes = [];
94+
foreach ($result->getItems() as $attribute) {
95+
$attributeCodes[] = $attribute->getAttributeCode();
96+
}
97+
return $attributeCodes;
98+
}
99+
}

0 commit comments

Comments
 (0)