Skip to content

Commit

Permalink
product review DataProvider sorting, integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
engcom-Charlie committed Nov 11, 2020
1 parent c1eb748 commit 1c6672e
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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(),
Expand All @@ -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();

Expand All @@ -108,6 +116,5 @@ public function addFilter(\Magento\Framework\Api\Filter $filter)
}

parent::addFilter($filter);
return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Review\Ui\DataProvider\Product;

use Magento\Framework\App\RequestInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* Test for \Magento\Review\Ui\DataProvider\Product\ReviewDataProvider.
*/
class ReviewDataProviderTest extends TestCase
{
/**
* @var array
*/
private $modelParams = [
'name' => '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'],
],
];
}
}

0 comments on commit 1c6672e

Please sign in to comment.