mandant-crm/backend/tests/Feature/Modules/History/HistoryTest.php

78 lines
2.6 KiB
PHP
Raw Normal View History

<?php
use App\Models\User;
use App\Modules\Client\Models\Client;
use App\Modules\History\Models\HistoryEntry;
use App\Modules\History\Models\HistoryEntryType;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
it('staff can list history entries for a client', function () {
$user = User::factory()->create(['role' => 'staff']);
$client = Client::factory()->create();
HistoryEntry::factory(3)->create(['client_id' => $client->id, 'author_id' => $user->id]);
$response = $this->actingAs($user)->getJson("/api/clients/{$client->id}/history");
$response->assertStatus(200)->assertJsonCount(3, 'data');
});
it('staff can add a history entry', function () {
$user = User::factory()->create(['role' => 'staff']);
$client = Client::factory()->create();
$response = $this->actingAs($user)->postJson("/api/clients/{$client->id}/history", [
'type' => 'phone_call',
'body' => 'Rückruf vereinbart.',
]);
$response->assertStatus(201)
->assertJsonPath('data.type', 'phone_call')
->assertJsonPath('data.body', 'Rückruf vereinbart.')
->assertJsonPath('data.author_id', $user->id);
$this->assertDatabaseHas('history_entries', [
'client_id' => $client->id,
'type' => 'phone_call',
]);
});
it('staff can update a history entry', function () {
$user = User::factory()->create(['role' => 'staff']);
$client = Client::factory()->create();
$entry = HistoryEntry::factory()->create([
'client_id' => $client->id,
'author_id' => $user->id,
'body' => 'Original',
]);
$response = $this->actingAs($user)->putJson("/api/history/{$entry->id}", [
'body' => 'Updated body',
]);
$response->assertStatus(200)->assertJsonPath('data.body', 'Updated body');
$this->assertDatabaseHas('history_entries', ['id' => $entry->id, 'updated_by' => $user->id]);
});
it('staff can soft-delete a history entry', function () {
$user = User::factory()->create(['role' => 'staff']);
$client = Client::factory()->create();
$entry = HistoryEntry::factory()->create(['client_id' => $client->id, 'author_id' => $user->id]);
$response = $this->actingAs($user)->deleteJson("/api/history/{$entry->id}");
$response->assertStatus(204);
$this->assertSoftDeleted('history_entries', ['id' => $entry->id]);
});
it('rejects invalid history entry type', function () {
$user = User::factory()->create(['role' => 'staff']);
$client = Client::factory()->create();
$this->actingAs($user)->postJson("/api/clients/{$client->id}/history", [
'type' => 'invalid_type',
'body' => 'test',
])->assertStatus(422);
});