Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions ProcessMaker/Http/Controllers/Api/DeployScreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class DeployScreen extends Controller
{
use TaskScreenResourceTrait;

private const EMPTY_PRESET_DATA = '{}';

/**
* A whitelist of attributes that should not be
* sanitized by our SanitizeInput middleware.
Expand Down Expand Up @@ -102,6 +104,7 @@ public function getScreenYaml(Request $request)
'computed' => $screenVersion->computed,
'watchers' => $screenVersion->watchers,
'custom_css' => $screenVersion->custom_css,
'preview_presets' => $screenVersion->preview_presets ?? [],
];
$yaml = Yaml::dump($data, 100, 2, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);

Expand Down Expand Up @@ -166,11 +169,16 @@ private function parseDeployYaml(string $ymlContent): array|JsonResponse

private function applyYamlFields(Screen $screen, array $parsedData, bool $created): void
{
if (array_key_exists('preview_presets', $parsedData)) {
$parsedData['preview_presets'] = $this->normalizePreviewPresets($parsedData['preview_presets']);
}

$defaults = [
'config' => [],
'computed' => [],
'watchers' => [],
'custom_css' => '',
'preview_presets' => [],
];

foreach ($defaults as $field => $default) {
Expand All @@ -181,4 +189,79 @@ private function applyYamlFields(Screen $screen, array $parsedData, bool $create
}
}
}

/**
* Designer stores each preset data value as a JSON string so `{}` survives decode.
*
* @return list<array{title: string, data: string}>
*/
private function normalizePreviewPresets(mixed $presets): array
{
if (!is_array($presets)) {
return [];
}

$normalized = [];
foreach ($presets as $preset) {
if (!is_array($preset)) {
continue;
}
$title = $preset['title'] ?? '';
$normalized[] = [
'title' => is_string($title) ? $title : '',
'data' => $this->encodePresetData($preset['data'] ?? self::EMPTY_PRESET_DATA),
];
}

return $normalized;
}

private function encodePresetData(mixed $data): string
{
if (is_array($data)) {
// YAML empty mappings parse as []; json_encode would persist them as [].
$encoded = json_encode(
$this->emptyMapsToObjects($data),
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
);

return $encoded === false ? self::EMPTY_PRESET_DATA : $encoded;
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
}

if (is_string($data) && $this->isJsonString($data)) {
return $data;
}

return self::EMPTY_PRESET_DATA;
}

/**
* Recursively turn empty arrays into objects so preview data keeps {}.
* shortcut: empty YAML sequences under a key also become {}, revisit if presets need [] leaves
*/
private function emptyMapsToObjects(array $data): array|\stdClass
{
if ($data === []) {
return new \stdClass();
}

foreach ($data as $key => $value) {
if (is_array($value)) {
$data[$key] = $this->emptyMapsToObjects($value);
}
}

return $data;
}

private function isJsonString(string $data): bool
{
try {
json_decode($data, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException) {
return false;
}

return true;
}
}
12 changes: 10 additions & 2 deletions ProcessMaker/Mcp/DataConnectors/CollectionCatalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DateTimeInterface;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
Expand Down Expand Up @@ -285,8 +286,15 @@ private function summary(Collection $collection): array
{
$count = 0;
$table = 'collection_' . $collection->id;
if (DB::getSchemaBuilder()->hasTable($table)) {
$count = (int) DB::table($table)->count();
try {
if (DB::getSchemaBuilder()->hasTable($table)) {
$count = (int) DB::table($table)->count();
}
} catch (QueryException $e) {
// MySQL 1412: DDL that created collection_* inside an open txn invalidates the table cache.
if (!str_contains($e->getMessage(), '1412')) {
throw $e;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exception matcher swallows unrelated errors

Low Severity

The 1412 guard uses str_contains on the exception message, and Laravel embeds the SQL (including collection_{id}) in that message. Any QueryException for a collection whose id contains 1412 is treated as the DDL cache issue, so record_count stays 0 and real failures are hidden.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 454eea0. Configure here.

}

return [
Expand Down
80 changes: 80 additions & 0 deletions tests/Feature/Api/DeployScreenControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,84 @@ public function testGetScreenYamlReturnsYamlForPublishedScreen(): void
$response->assertOk();
$this->assertStringContainsString('yaml', (string) $response->headers->get('Content-Type'));
}

public function testDeployScreenPersistsPreviewPresets(): void
{
$yaml = <<<'YAML'
config: []
computed: []
watchers: []
custom_css: ''
preview_presets:
- title: Empty / start
data: '{}'
- title: Happy path
data:
callsign: AVA123
YAML;

$this->apiCall('POST', self::API_ENDPOINT, [
'yml_content' => $yaml,
'name' => 'preset-screen',
])->assertOk();

$screen = Screen::where('title', 'preset-screen')->first();
$expected = [
['title' => 'Empty / start', 'data' => '{}'],
['title' => 'Happy path', 'data' => '{"callsign":"AVA123"}'],
];
$this->assertSame($expected, $screen->preview_presets);
$this->assertSame($expected, $screen->versions()->published()->first()->preview_presets);

$updated = <<<'YAML'
config: []
preview_presets:
- title: Updated
data: '{"callsign":"N45XB"}'
YAML;
$this->apiCall('POST', self::API_ENDPOINT, [
'yml_content' => $updated,
'name' => 'preset-screen',
])->assertOk()->assertJson(['action' => 'updated']);

$screen->refresh();
$this->assertSame(
[['title' => 'Updated', 'data' => '{"callsign":"N45XB"}']],
$screen->preview_presets
);

$pull = $this->actingAs($this->user, 'api')
->get('/api/1.0' . self::GET_ENDPOINT . '?name=preset-screen');
$pull->assertOk();
$this->assertStringContainsString('Updated', $pull->getContent());
$this->assertStringContainsString('N45XB', $pull->getContent());
}

public function testDeployScreenEncodesEmptyYamlMappingsAsObjects(): void
{
$yaml = <<<'YAML'
config: []
preview_presets:
- title: Empty mapping
data: {}
- title: Nested empty mapping
data:
callsign: AVA123
meta: {}
YAML;

$this->apiCall('POST', self::API_ENDPOINT, [
'yml_content' => $yaml,
'name' => 'preset-empty-maps',
])->assertOk();

$screen = Screen::where('title', 'preset-empty-maps')->first();
$this->assertSame(
[
['title' => 'Empty mapping', 'data' => '{}'],
['title' => 'Nested empty mapping', 'data' => '{"callsign":"AVA123","meta":{}}'],
],
$screen->preview_presets
);
}
}
7 changes: 6 additions & 1 deletion tests/Feature/Api/DeployScriptControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,12 @@ public function testGetScriptPhpByNameReturnsPhpCode(): void
public function testDeployScriptPrefersPhpNayraExecutorOnCreate(): void
{
ScriptExecutor::factory()->create(['language' => 'php']);
$nayra = ScriptExecutor::factory()->create(['language' => 'php-nayra']);
// TestSeeder already seeds php-nayra; prefer the lowest-id one (resolver order).
$nayra = ScriptExecutor::query()
->where('language', 'php-nayra')
->orderBy('id')
->first()
?? ScriptExecutor::factory()->create(['language' => 'php-nayra']);

$this->apiCall('POST', self::API_ENDPOINT, [
'code' => '<?php return [];',
Expand Down
9 changes: 8 additions & 1 deletion tests/Feature/Api/ScriptsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ public function testNonPrivilegedUserEditingModulesScriptFallsBackToBare(): void
public function testNonPrivilegedUserDuplicatesModulesScriptAsBare(): void
{
$editor = User::factory()->create(['is_administrator' => false]);
$editor->giveDirectPermission('create-scripts');
$editor->giveDirectPermission('edit-scripts');
$script = $this->getScript('php');
$script->update([
Expand All @@ -589,7 +590,13 @@ public function testNonPrivilegedUserDuplicatesModulesScriptAsBare(): void

$response = $this->actingAs($editor, 'api')->putJson(
route('api.scripts.duplicate', $script),
['title' => 'Duplicate without modules']
[
'title' => 'Duplicate without modules',
'language' => 'php',
'description' => $script->description,
'run_as_user_id' => $script->run_as_user_id,
'script_category_id' => $script->script_category_id,
]
);

$response->assertCreated();
Expand Down
59 changes: 0 additions & 59 deletions tests/Feature/Console/DuplicateProcessRequestTest.php

This file was deleted.

Loading