From 6b48e1e37014ab8867f1b121ab8695fa2a5d4122 Mon Sep 17 00:00:00 2001 From: Dieter Holvoet Date: Thu, 7 Apr 2022 11:15:44 +0200 Subject: [PATCH 1/4] Validate the --target-bundle option of field:create --- 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..8e265d0ff5 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_test3', 'field-type' => 'entity_reference', 'field-widget' => 'entity_reference_autocomplete', 'cardinality' => '-1', 'target-type' => 'unish_article', 'target-bundle' => 'NO-EXIST']); + $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()); From c061a11b4c3b27ec476f40e5cf749ef66a484622 Mon Sep 17 00:00:00 2001 From: Dieter Holvoet Date: Thu, 7 Apr 2022 12:41:52 +0200 Subject: [PATCH 2/4] Expect an error in the field create test --- tests/functional/FieldTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/functional/FieldTest.php b/tests/functional/FieldTest.php index 8e265d0ff5..e68adb86d9 100644 --- a/tests/functional/FieldTest.php +++ b/tests/functional/FieldTest.php @@ -37,7 +37,7 @@ 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()); - $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->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'], 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()); From 294a426d37b05c8d6cb5fb7f5473517732a1fd23 Mon Sep 17 00:00:00 2001 From: Dieter Holvoet Date: Thu, 7 Apr 2022 13:43:00 +0200 Subject: [PATCH 3/4] Make field name unique --- tests/functional/FieldTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/functional/FieldTest.php b/tests/functional/FieldTest.php index e68adb86d9..58689aacff 100644 --- a/tests/functional/FieldTest.php +++ b/tests/functional/FieldTest.php @@ -39,7 +39,7 @@ public function testFieldCreate() $this->assertStringContainsString('The target-type option is required.', $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' => '-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->drush('field:create', ['unish_article', 'alpha'], ['field-label' => 'Test', 'field-name' => 'field_test6', '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()); $php = "return Drupal::entityTypeManager()->getStorage('field_config')->load('unish_article.alpha.field_test3')->getSettings()"; From f006a97d165020493b60f11e5fecec7f1ca8b707 Mon Sep 17 00:00:00 2001 From: Dieter Holvoet Date: Thu, 7 Apr 2022 13:59:47 +0200 Subject: [PATCH 4/4] Make field name unique --- tests/functional/FieldTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/functional/FieldTest.php b/tests/functional/FieldTest.php index 58689aacff..f772010812 100644 --- a/tests/functional/FieldTest.php +++ b/tests/functional/FieldTest.php @@ -37,9 +37,9 @@ 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()); - $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'], null, null, self::EXIT_ERROR); + $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_test6', 'field-type' => 'entity_reference', 'field-widget' => 'entity_reference_autocomplete', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, 'target-type' => 'unish_article', 'target-bundle' => 'beta']); + $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()); $php = "return Drupal::entityTypeManager()->getStorage('field_config')->load('unish_article.alpha.field_test3')->getSettings()";