Skip to content

Commit cca7600

Browse files
authored
Merge pull request #15852 from marcusmoore/testing/ui-delete-component
Added tests around deleting component via ui
2 parents 5a5f108 + 54f5f46 commit cca7600

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

app/Http/Controllers/Components/ComponentsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public function destroy($componentId)
193193
$this->authorize('delete', $component);
194194

195195
// Remove the image if one exists
196-
if (Storage::disk('public')->exists('components/'.$component->image)) {
196+
if ($component->image && Storage::disk('public')->exists('components/' . $component->image)) {
197197
try {
198198
Storage::disk('public')->delete('components/'.$component->image);
199199
} catch (\Exception $e) {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)