Files
Parsa Aghayi fbacb0c9a1
Deploy Backend / deploy (push) Successful in 2m35s
Expand blog to 36 mirrored posts with deepened sections and page-turn links
- Six new topic pairs (EN+FA): TypeScript advanced patterns, SQL indexes,
  Git branching/reviews, TDD, clean architecture in Laravel, browser page load
- Every existing post gains Key takeaways + FAQ sections in both languages
- New engineering-practices category pair and database/git/testing tag pairs
- Single-post API now returns same-language previous_post/next_post links
2026-10-02 09:34:53 -04:00

134 lines
4.2 KiB
PHP

<?php
namespace Tests\Feature\Blog;
use App\Models\Blog\Category;
use App\Models\Blog\Post;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PostTest extends TestCase
{
use RefreshDatabase;
public function test_can_list_posts()
{
Post::factory()->count(3)->create();
$response = $this->getJson('/api/blog/posts');
$response->assertStatus(200);
}
public function test_can_get_single_post()
{
$post = Post::factory()->create();
$response = $this->getJson("/api/blog/posts/{$post->slug}");
$response->assertStatus(200)
->assertJsonStructure(['post', 'related_posts', 'previous_post', 'next_post']);
}
public function test_page_turn_links_stay_inside_the_reader_language()
{
Post::factory()->create(['slug' => 'older-topic', 'published_at' => now()->subDays(2)]);
Post::factory()->create(['slug' => 'older-topic-fa', 'published_at' => now()->subDays(2)]);
Post::factory()->create(['slug' => 'newer-topic', 'published_at' => now()->addDay()]);
Post::factory()->create(['slug' => 'newer-topic-fa', 'published_at' => now()->addDay()]);
$current = Post::factory()->create(['slug' => 'current-topic', 'published_at' => now()]);
$currentFa = Post::factory()->create(['slug' => 'current-topic-fa', 'published_at' => now()]);
$response = $this->getJson("/api/blog/posts/{$current->slug}");
$response->assertStatus(200)
->assertJsonPath('previous_post.slug', 'newer-topic')
->assertJsonPath('next_post.slug', 'older-topic')
->assertJsonMissing(['previous_post' => ['slug' => 'newer-topic-fa']]);
$response = $this->getJson("/api/blog/posts/{$currentFa->slug}");
$response->assertStatus(200)
->assertJsonPath('previous_post.slug', 'newer-topic-fa')
->assertJsonPath('next_post.slug', 'older-topic-fa');
}
public function test_can_create_post()
{
$this->actingAsAdmin();
$category = Category::factory()->create();
$user = User::factory()->create();
$response = $this->postJson('/api/blog/posts', [
'title' => 'Test Post',
'body' => 'Content',
'slug' => 'test-post',
'category_id' => $category->id,
'author_id' => $user->id,
]);
$response->assertStatus(201);
$this->assertDatabaseHas('posts', ['slug' => 'test-post']);
}
public function test_can_update_post()
{
$this->actingAsAdmin();
$post = Post::factory()->create();
$response = $this->putJson("/api/blog/posts/{$post->id}", [
'title' => 'Updated',
]);
$response->assertStatus(200);
}
public function test_can_delete_post()
{
$this->actingAsAdmin();
$post = Post::factory()->create();
$response = $this->deleteJson("/api/blog/posts/{$post->id}");
$response->assertStatus(200);
$this->assertDatabaseMissing('posts', ['id' => $post->id]);
}
public function test_can_filter_posts()
{
Post::factory()->count(3)->create();
$response = $this->getJson('/api/blog/posts?per_page=2');
$response->assertStatus(200);
}
public function test_can_filter_posts_by_language()
{
Post::factory()->create(['slug' => 'localized-topic', 'title' => 'A topic']);
Post::factory()->create(['slug' => 'localized-topic-fa', 'title' => 'یک موضوع']);
$response = $this->getJson('/api/blog/posts?per_page=10&lang=fa');
$response->assertStatus(200)
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.slug', 'localized-topic-fa');
$response = $this->getJson('/api/blog/posts?per_page=10&lang=en');
$response->assertStatus(200)
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.slug', 'localized-topic');
// Omitting lang keeps every language (sitemaps, feeds, admin).
$response = $this->getJson('/api/blog/posts?per_page=10');
$response->assertStatus(200)
->assertJsonCount(2, 'data');
}
}