Skip to content
Open
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
7 changes: 7 additions & 0 deletions ProcessMaker/ImportExport/Exporters/ExporterBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, string>
*/
public static $doNotUpdateAttributes = [];

// public $ignoreExplicitDiscard = false;

public static $fallbackMatchColumn = null;
Expand Down
16 changes: 10 additions & 6 deletions ProcessMaker/ImportExport/Exporters/ScriptExecutorExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'];
Comment thread
nolanpro marked this conversation as resolved.
Resolved
Comment thread
eiresendez marked this conversation as resolved.
Resolved

public function export() : void
{
}
Expand All @@ -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();
}
}
Expand Down
2 changes: 1 addition & 1 deletion ProcessMaker/ImportExport/Manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Comment thread
nolanpro marked this conversation as resolved.
Resolved
Comment thread
nolanpro marked this conversation as resolved.
Resolved
break;
case 'discard':
// Keep the model, just don't save it later in doImport
Expand Down
212 changes: 212 additions & 0 deletions tests/Feature/ImportExport/Exporters/ScriptExecutorExporterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
<?php

namespace Tests\Feature\ImportExport\Exporters;

use Illuminate\Support\Facades\Bus;
use ProcessMaker\Enums\ScriptExecutorType;
use ProcessMaker\ImportExport\Exporters\ScriptExecutorExporter;
use ProcessMaker\ImportExport\Importer;
use ProcessMaker\ImportExport\Manifest;
use ProcessMaker\ImportExport\Options;
use ProcessMaker\Jobs\BuildScriptExecutor;
use ProcessMaker\Jobs\MoveScriptExecutorToMicroservice;
use ProcessMaker\Models\ScriptExecutor;
use Tests\Feature\ImportExport\HelperTrait;
use Tests\TestCase;

class ScriptExecutorExporterTest extends TestCase
{
use HelperTrait;

protected function setUp(): void
{
parent::setUp();
$this->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);
}
}
Loading