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