Skip to content

Commit

Permalink
fix review sorting via rewrite addOrder, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
engcom-Charlie committed Nov 18, 2020
1 parent f18ab23 commit da7bb72
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ public function testGetData()
$this->collectionMock->expects($this->once())
->method('addStoreData')
->willReturnSelf();
$this->requestMock->expects($this->exactly(2))
$this->requestMock->expects($this->once())
->method('getParam')
->withConsecutive(['current_product_id', 0], ['sorting'])
->willReturnOnConsecutiveCalls(1, null);
->with('current_product_id', 0)
->willReturn(1);

$this->assertSame($expected, $this->model->getData());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

use Magento\Framework\Api\Filter;
use Magento\Framework\App\RequestInterface;
use Magento\Ui\DataProvider\AbstractDataProvider;
use Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory;
use Magento\Review\Model\ResourceModel\Review\Product\Collection;
use Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory;
use Magento\Ui\DataProvider\AbstractDataProvider;

/**
* DataProvider for product reviews
Expand Down Expand Up @@ -66,8 +66,6 @@ public function getData()
$this->getCollection()->addEntityFilter($this->request->getParam('current_product_id', 0))
->addStoreData();

$this->applySorting();

$arrItems = [
'totalRecords' => $this->getCollection()->getSize(),
'items' => [],
Expand All @@ -81,17 +79,32 @@ public function getData()
}

/**
* Apply sorting if it set
* Returns prepared field name
*
* @return void
* @param string $name
* @return string
*/
private function applySorting(): void
private function getPreparedField(string $name): string
{
$sorting = $this->request->getParam('sorting');
if (is_array($sorting)) {
$select = $this->getCollection()->getSelect();
$select->order($sorting['field'] . ' ' . $sorting['direction']);
$preparedName = '';

if (in_array($name, ['review_id', 'created_at', 'status_id'])) {
$preparedName = 'rt.' . $name;
} elseif (in_array($name, ['title', 'nickname', 'detail'])) {
$preparedName = 'rdt.' . $name;
} elseif ($name === 'review_created_at') {
$preparedName = 'rt.created_at';
}

return $preparedName ?: $name;
}

/**
* @inheritDoc
*/
public function addOrder($field, $direction): void
{
$this->getCollection()->setOrder($this->getPreparedField($field), $direction);
}

/**
Expand All @@ -102,18 +115,7 @@ private function applySorting(): void
public function addFilter(Filter $filter): void
{
$field = $filter->getField();

if (in_array($field, ['review_id', 'created_at', 'status_id'])) {
$filter->setField('rt.' . $field);
}

if (in_array($field, ['title', 'nickname', 'detail'])) {
$filter->setField('rdt.' . $field);
}

if ($field === 'review_created_at') {
$filter->setField('rt.created_at');
}
$filter->setField($this->getPreparedField($field));

parent::addFilter($filter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ protected function setUp(): void
* @magentoDataFixture Magento/Review/_files/different_reviews.php
* @dataProvider sortingDataProvider
*
* @param array $sorting
* @param string $field
* @param string $direction
* @param array $expectedSortedTitles
* @return void
*/
public function testSorting(array $sorting, array $expectedSortedTitles): void
public function testSorting(string $field, string $direction, array $expectedSortedTitles): void
{
$request = $this->objectManager->create(RequestInterface::class);
$request->setParam('sorting', $sorting);
$request->setParam('current_product_id', 1);

$dataProvider = $this->objectManager->create(
ReviewDataProvider::class,
array_merge($this->modelParams, ['request' => $request])
);

$dataProvider->addOrder($field, $direction);
$result = $dataProvider->getData();

$this->assertEquals($this->getItemsField($result, 'title'), $expectedSortedTitles);
Expand Down Expand Up @@ -91,12 +91,14 @@ private function getItemsField(array $arrItems, string $field): array
public function sortingDataProvider(): array
{
return [
[
['field' => 'title', 'direction' => 'asc'],
'sort by title field ascending' => [
'title',
'asc',
['1 filter second review', '2 filter first review', 'Review Summary'],
],
[
['field' => 'title', 'direction' => 'desc'],
'sort by title field descending' => [
'title',
'desc',
['Review Summary', '2 filter first review', '1 filter second review'],
],
];
Expand Down

0 comments on commit da7bb72

Please sign in to comment.