Skip to content

Commit 6626381

Browse files
author
Sergey Semenov
committed
MAGETWO-36033: Exception if trying to sort Customer Groups by Tax Class
1 parent 0389ffb commit 6626381

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

app/code/Magento/Customer/Model/Resource/GroupRepository.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ public function getList(SearchCriteriaInterface $searchCriteria)
164164

165165
/** @var \Magento\Customer\Model\Resource\Group\Collection $collection */
166166
$collection = $this->groupFactory->create()->getCollection();
167+
$collection->addTaxClass();
167168

168169
//Add filters from root filter group to the collection
169170
/** @var FilterGroup $group */
@@ -234,6 +235,8 @@ protected function translateField($field)
234235
return 'customer_group_code';
235236
case GroupInterface::ID:
236237
return 'customer_group_id';
238+
case GroupInterface::TAX_CLASS_NAME:
239+
return 'class_name';
237240
default:
238241
return $field;
239242
}

dev/tests/integration/testsuite/Magento/Customer/Model/Resource/GroupRepositoryTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Customer\Model\Resource;
88

99
use Magento\Customer\Api\Data\GroupInterface;
10+
use Magento\Framework\Api\SearchCriteria;
1011

1112
/**
1213
* Integration test for \Magento\Customer\Model\Resource\GroupRepository
@@ -28,12 +29,16 @@ class GroupRepositoryTest extends \PHPUnit_Framework_TestCase
2829
/** @var \Magento\Framework\Api\SearchCriteriaBuilder */
2930
private $searchCriteriaBuilder;
3031

32+
/** @var \Magento\Framework\Api\SortOrderBuilder */
33+
private $sortOrderBuilder;
34+
3135
protected function setUp()
3236
{
3337
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
3438
$this->groupRepository = $this->objectManager->create('Magento\Customer\Api\GroupRepositoryInterface');
3539
$this->groupFactory = $this->objectManager->create('Magento\Customer\Api\Data\GroupInterfaceFactory');
3640
$this->searchCriteriaBuilder = $this->objectManager->create('Magento\Framework\Api\SearchCriteriaBuilder');
41+
$this->sortOrderBuilder = $this->objectManager->create('Magento\Framework\Api\SortOrderBuilder');
3742
}
3843

3944
/**
@@ -249,6 +254,95 @@ public function searchGroupsDataProvider()
249254
3 => [GroupInterface::CODE => 'Retailer', GroupInterface::TAX_CLASS_ID => 3]
250255
],
251256
],
257+
'like_tax_name' => [
258+
[
259+
$builder->setField(GroupInterface::TAX_CLASS_NAME)->setValue('Retail Customer')
260+
->setConditionType('like')
261+
->create(),
262+
],
263+
[],
264+
[
265+
0 => [GroupInterface::CODE => 'NOT LOGGED IN', GroupInterface::TAX_CLASS_ID => 3],
266+
1 => [GroupInterface::CODE => 'General', GroupInterface::TAX_CLASS_ID => 3],
267+
2 => [GroupInterface::CODE => 'Wholesale', GroupInterface::TAX_CLASS_ID => 3],
268+
3 => [GroupInterface::CODE => 'Retailer', GroupInterface::TAX_CLASS_ID => 3],
269+
],
270+
],
271+
];
272+
}
273+
274+
/**
275+
* @param string $field
276+
* @param string, $direction
277+
* @param string, $methodName
278+
* @param array $expectedResult
279+
*
280+
* @dataProvider sortOrderDataProvider
281+
*/
282+
public function testGetListSortOrder($field, $direction, $methodName, $expectedResult)
283+
{
284+
/** @var \Magento\Framework\Api\SortOrder $sortOrder */
285+
/** @var string $direction */
286+
$direction = ($direction == 'ASC') ? SearchCriteria::SORT_ASC : SearchCriteria::SORT_DESC;
287+
$sortOrder = $this->sortOrderBuilder->setField($field)->setDirection($direction)->create();
288+
$this->searchCriteriaBuilder->addSortOrder($sortOrder);
289+
290+
$searchResults = $this->groupRepository->getList($this->searchCriteriaBuilder->create());
291+
292+
/** @var \Magento\Customer\Api\Data\GroupInterface[] $resultItems */
293+
$resultItems = $searchResults->getItems();
294+
$this->assertTrue(count($resultItems) > 0);
295+
296+
$result = [];
297+
foreach ($resultItems as $item) {
298+
/** @var \Magento\Customer\Model\Data\Group $item */
299+
$result[] = $item->$methodName();
300+
}
301+
$this->assertEquals($expectedResult, $result);
302+
}
303+
304+
/**
305+
* @return array
306+
*/
307+
public function sortOrderDataProvider()
308+
{
309+
return [
310+
[
311+
GroupInterface::ID,
312+
'ASC',
313+
'getId',
314+
[0, 1, 2, 3],
315+
],
316+
[
317+
GroupInterface::ID,
318+
'DESC',
319+
'getId',
320+
[3, 2, 1, 0],
321+
],
322+
[
323+
GroupInterface::CODE,
324+
'ASC',
325+
'getCode',
326+
['General', 'NOT LOGGED IN', 'Retailer', 'Wholesale'],
327+
],
328+
[
329+
GroupInterface::CODE,
330+
'DESC',
331+
'getCode',
332+
['Wholesale', 'Retailer', 'NOT LOGGED IN', 'General'],
333+
],
334+
[
335+
GroupInterface::TAX_CLASS_NAME,
336+
'ASC',
337+
'getTaxClassName',
338+
['Retail Customer', 'Retail Customer', 'Retail Customer', 'Retail Customer']
339+
],
340+
[
341+
GroupInterface::TAX_CLASS_NAME,
342+
'DESC',
343+
'getTaxClassName',
344+
['Retail Customer', 'Retail Customer', 'Retail Customer', 'Retail Customer']
345+
],
252346
];
253347
}
254348
}

0 commit comments

Comments
 (0)