diff --git a/app/code/Magento/Review/Ui/DataProvider/Product/ReviewDataProvider.php b/app/code/Magento/Review/Ui/DataProvider/Product/ReviewDataProvider.php index 27cb887e89636..c41bee58863db 100644 --- a/app/code/Magento/Review/Ui/DataProvider/Product/ReviewDataProvider.php +++ b/app/code/Magento/Review/Ui/DataProvider/Product/ReviewDataProvider.php @@ -5,14 +5,14 @@ */ namespace Magento\Review\Ui\DataProvider\Product; +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\Review; /** - * DataProvider Product ReviewDataProvider + * DataProvider for product reviews * * @api * @@ -66,13 +66,7 @@ public function getData() $this->getCollection()->addEntityFilter($this->request->getParam('current_product_id', 0)) ->addStoreData(); - $params = $this->request->getParams(); - if (isset($params['sorting'])) { - $sorting = $params['sorting']; - $field = $sorting['field']; - $direction = $sorting['direction']; - $this->getCollection()->getSelect()->order($field . ' ' . $direction); - } + $this->applySorting(); $arrItems = [ 'totalRecords' => $this->getCollection()->getSize(), @@ -86,12 +80,26 @@ public function getData() return $arrItems; } + /** + * Apply sorting if it set + * + * @return void + */ + private function applySorting(): void + { + $sorting = $this->request->getParam('sorting'); + if (is_array($sorting)) { + $select = $this->getCollection()->getSelect(); + $select->order($sorting['field'] . ' ' . $sorting['direction']); + } + } + /** * @inheritdoc * @since 100.1.0 - * @return mixed|$this + * @return void */ - public function addFilter(\Magento\Framework\Api\Filter $filter) + public function addFilter(Filter $filter): void { $field = $filter->getField(); @@ -108,6 +116,5 @@ public function addFilter(\Magento\Framework\Api\Filter $filter) } parent::addFilter($filter); - return $this; } } diff --git a/dev/tests/integration/testsuite/Magento/Review/Ui/DataProvider/Product/ReviewDataProviderTest.php b/dev/tests/integration/testsuite/Magento/Review/Ui/DataProvider/Product/ReviewDataProviderTest.php new file mode 100644 index 0000000000000..f23a0261fe629 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Review/Ui/DataProvider/Product/ReviewDataProviderTest.php @@ -0,0 +1,104 @@ + 'review_listing_data_source', + 'primaryFieldName' => 'review_id', + 'requestFieldName' => 'entity_id', + ]; + + /** + * @var ObjectManagerInterface + */ + private $objectManager; + + /** + * @inheritDoc + */ + protected function setUp(): void + { + $this->objectManager = Bootstrap::getObjectManager(); + } + + /** + * Sorting dataProvider test + * + * @magentoDataFixture Magento/Review/_files/different_reviews.php + * @dataProvider sortingDataProvider + * + * @param array $sorting + * @param array $expectedSortedTitles + * @return void + */ + public function testSorting(array $sorting, 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]) + ); + + $result = $dataProvider->getData(); + + $this->assertEquals($this->getItemsField($result, 'title'), $expectedSortedTitles); + } + + /** + * Return items field data + * + * @param array $arrItems + * @param string $field + * @return array + */ + private function getItemsField(array $arrItems, string $field): array + { + $data = []; + foreach ($arrItems['items'] as $review) { + $data[] = $review[$field]; + } + + return $data; + } + + /** + * DataProvider for testSorting + * + * @return array + */ + public function sortingDataProvider(): array + { + return [ + [ + ['field' => 'title', 'direction' => 'asc'], + ['1 filter second review', '2 filter first review', 'Review Summary'], + ], + [ + ['field' => 'title', 'direction' => 'desc'], + ['Review Summary', '2 filter first review', '1 filter second review'], + ], + ]; + } +}