@@ -195,5 +195,267 @@ public function it_can_show_others_media_to_authorized_users_on_the_index_page()
195195 ->assertSee ($ media ->name );
196196 }
197197
198+ #[Test]
199+ public function it_can_show_the_create_media_page_for_authorized_users (): void
200+ {
201+ $ this ->withoutExceptionHandling ();
202+
203+ Gate::define ('edit_media ' , function (User $ user ) {
204+ return true ;
205+ });
206+
207+ $ user = User::factory ()->create ();
208+ $ this ->actingAs ($ user );
209+
210+ $ this ->get ('/media/create ' )
211+ ->assertSuccessful ()
212+ ->assertViewIs ('mediapicker::material-admin-26.media.create ' )
213+ ->assertSee ('Add Media ' );
214+ }
215+
216+ #[Test]
217+ public function it_cannot_show_the_create_media_page_for_unauthorized_users (): void
218+ {
219+ Gate::define ('edit_media ' , function (User $ user ) {
220+ return false ;
221+ });
222+
223+ $ user = User::factory ()->create ();
224+ $ this ->actingAs ($ user );
225+
226+ $ this ->get ('/media/create ' )
227+ ->assertStatus (403 )
228+ ->assertDontSee ('Add Media ' );
229+ }
230+
231+ #[Test]
232+ public function it_can_show_media_details_for_authorized_users (): void
233+ {
234+ $ this ->withoutExceptionHandling ();
235+
236+ Gate::define ('view_media ' , function (User $ user ) {
237+ return true ;
238+ });
239+
240+ $ user = User::factory ()->create ();
241+ $ this ->actingAs ($ user );
242+
243+ $ media = $ this ->getMedia (user: $ user );
244+
245+ $ this ->get ("/media/ {$ media ->id }" )
246+ ->assertSuccessful ()
247+ ->assertViewIs ('mediapicker::material-admin-26.media.show ' )
248+ ->assertSee ($ media ->name );
249+ }
250+
251+ #[Test]
252+ public function it_cannot_show_media_details_for_unauthorized_users (): void
253+ {
254+ Gate::define ('view_media ' , function (User $ user ) {
255+ return false ;
256+ });
257+
258+ $ user = User::factory ()->create ();
259+ $ this ->actingAs ($ user );
198260
261+ $ media = $ this ->getMedia (user: $ user );
262+
263+ $ this ->get ("/media/ {$ media ->id }" )
264+ ->assertStatus (403 )
265+ ->assertDontSee ($ media ->name );
266+ }
267+
268+ #[Test]
269+ public function it_can_show_edit_media_page_for_authorized_users (): void
270+ {
271+ $ this ->withoutExceptionHandling ();
272+
273+ Gate::define ('edit_media ' , function (User $ user ) {
274+ return true ;
275+ });
276+
277+ $ user = User::factory ()->create ();
278+ $ this ->actingAs ($ user );
279+
280+ $ media = $ this ->getMedia (user: $ user );
281+
282+ $ this ->get ("/media/ {$ media ->id }/edit " )
283+ ->assertSuccessful ()
284+ ->assertViewIs ('mediapicker::material-admin-26.media.edit ' )
285+ ->assertSee ($ media ->name );
286+ }
287+
288+ #[Test]
289+ public function it_cannot_update_media_for_unauthorized_users (): void
290+ {
291+ Gate::define ('edit_media ' , function (User $ user ) {
292+ return false ;
293+ });
294+
295+ $ user = User::factory ()->create ();
296+ $ this ->actingAs ($ user );
297+
298+ $ media = $ this ->getMedia (user: $ user );
299+
300+ $ this ->put ("/media/ {$ media ->id }" , [
301+ 'name ' => 'Updated Name ' ,
302+ ])
303+ ->assertStatus (403 );
304+
305+ $ this ->assertDatabaseHas ('media ' , [
306+ 'id ' => $ media ->id ,
307+ 'name ' => 'test '
308+ ]);
309+ }
310+
311+ #[Test]
312+ public function it_can_update_media_for_authorized_users (): void
313+ {
314+ $ this ->withoutExceptionHandling ();
315+
316+ Gate::define ('edit_media ' , function (User $ user ) {
317+ return true ;
318+ });
319+
320+ $ user = User::factory ()->create ();
321+ $ this ->actingAs ($ user );
322+
323+ $ media = $ this ->getMedia (user: $ user );
324+
325+ $ this ->put ("/media/ {$ media ->id }" , [
326+ 'name ' => 'Updated Name ' ,
327+ 'description ' => 'Updated Description ' ,
328+ ])
329+ ->assertRedirect ()
330+ ->assertSessionMissing ('errors ' );
331+
332+ $ this ->assertDatabaseHas ('media ' , [
333+ 'id ' => $ media ->id ,
334+ 'name ' => 'Updated Name ' ,
335+ 'custom_properties ' => json_encode (['description ' => 'Updated Description ' ])
336+ ]);
337+ }
338+
339+ #[Test]
340+ public function it_can_update_media_and_return_json_response (): void
341+ {
342+ $ this ->withoutExceptionHandling ();
343+
344+ Gate::define ('edit_media ' , function (User $ user ) {
345+ return true ;
346+ });
347+
348+ $ user = User::factory ()->create ();
349+ $ this ->actingAs ($ user );
350+
351+ $ media = $ this ->getMedia (user: $ user );
352+
353+ $ this ->putJson ("/media/ {$ media ->id }" , [
354+ 'name ' => 'Updated Name ' ,
355+ 'description ' => 'Updated Description ' ,
356+ ])
357+ ->assertSuccessful ()
358+ ->assertJson ([
359+ 'name ' => 'Updated Name ' ,
360+ ]);
361+ }
362+
363+ #[Test]
364+ public function it_can_delete_media_for_authorized_users (): void
365+ {
366+ $ this ->withoutExceptionHandling ();
367+
368+ Gate::define ('delete_media ' , function (User $ user ) {
369+ return true ;
370+ });
371+
372+ $ user = User::factory ()->create ();
373+ $ this ->actingAs ($ user );
374+
375+ $ media = $ this ->getMedia (user: $ user );
376+
377+ $ this ->delete ("/media/ {$ media ->id }" )
378+ ->assertRedirect ();
379+
380+ $ this ->assertDatabaseMissing ('media ' , [
381+ 'id ' => $ media ->id
382+ ]);
383+ }
384+
385+ #[Test]
386+ public function it_can_delete_media_and_return_json_response (): void
387+ {
388+ $ this ->withoutExceptionHandling ();
389+
390+ Gate::define ('delete_media ' , function (User $ user ) {
391+ return true ;
392+ });
393+
394+ $ user = User::factory ()->create ();
395+ $ this ->actingAs ($ user );
396+
397+ $ media = $ this ->getMedia (user: $ user );
398+
399+ $ this ->deleteJson ("/media/ {$ media ->id }" )
400+ ->assertSuccessful ();
401+
402+ $ this ->assertDatabaseMissing ('media ' , [
403+ 'id ' => $ media ->id
404+ ]);
405+ }
406+
407+ #[Test]
408+ public function it_can_perform_bulk_delete_for_authorized_users (): void
409+ {
410+ $ this ->withoutExceptionHandling ();
411+
412+ Gate::define ('edit_media ' , function (User $ user ) {
413+ return true ;
414+ });
415+
416+ Gate::define ('delete_media ' , function (User $ user ) {
417+ return true ;
418+ });
419+
420+ $ user = User::factory ()->create ();
421+ $ this ->actingAs ($ user );
422+
423+ $ media1 = $ this ->getMedia (user: $ user );
424+ $ media2 = $ this ->getMedia ($ this ->getTestImageEndingWithUnderscore (), user: $ user );
425+
426+ $ this ->patch ('/media ' , [
427+ 'action ' => 'delete ' ,
428+ 'media ' => [$ media1 ->id , $ media2 ->id ],
429+ ])
430+ ->assertRedirect ()
431+ ->assertSessionMissing ('errors ' );
432+
433+ $ this ->assertDatabaseMissing ('media ' , ['id ' => $ media1 ->id ]);
434+ $ this ->assertDatabaseMissing ('media ' , ['id ' => $ media2 ->id ]);
435+ }
436+
437+ #[Test]
438+ public function it_cannot_perform_bulk_delete_for_unauthorized_users (): void
439+ {
440+ Gate::define ('edit_media ' , function (User $ user ) {
441+ return true ;
442+ });
443+
444+ Gate::define ('delete_media ' , function (User $ user ) {
445+ return false ;
446+ });
447+
448+ $ user = User::factory ()->create ();
449+ $ this ->actingAs ($ user );
450+
451+ $ media = $ this ->getMedia (user: $ user );
452+
453+ $ this ->patch ('/media ' , [
454+ 'action ' => 'delete ' ,
455+ 'media ' => [$ media ->id ],
456+ ])
457+ ->assertStatus (403 );
458+
459+ $ this ->assertDatabaseHas ('media ' , ['id ' => $ media ->id ]);
460+ }
199461}
0 commit comments