Skip to content

Commit

Permalink
Merge pull request #258 from magento-south/MAGETWO-56012
Browse files Browse the repository at this point in the history
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
  • Loading branch information
VladimirZaets authored Aug 22, 2016
2 parents 5a615bb + 5354548 commit eacac63
Show file tree
Hide file tree
Showing 172 changed files with 7,861 additions and 1,927 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,25 @@ private function isExpiredAuthorization(OrderPaymentInterface $payment)
*/
private function isExistsCaptureTransaction(OrderPaymentInterface $payment)
{
$filters[] = $this->filterBuilder->setField('payment_id')
->setValue($payment->getId())
->create();
$this->searchCriteriaBuilder->addFilters(
[
$this->filterBuilder
->setField('payment_id')
->setValue($payment->getId())
->create(),
]
);

$filters[] = $this->filterBuilder->setField('txn_type')
->setValue(TransactionInterface::TYPE_CAPTURE)
->create();
$this->searchCriteriaBuilder->addFilters(
[
$this->filterBuilder
->setField('txn_type')
->setValue(TransactionInterface::TYPE_CAPTURE)
->create(),
]
);

$searchCriteria = $this->searchCriteriaBuilder->addFilters($filters)
->create();
$searchCriteria = $this->searchCriteriaBuilder->create();

$count = $this->transactionRepository->getList($searchCriteria)->getTotalCount();
return (boolean) $count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ private function buildSearchCriteria()
->willReturnSelf();

$searchCriteria = new SearchCriteria();
$this->searchCriteriaBuilder->expects(static::once())
$this->searchCriteriaBuilder->expects(static::exactly(2))
->method('addFilters')
->willReturnSelf();
$this->searchCriteriaBuilder->expects(static::once())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function testBuild()
$paymentExtension = $this->getMockBuilder(OrderPaymentExtension::class)
->setMethods(['getVaultPaymentToken'])
->disableOriginalConstructor()
->getMock();
->getMockForAbstractClass();

$paymentToken = $this->getMockBuilder(PaymentToken::class)
->disableOriginalConstructor()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor;

use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Framework\Api\Filter;
use Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface;
use Magento\Framework\Data\Collection\AbstractDb;

class ProductCategoryFilter implements CustomFilterInterface
{
/**
* Apply category_id Filter to Product Collection
*
* @param Filter $filter
* @param AbstractDb $collection
* @return bool Whether the filter is applied
*/
public function apply(Filter $filter, AbstractDb $collection)
{
$conditionType = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
$categoryFilter = [$conditionType => [$filter->getValue()]];

/** @var Collection $collection */
$collection->addCategoriesFilter($categoryFilter);

return true;
}
}
54 changes: 20 additions & 34 deletions app/code/Magento/Catalog/Model/CategoryList.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
use Magento\Catalog\Model\ResourceModel\Category\Collection;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
use Magento\Framework\Api\Search\FilterGroup;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;

class CategoryList implements CategoryListInterface
{
Expand All @@ -39,22 +37,30 @@ class CategoryList implements CategoryListInterface
*/
private $categoryRepository;

/**
* @var CollectionProcessorInterface
*/
private $collectionProcessor;

/**
* @param CollectionFactory $categoryCollectionFactory
* @param JoinProcessorInterface $extensionAttributesJoinProcessor
* @param CategorySearchResultsInterfaceFactory $categorySearchResultsFactory
* @param CategoryRepositoryInterface $categoryRepository
* @param CollectionProcessorInterface $collectionProcessor
*/
public function __construct(
CollectionFactory $categoryCollectionFactory,
JoinProcessorInterface $extensionAttributesJoinProcessor,
CategorySearchResultsInterfaceFactory $categorySearchResultsFactory,
CategoryRepositoryInterface $categoryRepository
CategoryRepositoryInterface $categoryRepository,
CollectionProcessorInterface $collectionProcessor = null
) {
$this->categoryCollectionFactory = $categoryCollectionFactory;
$this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
$this->categorySearchResultsFactory = $categorySearchResultsFactory;
$this->categoryRepository = $categoryRepository;
$this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
}

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

foreach ($searchCriteria->getFilterGroups() as $group) {
$this->addFilterGroupToCollection($group, $collection);
}

/** @var SortOrder $sortOrder */
$sortOrders = $searchCriteria->getSortOrders();
if ($sortOrders) {
foreach ($sortOrders as $sortOrder) {
$collection->addOrder(
$sortOrder->getField(),
($sortOrder->getDirection() === SortOrder::SORT_ASC) ? SortOrder::SORT_ASC : SortOrder::SORT_DESC
);
}
}

$collection->setCurPage($searchCriteria->getCurrentPage());
$collection->setPageSize($searchCriteria->getPageSize());
$this->collectionProcessor->process($searchCriteria, $collection);

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

/**
* Add filter group to collection
* Retrieve collection processor
*
* @param FilterGroup $filterGroup
* @param Collection $collection
* @return void
* @deprecated
* @return CollectionProcessorInterface
*/
private function addFilterGroupToCollection(FilterGroup $filterGroup, Collection $collection)
private function getCollectionProcessor()
{
$filters = $filterGroup->getFilters();
if ($filters) {
$fields = [];
foreach ($filters as $filter) {
$conditionType = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
$fields[] = ['attribute' => $filter->getField(), $conditionType => $filter->getValue()];
}
$collection->addFieldToFilter($fields);
if (!$this->collectionProcessor) {
$this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
'Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor'
);
}
return $this->collectionProcessor;
}
}
45 changes: 29 additions & 16 deletions app/code/Magento/Catalog/Model/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Magento\Framework\Api\Data\ImageContentInterfaceFactory;
use Magento\Framework\Api\ImageContentValidatorInterface;
use Magento\Framework\Api\ImageProcessorInterface;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use Magento\Framework\DB\Adapter\ConnectionException;
use Magento\Framework\DB\Adapter\DeadlockException;
use Magento\Framework\DB\Adapter\LockWaitException;
Expand Down Expand Up @@ -135,6 +135,11 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
*/
protected $mediaGalleryProcessor;

/**
* @var CollectionProcessorInterface
*/
private $collectionProcessor;

/**
* ProductRepository constructor.
* @param ProductFactory $productFactory
Expand All @@ -157,6 +162,7 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
* @param MimeTypeExtensionMap $mimeTypeExtensionMap
* @param ImageProcessorInterface $imageProcessor
* @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
* @param CollectionProcessorInterface $collectionProcessor
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
Expand All @@ -180,7 +186,8 @@ public function __construct(
ImageContentInterfaceFactory $contentFactory,
MimeTypeExtensionMap $mimeTypeExtensionMap,
ImageProcessorInterface $imageProcessor,
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor,
CollectionProcessorInterface $collectionProcessor = null
) {
$this->productFactory = $productFactory;
$this->collectionFactory = $collectionFactory;
Expand All @@ -199,6 +206,7 @@ public function __construct(
$this->contentFactory = $contentFactory;
$this->imageProcessor = $imageProcessor;
$this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
$this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
}

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

//Add filters from root filter group to the collection
foreach ($searchCriteria->getFilterGroups() as $group) {
$this->addFilterGroupToCollection($group, $collection);
}
/** @var SortOrder $sortOrder */
foreach ((array)$searchCriteria->getSortOrders() as $sortOrder) {
$field = $sortOrder->getField();
$collection->addOrder(
$field,
($sortOrder->getDirection() == SortOrder::SORT_ASC) ? 'ASC' : 'DESC'
);
}
$collection->setCurPage($searchCriteria->getCurrentPage());
$collection->setPageSize($searchCriteria->getPageSize());
$this->collectionProcessor->process($searchCriteria, $collection);

$collection->load();

$searchResult = $this->searchResultsFactory->create();
Expand All @@ -647,6 +643,7 @@ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCr
/**
* Helper function that adds a FilterGroup to the collection.
*
* @deprecated
* @param \Magento\Framework\Api\Search\FilterGroup $filterGroup
* @param Collection $collection
* @return void
Expand Down Expand Up @@ -698,4 +695,20 @@ private function getMediaGalleryProcessor()
}
return $this->mediaGalleryProcessor;
}

/**
* Retrieve collection processor
*
* @deprecated
* @return CollectionProcessorInterface
*/
private function getCollectionProcessor()
{
if (!$this->collectionProcessor) {
$this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
'Magento\Catalog\Model\Api\SearchCriteria\ProductCollectionProcessor'
);
}
return $this->collectionProcessor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Test\Unit\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor;

use Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductCategoryFilter;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Framework\Api\Filter;

class ProductCategoryFilterTest extends \PHPUnit_Framework_TestCase
{
/** @var ProductCategoryFilter */
private $model;

protected function setUp()
{
$this->model = new ProductCategoryFilter();
}

public function testApply()
{
/** @var Filter|\PHPUnit_Framework_MockObject_MockObject $filterMock */
$filterMock = $this->getMockBuilder(Filter::class)
->disableOriginalConstructor()
->getMock();

/** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
$collectionMock = $this->getMockBuilder(Collection::class)
->disableOriginalConstructor()
->getMock();

$filterMock->expects($this->exactly(2))
->method('getConditionType')
->willReturn('condition');
$filterMock->expects($this->once())
->method('getValue')
->willReturn('value');

$collectionMock->expects($this->once())
->method('addCategoriesFilter')
->with(['condition' => ['value']]);

$this->assertTrue($this->model->apply($filterMock, $collectionMock));
}

public function testApplyWithoutCondition()
{
/** @var Filter|\PHPUnit_Framework_MockObject_MockObject $filterMock */
$filterMock = $this->getMockBuilder(Filter::class)
->disableOriginalConstructor()
->getMock();

/** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
$collectionMock = $this->getMockBuilder(Collection::class)
->disableOriginalConstructor()
->getMock();

$filterMock->expects($this->once())
->method('getConditionType')
->willReturn(null);
$filterMock->expects($this->once())
->method('getValue')
->willReturn('value');

$collectionMock->expects($this->once())
->method('addCategoriesFilter')
->with(['eq' => ['value']]);

$this->assertTrue($this->model->apply($filterMock, $collectionMock));
}
}
Loading

0 comments on commit eacac63

Please sign in to comment.