Issue #3077661 by cspitzlay, Wim Leers: Check for conflict in fieldname mapping does not work

merge-requests/55/head
catch 2019-09-17 12:02:36 +01:00
parent 4f05e52621
commit d9936f2817
2 changed files with 38 additions and 2 deletions

View File

@ -233,8 +233,8 @@ class ResourceTypeRepository implements ResourceTypeRepositoryInterface {
foreach (array_diff($field_names, array_keys($mapping)) as $field_name) {
if ($field_name === 'id' || $field_name === 'type') {
$alias = $entity_type->id() . '_' . $field_name;
if (isset($field_name[$alias])) {
throw new \LogicException('The generated alias conflicts with an existing field. Please report this in the JSON:API issue queue!');
if (in_array($alias, $field_names, TRUE)) {
throw new \LogicException("The generated alias '{$alias}' for field name '{$field_name}' conflicts with an existing field. Please report this in the JSON:API issue queue!");
}
$mapping[$field_name] = $alias;
continue;

View File

@ -111,4 +111,40 @@ class ResourceTypeRepositoryTest extends JsonapiKernelTestBase {
$this->assertCount(4, $this->resourceTypeRepository->get('node', 'article')->getRelatableResourceTypesByField('field_relationship'));
}
/**
* Ensures that a naming conflict in the mapping causes an exception to be
* thrown.
*
* @covers ::getFieldMapping
* @dataProvider getFieldMappingProvider
*/
public function testMappingNameConflictCheck($field_name_list) {
$entity_type = \Drupal::entityTypeManager()->getDefinition('node');
$bundle = 'article';
$reflection_class = new \ReflectionClass($this->resourceTypeRepository);
$reflection_method = $reflection_class->getMethod('getFieldMapping');
$reflection_method->setAccessible(TRUE);
$this->expectException(\LogicException::class);
$this->expectExceptionMessage("The generated alias '{$field_name_list[1]}' for field name '{$field_name_list[0]}' conflicts with an existing field. Please report this in the JSON:API issue queue!");
$reflection_method->invokeArgs($this->resourceTypeRepository, [$field_name_list, $entity_type, $bundle]);
}
/**
* Data provider for testGetFieldMapping.
*
* These field name lists are designed to trigger a naming conflict in the
* mapping: the special-cased names "type" or "id", and the name
* "{$entity_type_id}_type" or "{$entity_type_id}_id", respectively.
*
* @returns array
* The data for the test method.
*/
public function getFieldMappingProvider() {
return [
[['type', 'node_type']],
[['id', 'node_id']],
];
}
}