Skip to content

Commit 427bc9c

Browse files
committed
Hide system categories using api resources
1 parent 55a7998 commit 427bc9c

7 files changed

Lines changed: 196 additions & 0 deletions

File tree

ProcessMaker/Http/Resources/ApiCollection.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ public function toArray($request)
8787
*/
8888
public function toResponse($request)
8989
{
90+
91+
$this->resource = $this->resource->reject(function($item) {
92+
$prefix = strtolower(substr(strrchr(get_class($item), '\\'), 1));
93+
$attribute = "{$prefix}_category_id";
94+
if ($item->$attribute && $item->category()->first()->is_system) {
95+
return true;
96+
} else if ($item->is_system) {
97+
return true;
98+
}
99+
return false;
100+
});
101+
90102
if ($this->resource instanceof Collection) {
91103
$this->resource = $this->collectionToPaginator($this->resource, $request);
92104
}

ProcessMaker/Models/Screen.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,12 @@ public function versions()
9696
{
9797
return $this->hasMany(ScreenVersion::class);
9898
}
99+
100+
/**
101+
* Get the associated category
102+
*/
103+
public function category()
104+
{
105+
return $this->belongsTo(ScreenCategory::class, 'screen_category_id');
106+
}
99107
}

ProcessMaker/Models/Script.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use ProcessMaker\GenerateAccessToken;
99
use ProcessMaker\Models\User;
1010
use ProcessMaker\ScriptRunners\ScriptRunner;
11+
use ProcessMaker\Models\ScriptCategory;
1112

1213
/**
1314
* Represents an Eloquent model of a Script
@@ -224,4 +225,12 @@ public static function defaultRunAsUser()
224225
# return the default admin user
225226
return User::where('is_administrator', true)->firstOrFail();
226227
}
228+
229+
/**
230+
* Get the associated category
231+
*/
232+
public function category()
233+
{
234+
return $this->belongsTo(ScriptCategory::class, 'script_category_id');
235+
}
227236
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace ProcessMaker\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Validation\Rule;
7+
use ProcessMaker\Models\Script;
8+
9+
class ScriptCategory extends Model
10+
{
11+
protected $connection = 'processmaker';
12+
13+
public function scripts()
14+
{
15+
return $this->hasMany(Script::class);
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use Faker\Generator as Faker;
4+
use ProcessMaker\Models\ScriptCategory;
5+
6+
/**
7+
* Model factory for a script category.
8+
*/
9+
$factory->define(ScriptCategory::class, function (Faker $faker) {
10+
return [
11+
'name' => $faker->unique()->sentence(),
12+
'status' => 'ACTIVE',
13+
'is_system' => false
14+
];
15+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddIsProcessToScriptAndScreenCategories extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('script_categories', function (Blueprint $table) {
17+
$table->boolean('is_system')->after('status')->default(false);
18+
});
19+
20+
Schema::table('screen_categories', function (Blueprint $table) {
21+
$table->boolean('is_system')->after('status')->default(false);
22+
});
23+
24+
Schema::table('screens', function (Blueprint $table) {
25+
$table->string('key')->nullable()->default(null);
26+
});
27+
28+
Schema::table('screen_versions', function (Blueprint $table) {
29+
$table->string('key')->nullable()->default(null);
30+
});
31+
}
32+
33+
/**
34+
* Reverse the migrations.
35+
*
36+
* @return void
37+
*/
38+
public function down()
39+
{
40+
Schema::table('', function (Blueprint $table) {
41+
//
42+
});
43+
}
44+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
namespace Tests\Model;
3+
4+
use Tests\TestCase;
5+
use ProcessMaker\Models\Process;
6+
use ProcessMaker\Models\ProcessCategory;
7+
use ProcessMaker\Models\Script;
8+
use ProcessMaker\Models\ScriptCategory;
9+
use ProcessMaker\Models\Screen;
10+
use ProcessMaker\Models\ScreenCategory;
11+
use Tests\Feature\Shared\RequestHelper;
12+
use Illuminate\Support\Str;
13+
14+
class HideSystemCategoriesTest extends TestCase
15+
{
16+
use RequestHelper;
17+
18+
private function categoryFiltered($model) {
19+
$prefix = strtolower(substr(strrchr($model, '\\'), 1));
20+
$category = factory($model . 'Category')->create([
21+
'is_system' => false,
22+
]);
23+
$hiddenCategory = factory($model . 'Category')->create([
24+
'is_system' => true,
25+
]);
26+
$response = $this->apiCall('GET', route('api.' . $prefix . '_categories.index'));
27+
$json = $response->json();
28+
$ids = array_map(function($d) { return $d['id']; }, $json['data']);
29+
30+
$this->assertCount(1, $ids);
31+
$this->assertNotContains($hiddenCategory->id, $ids);
32+
$this->assertContains($category->id, $ids);
33+
}
34+
35+
public function testCategoryFiltered() {
36+
$this->categoryFiltered(Process::class);
37+
// $this->categoryFiltered(Script::class); // No api endpoint yet for script categories
38+
$this->categoryFiltered(Screen::class);
39+
}
40+
41+
private function resourceInCategoryFiltered($model) {
42+
$prefix = strtolower(substr(strrchr($model, '\\'), 1));
43+
$category = factory($model . 'Category')->create([
44+
'is_system' => false,
45+
]);
46+
$instance = factory($model)->create([
47+
$prefix . '_category_id' => $category->id
48+
]);
49+
$hiddenCategory = factory($model . 'Category')->create([
50+
'is_system' => true,
51+
]);
52+
$hiddenInstance = factory($model)->create([
53+
$prefix . '_category_id' => $hiddenCategory->id
54+
]);
55+
56+
$response = $this->apiCall('GET', route('api.' . Str::plural($prefix) . '.index'));
57+
$json = $response->json();
58+
$ids = array_map(function($d) { return $d['id']; }, $json['data']);
59+
60+
$this->assertCount(1, $ids);
61+
$this->assertNotContains($hiddenInstance->id, $ids);
62+
$this->assertContains($instance->id, $ids);
63+
}
64+
65+
public function testResourceInCategoryFiltered() {
66+
$this->resourceInCategoryFiltered(Process::class);
67+
$this->resourceInCategoryFiltered(Script::class);
68+
$this->resourceInCategoryFiltered(Screen::class);
69+
}
70+
71+
private function resourceWithoutCategoryNotFiltered($model) {
72+
$prefix = strtolower(substr(strrchr($model, '\\'), 1));
73+
$instance = factory($model)->create([
74+
$prefix . '_category_id' => null
75+
]);
76+
77+
$response = $this->apiCall('GET', route('api.' . Str::plural($prefix) . '.index'));
78+
$json = $response->json();
79+
$ids = array_map(function($d) { return $d['id']; }, $json['data']);
80+
81+
$this->assertCount(1, $ids);
82+
$this->assertContains($instance->id, $ids);
83+
}
84+
85+
public function testResourceWithoutCategoryNotFiltered() {
86+
$this->resourceWithoutCategoryNotFiltered(Process::class);
87+
$this->resourceWithoutCategoryNotFiltered(Script::class);
88+
$this->resourceWithoutCategoryNotFiltered(Screen::class);
89+
}
90+
91+
}

0 commit comments

Comments
 (0)