httpStatus === $status, 'Unexpected rejection status: ' . $exception->httpStatus); return; } throw new RuntimeException('Expected rejection was accepted.'); }; $capsule = new Capsule(); $capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $connection = $capsule->getConnection(); $schema = $connection->getSchemaBuilder(); $schema->create('legal_documents', static function (Blueprint $table): void { $table->bigIncrements('id'); $table->string('slug', 32); $table->string('locale', 10); $table->string('title', 120); $table->longText('draft_html'); $table->date('effective_date'); $table->unsignedInteger('draft_version')->default(0); $table->unsignedBigInteger('published_revision_id')->nullable(); $table->unsignedBigInteger('updated_by')->default(0); $table->dateTime('created_at'); $table->dateTime('updated_at'); $table->unique(['slug', 'locale']); }); $schema->create('legal_document_revisions', static function (Blueprint $table): void { $table->bigIncrements('id'); $table->unsignedBigInteger('document_id'); $table->unsignedInteger('version'); $table->string('title', 120); $table->longText('content_html'); $table->date('effective_date'); $table->dateTime('published_at'); $table->unsignedBigInteger('published_by'); $table->unique(['document_id', 'version']); }); $customer = ['user_id' => 1, 'is_admin' => false, 'is_super_admin' => false, 'can_manage' => false]; $admin = ['user_id' => 2, 'is_admin' => true, 'is_super_admin' => false, 'can_manage' => false]; $super = ['user_id' => 3, 'is_admin' => true, 'is_super_admin' => true, 'can_manage' => true]; $initial = LegalDocumentService::editorState($admin, 'terms', 'zh-CN'); $assert($initial['draft_version'] === 0 && $initial['published'] === null && str_contains($initial['content_html'], '接受与适用范围'), 'New editor did not expose safe default copy.'); $fallback = LegalDocumentService::published('terms', 'zh-CN', '测试云'); $assert($fallback['fallback'] === true && $fallback['version'] === 1 && str_contains($fallback['content_html'], '测试云') && count($fallback['outline']) >= 5, 'Public fallback or site-name substitution is incorrect.'); $draft = LegalDocumentService::saveDraft($super, 'terms', [ 'locale' => 'zh-CN', 'title' => '平台服务条款', 'effective_date' => '2026-11-01', 'draft_version' => '0', 'content_html' => '

范围

使用 {{site_name}}。

' . '危险链接' . '
说明
', ]); $assert($draft['draft_version'] === 1 && !str_contains($draft['content_html'], ' 'zh-CN', 'title' => '平台服务条款', 'effective_date' => '2026-11-01', 'draft_version' => '1', 'content_html' => '

适用范围

{{site_name}} 为你提供云服务。

账户

请保护凭据。

', ]); $assert($publishedState['draft_version'] === 2 && $publishedState['published']['version'] === 1 && count($publishedState['history']) === 1, 'First publication did not create an immutable revision.'); $published = LegalDocumentService::published('terms', 'zh-CN', '<站点>'); $assert($published['fallback'] === false && $published['version'] === 1 && str_contains($published['content_html'], '<站点>') && str_contains($published['content_html'], 'id="legal-section-1"') && count($published['outline']) === 2, 'Published DTO is unsafe or missing its outline.'); $firstRevision = $connection->table('legal_document_revisions')->where('version', 1)->first(); $secondState = LegalDocumentService::publish($super, 'terms', [ 'locale' => 'zh-CN', 'title' => '平台服务条款(更新)', 'effective_date' => '2026-12-01', 'draft_version' => '2', 'content_html' => '

更新范围

第二版正文。

', ]); $assert($secondState['draft_version'] === 3 && $secondState['published']['version'] === 2 && count($secondState['history']) === 2, 'Second publication overwrote or omitted revision history.'); $unchangedFirst = $connection->table('legal_document_revisions')->where('version', 1)->first(); $assert($firstRevision->content_html === $unchangedFirst->content_html, 'Publishing a new version mutated an older revision.'); $reject(static fn () => LegalDocumentService::saveDraft($super, 'terms', [ 'locale' => 'zh-CN', 'title' => '冲突', 'effective_date' => '2026-12-01', 'draft_version' => '2', 'content_html' => '

陈旧版本。

', ]), 409); $reject(static fn () => LegalDocumentService::saveDraft($admin, 'terms', [ 'locale' => 'zh-CN', 'title' => '无权限', 'effective_date' => '2026-12-01', 'draft_version' => '3', 'content_html' => '

无权限。

', ]), 403); $reject(static fn () => LegalDocumentService::editorState($customer, 'terms', 'zh-CN'), 403); $reject(static fn () => LegalDocumentService::editorState([], 'terms', 'zh-CN'), 401); $reject(static fn () => LegalDocumentService::preview($admin, 'wrong', [], '站点'), 400); $reject(static fn () => LegalDocumentService::preview($admin, 'terms', [ 'locale' => 'fr', 'title' => 'Title', 'effective_date' => '2026-12-01', 'content_html' => '

Body

', ], '站点'), 400); $reject(static fn () => LegalDocumentService::preview($admin, 'terms', [ 'locale' => 'en', 'title' => 'Title', 'effective_date' => '2026-02-30', 'content_html' => '

Body

', ], '站点'), 400); $reject(static fn () => LegalDocumentService::preview($admin, 'terms', [ 'locale' => 'en', 'title' => 'Title', 'effective_date' => '2026-12-01', 'content_html' => ' ', ], '站点'), 400); $documentId = (int) $connection->table('legal_documents')->where('slug', 'terms')->where('locale', 'zh-CN')->value('id'); $revisionId = (int) $connection->table('legal_documents')->where('id', $documentId)->value('published_revision_id'); $connection->table('legal_document_revisions')->where('id', $revisionId)->update([ 'content_html' => '

数据库异常内容

仍可显示。

', ]); $sanitizedRead = LegalDocumentService::published('terms', 'zh-CN', '站点'); $assert(!str_contains($sanitizedRead['content_html'], 'hasColumn('legal_documents', 'domain') && !$schema->hasColumn('legal_documents', 'host'), 'Legal content was accidentally scoped per domain.'); echo "Legal document service tests passed.\n";