Files
parsaaghayi-backend/tests/Feature/Blog/RealBlogContentSeederTest.php
T
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

89 lines
3.7 KiB
PHP

<?php
namespace Tests\Feature\Blog;
use App\Models\Blog\Category;
use App\Models\Blog\Post;
use App\Models\Blog\Tag;
use App\Models\User;
use Database\Seeders\Blog\RealBlogContentSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
/**
* The production blog content comes from RealBlogContentSeeder, not from the
* random factories. Guard the seeder against regressions by running it against
* a fresh database and asserting the shape of what it published.
*/
class RealBlogContentSeederTest extends TestCase
{
use RefreshDatabase;
public function test_it_seeds_real_content_in_both_languages(): void
{
$user = User::factory()->create(['id' => 1]);
$this->seed(RealBlogContentSeeder::class);
$this->assertSame(14, Category::count());
$this->assertSame(32, Tag::count());
$this->assertSame(36, Post::count());
$post = Post::where('slug', 'core-web-vitals-practical-guide')->firstOrFail();
$this->assertSame($user->id, $post->author_id);
$this->assertNotNull($post->published_at);
$this->assertStringStartsWith('Core Web Vitals', $post->title);
$this->assertStringNotContainsStringIgnoringCase('lorem', $post->body);
$this->assertStringContainsString('##', $post->body);
$this->assertGreaterThan(0, $post->tags()->count());
$faTwin = Post::where('slug', 'core-web-vitals-practical-guide-fa')->firstOrFail();
$this->assertNotNull($faTwin->published_at);
// Every English post and every new topic pair resolve their Persian twin:
// the strict English/Persian mirror now spans 18 posts per language.
$englishCount = Post::where('slug', 'not like', '%-fa')->count();
$persianCount = Post::where('slug', 'like', '%-fa')->count();
$this->assertSame($englishCount, $persianCount);
$this->assertSame(18, $englishCount);
foreach (Post::where('slug', 'not like', '%-fa')->pluck('slug') as $slug) {
$this->assertDatabaseHas('posts', ['slug' => $slug.'-fa']);
}
// Deepened editing: every article ends with Key takeaways + FAQ sections
// in both languages, plus the new engineering-practices category pair.
foreach (Post::pluck('body') as $body) {
$this->assertStringContainsString('## FAQ', $body);
}
$this->assertDatabaseHas('categories', ['slug' => 'engineering-practices']);
$this->assertDatabaseHas('categories', ['slug' => 'engineering-practices-fa']);
$this->assertDatabaseHas('posts', ['slug' => 'typescript-advanced-patterns']);
$this->assertDatabaseHas('posts', ['slug' => 'typescript-advanced-patterns-fa']);
// featured_image is a VARCHAR(255) column; the generated og-image URLs
// must never overrun it, or the seeder fails in production.
foreach (Post::pluck('featured_image') as $image) {
$this->assertLessThanOrEqual(255, strlen($image));
$this->assertNotSame('', $image);
}
// Related posts stay inside the reader's language: the English version
// of a post never offers a Persian card and vice versa.
$this->assertGreaterThan(0, $post->relatedPosts()->count());
$this->assertSame(0, $post->relatedPosts()->where('slug', 'like', '%-fa')->count());
$this->assertSame(0, $faTwin->relatedPosts()->where('slug', 'not like', '%-fa')->count());
}
public function test_it_is_safe_to_run_twice(): void
{
User::factory()->create(['id' => 1]);
$this->seed(RealBlogContentSeeder::class);
$this->seed(RealBlogContentSeeder::class);
$this->assertSame(14, Category::count());
$this->assertSame(32, Tag::count());
$this->assertSame(36, Post::count());
}
}