Skip to content

Commit

Permalink
Add support for allowDynamicListValues parameter in MetadataField
Browse files Browse the repository at this point in the history
  • Loading branch information
const-cloudinary committed Jan 7, 2025
1 parent 121376a commit c1a95a6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
21 changes: 20 additions & 1 deletion src/Api/Metadata/MetadataFieldList.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
abstract class MetadataFieldList extends MetadataField
{
protected MetadataDataSource $datasource;
protected bool $allowDynamicListValues;

/**
* The MetadataFieldList constructor.
Expand All @@ -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']);
}

/**
Expand Down Expand Up @@ -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;
}
}
4 changes: 3 additions & 1 deletion tests/Integration/Admin/MetadataFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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,
]
);
}
Expand Down
32 changes: 18 additions & 14 deletions tests/Unit/Admin/MetadataFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();

Expand All @@ -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,
]
);
Expand Down Expand Up @@ -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,
]
);
}
Expand All @@ -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();
Expand All @@ -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,

]
);
}
Expand Down

0 comments on commit c1a95a6

Please sign in to comment.