|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Components\Ui; |
| 4 | + |
| 5 | +use App\Models\Company; |
| 6 | +use App\Models\Component; |
| 7 | +use App\Models\User; |
| 8 | +use Illuminate\Support\Facades\Storage; |
| 9 | +use Tests\Concerns\TestsFullMultipleCompaniesSupport; |
| 10 | +use Tests\Concerns\TestsPermissionsRequirement; |
| 11 | +use Tests\TestCase; |
| 12 | + |
| 13 | +class DeleteComponentTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement |
| 14 | +{ |
| 15 | + public function testRequiresPermission() |
| 16 | + { |
| 17 | + $component = Component::factory()->create(); |
| 18 | + |
| 19 | + $this->actingAs(User::factory()->create()) |
| 20 | + ->delete(route('components.destroy', $component->id)) |
| 21 | + ->assertForbidden(); |
| 22 | + } |
| 23 | + |
| 24 | + public function testHandlesNonExistentComponent() |
| 25 | + { |
| 26 | + $this->actingAs(User::factory()->deleteComponents()->create()) |
| 27 | + ->delete(route('components.destroy', 10000)) |
| 28 | + ->assertSessionHas('error'); |
| 29 | + } |
| 30 | + |
| 31 | + public function testCanDeleteComponent() |
| 32 | + { |
| 33 | + $component = Component::factory()->create(); |
| 34 | + |
| 35 | + $this->actingAs(User::factory()->deleteComponents()->create()) |
| 36 | + ->delete(route('components.destroy', $component->id)) |
| 37 | + ->assertSessionHas('success') |
| 38 | + ->assertRedirect(route('components.index')); |
| 39 | + |
| 40 | + $this->assertSoftDeleted($component); |
| 41 | + } |
| 42 | + |
| 43 | + public function testDeletingComponentRemovesComponentImage() |
| 44 | + { |
| 45 | + Storage::fake('public'); |
| 46 | + |
| 47 | + $component = Component::factory()->create(['image' => 'component-image.jpg']); |
| 48 | + |
| 49 | + Storage::disk('public')->put('components/component-image.jpg', 'content'); |
| 50 | + |
| 51 | + Storage::disk('public')->assertExists('components/component-image.jpg'); |
| 52 | + |
| 53 | + $this->actingAs(User::factory()->deleteComponents()->create())->delete(route('components.destroy', $component->id)); |
| 54 | + |
| 55 | + Storage::disk('public')->assertMissing('components/component-image.jpg'); |
| 56 | + } |
| 57 | + |
| 58 | + public function testDeletingComponentIsLogged() |
| 59 | + { |
| 60 | + $user = User::factory()->deleteComponents()->create(); |
| 61 | + $component = Component::factory()->create(); |
| 62 | + |
| 63 | + $this->actingAs($user)->delete(route('components.destroy', $component->id)); |
| 64 | + |
| 65 | + $this->assertDatabaseHas('action_logs', [ |
| 66 | + 'created_by' => $user->id, |
| 67 | + 'action_type' => 'delete', |
| 68 | + 'item_type' => Component::class, |
| 69 | + 'item_id' => $component->id, |
| 70 | + ]); |
| 71 | + } |
| 72 | + |
| 73 | + public function testAdheresToFullMultipleCompaniesSupportScoping() |
| 74 | + { |
| 75 | + $this->settings->enableMultipleFullCompanySupport(); |
| 76 | + |
| 77 | + [$companyA, $companyB] = Company::factory()->count(2)->create(); |
| 78 | + |
| 79 | + $userInCompanyA = User::factory()->for($companyA)->create(); |
| 80 | + $componentForCompanyB = Component::factory()->for($companyB)->create(); |
| 81 | + |
| 82 | + $this->actingAs($userInCompanyA) |
| 83 | + ->delete(route('components.destroy', $componentForCompanyB->id)) |
| 84 | + ->assertSessionHas('error'); |
| 85 | + |
| 86 | + $this->assertNotSoftDeleted($componentForCompanyB); |
| 87 | + } |
| 88 | +} |
0 commit comments