Skip to content

FOUR-21522 Fix Get variable list without VarFinder package was not implemented #7912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 17, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use ProcessMaker\Managers\ExportManager;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\Screen;
use ProcessMaker\Package\SavedSearch\Models\SavedSearch;
use ProcessMaker\Package\VariableFinder\Models\ProcessVariable;

Expand Down Expand Up @@ -202,7 +205,7 @@ public function getProcessesVariables(array $processIds, $excludeSavedSearch, $p
{
// If the classes or tables do not exist, fallback to a saved search approach.
if (!class_exists(ProcessVariable::class) || !Schema::hasTable('process_variables')) {
return $this->getProcessesVariablesFromSavedSearch($processIds);
return $this->getProcessesVariablesFrom($processIds);
}

// Determine which columns to exclude based on the saved search
Expand All @@ -219,7 +222,8 @@ public function getProcessesVariables(array $processIds, $excludeSavedSearch, $p
$query = DB::table('var_finder_variables AS vfv')
->join('asset_variables AS av', 'vfv.asset_variable_id', '=', 'av.id')
->join('process_variables AS pv', 'av.id', '=', 'pv.asset_variable_id')
->whereIn('pv.process_id', $processIds);
->whereIn('pv.process_id', $processIds)
->orderBy('vfv.id');

if (!empty($activeColumns)) {
$query->whereNotIn('vfv.field', $activeColumns);
Expand All @@ -243,8 +247,56 @@ public function getProcessesVariables(array $processIds, $excludeSavedSearch, $p
*
* @return void
*/
public static function mock()
public static function mock(bool $value = true)
{
static::$mockData = true;
static::$mockData = $value;
}

/**
* Retrieve process variables from its screens.
*
* @param array $processIds
*
* @return LengthAwarePaginator
*/
private function getProcessesVariablesFrom(array $processIds)
{
$perPage = request()->get('per_page', 20);
// Validate processIds input is required
if (empty($processIds)) {
return new LengthAwarePaginator([], 0, $perPage, 1);
}

// Get screens used in the processes
$processes = Process::whereIn('id', $processIds)->get();
$ids = collect([]);
foreach ($processes as $process) {
$manager = app(ExportManager::class);
try {
$ids = $ids->merge($manager->getDependenciesOfType(Screen::class, $process));
} catch (\Exception $e) {
$ids = collect([]);
}
}

// Get columns from screens
$columns = collect([]);
$screens = Screen::whereIn('id', $ids->unique())->where('type', '!=', 'DISPLAY')->get();
foreach ($screens as $screen) {
$screenColumns = $screen->fields->map(function ($item) {
$item->field = "data.{$item->field}";

return $item;
});

$columns = $columns->merge($screenColumns);
}

// Paginate the result
$page = request()->get('page', 1);
$total = $columns->count();
$items = $columns->forPage($page, $perPage);

return new LengthAwarePaginator($items, $total, $perPage, $page);
}
}
90 changes: 90 additions & 0 deletions tests/Feature/Api/V1_1/ProcessVariableControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,58 @@ public function test_can_get_process_variables_with_pagination(): void
$this->assertEquals(30, $responseData['meta']['total']);
}

/**
* Test successful variables retrieval with pagination
*/
public function test_can_get_process_variables_from_process_screens_with_pagination(): void
{
$bpmn = file_get_contents(base_path('tests/Feature/Api/bpmnPatterns/SimpleTaskProcess.bpmn'));
ProcessVariableController::mock(false);
$screen1 = $this->createScreenWithFields(1, 10);
$screen2 = $this->createScreenWithFields(2, 10);
$screen3 = $this->createScreenWithFields(3, 10);
Process::factory()->create(['id' => 1, 'bpmn' => str_replace('pm:screenRef="2"', 'pm:screenRef="' . $screen1->id . '"', $bpmn)]);
Process::factory()->create(['id' => 2, 'bpmn' => str_replace('pm:screenRef="2"', 'pm:screenRef="' . $screen2->id . '"', $bpmn)]);
Process::factory()->create(['id' => 3, 'bpmn' => str_replace('pm:screenRef="2"', 'pm:screenRef="' . $screen3->id . '"', $bpmn)]);

// Make request to the endpoint
$response = $this->apiCall('GET', '/api/1.1/processes/variables?processIds=1,2,3&page=1&per_page=15');

// Assert response structure and status
$response->assertStatus(200)
->assertJsonStructure([
'data' => [
'*' => [
//'id',
//'process_id',
'format',
'label',
'field',
'default',
//'created_at',
//'updated_at',
]
],
'meta' => [
'current_page',
'from',
'last_page',
'path',
'per_page',
'to',
'total',
]
]);

// Assert pagination works correctly
$responseData = $response->json();
$this->assertEquals(15, $responseData['meta']['per_page']);
$this->assertEquals(1, $responseData['meta']['current_page']);

// Since we're generating 10 variables per process (3 processes = 30 total)
$this->assertEquals(30, $responseData['meta']['total']);
}

private function mockVariableFinder(array $processIds, $excludeSavedSearch)
{
// Create a cache key based on process IDs
Expand Down Expand Up @@ -281,4 +333,42 @@ public function test_saved_search_id_filtering(): void
// Check that the total count is reduced by the number of excluded fields
$this->assertEquals(8, $responseData['meta']['total']); // 10 total - 2 excluded
}

/**
* Create a screen with a given number of fields
*
* @param int $processId
* @param int $fieldsCount
*
* @return Screen
*/
private function createScreenWithFields(int $processId, int $fieldsCount)
{
$items = [];
for ($i = 1; $i <= $fieldsCount; $i++) {
$items[] = [
'component' => 'FormInput',
'config' => [
'name' => "var_{$processId}_{$i}",
'type' => 'text',
'label' => "Variable {$i} for Process {$processId}",
'helper' => null,
'dataFormat' => 'string',
'validation' => null,
'placeholder' => null,
],
];
}

return Screen::factory()->create([
'config' => [
[
[
'name' => 'screen name',
'items' => $items,
],
]
],
]);
}
}
Loading