From dc90b7928a52aadb8583fde3cd5bcfd0000d0d7d Mon Sep 17 00:00:00 2001 From: Paula Quispe Date: Fri, 1 Dec 2023 08:33:29 -0400 Subject: [PATCH 1/2] FOUR-12621 --- .../Controllers/Api/ProcessController.php | 5 ++++ ProcessMaker/Models/Process.php | 8 +++++++ tests/Feature/Api/ProcessTest.php | 23 +++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/ProcessMaker/Http/Controllers/Api/ProcessController.php b/ProcessMaker/Http/Controllers/Api/ProcessController.php index 6a834fef74..7560aff516 100644 --- a/ProcessMaker/Http/Controllers/Api/ProcessController.php +++ b/ProcessMaker/Http/Controllers/Api/ProcessController.php @@ -107,6 +107,11 @@ public function index(Request $request) if (!empty($filter)) { $processes->filter($filter); } + // Filter by category + $category = $request->input('category', null); + if (!empty($category)) { + $processes->category($category); + } if (!empty($pmql)) { try { diff --git a/ProcessMaker/Models/Process.php b/ProcessMaker/Models/Process.php index 9bb5270516..230b790f7f 100644 --- a/ProcessMaker/Models/Process.php +++ b/ProcessMaker/Models/Process.php @@ -450,6 +450,14 @@ public function scopeArchived($query) return $query->where('processes.status', 'ARCHIVED'); } + /** + * Scope a query to include a specific category + */ + public function scopeCategory($query, int $id) + { + return $query->where('processes.process_category_id', $id); + } + public function getCollaborations() { $this->bpmnDefinitions = app(BpmnDocumentInterface::class, ['process' => $this]); diff --git a/tests/Feature/Api/ProcessTest.php b/tests/Feature/Api/ProcessTest.php index 018605ab4e..4e58be9912 100644 --- a/tests/Feature/Api/ProcessTest.php +++ b/tests/Feature/Api/ProcessTest.php @@ -555,6 +555,29 @@ public function testSorting() ]); } + /** + * Test filter by Category + */ + public function testFilterCategory() + { + // Create Category + $categoryA = ProcessCategory::factory()->create(); + $categoryB = ProcessCategory::factory()->create(); + // Now we create process related to this + Process::factory()->count(5)->create([ + 'process_category_id' => $categoryB->id, + ]); + // Get process without category + $response = $this->apiCall('GET', route('api.processes.index', ['per_page' => 5, 'page' => 1])); + $response->assertJsonCount(5, 'data'); + // Get process without category + $response = $this->apiCall('GET', route('api.processes.index', ['category' => $categoryA->id])); + $response->assertJsonCount(0, 'data'); + // The first page should have 5 items related to the category + $response = $this->apiCall('GET', route('api.processes.index', ['category' => $categoryB->id])); + $response->assertJsonCount(5, 'data'); + } + /** * Test pagination of process list */ From 44fe7624a9a860cb3ba09bb76d85bc4f20c38efb Mon Sep 17 00:00:00 2001 From: Paula Quispe Date: Fri, 1 Dec 2023 17:30:46 -0400 Subject: [PATCH 2/2] PR observation --- ProcessMaker/Http/Controllers/Api/ProcessController.php | 2 +- ProcessMaker/Models/Process.php | 2 +- tests/Feature/Api/ProcessTest.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ProcessMaker/Http/Controllers/Api/ProcessController.php b/ProcessMaker/Http/Controllers/Api/ProcessController.php index 7560aff516..8ea9f56958 100644 --- a/ProcessMaker/Http/Controllers/Api/ProcessController.php +++ b/ProcessMaker/Http/Controllers/Api/ProcessController.php @@ -110,7 +110,7 @@ public function index(Request $request) // Filter by category $category = $request->input('category', null); if (!empty($category)) { - $processes->category($category); + $processes->processCategory($category); } if (!empty($pmql)) { diff --git a/ProcessMaker/Models/Process.php b/ProcessMaker/Models/Process.php index 230b790f7f..3d471bcccc 100644 --- a/ProcessMaker/Models/Process.php +++ b/ProcessMaker/Models/Process.php @@ -453,7 +453,7 @@ public function scopeArchived($query) /** * Scope a query to include a specific category */ - public function scopeCategory($query, int $id) + public function scopeProcessCategory($query, int $id) { return $query->where('processes.process_category_id', $id); } diff --git a/tests/Feature/Api/ProcessTest.php b/tests/Feature/Api/ProcessTest.php index 4e58be9912..18bad14bc2 100644 --- a/tests/Feature/Api/ProcessTest.php +++ b/tests/Feature/Api/ProcessTest.php @@ -570,10 +570,10 @@ public function testFilterCategory() // Get process without category $response = $this->apiCall('GET', route('api.processes.index', ['per_page' => 5, 'page' => 1])); $response->assertJsonCount(5, 'data'); - // Get process without category + // Get process related categoryA $response = $this->apiCall('GET', route('api.processes.index', ['category' => $categoryA->id])); $response->assertJsonCount(0, 'data'); - // The first page should have 5 items related to the category + // Get process related categoryB $response = $this->apiCall('GET', route('api.processes.index', ['category' => $categoryB->id])); $response->assertJsonCount(5, 'data'); }