From 6ed8f3ccec9ab6bd2f6cfa01c42e0c48d42f1052 Mon Sep 17 00:00:00 2001 From: Michael Stenta Date: Mon, 9 Oct 2023 13:18:11 -0400 Subject: [PATCH] Validate ID tag types. Fixes #725 --- CHANGELOG.md | 3 ++- modules/core/id_tag/farm_id_tag.module | 3 +++ .../Constraint/IdTagTypeConstraint.php | 24 +++++++++++++++++ .../IdTagTypeConstraintValidator.php | 27 +++++++++++++++++++ .../id_tag/tests/src/Kernel/IdTagTest.php | 12 +++++++++ .../tests/src/Kernel/AssetCsvImportTest.php | 2 +- 6 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 modules/core/id_tag/src/Plugin/Validation/Constraint/IdTagTypeConstraint.php create mode 100644 modules/core/id_tag/src/Plugin/Validation/Constraint/IdTagTypeConstraintValidator.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e8a9589b..978dd9463 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -### Fixes +### Fixed - [Fix asset_lookup and term_lookup exception messages #731](https://github.com/farmOS/farmOS/pull/731) +- [Prevent saving invalid ID tag types #725](https://github.com/farmOS/farmOS/issues/725) ## [2.2.0] 2023-10-06 diff --git a/modules/core/id_tag/farm_id_tag.module b/modules/core/id_tag/farm_id_tag.module index 99bf88620..0b8c9c21c 100644 --- a/modules/core/id_tag/farm_id_tag.module +++ b/modules/core/id_tag/farm_id_tag.module @@ -26,6 +26,9 @@ function farm_id_tag_entity_base_field_info(EntityTypeInterface $entity_type) { ], ]; $fields['id_tag'] = \Drupal::service('farm_field.factory')->baseFieldDefinition($field_info); + + // Add an ID tag type constraint to ID tag fields to ensure valid type. + $fields['id_tag']->addConstraint('IdTagType'); } return $fields; diff --git a/modules/core/id_tag/src/Plugin/Validation/Constraint/IdTagTypeConstraint.php b/modules/core/id_tag/src/Plugin/Validation/Constraint/IdTagTypeConstraint.php new file mode 100644 index 000000000..3baa4cdc3 --- /dev/null +++ b/modules/core/id_tag/src/Plugin/Validation/Constraint/IdTagTypeConstraint.php @@ -0,0 +1,24 @@ +type)) { + return; + } + $bundle = $value->getEntity()->bundle(); + $valid_types = array_keys(farm_id_tag_type_options($bundle)); + if (!in_array($value->type, $valid_types)) { + $this->context->addViolation($constraint->message, ['@type' => $value->type]); + } + } + +} diff --git a/modules/core/id_tag/tests/src/Kernel/IdTagTest.php b/modules/core/id_tag/tests/src/Kernel/IdTagTest.php index c0a280f3c..730a668f4 100644 --- a/modules/core/id_tag/tests/src/Kernel/IdTagTest.php +++ b/modules/core/id_tag/tests/src/Kernel/IdTagTest.php @@ -78,6 +78,18 @@ class IdTagTest extends KernelTestBase { ]); $violations = $asset->validate(); $this->assertEmpty($violations); + + // Confirm that an invalid tag type does not pass validation. + $asset = Asset::create([ + 'name' => $this->randomString(), + 'type' => 'test', + 'id_tag' => [ + 'type' => 'invalid', + ], + ]); + $violations = $asset->validate(); + $this->assertNotEmpty($violations); + $this->assertEquals('Invalid ID tag type: invalid', $violations[0]->getMessage()); } } diff --git a/modules/core/import/modules/csv/tests/src/Kernel/AssetCsvImportTest.php b/modules/core/import/modules/csv/tests/src/Kernel/AssetCsvImportTest.php index 79c8ace93..c20622b3e 100644 --- a/modules/core/import/modules/csv/tests/src/Kernel/AssetCsvImportTest.php +++ b/modules/core/import/modules/csv/tests/src/Kernel/AssetCsvImportTest.php @@ -27,7 +27,7 @@ class AssetCsvImportTest extends CsvImportTestBase { */ public function setUp(): void { parent::setUp(); - $this->installConfig(['farm_equipment']); + $this->installConfig(['farm_id_tag', 'farm_equipment']); // Add an asset to test parent relationship. $asset = Asset::create(['name' => 'Test parent', 'type' => 'equipment', 'status' => 'active']);