Skip to content

Commit

Permalink
MAGETWO-52161: [GitHub] Bug in EAV with group_price attribute #69
Browse files Browse the repository at this point in the history
  • Loading branch information
le0n4eg committed Aug 19, 2016
1 parent fbb9e3c commit c98fcdf
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 34 deletions.
29 changes: 1 addition & 28 deletions src/Migration/Step/Eav/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,6 @@ class Data implements StageInterface, RollbackInterface
*/
protected $readerGroups;

/**
* @var \Migration\Reader\Groups
*/
protected $readerAttributes;

/**
* @var array
*/
Expand Down Expand Up @@ -146,7 +141,6 @@ public function __construct(
$this->destination = $destination;
$this->map = $mapFactory->create('eav_map_file');
$this->readerGroups = $groupsFactory->create('eav_document_groups_file');
$this->readerAttributes = $groupsFactory->create('eav_attribute_groups_file');
$this->helper = $helper;
$this->factory = $factory;
$this->initialData = $initialData;
Expand Down Expand Up @@ -389,10 +383,7 @@ protected function migrateAttributes()
$this->map->getDocumentMap($sourceDocName, MapInterface::TYPE_SOURCE)
);
$this->destination->backupDocument($destinationDocument->getName());
$sourceRecords = $this->source->getRecords($sourceDocName, 0, $this->source->getRecordsCount($sourceDocName));
foreach (array_keys($this->readerAttributes->getGroup('ignore')) as $attributeToClear) {
$sourceRecords = $this->clearIgnoredAttributes($sourceRecords, $attributeToClear);
}
$sourceRecords = $this->initialData->getAttributes('source');;
$destinationRecords = $this->initialData->getAttributes('dest');

$recordsToSave = $destinationDocument->getRecords();
Expand Down Expand Up @@ -837,22 +828,4 @@ public function rollback()
}
}
}

/**
* Remove ignored attributes from source records
*
* @param array $sourceRecords
* @param array $attributeToClear
* @return array
*/
protected function clearIgnoredAttributes($sourceRecords, $attributeToClear)
{
foreach ($sourceRecords as $attrNum => $sourceAttribute) {
if ($sourceAttribute['attribute_code'] == $attributeToClear) {
unset($sourceRecords[$attrNum]);
}
}
return $sourceRecords;
}
// @codeCoverageIgnoreEnd
}
39 changes: 38 additions & 1 deletion src/Migration/Step/Eav/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ class Helper
*/
protected $readerGroups;

/**
* @var \Migration\Reader\Groups
*/
protected $readerAttributes;

/**
* @var \Migration\ResourceModel\Record[]
*/
protected $addedGroups ;
protected $addedGroups;

/**
* @param MapFactory $mapFactory
Expand All @@ -64,6 +69,7 @@ public function __construct(
$this->destination = $destination;
$this->factory = $factory;
$this->readerGroups = $groupsFactory->create('eav_document_groups_file');
$this->readerAttributes = $groupsFactory->create('eav_attribute_groups_file');
}

/**
Expand Down Expand Up @@ -231,4 +237,35 @@ public function getDesignAttributeAndGroupsData()
'entityTypeIdCatalogProduct' => $entityTypeIdCatalogProduct
];
}

/**
* @param array $sourceRecords
* @return array
*/
public function clearIgnored($sourceRecords)
{
foreach (array_keys($this->readerAttributes->getGroup('ignore')) as $attributeToClear) {
$sourceRecords = $this->clearIgnoredAttributes($sourceRecords, $attributeToClear);
}
return $sourceRecords;
}

