mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-01-13 15:07:49 +00:00
add checks for comma when importing recipies
This commit is contained in:
parent
164a990dfe
commit
e176f2828e
2 changed files with 87 additions and 0 deletions
|
|
@ -17,6 +17,32 @@ use ZipArchive;
|
||||||
|
|
||||||
class PluginImportService
|
class PluginImportService
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Validate YAML settings
|
||||||
|
*
|
||||||
|
* @param array $settings The parsed YAML settings
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function validateYAML(array $settings): void
|
||||||
|
{
|
||||||
|
if (!isset($settings['custom_fields']) || !is_array($settings['custom_fields'])) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($settings['custom_fields'] as $field) {
|
||||||
|
if (isset($field['field_type']) && $field['field_type'] === 'multi_string') {
|
||||||
|
|
||||||
|
if (isset($field['default']) && str_contains($field['default'], ',')) {
|
||||||
|
throw new Exception("Validation Error: The default value for multistring fields like `{$field['keyname']}` cannot contain commas.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($field['placeholder']) && str_contains($field['placeholder'], ',')) {
|
||||||
|
throw new Exception("Validation Error: The placeholder value for multistring fields like `{$field['keyname']}` cannot contain commas.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Import a plugin from a ZIP file
|
* Import a plugin from a ZIP file
|
||||||
*
|
*
|
||||||
|
|
@ -58,6 +84,7 @@ class PluginImportService
|
||||||
// Parse settings.yml
|
// Parse settings.yml
|
||||||
$settingsYaml = File::get($filePaths['settingsYamlPath']);
|
$settingsYaml = File::get($filePaths['settingsYamlPath']);
|
||||||
$settings = Yaml::parse($settingsYaml);
|
$settings = Yaml::parse($settingsYaml);
|
||||||
|
$this->validateYAML($settings);
|
||||||
|
|
||||||
// Read full.liquid content
|
// Read full.liquid content
|
||||||
$fullLiquid = File::get($filePaths['fullLiquidPath']);
|
$fullLiquid = File::get($filePaths['fullLiquidPath']);
|
||||||
|
|
@ -187,6 +214,7 @@ class PluginImportService
|
||||||
// Parse settings.yml
|
// Parse settings.yml
|
||||||
$settingsYaml = File::get($filePaths['settingsYamlPath']);
|
$settingsYaml = File::get($filePaths['settingsYamlPath']);
|
||||||
$settings = Yaml::parse($settingsYaml);
|
$settings = Yaml::parse($settingsYaml);
|
||||||
|
$this->validateYAML($settings);
|
||||||
|
|
||||||
// Read full.liquid content
|
// Read full.liquid content
|
||||||
$fullLiquid = File::get($filePaths['fullLiquidPath']);
|
$fullLiquid = File::get($filePaths['fullLiquidPath']);
|
||||||
|
|
|
||||||
|
|
@ -427,6 +427,65 @@ YAML;
|
||||||
->and($displayIncidentField['default'])->toBe('true');
|
->and($displayIncidentField['default'])->toBe('true');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('throws exception when multi_string default value contains a comma', function (): void {
|
||||||
|
$user = User::factory()->create();
|
||||||
|
|
||||||
|
// YAML with a comma in the 'default' field of a multi_string
|
||||||
|
$invalidYaml = <<<YAML
|
||||||
|
name: Test Plugin
|
||||||
|
refresh_interval: 30
|
||||||
|
strategy: static
|
||||||
|
polling_verb: get
|
||||||
|
static_data: '{"test": "data"}'
|
||||||
|
custom_fields:
|
||||||
|
- keyname: api_key
|
||||||
|
field_type: multi_string
|
||||||
|
default: default-api-key1,default-api-key2
|
||||||
|
label: API Key
|
||||||
|
YAML;
|
||||||
|
|
||||||
|
$zipContent = createMockZipFile([
|
||||||
|
'src/settings.yml' => $invalidYaml,
|
||||||
|
'src/full.liquid' => getValidFullLiquid(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$zipFile = UploadedFile::fake()->createWithContent('invalid-default.zip', $zipContent);
|
||||||
|
$pluginImportService = new PluginImportService();
|
||||||
|
|
||||||
|
expect(fn () => $pluginImportService->importFromZip($zipFile, $user))
|
||||||
|
->toThrow(Exception::class, "Validation Error: The default value for multistring fields like `api_key` cannot contain commas.");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws exception when multi_string placeholder contains a comma', function (): void {
|
||||||
|
$user = User::factory()->create();
|
||||||
|
|
||||||
|
// YAML with a comma in the 'placeholder' field
|
||||||
|
$invalidYaml = <<<YAML
|
||||||
|
name: Test Plugin
|
||||||
|
refresh_interval: 30
|
||||||
|
strategy: static
|
||||||
|
polling_verb: get
|
||||||
|
static_data: '{"test": "data"}'
|
||||||
|
custom_fields:
|
||||||
|
- keyname: api_key
|
||||||
|
field_type: multi_string
|
||||||
|
default: default-api-key
|
||||||
|
label: API Key
|
||||||
|
placeholder: "value1, value2"
|
||||||
|
YAML;
|
||||||
|
|
||||||
|
$zipContent = createMockZipFile([
|
||||||
|
'src/settings.yml' => $invalidYaml,
|
||||||
|
'src/full.liquid' => getValidFullLiquid(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$zipFile = UploadedFile::fake()->createWithContent('invalid-placeholder.zip', $zipContent);
|
||||||
|
$pluginImportService = new PluginImportService();
|
||||||
|
|
||||||
|
expect(fn () => $pluginImportService->importFromZip($zipFile, $user))
|
||||||
|
->toThrow(Exception::class, "Validation Error: The placeholder value for multistring fields like `api_key` cannot contain commas.");
|
||||||
|
});
|
||||||
|
|
||||||
// Helper methods
|
// Helper methods
|
||||||
function createMockZipFile(array $files): string
|
function createMockZipFile(array $files): string
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue