From c1a95a6ce19f39da57cc9b7e13cd5ab2c8337328 Mon Sep 17 00:00:00 2001 From: Constantine Nathanson <35217733+const-cloudinary@users.noreply.github.com> Date: Tue, 7 Jan 2025 02:59:29 +0200 Subject: [PATCH] Add support for `allowDynamicListValues` parameter in `MetadataField` --- src/Api/Metadata/MetadataFieldList.php | 21 +++++++++++- .../Integration/Admin/MetadataFieldsTest.php | 4 ++- tests/Unit/Admin/MetadataFieldsTest.php | 32 +++++++++++-------- 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/Api/Metadata/MetadataFieldList.php b/src/Api/Metadata/MetadataFieldList.php index c98e5b64..d3168261 100644 --- a/src/Api/Metadata/MetadataFieldList.php +++ b/src/Api/Metadata/MetadataFieldList.php @@ -20,6 +20,7 @@ abstract class MetadataFieldList extends MetadataField { protected MetadataDataSource $datasource; + protected bool $allowDynamicListValues; /** * The MetadataFieldList constructor. @@ -41,7 +42,7 @@ public function __construct(string $label, array|MetadataDataSource $dataSource */ public function getPropertyKeys(): array { - return array_merge(parent::getPropertyKeys(), ['datasource']); + return array_merge(parent::getPropertyKeys(), ['datasource', 'allowDynamicListValues']); } /** @@ -71,4 +72,22 @@ public function setDataSource(array|MetadataDataSource $metadataDataSource): voi throw new InvalidArgumentException('The specified MetadataFieldList datasource is not valid.'); } } + + /** + * Gets the value indicating whether the field should allow dynamic list values. + */ + public function isAllowDynamicListValues(): bool + { + return $this->allowDynamicListValues; + } + + /** + * Sets the value indicating whether the field should allow dynamic list values. + * + * @param bool $allowDynamicListValues The value to set. + */ + public function setAllowDynamicListValues(bool $allowDynamicListValues = true): void + { + $this->allowDynamicListValues = $allowDynamicListValues; + } } diff --git a/tests/Integration/Admin/MetadataFieldsTest.php b/tests/Integration/Admin/MetadataFieldsTest.php index 2f3cc094..05bac6d6 100644 --- a/tests/Integration/Admin/MetadataFieldsTest.php +++ b/tests/Integration/Admin/MetadataFieldsTest.php @@ -246,6 +246,7 @@ public function testCreateSetMetadataField() $setMetadataField = new SetMetadataField(self::$EXTERNAL_ID_SET, self::$DATASOURCE_MULTIPLE); $setMetadataField->setExternalId(self::$EXTERNAL_ID_SET); $setMetadataField->setDefaultValue([self::$DATASOURCE_ENTRY_EXTERNAL_ID, 'v4']); + $setMetadataField->setAllowDynamicListValues(false); $result = self::$adminApi->addMetadataField($setMetadataField); @@ -256,7 +257,8 @@ public function testCreateSetMetadataField() 'label' => self::$EXTERNAL_ID_SET, 'external_id' => self::$EXTERNAL_ID_SET, 'mandatory' => false, - 'default_value' => [self::$DATASOURCE_ENTRY_EXTERNAL_ID, 'v4'] + 'default_value' => [self::$DATASOURCE_ENTRY_EXTERNAL_ID, 'v4'], + 'allow_dynamic_list_values' => false, ] ); } diff --git a/tests/Unit/Admin/MetadataFieldsTest.php b/tests/Unit/Admin/MetadataFieldsTest.php index 9f67d1c5..06f272c2 100644 --- a/tests/Unit/Admin/MetadataFieldsTest.php +++ b/tests/Unit/Admin/MetadataFieldsTest.php @@ -30,12 +30,13 @@ class MetadataFieldsTest extends UnitTestCase const EXTERNAL_ID_INT = 'metadata_external_id_int'; const EXTERNAL_ID_ENUM = 'metadata_external_id_enum'; const EXTERNAL_ID_DELETE = 'metadata_deletion_test'; - const DATASOURCE_SINGLE = [ - [ - 'value' => 'v1', - 'external_id' => 'metadata_datasource_entry_external_id' - ] - ]; + const DATASOURCE_SINGLE + = [ + [ + 'value' => 'v1', + 'external_id' => 'metadata_datasource_entry_external_id', + ], + ]; /** * Test getting a list of all metadata fields. @@ -65,7 +66,7 @@ public function testCreateStringMetadataField() $stringMetadataField = new StringMetadataField(self::EXTERNAL_ID_STRING); $stringMetadataField->setExternalId(self::EXTERNAL_ID_STRING); - $stringMetadataField->setRestrictions(["readonly_ui" => true]); + $stringMetadataField->setRestrictions(['readonly_ui' => true]); $stringMetadataField->setMandatory(false); $stringMetadataField->setDefaultDisabled(); @@ -81,7 +82,7 @@ public function testCreateStringMetadataField() 'external_id' => self::EXTERNAL_ID_STRING, 'label' => self::EXTERNAL_ID_STRING, 'mandatory' => false, - 'restrictions' => ["readonly_ui" => true], + 'restrictions' => ['readonly_ui' => true], 'default_disabled' => true, ] ); @@ -109,7 +110,7 @@ public function testCreateIntMetadataField() [ 'type' => MetadataFieldType::INTEGER, 'external_id' => self::EXTERNAL_ID_INT, - 'label' => self::EXTERNAL_ID_INT + 'label' => self::EXTERNAL_ID_INT, ] ); } @@ -126,6 +127,7 @@ public function testCreateEnumMetadataField() $enumMetadataField = new EnumMetadataField(self::EXTERNAL_ID_ENUM, self::DATASOURCE_SINGLE); $enumMetadataField->setDataSource(self::DATASOURCE_SINGLE); $enumMetadataField->setExternalId(self::EXTERNAL_ID_ENUM); + $enumMetadataField->setAllowDynamicListValues(); $mockAdminApi->addMetadataField($enumMetadataField); $lastRequest = $mockAdminApi->getMockHandler()->getLastRequest(); @@ -135,12 +137,14 @@ public function testCreateEnumMetadataField() self::assertRequestFields( $lastRequest, [ - 'datasource' => [ - 'values' => self::DATASOURCE_SINGLE + 'datasource' => [ + 'values' => self::DATASOURCE_SINGLE, ], - 'external_id' => self::EXTERNAL_ID_ENUM, - 'label' => self::EXTERNAL_ID_ENUM, - 'type' => MetadataFieldType::ENUM + 'external_id' => self::EXTERNAL_ID_ENUM, + 'label' => self::EXTERNAL_ID_ENUM, + 'type' => MetadataFieldType::ENUM, + 'allow_dynamic_list_values' => true, + ] ); }