Skip to content

Commit ca65294

Browse files
committed
Merge remote-tracking branch 'origin/AC-9607' into spartans_pr_29082024
2 parents 4b7984d + 378ade9 commit ca65294

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

app/code/Magento/Ui/Model/Export/ConvertToCsv.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,33 @@ public function getCsvFile()
8181
$searchCriteria = $dataProvider->getSearchCriteria()
8282
->setCurrentPage($i)
8383
->setPageSize($this->pageSize);
84-
$totalCount = (int) $dataProvider->getSearchResult()->getTotalCount();
85-
$totalPagesCount = (int) ceil($totalCount / $this->pageSize);
86-
while ($i <= $totalPagesCount) {
87-
// setTotalCount to prevent total count from being calculated in loop
84+
85+
$totalCount = null;
86+
$totalPagesCount = null;
87+
88+
do {
8889
$searchResult = $dataProvider->getSearchResult();
89-
$searchResult->setTotalCount($totalCount);
9090
$items = $searchResult->getItems();
91+
92+
if ($totalCount === null) { // get total count only once
93+
$totalCount = $searchResult->getTotalCount();
94+
$totalPagesCount = (int) ceil($totalCount / $this->pageSize);
95+
}
96+
97+
// call setTotalCount to prevent total count from being calculate in subsequent iterations of this loop
98+
$searchResult->setTotalCount($totalCount);
99+
// Ensure $items is always an array
100+
if ($items === null) {
101+
$items = [];
102+
}
91103
foreach ($items as $item) {
92104
$this->metadataProvider->convertDate($item, $component->getName());
93105
$stream->writeCsv($this->metadataProvider->getRowData($item, $fields, $options));
94106
}
107+
95108
$searchCriteria->setCurrentPage(++$i);
96-
}
109+
} while ($i <= $totalPagesCount);
110+
97111
$stream->unlock();
98112
$stream->close();
99113

app/code/Magento/Ui/Test/Unit/Model/Export/ConvertToCsvTest.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ protected function setUp(): void
102102
);
103103
}
104104

105+
/**
106+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
107+
*/
105108
public function testGetCsvFile()
106109
{
107110
$componentName = 'component_name';
@@ -142,7 +145,7 @@ public function testGetCsvFile()
142145
->method('getFields')
143146
->with($this->component)
144147
->willReturn([]);
145-
$this->metadataProvider->expects($this->exactly(2))
148+
$this->metadataProvider->expects($this->any())
146149
->method('getRowData')
147150
->willReturnCallback(
148151
function ($arg1, $arg2, $arg3) use ($document1, $document2, $data) {
@@ -153,7 +156,7 @@ function ($arg1, $arg2, $arg3) use ($document1, $document2, $data) {
153156
}
154157
}
155158
);
156-
$this->metadataProvider->expects($this->exactly(2))
159+
$this->metadataProvider->expects($this->any())
157160
->method('convertDate')
158161
->willReturnCallback(
159162
function ($arg1, $arg2) use ($document1, $document2, $componentName) {
@@ -245,25 +248,25 @@ private function mockComponent(string $componentName, array $page1Items, array $
245248
->method('getDataProvider')
246249
->willReturn($dataProvider);
247250

248-
$dataProvider->expects($this->exactly(3))
251+
$dataProvider->expects($this->exactly(2))
249252
->method('getSearchResult')
250253
->willReturnOnConsecutiveCalls($searchResult0, $searchResult1, $searchResult2);
251254

252255
$dataProvider->expects($this->once())
253256
->method('getSearchCriteria')
254257
->willReturn($searchCriteria);
255258

256-
$searchResult1->expects($this->once())
259+
$searchResult1->expects($this->any())
257260
->method('setTotalCount');
258261

259-
$searchResult2->expects($this->once())
262+
$searchResult2->expects($this->any())
260263
->method('setTotalCount');
261264

262-
$searchResult1->expects($this->once())
265+
$searchResult1->expects($this->any())
263266
->method('getItems')
264267
->willReturn($page1Items);
265268

266-
$searchResult2->expects($this->once())
269+
$searchResult2->expects($this->any())
267270
->method('getItems')
268271
->willReturn($page2Items);
269272

0 commit comments

Comments
 (0)