diff --git a/ProcessMaker/Http/Controllers/Api/Concerns/AppliesDeployVersionName.php b/ProcessMaker/Http/Controllers/Api/Concerns/AppliesDeployVersionName.php new file mode 100644 index 0000000000..e202a6c3b6 --- /dev/null +++ b/ProcessMaker/Http/Controllers/Api/Concerns/AppliesDeployVersionName.php @@ -0,0 +1,63 @@ + $rules + * @return array + */ + protected function deployRulesForUpdate(array $rules, bool $isUpdate): array + { + return $this->deployVersionName()->mergeRules($rules, $isUpdate); + } + + protected function ensureDeployVersionHistoryAvailable(bool $isUpdate): ?JsonResponse + { + if ($isUpdate && !$this->deployVersionName()->canRecordHistory()) { + return response()->json([ + 'success' => false, + 'message' => 'Version history (package-versions) is required to publish asset updates.', + ], 422); + } + + return null; + } + + protected function latestPublishedVersionId(Model $asset): int + { + return $this->deployVersionName()->latestPublishedVersionId($asset); + } + + protected function applyDeployVersionName( + Model $asset, + Request $request, + bool $isUpdate, + int $publishedVersionIdBefore, + ): void { + $versionName = $request->input('version_name'); + $description = $request->input('version_description'); + + $this->deployVersionName()->applyAfterSave( + $asset, + is_string($versionName) ? $versionName : null, + (int) $request->user()->id, + $isUpdate, + $publishedVersionIdBefore, + is_string($description) ? $description : null, + ); + } +} diff --git a/ProcessMaker/Http/Controllers/Api/DeployProcess.php b/ProcessMaker/Http/Controllers/Api/DeployProcess.php index 7ff6134846..1070693b7f 100644 --- a/ProcessMaker/Http/Controllers/Api/DeployProcess.php +++ b/ProcessMaker/Http/Controllers/Api/DeployProcess.php @@ -10,6 +10,7 @@ use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Validator; use ProcessMaker\Exception\MissingBpmnReferenceException; +use ProcessMaker\Http\Controllers\Api\Concerns\AppliesDeployVersionName; use ProcessMaker\Http\Controllers\Controller; use ProcessMaker\Models\Process; use ProcessMaker\Services\ProcessBpmnReferenceService; @@ -17,6 +18,8 @@ class DeployProcess extends Controller { + use AppliesDeployVersionName; + /** * A whitelist of attributes that should not be * sanitized by our SanitizeInput middleware. @@ -34,11 +37,21 @@ public function __construct( public function deployProcess(Request $request): JsonResponse { - $validator = Validator::make($request->all(), [ - 'bpmn' => 'required|string', - 'name' => 'required|string|max:255', - 'description' => 'nullable|string|max:65535', - ]); + $name = (string) $request->input('name'); + $isUpdate = Process::where('name', $name)->exists(); + + if ($unavailable = $this->ensureDeployVersionHistoryAvailable($isUpdate)) { + return $unavailable; + } + + $validator = Validator::make( + $request->all(), + $this->deployRulesForUpdate([ + 'bpmn' => 'required|string', + 'name' => 'required|string|max:255', + 'description' => 'nullable|string|max:65535', + ], $isUpdate), + ); if ($validator->fails()) { return response()->json([ @@ -96,8 +109,11 @@ private function upsertProcessByName(Request $request, string $bpmn): array } $process->bpmn = $bpmn; + $publishedVersionIdBefore = $this->latestPublishedVersionId($process); $process->saveOrFail(); + $this->applyDeployVersionName($process, $request, !$created, $publishedVersionIdBefore); + return [$process->refresh(), $created]; }); } diff --git a/ProcessMaker/Http/Controllers/Api/DeployScreen.php b/ProcessMaker/Http/Controllers/Api/DeployScreen.php index 02d36dafb1..7c7a1bee00 100644 --- a/ProcessMaker/Http/Controllers/Api/DeployScreen.php +++ b/ProcessMaker/Http/Controllers/Api/DeployScreen.php @@ -7,6 +7,7 @@ use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; +use ProcessMaker\Http\Controllers\Api\Concerns\AppliesDeployVersionName; use ProcessMaker\Http\Controllers\Controller; use ProcessMaker\Models\Screen; use ProcessMaker\Traits\TaskScreenResourceTrait; @@ -14,6 +15,7 @@ class DeployScreen extends Controller { + use AppliesDeployVersionName; use TaskScreenResourceTrait; private const EMPTY_PRESET_DATA = '{}'; @@ -30,10 +32,20 @@ class DeployScreen extends Controller public function deployScreen(Request $request): JsonResponse { - $validator = Validator::make($request->all(), [ - 'yml_content' => 'required|string', - 'name' => 'required|string', - ]); + $name = (string) $request->input('name'); + $isUpdate = Screen::where('title', $name)->exists(); + + if ($unavailable = $this->ensureDeployVersionHistoryAvailable($isUpdate)) { + return $unavailable; + } + + $validator = Validator::make( + $request->all(), + $this->deployRulesForUpdate([ + 'yml_content' => 'required|string', + 'name' => 'required|string', + ], $isUpdate), + ); if ($validator->fails()) { return response()->json([ @@ -42,9 +54,12 @@ public function deployScreen(Request $request): JsonResponse ], 422); } - $ymlContent = $request->input('yml_content'); - $name = $request->input('name'); + return $this->persistDeployedScreen($request, $name); + } + private function persistDeployedScreen(Request $request, string $name): JsonResponse + { + $ymlContent = $request->input('yml_content'); $parsedData = $this->parseDeployYaml($ymlContent); if ($parsedData instanceof JsonResponse) { return $parsedData; @@ -61,7 +76,9 @@ public function deployScreen(Request $request): JsonResponse } $this->applyYamlFields($screen, $parsedData, $created); + $publishedVersionIdBefore = $this->latestPublishedVersionId($screen); $screen->save(); + $this->applyDeployVersionName($screen, $request, !$created, $publishedVersionIdBefore); $screen->refresh(); return response()->json([ diff --git a/ProcessMaker/Http/Controllers/Api/DeployScript.php b/ProcessMaker/Http/Controllers/Api/DeployScript.php index 8d978ab0c9..9f83f8a4e7 100644 --- a/ProcessMaker/Http/Controllers/Api/DeployScript.php +++ b/ProcessMaker/Http/Controllers/Api/DeployScript.php @@ -7,12 +7,15 @@ use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; +use ProcessMaker\Http\Controllers\Api\Concerns\AppliesDeployVersionName; use ProcessMaker\Http\Controllers\Controller; use ProcessMaker\Models\Script; use ProcessMaker\Services\ScriptExecutorResolver; class DeployScript extends Controller { + use AppliesDeployVersionName; + /** * A whitelist of attributes that should not be * sanitized by our SanitizeInput middleware. @@ -38,10 +41,20 @@ public function __construct(private readonly ScriptExecutorResolver $executors) */ public function deployScript(Request $request): JsonResponse { - $validator = Validator::make($request->all(), [ - 'code' => 'required|string', - 'name' => 'required|string', - ]); + $name = (string) $request->input('name'); + $isUpdate = Script::where('title', $name)->exists(); + + if ($unavailable = $this->ensureDeployVersionHistoryAvailable($isUpdate)) { + return $unavailable; + } + + $validator = Validator::make( + $request->all(), + $this->deployRulesForUpdate([ + 'code' => 'required|string', + 'name' => 'required|string', + ], $isUpdate), + ); if ($validator->fails()) { return response()->json([ @@ -50,9 +63,12 @@ public function deployScript(Request $request): JsonResponse ], 422); } - $name = $request->input('name'); - $code = $request->input('code'); + return $this->persistDeployedScript($request, $name); + } + private function persistDeployedScript(Request $request, string $name): JsonResponse + { + $code = $request->input('code'); $script = Script::where('title', $name)->first(); $created = $script === null; @@ -77,7 +93,9 @@ public function deployScript(Request $request): JsonResponse } $script->code = $code; + $publishedVersionIdBefore = $this->latestPublishedVersionId($script); $script->save(); + $this->applyDeployVersionName($script, $request, !$created, $publishedVersionIdBefore); $script->refresh(); return response()->json([ diff --git a/ProcessMaker/Services/DeployVersionName.php b/ProcessMaker/Services/DeployVersionName.php new file mode 100644 index 0000000000..d8c778e890 --- /dev/null +++ b/ProcessMaker/Services/DeployVersionName.php @@ -0,0 +1,127 @@ + + */ + public function validationRules(bool $isUpdate): array + { + if ($isUpdate) { + return ['version_name' => 'required|string|min:1|max:255']; + } + + return ['version_name' => 'nullable|string|max:255']; + } + + /** + * @param array $rules + * @return array + */ + public function mergeRules(array $rules, bool $isUpdate): array + { + return array_merge($rules, $this->validationRules($isUpdate)); + } + + public function canRecordHistory(): bool + { + return class_exists(VersionHistory::class) + && Schema::hasTable('version_history'); + } + + public function latestPublishedVersionId(Model $asset): int + { + if (!method_exists($asset, 'versions')) { + return 0; + } + + $maxId = $asset->versions()->published()->max('id'); + + return $maxId !== null ? (int) $maxId : 0; + } + + public function applyAfterSave( + Model $asset, + ?string $versionName, + int $userId, + bool $isUpdate, + int $publishedVersionIdBefore, + ?string $description = null, + ): void { + $subject = $versionName !== null ? trim($versionName) : ''; + if ( + $subject === '' + || !$this->canRecordHistory() + || !method_exists($asset, 'versions') + ) { + return; + } + + $version = $this->resolvePublishedVersionAfterSave($asset, $publishedVersionIdBefore) + ?? $this->resolveCurrentPublishedForRedeploy($asset, $isUpdate, $publishedVersionIdBefore); + if ($version === null) { + if ($isUpdate) { + throw ValidationException::withMessages([ + 'version_name' => 'Deploy did not publish a new asset version.', + ]); + } + + return; + } + + $history = new VersionHistory(); + $history->user_id = $userId; + $history->versionable_id = (int) $version->getKey(); + $history->versionable_type = $version::class; + $history->subject = $subject; + $history->description = $description !== null && trim($description) !== '' + ? trim($description) + : null; + $history->status = 'ACTIVE'; + $history->saveOrFail(); + } + + private function resolvePublishedVersionAfterSave(Model $asset, int $publishedVersionIdBefore): ?Model + { + $version = $asset->versions()->published()->orderByDesc('id')->first(); + if ($version === null) { + return null; + } + + if ((int) $version->getKey() <= $publishedVersionIdBefore) { + return null; + } + + return $version; + } + + /** + * Scripts can skip publishing when a draft exists and versionable content is unchanged. + * Still record deploy version history on the current published row (idempotent upsert). + */ + private function resolveCurrentPublishedForRedeploy( + Model $asset, + bool $isUpdate, + int $publishedVersionIdBefore, + ): ?Model { + if (!$isUpdate || $publishedVersionIdBefore < 1) { + return null; + } + + $version = $asset->versions()->published()->orderByDesc('id')->first(); + if ($version === null) { + return null; + } + + return (int) $version->getKey() === $publishedVersionIdBefore ? $version : null; + } +} diff --git a/tests/Feature/Api/DeployProcessControllerTest.php b/tests/Feature/Api/DeployProcessControllerTest.php index 265f624e13..c8cc8e1838 100644 --- a/tests/Feature/Api/DeployProcessControllerTest.php +++ b/tests/Feature/Api/DeployProcessControllerTest.php @@ -3,8 +3,10 @@ namespace Tests\Feature\Api; use ProcessMaker\Models\Process; +use ProcessMaker\Models\ProcessVersion; use ProcessMaker\Models\Screen; use ProcessMaker\Models\Script; +use ProcessMaker\Package\Versions\Models\VersionHistory; use Tests\Feature\Shared\RequestHelper; use Tests\TestCase; @@ -58,11 +60,65 @@ public function testDeployProcessAcceptsDescription(): void 'name' => 'described-process', 'description' => $updated, 'bpmn' => $this->sampleBpmn('', ''), + 'version_name' => 'Update process description', ])->assertOk(); $this->assertSame($updated, $process->refresh()->description); } + public function testDeployProcessUpdateRequiresVersionName(): void + { + $name = 'version-name-process'; + + $this->apiCall('POST', self::DEPLOY_ENDPOINT, [ + 'name' => $name, + 'bpmn' => $this->sampleBpmn('', ''), + ])->assertOk(); + + $this->apiCall('POST', self::DEPLOY_ENDPOINT, [ + 'name' => $name, + 'bpmn' => $this->sampleBpmn('', ''), + ]) + ->assertStatus(422) + ->assertJsonValidationErrors(['version_name']); + } + + public function testDeployProcessUpdateStoresVersionName(): void + { + if (!class_exists(VersionHistory::class)) { + $this->markTestSkipped('package-versions is not installed.'); + } + + $name = 'version-name-process-stored'; + $summary = 'Adjust BPMN layout'; + + Screen::factory()->create(['title' => 'vn-screen']); + + $this->apiCall('POST', self::DEPLOY_ENDPOINT, [ + 'name' => $name, + 'bpmn' => $this->sampleBpmn('', ''), + ])->assertOk(); + + $this->apiCall('POST', self::DEPLOY_ENDPOINT, [ + 'name' => $name, + 'bpmn' => $this->sampleBpmn('${screen:vn-screen}', ''), + 'version_name' => $summary, + ])->assertOk(); + + $process = Process::where('name', $name)->firstOrFail(); + $version = $process->versions()->orderByDesc('id')->first(); + $this->assertInstanceOf(ProcessVersion::class, $version); + + $history = VersionHistory::query() + ->where('versionable_id', $version->id) + ->where('versionable_type', ProcessVersion::class) + ->orderByDesc('id') + ->first(); + + $this->assertNotNull($history); + $this->assertSame($summary, $history->subject); + } + public function testGetProcessBpmnRestoresNamedReferences(): void { Screen::factory()->create(['title' => 'restore-screen']); diff --git a/tests/Feature/Api/DeployScreenControllerTest.php b/tests/Feature/Api/DeployScreenControllerTest.php index a207af317f..3b3c31aef6 100644 --- a/tests/Feature/Api/DeployScreenControllerTest.php +++ b/tests/Feature/Api/DeployScreenControllerTest.php @@ -3,6 +3,8 @@ namespace Tests\Feature\Api; use ProcessMaker\Models\Screen; +use ProcessMaker\Models\ScreenVersion; +use ProcessMaker\Package\Versions\Models\VersionHistory; use Tests\Feature\Shared\RequestHelper; use Tests\TestCase; @@ -47,6 +49,58 @@ public function testDeployScreenCreatesFromYaml(): void $this->assertNotNull(Screen::where('title', 'sample-screen')->first()); } + public function testDeployScreenUpdateRequiresVersionName(): void + { + $name = 'version-name-required-screen'; + $yaml = "config: []\n"; + + $this->apiCall('POST', self::API_ENDPOINT, [ + 'yml_content' => $yaml, + 'name' => $name, + ])->assertOk(); + + $this->apiCall('POST', self::API_ENDPOINT, [ + 'yml_content' => "config: []\ncomputed: []\n", + 'name' => $name, + ]) + ->assertStatus(422) + ->assertJsonValidationErrors(['version_name']); + } + + public function testDeployScreenUpdateStoresVersionName(): void + { + if (!class_exists(VersionHistory::class)) { + $this->markTestSkipped('package-versions is not installed.'); + } + + $name = 'version-name-stored-screen'; + $summary = 'Add computed properties'; + + $this->apiCall('POST', self::API_ENDPOINT, [ + 'yml_content' => "config: []\n", + 'name' => $name, + ])->assertOk(); + + $this->apiCall('POST', self::API_ENDPOINT, [ + 'yml_content' => "config: []\ncomputed: []\n", + 'name' => $name, + 'version_name' => $summary, + ])->assertOk(); + + $screen = Screen::where('title', $name)->firstOrFail(); + $version = $screen->versions()->orderByDesc('id')->first(); + $this->assertInstanceOf(ScreenVersion::class, $version); + + $history = VersionHistory::query() + ->where('versionable_id', $version->id) + ->where('versionable_type', ScreenVersion::class) + ->orderByDesc('id') + ->first(); + + $this->assertNotNull($history); + $this->assertSame($summary, $history->subject); + } + public function testGetScreenYamlReturnsYamlForPublishedScreen(): void { $yaml = "config: []\n"; @@ -99,6 +153,7 @@ public function testDeployScreenPersistsPreviewPresets(): void $this->apiCall('POST', self::API_ENDPOINT, [ 'yml_content' => $updated, 'name' => 'preset-screen', + 'version_name' => 'Update preview presets', ])->assertOk()->assertJson(['action' => 'updated']); $screen->refresh(); diff --git a/tests/Feature/Api/DeployScriptControllerTest.php b/tests/Feature/Api/DeployScriptControllerTest.php index 71bdf8f07c..11e23338e8 100644 --- a/tests/Feature/Api/DeployScriptControllerTest.php +++ b/tests/Feature/Api/DeployScriptControllerTest.php @@ -4,6 +4,9 @@ use ProcessMaker\Models\Script; use ProcessMaker\Models\ScriptExecutor; +use ProcessMaker\Models\ScriptVersion; +use ProcessMaker\Package\Versions\Models\VersionHistory; +use ProcessMaker\Services\ScriptExecutorResolver; use Tests\Feature\Shared\RequestHelper; use Tests\TestCase; @@ -88,6 +91,7 @@ public function testDeployScriptUpdatesExistingScriptByTitle(): void $this->apiCall('POST', self::API_ENDPOINT, [ 'code' => $newCode, 'name' => 'sample-update-script', + 'version_name' => 'Refresh script payload', ]) ->assertStatus(200) ->assertJson([ @@ -100,6 +104,102 @@ public function testDeployScriptUpdatesExistingScriptByTitle(): void $this->assertSame(rtrim($newCode), rtrim((string) $existing->code)); } + public function testDeployScriptUpdateRequiresVersionName(): void + { + $this->ensureExecutor(); + $name = 'version-name-script'; + + $this->apiCall('POST', self::API_ENDPOINT, [ + 'code' => ' 1];', + 'name' => $name, + ])->assertOk(); + + $this->apiCall('POST', self::API_ENDPOINT, [ + 'code' => ' 2];', + 'name' => $name, + ]) + ->assertStatus(422) + ->assertJsonValidationErrors(['version_name']); + } + + public function testDeployScriptUpdateStoresVersionNameWhenCodeUnchangedAndDraftExists(): void + { + if (!class_exists(VersionHistory::class)) { + $this->markTestSkipped('package-versions is not installed.'); + } + + $this->ensureExecutor(); + $name = 'unchanged-deploy-with-draft'; + $code = ' 1];'; + $summary = 'Redeploy unchanged payload'; + + $this->apiCall('POST', self::API_ENDPOINT, [ + 'code' => $code, + 'name' => $name, + ])->assertOk(); + + $script = Script::where('title', $name)->firstOrFail(); + $publishedVersionId = (int) $script->versions()->published()->max('id'); + $script->saveDraft(); + + $this->apiCall('POST', self::API_ENDPOINT, [ + 'code' => $code, + 'name' => $name, + 'version_name' => $summary, + ]) + ->assertOk() + ->assertJson(['action' => 'updated']); + + $this->assertSame( + $publishedVersionId, + (int) $script->versions()->published()->max('id') + ); + + $history = VersionHistory::query() + ->where('versionable_id', $publishedVersionId) + ->where('versionable_type', ScriptVersion::class) + ->orderByDesc('id') + ->first(); + + $this->assertNotNull($history); + $this->assertSame($summary, $history->subject); + } + + public function testDeployScriptUpdateStoresVersionName(): void + { + if (!class_exists(VersionHistory::class)) { + $this->markTestSkipped('package-versions is not installed.'); + } + + $this->ensureExecutor(); + $name = 'version-name-script-stored'; + $summary = 'Bump return payload'; + + $this->apiCall('POST', self::API_ENDPOINT, [ + 'code' => ' $name, + ])->assertOk(); + + $this->apiCall('POST', self::API_ENDPOINT, [ + 'code' => ' $name, + 'version_name' => $summary, + ])->assertOk(); + + $script = Script::where('title', $name)->firstOrFail(); + $version = $script->versions()->orderByDesc('id')->first(); + $this->assertInstanceOf(ScriptVersion::class, $version); + + $history = VersionHistory::query() + ->where('versionable_id', $version->id) + ->where('versionable_type', ScriptVersion::class) + ->orderByDesc('id') + ->first(); + + $this->assertNotNull($history); + $this->assertSame($summary, $history->subject); + } + public function testGetScriptPhpByNameReturnsPhpCode(): void { $code = " true];\n"; @@ -120,13 +220,13 @@ public function testGetScriptPhpByNameReturnsPhpCode(): void public function testDeployScriptPrefersPhpNayraExecutorOnCreate(): void { - ScriptExecutor::factory()->create(['language' => 'php']); - // 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']); + if (!ScriptExecutor::query()->where('language', 'php-nayra')->exists()) { + ScriptExecutor::factory()->create(['language' => 'php-nayra']); + } + + $executor = app(ScriptExecutorResolver::class)->resolveForNewScript(); + $this->assertNotNull($executor); + $this->assertSame('php-nayra', $executor->language); $this->apiCall('POST', self::API_ENDPOINT, [ 'code' => 'assertJson(['action' => 'created']); $script = Script::where('title', 'new-nayra-script')->first(); - $this->assertSame($nayra->id, $script->script_executor_id); + $this->assertSame($executor->id, $script->script_executor_id); } private function ensureExecutor(): ScriptExecutor diff --git a/tests/Feature/Api/ScriptsTest.php b/tests/Feature/Api/ScriptsTest.php index 603cd96374..bbccc80d33 100644 --- a/tests/Feature/Api/ScriptsTest.php +++ b/tests/Feature/Api/ScriptsTest.php @@ -580,8 +580,8 @@ public function testNonPrivilegedUserEditingModulesScriptFallsBackToBare(): void public function testNonPrivilegedUserDuplicatesModulesScriptAsBare(): void { $editor = User::factory()->create(['is_administrator' => false]); - $editor->giveDirectPermission('create-scripts'); $editor->giveDirectPermission('edit-scripts'); + $editor->giveDirectPermission('create-scripts'); $script = $this->getScript('php'); $script->update([ 'allow_in_process' => true, diff --git a/tests/Feature/Mcp/DataConnectorsServerTest.php b/tests/Feature/Mcp/DataConnectorsServerTest.php index bed8616ff7..c99000da00 100644 --- a/tests/Feature/Mcp/DataConnectorsServerTest.php +++ b/tests/Feature/Mcp/DataConnectorsServerTest.php @@ -29,10 +29,18 @@ use Tests\Feature\Shared\RequestHelper; use Tests\TestCase; +/** + * CollectionObserver DDL + record inserts conflict with RefreshDatabase transactions (MySQL 1412). + */ class DataConnectorsServerTest extends TestCase { use RequestHelper; + /** + * @var list + */ + protected $connectionsToTransact = []; + protected function setUp(): void { parent::setUp();