Skip to content

Commit

Permalink
Merge pull request magento#9281 from adobe-commerce-tier-4/Tier4-King…
Browse files Browse the repository at this point in the history
…s-PR-09-27-2024

[Support Tier-4-Kings glo23503] 09.27.2024 Regular delivery of bugfixes and improvements
  • Loading branch information
2 parents 249c371 + f70a20e commit 1819fe7
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class Validate extends ImportResultController implements HttpPostActionInterface
*/
private $import;

/**
* @var Import
*/
private $_validateRowError = false;

/**
* Validate uploaded files action
*
Expand Down Expand Up @@ -80,11 +85,11 @@ private function processValidationResult($validationResult, $resultBlock)
{
$import = $this->getImport();
$errorAggregator = $import->getErrorAggregator();

if ($import->getProcessedRowsCount()) {
if ($validationResult) {
$totalError = $errorAggregator->getErrorsCount();
$totalRows = $import->getProcessedRowsCount();
$this->validateRowError($errorAggregator, $totalRows);
$this->addMessageForValidResult($resultBlock, $totalError, $totalRows);
} else {
$resultBlock->addError(
Expand Down Expand Up @@ -115,6 +120,24 @@ private function processValidationResult($validationResult, $resultBlock)
}
}

/**
* Validate row error.
*
* @param object $errorAggregator
* @param int $totalRows
* @return bool
*/
private function validateRowError(object $errorAggregator, int $totalRows): bool
{
$errors = $errorAggregator->getAllErrors();
$rowNumber = [];
foreach ($errors as $error) {
$rowNumber = array_unique([...$rowNumber , ...[$error->getRowNumber()]]);
}
(count($rowNumber) < $totalRows)? $this->_validateRowError = true : $this->_validateRowError = false;
return $this->_validateRowError;
}

/**
* Provides import model.
*
Expand Down Expand Up @@ -163,7 +186,7 @@ private function addMessageToSkipErrors(Result $resultBlock)
*/
private function addMessageForValidResult(Result $resultBlock, $totalError, $totalRows)
{
if ($this->getImport()->isImportAllowed() && $totalRows > $totalError) {
if ($this->getImport()->isImportAllowed() && ($totalRows > $totalError || $this->_validateRowError)) {
$resultBlock->addSuccess(__('File is valid! To start import process press "Import" button'), true);
} else {
$resultBlock->addError(__('The file is valid, but we can\'t import it for some reason.'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
use Magento\ImportExport\Model\Report\ReportProcessorInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Magento\ImportExport\Model\Import;
use Magento\ImportExport\Model\Import\AbstractSource;
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Expand Down Expand Up @@ -55,6 +58,11 @@ class ValidateTest extends TestCase
*/
private $validate;

/**
* @var Import
*/
private $importMock;

/**
* @var Http|MockObject
*/
Expand All @@ -70,6 +78,11 @@ class ValidateTest extends TestCase
*/
private $messageManagerMock;

/**
* @var AbstractSourceMock|MockObject
*/
private $abstractSourceMock;

protected function setUp(): void
{
$objectManagerHelper = new ObjectManagerHelper($this);
Expand Down Expand Up @@ -126,12 +139,24 @@ protected function setUp(): void
->disableOriginalConstructor()
->getMock();

$this->importMock = $this->getMockBuilder(import::class)
->disableOriginalConstructor()
->getMock();

$this->abstractSourceMock = $this->getMockBuilder(AbstractSource::class)
->disableOriginalConstructor()
->getMockForAbstractClass();

$this->validate = new Validate(
$this->contextMock,
$this->reportProcessorMock,
$this->historyMock,
$this->reportHelperMock
);
$reflection = new \ReflectionClass($this->validate);
$importProperty = $reflection->getProperty('import');
$importProperty->setAccessible(true);
$importProperty->setValue($this->validate, $this->importMock);
}

/**
Expand Down Expand Up @@ -230,4 +255,86 @@ public function testFileWasNotUploaded()

$this->assertEquals($resultLayoutMock, $this->validate->execute());
}

/**
* Test execute() method
*
* Check the case in which the import file was not uploaded.
*/
public function testFileVerifiedWithImport()
{
$data = ['key' => 'value'];

$this->requestMock->expects($this->once())
->method('getPostValue')
->willReturn($data);

$resultBlock = $this->getMockBuilder(Result::class)
->disableOriginalConstructor()
->getMock();
$resultBlock->expects($this->once())
->method('addSuccess')
->with(__('File is valid! To start import process press "Import" button'));

$layoutMock = $this->getMockBuilder(LayoutInterface::class)
->getMockForAbstractClass();
$layoutMock->expects($this->once())
->method('getBlock')
->with('import.frame.result')
->willReturn($resultBlock);

$resultLayoutMock = $this->getMockBuilder(Layout::class)
->disableOriginalConstructor()
->getMock();
$resultLayoutMock->expects($this->once())
->method('getLayout')
->willReturn($layoutMock);
$this->importMock->expects($this->once())
->method('setData')
->with($data)
->willReturn($this->importMock);
$this->importMock->expects($this->once())
->method('uploadFileAndGetSource')
->willReturn($this->abstractSourceMock);
$this->importMock->expects($this->once())
->method('validateSource')
->with($this->abstractSourceMock)
->willReturn(true);

$resultBlock->expects($this->once())
->method('addAction')
->willReturn(
['show', 'import_validation_container'],
['value', Import::FIELD_IMPORT_IDS, [1, 2, 3]]
);
$this->importMock->expects($this->exactly(3))
->method('getProcessedRowsCount')
->willReturn(2);
$this->importMock->expects($this->once())
->method('isImportAllowed')
->willReturn(true);

$this->importMock->expects($this->once())
->method('getProcessedEntitiesCount')
->willReturn(10);

$errorAggregatorMock = $this->createMock(ProcessingErrorAggregatorInterface::class);
$this->importMock->expects($this->any())
->method('getErrorAggregator')
->willReturn($errorAggregatorMock);

$errorAggregatorMock->expects($this->exactly(3))
->method('getErrorsCount')
->willReturn(2);

$errorAggregatorMock->expects($this->once())
->method('getAllErrors')
->willReturn($errorAggregatorMock);

$this->resultFactoryMock->expects($this->any())
->method('create')
->with(ResultFactory::TYPE_LAYOUT)
->willReturn($resultLayoutMock);
$this->assertEquals($resultLayoutMock, $this->validate->execute());
}
}

0 comments on commit 1819fe7

Please sign in to comment.