Skip to content

Commit

Permalink
fix: Sort Search Result when fetching Order by Quote ID (#2824)
Browse files Browse the repository at this point in the history
* fix: Sort Search Result when fetching Order by Quote ID

Multiple orders might exist for the same Quote. We should be using the most recent Order when dealing with the Payment.

* fix: Updated Unit Test for OrderRepository

* fix: Missing create call on Sort Order Builder

---------

Co-authored-by: Khushboo <[email protected]>
Co-authored-by: Can Demiralp <[email protected]>
  • Loading branch information
3 people authored Dec 13, 2024
1 parent 8e6a869 commit ea6e8fc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
9 changes: 9 additions & 0 deletions Model/Sales/OrderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Magento\Framework\Api\Search\FilterGroupBuilder;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SortOrderBuilder;
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
use Magento\Payment\Api\Data\PaymentAdditionalInfoInterfaceFactory;
use Magento\Sales\Api\Data\OrderExtensionFactory;
Expand All @@ -28,13 +29,15 @@
class OrderRepository extends SalesOrderRepository
{
private SearchCriteriaBuilder $searchCriteriaBuilder;
private SortOrderBuilder $sortOrderBuilder;
private FilterBuilder $filterBuilder;
private FilterGroupBuilder $filterGroupBuilder;

public function __construct(
SearchCriteriaBuilder $searchCriteriaBuilder,
FilterBuilder $filterBuilder,
FilterGroupBuilder $filterGroupBuilder,
SortOrderBuilder $sortOrderBuilder,
Metadata $metadata,
SearchResultFactory $searchResultFactory,
CollectionProcessorInterface $collectionProcessor = null,
Expand All @@ -56,6 +59,7 @@ public function __construct(
);

$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->sortOrderBuilder = $sortOrderBuilder;
$this->filterBuilder = $filterBuilder;
$this->filterGroupBuilder = $filterGroupBuilder;
}
Expand All @@ -68,9 +72,14 @@ public function getOrderByQuoteId(int $quoteId): OrderInterface|false
->create();

$quoteIdFilterGroup = $this->filterGroupBuilder->setFilters([$quoteIdFilter])->create();
$sortOrder = $this->sortOrderBuilder->setField('entity_id')
->setDescendingDirection()
->create();

$searchCriteria = $this->searchCriteriaBuilder
->setFilterGroups([$quoteIdFilterGroup])
->setSortOrders([$sortOrder])
->setPageSize(1)
->create();

$orders = $this->getList($searchCriteria)->getItems();
Expand Down
20 changes: 18 additions & 2 deletions Test/Unit/Model/Sales/OrderRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Magento\Framework\Api\Search\FilterGroupBuilder;
use Magento\Framework\Api\SearchCriteria;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\Api\SortOrderBuilder;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderSearchResultInterface;
use ReflectionClass;
Expand All @@ -41,10 +43,15 @@ public function testGetOrderByQuoteId()
$filterGroupBuilderMock->method('create')
->willReturn($this->createMock(FilterGroup::class));

$sortOrderBuilderMock = $this->createPartialMock(SortOrderBuilder::class, ['create']);
$sortOrderBuilderMock->method('create')
->willReturn($this->createMock(SortOrder::class));

$orderRepository = $this->buildOrderRepositoryClass(
$searchCriteriaBuilderMock,
$filterBuilderMock,
$filterGroupBuilderMock
$filterGroupBuilderMock,
$sortOrderBuilderMock
);

$order = $orderRepository->getOrderByQuoteId($quoteId);
Expand All @@ -54,7 +61,8 @@ public function testGetOrderByQuoteId()
public function buildOrderRepositoryClass(
$searchCriteriaBuilderMock = null,
$filterBuilderMock = null,
$filterGroupBuilderMock = null
$filterGroupBuilderMock = null,
$sortOrderBuilderMock = null
): OrderRepository {
if (is_null($searchCriteriaBuilderMock)) {
$searchCriteriaBuilderMock = $this->createMock(SearchCriteriaBuilder::class);
Expand All @@ -68,6 +76,10 @@ public function buildOrderRepositoryClass(
$filterGroupBuilderMock = $this->createMock(FilterGroupBuilder::class);
}

if (is_null($sortOrderBuilderMock)) {
$sortOrderBuilderMock = $this->createMock(SortOrderBuilder::class);
}

$orderRepositoryPartialMock = $this->getMockBuilder(OrderRepository::class)
->setMethods(['getList'])
->disableOriginalConstructor()
Expand All @@ -87,6 +99,10 @@ public function buildOrderRepositoryClass(
$filterGroupBuilderProperty->setAccessible(true);
$filterGroupBuilderProperty->setValue($orderRepositoryPartialMock, $filterGroupBuilderMock);

$sortOrderBuilderProperty = $reflection->getProperty('sortOrderBuilder');
$sortOrderBuilderProperty->setAccessible(true);
$sortOrderBuilderProperty->setValue($orderRepositoryPartialMock, $sortOrderBuilderMock);

$orderSearchResultMock = $this->createConfiguredMock(OrderSearchResultInterface::class, [
'getItems' => [$this->createMock(OrderInterface::class)]
]);
Expand Down

0 comments on commit ea6e8fc

Please sign in to comment.