Skip to content

Commit

Permalink
FRW-6728 Fixed identifier visible name resolving issue. (#10589)
Browse files Browse the repository at this point in the history
FRW-6728 Fixed identifier visible name resolving issue.
  • Loading branch information
dimitriyTsemma authored Nov 2, 2023
1 parent 0b14850 commit 3fb6992
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ protected function processActiveRecordSaving(
}

$dynamicEntityTransfer = $this->buildDynamicEntityFields($dynamicEntityTransfer, $dynamicEntityDefinitionTransfer, $activeRecord);
$dynamicEntityTransfer = $this->addIdentifierToFields($dynamicEntityTransfer, $dynamicEntityDefinitionTransfer, $activeRecord);
$dynamicEntityTransfer = $this->addIdentifierToFields($dynamicEntityTransfer, $dynamicEntityDefinitionTransfer, $activeRecord, $dynamicEntityConfigurationTransfer);

$dynamicEntityCollectionResponseTransfer->addDynamicEntity($dynamicEntityTransfer);

Expand Down Expand Up @@ -453,13 +453,17 @@ protected function addErrorToResponseTransfer(
protected function addIdentifierToFields(
DynamicEntityTransfer $dynamicEntityTransfer,
DynamicEntityDefinitionTransfer $dynamicEntityDefinitionTransfer,
ActiveRecordInterface $activeRecord
ActiveRecordInterface $activeRecord,
DynamicEntityConfigurationTransfer $dynamicEntityConfigurationTransfer
): DynamicEntityTransfer {
$identifier = $dynamicEntityDefinitionTransfer->getIdentifierOrFail();

$identifierVisibleName = $this->getIdentifierVisibleName($identifier, $dynamicEntityConfigurationTransfer);

$identifierValue = $activeRecord->getByName($identifier);
$dynamicEntityTransfer->setFields(array_merge(
$dynamicEntityTransfer->getFields(),
[$identifier => $identifierValue],
[$identifierVisibleName => $identifierValue],
));
$dynamicEntityTransfer->setIdentifier($identifierValue);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ class DynamicEntityFacadeTest extends Unit
*/
protected const FOO_DEFINITION = '{"identifier":"id_dynamic_entity_configuration","fields":[{"fieldName":"id_dynamic_entity_configuration","fieldVisibleName":"id_dynamic_entity_configuration","isEditable":true,"isCreatable":false,"type":"integer","validation":{"isRequired":false}},{"fieldName":"table_alias","fieldVisibleName":"table_alias","type":"string","isEditable":true,"isCreatable":true,"validation":{"isRequired":false}},{"fieldName":"table_name","fieldVisibleName":"table_name","type":"string","isEditable":true,"isCreatable":true,"validation":{"isRequired":false}},{"fieldName":"is_active","fieldVisibleName":"is_active","isEditable":false,"isCreatable":true,"type":"boolean","validation":{"isRequired":false}},{"fieldName":"definition","fieldVisibleName":"definition","type":"string","isEditable":true,"isCreatable":true,"validation":{"isRequired":false}}]}';

/**
* @var string
*/
protected const IDENTIFIER_TEST_TABLE_ALIAS = 'test_identifiers';

/**
* @var string
*/
protected const IDENTIFIER_TEST_DIFFERENT_VISIBLE_NAME_DEFINITION = '{"identifier":"id_dynamic_entity_configuration","fields":[{"fieldName":"id_dynamic_entity_configuration","fieldVisibleName":"idDynamicEntityConfiguration","isEditable":true,"isCreatable":false,"type":"integer","validation":{"isRequired":false}},{"fieldName":"table_alias","fieldVisibleName":"table_alias","type":"string","isEditable":true,"isCreatable":true,"validation":{"isRequired":false}},{"fieldName":"table_name","fieldVisibleName":"table_name","type":"string","isEditable":true,"isCreatable":true,"validation":{"isRequired":false}},{"fieldName":"is_active","fieldVisibleName":"is_active","isEditable":false,"isCreatable":true,"type":"boolean","validation":{"isRequired":false}},{"fieldName":"definition","fieldVisibleName":"definition","type":"string","isEditable":true,"isCreatable":true,"validation":{"isRequired":false}}]}';

/**
* @var \Spryker\Zed\DynamicEntity\Business\DynamicEntityFacadeInterface
*/
Expand Down Expand Up @@ -615,6 +625,41 @@ public function testUpdateDynamicEntityCollectionUpdatesTheRecordAndReturnsCorre
$this->assertEquals($updatedFooEntity->getDefinition(), $dynamicEntityCollectionResponseTransfer->getDynamicEntities()[0]->getFields()['definition']);
}

/**
* @return void
*/
public function testUpdateDynamicEntityCollectionUpdatesTheRecordWithNonDefaultIdentifierVisibleName(): void
{
//Arrange
$this->createIdentifierInCamelCaseEntity();
$fooEntity = SpyDynamicEntityConfigurationQuery::create()
->filterByTableAlias(static::IDENTIFIER_TEST_TABLE_ALIAS)
->find()
->getData()[0];

$dynamicEntityCollectionRequestTransfer = $this->createDynamicEntityCollectionRequestTransfer(static::IDENTIFIER_TEST_TABLE_ALIAS);
$dynamicEntityCollectionRequestTransfer->addDynamicEntity(
(new DynamicEntityTransfer())
->setFields([
'idDynamicEntityConfiguration' => $fooEntity->getIdDynamicEntityConfiguration(),
'table_name' => 'newid',
]),
);

//Act
$dynamicEntityCollectionResponseTransfer = $this->dynamicEntityFacade->updateDynamicEntityCollection($dynamicEntityCollectionRequestTransfer);

//Assert
$updatedFooEntity = SpyDynamicEntityConfigurationQuery::create()
->filterByIdDynamicEntityConfiguration($fooEntity->getIdDynamicEntityConfiguration())
->find()
->getData()[0];
$this->assertEmpty($dynamicEntityCollectionResponseTransfer->getErrors());
$this->assertNotEquals($fooEntity->getTableName(), $updatedFooEntity->getTableName());
$this->assertEquals('newid', $updatedFooEntity->getTableName());
$this->assertEquals($updatedFooEntity->getTableName(), $dynamicEntityCollectionResponseTransfer->getDynamicEntities()[0]->getFields()['table_name']);
}

/**
* @return void
*/
Expand Down Expand Up @@ -729,6 +774,19 @@ protected function createFooEntity(): void
->save();
}

/**
* @return void
*/
protected function createIdentifierInCamelCaseEntity(): void
{
(new SpyDynamicEntityConfiguration())
->setIsActive(true)
->setTableAlias(static::IDENTIFIER_TEST_TABLE_ALIAS)
->setTableName(static::TABLE_NAME)
->setDefinition(static::IDENTIFIER_TEST_DIFFERENT_VISIBLE_NAME_DEFINITION)
->save();
}

/**
* @param string $tableAlias
* @param string|null $filterCondition
Expand All @@ -755,12 +813,14 @@ protected function haveDynamicEntityCriteriaTransfer(
}

/**
* @param string $tableAlias
*
* @return \Generated\Shared\Transfer\DynamicEntityCollectionRequestTransfer
*/
protected function createDynamicEntityCollectionRequestTransfer(): DynamicEntityCollectionRequestTransfer
protected function createDynamicEntityCollectionRequestTransfer(string $tableAlias = self::FOO_TABLE_ALIAS_1): DynamicEntityCollectionRequestTransfer
{
$dynamicEntityCollectionRequestTransfer = new DynamicEntityCollectionRequestTransfer();
$dynamicEntityCollectionRequestTransfer->setTableAlias(static::FOO_TABLE_ALIAS_1);
$dynamicEntityCollectionRequestTransfer->setTableAlias($tableAlias);

return $dynamicEntityCollectionRequestTransfer;
}
Expand Down

0 comments on commit 3fb6992

Please sign in to comment.