132 lines
4.8 KiB
PHP
132 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Cms;
|
|
|
|
use App\Models\Document;
|
|
use App\Models\DocumentCategory;
|
|
use App\Models\DocumentVersion;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Str;
|
|
use Tests\TestCase;
|
|
|
|
class PublicDocumentApiTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_public_documents_index_only_returns_active_public_documents(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$category = DocumentCategory::factory()->create(['slug' => 'legal-docs', 'name' => '法規文件']);
|
|
|
|
$visible = $this->createDocumentWithVersion($user, $category, [
|
|
'title' => '可見文件',
|
|
'access_level' => 'public',
|
|
'status' => 'active',
|
|
]);
|
|
|
|
$this->createDocumentWithVersion($user, $category, [
|
|
'title' => '非公開文件',
|
|
'access_level' => 'members',
|
|
'status' => 'active',
|
|
]);
|
|
|
|
$this->createDocumentWithVersion($user, $category, [
|
|
'title' => '封存文件',
|
|
'access_level' => 'public',
|
|
'status' => 'archived',
|
|
]);
|
|
|
|
$this->getJson('/api/v1/public-documents?per_page=50')
|
|
->assertOk()
|
|
->assertJsonCount(1, 'data')
|
|
->assertJsonPath('data.0.slug', $visible->public_uuid)
|
|
->assertJsonPath('data.0.current_version.version_number', '1.0')
|
|
->assertJsonPath('data.0.category.slug', 'legal-docs')
|
|
->assertJsonMissing(['title' => '非公開文件'])
|
|
->assertJsonMissing(['title' => '封存文件']);
|
|
}
|
|
|
|
public function test_public_documents_show_returns_versions_and_related_documents(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$category = DocumentCategory::factory()->create(['slug' => 'governance', 'name' => '治理文件']);
|
|
|
|
$document = $this->createDocumentWithVersion($user, $category, [
|
|
'title' => '主文件',
|
|
'description' => '主文件描述',
|
|
'access_level' => 'public',
|
|
'status' => 'active',
|
|
]);
|
|
|
|
$secondVersion = DocumentVersion::create([
|
|
'document_id' => $document->id,
|
|
'version_number' => '1.1',
|
|
'version_notes' => '更新內容',
|
|
'is_current' => true,
|
|
'file_path' => 'documents/test-main-v1_1.pdf',
|
|
'original_filename' => 'test-main-v1_1.pdf',
|
|
'mime_type' => 'application/pdf',
|
|
'file_size' => 4096,
|
|
'file_hash' => Str::random(64),
|
|
'uploaded_by_user_id' => $user->id,
|
|
'uploaded_at' => now(),
|
|
]);
|
|
|
|
$document->versions()->where('id', '!=', $secondVersion->id)->update(['is_current' => false]);
|
|
$document->update([
|
|
'current_version_id' => $secondVersion->id,
|
|
'version_count' => 2,
|
|
'last_updated_by_user_id' => $user->id,
|
|
]);
|
|
|
|
$related = $this->createDocumentWithVersion($user, $category, [
|
|
'title' => '相關文件',
|
|
'access_level' => 'public',
|
|
'status' => 'active',
|
|
]);
|
|
|
|
$this->getJson('/api/v1/public-documents/'.$document->public_uuid)
|
|
->assertOk()
|
|
->assertJsonPath('data.slug', $document->public_uuid)
|
|
->assertJsonPath('data.title', '主文件')
|
|
->assertJsonPath('data.version_count', 2)
|
|
->assertJsonPath('data.current_version.version_number', '1.1')
|
|
->assertJsonPath('data.versions.0.version_number', '1.1')
|
|
->assertJsonPath('related.0.slug', $related->public_uuid);
|
|
}
|
|
|
|
private function createDocumentWithVersion(User $user, DocumentCategory $category, array $overrides = []): Document
|
|
{
|
|
$document = Document::factory()->create(array_merge([
|
|
'document_category_id' => $category->id,
|
|
'created_by_user_id' => $user->id,
|
|
'last_updated_by_user_id' => $user->id,
|
|
'access_level' => 'public',
|
|
'status' => 'active',
|
|
'version_count' => 0,
|
|
], $overrides));
|
|
|
|
$version = DocumentVersion::create([
|
|
'document_id' => $document->id,
|
|
'version_number' => '1.0',
|
|
'version_notes' => '初始版本',
|
|
'is_current' => true,
|
|
'file_path' => 'documents/test-'.$document->id.'.pdf',
|
|
'original_filename' => 'test-'.$document->id.'.pdf',
|
|
'mime_type' => 'application/pdf',
|
|
'file_size' => 2048,
|
|
'file_hash' => Str::random(64),
|
|
'uploaded_by_user_id' => $user->id,
|
|
'uploaded_at' => now()->subHour(),
|
|
]);
|
|
|
|
$document->update([
|
|
'current_version_id' => $version->id,
|
|
'version_count' => 1,
|
|
]);
|
|
|
|
return $document->fresh();
|
|
}
|
|
}
|