/**
* Remove ignored attributes from source records
*
* @param array $sourceRecords
* @param array $attributeToClear
* @return array
*/
protected function clearIgnoredAttributes($sourceRecords, $attributeToClear)
{
foreach ($sourceRecords as $attrNum => $sourceAttribute) {
if (
isset($sourceAttribute['attribute_code']) && ($sourceAttribute['attribute_code'] == $attributeToClear)
) {
unset($sourceRecords[$attrNum]);
}
}
return $sourceRecords;
}
}
6 changes: 5 additions & 1 deletion src/Migration/Step/Eav/InitialData.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ protected function initAttributes()
if ($this->attributes === null) {
$sourceDocument = 'eav_attribute';

foreach ($this->helper->getSourceRecords($sourceDocument, ['attribute_id']) as $id => $record) {
foreach (
$this->helper->clearIgnored(
$this->helper->getSourceRecords($sourceDocument, ['attribute_id'])
) as $id => $record
) {
$this->attributes['source'][$id] = $record;
}

Expand Down
1 change: 1 addition & 0 deletions tests/integration/resource/eav/dest.sql
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ ENGINE=InnoDB
;
INSERT INTO `eav_attribute_group` (`attribute_group_id`, `attribute_set_id`, `attribute_group_name`, `sort_order`, `default_id`, `attribute_group_code`, `tab_group_code`) VALUES (7, 4, 'Product Details', 10, 1, 'product-details', 'basic');
INSERT INTO `eav_attribute_group` (`attribute_group_id`, `attribute_set_id`, `attribute_group_name`, `sort_order`, `default_id`, `attribute_group_code`, `tab_group_code`) VALUES (1, 1, 'General', 1, 1, NULL, NULL);
INSERT INTO `eav_attribute_group` (`attribute_group_id`, `attribute_set_id`, `attribute_group_name`, `sort_order`, `default_id`, `attribute_group_code`, `tab_group_code`) VALUES (8, 4, 'Design', 11, 1, 'design', 'advanced');

CREATE TABLE `eav_entity_attribute` (
`entity_attribute_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Entity Attribute Id',
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/resource/eav/source.sql
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ ENGINE=InnoDB
;
INSERT INTO `eav_attribute_group` (`attribute_group_id`, `attribute_set_id`, `attribute_group_name`, `sort_order`, `default_id`) VALUES (4, 4, 'General', 1, 1);
INSERT INTO `eav_attribute_group` (`attribute_group_id`, `attribute_set_id`, `attribute_group_name`, `sort_order`, `default_id`) VALUES (1, 1, 'General', 1, 1);
INSERT INTO `eav_attribute_group` (`attribute_group_id`, `attribute_set_id`, `attribute_group_name`, `sort_order`, `default_id`) VALUES (5, 4, 'Prices', 2, 0);
INSERT INTO `eav_attribute_group` (`attribute_group_id`, `attribute_set_id`, `attribute_group_name`, `sort_order`, `default_id`) VALUES (6, 4, 'Design', 3, 0);

CREATE TABLE `eav_entity_attribute` (
`entity_attribute_id` INT(10) UNSIGNED NOT NULL COMMENT 'Entity Attribute Id',
Expand Down
1 change: 1 addition & 0 deletions tests/integration/resource/ee.eav/dest.sql
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ ENGINE=InnoDB
;
INSERT INTO `eav_attribute_group` (`attribute_group_id`, `attribute_set_id`, `attribute_group_name`, `sort_order`, `default_id`, `attribute_group_code`, `tab_group_code`) VALUES (7, 4, 'Product Details', 10, 1, 'product-details', 'basic');
INSERT INTO `eav_attribute_group` (`attribute_group_id`, `attribute_set_id`, `attribute_group_name`, `sort_order`, `default_id`, `attribute_group_code`, `tab_group_code`) VALUES (1, 1, 'General', 1, 1, NULL, NULL);
INSERT INTO `eav_attribute_group` (`attribute_group_id`, `attribute_set_id`, `attribute_group_name`, `sort_order`, `default_id`, `attribute_group_code`, `tab_group_code`) VALUES (8, 4, 'Design', 11, 1, 'design', 'advanced');

CREATE TABLE `eav_entity_attribute` (
`entity_attribute_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Entity Attribute Id',
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/resource/ee.eav/source.sql
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ ENGINE=InnoDB
;
INSERT INTO `eav_attribute_group` (`attribute_group_id`, `attribute_set_id`, `attribute_group_name`, `sort_order`, `default_id`) VALUES (4, 4, 'General', 1, 1);
INSERT INTO `eav_attribute_group` (`attribute_group_id`, `attribute_set_id`, `attribute_group_name`, `sort_order`, `default_id`) VALUES (1, 1, 'General', 1, 1);
INSERT INTO `eav_attribute_group` (`attribute_group_id`, `attribute_set_id`, `attribute_group_name`, `sort_order`, `default_id`) VALUES (5, 4, 'Prices', 2, 0);
INSERT INTO `eav_attribute_group` (`attribute_group_id`, `attribute_set_id`, `attribute_group_name`, `sort_order`, `default_id`) VALUES (6, 4, 'Design', 3, 0);

CREATE TABLE `eav_entity_attribute` (
`entity_attribute_id` INT(10) UNSIGNED NOT NULL COMMENT 'Entity Attribute Id',
Expand Down
18 changes: 15 additions & 3 deletions tests/unit/testsuite/Migration/Step/Eav/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class HelperTest extends \PHPUnit_Framework_TestCase
*/
protected $readerGroups;

/**
* @var \Migration\Reader\Groups|\PHPUnit_Framework_MockObject_MockObject
*/
protected $readerAttributes;

/**
* @return void
*/
Expand All @@ -72,16 +77,23 @@ public function setUp()
->disableOriginalConstructor()
->setMethods([])
->getMock();
$this->readerAttributes = $this->getMockBuilder('\Migration\Reader\Groups')
->disableOriginalConstructor()
->setMethods([])
->getMock();
/** @var \Migration\Reader\GroupsFactory|\PHPUnit_Framework_MockObject_MockObject $groupsFactory */
$groupsFactory = $this->getMockBuilder('\Migration\Reader\GroupsFactory')
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$groupsFactory->expects($this->any())
->method('create')
->with('eav_document_groups_file')
->willReturn($this->readerGroups);

->willReturnMap(
[
['eav_document_groups_file', $this->readerGroups],
['eav_attribute_groups_file', $this->readerAttributes]
]
);
$this->helper = new Helper($mapFactory, $this->source, $this->destination, $this->factory, $groupsFactory);
}

Expand Down
6 changes: 5 additions & 1 deletion tests/unit/testsuite/Migration/Step/Eav/InitialDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function setUp()
->setMethods([])
->getMock();
$this->helper = $this->getMockBuilder('\Migration\Step\Eav\Helper')->disableOriginalConstructor()
->setMethods(['getSourceRecords', 'getDestinationRecords'])
->setMethods(['getSourceRecords', 'getDestinationRecords', 'clearIgnored'])
->getMock();
$this->initialData = new InitialData($mapFactory, $this->source, $this->destination, $this->helper);
}
Expand Down Expand Up @@ -101,6 +101,10 @@ public function testInit()
['eav_attribute', ['attribute_id'], $dataAttributes['source']],
['eav_entity_type', [], $eavEntityTypes['source']]
]);
$this->helper->expects($this->any())->method('clearIgnored')->willReturnMap([
[$dataAttributes['source'], $dataAttributes['source']],
[$eavEntityTypes['source'], $eavEntityTypes['source']]
]);
$this->helper->expects($this->any())->method('getDestinationRecords')->willReturnMap(
[
['eav_attribute', ['entity_type_id', 'attribute_code'], $dataAttributes['dest']],
Expand Down

0 comments on commit c98fcdf

Please sign in to comment.