141 lines
5.4 KiB
PHP
141 lines
5.4 KiB
PHP
<?php
|
|
|
|
use App\Models\Advance;
|
|
use App\Models\AdvanceType;
|
|
use App\Models\User;
|
|
use Spatie\Permission\Models\Permission;
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
beforeEach(function () {
|
|
Permission::firstOrCreate(['name' => 'zaliczki_access']);
|
|
});
|
|
|
|
function userWithAdvanceAccess(string $roleName = 'pracownik'): User
|
|
{
|
|
$user = User::factory()->create();
|
|
$role = Role::firstOrCreate(['name' => $roleName]);
|
|
$role->givePermissionTo('zaliczki_access');
|
|
$user->assignRole($role);
|
|
|
|
return $user;
|
|
}
|
|
|
|
test('employee sees only own advances', function () {
|
|
$employee = userWithAdvanceAccess();
|
|
$otherUser = User::factory()->create();
|
|
$type = AdvanceType::create(['name' => 'Paliwo']);
|
|
|
|
Advance::create(['date' => '2026-08-01', 'user_id' => $employee->id, 'amount' => 100, 'advance_type_id' => $type->id]);
|
|
Advance::create(['date' => '2026-08-02', 'user_id' => $otherUser->id, 'amount' => 200, 'advance_type_id' => $type->id]);
|
|
|
|
$response = $this->actingAs($employee)->get(route('zaliczki'));
|
|
|
|
$response->assertOk();
|
|
$response->assertSee('100,00');
|
|
$response->assertDontSee('200,00');
|
|
});
|
|
|
|
test('advance is stored for authenticated user', function () {
|
|
$employee = userWithAdvanceAccess();
|
|
$otherUser = User::factory()->create();
|
|
$type = AdvanceType::create(['name' => 'Hotel']);
|
|
|
|
$response = $this->actingAs($employee)->post(route('zaliczki.store'), [
|
|
'date' => '2026-08-28',
|
|
'user_id' => $otherUser->id,
|
|
'amount' => '250.50',
|
|
'advance_type_id' => $type->id,
|
|
]);
|
|
|
|
$response->assertRedirect(route('zaliczki'));
|
|
$this->assertDatabaseHas('advances', [
|
|
'user_id' => $employee->id,
|
|
'advance_type_id' => $type->id,
|
|
'amount' => -250.50,
|
|
]);
|
|
});
|
|
|
|
test('manager can create an advance for another user', function () {
|
|
$manager = userWithAdvanceAccess('kierownik');
|
|
$employee = User::factory()->create();
|
|
$type = AdvanceType::create(['name' => 'Wpłata']);
|
|
|
|
$response = $this->actingAs($manager)->post(route('zaliczki.store'), [
|
|
'date' => '2026-08-28',
|
|
'user_id' => $employee->id,
|
|
'amount' => '300.00',
|
|
'advance_type_id' => $type->id,
|
|
]);
|
|
|
|
$response->assertRedirect(route('zaliczki'));
|
|
$this->assertDatabaseHas('advances', [
|
|
'user_id' => $employee->id,
|
|
'advance_type_id' => $type->id,
|
|
'amount' => 300.00,
|
|
]);
|
|
});
|
|
|
|
test('employee can edit own advance but cannot access another users advance', function () {
|
|
$employee = userWithAdvanceAccess();
|
|
$otherUser = User::factory()->create();
|
|
$type = AdvanceType::create(['name' => 'Hotel']);
|
|
$advance = Advance::create(['date' => '2026-08-28', 'user_id' => $employee->id, 'amount' => 100, 'advance_type_id' => $type->id]);
|
|
$otherAdvance = Advance::create(['date' => '2026-08-28', 'user_id' => $otherUser->id, 'amount' => 200, 'advance_type_id' => $type->id]);
|
|
|
|
$this->actingAs($employee)->get(route('zaliczki.edit', $otherAdvance))->assertForbidden();
|
|
|
|
$response = $this->actingAs($employee)->put(route('zaliczki.update', $advance), [
|
|
'date' => '2026-08-27',
|
|
'amount' => '150.00',
|
|
'advance_type_id' => $type->id,
|
|
]);
|
|
|
|
$response->assertRedirect(route('zaliczki'));
|
|
$this->assertDatabaseHas('advances', ['id' => $advance->id, 'amount' => -150, 'date' => '2026-08-27 00:00:00']);
|
|
});
|
|
|
|
test('manager can delete an advance', function () {
|
|
$manager = userWithAdvanceAccess('kierownik');
|
|
$employee = User::factory()->create();
|
|
$type = AdvanceType::create(['name' => 'Paliwo']);
|
|
$advance = Advance::create(['date' => '2026-08-28', 'user_id' => $employee->id, 'amount' => 100, 'advance_type_id' => $type->id]);
|
|
|
|
$response = $this->actingAs($manager)->delete(route('zaliczki.destroy', $advance));
|
|
|
|
$response->assertRedirect(route('zaliczki'));
|
|
$this->assertDatabaseMissing('advances', ['id' => $advance->id]);
|
|
});
|
|
|
|
test('expenses are negative and deposits are positive regardless of input sign', function () {
|
|
$employee = userWithAdvanceAccess();
|
|
$fuel = AdvanceType::create(['name' => 'Paliwo']);
|
|
$deposit = AdvanceType::create(['name' => 'Wpłata']);
|
|
|
|
Advance::create(['date' => '2026-08-28', 'user_id' => $employee->id, 'amount' => 100, 'advance_type_id' => $fuel->id]);
|
|
Advance::create(['date' => '2026-08-28', 'user_id' => $employee->id, 'amount' => -200, 'advance_type_id' => $deposit->id]);
|
|
|
|
$this->assertDatabaseHas('advances', ['advance_type_id' => $fuel->id, 'amount' => -100]);
|
|
$this->assertDatabaseHas('advances', ['advance_type_id' => $deposit->id, 'amount' => 200]);
|
|
});
|
|
|
|
test('manager can filter advances by text type and date range', function () {
|
|
$manager = userWithAdvanceAccess('kierownik');
|
|
$employee = User::factory()->create(['name' => 'Anna', 'surname' => 'Nowak']);
|
|
$fuel = AdvanceType::create(['name' => 'Paliwo']);
|
|
$hotel = AdvanceType::create(['name' => 'Hotel']);
|
|
|
|
Advance::create(['date' => '2026-08-10', 'user_id' => $employee->id, 'amount' => 100, 'advance_type_id' => $fuel->id]);
|
|
Advance::create(['date' => '2026-08-20', 'user_id' => $employee->id, 'amount' => 200, 'advance_type_id' => $hotel->id]);
|
|
|
|
$response = $this->actingAs($manager)->get(route('zaliczki', [
|
|
'search' => 'Anna',
|
|
'type' => $fuel->id,
|
|
'date_from' => '2026-08-01',
|
|
'date_to' => '2026-08-15',
|
|
]));
|
|
|
|
$response->assertOk();
|
|
$response->assertSee('100,00');
|
|
$response->assertDontSee('200,00');
|
|
});
|