From 71dbbc67e9cf404d1260d84f36ff451493669655 Mon Sep 17 00:00:00 2001 From: Dieter Holvoet Date: Fri, 15 Apr 2022 13:09:56 +0200 Subject: [PATCH] Validate the --target-bundle option of field:create (#5116) --- src/Drupal/Commands/field/FieldCreateCommands.php | 1 + tests/functional/FieldTest.php | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Drupal/Commands/field/FieldCreateCommands.php b/src/Drupal/Commands/field/FieldCreateCommands.php index f892f0115d..6a81d98562 100644 --- a/src/Drupal/Commands/field/FieldCreateCommands.php +++ b/src/Drupal/Commands/field/FieldCreateCommands.php @@ -429,6 +429,7 @@ protected function createField(): FieldConfigInterface if ($targetTypeDefinition->hasKey('bundle')) { if ($referencedBundle = $this->input->getOption('target-bundle')) { + $this->validateBundle($targetType, $referencedBundle); $referencedBundles = [$referencedBundle]; } else { $referencedBundles = $this->askReferencedBundles($field); diff --git a/tests/functional/FieldTest.php b/tests/functional/FieldTest.php index 80bbc45df3..f772010812 100644 --- a/tests/functional/FieldTest.php +++ b/tests/functional/FieldTest.php @@ -37,9 +37,8 @@ public function testFieldCreate() // New field storage $this->drush('field:create', ['unish_article', 'alpha'], ['field-label' => 'Test', 'field-name' => 'field_test2', 'field-type' => 'entity_reference', 'field-widget' => 'entity_reference_autocomplete', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED], null, null, self::EXIT_ERROR); $this->assertStringContainsString('The target-type option is required.', $this->getErrorOutputRaw()); - /// @todo --target-bundle not yet validated. - // $this->drush('field:create', ['unish_article', 'alpha'], ['field-label' => 'Test', 'field-name' => 'field_test3', 'field-type' => 'entity_reference', 'field-widget' => 'entity_reference_autocomplete', 'cardinality' => '-1', 'target-type' => 'unish_article', 'target-bundle' => 'NO-EXIST']); - // $this->assertStringContainsString('TODO', $this->getErrorOutputRaw()); + $this->drush('field:create', ['unish_article', 'alpha'], ['field-label' => 'Test', 'field-name' => 'field_test6', 'field-type' => 'entity_reference', 'field-widget' => 'entity_reference_autocomplete', 'cardinality' => '-1', 'target-type' => 'unish_article', 'target-bundle' => 'NO-EXIST'], null, null, self::EXIT_ERROR); + $this->assertStringContainsString("Bundle 'NO-EXIST' does not exist on entity type with id 'unish_article'.", $this->getErrorOutputRaw()); $this->drush('field:create', ['unish_article', 'alpha'], ['field-label' => 'Test', 'field-name' => 'field_test3', 'field-type' => 'entity_reference', 'field-widget' => 'entity_reference_autocomplete', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, 'target-type' => 'unish_article', 'target-bundle' => 'beta']); $this->assertStringContainsString("Successfully created field 'field_test3' on unish_article type with bundle 'alpha'", $this->getErrorOutputRaw()); $this->assertStringContainsString("http://dev/admin/structure/unish_article_types/manage/alpha/fields/unish_article.alpha.field_test3", $this->getSimplifiedErrorOutput());