Files
Gnuboard7/tests/Feature/Api/Admin/TemplateUpdateVersionCheckTest.php
T
2026-05-11 11:29:41 +09:00

132 lines
4.3 KiB
PHP

<?php
namespace Tests\Feature\Api\Admin;
use App\Enums\ExtensionOwnerType;
use App\Models\Permission;
use App\Models\Role;
use App\Models\User;
use App\Services\TemplateService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Mockery;
use Tests\TestCase;
/**
* 템플릿 업데이트 호환성 응답 페이로드 검증.
*/
class TemplateUpdateVersionCheckTest extends TestCase
{
use RefreshDatabase;
private User $admin;
private string $token;
protected function setUp(): void
{
parent::setUp();
$this->admin = $this->createAdminUser([
'core.templates.read',
'core.templates.activate',
'core.templates.update',
'core.templates.install',
]);
$this->token = $this->admin->createToken('test-token')->plainTextToken;
}
private function createAdminUser(array $permissions): User
{
$user = User::factory()->create();
$permissionIds = [];
foreach ($permissions as $permIdentifier) {
$permission = Permission::firstOrCreate(
['identifier' => $permIdentifier],
[
'name' => json_encode(['ko' => $permIdentifier, 'en' => $permIdentifier]),
'description' => json_encode(['ko' => $permIdentifier, 'en' => $permIdentifier]),
'extension_type' => ExtensionOwnerType::Core,
'extension_identifier' => 'core',
'type' => \App\Enums\PermissionType::Admin,
]
);
$permissionIds[] = $permission->id;
}
$testRole = Role::create([
'identifier' => 'admin_test_'.uniqid(),
'name' => json_encode(['ko' => '테스트 관리자', 'en' => 'Test Administrator']),
'description' => json_encode(['ko' => '테스트 관리자', 'en' => 'Test Administrator']),
'is_active' => true,
]);
$adminRole = Role::firstOrCreate(
['identifier' => 'admin'],
[
'name' => json_encode(['ko' => '관리자', 'en' => 'Administrator']),
'description' => json_encode(['ko' => '시스템 관리자', 'en' => 'System Administrator']),
'extension_type' => ExtensionOwnerType::Core,
'extension_identifier' => 'core',
'is_active' => true,
]
);
$testRole->permissions()->sync($permissionIds);
$user->roles()->attach($adminRole->id, [
'assigned_at' => now(),
'assigned_by' => null,
]);
$user->roles()->attach($testRole->id, [
'assigned_at' => now(),
'assigned_by' => null,
]);
return $user->fresh();
}
private function authRequest(): static
{
return $this->withHeaders([
'Authorization' => 'Bearer '.$this->token,
'Accept' => 'application/json',
]);
}
public function test_perform_update_request_accepts_force_flag(): void
{
$request = new \App\Http\Requests\Template\PerformTemplateUpdateRequest;
$rules = $request->rules();
$this->assertArrayHasKey('force', $rules);
$this->assertContains('boolean', $rules['force']);
}
public function test_check_updates_response_includes_compatibility_fields(): void
{
$mockService = Mockery::mock(TemplateService::class);
$mockService->shouldReceive('checkForUpdates')
->once()
->andReturn([
'updated_count' => 1,
'details' => [
'sirsoft-admin_basic' => [
'update_available' => true,
'update_source' => 'bundled',
'latest_version' => '2.0.0',
'current_version' => '1.0.0',
'required_core_version' => '>=7.0.0',
'is_compatible' => true,
'current_core_version' => '7.0.0-beta.1',
],
],
]);
$this->app->instance(TemplateService::class, $mockService);
$response = $this->authRequest()->postJson('/api/admin/templates/check-updates');
$response->assertStatus(200)
->assertJsonPath('data.details.sirsoft-admin_basic.is_compatible', true);
}
}