Files
parsaaghayi-backend/tests/Feature/ContactMessageTest.php
T
Parsa Aghayi d4f41b9c2e
Deploy Backend / deploy (push) Successful in 2m40s
Store contact messages in DB and seed real bilingual blog content
Contact form messages were sent via mailto and the site has no mail
service, so visitors saw a placeholder caveat instead of a working
channel. Add a contact_messages table, a public throttled POST endpoint,
and admin list/mark/delete endpoints, then wire them into the panel.

Replace the faker-generated blog demonstration content with real
English and Persian SEO-oriented articles via RealBlogContentSeeder
(deterministic, bilingual posts with per-language categories and tags,
and cross-linked related posts).
2026-10-02 05:41:05 -04:00

116 lines
3.6 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\User;
use Laravel\Sanctum\Sanctum;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ContactMessageTest extends TestCase
{
use RefreshDatabase;
public function test_the_public_form_stores_a_message(): void
{
$response = $this->postJson('/api/contact', [
'name' => 'Ali',
'email' => 'ali@example.com',
'message' => 'می‌خواهم درباره یک پروژه صحبت کنم.',
]);
$response->assertCreated();
$this->assertDatabaseHas('contact_messages', [
'email' => 'ali@example.com',
'read_at' => null,
]);
}
public function test_the_public_form_rejects_invalid_payloads(): void
{
$this->postJson('/api/contact', ['name' => '', 'email' => 'not-an-email', 'message' => ''])
->assertUnprocessable()
->assertJsonValidationErrors(['name', 'email', 'message']);
$this->assertDatabaseCount('contact_messages', 0);
}
public function test_anonymous_requests_cannot_manage_messages(): void
{
$this->getJson('/api/admin/messages')->assertUnauthorized();
$this->deleteJson('/api/admin/messages/1')->assertUnauthorized();
$this->patchJson('/api/admin/messages/1', ['read' => true])->assertUnauthorized();
}
public function test_admins_can_list_messages(): void
{
$message = \App\Models\ContactMessage::create([
'name' => 'Ali',
'email' => 'ali@example.com',
'message' => 'سلام',
]);
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
$this->getJson('/api/admin/messages')
->assertOk()
->assertJsonPath('data.0.id', $message->id)
->assertJsonCount(1, 'data');
$this->assertFalse($message->fresh()->isRead());
}
public function test_admins_can_mark_a_message_as_read_and_unread(): void
{
$message = \App\Models\ContactMessage::create([
'name' => 'Ali',
'email' => 'ali@example.com',
'message' => 'سلام',
]);
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
$this->patchJson('/api/admin/messages/'.$message->id, ['read' => true])
->assertOk();
$this->assertTrue($message->fresh()->isRead());
$this->patchJson('/api/admin/messages/'.$message->id, ['read' => false])
->assertOk();
$this->assertFalse($message->fresh()->isRead());
}
public function test_admins_can_delete_a_message(): void
{
$message = \App\Models\ContactMessage::create([
'name' => 'Ali',
'email' => 'ali@example.com',
'message' => 'سلام',
]);
Sanctum::actingAs(User::factory()->create(['is_admin' => true]));
$this->deleteJson('/api/admin/messages/'.$message->id)->assertOk();
$this->assertDatabaseMissing('contact_messages', ['id' => $message->id]);
}
public function test_non_admin_users_cannot_manage_messages(): void
{
$message = \App\Models\ContactMessage::create([
'name' => 'Ali',
'email' => 'ali@example.com',
'message' => 'سلام',
]);
Sanctum::actingAs(User::factory()->create(['is_admin' => false]));
$this->getJson('/api/admin/messages')->assertForbidden();
$this->patchJson('/api/admin/messages/'.$message->id, ['read' => true])->assertForbidden();
$this->deleteJson('/api/admin/messages/'.$message->id)->assertForbidden();
}
}