80 lines
2.8 KiB
PHP
80 lines
2.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Tests\Unit\Models;
|
||
|
|
|
||
|
|
use App\Models\Document;
|
||
|
|
use App\Models\DocumentTranslation;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
use Illuminate\Support\Facades\App;
|
||
|
|
use Tests\TestCase;
|
||
|
|
|
||
|
|
class DocumentTest extends TestCase
|
||
|
|
{
|
||
|
|
use RefreshDatabase;
|
||
|
|
|
||
|
|
public function test_title_accessor_returns_current_locale_translation(): void
|
||
|
|
{
|
||
|
|
$doc = Document::factory()->create(['default_locale' => 'en']);
|
||
|
|
$doc->translations()->where('locale', 'en')->update(['title' => 'Hello']);
|
||
|
|
DocumentTranslation::factory()->create([
|
||
|
|
'document_id' => $doc->id,
|
||
|
|
'locale' => 'ja',
|
||
|
|
'title' => 'こんにちは',
|
||
|
|
]);
|
||
|
|
|
||
|
|
App::setLocale('ja');
|
||
|
|
$this->assertSame('こんにちは', $doc->fresh()->title);
|
||
|
|
|
||
|
|
App::setLocale('en');
|
||
|
|
$this->assertSame('Hello', $doc->fresh()->title);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_title_accessor_falls_back_to_default_locale(): void
|
||
|
|
{
|
||
|
|
$doc = Document::factory()->create(['default_locale' => 'en']);
|
||
|
|
$doc->translations()->where('locale', 'en')->update(['title' => 'Hello']);
|
||
|
|
|
||
|
|
App::setLocale('ja');
|
||
|
|
$this->assertSame('Hello', $doc->fresh()->title);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_content_and_rendered_html_accessors_fall_back(): void
|
||
|
|
{
|
||
|
|
$doc = Document::factory()->create(['default_locale' => 'en']);
|
||
|
|
$doc->translations()->where('locale', 'en')->update([
|
||
|
|
'content' => 'English body',
|
||
|
|
'rendered_html' => '<p>English body</p>',
|
||
|
|
]);
|
||
|
|
|
||
|
|
App::setLocale('ja');
|
||
|
|
$fresh = $doc->fresh();
|
||
|
|
$this->assertSame('English body', $fresh->content);
|
||
|
|
$this->assertSame('<p>English body</p>', $fresh->rendered_html);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_is_fallback_returns_true_when_locale_missing(): void
|
||
|
|
{
|
||
|
|
$doc = Document::factory()->create(['default_locale' => 'en']);
|
||
|
|
$this->assertTrue($doc->isFallback('ja'));
|
||
|
|
$this->assertFalse($doc->isFallback('en'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_translation_for_returns_null_when_fallback_disabled(): void
|
||
|
|
{
|
||
|
|
$doc = Document::factory()->create(['default_locale' => 'en']);
|
||
|
|
$this->assertNull($doc->translationFor('ja', fallback: false));
|
||
|
|
$this->assertNotNull($doc->translationFor('ja', fallback: true));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_available_locales_lists_existing_translations(): void
|
||
|
|
{
|
||
|
|
$doc = Document::factory()->create(['default_locale' => 'en']);
|
||
|
|
DocumentTranslation::factory()->create(['document_id' => $doc->id, 'locale' => 'ja']);
|
||
|
|
DocumentTranslation::factory()->create(['document_id' => $doc->id, 'locale' => 'fr']);
|
||
|
|
|
||
|
|
$locales = $doc->fresh()->availableLocales();
|
||
|
|
sort($locales);
|
||
|
|
$this->assertSame(['en', 'fr', 'ja'], $locales);
|
||
|
|
}
|
||
|
|
}
|