libra/tests/Feature/Public/PostsTest.php

156 lines
5.1 KiB
PHP

<?php
use App\Models\Post;
test('posts listing page shows only published posts', function () {
Post::factory()->published()->create(['title' => ['en' => 'Published Post', 'ar' => 'مقال منشور']]);
Post::factory()->draft()->create(['title' => ['en' => 'Draft Post', 'ar' => 'مقال مسودة']]);
$this->withSession(['locale' => 'en'])
->get(route('posts.index'))
->assertOk()
->assertSee('Published Post')
->assertDontSee('Draft Post');
});
test('posts listing page displays in reverse chronological order', function () {
Post::factory()->published()->create([
'title' => ['en' => 'Older Post', 'ar' => 'مقال قديم'],
'created_at' => now()->subDays(5),
'published_at' => now()->subDays(5),
]);
Post::factory()->published()->create([
'title' => ['en' => 'Newer Post', 'ar' => 'مقال جديد'],
'created_at' => now(),
'published_at' => now(),
]);
$this->withSession(['locale' => 'en'])
->get(route('posts.index'))
->assertOk()
->assertSeeInOrder(['Newer Post', 'Older Post']);
});
test('posts listing page paginates results', function () {
Post::factory()->count(15)->published()->create();
// First page should show navigation to next page
$this->withSession(['locale' => 'en'])
->get(route('posts.index'))
->assertOk()
->assertSee('Next');
// Second page should also work
$this->withSession(['locale' => 'en'])
->get(route('posts.index').'?page=2')
->assertOk();
});
test('posts listing page shows no posts message when empty', function () {
$this->withSession(['locale' => 'en'])
->get(route('posts.index'))
->assertOk()
->assertSee('No posts found.');
});
test('posts listing page shows post excerpt', function () {
Post::factory()->published()->create([
'title' => ['en' => 'Test Post', 'ar' => 'مقال اختباري'],
'body' => ['en' => 'This is a test body content that should be displayed as an excerpt.', 'ar' => 'محتوى اختباري'],
]);
$this->withSession(['locale' => 'en'])
->get(route('posts.index'))
->assertOk()
->assertSee('Test Post')
->assertSee('This is a test body content');
});
test('posts listing page is accessible without authentication', function () {
Post::factory()->published()->create();
$this->get(route('posts.index'))
->assertOk();
});
test('individual post page shows full content', function () {
$post = Post::factory()->published()->create([
'title' => ['en' => 'Full Post Title', 'ar' => 'عنوان المقال الكامل'],
'body' => ['en' => '<p>Full post body content here.</p>', 'ar' => '<p>محتوى المقال الكامل هنا.</p>'],
]);
$this->withSession(['locale' => 'en'])
->get(route('posts.show', $post))
->assertOk()
->assertSee('Full Post Title')
->assertSee('Full post body content here.');
});
test('individual post page returns 404 for unpublished posts', function () {
$post = Post::factory()->draft()->create();
$this->get(route('posts.show', $post))
->assertNotFound();
});
test('individual post page returns 404 for non-existent posts', function () {
$this->get(route('posts.show', 999))
->assertNotFound();
});
test('individual post page shows back to posts link', function () {
$post = Post::factory()->published()->create();
$this->withSession(['locale' => 'en'])
->get(route('posts.show', $post))
->assertOk()
->assertSee('Back to Posts');
});
test('individual post page is accessible without authentication', function () {
$post = Post::factory()->published()->create();
$this->get(route('posts.show', $post))
->assertOk();
});
test('posts listing page displays content in current locale', function () {
Post::factory()->published()->create([
'title' => ['en' => 'English Title', 'ar' => 'عنوان عربي'],
'body' => ['en' => 'English content', 'ar' => 'محتوى عربي'],
]);
// Test English
$this->withSession(['locale' => 'en'])
->get(route('posts.index'))
->assertOk()
->assertSee('English Title');
// Test Arabic
$this->withSession(['locale' => 'ar'])
->get(route('posts.index'))
->assertOk()
->assertSee('عنوان عربي');
});
test('individual post page displays content in current locale', function () {
$post = Post::factory()->published()->create([
'title' => ['en' => 'English Title', 'ar' => 'عنوان عربي'],
'body' => ['en' => 'English content', 'ar' => 'محتوى عربي'],
]);
// Test English
$this->withSession(['locale' => 'en'])
->get(route('posts.show', $post))
->assertOk()
->assertSee('English Title')
->assertSee('English content');
// Test Arabic
$this->withSession(['locale' => 'ar'])
->get(route('posts.show', $post))
->assertOk()
->assertSee('عنوان عربي')
->assertSee('محتوى عربي');
});