Skip to content

Commit 098eb09

Browse files
Merge branch 'develop' of https://github.com/magento/magento2ce into MAGETWO-67152
2 parents c5705c6 + 5640494 commit 098eb09

File tree

9 files changed

+199
-71
lines changed

9 files changed

+199
-71
lines changed

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\Search\Request\BucketInterface;
1313
use Magento\Framework\Search\Request\Config;
1414
use Magento\Framework\Search\RequestInterface;
15+
use Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection as AttributeCollection;
1516

1617
class AggregationResolver implements AggregationResolverInterface
1718
{
@@ -34,6 +35,11 @@ class AggregationResolver implements AggregationResolverInterface
3435
* @var Config
3536
*/
3637
private $config;
38+
39+
/**
40+
* @var AttributeCollection
41+
*/
42+
private $attributeCollection;
3743

3844
/**
3945
* AggregationResolver constructor
@@ -42,17 +48,21 @@ class AggregationResolver implements AggregationResolverInterface
4248
* @param ProductAttributeRepositoryInterface $productAttributeRepository
4349
* @param SearchCriteriaBuilder $searchCriteriaBuilder
4450
* @param Config $config
51+
* @param AttributeCollection $attributeCollection [optional]
4552
*/
4653
public function __construct(
4754
AttributeSetFinderInterface $attributeSetFinder,
4855
ProductAttributeRepositoryInterface $productAttributeRepository,
4956
SearchCriteriaBuilder $searchCriteriaBuilder,
50-
Config $config
57+
Config $config,
58+
AttributeCollection $attributeCollection = null
5159
) {
5260
$this->attributeSetFinder = $attributeSetFinder;
5361
$this->productAttributeRepository = $productAttributeRepository;
5462
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
5563
$this->config = $config;
64+
$this->attributeCollection = $attributeCollection
65+
?: \Magento\Framework\App\ObjectManager::getInstance()->get(AttributeCollection::class);
5666
}
5767

5868
/**
@@ -85,15 +95,14 @@ private function getApplicableAttributeCodes(array $documentIds)
8595
{
8696
$attributeSetIds = $this->attributeSetFinder->findAttributeSetIdsByProductIds($documentIds);
8797

88-
$searchCriteria = $this->searchCriteriaBuilder
89-
->addFilter('attribute_set_id', $attributeSetIds, 'in')
90-
->create();
91-
$result = $this->productAttributeRepository->getList($searchCriteria);
98+
$this->attributeCollection->setAttributeSetFilter($attributeSetIds);
99+
$this->attributeCollection->setEntityTypeFilter(
100+
\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE
101+
);
102+
$this->attributeCollection->getSelect()
103+
->reset(\Magento\Framework\DB\Select::COLUMNS)
104+
->columns('attribute_code');
92105

93-
$attributeCodes = [];
94-
foreach ($result->getItems() as $attribute) {
95-
$attributeCodes[] = $attribute->getAttributeCode();
96-
}
97-
return $attributeCodes;
106+
return $this->attributeCollection->getConnection()->fetchCol($this->attributeCollection->getSelect());
98107
}
99108
}

app/code/Magento/CatalogSearch/Model/Adapter/Mysql/Field/Resolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function __construct(
4040
public function resolve(array $fields)
4141
{
4242
$resolvedFields = [];
43+
$this->attributeCollection->addFieldToFilter('attribute_code', ['in' => $fields]);
4344
foreach ($fields as $field) {
4445
if ('*' === $field) {
4546
$resolvedFields = [

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

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
*/
66
namespace Magento\CatalogSearch\Test\Unit\Model\Adapter\Aggregation;
77

8-
use Magento\Catalog\Api\Data\ProductAttributeInterface;
9-
use Magento\Catalog\Api\Data\ProductAttributeSearchResultsInterface;
108
use Magento\CatalogSearch\Model\Adapter\Aggregation\AggregationResolver;
119
use Magento\Catalog\Api\AttributeSetFinderInterface;
12-
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
1310
use Magento\Framework\Api\SearchCriteriaBuilder;
14-
use Magento\Framework\Api\SearchCriteriaInterface;
1511
use Magento\Framework\Search\Request\BucketInterface;
1612
use Magento\Framework\Search\Request\Config;
1713
use Magento\Framework\Search\RequestInterface;
@@ -27,11 +23,6 @@ class AggregationResolverTest extends \PHPUnit_Framework_TestCase
2723
*/
2824
private $attributeSetFinder;
2925

30-
/**
31-
* @var ProductAttributeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
32-
*/
33-
private $productAttributeRepository;
34-
3526
/**
3627
* @var SearchCriteriaBuilder|\PHPUnit_Framework_MockObject_MockObject
3728
*/
@@ -47,28 +38,44 @@ class AggregationResolverTest extends \PHPUnit_Framework_TestCase
4738
*/
4839
private $config;
4940

41+
/**
42+
* @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection|\PHPUnit_Framework_MockObject_MockObject
43+
*/
44+
private $attributeCollection;
45+
5046
/**
5147
* @var AggregationResolver
5248
*/
5349
private $aggregationResolver;
5450

51+
/**
52+
* @inheritdoc
53+
*/
5554
protected function setUp()
5655
{
57-
$this->attributeSetFinder = $this->getMock(AttributeSetFinderInterface::class);
58-
$this->productAttributeRepository = $this->getMock(ProductAttributeRepositoryInterface::class);
56+
$this->attributeSetFinder = $this->getMockBuilder(AttributeSetFinderInterface::class)
57+
->disableOriginalConstructor()
58+
->getMockForAbstractClass();
5959
$this->searchCriteriaBuilder = $this->getMockBuilder(SearchCriteriaBuilder::class)
6060
->disableOriginalConstructor()
6161
->getMock();
62-
$this->request = $this->getMock(RequestInterface::class);
62+
$this->request = $this->getMockBuilder(RequestInterface::class)
63+
->disableOriginalConstructor()
64+
->getMockForAbstractClass();
6365
$this->config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
66+
$this->attributeCollection = $this->getMockBuilder(
67+
\Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection::class
68+
)
69+
->disableOriginalConstructor()
70+
->getMock();
6471

6572
$this->aggregationResolver = (new ObjectManager($this))->getObject(
6673
AggregationResolver::class,
6774
[
6875
'attributeSetFinder' => $this->attributeSetFinder,
69-
'productAttributeRepository' => $this->productAttributeRepository,
7076
'searchCriteriaBuilder' => $this->searchCriteriaBuilder,
7177
'config' => $this->config,
78+
'attributeCollection' => $this->attributeCollection,
7279
]
7380
);
7481
}
@@ -78,54 +85,50 @@ public function testResolve()
7885
$documentIds = [1, 2, 3];
7986
$attributeSetIds = [4, 5];
8087
$requestName = 'request_name';
81-
88+
$select = $this->searchCriteriaBuilder = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
89+
->disableOriginalConstructor()
90+
->getMock();
91+
$adapter = $this->searchCriteriaBuilder = $this->getMockBuilder(
92+
\Magento\Framework\DB\Adapter\AdapterInterface::class
93+
)
94+
->disableOriginalConstructor()
95+
->getMockForAbstractClass();
8296
$this->attributeSetFinder
8397
->expects($this->once())
8498
->method('findAttributeSetIdsByProductIds')
8599
->with($documentIds)
86100
->willReturn($attributeSetIds);
87-
88-
$searchCriteria = $this->getMock(SearchCriteriaInterface::class);
89-
90-
$this->searchCriteriaBuilder
91-
->expects($this->once())
92-
->method('addFilter')
93-
->with('attribute_set_id', $attributeSetIds, 'in')
101+
$this->attributeCollection->expects($this->once())
102+
->method('setAttributeSetFilter')
103+
->with($attributeSetIds)
94104
->willReturnSelf();
95-
$this->searchCriteriaBuilder
96-
->expects($this->once())
97-
->method('create')
98-
->willReturn($searchCriteria);
99-
100-
$attributeFirst = $this->getMock(ProductAttributeInterface::class);
101-
$attributeFirst->expects($this->once())
102-
->method('getAttributeCode')
103-
->willReturn('code_1');
104-
$attributeSecond = $this->getMock(ProductAttributeInterface::class);
105-
$attributeSecond->expects($this->once())
106-
->method('getAttributeCode')
107-
->willReturn('code_2');
108-
109-
$searchResult = $this->getMock(ProductAttributeSearchResultsInterface::class);
110-
$searchResult->expects($this->once())
111-
->method('getItems')
112-
->willReturn([$attributeFirst, $attributeSecond]);
113-
114-
$this->productAttributeRepository
115-
->expects($this->once())
116-
->method('getList')
117-
->with($searchCriteria)
118-
->willReturn($searchResult);
119-
120-
$bucketFirst = $this->getMock(BucketInterface::class);
105+
$this->attributeCollection->expects($this->once())
106+
->method('setEntityTypeFilter')
107+
->with(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE)
108+
->willReturnSelf();
109+
$this->attributeCollection->expects($this->atLeastOnce())
110+
->method('getSelect')
111+
->willReturn($select);
112+
$select->expects($this->once())->method('reset')->with(\Magento\Framework\DB\Select::COLUMNS)->willReturnSelf();
113+
$select->expects($this->once())->method('columns')->with('attribute_code')->willReturnSelf();
114+
$this->attributeCollection->expects($this->once())->method('getConnection')->willReturn($adapter);
115+
$adapter->expects($this->once())->method('fetchCol')->with($select)->willReturn(['code_1', 'code_2']);
116+
117+
$bucketFirst = $this->getMockBuilder(BucketInterface::class)
118+
->disableOriginalConstructor()
119+
->getMockForAbstractClass();
121120
$bucketFirst->expects($this->once())
122121
->method('getField')
123122
->willReturn('code_1');
124-
$bucketSecond = $this->getMock(BucketInterface::class);
123+
$bucketSecond = $this->getMockBuilder(BucketInterface::class)
124+
->disableOriginalConstructor()
125+
->getMockForAbstractClass();
125126
$bucketSecond->expects($this->once())
126127
->method('getField')
127128
->willReturn('some_another_code');
128-
$bucketThird = $this->getMock(BucketInterface::class);
129+
$bucketThird = $this->getMockBuilder(BucketInterface::class)
130+
->disableOriginalConstructor()
131+
->getMockForAbstractClass();
129132
$bucketThird->expects($this->once())
130133
->method('getName')
131134
->willReturn('custom_not_attribute_field');
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogSearch\Test\Unit\Model\Adapter\Mysql\Field;
7+
8+
use Magento\Framework\Search\Adapter\Mysql\Field\FieldInterface;
9+
10+
/**
11+
* Unit tests for Magento\CatalogSearch\Model\Adapter\Mysql\Field\Resolver class.
12+
*/
13+
class ResolverTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/**
16+
* @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection|\PHPUnit_Framework_MockObject_MockObject
17+
*/
18+
private $attributeCollection;
19+
20+
/**
21+
* @var \Magento\Framework\Search\Adapter\Mysql\Field\FieldFactory|\PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
private $fieldFactory;
24+
25+
/**
26+
* @var \Magento\CatalogSearch\Model\Adapter\Mysql\Field\Resolver
27+
*/
28+
private $model;
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
protected function setUp()
34+
{
35+
$this->attributeCollection = $this->getMockBuilder(
36+
\Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection::class
37+
)
38+
->disableOriginalConstructor()
39+
->getMock();
40+
$this->fieldFactory = $this->getMockBuilder(\Magento\Framework\Search\Adapter\Mysql\Field\FieldFactory::class)
41+
->disableOriginalConstructor()
42+
->setMethods(['create'])
43+
->getMock();
44+
45+
$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
46+
$this->model = $objectManagerHelper->getObject(
47+
\Magento\CatalogSearch\Model\Adapter\Mysql\Field\Resolver::class,
48+
[
49+
'attributeCollection' => $this->attributeCollection,
50+
'fieldFactory' => $this->fieldFactory,
51+
]
52+
);
53+
}
54+
55+
/**
56+
* Test resolve method.
57+
*
58+
* @param array $fields
59+
* @param int|null $attributeId
60+
* @param \PHPUnit_Framework_MockObject_MockObject $field
61+
* @param array $expectedResult
62+
* @return void
63+
* @dataProvider resolveDataProvider
64+
*/
65+
public function testResolve(
66+
array $fields,
67+
$attributeId,
68+
\PHPUnit_Framework_MockObject_MockObject $field,
69+
array $expectedResult
70+
) {
71+
$item = $this->getMockBuilder(\Magento\Framework\DataObject::class)
72+
->disableOriginalConstructor()
73+
->setMethods(['getId'])
74+
->getMock();
75+
$this->attributeCollection->expects($this->once())
76+
->method('addFieldToFilter')
77+
->with('attribute_code', ['in' => $fields])
78+
->willReturnSelf();
79+
$this->fieldFactory->expects($this->once())
80+
->method('create')
81+
->with(['attributeId' => $attributeId, 'column' => 'data_index', 'type' => FieldInterface::TYPE_FULLTEXT])
82+
->willReturn($field);
83+
if ($attributeId) {
84+
$this->attributeCollection->expects($this->once())
85+
->method('getItemByColumnValue')
86+
->with('attribute_code', $fields[0])
87+
->willReturn($item);
88+
$item->expects($this->once())->method('getId')->willReturn($attributeId);
89+
}
90+
91+
$this->assertSame($expectedResult, $this->model->resolve($fields));
92+
}
93+
94+
/**
95+
* Data provider for resolve method.
96+
*
97+
* @return array
98+
*/
99+
public function resolveDataProvider()
100+
{
101+
$field = $this->getMockBuilder(\Magento\Framework\Search\Adapter\Mysql\Field\Field::class)
102+
->disableOriginalConstructor()
103+
->getMock();
104+
return [
105+
[['code_1'], 1, $field, ['code_1' => $field]],
106+
[['*'], null, $field, [$field]],
107+
];
108+
}
109+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@
5252
</argument>
5353
</arguments>
5454
</type>
55+
<type name="Magento\CatalogSearch\Model\Adapter\Mysql\Field\Resolver">
56+
<arguments>
57+
<argument name="attributeCollection" xsi:type="object" shared="false">Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection</argument>
58+
</arguments>
59+
</type>
5560
<type name="Magento\Catalog\Model\ResourceModel\Product">
5661
<plugin name="catalogsearchFulltextProduct" type="Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Product"/>
5762
</type>

app/code/Magento/Quote/Model/QuoteRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function __construct(
105105
public function get($cartId, array $sharedStoreIds = [])
106106
{
107107
if (!isset($this->quotesById[$cartId])) {
108-
$quote = $this->loadQuote('load', 'cartId', $cartId, $sharedStoreIds);
108+
$quote = $this->loadQuote('loadByIdWithoutStore', 'cartId', $cartId, $sharedStoreIds);
109109
$this->getLoadHandler()->load($quote);
110110
$this->quotesById[$cartId] = $quote;
111111
}

0 commit comments

Comments
 (0)