66use Lomkit \Rest \Http \Requests \RestRequest ;
77use Lomkit \Rest \Tests \Feature \TestCase ;
88use Lomkit \Rest \Tests \Support \Database \Factories \BelongsToManyRelationFactory ;
9+ use Lomkit \Rest \Tests \Support \Database \Factories \HasManyRelationFactory ;
910use Lomkit \Rest \Tests \Support \Database \Factories \ModelFactory ;
1011use Lomkit \Rest \Tests \Support \Models \BelongsToManyRelation ;
12+ use Lomkit \Rest \Tests \Support \Models \HasManyRelation ;
1113use Lomkit \Rest \Tests \Support \Models \Model ;
1214use Lomkit \Rest \Tests \Support \Policies \CreatePolicy ;
1315use Lomkit \Rest \Tests \Support \Policies \DeletePolicy ;
@@ -32,7 +34,7 @@ public function test_searching_automatic_gated_resource(): void
3234 $ response = $ this ->post (
3335 '/api/automatic-gating/search ' ,
3436 [
35-
37+ ' gates ' => [ ' create ' , ' view ' , ' update ' , ' delete ' , ' forceDelete ' , ' restore ' ],
3638 ],
3739 ['Accept ' => 'application/json ' ]
3840 );
@@ -92,7 +94,7 @@ public function test_searching_automatic_gated_resource_with_create_policy(): vo
9294 $ response = $ this ->post (
9395 '/api/automatic-gating/search ' ,
9496 [
95-
97+ ' gates ' => [ ' create ' , ' view ' ],
9698 ],
9799 ['Accept ' => 'application/json ' ]
98100 );
@@ -105,10 +107,6 @@ public function test_searching_automatic_gated_resource_with_create_policy(): vo
105107 [
106108 'gates ' => [
107109 'authorized_to_view ' => false ,
108- 'authorized_to_update ' => false ,
109- 'authorized_to_delete ' => false ,
110- 'authorized_to_restore ' => false ,
111- 'authorized_to_force_delete ' => false ,
112110 ],
113111 ],
114112 ]
@@ -128,7 +126,7 @@ public function test_searching_automatic_gated_resource_with_view_policy(): void
128126 $ response = $ this ->post (
129127 '/api/automatic-gating/search ' ,
130128 [
131-
129+ ' gates ' => [ ' view ' , ' create ' ],
132130 ],
133131 ['Accept ' => 'application/json ' ]
134132 );
@@ -141,10 +139,6 @@ public function test_searching_automatic_gated_resource_with_view_policy(): void
141139 [
142140 'gates ' => [
143141 'authorized_to_view ' => true ,
144- 'authorized_to_update ' => false ,
145- 'authorized_to_delete ' => false ,
146- 'authorized_to_restore ' => false ,
147- 'authorized_to_force_delete ' => false ,
148142 ],
149143 ],
150144 ]
@@ -164,7 +158,7 @@ public function test_searching_automatic_gated_resource_with_update_policy(): vo
164158 $ response = $ this ->post (
165159 '/api/automatic-gating/search ' ,
166160 [
167-
161+ ' gates ' => [ ' update ' ],
168162 ],
169163 ['Accept ' => 'application/json ' ]
170164 );
@@ -176,17 +170,13 @@ public function test_searching_automatic_gated_resource_with_update_policy(): vo
176170 [
177171 [
178172 'gates ' => [
179- 'authorized_to_view ' => false ,
180173 'authorized_to_update ' => true ,
181- 'authorized_to_delete ' => false ,
182- 'authorized_to_restore ' => false ,
183- 'authorized_to_force_delete ' => false ,
184174 ],
185175 ],
186176 ]
187177 );
188178 $ response ->assertJson (
189- ['meta ' => [' gates ' => [ ' authorized_to_create ' => false ] ]]
179+ ['meta ' => []]
190180 );
191181 }
192182
@@ -200,7 +190,7 @@ public function test_searching_automatic_gated_resource_with_delete_policy(): vo
200190 $ response = $ this ->post (
201191 '/api/automatic-gating/search ' ,
202192 [
203-
193+ ' gates ' => [ ' create ' , ' delete ' ],
204194 ],
205195 ['Accept ' => 'application/json ' ]
206196 );
@@ -212,11 +202,7 @@ public function test_searching_automatic_gated_resource_with_delete_policy(): vo
212202 [
213203 [
214204 'gates ' => [
215- 'authorized_to_view ' => false ,
216- 'authorized_to_update ' => false ,
217205 'authorized_to_delete ' => true ,
218- 'authorized_to_restore ' => false ,
219- 'authorized_to_force_delete ' => false ,
220206 ],
221207 ],
222208 ]
@@ -236,7 +222,7 @@ public function test_searching_automatic_gated_resource_with_restore_policy(): v
236222 $ response = $ this ->post (
237223 '/api/automatic-gating/search ' ,
238224 [
239-
225+ ' gates ' => [ ' restore ' , ' view ' ],
240226 ],
241227 ['Accept ' => 'application/json ' ]
242228 );
@@ -249,16 +235,13 @@ public function test_searching_automatic_gated_resource_with_restore_policy(): v
249235 [
250236 'gates ' => [
251237 'authorized_to_view ' => false ,
252- 'authorized_to_update ' => false ,
253- 'authorized_to_delete ' => false ,
254238 'authorized_to_restore ' => true ,
255- 'authorized_to_force_delete ' => false ,
256239 ],
257240 ],
258241 ]
259242 );
260243 $ response ->assertJson (
261- ['meta ' => [' gates ' => [ ' authorized_to_create ' => false ] ]]
244+ ['meta ' => []]
262245 );
263246 }
264247
@@ -272,7 +255,7 @@ public function test_searching_automatic_gated_resource_with_force_delete_policy
272255 $ response = $ this ->post (
273256 '/api/automatic-gating/search ' ,
274257 [
275-
258+ ' gates ' => [ ' forceDelete ' , ' create ' ],
276259 ],
277260 ['Accept ' => 'application/json ' ]
278261 );
@@ -284,10 +267,6 @@ public function test_searching_automatic_gated_resource_with_force_delete_policy
284267 [
285268 [
286269 'gates ' => [
287- 'authorized_to_view ' => false ,
288- 'authorized_to_update ' => false ,
289- 'authorized_to_delete ' => false ,
290- 'authorized_to_restore ' => false ,
291270 'authorized_to_force_delete ' => true ,
292271 ],
293272 ],
@@ -316,11 +295,15 @@ public function test_searching_automatic_gated_resource_with_belongs_to_many_rel
316295 'includes ' => [
317296 [
318297 'relation ' => 'belongsToManyRelation ' ,
298+ 'gates ' => ['view ' ],
319299 ],
320300 ],
321301 'sorts ' => [
322302 ['field ' => 'id ' , 'direction ' => 'asc ' ],
323303 ],
304+ 'gates ' => [
305+ 'view ' , 'update ' , 'create ' , 'delete ' , 'restore ' , 'forceDelete ' ,
306+ ],
324307 ],
325308 ['Accept ' => 'application/json ' ]
326309 );
@@ -368,8 +351,44 @@ public function test_searching_automatic_gated_resource_with_belongs_to_many_rel
368351 ],
369352 ]
370353 );
354+ $ this ->assertArrayNotHasKey (
355+ 'gates ' ,
356+ $ response ->json ('data.0.belongs_to_many_relation.0 ' )
357+ );
371358 $ response ->assertJson (
372359 ['meta ' => ['gates ' => ['authorized_to_create ' => true ]]]
373360 );
374361 }
362+
363+ public function test_searching_automatic_gated_resource_with_not_requested_includes_gates (): void
364+ {
365+ ModelFactory::new ()
366+ ->count (10 )
367+ ->has (
368+ HasManyRelationFactory::new ()
369+ ->count (10 )
370+ )
371+ ->create ();
372+
373+ Gate::policy (Model::class, GreenPolicy::class);
374+ Gate::policy (HasManyRelation::class, GreenPolicy::class);
375+
376+ $ response = $ this ->post (
377+ '/api/automatic-gating/search ' ,
378+ [
379+ 'includes ' => [
380+ [
381+ 'relation ' => 'hasManyRelation ' ,
382+ ],
383+ ],
384+ 'gates ' => ['view ' , 'create ' , 'update ' , 'delete ' , 'restore ' , 'forceDelete ' ],
385+ ],
386+ ['Accept ' => 'application/json ' ]
387+ );
388+
389+ $ this ->assertArrayNotHasKey (
390+ 'gates ' ,
391+ $ response ->json ('data.0.has_many_relation.0 ' )
392+ );
393+ }
375394}
0 commit comments