cms/tests/Feature/Admin/BlockTranslationTest.php

82 lines
2.3 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Feature\Admin;
use App\Models\Page;
use App\Models\Role;
use App\Models\Setting;
use App\Models\User;
use App\Models\Permission;
use Illuminate\Support\Facades\Cache;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class BlockTranslationTest extends TestCase
{
use RefreshDatabase;
protected User $admin;
protected function setUp(): void
{
parent::setUp();
Cache::flush();
Setting::create([
'key' => 'supported_languages',
'value' => [
['name' => 'English', 'abbreviation' => 'en'],
['name' => 'Spanish', 'abbreviation' => 'es'],
],
'group' => 'general'
]);
$adminRole = Role::create(['name' => 'Admin', 'slug' => 'admin']);
$permission = Permission::create([
'name' => 'Manage Translations',
'slug' => 'manage-translations',
'resource' => 'translations',
'action' => 'manage'
]);
$adminRole->permissions()->attach($permission);
$this->admin = User::factory()->create();
$this->admin->roles()->attach($adminRole);
}
/** @test */
public function it_can_translate_a_text_block()
{
// Mock the translation service via the controller's dependency if possible,
// but here we just test the endpoint which uses TranslationProviderService.
// TranslationProviderService uses 'mock' driver by default if no keys are set.
$response = $this->actingAs($this->admin)
->postJson(route('admin.translate'), [
'text' => 'Hello world',
'from' => 'en',
'to' => 'es'
]);
$response->assertStatus(200);
$response->assertJsonStructure(['translated', 'from', 'to']);
// Mock driver returns the original text + [MOCK-es]
$this->assertEquals('[es] Hello world', $response->json('translated'));
}
/** @test */
public function it_fails_if_unauthorized()
{
$user = User::factory()->create();
$response = $this->actingAs($user)
->postJson(route('admin.translate'), [
'text' => 'Hello world',
'from' => 'en',
'to' => 'es'
]);
$response->assertStatus(403);
}
}