diff --git a/ProcessMaker/ImportExport/Exporters/ExporterBase.php b/ProcessMaker/ImportExport/Exporters/ExporterBase.php index e951581bee..5b2a12181e 100644 --- a/ProcessMaker/ImportExport/Exporters/ExporterBase.php +++ b/ProcessMaker/ImportExport/Exporters/ExporterBase.php @@ -57,6 +57,13 @@ abstract class ExporterBase implements ExporterInterface public static $forceUpdate = false; + /** + * Attribute names that must not be overwritten when importing in update mode. + * + * @var array + */ + public static $doNotUpdateAttributes = []; + // public $ignoreExplicitDiscard = false; public static $fallbackMatchColumn = null; diff --git a/ProcessMaker/ImportExport/Exporters/ScriptExecutorExporter.php b/ProcessMaker/ImportExport/Exporters/ScriptExecutorExporter.php index 7f19142a6f..24bb31c257 100644 --- a/ProcessMaker/ImportExport/Exporters/ScriptExecutorExporter.php +++ b/ProcessMaker/ImportExport/Exporters/ScriptExecutorExporter.php @@ -3,6 +3,7 @@ namespace ProcessMaker\ImportExport\Exporters; use Illuminate\Support\Facades\Auth; +use ProcessMaker\Enums\ScriptExecutorType; use ProcessMaker\Events\ScriptExecutorUpdated; use ProcessMaker\Jobs\BuildScriptExecutor; use ProcessMaker\Jobs\MoveScriptExecutorToMicroservice; @@ -17,6 +18,10 @@ class ScriptExecutorExporter extends ExporterBase // not exist. Otherwise associate existing on import. public static $forceUpdate = true; + // Type is managed by the target instance (Base vs Custom). Importing a + // process package must not flip a Base executor to Custom. + public static $doNotUpdateAttributes = ['type']; + public function export() : void { } @@ -36,18 +41,17 @@ public function import() : bool } break; case 'update': + // getChanges() survives refresh() after save, so description/config + // updates still reach this branch during process import. if (!empty($this->model->getChanges())) { $original = $this->model->getAttributes(); ScriptExecutorUpdated::dispatch($this->model->id, $original, $this->model->getChanges()); - if (!app()->runningInConsole()) { - $user = Auth::user(); - } else { - $user = User::where('is_administrator', 1)->first(); - } // afterCommit is needed because we are in a db transaction if (!config('script-runner-microservice.enabled')) { BuildScriptExecutor::dispatch($this->model->id, $userId)->afterCommit(); - } else { + } elseif ($this->model->type === ScriptExecutorType::Custom) { + // Base/system executors must stay on the shared language runner. + // Transitioning them here would set type=Custom and break runs. MoveScriptExecutorToMicroservice::dispatch($this->model->uuid)->afterCommit(); } } diff --git a/ProcessMaker/ImportExport/Manifest.php b/ProcessMaker/ImportExport/Manifest.php index ac5372b777..c7c28c2455 100644 --- a/ProcessMaker/ImportExport/Manifest.php +++ b/ProcessMaker/ImportExport/Manifest.php @@ -183,7 +183,7 @@ public static function getModel($uuid, $assetInfo, $mode, $exporterClass, $isRoo $class::unguard(); switch ($mode) { case 'update': - $model->fill($attrs); + $model->fill(Arr::except($attrs, $exporterClass::$doNotUpdateAttributes)); break; case 'discard': // Keep the model, just don't save it later in doImport diff --git a/tests/Feature/ImportExport/Exporters/ScriptExecutorExporterTest.php b/tests/Feature/ImportExport/Exporters/ScriptExecutorExporterTest.php new file mode 100644 index 0000000000..c971886748 --- /dev/null +++ b/tests/Feature/ImportExport/Exporters/ScriptExecutorExporterTest.php @@ -0,0 +1,212 @@ +createAdminUser(); + config(['script-runner-microservice.enabled' => true]); + } + + public function testUpdateModeDoesNotOverwriteType(): void + { + $executor = ScriptExecutor::factory()->create([ + 'title' => 'PHP Executor', + 'description' => 'local description', + 'config' => '', + 'language' => 'php', + 'type' => null, + ]); + + $assetInfo = [ + 'model' => ScriptExecutor::class, + 'attributes' => [ + 'id' => $executor->id, + 'uuid' => $executor->uuid, + 'title' => 'PHP Executor', + 'description' => 'from package', + 'language' => 'php', + 'config' => 'RUN composer require aws/aws-sdk-php:3.226.0', + 'is_system' => 0, + 'type' => ScriptExecutorType::Custom->value, + ], + ]; + + [$mode, $model] = Manifest::getModel( + $executor->uuid, + $assetInfo, + 'update', + ScriptExecutorExporter::class, + false + ); + + $this->assertEquals('update', $mode); + $this->assertNull($model->type); + $this->assertEquals('from package', $model->description); + $this->assertEquals('RUN composer require aws/aws-sdk-php:3.226.0', $model->config); + } + + public function testNewModeStillAppliesType(): void + { + $uuid = '9acb37b3-a8ed-49dc-b4de-f686ad4e286f'; + $assetInfo = [ + 'model' => ScriptExecutor::class, + 'attributes' => [ + 'uuid' => $uuid, + 'title' => 'Custom Package Executor', + 'description' => 'custom executor', + 'language' => 'php', + 'config' => 'RUN echo custom', + 'is_system' => 0, + 'type' => ScriptExecutorType::Custom->value, + ], + ]; + + // Mode "new" is assigned by the system when no matching model exists. + [$mode, $model] = Manifest::getModel( + $uuid, + $assetInfo, + 'update', + ScriptExecutorExporter::class, + false + ); + + $this->assertEquals('new', $mode); + $this->assertEquals(ScriptExecutorType::Custom, $model->type); + $this->assertEquals('Custom Package Executor', $model->title); + } + + public function testImportUpdateDoesNotPromoteBaseExecutorViaTransition(): void + { + Bus::fake([ + BuildScriptExecutor::class, + MoveScriptExecutorToMicroservice::class, + ]); + + $executor = ScriptExecutor::factory()->create([ + 'title' => 'PHP Executor', + 'description' => 'Default PHP Executor', + 'config' => '', + 'language' => 'php', + 'type' => null, + ]); + + $payload = $this->scriptExecutorImportPayload($executor, [ + 'description' => 'QA FOUR-34379 import regression marker', + 'config' => 'RUN composer require aws/aws-sdk-php:3.226.0', + 'type' => ScriptExecutorType::Custom->value, + ]); + + (new Importer($payload, new Options([])))->doImport(); + + $executor->refresh(); + $this->assertNull($executor->type); + $this->assertEquals('QA FOUR-34379 import regression marker', $executor->description); + $this->assertEquals( + 'RUN composer require aws/aws-sdk-php:3.226.0', + $executor->config + ); + Bus::assertNotDispatched(MoveScriptExecutorToMicroservice::class); + } + + public function testImportUpdateStillTransitionsCustomExecutor(): void + { + Bus::fake([ + BuildScriptExecutor::class, + MoveScriptExecutorToMicroservice::class, + ]); + + $executor = ScriptExecutor::factory()->create([ + 'title' => 'My Custom Executor', + 'description' => 'custom before', + 'config' => 'RUN echo before', + 'language' => 'php', + 'type' => ScriptExecutorType::Custom, + ]); + + $payload = $this->scriptExecutorImportPayload($executor, [ + 'description' => 'custom after', + 'config' => 'RUN echo after', + 'type' => ScriptExecutorType::Custom->value, + ]); + + (new Importer($payload, new Options([])))->doImport(); + + $executor->refresh(); + $this->assertEquals(ScriptExecutorType::Custom, $executor->type); + $this->assertEquals('custom after', $executor->description); + Bus::assertDispatched(MoveScriptExecutorToMicroservice::class, function ($job) use ($executor) { + return $this->jobUuid($job) === $executor->uuid; + }); + } + + private function scriptExecutorImportPayload(ScriptExecutor $executor, array $attributeOverrides): array + { + $attributes = array_merge([ + 'uuid' => $executor->uuid, + 'title' => $executor->title, + 'description' => $executor->description, + 'language' => $executor->language, + 'config' => $executor->config, + 'is_system' => 0, + 'type' => $executor->type?->value, + ], $attributeOverrides); + + return [ + 'type' => 'process_package', + 'root' => $executor->uuid, + 'export' => [ + $executor->uuid => [ + 'exporter' => ScriptExecutorExporter::class, + 'type' => 'ScriptExecutor', + 'type_human' => 'Script Executor', + 'type_plural' => 'ScriptExecutors', + 'type_human_plural' => 'Script Executors', + 'last_modified_by' => '', + 'last_modified_by_id' => null, + 'model' => ScriptExecutor::class, + 'force_password_protect' => false, + 'hidden' => false, + 'mode' => 'update', + 'saveAssetsMode' => 'saveAllAssets', + 'explicit_discard' => false, + 'dependents' => [], + 'name' => $attributes['title'], + 'description' => $attributes['description'], + 'process_manager' => [], + 'process_manager_id' => [], + 'attributes' => $attributes, + 'extraAttributes' => [ + 'translatedLanguages' => [], + ], + 'references' => [], + ], + ], + ]; + } + + private function jobUuid(MoveScriptExecutorToMicroservice $job): string + { + $property = (new \ReflectionClass($job))->getProperty('uuid'); + $property->setAccessible(true); + + return $property->getValue($job); + } +}