Skip to content

Commit 999675a

Browse files
authored
Merge pull request #1093 from magento-fearless-kiwis/FearlessKiwis-MAGETWO-66402-mov-get-allids
- MAGETWO-66402: Backward Incompatible Change
2 parents 541e139 + 8d59d8c commit 999675a

File tree

7 files changed

+110
-47
lines changed

7 files changed

+110
-47
lines changed

app/code/Magento/Customer/Model/ResourceModel/Grid/Collection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class Collection extends \Magento\Framework\View\Element\UiComponent\DataProvide
1919
*/
2020
protected $document = Document::class;
2121

22+
/**
23+
* @inheritdoc
24+
*/
25+
protected $_map = ['fields' => ['entity_id' => 'main_table.entity_id']];
26+
2227
/**
2328
* Initialize dependencies.
2429
*

app/code/Magento/Ui/Component/MassAction/Filter.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public function getCollection(AbstractDb $collection)
9999
throw new LocalizedException(__('Please select item(s).'));
100100
}
101101
}
102+
/** @var \Magento\Customer\Model\ResourceModel\Customer\Collection $collection */
102103
$idsArray = $this->getFilterIds();
103104
if (!empty($idsArray)) {
104105
$collection->addFieldToFilter(
@@ -217,10 +218,19 @@ private function getDataProvider()
217218
*/
218219
private function getFilterIds()
219220
{
221+
$idsArray = [];
220222
$this->applySelectionOnTargetProvider();
221-
if ($this->getDataProvider()->getSearchResult()) {
222-
return $this->getDataProvider()->getSearchResult()->getAllIds();
223+
if ($this->getDataProvider() instanceof \Magento\Ui\DataProvider\AbstractDataProvider) {
224+
// Use collection's getAllIds for optimization purposes.
225+
$idsArray = $this->getDataProvider()->getAllIds();
226+
} else {
227+
$searchResult = $this->getDataProvider()->getSearchResult();
228+
// Use compatible search api getItems when searchResult is not a collection.
229+
foreach ($searchResult->getItems() as $item) {
230+
/** @var $item \Magento\Framework\Api\Search\DocumentInterface */
231+
$idsArray[] = $item->getId();
232+
}
223233
}
224-
return [];
234+
return $idsArray;
225235
}
226236
}

app/code/Magento/Ui/DataProvider/AbstractDataProvider.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public function getSearchCriteria()
172172
/**
173173
* Returns SearchResult
174174
*
175-
* @return null
175+
* @return \Magento\Framework\Api\Search\SearchResultInterface
176176
*/
177177
public function getSearchResult()
178178
{
@@ -279,4 +279,14 @@ public function setConfigData($config)
279279
{
280280
$this->data['config'] = $config;
281281
}
282+
283+
/**
284+
* Retrieve all ids from collection
285+
*
286+
* @return int[]
287+
*/
288+
public function getAllIds()
289+
{
290+
return $this->collection->getAllIds();
291+
}
282292
}

app/code/Magento/Ui/Test/Unit/Component/MassAction/FilterTest.php

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
use Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface;
1818
use Magento\Framework\View\Element\UiComponent\ContextInterface;
1919

20+
/**
21+
* Class FilterTest
22+
*
23+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
24+
*/
2025
class FilterTest extends \PHPUnit_Framework_TestCase
2126
{
2227
/**
@@ -62,6 +67,11 @@ class FilterTest extends \PHPUnit_Framework_TestCase
6267
*/
6368
private $uiComponentMock;
6469

70+
/**
71+
* \PHPUnit_Framework_MockObject_MockObject
72+
*/
73+
private $contextMock;
74+
6575
/**
6676
* Set up
6777
*/
@@ -74,7 +84,7 @@ protected function setUp()
7484
$this->dataProviderMock = $this->getMock(DataProviderInterface::class);
7585
$this->uiComponentMock = $this->getMock(UiComponentInterface::class);
7686
$this->abstractDbMock = $this->getMock(AbstractDb::class, [], [], '', false);
77-
$contextMock = $this->getMock(ContextInterface::class);
87+
$this->contextMock = $this->getMock(ContextInterface::class);
7888
$this->searchResultMock = $this->getMock(SearchResultInterface::class);
7989
$uiComponentMockTwo = $this->getMock(UiComponentInterface::class);
8090
$this->filter = $this->objectManager->getObject(
@@ -96,18 +106,7 @@ protected function setUp()
96106
->willReturn([]);
97107
$this->uiComponentMock->expects($this->any())
98108
->method('getContext')
99-
->willReturn($contextMock);
100-
$contextMock->expects($this->any())
101-
->method('getDataProvider')
102-
->willReturn($this->dataProviderMock);
103-
$this->dataProviderMock->expects($this->any())
104-
->method('setLimit');
105-
$this->dataProviderMock->expects($this->any())
106-
->method('searchResultMock')
107-
->willReturn($this->searchResultMock);
108-
$this->searchResultMock->expects($this->any())
109-
->method('getAllIds')
110-
->willReturn([]);
109+
->willReturn($this->contextMock);
111110
}
112111

113112
/**
@@ -144,6 +143,18 @@ public function applySelectionOnTargetProviderDataProvider()
144143
*/
145144
public function testApplySelectionOnTargetProviderException()
146145
{
146+
$this->contextMock->expects($this->any())
147+
->method('getDataProvider')
148+
->willReturn($this->dataProviderMock);
149+
$this->dataProviderMock->expects($this->any())
150+
->method('setLimit');
151+
$this->dataProviderMock->expects($this->any())
152+
->method('getSearchResult')
153+
->willReturn($this->searchResultMock);
154+
$this->searchResultMock->expects($this->any())
155+
->method('getItems')
156+
->willReturn([]);
157+
147158
$filterMock = $this->getMock(ApiFilter::class, [], [], '', false);
148159
$this->filterBuilderMock->expects($this->any())
149160
->method('setConditionType')
@@ -170,7 +181,7 @@ public function testApplySelectionOnTargetProviderException()
170181
}
171182

172183
/**
173-
* Run test for getCollection method
184+
* Run test for getCollection method with SearchResultInterface
174185
*
175186
* @param int[]|bool $selectedIds
176187
* @param int[]|bool $excludedIds
@@ -196,6 +207,44 @@ public function testGetCollection($selectedIds, $excludedIds, $filterExpected, $
196207
$this->assertEquals($this->abstractDbMock, $this->filter->getCollection($this->abstractDbMock));
197208
}
198209

210+
/**
211+
* Run test for getCollection method with collection
212+
*
213+
* @param int[]|bool $selectedIds
214+
* @param int[]|bool $excludedIds
215+
* @param int $filterExpected
216+
* @param string $conditionExpected
217+
* @dataProvider applySelectionOnTargetProviderDataProvider
218+
*/
219+
public function testGetCollectionWithCollection($selectedIds, $excludedIds, $filterExpected, $conditionExpected)
220+
{
221+
$this->dataProviderMock = $this->getMock(
222+
\Magento\Ui\DataProvider\AbstractDataProvider::class,
223+
[],
224+
[],
225+
'',
226+
false
227+
);
228+
$this->contextMock->expects($this->any())
229+
->method('getDataProvider')
230+
->willReturn($this->dataProviderMock);
231+
$this->dataProviderMock->expects($this->any())
232+
->method('getAllIds')
233+
->willReturn([1, 2, 3]);
234+
235+
$this->setUpApplySelection($selectedIds, $excludedIds, $filterExpected, $conditionExpected);
236+
237+
$this->requestMock->expects($this->any())
238+
->method('getParam')
239+
->willReturnMap([
240+
['namespace', null, ''],
241+
[Filter::SELECTED_PARAM, null, $selectedIds],
242+
[Filter::EXCLUDED_PARAM, null, $excludedIds],
243+
]);
244+
245+
$this->assertEquals($this->abstractDbMock, $this->filter->getCollection($this->abstractDbMock));
246+
}
247+
199248
/**
200249
* This tests the method prepareComponent()
201250
*/
@@ -221,6 +270,9 @@ public function testGetComponent()
221270
*/
222271
public function testGetComponentRefererUrlIsNotNull()
223272
{
273+
$this->contextMock->expects($this->any())
274+
->method('getDataProvider')
275+
->willReturn($this->dataProviderMock);
224276
$returnArray = [
225277
'referer_url' => 'referer_url'
226278
];
@@ -235,6 +287,9 @@ public function testGetComponentRefererUrlIsNotNull()
235287
*/
236288
public function testGetComponentRefererUrlIsNull()
237289
{
290+
$this->contextMock->expects($this->any())
291+
->method('getDataProvider')
292+
->willReturn($this->dataProviderMock);
238293
$this->assertNull($this->filter->getComponentRefererUrl());
239294
}
240295

@@ -248,6 +303,17 @@ public function testGetComponentRefererUrlIsNull()
248303
*/
249304
private function setUpApplySelection($selectedIds, $excludedIds, $filterExpected, $conditionExpected)
250305
{
306+
$this->contextMock->expects($this->any())
307+
->method('getDataProvider')
308+
->willReturn($this->dataProviderMock);
309+
$this->dataProviderMock->expects($this->any())
310+
->method('setLimit');
311+
$this->dataProviderMock->expects($this->any())
312+
->method('getSearchResult')
313+
->willReturn($this->searchResultMock);
314+
$this->searchResultMock->expects($this->any())
315+
->method('getItems')
316+
->willReturn([new \Magento\Framework\DataObject(['id' => 1])]);
251317
$filterMock = $this->getMock(ApiFilter::class, [], [], '', false);
252318
$this->requestMock->expects($this->at(0))
253319
->method('getParam')
@@ -260,6 +326,7 @@ private function setUpApplySelection($selectedIds, $excludedIds, $filterExpected
260326
$this->dataProviderMock->expects($this->exactly($filterExpected))
261327
->method('addFilter')
262328
->with($filterMock);
329+
263330
$this->filterBuilderMock->expects($this->exactly($filterExpected))
264331
->method('setConditionType')
265332
->with($conditionExpected)

lib/internal/Magento/Framework/Api/Search/SearchResult.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,4 @@ public function setTotalCount($totalCount)
8585
{
8686
return $this->setData(self::TOTAL_COUNT, $totalCount);
8787
}
88-
89-
/**
90-
* Retrieve ids of all items
91-
*
92-
* @return array
93-
*/
94-
public function getAllIds()
95-
{
96-
$ids = [];
97-
foreach ($this->getItems() as $item) {
98-
$ids[] = $item->getId();
99-
}
100-
return $ids;
101-
}
10288
}

lib/internal/Magento/Framework/Api/Search/SearchResultInterface.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,4 @@ public function setAggregations($aggregations);
5353
* @return \Magento\Framework\Api\Search\SearchCriteriaInterface
5454
*/
5555
public function getSearchCriteria();
56-
57-
/**
58-
* Retrieve all ids from list
59-
*
60-
* @return int[]
61-
*/
62-
public function getAllIds();
6356
}

lib/internal/Magento/Framework/Api/Test/Unit/Search/SearchResultTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,6 @@ protected function setUp()
5454
);
5555
}
5656

57-
/**
58-
* Test getAllIds
59-
*/
60-
public function testGetAllIds()
61-
{
62-
$this->assertEquals([1, 2], $this->search->getAllIds());
63-
}
64-
6557
/**
6658
* Test getItems
6759
*/

0 commit comments

Comments
 (0)