Skip to content

Commit 1190132

Browse files
committed
Merge remote-tracking branch 'origin/MC-32153' into 2.4-develop-pr16
2 parents 0fe8477 + cdfbf14 commit 1190132

File tree

3 files changed

+52
-111
lines changed

3 files changed

+52
-111
lines changed

app/code/Magento/CustomerImportExport/Model/Import/Address.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Store\Model\Store;
1414
use Magento\CustomerImportExport\Model\ResourceModel\Import\Address\Storage as AddressStorage;
1515
use Magento\ImportExport\Model\Import\AbstractSource;
16+
use Magento\Customer\Model\Indexer\Processor;
1617

1718
/**
1819
* Customer address import
@@ -254,6 +255,11 @@ class Address extends AbstractCustomer
254255
*/
255256
private $addressStorage;
256257

258+
/**
259+
* @var Processor
260+
*/
261+
private $indexerProcessor;
262+
257263
/**
258264
* @param \Magento\Framework\Stdlib\StringUtils $string
259265
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
@@ -274,6 +280,7 @@ class Address extends AbstractCustomer
274280
* @param array $data
275281
* @param CountryWithWebsitesSource|null $countryWithWebsites
276282
* @param AddressStorage|null $addressStorage
283+
* @param Processor $indexerProcessor
277284
*
278285
* @SuppressWarnings(PHPMD.NPathComplexity)
279286
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -296,8 +303,9 @@ public function __construct(
296303
\Magento\Framework\Stdlib\DateTime $dateTime,
297304
\Magento\Customer\Model\Address\Validator\Postcode $postcodeValidator,
298305
array $data = [],
299-
CountryWithWebsitesSource $countryWithWebsites = null,
300-
AddressStorage $addressStorage = null
306+
?CountryWithWebsitesSource $countryWithWebsites = null,
307+
?AddressStorage $addressStorage = null,
308+
?Processor $indexerProcessor = null
301309
) {
302310
$this->_customerFactory = $customerFactory;
303311
$this->_addressFactory = $addressFactory;
@@ -348,6 +356,9 @@ public function __construct(
348356
$this->addressStorage = $addressStorage
349357
?: ObjectManager::getInstance()->get(AddressStorage::class);
350358

359+
$this->indexerProcessor = $indexerProcessor
360+
?: ObjectManager::getInstance()->get(Processor::class);
361+
351362
$this->_initAttributes();
352363
$this->_initCountryRegions();
353364
}
@@ -562,6 +573,7 @@ protected function _importData()
562573

563574
$this->_deleteAddressEntities($deleteRowIds);
564575
}
576+
$this->indexerProcessor->markIndexerAsInvalid();
565577
return true;
566578
}
567579

app/code/Magento/CustomerImportExport/Test/Unit/Model/Import/AddressTest.php

Lines changed: 3 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,6 @@ protected function _createCustomerEntityMock()
298298
public function getWebsites($withDefault = false)
299299
{
300300
$websites = [];
301-
if (!$withDefault) {
302-
unset($websites[0]);
303-
}
304301
foreach ($this->_websites as $id => $code) {
305302
if (!$withDefault && $id == \Magento\Store\Model\Store::DEFAULT_STORE_ID) {
306303
continue;
@@ -330,97 +327,6 @@ public function iterate(\Magento\Framework\Data\Collection $collection, $pageSiz
330327
}
331328
}
332329

333-
/**
334-
* Create mock for custom behavior test
335-
*
336-
* @return Address|\PHPUnit_Framework_MockObject_MockObject
337-
*/
338-
protected function _getModelMockForTestImportDataWithCustomBehaviour()
339-
{
340-
// input data
341-
$customBehaviorRows = [
342-
[
343-
AbstractEntity::COLUMN_ACTION => 'update',
344-
Address::COLUMN_ADDRESS_ID => $this->_customBehaviour['update_id'],
345-
],
346-
[
347-
AbstractEntity::COLUMN_ACTION => AbstractEntity::COLUMN_ACTION_VALUE_DELETE,
348-
Address::COLUMN_ADDRESS_ID => $this->_customBehaviour['delete_id']
349-
],
350-
];
351-
$updateResult = [
352-
'entity_row_new' => [],
353-
'entity_row_update' => $this->_customBehaviour['update_id'],
354-
'attributes' => [],
355-
'defaults' => [],
356-
];
357-
// entity adapter mock
358-
$modelMock = $this->createPartialMock(
359-
\Magento\CustomerImportExport\Model\Import\Address::class,
360-
[
361-
'validateRow',
362-
'_prepareDataForUpdate',
363-
'_saveAddressEntities',
364-
'_saveAddressAttributes',
365-
'_saveCustomerDefaults',
366-
'_deleteAddressEntities',
367-
'_mergeEntityAttributes',
368-
'getErrorAggregator',
369-
'getCustomerStorage',
370-
'prepareCustomerData',
371-
]
372-
);
373-
//Adding behaviours
374-
$availableBehaviors = new \ReflectionProperty($modelMock, '_availableBehaviors');
375-
$availableBehaviors->setAccessible(true);
376-
$availableBehaviors->setValue($modelMock, $this->_availableBehaviors);
377-
// mock to imitate data source model
378-
$dataSourceMock = $this->createPartialMock(
379-
\Magento\ImportExport\Model\ResourceModel\Import\Data::class,
380-
['getNextBunch', '__wakeup', 'getIterator']
381-
);
382-
$dataSourceMock->expects($this->at(0))->method('getNextBunch')->will($this->returnValue($customBehaviorRows));
383-
$dataSourceMock->expects($this->at(1))->method('getNextBunch')->will($this->returnValue(null));
384-
$dataSourceMock->expects($this->any())
385-
->method('getIterator')
386-
->willReturn($this->getMockForAbstractClass(\Iterator::class));
387-
388-
$dataSourceModel = new \ReflectionProperty(
389-
\Magento\CustomerImportExport\Model\Import\Address::class,
390-
'_dataSourceModel'
391-
);
392-
$dataSourceModel->setAccessible(true);
393-
$dataSourceModel->setValue($modelMock, $dataSourceMock);
394-
// mock expects for entity adapter
395-
$modelMock->expects($this->any())->method('validateRow')->will($this->returnValue(true));
396-
$modelMock->expects($this->any())
397-
->method('getErrorAggregator')
398-
->will($this->returnValue($this->errorAggregator));
399-
$modelMock->expects($this->any())->method('_prepareDataForUpdate')->will($this->returnValue($updateResult));
400-
$modelMock->expects(
401-
$this->any()
402-
)->method(
403-
'_saveAddressEntities'
404-
)->will(
405-
$this->returnCallback([$this, 'validateSaveAddressEntities'])
406-
);
407-
$modelMock->expects($this->any())->method('_saveAddressAttributes')->will($this->returnValue($modelMock));
408-
$modelMock->expects($this->any())->method('_saveCustomerDefaults')->will($this->returnValue($modelMock));
409-
$modelMock->expects(
410-
$this->any()
411-
)->method(
412-
'_deleteAddressEntities'
413-
)->will(
414-
$this->returnCallback([$this, 'validateDeleteAddressEntities'])
415-
);
416-
$modelMock->expects($this->any())->method('_mergeEntityAttributes')->will($this->returnValue([]));
417-
$modelMock->expects($this->any())
418-
->method('getCustomerStorage')
419-
->willReturn($this->_createCustomerStorageMock());
420-
421-
return $modelMock;
422-
}
423-
424330
/**
425331
* Create mock for customer address model class
426332
*
@@ -449,7 +355,9 @@ protected function _getModelMock()
449355
new \Magento\Framework\Stdlib\DateTime(),
450356
$this->createMock(\Magento\Customer\Model\Address\Validator\Postcode::class),
451357
$this->_getModelDependencies(),
452-
$this->countryWithWebsites
358+
$this->countryWithWebsites,
359+
$this->createMock(\Magento\CustomerImportExport\Model\ResourceModel\Import\Address\Storage::class),
360+
$this->createMock(\Magento\Customer\Model\Indexer\Processor::class)
453361
);
454362

455363
$property = new \ReflectionProperty($modelMock, '_availableBehaviors');
@@ -606,20 +514,6 @@ public function testGetDefaultAddressAttributeMapping()
606514
);
607515
}
608516

609-
/**
610-
* Test if correct methods are invoked according to different custom behaviours
611-
*
612-
* @covers \Magento\CustomerImportExport\Model\Import\Address::_importData
613-
*/
614-
public function testImportDataWithCustomBehaviour()
615-
{
616-
$this->_model = $this->_getModelMockForTestImportDataWithCustomBehaviour();
617-
$this->_model->setParameters(['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_CUSTOM]);
618-
619-
// validation in validateSaveAddressEntities and validateDeleteAddressEntities
620-
$this->_model->importData();
621-
}
622-
623517
/**
624518
* Validation method for _saveAddressEntities (callback for _saveAddressEntities)
625519
*

dev/tests/integration/testsuite/Magento/CustomerImportExport/Model/Import/AddressTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\ImportExport\Model\Import as ImportModel;
1515
use Magento\ImportExport\Model\Import\Adapter as ImportAdapter;
1616
use Magento\TestFramework\Helper\Bootstrap;
17+
use Magento\Framework\Indexer\StateInterface;
1718

1819
/**
1920
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -84,6 +85,11 @@ class AddressTest extends \PHPUnit\Framework\TestCase
8485
/** @var \Magento\Customer\Model\ResourceModel\Customer */
8586
protected $customerResource;
8687

88+
/**
89+
* @var \Magento\Customer\Model\Indexer\Processor
90+
*/
91+
private $indexerProcessor;
92+
8793
/**
8894
* Init new instance of address entity adapter
8995
*/
@@ -96,6 +102,9 @@ protected function setUp()
96102
$this->_entityAdapter = Bootstrap::getObjectManager()->create(
97103
$this->_testClassName
98104
);
105+
$this->indexerProcessor = Bootstrap::getObjectManager()->create(
106+
\Magento\Customer\Model\Indexer\Processor::class
107+
);
99108
}
100109

101110
/**
@@ -353,6 +362,7 @@ public function testImportDataAddUpdate()
353362
$requiredAttributes[] = $keyAttribute;
354363
foreach (['update', 'remove'] as $action) {
355364
foreach ($this->_updateData[$action] as $attributes) {
365+
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
356366
$requiredAttributes = array_merge($requiredAttributes, array_keys($attributes));
357367
}
358368
}
@@ -494,4 +504,29 @@ public function testDifferentOptions(): void
494504
$imported = $this->_entityAdapter->importData();
495505
$this->assertTrue($imported, 'Must be successfully imported');
496506
}
507+
508+
/**
509+
* Test customer indexer gets invalidated after import when Update on Schedule mode is set
510+
*
511+
* @magentoDbIsolation enabled
512+
*/
513+
public function testCustomerIndexer(): void
514+
{
515+
$file = __DIR__ . '/_files/address_import_update.csv';
516+
$filesystem = Bootstrap::getObjectManager()->create(Filesystem::class);
517+
$directoryWrite = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
518+
$source = new \Magento\ImportExport\Model\Import\Source\Csv($file, $directoryWrite);
519+
$this->_entityAdapter
520+
->setParameters(['behavior' => ImportModel::BEHAVIOR_ADD_UPDATE])
521+
->setSource($source)
522+
->validateData()
523+
->hasToBeTerminated();
524+
$this->indexerProcessor->getIndexer()->reindexAll();
525+
$statusBeforeImport = $this->indexerProcessor->getIndexer()->getStatus();
526+
$this->indexerProcessor->getIndexer()->setScheduled(true);
527+
$this->_entityAdapter->importData();
528+
$statusAfterImport = $this->indexerProcessor->getIndexer()->getStatus();
529+
$this->assertEquals(StateInterface::STATUS_VALID, $statusBeforeImport);
530+
$this->assertEquals(StateInterface::STATUS_INVALID, $statusAfterImport);
531+
}
497532
}

0 commit comments

Comments
 (0)