seed(RolePermissionSeeder::class); $this->user = User::factory() ->active() ->has(Employee::factory()) ->create(); $this->ownerRole = Role::where('name', 'Owner')->first(); $this->user->roles()->attach($this->ownerRole->id); }); function mountBottleIndexComponent(User $user) { return Livewire::actingAs($user)->test(\App\Livewire\Studio\Catalog\Bottle\Index::class); } /* |-------------------------------------------------------------------------- | Index Component Rendering & Basic Functionality |-------------------------------------------------------------------------- */ it('renders bottle index page successfully', function () { mountBottleIndexComponent($this->user) ->assertViewIs('livewire.studio.catalog.bottle.index') ->assertViewHas('pageTitle', 'Botol'); }); it('displays bottles correctly', function () { Bottle::factory()->count(3)->create(); // Just check that the component renders without errors and has bottles $component = mountBottleIndexComponent($this->user); $component->assertViewIs('livewire.studio.catalog.bottle.index'); expect(Bottle::count())->toBe(3); }); /* |-------------------------------------------------------------------------- | Delete Bottle Tests |-------------------------------------------------------------------------- */ it('deletes bottle successfully when authorized', function () { $bottle = Bottle::factory()->create([ 'name' => 'Bottle to Delete', ]); mountBottleIndexComponent($this->user) ->call('delete', $bottle) ->assertHasNoErrors() ->assertDispatched('refreshDatatable'); $this->assertSoftDeleted('bottles', [ 'id' => $bottle->id, ]); }); it('prevents bottle deletion when unauthorized', function () { $this->ownerRole->revokePermissionTo('delete bottle'); $bottle = Bottle::factory()->create(); mountBottleIndexComponent($this->user) ->call('delete', $bottle) ->assertForbidden(); }); /* |-------------------------------------------------------------------------- | Authorization Edge Cases |-------------------------------------------------------------------------- */ it('handles unauthorized user with revoked permissions', function () { // Revoke all bottle permissions $this->ownerRole->revokePermissionTo(['delete bottle']); $bottle = Bottle::factory()->create(); // Test delete mountBottleIndexComponent($this->user) ->call('delete', $bottle) ->assertForbidden(); }); it('handles user with partial permissions correctly', function () { // Remove owner role and create a user with limited permissions $this->user->roles()->detach(); $limitedRole = Role::create(['name' => 'Limited User']); $this->user->roles()->attach($limitedRole->id); // Give only some permissions but not delete $limitedRole->givePermissionTo([]); $bottle = Bottle::factory()->create(); // Should not be able to delete mountBottleIndexComponent($this->user) ->call('delete', $bottle) ->assertForbidden(); });