Skip to content

Commit 1819fe7

Browse files
Merge pull request magento#9281 from adobe-commerce-tier-4/Tier4-Kings-PR-09-27-2024
[Support Tier-4-Kings glo23503] 09.27.2024 Regular delivery of bugfixes and improvements
2 parents 249c371 + f70a20e commit 1819fe7

File tree

2 files changed

+132
-2
lines changed

2 files changed

+132
-2
lines changed

app/code/Magento/ImportExport/Controller/Adminhtml/Import/Validate.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class Validate extends ImportResultController implements HttpPostActionInterface
2727
*/
2828
private $import;
2929

30+
/**
31+
* @var Import
32+
*/
33+
private $_validateRowError = false;
34+
3035
/**
3136
* Validate uploaded files action
3237
*
@@ -80,11 +85,11 @@ private function processValidationResult($validationResult, $resultBlock)
8085
{
8186
$import = $this->getImport();
8287
$errorAggregator = $import->getErrorAggregator();
83-
8488
if ($import->getProcessedRowsCount()) {
8589
if ($validationResult) {
8690
$totalError = $errorAggregator->getErrorsCount();
8791
$totalRows = $import->getProcessedRowsCount();
92+
$this->validateRowError($errorAggregator, $totalRows);
8893
$this->addMessageForValidResult($resultBlock, $totalError, $totalRows);
8994
} else {
9095
$resultBlock->addError(
@@ -115,6 +120,24 @@ private function processValidationResult($validationResult, $resultBlock)
115120
}
116121
}
117122

123+
/**
124+
* Validate row error.
125+
*
126+
* @param object $errorAggregator
127+
* @param int $totalRows
128+
* @return bool
129+
*/
130+
private function validateRowError(object $errorAggregator, int $totalRows): bool
131+
{
132+
$errors = $errorAggregator->getAllErrors();
133+
$rowNumber = [];
134+
foreach ($errors as $error) {
135+
$rowNumber = array_unique([...$rowNumber , ...[$error->getRowNumber()]]);
136+
}
137+
(count($rowNumber) < $totalRows)? $this->_validateRowError = true : $this->_validateRowError = false;
138+
return $this->_validateRowError;
139+
}
140+
118141
/**
119142
* Provides import model.
120143
*
@@ -163,7 +186,7 @@ private function addMessageToSkipErrors(Result $resultBlock)
163186
*/
164187
private function addMessageForValidResult(Result $resultBlock, $totalError, $totalRows)
165188
{
166-
if ($this->getImport()->isImportAllowed() && $totalRows > $totalError) {
189+
if ($this->getImport()->isImportAllowed() && ($totalRows > $totalError || $this->_validateRowError)) {
167190
$resultBlock->addSuccess(__('File is valid! To start import process press "Import" button'), true);
168191
} else {
169192
$resultBlock->addError(__('The file is valid, but we can\'t import it for some reason.'));

app/code/Magento/ImportExport/Test/Unit/Controller/Adminhtml/Import/ValidateTest.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
use Magento\ImportExport\Model\Report\ReportProcessorInterface;
2525
use PHPUnit\Framework\MockObject\MockObject;
2626
use PHPUnit\Framework\TestCase;
27+
use Magento\ImportExport\Model\Import;
28+
use Magento\ImportExport\Model\Import\AbstractSource;
29+
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
2730

2831
/**
2932
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -55,6 +58,11 @@ class ValidateTest extends TestCase
5558
*/
5659
private $validate;
5760

61+
/**
62+
* @var Import
63+
*/
64+
private $importMock;
65+
5866
/**
5967
* @var Http|MockObject
6068
*/
@@ -70,6 +78,11 @@ class ValidateTest extends TestCase
7078
*/
7179
private $messageManagerMock;
7280

81+
/**
82+
* @var AbstractSourceMock|MockObject
83+
*/
84+
private $abstractSourceMock;
85+
7386
protected function setUp(): void
7487
{
7588
$objectManagerHelper = new ObjectManagerHelper($this);
@@ -126,12 +139,24 @@ protected function setUp(): void
126139
->disableOriginalConstructor()
127140
->getMock();
128141

142+
$this->importMock = $this->getMockBuilder(import::class)
143+
->disableOriginalConstructor()
144+
->getMock();
145+
146+
$this->abstractSourceMock = $this->getMockBuilder(AbstractSource::class)
147+
->disableOriginalConstructor()
148+
->getMockForAbstractClass();
149+
129150
$this->validate = new Validate(
130151
$this->contextMock,
131152
$this->reportProcessorMock,
132153
$this->historyMock,
133154
$this->reportHelperMock
134155
);
156+
$reflection = new \ReflectionClass($this->validate);
157+
$importProperty = $reflection->getProperty('import');
158+
$importProperty->setAccessible(true);
159+
$importProperty->setValue($this->validate, $this->importMock);
135160
}
136161

137162
/**
@@ -230,4 +255,86 @@ public function testFileWasNotUploaded()
230255

231256
$this->assertEquals($resultLayoutMock, $this->validate->execute());
232257
}
258+
259+
/**
260+
* Test execute() method
261+
*
262+
* Check the case in which the import file was not uploaded.
263+
*/
264+
public function testFileVerifiedWithImport()
265+
{
266+
$data = ['key' => 'value'];
267+
268+
$this->requestMock->expects($this->once())
269+
->method('getPostValue')
270+
->willReturn($data);
271+
272+
$resultBlock = $this->getMockBuilder(Result::class)
273+
->disableOriginalConstructor()
274+
->getMock();
275+
$resultBlock->expects($this->once())
276+
->method('addSuccess')
277+
->with(__('File is valid! To start import process press "Import" button'));
278+
279+
$layoutMock = $this->getMockBuilder(LayoutInterface::class)
280+
->getMockForAbstractClass();
281+
$layoutMock->expects($this->once())
282+
->method('getBlock')
283+
->with('import.frame.result')
284+
->willReturn($resultBlock);
285+
286+
$resultLayoutMock = $this->getMockBuilder(Layout::class)
287+
->disableOriginalConstructor()
288+
->getMock();
289+
$resultLayoutMock->expects($this->once())
290+
->method('getLayout')
291+
->willReturn($layoutMock);
292+
$this->importMock->expects($this->once())
293+
->method('setData')
294+
->with($data)
295+
->willReturn($this->importMock);
296+
$this->importMock->expects($this->once())
297+
->method('uploadFileAndGetSource')
298+
->willReturn($this->abstractSourceMock);
299+
$this->importMock->expects($this->once())
300+
->method('validateSource')
301+
->with($this->abstractSourceMock)
302+
->willReturn(true);
303+
304+
$resultBlock->expects($this->once())
305+
->method('addAction')
306+
->willReturn(
307+
['show', 'import_validation_container'],
308+
['value', Import::FIELD_IMPORT_IDS, [1, 2, 3]]
309+
);
310+
$this->importMock->expects($this->exactly(3))
311+
->method('getProcessedRowsCount')
312+
->willReturn(2);
313+
$this->importMock->expects($this->once())
314+
->method('isImportAllowed')
315+
->willReturn(true);
316+
317+
$this->importMock->expects($this->once())
318+
->method('getProcessedEntitiesCount')
319+
->willReturn(10);
320+
321+
$errorAggregatorMock = $this->createMock(ProcessingErrorAggregatorInterface::class);
322+
$this->importMock->expects($this->any())
323+
->method('getErrorAggregator')
324+
->willReturn($errorAggregatorMock);
325+
326+
$errorAggregatorMock->expects($this->exactly(3))
327+
->method('getErrorsCount')
328+
->willReturn(2);
329+
330+
$errorAggregatorMock->expects($this->once())
331+
->method('getAllErrors')
332+
->willReturn($errorAggregatorMock);
333+
334+
$this->resultFactoryMock->expects($this->any())
335+
->method('create')
336+
->with(ResultFactory::TYPE_LAYOUT)
337+
->willReturn($resultLayoutMock);
338+
$this->assertEquals($resultLayoutMock, $this->validate->execute());
339+
}
233340
}

0 commit comments

Comments
 (0)