Skip to content

Commit 68cde53

Browse files
authored
test(entity_definition): Add test coverage and minor fixes for entiy definition data producers (#1132)
1 parent 9654fa1 commit 68cde53

5 files changed

Lines changed: 554 additions & 43 deletions

File tree

src/GraphQL/Execution/ExecutionResult.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,16 @@
99
class ExecutionResult extends LibraryExecutionResult implements CacheableDependencyInterface {
1010
use RefinableCacheableDependencyTrait;
1111

12+
/**
13+
* PHP Serialization: skip some class members when serializing during tests.
14+
*/
15+
public function __sleep(): array {
16+
// PHPUnit error: Fatal error: Uncaught Exception: Serialization of
17+
// 'Closure' is not allowed.
18+
// Remove some closure-containing members before serializing.
19+
$vars = get_object_vars($this);
20+
unset($vars['extensions']);
21+
return array_keys($vars);
22+
}
23+
1224
}

src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Drupal\graphql\Plugin\GraphQL\DataProducer\EntityDefinition;
44

5-
use Drupal\Core\Entity\ContentEntityType;
5+
use Drupal\Core\Entity\ContentEntityTypeInterface;
66
use Drupal\Core\Entity\EntityFieldManager;
77
use Drupal\Core\Entity\EntityTypeInterface;
88
use Drupal\Core\Entity\EntityTypeManager;
@@ -121,24 +121,22 @@ public function resolve(
121121
?array $field_types_context = NULL,
122122
FieldContext $field_context
123123
): \Iterator {
124-
$entity_definition->getBundleEntityType();
125-
if ($entity_definition instanceof ContentEntityType) {
124+
125+
if ($entity_definition instanceof ContentEntityTypeInterface) {
126+
$entity_type_id = $entity_definition->id();
126127
if ($bundle_context) {
127128
$key = $bundle_context['key'];
128-
$id = $entity_definition->id();
129-
$fields = $this->entityFieldManager->getFieldDefinitions($id, $key);
129+
$fields = $this->entityFieldManager->getFieldDefinitions($entity_type_id, $key);
130130

131131
// Set entity form default display as context.
132-
$entity_id = $id . '.' . $key . '.default';
133-
/** @var \Drupal\Core\Config\Entity\ConfigEntityStorage $form_display_context */
132+
$form_display_id = $entity_type_id . '.' . $key . '.default';
134133
$form_display_context = $this->entityTypeManager
135134
->getStorage('entity_form_display')
136-
->load($entity_id);
135+
->load($form_display_id);
137136
$field_context->setContextValue('entity_form_display', $form_display_context);
138137
}
139138
else {
140-
$id = $entity_definition->id();
141-
$fields = $this->entityFieldManager->getFieldDefinitions($id, $id);
139+
$fields = $this->entityFieldManager->getFieldDefinitions($entity_type_id, $entity_type_id);
142140
}
143141

144142
if ($field_types_context) {

src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Multiple.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Drupal\graphql\Plugin\GraphQL\DataProducer\EntityDefinition\Fields;
44

55
use Drupal\Core\Field\FieldDefinitionInterface;
6+
use Drupal\Core\Field\FieldStorageDefinitionInterface;
67
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
78

89
/**
@@ -34,6 +35,9 @@ class Multiple extends DataProducerPluginBase {
3435
* If the field contains multiple values or just single value.
3536
*/
3637
public function resolve(FieldDefinitionInterface $entity_definition_field): bool {
38+
if ($entity_definition_field instanceof FieldStorageDefinitionInterface) {
39+
return $entity_definition_field->isMultiple();
40+
}
3741
return $entity_definition_field->isList();
3842
}
3943

src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Reference.php

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
namespace Drupal\graphql\Plugin\GraphQL\DataProducer\EntityDefinition\Fields;
44

5-
use Drupal\Core\Field\BaseFieldDefinition;
6-
use Drupal\Core\Field\Entity\BaseFieldOverride;
75
use Drupal\Core\Field\FieldDefinitionInterface;
8-
use Drupal\field\Entity\FieldConfig;
96
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
107

118
/**
@@ -37,36 +34,7 @@ class Reference extends DataProducerPluginBase {
3734
* If the field is referencing entities (is the entity reference type).
3835
*/
3936
public function resolve(FieldDefinitionInterface $entity_definition_field): bool {
40-
if ($entity_definition_field instanceof BaseFieldDefinition) {
41-
/** @var \Drupal\Core\Field\BaseFieldDefinition $entity_definition_field */
42-
if ($entity_definition_field->getType() === 'entity_reference') {
43-
return TRUE;
44-
}
45-
else {
46-
return FALSE;
47-
}
48-
}
49-
elseif ($entity_definition_field instanceof FieldConfig) {
50-
/** @var \Drupal\field\Entity\FieldConfig $entity_definition_field */
51-
if ($entity_definition_field->getType() === 'entity_reference') {
52-
return TRUE;
53-
}
54-
else {
55-
return FALSE;
56-
}
57-
}
58-
elseif ($entity_definition_field instanceof BaseFieldOverride) {
59-
/** @var \Drupal\field\Entity\FieldConfig $entity_definition_field */
60-
if ($entity_definition_field->getType() === 'entity_reference') {
61-
return TRUE;
62-
}
63-
else {
64-
return FALSE;
65-
}
66-
}
67-
else {
68-
return FALSE;
69-
}
37+
return $entity_definition_field->getType() === 'entity_reference';
7038
}
7139

7240
}

0 commit comments

Comments
 (0)