From b1a758e5b53a9ed8cacc8c598e89a831bb66f8e3 Mon Sep 17 00:00:00 2001 From: Radoslav Terezka Date: Wed, 15 Apr 2020 11:46:19 +0200 Subject: [PATCH 1/9] Fix problems with entity form display context. --- .../DataProducer/EntityDefinition/Fields.php | 17 ++++++++--------- .../EntityDefinition/Fields/Hidden.php | 8 ++++---- .../EntityDefinition/Fields/Settings.php | 8 ++++---- .../EntityDefinition/Fields/Weight.php | 8 ++++---- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields.php b/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields.php index c3cea454d..024039b4b 100644 --- a/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields.php +++ b/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields.php @@ -114,23 +114,22 @@ public function resolve( if ($bundle_context) { $key = $bundle_context['key']; $id = $entity_definition->id(); - $entity_id = $id . '.' . $id . '.' . $key; $fields = \Drupal::entityManager()->getFieldDefinitions($id, $key); + + // Set entity form default display as context. + $entity_id = $id . '.' . $key . '.default'; + /** @var \Drupal\Core\Config\Entity\ConfigEntityStorage $form_display_context */ + $form_display_context = $this->entityTypeManager + ->getStorage('entity_form_display') + ->load($entity_id); + $field_context->setContextValue('entity_form_display', $form_display_context); } else { $id = $entity_definition->id(); - $entity_id = $id . '.' . $id . '.default'; $fields = \Drupal::entityManager()->getFieldDefinitions($id, $id); } - /** @var \Drupal\Core\Config\Entity\ConfigEntityStorage $form_display_context */ - $form_display_context = $this->entityTypeManager - ->getStorage('entity_form_display') - ->load($entity_id); - - $field_context->setContextValue('entity_form_display', $form_display_context); if ($field_types_context) { - $field_types = $field_types_context['key']; foreach ($fields as $field) { if ($field_types === 'BASE_FIELDS') { if ($field instanceof BaseFieldDefinition) { diff --git a/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Hidden.php b/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Hidden.php index 340b93e94..9e953af22 100644 --- a/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Hidden.php +++ b/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Hidden.php @@ -2,6 +2,7 @@ namespace Drupal\graphql\Plugin\GraphQL\DataProducer\EntityDefinition\Fields; +use Drupal\Core\Entity\Entity\EntityFormDisplay; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase; @@ -33,7 +34,7 @@ class Hidden extends DataProducerPluginBase { * * @param \Drupal\Core\Field\FieldDefinitionInterface $entity_definition_field * The entity field definition. - * @param array|null $entity_form_display_context + * @param \Drupal\Core\Entity\Entity\EntityFormDisplay|null $entity_form_display_context * Entity form display context. * * @return bool @@ -41,11 +42,10 @@ class Hidden extends DataProducerPluginBase { */ public function resolve( FieldDefinitionInterface $entity_definition_field, - ?array $entity_form_display_context = NULL + ?EntityFormDisplay $entity_form_display_context ): bool { if ($entity_form_display_context) { - $entity_form_display = $entity_form_display_context['key']; - $hidden = $entity_form_display->get('hidden'); + $hidden = $entity_form_display_context->get('hidden'); $field_id = $entity_definition_field->getName(); if (isset($hidden[$field_id])) { diff --git a/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Settings.php b/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Settings.php index 348bf4141..96eb94a0b 100644 --- a/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Settings.php +++ b/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Settings.php @@ -2,6 +2,7 @@ namespace Drupal\graphql\Plugin\GraphQL\DataProducer\EntityDefinition\Fields; +use Drupal\Core\Entity\Entity\EntityFormDisplay; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase; @@ -33,7 +34,7 @@ class Settings extends DataProducerPluginBase { * * @param \Drupal\Core\Field\FieldDefinitionInterface $entity_definition_field * The entity field definition. - * @param array|null $entity_form_display_context + * @param \Drupal\Core\Entity\Entity\EntityFormDisplay|null $entity_form_display_context * Entity form display context. * * @return array @@ -41,13 +42,12 @@ class Settings extends DataProducerPluginBase { */ public function resolve( FieldDefinitionInterface $entity_definition_field, - ?array $entity_form_display_context = NULL + ?EntityFormDisplay $entity_form_display_context ): \Iterator { $settings = $entity_definition_field->getSettings(); if ($entity_form_display_context) { - $entity_form_display = $entity_form_display_context['key']; - $content = $entity_form_display->get('content'); + $content = $entity_form_display_context->get('content'); $field_id = $entity_definition_field->getName(); if (isset($content[$field_id])) { $form_settings = $content[$field_id]['settings']; diff --git a/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Weight.php b/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Weight.php index 6c7c57b75..64e25736e 100644 --- a/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Weight.php +++ b/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Weight.php @@ -2,6 +2,7 @@ namespace Drupal\graphql\Plugin\GraphQL\DataProducer\EntityDefinition\Fields; +use Drupal\Core\Entity\Entity\EntityFormDisplay; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase; @@ -33,7 +34,7 @@ class Weight extends DataProducerPluginBase { * * @param \Drupal\Core\Field\FieldDefinitionInterface $entity_definition_field * The entity field definition. - * @param array|null $entity_form_display_context + * @param \Drupal\Core\Entity\Entity\EntityFormDisplay|null $entity_form_display_context * Entity form display context. * * @return int @@ -41,11 +42,10 @@ class Weight extends DataProducerPluginBase { */ public function resolve( FieldDefinitionInterface $entity_definition_field, - ?array $entity_form_display_context = NULL + ?EntityFormDisplay $entity_form_display_context ): int { if ($entity_form_display_context) { - $entity_form_display = $entity_form_display_context['key']; - $content = $entity_form_display->get('content'); + $content = $entity_form_display_context->get('content'); $field_id = $entity_definition_field->getName(); if (isset($content[$field_id])) { From b95486535bcdf5b1760674e803ae17209783a8ff Mon Sep 17 00:00:00 2001 From: Radoslav Terezka Date: Wed, 15 Apr 2020 11:55:22 +0200 Subject: [PATCH 2/9] Revert line mistakenly included from different patch. --- src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields.php b/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields.php index 024039b4b..91a540b2c 100644 --- a/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields.php +++ b/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields.php @@ -131,6 +131,7 @@ public function resolve( if ($field_types_context) { foreach ($fields as $field) { + $field_types = $field_types_context['key']; if ($field_types === 'BASE_FIELDS') { if ($field instanceof BaseFieldDefinition) { yield $field; From 551c6dc89426497dcf38f6fb5566eabaf5f4069f Mon Sep 17 00:00:00 2001 From: Radoslav Terezka Date: Wed, 15 Apr 2020 11:56:09 +0200 Subject: [PATCH 3/9] Align code with 4.x. --- src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields.php b/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields.php index 91a540b2c..297b80ce0 100644 --- a/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields.php +++ b/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields.php @@ -130,8 +130,8 @@ public function resolve( } if ($field_types_context) { + $field_types = $field_types_context['key']; foreach ($fields as $field) { - $field_types = $field_types_context['key']; if ($field_types === 'BASE_FIELDS') { if ($field instanceof BaseFieldDefinition) { yield $field; From ef1e9bb8e5a8395361631cf483be8692f81c2d77 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Fri, 11 Dec 2020 13:23:22 +0100 Subject: [PATCH 4/9] start test case --- .../DataProducer/EntityDefinitionTest.php | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 tests/src/Kernel/DataProducer/EntityDefinitionTest.php diff --git a/tests/src/Kernel/DataProducer/EntityDefinitionTest.php b/tests/src/Kernel/DataProducer/EntityDefinitionTest.php new file mode 100644 index 000000000..86cc60857 --- /dev/null +++ b/tests/src/Kernel/DataProducer/EntityDefinitionTest.php @@ -0,0 +1,74 @@ + 'article', + 'name' => 'article', + ]); + $content_type->save(); + + $schema = <<setUpSchema($schema); + } + + /** + * Tests that retrieving an entity definition works. + */ + public function testResolveBundle() { + /*$result = $this->executeDataProducer('entity_definition', [ + 'entity_type' => 'node', + 'bundle' => 'article', + ]);*/ + + $this->assertEquals('page', 'page'); + } + +} From dae988f39de34217dddbe131e3ee3c8736aafa09 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Sun, 13 Dec 2020 19:00:54 +0100 Subject: [PATCH 5/9] Finish query setup and check values --- .../DataProducer/EntityDefinitionTest.php | 144 +++++++++++++++++- .../src/Traits/QueryResultAssertionTrait.php | 2 + 2 files changed, 140 insertions(+), 6 deletions(-) diff --git a/tests/src/Kernel/DataProducer/EntityDefinitionTest.php b/tests/src/Kernel/DataProducer/EntityDefinitionTest.php index 86cc60857..62d14acfa 100644 --- a/tests/src/Kernel/DataProducer/EntityDefinitionTest.php +++ b/tests/src/Kernel/DataProducer/EntityDefinitionTest.php @@ -2,8 +2,10 @@ namespace Drupal\Tests\graphql\Kernel\DataProducer; +use Drupal\Core\Entity\Entity\EntityFormDisplay; use Drupal\Tests\graphql\Kernel\GraphQLTestBase; use Drupal\node\Entity\NodeType; +use Drupal\graphql\GraphQL\ResolverBuilder; /** * Test the entity_definition data producer and friends. @@ -23,6 +25,14 @@ public function setUp() { ]); $content_type->save(); + // Create a form display. + $form_display = EntityFormDisplay::create([ + 'targetEntityType' => 'node', + 'bundle' => 'article', + 'mode' => 'default', + ]); + $form_display->save(); + $schema = <<setUpSchema($schema); + + $registry = $this->registry; + $builder = new ResolverBuilder(); + + // Entity definition query. + $registry->addFieldResolver('Query', 'entityDefinition', + $builder->produce('entity_definition', [ + 'entity_type' => $builder->fromArgument('entity_type'), + 'bundle' => $builder->fromArgument('bundle'), + 'field_types' => $builder->fromArgument('field_types'), + ]) + ); + // Entity definition fields. + $registry->addFieldResolver('EntityDefinition', 'label', + $builder->produce('entity_definition_label', [ + 'entity_definition' => $builder->fromParent(), + ]) + ); + $registry->addFieldResolver('EntityDefinition', 'fields', + $builder->produce('entity_definition_fields', [ + 'entity_definition' => $builder->fromParent(), + 'bundle_context' => $builder->fromContext('bundle'), + 'field_types_context' => $builder->fromContext('field_types'), + ]) + ); + $registry->addFieldResolver('EntityDefinitionField', 'id', + $builder->produce('entity_definition_field_id', [ + 'entity_definition_field' => $builder->fromParent(), + ]) + ); + $registry->addFieldResolver('EntityDefinitionField', 'label', + $builder->produce('entity_definition_field_label', [ + 'entity_definition_field' => $builder->fromParent(), + ]) + ); + $registry->addFieldResolver('EntityDefinitionField', 'description', + $builder->produce('entity_definition_field_description', [ + 'entity_definition_field' => $builder->fromParent(), + ]) + ); + $registry->addFieldResolver('EntityDefinitionField', 'type', + $builder->produce('entity_definition_field_type', [ + 'entity_definition_field' => $builder->fromParent(), + ]) + ); + $registry->addFieldResolver('EntityDefinitionField', 'required', + $builder->produce('entity_definition_field_required', [ + 'entity_definition_field' => $builder->fromParent(), + ]) + ); + $registry->addFieldResolver('EntityDefinitionField', 'multiple', + $builder->produce('entity_definition_field_multiple', [ + 'entity_definition_field' => $builder->fromParent(), + ]) + ); + $registry->addFieldResolver('EntityDefinitionField', 'maxNumItems', + $builder->produce('entity_definition_field_max_num_items', [ + 'entity_definition_field' => $builder->fromParent(), + ]) + ); + $registry->addFieldResolver('EntityDefinitionField', 'status', + $builder->produce('entity_definition_field_status', [ + 'entity_definition_field' => $builder->fromParent(), + ]) + ); + $registry->addFieldResolver('EntityDefinitionField', 'defaultValue', + $builder->produce('entity_definition_field_default_value', [ + 'entity_definition_field' => $builder->fromParent(), + ]) + ); + $registry->addFieldResolver('EntityDefinitionField', 'defaultValues', + $builder->produce('entity_definition_field_additional_default_value', [ + 'entity_definition_field' => $builder->fromParent(), + ]) + ); + $registry->addFieldResolver('EntityDefinitionField', 'isReference', + $builder->produce('entity_definition_field_reference', [ + 'entity_definition_field' => $builder->fromParent(), + ]) + ); + $registry->addFieldResolver('EntityDefinitionField', 'isHidden', + $builder->produce('entity_definition_field_hidden', [ + 'entity_definition_field' => $builder->fromParent(), + 'entity_form_display_context' => $builder->fromContext('entity_form_display'), + ]) + ); + $registry->addFieldResolver('EntityDefinitionField', 'weight', + $builder->produce('entity_definition_field_weight', [ + 'entity_definition_field' => $builder->fromParent(), + 'entity_form_display_context' => $builder->fromContext('entity_form_display'), + ]) + ); + $registry->addFieldResolver('EntityDefinitionField', 'settings', + $builder->produce('translatable_entity_definition_field_settings', [ + 'entity_definition_field' => $builder->fromParent(), + 'entity_form_display_context' => $builder->fromContext('entity_form_display'), + ]) + ); } /** * Tests that retrieving an entity definition works. */ - public function testResolveBundle() { - /*$result = $this->executeDataProducer('entity_definition', [ - 'entity_type' => 'node', - 'bundle' => 'article', - ]);*/ + public function testEntityDefinition() { + $query = <<assertEquals('page', 'page'); + $this->assertResults($query, [], ['entityDefinition' => ['label' => 'Content']]); } } diff --git a/tests/src/Traits/QueryResultAssertionTrait.php b/tests/src/Traits/QueryResultAssertionTrait.php index 8b7ecad1d..2f8d683df 100644 --- a/tests/src/Traits/QueryResultAssertionTrait.php +++ b/tests/src/Traits/QueryResultAssertionTrait.php @@ -88,6 +88,8 @@ protected function assertResults($query, array $variables, array $expected, Cach 'variables' => $variables, ]) ); + var_export($result->toArray()['data']); + return; $this->assertResultErrors($result, []); $this->assertResultData($result, $expected); From f03c0eb1d6e3e2161c2de190c03306849bee84c9 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Sun, 13 Dec 2020 20:03:15 +0100 Subject: [PATCH 6/9] First passing assertion --- .../DataProducer/EntityDefinitionTest.php | 324 +++++++++++++++++- .../src/Traits/QueryResultAssertionTrait.php | 4 +- 2 files changed, 325 insertions(+), 3 deletions(-) diff --git a/tests/src/Kernel/DataProducer/EntityDefinitionTest.php b/tests/src/Kernel/DataProducer/EntityDefinitionTest.php index 62d14acfa..a342b92e8 100644 --- a/tests/src/Kernel/DataProducer/EntityDefinitionTest.php +++ b/tests/src/Kernel/DataProducer/EntityDefinitionTest.php @@ -200,7 +200,329 @@ public function testEntityDefinition() { } GQL; - $this->assertResults($query, [], ['entityDefinition' => ['label' => 'Content']]); + $this->assertResults($query, [], [ + 'entityDefinition' => + [ + 'label' => 'Content', + 'fields' => + [ + 0 => + [ + 'id' => 'nid', + 'label' => 'ID', + 'description' => NULL, + 'type' => 'integer', + 'required' => FALSE, + 'multiple' => TRUE, + 'maxNumItems' => 1, + 'status' => TRUE, + 'defaultValue' => NULL, + 'isReference' => FALSE, + 'isHidden' => FALSE, + 'weight' => 0, + 'settings' => NULL, + ], + 1 => + [ + 'id' => 'uuid', + 'label' => 'UUID', + 'description' => NULL, + 'type' => 'uuid', + 'required' => FALSE, + 'multiple' => TRUE, + 'maxNumItems' => 1, + 'status' => TRUE, + 'defaultValue' => NULL, + 'isReference' => FALSE, + 'isHidden' => FALSE, + 'weight' => 0, + 'settings' => NULL, + ], + 2 => + [ + 'id' => 'vid', + 'label' => 'Revision ID', + 'description' => NULL, + 'type' => 'integer', + 'required' => FALSE, + 'multiple' => TRUE, + 'maxNumItems' => 1, + 'status' => TRUE, + 'defaultValue' => NULL, + 'isReference' => FALSE, + 'isHidden' => FALSE, + 'weight' => 0, + 'settings' => NULL, + ], + 3 => + [ + 'id' => 'langcode', + 'label' => 'Language', + 'description' => NULL, + 'type' => 'language', + 'required' => FALSE, + 'multiple' => TRUE, + 'maxNumItems' => 1, + 'status' => TRUE, + 'defaultValue' => NULL, + 'isReference' => FALSE, + 'isHidden' => FALSE, + 'weight' => 2, + 'settings' => NULL, + ], + 4 => + [ + 'id' => 'type', + 'label' => 'Content type', + 'description' => NULL, + 'type' => 'entity_reference', + 'required' => TRUE, + 'multiple' => TRUE, + 'maxNumItems' => 1, + 'status' => TRUE, + 'defaultValue' => NULL, + 'isReference' => TRUE, + 'isHidden' => FALSE, + 'weight' => 0, + 'settings' => NULL, + ], + 5 => + [ + 'id' => 'revision_timestamp', + 'label' => 'Revision create time', + 'description' => 'The time that the current revision was created.', + 'type' => 'created', + 'required' => FALSE, + 'multiple' => TRUE, + 'maxNumItems' => 1, + 'status' => TRUE, + 'defaultValue' => NULL, + 'isReference' => FALSE, + 'isHidden' => FALSE, + 'weight' => 0, + 'settings' => NULL, + ], + 6 => + [ + 'id' => 'revision_uid', + 'label' => 'Revision user', + 'description' => 'The user ID of the author of the current revision.', + 'type' => 'entity_reference', + 'required' => FALSE, + 'multiple' => TRUE, + 'maxNumItems' => 1, + 'status' => TRUE, + 'defaultValue' => NULL, + 'isReference' => TRUE, + 'isHidden' => FALSE, + 'weight' => 0, + 'settings' => NULL, + ], + 7 => + [ + 'id' => 'revision_log', + 'label' => 'Revision log message', + 'description' => 'Briefly describe the changes you have made.', + 'type' => 'string_long', + 'required' => FALSE, + 'multiple' => TRUE, + 'maxNumItems' => 1, + 'status' => TRUE, + 'defaultValue' => NULL, + 'isReference' => FALSE, + 'isHidden' => FALSE, + 'weight' => 25, + 'settings' => NULL, + ], + 8 => + [ + 'id' => 'status', + 'label' => 'Published', + 'description' => NULL, + 'type' => 'boolean', + 'required' => FALSE, + 'multiple' => TRUE, + 'maxNumItems' => 1, + 'status' => TRUE, + 'defaultValue' => '1', + 'isReference' => FALSE, + 'isHidden' => FALSE, + 'weight' => 120, + 'settings' => NULL, + ], + 9 => + [ + 'id' => 'uid', + 'label' => 'Authored by', + 'description' => 'The username of the content author.', + 'type' => 'entity_reference', + 'required' => FALSE, + 'multiple' => TRUE, + 'maxNumItems' => 1, + 'status' => TRUE, + 'defaultValue' => NULL, + 'isReference' => TRUE, + 'isHidden' => FALSE, + 'weight' => 5, + 'settings' => NULL, + ], + 10 => + [ + 'id' => 'title', + 'label' => 'Title', + 'description' => NULL, + 'type' => 'string', + 'required' => TRUE, + 'multiple' => TRUE, + 'maxNumItems' => 1, + 'status' => TRUE, + 'defaultValue' => NULL, + 'isReference' => FALSE, + 'isHidden' => FALSE, + 'weight' => -5, + 'settings' => NULL, + ], + 11 => + [ + 'id' => 'created', + 'label' => 'Authored on', + 'description' => 'The time that the node was created.', + 'type' => 'created', + 'required' => FALSE, + 'multiple' => TRUE, + 'maxNumItems' => 1, + 'status' => TRUE, + 'defaultValue' => NULL, + 'isReference' => FALSE, + 'isHidden' => FALSE, + 'weight' => 10, + 'settings' => NULL, + ], + 12 => + [ + 'id' => 'changed', + 'label' => 'Changed', + 'description' => 'The time that the node was last edited.', + 'type' => 'changed', + 'required' => FALSE, + 'multiple' => TRUE, + 'maxNumItems' => 1, + 'status' => TRUE, + 'defaultValue' => NULL, + 'isReference' => FALSE, + 'isHidden' => FALSE, + 'weight' => 0, + 'settings' => NULL, + ], + 13 => + [ + 'id' => 'promote', + 'label' => 'Promoted to front page', + 'description' => NULL, + 'type' => 'boolean', + 'required' => FALSE, + 'multiple' => TRUE, + 'maxNumItems' => 1, + 'status' => TRUE, + 'defaultValue' => '1', + 'isReference' => FALSE, + 'isHidden' => FALSE, + 'weight' => 15, + 'settings' => NULL, + ], + 14 => + [ + 'id' => 'sticky', + 'label' => 'Sticky at top of lists', + 'description' => NULL, + 'type' => 'boolean', + 'required' => FALSE, + 'multiple' => TRUE, + 'maxNumItems' => 1, + 'status' => TRUE, + 'defaultValue' => '', + 'isReference' => FALSE, + 'isHidden' => FALSE, + 'weight' => 16, + 'settings' => NULL, + ], + 15 => + [ + 'id' => 'default_langcode', + 'label' => 'Default translation', + 'description' => 'A flag indicating whether this is the default translation.', + 'type' => 'boolean', + 'required' => FALSE, + 'multiple' => TRUE, + 'maxNumItems' => 1, + 'status' => TRUE, + 'defaultValue' => '1', + 'isReference' => FALSE, + 'isHidden' => FALSE, + 'weight' => 0, + 'settings' => NULL, + ], + 16 => + [ + 'id' => 'revision_default', + 'label' => 'Default revision', + 'description' => 'A flag indicating whether this was a default revision when it was saved.', + 'type' => 'boolean', + 'required' => FALSE, + 'multiple' => TRUE, + 'maxNumItems' => 1, + 'status' => TRUE, + 'defaultValue' => '', + 'isReference' => FALSE, + 'isHidden' => FALSE, + 'weight' => 0, + 'settings' => NULL, + ], + 17 => + [ + 'id' => 'revision_translation_affected', + 'label' => 'Revision translation affected', + 'description' => 'Indicates if the last edit of a translation belongs to current revision.', + 'type' => 'boolean', + 'required' => FALSE, + 'multiple' => TRUE, + 'maxNumItems' => 1, + 'status' => TRUE, + 'defaultValue' => '', + 'isReference' => FALSE, + 'isHidden' => FALSE, + 'weight' => 0, + 'settings' => NULL, + ], + ], + ], + ] + ); + } + + /** + * {@inheritdoc} + */ + protected function defaultCacheMaxAge() { + // @todo this is wrong, we should have a maximum of caching for entity + // definitions, not 0. + return 0; + } + + /** + * {@inheritdoc} + */ + protected function defaultCacheContexts() { + return ['languages:language_interface', 'user.permissions']; + } + + /** + * {@inheritdoc} + */ + protected function defaultCacheTags() { + $tags = parent::defaultCacheTags(); + $tags[] = 'config:core.entity_form_display.node.article.default'; + return $tags; } } diff --git a/tests/src/Traits/QueryResultAssertionTrait.php b/tests/src/Traits/QueryResultAssertionTrait.php index 2f8d683df..6aa329bd3 100644 --- a/tests/src/Traits/QueryResultAssertionTrait.php +++ b/tests/src/Traits/QueryResultAssertionTrait.php @@ -88,8 +88,8 @@ protected function assertResults($query, array $variables, array $expected, Cach 'variables' => $variables, ]) ); - var_export($result->toArray()['data']); - return; + //var_export($result->toArray()['data']); + //return; $this->assertResultErrors($result, []); $this->assertResultData($result, $expected); From 7bd0c0d57c20cdc8b43f84e7b3b7b3ef0180ec08 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Sun, 13 Dec 2020 23:04:05 +0100 Subject: [PATCH 7/9] fix phpunit serialization problem --- src/GraphQL/Execution/ExecutionResult.php | 12 ++++++++++++ .../src/Kernel/DataProducer/EntityDefinitionTest.php | 1 + 2 files changed, 13 insertions(+) diff --git a/src/GraphQL/Execution/ExecutionResult.php b/src/GraphQL/Execution/ExecutionResult.php index ad17b6817..ceec543ad 100644 --- a/src/GraphQL/Execution/ExecutionResult.php +++ b/src/GraphQL/Execution/ExecutionResult.php @@ -9,4 +9,16 @@ class ExecutionResult extends LibraryExecutionResult implements CacheableDependencyInterface { use RefinableCacheableDependencyTrait; + /** + * PHP Serialization: skip some class members when serializing during tests. + */ + public function __sleep(): array { + // PHPUnit error: Fatal error: Uncaught Exception: Serialization of + // 'Closure' is not allowed. + // Remove some closure-containing members before serializing. + $vars = get_object_vars($this); + unset($vars['extensions']); + return array_keys($vars); + } + } diff --git a/tests/src/Kernel/DataProducer/EntityDefinitionTest.php b/tests/src/Kernel/DataProducer/EntityDefinitionTest.php index a342b92e8..0dfb93cdb 100644 --- a/tests/src/Kernel/DataProducer/EntityDefinitionTest.php +++ b/tests/src/Kernel/DataProducer/EntityDefinitionTest.php @@ -13,6 +13,7 @@ * @group graphql */ class EntityDefinitionTest extends GraphQLTestBase { + /** * {@inheritdoc} */ From afde1b59d53233f3912a1be2152108a6022a0f53 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Sun, 13 Dec 2020 23:43:32 +0100 Subject: [PATCH 8/9] Finish tests --- .../DataProducer/EntityDefinition/Fields.php | 18 +++++----- .../EntityDefinition/Fields/Multiple.php | 4 +++ .../EntityDefinition/Fields/Reference.php | 31 +--------------- .../DataProducer/EntityDefinitionTest.php | 36 +++++++++---------- .../src/Traits/QueryResultAssertionTrait.php | 2 -- 5 files changed, 31 insertions(+), 60 deletions(-) diff --git a/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields.php b/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields.php index 24ebdb2b8..a9341c10f 100644 --- a/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields.php +++ b/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields.php @@ -2,7 +2,7 @@ namespace Drupal\graphql\Plugin\GraphQL\DataProducer\EntityDefinition; -use Drupal\Core\Entity\ContentEntityType; +use Drupal\Core\Entity\ContentEntityTypeInterface; use Drupal\Core\Entity\EntityFieldManager; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManager; @@ -121,24 +121,22 @@ public function resolve( ?array $field_types_context = NULL, FieldContext $field_context ): \Iterator { - $entity_definition->getBundleEntityType(); - if ($entity_definition instanceof ContentEntityType) { + + if ($entity_definition instanceof ContentEntityTypeInterface) { + $entity_type_id = $entity_definition->id(); if ($bundle_context) { $key = $bundle_context['key']; - $id = $entity_definition->id(); - $fields = $this->entityFieldManager->getFieldDefinitions($id, $key); + $fields = $this->entityFieldManager->getFieldDefinitions($entity_type_id, $key); // Set entity form default display as context. - $entity_id = $id . '.' . $key . '.default'; - /** @var \Drupal\Core\Config\Entity\ConfigEntityStorage $form_display_context */ + $form_display_id = $entity_type_id . '.' . $key . '.default'; $form_display_context = $this->entityTypeManager ->getStorage('entity_form_display') - ->load($entity_id); + ->load($form_display_id); $field_context->setContextValue('entity_form_display', $form_display_context); } else { - $id = $entity_definition->id(); - $fields = $this->entityFieldManager->getFieldDefinitions($id, $id); + $fields = $this->entityFieldManager->getFieldDefinitions($entity_type_id, $entity_type_id); } if ($field_types_context) { diff --git a/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Multiple.php b/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Multiple.php index c82252118..edf16a212 100644 --- a/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Multiple.php +++ b/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Multiple.php @@ -3,6 +3,7 @@ namespace Drupal\graphql\Plugin\GraphQL\DataProducer\EntityDefinition\Fields; use Drupal\Core\Field\FieldDefinitionInterface; +use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase; /** @@ -34,6 +35,9 @@ class Multiple extends DataProducerPluginBase { * If the field contains multiple values or just single value. */ public function resolve(FieldDefinitionInterface $entity_definition_field): bool { + if ($entity_definition_field instanceof FieldStorageDefinitionInterface) { + return $entity_definition_field->isMultiple(); + } return $entity_definition_field->isList(); } diff --git a/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Reference.php b/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Reference.php index fe3c11f2e..84db1f63e 100644 --- a/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Reference.php +++ b/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Reference.php @@ -37,36 +37,7 @@ class Reference extends DataProducerPluginBase { * If the field is referencing entities (is the entity reference type). */ public function resolve(FieldDefinitionInterface $entity_definition_field): bool { - if ($entity_definition_field instanceof BaseFieldDefinition) { - /** @var \Drupal\Core\Field\BaseFieldDefinition $entity_definition_field */ - if ($entity_definition_field->getType() === 'entity_reference') { - return TRUE; - } - else { - return FALSE; - } - } - elseif ($entity_definition_field instanceof FieldConfig) { - /** @var \Drupal\field\Entity\FieldConfig $entity_definition_field */ - if ($entity_definition_field->getType() === 'entity_reference') { - return TRUE; - } - else { - return FALSE; - } - } - elseif ($entity_definition_field instanceof BaseFieldOverride) { - /** @var \Drupal\field\Entity\FieldConfig $entity_definition_field */ - if ($entity_definition_field->getType() === 'entity_reference') { - return TRUE; - } - else { - return FALSE; - } - } - else { - return FALSE; - } + return $entity_definition_field->getType() === 'entity_reference'; } } diff --git a/tests/src/Kernel/DataProducer/EntityDefinitionTest.php b/tests/src/Kernel/DataProducer/EntityDefinitionTest.php index 0dfb93cdb..d73d8e61e 100644 --- a/tests/src/Kernel/DataProducer/EntityDefinitionTest.php +++ b/tests/src/Kernel/DataProducer/EntityDefinitionTest.php @@ -214,7 +214,7 @@ public function testEntityDefinition() { 'description' => NULL, 'type' => 'integer', 'required' => FALSE, - 'multiple' => TRUE, + 'multiple' => FALSE, 'maxNumItems' => 1, 'status' => TRUE, 'defaultValue' => NULL, @@ -230,7 +230,7 @@ public function testEntityDefinition() { 'description' => NULL, 'type' => 'uuid', 'required' => FALSE, - 'multiple' => TRUE, + 'multiple' => FALSE, 'maxNumItems' => 1, 'status' => TRUE, 'defaultValue' => NULL, @@ -246,7 +246,7 @@ public function testEntityDefinition() { 'description' => NULL, 'type' => 'integer', 'required' => FALSE, - 'multiple' => TRUE, + 'multiple' => FALSE, 'maxNumItems' => 1, 'status' => TRUE, 'defaultValue' => NULL, @@ -262,7 +262,7 @@ public function testEntityDefinition() { 'description' => NULL, 'type' => 'language', 'required' => FALSE, - 'multiple' => TRUE, + 'multiple' => FALSE, 'maxNumItems' => 1, 'status' => TRUE, 'defaultValue' => NULL, @@ -278,7 +278,7 @@ public function testEntityDefinition() { 'description' => NULL, 'type' => 'entity_reference', 'required' => TRUE, - 'multiple' => TRUE, + 'multiple' => FALSE, 'maxNumItems' => 1, 'status' => TRUE, 'defaultValue' => NULL, @@ -294,7 +294,7 @@ public function testEntityDefinition() { 'description' => 'The time that the current revision was created.', 'type' => 'created', 'required' => FALSE, - 'multiple' => TRUE, + 'multiple' => FALSE, 'maxNumItems' => 1, 'status' => TRUE, 'defaultValue' => NULL, @@ -310,7 +310,7 @@ public function testEntityDefinition() { 'description' => 'The user ID of the author of the current revision.', 'type' => 'entity_reference', 'required' => FALSE, - 'multiple' => TRUE, + 'multiple' => FALSE, 'maxNumItems' => 1, 'status' => TRUE, 'defaultValue' => NULL, @@ -326,7 +326,7 @@ public function testEntityDefinition() { 'description' => 'Briefly describe the changes you have made.', 'type' => 'string_long', 'required' => FALSE, - 'multiple' => TRUE, + 'multiple' => FALSE, 'maxNumItems' => 1, 'status' => TRUE, 'defaultValue' => NULL, @@ -342,7 +342,7 @@ public function testEntityDefinition() { 'description' => NULL, 'type' => 'boolean', 'required' => FALSE, - 'multiple' => TRUE, + 'multiple' => FALSE, 'maxNumItems' => 1, 'status' => TRUE, 'defaultValue' => '1', @@ -358,7 +358,7 @@ public function testEntityDefinition() { 'description' => 'The username of the content author.', 'type' => 'entity_reference', 'required' => FALSE, - 'multiple' => TRUE, + 'multiple' => FALSE, 'maxNumItems' => 1, 'status' => TRUE, 'defaultValue' => NULL, @@ -374,7 +374,7 @@ public function testEntityDefinition() { 'description' => NULL, 'type' => 'string', 'required' => TRUE, - 'multiple' => TRUE, + 'multiple' => FALSE, 'maxNumItems' => 1, 'status' => TRUE, 'defaultValue' => NULL, @@ -390,7 +390,7 @@ public function testEntityDefinition() { 'description' => 'The time that the node was created.', 'type' => 'created', 'required' => FALSE, - 'multiple' => TRUE, + 'multiple' => FALSE, 'maxNumItems' => 1, 'status' => TRUE, 'defaultValue' => NULL, @@ -406,7 +406,7 @@ public function testEntityDefinition() { 'description' => 'The time that the node was last edited.', 'type' => 'changed', 'required' => FALSE, - 'multiple' => TRUE, + 'multiple' => FALSE, 'maxNumItems' => 1, 'status' => TRUE, 'defaultValue' => NULL, @@ -422,7 +422,7 @@ public function testEntityDefinition() { 'description' => NULL, 'type' => 'boolean', 'required' => FALSE, - 'multiple' => TRUE, + 'multiple' => FALSE, 'maxNumItems' => 1, 'status' => TRUE, 'defaultValue' => '1', @@ -438,7 +438,7 @@ public function testEntityDefinition() { 'description' => NULL, 'type' => 'boolean', 'required' => FALSE, - 'multiple' => TRUE, + 'multiple' => FALSE, 'maxNumItems' => 1, 'status' => TRUE, 'defaultValue' => '', @@ -454,7 +454,7 @@ public function testEntityDefinition() { 'description' => 'A flag indicating whether this is the default translation.', 'type' => 'boolean', 'required' => FALSE, - 'multiple' => TRUE, + 'multiple' => FALSE, 'maxNumItems' => 1, 'status' => TRUE, 'defaultValue' => '1', @@ -470,7 +470,7 @@ public function testEntityDefinition() { 'description' => 'A flag indicating whether this was a default revision when it was saved.', 'type' => 'boolean', 'required' => FALSE, - 'multiple' => TRUE, + 'multiple' => FALSE, 'maxNumItems' => 1, 'status' => TRUE, 'defaultValue' => '', @@ -486,7 +486,7 @@ public function testEntityDefinition() { 'description' => 'Indicates if the last edit of a translation belongs to current revision.', 'type' => 'boolean', 'required' => FALSE, - 'multiple' => TRUE, + 'multiple' => FALSE, 'maxNumItems' => 1, 'status' => TRUE, 'defaultValue' => '', diff --git a/tests/src/Traits/QueryResultAssertionTrait.php b/tests/src/Traits/QueryResultAssertionTrait.php index 6aa329bd3..8b7ecad1d 100644 --- a/tests/src/Traits/QueryResultAssertionTrait.php +++ b/tests/src/Traits/QueryResultAssertionTrait.php @@ -88,8 +88,6 @@ protected function assertResults($query, array $variables, array $expected, Cach 'variables' => $variables, ]) ); - //var_export($result->toArray()['data']); - //return; $this->assertResultErrors($result, []); $this->assertResultData($result, $expected); From 5b55cb832ed1c2c1d18447f531a46b446897bf59 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Mon, 14 Dec 2020 15:06:18 +0100 Subject: [PATCH 9/9] fix unused use statments --- .../GraphQL/DataProducer/EntityDefinition/Fields/Reference.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Reference.php b/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Reference.php index 84db1f63e..4723ea4c7 100644 --- a/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Reference.php +++ b/src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Reference.php @@ -2,10 +2,7 @@ namespace Drupal\graphql\Plugin\GraphQL\DataProducer\EntityDefinition\Fields; -use Drupal\Core\Field\BaseFieldDefinition; -use Drupal\Core\Field\Entity\BaseFieldOverride; use Drupal\Core\Field\FieldDefinitionInterface; -use Drupal\field\Entity\FieldConfig; use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase; /**