Skip to content

Commit 26adb54

Browse files
committed
coverage test
Signed-off-by: Arushad Ahmed <dash-8x@hotmail.com>
1 parent e39f033 commit 26adb54

7 files changed

Lines changed: 269 additions & 6 deletions

File tree

resources/views/material-admin-26/media/create.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
@section('page-title', __('Add Media'))
44

55
@section('content')
6-
@include('mediapicker::material-admin-26.media._uploader', ['view' => 'list'])
6+
@include('mediapicker::material-admin-26.media.form._uploader', ['view' => 'list'])
77
@endsection

resources/views/material-admin-26/media/form/_general.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<x-forms::card>
22

3-
<x-forms::text-entry name="url">
3+
<x-forms::text-entry :label="__('URL')">
44
<a href="{{ $media->getUrl() }}" target="_blank">
55
<i class="zmdi zmdi-open-in-new mr-2"></i> {{ $media->getUrl() }}
66
</a>

resources/views/material-admin-26/media/form/_upload-script.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
uploadMultiple: false,
1515
parallelUploads: 50,
1616
timeout: 600000,
17-
acceptedFiles: '{{ AllowedMimeTypes::getAllowedMimeTypesString($type ?? '') }}',
17+
acceptedFiles: '{{ \Javaabu\Helpers\Media\AllowedMimeTypes::getAllowedMimeTypesString($type ?? '') }}',
1818
error: function (file, response) {
1919
var message = '';
2020

resources/views/material-admin-26/media/form/_uploader.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<x-forms::card :title="__('Upload Media')"">
1+
<x-forms::card :title="__('Upload Media')">
22
@if(! empty($type))
33
<div id="append-data">
44
<x-forms::hidden name="type" :value="$type" />
@@ -36,5 +36,5 @@
3636

3737

3838
@push(config('mediapicker.scripts_stack'))
39-
@include('mediapicker::material-admin-26.media.form._script')
39+
@include('mediapicker::material-admin-26.media.form._upload-script')
4040
@endpush

src/Http/Controllers/MediaController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public function bulk(Request $request)
261261
];
262262

263263
if (! $user->canDeleteOthersMedia()) {
264-
$rules['media.*'] .= ',model_id,' . $user->getKeyName();
264+
$rules['media.*'] .= ',model_id,' . $user->getKey();
265265
}
266266

267267
$this->validate($request, $rules);

src/Models/Media.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Media extends BaseMedia implements AdminModel
4141
*/
4242
protected $fillable = [
4343
'name',
44+
'description',
4445
];
4546

4647
protected function setDescriptionAttribute(?string $value = null)

tests/Feature/Controllers/MediaControllerTest.php

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)