Skip to content

Commit 9972f22

Browse files
committed
fix(process-browser): handle invalid category IDs
1 parent 58a8d9b commit 9972f22

2 files changed

Lines changed: 57 additions & 2 deletions

File tree

‎ProcessMaker/Http/Controllers/Api/ProcessLaunchpadController.php‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ public function getProcesses(Request $request)
2828
$category = $request->input('category', null);
2929
if ($category === 'recent') {
3030
$processes->orderByRecentRequests();
31-
} elseif (!empty($category)) {
32-
$processes->processCategory($category);
31+
} elseif ($category !== null && $category !== '') {
32+
$categoryId = filter_var($category, FILTER_VALIDATE_INT);
33+
if ($categoryId === false) {
34+
$processes->whereRaw('1 = 0');
35+
} else {
36+
$processes->processCategory($categoryId);
37+
}
3338
}
3439
// Filter pmql
3540
$pmql = $request->input('pmql', '');

‎tests/Feature/Api/ProcessLaunchpadTest.php‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Foundation\Testing\RefreshDatabase;
66
use Illuminate\Support\Facades\Auth;
77
use ProcessMaker\Models\Process;
8+
use ProcessMaker\Models\ProcessCategory;
89
use ProcessMaker\Models\ProcessLaunchpad;
910
use ProcessMaker\Models\ProcessRequest;
1011
use Tests\Feature\Shared\RequestHelper;
@@ -16,6 +17,8 @@ class ProcessLaunchpadTest extends TestCase
1617

1718
const API_TEST_URL = '/process_launchpad';
1819

20+
const PROCESSES_API_TEST_URL = '/process_bookmarks/processes';
21+
1922
const STRUCTURE = [
2023
'launchpad',
2124
'media',
@@ -49,6 +52,53 @@ public function testGetProcessLaunchpad()
4952
$this->assertNotEmpty($response);
5053
}
5154

55+
public function testGetProcessesReturnsEmptyCollectionForNonNumericCategory()
56+
{
57+
Process::factory()->create();
58+
59+
$response = $this->apiCall('GET', self::PROCESSES_API_TEST_URL, [
60+
'category' => 'lorem',
61+
]);
62+
63+
$response->assertOk()
64+
->assertJsonCount(0, 'data')
65+
->assertJsonPath('meta.total', 0);
66+
}
67+
68+
public function testGetProcessesReturnsEmptyCollectionForMissingNumericCategory()
69+
{
70+
Process::factory()->create();
71+
72+
$response = $this->apiCall('GET', self::PROCESSES_API_TEST_URL, [
73+
'category' => '0',
74+
]);
75+
76+
$response->assertOk()
77+
->assertJsonCount(0, 'data')
78+
->assertJsonPath('meta.total', 0);
79+
}
80+
81+
public function testGetProcessesFiltersByValidCategory()
82+
{
83+
$category = ProcessCategory::factory()->create();
84+
$otherCategory = ProcessCategory::factory()->create();
85+
$process = Process::factory()->create([
86+
'process_category_id' => $category->id,
87+
]);
88+
Process::factory()->create([
89+
'process_category_id' => $otherCategory->id,
90+
]);
91+
92+
$response = $this->apiCall('GET', self::PROCESSES_API_TEST_URL, [
93+
'category' => $category->id,
94+
]);
95+
96+
$response->assertOk()
97+
->assertJsonCount(1, 'data')
98+
->assertJsonPath('data.0.id', $process->id)
99+
->assertJsonPath('meta.total', 1);
100+
}
101+
52102
/**
53103
* Test store process launchpad
54104
*/

0 commit comments

Comments
 (0)