seed(RolePermissionSeeder::class); Storage::fake((string) config('media.disk')); Queue::fake(); } protected function actingWithRole(string $role): User { $user = User::factory()->create(); $user->syncRoles([$role]); $this->withHeaders($this->apiHeadersFor($user)); return $user; } protected function stored(string $path): Media { Storage::disk((string) config('media.disk'))->put($path, 'x'); return Media::for($path); } #[Test] public function the_library_says_what_is_in_use(): void { $this->actingWithRole('editor'); $logo = $this->stored('technologies/angular.svg'); $orphan = $this->stored('pages/forgotten.jpg'); Technology::factory()->create(['logo' => $logo->path]); $this->getJson('/api/v1/media') ->assertOk() ->assertJsonCount(2, 'data') ->assertJsonPath('data.0.path', 'pages/forgotten.jpg') ->assertJsonPath('data.0.referenced', false) ->assertJsonPath('data.1.referenced', true); $this->getJson('/api/v1/media?orphans=1') ->assertJsonCount(1, 'data') ->assertJsonPath('data.0.id', $orphan->id); $this->getJson('/api/v1/media?for=technologies') ->assertJsonCount(1, 'data') ->assertJsonPath('data.0.id', $logo->id); } #[Test] public function an_image_nothing_uses_is_deleted_with_its_file(): void { $this->actingWithRole('editor'); $orphan = $this->stored('pages/forgotten.jpg'); $this->deleteJson("/api/v1/media/{$orphan->id}")->assertNoContent(); $this->assertModelMissing($orphan); Storage::disk((string) config('media.disk'))->assertMissing('pages/forgotten.jpg'); } #[Test] public function an_image_in_use_is_not_deleted(): void { $this->actingWithRole('editor'); $image = $this->stored('pages/hero.jpg'); Page::factory()->create(['featured_image' => $image->path]); $this->deleteJson("/api/v1/media/{$image->id}")->assertConflict(); $this->assertModelExists($image); Storage::disk((string) config('media.disk'))->assertExists('pages/hero.jpg'); } #[Test] public function a_writer_sees_the_library_but_deletes_nothing(): void { $this->actingWithRole('writer'); $orphan = $this->stored('pages/forgotten.jpg'); $this->getJson('/api/v1/media')->assertOk(); $this->getJson("/api/v1/media/{$orphan->id}")->assertOk()->assertJsonPath('data.referenced', false); $this->deleteJson("/api/v1/media/{$orphan->id}")->assertForbidden(); } #[Test] public function an_image_is_processed_again_on_request(): void { $this->actingWithRole('editor'); $image = $this->stored('pages/hero.jpg'); Queue::fake(); $this->postJson("/api/v1/media/{$image->id}/reprocess")->assertAccepted(); Queue::assertPushed(ProcessMedia::class); } }