Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

Commit eacac63

Browse files
Merge pull request #258 from magento-south/MAGETWO-56012
Story: MAGETWO-56012 SearchCriteria Unified Processing MAGETWO-56079 SearchCriteria Unified Processing for Cms, Customer and Eav modules MAGETWO-56080 SearchCriteria Unified Processing for GiftWrapping, Quote and Tax modules MAGETWO-56081 SearchCriteria Unified Processing for autogenerated repositories of GiftCardAccount, Rma and Sales modules MAGETWO-56082 SearchCriteria Unified Processing for Rma, Sales, SalesRule and Staging modules MAGETWO-56083 SearchCriteria Unified Processing for Ui, Vault, and VersionsCms modules
2 parents 5a615bb + 5354548 commit eacac63

File tree

172 files changed

+7861
-1927
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

172 files changed

+7861
-1927
lines changed

app/code/Magento/Braintree/Gateway/Command/CaptureStrategyCommand.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,25 @@ private function isExpiredAuthorization(OrderPaymentInterface $payment)
166166
*/
167167
private function isExistsCaptureTransaction(OrderPaymentInterface $payment)
168168
{
169-
$filters[] = $this->filterBuilder->setField('payment_id')
170-
->setValue($payment->getId())
171-
->create();
169+
$this->searchCriteriaBuilder->addFilters(
170+
[
171+
$this->filterBuilder
172+
->setField('payment_id')
173+
->setValue($payment->getId())
174+
->create(),
175+
]
176+
);
172177

173-
$filters[] = $this->filterBuilder->setField('txn_type')
174-
->setValue(TransactionInterface::TYPE_CAPTURE)
175-
->create();
178+
$this->searchCriteriaBuilder->addFilters(
179+
[
180+
$this->filterBuilder
181+
->setField('txn_type')
182+
->setValue(TransactionInterface::TYPE_CAPTURE)
183+
->create(),
184+
]
185+
);
176186

177-
$searchCriteria = $this->searchCriteriaBuilder->addFilters($filters)
178-
->create();
187+
$searchCriteria = $this->searchCriteriaBuilder->create();
179188

180189
$count = $this->transactionRepository->getList($searchCriteria)->getTotalCount();
181190
return (boolean) $count;

app/code/Magento/Braintree/Test/Unit/Gateway/Command/CaptureStrategyCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ private function buildSearchCriteria()
374374
->willReturnSelf();
375375

376376
$searchCriteria = new SearchCriteria();
377-
$this->searchCriteriaBuilder->expects(static::once())
377+
$this->searchCriteriaBuilder->expects(static::exactly(2))
378378
->method('addFilters')
379379
->willReturnSelf();
380380
$this->searchCriteriaBuilder->expects(static::once())

app/code/Magento/Braintree/Test/Unit/Gateway/Request/VaultCaptureDataBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function testBuild()
8080
$paymentExtension = $this->getMockBuilder(OrderPaymentExtension::class)
8181
->setMethods(['getVaultPaymentToken'])
8282
->disableOriginalConstructor()
83-
->getMock();
83+
->getMockForAbstractClass();
8484

8585
$paymentToken = $this->getMockBuilder(PaymentToken::class)
8686
->disableOriginalConstructor()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor;
7+
8+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
9+
use Magento\Framework\Api\Filter;
10+
use Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface;
11+
use Magento\Framework\Data\Collection\AbstractDb;
12+
13+
class ProductCategoryFilter implements CustomFilterInterface
14+
{
15+
/**
16+
* Apply category_id Filter to Product Collection
17+
*
18+
* @param Filter $filter
19+
* @param AbstractDb $collection
20+
* @return bool Whether the filter is applied
21+
*/
22+
public function apply(Filter $filter, AbstractDb $collection)
23+
{
24+
$conditionType = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
25+
$categoryFilter = [$conditionType => [$filter->getValue()]];
26+
27+
/** @var Collection $collection */
28+
$collection->addCategoriesFilter($categoryFilter);
29+
30+
return true;
31+
}
32+
}

app/code/Magento/Catalog/Model/CategoryList.php

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
use Magento\Catalog\Model\ResourceModel\Category\Collection;
1313
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
1414
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
15-
use Magento\Framework\Api\Search\FilterGroup;
1615
use Magento\Framework\Api\SearchCriteriaInterface;
17-
use Magento\Framework\Api\SortOrder;
18-
use Magento\Framework\App\ObjectManager;
16+
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
1917

2018
class CategoryList implements CategoryListInterface
2119
{
@@ -39,22 +37,30 @@ class CategoryList implements CategoryListInterface
3937
*/
4038
private $categoryRepository;
4139

40+
/**
41+
* @var CollectionProcessorInterface
42+
*/
43+
private $collectionProcessor;
44+
4245
/**
4346
* @param CollectionFactory $categoryCollectionFactory
4447
* @param JoinProcessorInterface $extensionAttributesJoinProcessor
4548
* @param CategorySearchResultsInterfaceFactory $categorySearchResultsFactory
4649
* @param CategoryRepositoryInterface $categoryRepository
50+
* @param CollectionProcessorInterface $collectionProcessor
4751
*/
4852
public function __construct(
4953
CollectionFactory $categoryCollectionFactory,
5054
JoinProcessorInterface $extensionAttributesJoinProcessor,
5155
CategorySearchResultsInterfaceFactory $categorySearchResultsFactory,
52-
CategoryRepositoryInterface $categoryRepository
56+
CategoryRepositoryInterface $categoryRepository,
57+
CollectionProcessorInterface $collectionProcessor = null
5358
) {
5459
$this->categoryCollectionFactory = $categoryCollectionFactory;
5560
$this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
5661
$this->categorySearchResultsFactory = $categorySearchResultsFactory;
5762
$this->categoryRepository = $categoryRepository;
63+
$this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
5864
}
5965

6066
/**
@@ -66,23 +72,7 @@ public function getList(SearchCriteriaInterface $searchCriteria)
6672
$collection = $this->categoryCollectionFactory->create();
6773
$this->extensionAttributesJoinProcessor->process($collection);
6874

69-
foreach ($searchCriteria->getFilterGroups() as $group) {
70-
$this->addFilterGroupToCollection($group, $collection);
71-
}
72-
73-
/** @var SortOrder $sortOrder */
74-
$sortOrders = $searchCriteria->getSortOrders();
75-
if ($sortOrders) {
76-
foreach ($sortOrders as $sortOrder) {
77-
$collection->addOrder(
78-
$sortOrder->getField(),
79-
($sortOrder->getDirection() === SortOrder::SORT_ASC) ? SortOrder::SORT_ASC : SortOrder::SORT_DESC
80-
);
81-
}
82-
}
83-
84-
$collection->setCurPage($searchCriteria->getCurrentPage());
85-
$collection->setPageSize($searchCriteria->getPageSize());
75+
$this->collectionProcessor->process($searchCriteria, $collection);
8676

8777
$items = [];
8878
foreach ($collection->getAllIds() as $id) {
@@ -98,22 +88,18 @@ public function getList(SearchCriteriaInterface $searchCriteria)
9888
}
9989

10090
/**
101-
* Add filter group to collection
91+
* Retrieve collection processor
10292
*
103-
* @param FilterGroup $filterGroup
104-
* @param Collection $collection
105-
* @return void
93+
* @deprecated
94+
* @return CollectionProcessorInterface
10695
*/
107-
private function addFilterGroupToCollection(FilterGroup $filterGroup, Collection $collection)
96+
private function getCollectionProcessor()
10897
{
109-
$filters = $filterGroup->getFilters();
110-
if ($filters) {
111-
$fields = [];
112-
foreach ($filters as $filter) {
113-
$conditionType = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
114-
$fields[] = ['attribute' => $filter->getField(), $conditionType => $filter->getValue()];
115-
}
116-
$collection->addFieldToFilter($fields);
98+
if (!$this->collectionProcessor) {
99+
$this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
100+
'Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor'
101+
);
117102
}
103+
return $this->collectionProcessor;
118104
}
119105
}

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Magento\Framework\Api\Data\ImageContentInterfaceFactory;
1414
use Magento\Framework\Api\ImageContentValidatorInterface;
1515
use Magento\Framework\Api\ImageProcessorInterface;
16-
use Magento\Framework\Api\SortOrder;
16+
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
1717
use Magento\Framework\DB\Adapter\ConnectionException;
1818
use Magento\Framework\DB\Adapter\DeadlockException;
1919
use Magento\Framework\DB\Adapter\LockWaitException;
@@ -135,6 +135,11 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
135135
*/
136136
protected $mediaGalleryProcessor;
137137

138+
/**
139+
* @var CollectionProcessorInterface
140+
*/
141+
private $collectionProcessor;
142+
138143
/**
139144
* ProductRepository constructor.
140145
* @param ProductFactory $productFactory
@@ -157,6 +162,7 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
157162
* @param MimeTypeExtensionMap $mimeTypeExtensionMap
158163
* @param ImageProcessorInterface $imageProcessor
159164
* @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
165+
* @param CollectionProcessorInterface $collectionProcessor
160166
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
161167
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
162168
*/
@@ -180,7 +186,8 @@ public function __construct(
180186
ImageContentInterfaceFactory $contentFactory,
181187
MimeTypeExtensionMap $mimeTypeExtensionMap,
182188
ImageProcessorInterface $imageProcessor,
183-
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
189+
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor,
190+
CollectionProcessorInterface $collectionProcessor = null
184191
) {
185192
$this->productFactory = $productFactory;
186193
$this->collectionFactory = $collectionFactory;
@@ -199,6 +206,7 @@ public function __construct(
199206
$this->contentFactory = $contentFactory;
200207
$this->imageProcessor = $imageProcessor;
201208
$this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
209+
$this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
202210
}
203211

204212
/**
@@ -621,20 +629,8 @@ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCr
621629
$collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
622630
$collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
623631

624-
//Add filters from root filter group to the collection
625-
foreach ($searchCriteria->getFilterGroups() as $group) {
626-
$this->addFilterGroupToCollection($group, $collection);
627-
}
628-
/** @var SortOrder $sortOrder */
629-
foreach ((array)$searchCriteria->getSortOrders() as $sortOrder) {
630-
$field = $sortOrder->getField();
631-
$collection->addOrder(
632-
$field,
633-
($sortOrder->getDirection() == SortOrder::SORT_ASC) ? 'ASC' : 'DESC'
634-
);
635-
}
636-
$collection->setCurPage($searchCriteria->getCurrentPage());
637-
$collection->setPageSize($searchCriteria->getPageSize());
632+
$this->collectionProcessor->process($searchCriteria, $collection);
633+
638634
$collection->load();
639635

640636
$searchResult = $this->searchResultsFactory->create();
@@ -647,6 +643,7 @@ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCr
647643
/**
648644
* Helper function that adds a FilterGroup to the collection.
649645
*
646+
* @deprecated
650647
* @param \Magento\Framework\Api\Search\FilterGroup $filterGroup
651648
* @param Collection $collection
652649
* @return void
@@ -698,4 +695,20 @@ private function getMediaGalleryProcessor()
698695
}
699696
return $this->mediaGalleryProcessor;
700697
}
698+
699+
/**
700+
* Retrieve collection processor
701+
*
702+
* @deprecated
703+
* @return CollectionProcessorInterface
704+
*/
705+
private function getCollectionProcessor()
706+
{
707+
if (!$this->collectionProcessor) {
708+
$this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
709+
'Magento\Catalog\Model\Api\SearchCriteria\ProductCollectionProcessor'
710+
);
711+
}
712+
return $this->collectionProcessor;
713+
}
701714
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Test\Unit\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor;
7+
8+
use Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductCategoryFilter;
9+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
10+
use Magento\Framework\Api\Filter;
11+
12+
class ProductCategoryFilterTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/** @var ProductCategoryFilter */
15+
private $model;
16+
17+
protected function setUp()
18+
{
19+
$this->model = new ProductCategoryFilter();
20+
}
21+
22+
public function testApply()
23+
{
24+
/** @var Filter|\PHPUnit_Framework_MockObject_MockObject $filterMock */
25+
$filterMock = $this->getMockBuilder(Filter::class)
26+
->disableOriginalConstructor()
27+
->getMock();
28+
29+
/** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
30+
$collectionMock = $this->getMockBuilder(Collection::class)
31+
->disableOriginalConstructor()
32+
->getMock();
33+
34+
$filterMock->expects($this->exactly(2))
35+
->method('getConditionType')
36+
->willReturn('condition');
37+
$filterMock->expects($this->once())
38+
->method('getValue')
39+
->willReturn('value');
40+
41+
$collectionMock->expects($this->once())
42+
->method('addCategoriesFilter')
43+
->with(['condition' => ['value']]);
44+
45+
$this->assertTrue($this->model->apply($filterMock, $collectionMock));
46+
}
47+
48+
public function testApplyWithoutCondition()
49+
{
50+
/** @var Filter|\PHPUnit_Framework_MockObject_MockObject $filterMock */
51+
$filterMock = $this->getMockBuilder(Filter::class)
52+
->disableOriginalConstructor()
53+
->getMock();
54+
55+
/** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
56+
$collectionMock = $this->getMockBuilder(Collection::class)
57+
->disableOriginalConstructor()
58+
->getMock();
59+
60+
$filterMock->expects($this->once())
61+
->method('getConditionType')
62+
->willReturn(null);
63+
$filterMock->expects($this->once())
64+
->method('getValue')
65+
->willReturn('value');
66+
67+
$collectionMock->expects($this->once())
68+
->method('addCategoriesFilter')
69+
->with(['eq' => ['value']]);
70+
71+
$this->assertTrue($this->model->apply($filterMock, $collectionMock));
72+
}
73+
}

0 commit comments

Comments
 (0)