isDir() ? rmdir($item->getPathname()) : unlink($item->getPathname()); } rmdir($directory); }; $runtimeRoot = sys_get_temp_dir() . '/virt-user-copy-view-' . bin2hex(random_bytes(8)); $smartyRoot = $runtimeRoot . '/smarty'; mkdir($smartyRoot, 0700, true); try { $source = new UserCopySourceCatalog(UserCopyManifest::load()); $compiled = (new UserCopyCatalogCompiler($source))->compile('en', [ // Test fixture copy is runtime publication data, never source copy. 'common.shell.user_center' => '运行时发布的账户外壳', 'common.shell.nav.instances' => '运行时发布的实例导航', 'common.toast.success.title' => '运行时发布的成功标题', 'common.toast.success.body' => '运行时发布的成功内容', 'common.toast.error.title' => '运行时发布的失败标题', 'common.toast.warning.title' => '运行时发布的警告标题', 'common.toast.close.aria' => '运行时发布的关闭标签', ]); $store = new UserCopyPublicationStore($runtimeRoot); $store->publish($compiled); $japanese = (new UserCopyCatalogCompiler($source))->compile('ja', [ 'common.shell.user_center' => 'ユーザーセンター', ]); $store->publish($japanese); $currentPath = $runtimeRoot . '/current.json'; $current = json_decode( (string) file_get_contents($currentPath), true, 512, JSON_THROW_ON_ERROR ); $current['locale_directory'] = [ 'en' => ['display_name' => 'English', 'enabled' => true], 'ja' => ['display_name' => '日本語', 'enabled' => true], 'de' => ['display_name' => 'Deutsch', 'enabled' => true], ]; file_put_contents( $currentPath, UserCopySourceCatalog::canonicalJson($current) ); $reader = new UserCopyRuntimeReader($runtimeRoot, $source); $runtime = new UserCopyViewRuntime($reader); $assert(array_column($reader->availableLocales(), 'locale') === [ 'zh-CN', 'en', 'ja', ], 'Runtime exposed an unpublished target or omitted a published locale.'); $smarty = new Smarty(); $smarty->setCompileDir($smartyRoot); $smarty->setCacheDir($smartyRoot); $catalog = $runtime->bind( $smarty, 'en', '/user/cloud/instances?page=2', 'cloud.instances' ); $html = $smarty->fetch( 'string:' . '{user_copy key="common.shell.user_center"}|' . '{user_copy key="common.shell.nav.instances"}|' . '{$user_copy_language_zh_url}|{$user_copy_language_en_url}' . '' ); $assert(str_contains($html, 'lang="en"') && str_contains($html, '运行时发布的账户外壳') && str_contains($html, '运行时发布的实例导航'), 'Smarty did not render the active runtime publication.'); $assert(str_contains($html, 'page=2&lang=zh-CN') && str_contains($html, 'page=2&lang=en'), 'Language switching did not preserve the current query.'); $assert($catalog->generation() === $compiled->generation, 'The user view did not bind the active immutable generation.'); $ordinaryBinding = $smarty->fetch( 'string:' . 'Heading' ); $assert($ordinaryBinding === '

Heading

', 'Ordinary users received administrator copy-binding metadata.'); $ordinaryVariables = $smarty->getTemplateVars(); $assert(($ordinaryVariables['user_copy_page'] ?? null) === 'cloud.instances' && ($ordinaryVariables['user_copy_editor_enabled'] ?? true) === false && ($ordinaryVariables['user_copy_editor_bootstrap_json'] ?? null) === '{}', 'Ordinary users received page-editor capability or bootstrap data.'); $languages = $ordinaryVariables['user_copy_languages'] ?? []; $assert(array_column($languages, 'locale') === ['zh-CN', 'en', 'ja'] && ($languages[1]['active'] ?? false) === true && ($languages[2]['display_name'] ?? null) === '日本語' && str_contains((string) ($languages[2]['url'] ?? ''), 'lang=ja'), 'The view runtime did not expose its dynamic language menu.'); $authorSmarty = new Smarty(); $authorSmarty->setCompileDir($smartyRoot); $authorSmarty->setCacheDir($smartyRoot); $authorSmarty->assign('admin_can_manage', true); $runtime->bind( $authorSmarty, 'en', '/user/cloud/instances', 'cloud.instances' ); $authorBinding = $authorSmarty->fetch( 'string:' . 'Heading' ); $assert($authorBinding === '

' . 'Heading

', 'Super administrator did not receive the safe copy-binding attribute.'); $assert($authorSmarty->getTemplateVars('user_copy_editor_enabled') === false, 'An author without a valid CSRF token received the page editor.'); foreach (['missing.copy.key', 'Invalid key'] as $invalidBindingKey) { try { $authorSmarty->fetch( 'string:Copy' ); throw new RuntimeException( 'Invalid user-copy binding key was accepted: ' . $invalidBindingKey ); } catch (Throwable $exception) { $assert(str_contains( $exception->getMessage(), 'Unknown user copy binding key' ), 'Invalid binding failed without a catalog contract error.'); } } $editorSmarty = new Smarty(); $editorSmarty->setCompileDir($smartyRoot); $editorSmarty->setCacheDir($smartyRoot); $editorSmarty->assign([ 'admin_can_manage' => true, 'admin_csrf_token' => str_repeat('b', 64), ]); $runtime->bind( $editorSmarty, 'en', '/user/cloud/instances', 'cloud.instances' ); $editorVariables = $editorSmarty->getTemplateVars(); $editorBootstrap = json_decode( (string) $editorVariables['user_copy_editor_bootstrap_json'], true, 512, JSON_THROW_ON_ERROR ); $assert($editorVariables['user_copy_editor_enabled'] === true && array_keys($editorBootstrap) === [ 'schema', 'page', 'runtime_locale', 'csrf_token', ] && $editorBootstrap['page'] === 'cloud.instances' && $editorBootstrap['runtime_locale'] === 'en' && $editorBootstrap['csrf_token'] === str_repeat('b', 64), 'The authorized page editor did not receive its minimal bootstrap.'); $noPageSmarty = new Smarty(); $noPageSmarty->setCompileDir($smartyRoot); $noPageSmarty->setCacheDir($smartyRoot); $noPageSmarty->assign([ 'admin_can_manage' => true, 'admin_csrf_token' => str_repeat('c', 64), ]); $runtime->bind($noPageSmarty, 'en', '/user/unmapped', null); $assert($noPageSmarty->getTemplateVars('user_copy_editor_enabled') === false && $noPageSmarty->getTemplateVars('user_copy_editor_bootstrap_json') === '{}', 'A request without a mapped page received the page editor.'); $variables = $smarty->getTemplateVars(); $bootstrap = json_decode( (string) ($variables['user_copy_bootstrap_json'] ?? ''), true, 512, JSON_THROW_ON_ERROR ); $assert(($bootstrap['locale'] ?? null) === 'en' && ($bootstrap['generation'] ?? null) === $compiled->generation && ($bootstrap['messages']['common.toast.success.title'] ?? null) === '运行时发布的成功标题', 'Browser bootstrap does not match the server request snapshot.'); $fallbackSmarty = new Smarty(); $fallbackSmarty->setCompileDir($smartyRoot); $fallbackSmarty->setCacheDir($smartyRoot); $fallback = $runtime->bind( $fallbackSmarty, 'fr', '/user/ticket', null ); $assert($fallback->generation() === null && $fallback->text('common.shell.user_center') === '用户中心', 'An unpublished locale did not fall back to source copy.'); $changedManifest = UserCopyManifest::load(); foreach ($changedManifest['entries'] as &$entry) { if ($entry['key'] === 'common.shell.user_center') { $entry['source'] = '更新后的用户中心'; } } unset($entry); $changedManifest['entries'][] = [ 'key' => 'common.runtime.new_notice', 'scope' => 'common', 'page' => 'cloud.instances', 'kind' => 'notice', 'source' => '新增中文提示', 'format' => 'plain_text', 'parameters' => [], 'criticality' => 'required', 'allow_hide' => false, 'toast_tone' => null, 'editor_surface' => 'feedback', 'editor_group' => 'common.shared', ]; $changedSource = new UserCopySourceCatalog( UserCopyManifest::validate($changedManifest) ); $changedReader = new UserCopyRuntimeReader($runtimeRoot, $changedSource); $changedCatalog = new App\Services\UserCopy\UserCopyCatalog( $changedReader, 'en', ['common'] ); $assert($changedCatalog->generation() === $compiled->generation && $changedCatalog->text('common.shell.nav.instances') === '运行时发布的实例导航' && $changedCatalog->text('common.shell.user_center') === '更新后的用户中心' && $changedCatalog->text('common.runtime.new_notice') === '新增中文提示', 'A source update discarded compatible published copy or retained stale copy.'); $current['locale_directory']['en']['enabled'] = false; file_put_contents( $currentPath, UserCopySourceCatalog::canonicalJson($current) ); $disabledReader = new UserCopyRuntimeReader($runtimeRoot, $source); $assert(array_column($disabledReader->availableLocales(), 'locale') === [ 'zh-CN', 'ja', ], 'An explicitly disabled English locale was restored by legacy fallback.'); unset($current['locale_directory']); file_put_contents( $currentPath, UserCopySourceCatalog::canonicalJson($current) ); $legacyReader = new UserCopyRuntimeReader($runtimeRoot, $source); $assert(array_column($legacyReader->availableLocales(), 'locale') === [ 'zh-CN', 'en', ], 'A legacy English publication without locale metadata became unavailable.'); } finally { $removeTree($runtimeRoot); } echo "User copy locale, Smarty and browser-bootstrap runtime checks passed.\n";