Skip to content

Commit e39f033

Browse files
committed
- Added tests for the media controller
Signed-off-by: Arushad Ahmed <[email protected]>
1 parent b87e3f4 commit e39f033

File tree

3 files changed

+116
-7
lines changed

3 files changed

+116
-7
lines changed

tests/Feature/Controllers/MediaControllerTest.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,85 @@ public function it_can_upload_a_file_to_the_media_library_and_return_json(): voi
115115
]);
116116
}
117117

118+
#[Test]
119+
public function it_does_not_allow_viewing_media_index_page_for_unauthorized_users(): void
120+
{
121+
Gate::define('view_media', function (User $user) {
122+
return false;
123+
});
124+
125+
$user = User::factory()->create();
126+
127+
$this->actingAs($user);
128+
129+
$media = $this->getMedia(user: $user);
130+
131+
$this->get('/media')
132+
->assertStatus(403)
133+
->assertDontSee($media->name);
134+
}
135+
136+
#[Test]
137+
public function it_does_not_show_other_users_media_to_unauthorized_users_on_the_index_page(): void
138+
{
139+
$this->withoutExceptionHandling();
140+
141+
Gate::define('view_media', function (User $user) {
142+
return true;
143+
});
144+
145+
Gate::define('view_others_media', function (User $user) {
146+
return false;
147+
});
148+
149+
$user = User::factory()->create();
150+
151+
$this->actingAs($user);
152+
153+
$media = $this->getMedia(user: $user);
154+
155+
$other_user = User::factory()->create();
156+
157+
158+
$other_file = $this->getTestImageEndingWithUnderscore();
159+
$other_media = $this->getMedia($other_file, user: $other_user);
160+
161+
$this->get('/media')
162+
->assertSuccessful()
163+
->assertDontSee($other_media->name)
164+
->assertSee($media->name);
165+
}
166+
167+
#[Test]
168+
public function it_can_show_others_media_to_authorized_users_on_the_index_page(): void
169+
{
170+
$this->withoutExceptionHandling();
171+
172+
Gate::define('view_media', function (User $user) {
173+
return true;
174+
});
175+
176+
Gate::define('view_others_media', function (User $user) {
177+
return true;
178+
});
179+
180+
$user = User::factory()->create();
181+
182+
$this->actingAs($user);
183+
184+
$media = $this->getMedia(user: $user);
185+
186+
$other_user = User::factory()->create();
187+
188+
189+
$other_file = $this->getTestImageEndingWithUnderscore();
190+
$other_media = $this->getMedia($other_file, user: $other_user);
191+
192+
$this->get('/media')
193+
->assertSuccessful()
194+
->assertSee($other_media->name)
195+
->assertSee($media->name);
196+
}
197+
118198

119199
}

tests/TestCase.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\Facades\Artisan;
88
use Illuminate\Support\Facades\File;
99
use Illuminate\Support\Facades\View;
10+
use Javaabu\Forms\FormsServiceProvider;
1011
use Javaabu\Helpers\HelpersServiceProvider;
1112
use Javaabu\Mediapicker\Models\Attachment;
1213
use Javaabu\Mediapicker\Tests\TestSupport\Models\ModelWithConversions;
@@ -61,6 +62,7 @@ protected function getPackageProviders($app)
6162
TestServiceProvider::class,
6263
HelpersServiceProvider::class,
6364
SettingsServiceProvider::class,
65+
FormsServiceProvider::class,
6466
ActivitylogServiceProvider::class,
6567
];
6668
}
@@ -96,16 +98,18 @@ protected function getImageDimensions(string $path): array
9698
return compact('width', 'height');
9799
}
98100

99-
protected function getUserWithMedia(string $file = '', string $name = ''): User
101+
protected function getUserWithMedia(string $file = '', string $name = '', ?User $user = null): User
100102
{
101103
if (! $file) {
102104
$file = $this->getTestJpg();
103105
}
104106

105-
/** @var User $user */
106-
$user = User::factory()->create([
107-
'name' => $name ?: fake()->name,
108-
]);
107+
if (! $user) {
108+
/** @var User $user */
109+
$user = User::factory()->create([
110+
'name' => $name ?: fake()->name,
111+
]);
112+
}
109113

110114
$user->addMedia($file)
111115
->preservingOriginal()
@@ -114,13 +118,13 @@ protected function getUserWithMedia(string $file = '', string $name = ''): User
114118
return $user;
115119
}
116120

117-
protected function getMedia(string $file = ''): Media
121+
protected function getMedia(string $file = '', ?User $user = null): Media
118122
{
119123
if (! $file) {
120124
$file = $this->getTestJpg();
121125
}
122126

123-
$user = $this->getUserWithMedia($file);
127+
$user = $this->getUserWithMedia($file, user: $user);
124128

125129
return $user->getFirstMedia('mediapicker');
126130
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!doctype html>
2+
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
3+
@section('title', 'Admin')
4+
5+
<body>
6+
<main class="main">
7+
8+
9+
<!-- Contents -->
10+
<section class="content">
11+
<header class="content__title">
12+
<h1>@yield('page-title')</h1>
13+
@yield('page-subheading')
14+
{{--<small>Sub heading contents</small>--}}
15+
@yield('model-actions')
16+
</header>
17+
18+
@section('content')
19+
@show
20+
</section>
21+
</main>
22+
23+
@stack('scripts')
24+
</body>
25+
</html>

0 commit comments

Comments
 (0)