b7a70f74e5
Includes a minimal Document::translations() HasMany relation so that DocumentFactory's afterCreating callback (which calls $document->translations()->count()) works. The full Document model refactor (accessors, fallback helpers, default-translation accessor) lands in Task 4.
56 lines
1.6 KiB
PHP
56 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Models;
|
|
|
|
use App\Models\Document;
|
|
use App\Models\DocumentTranslation;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class DocumentTranslationTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_belongs_to_a_document(): void
|
|
{
|
|
$doc = Document::factory()->create();
|
|
$translation = $doc->translations()->first();
|
|
|
|
$this->assertInstanceOf(Document::class, $translation->document);
|
|
$this->assertSame($doc->id, $translation->document->id);
|
|
}
|
|
|
|
public function test_unique_document_locale_constraint(): void
|
|
{
|
|
$doc = Document::factory()->create(['default_locale' => 'en']);
|
|
|
|
$this->expectException(QueryException::class);
|
|
|
|
DocumentTranslation::create([
|
|
'document_id' => $doc->id,
|
|
'locale' => 'en',
|
|
'title' => 'Duplicate',
|
|
'content' => 'x',
|
|
'rendered_html' => '<p>x</p>',
|
|
]);
|
|
}
|
|
|
|
public function test_cascade_delete_when_document_deleted(): void
|
|
{
|
|
$doc = Document::factory()->create();
|
|
$translationId = $doc->translations()->first()->id;
|
|
|
|
$doc->forceDelete();
|
|
|
|
$this->assertNull(DocumentTranslation::find($translationId));
|
|
}
|
|
|
|
public function test_render_markdown_converts_basic_markdown(): void
|
|
{
|
|
$html = DocumentTranslation::renderMarkdown('# Hello');
|
|
$this->assertStringContainsString('<h1>', $html);
|
|
$this->assertStringContainsString('Hello', $html);
|
|
}
|
|
}
|