cms/tests/Feature/TranslationTest.php

81 lines
2.1 KiB
PHP
Raw Permalink Normal View History

<?php
namespace Tests\Feature;
use App\Models\Role;
use App\Models\Translation;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class TranslationTest extends TestCase
{
use RefreshDatabase;
protected User $admin;
protected function setUp(): void
{
parent::setUp();
$adminRole = Role::create(['name' => 'Admin', 'slug' => 'admin']);
$this->admin = User::factory()->create();
$this->admin->roles()->attach($adminRole);
}
public function test_t_helper_returns_humanized_key_when_not_found()
{
$this->assertEquals('Home', t('nav.home'));
$this->assertEquals('My awesome page', t('cms::my_awesome_page'));
}
public function test_t_helper_returns_db_override_when_present()
{
Translation::create([
'locale' => 'en',
'group' => 'cms',
'key' => 'navigation.home',
'value' => 'Dashboard Home'
]);
$this->assertEquals('Dashboard Home', t('cms::navigation.home'));
}
public function test_admin_can_manage_translations()
{
$response = $this->actingAs($this->admin)
->postJson(route('admin.translations.update'), [
'locale' => 'en',
'group' => 'cms',
'key' => 'test.key',
'value' => 'Test Value'
]);
$response->assertStatus(200);
$this->assertDatabaseHas('translations', [
'locale' => 'en',
'group' => 'cms',
'key' => 'test.key',
'value' => 'Test Value'
]);
$this->assertEquals('Test Value', t('cms::test.key'));
}
public function test_locale_switching_via_middleware()
{
Translation::create([
'locale' => 'es',
'group' => 'cms',
'key' => 'welcome',
'value' => 'Bienvenido'
]);
// Mock session
$this->withSession(['locale' => 'es'])
->get(route('admin.dashboard'));
$this->assertEquals('Bienvenido', t('cms::welcome', [], 'es'));
}
}