VvvebJs / github-advanced-test.php
CatPtain's picture
Upload 2 files
6f6abdf verified
<?php
require_once __DIR__ . '/storage.php';
// 简单认证
$testAuth = false;
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
$users = StorageConfig::getUsers();
$testAuth = isset($users[$_SERVER['PHP_AUTH_USER']]) &&
$users[$_SERVER['PHP_AUTH_USER']] === $_SERVER['PHP_AUTH_PW'];
}
if (!$testAuth) {
header('WWW-Authenticate: Basic realm="VvvebJs GitHub Advanced Test"');
header('HTTP/1.0 401 Unauthorized');
die('Authentication required for GitHub advanced test');
}
$github = StorageConfig::getGitHubConfig();
$currentUser = StorageConfig::getCurrentUser();
$userPath = StorageConfig::getUserPath();
// 执行完整的GitHub测试
$testResults = [];
if ($_POST['run_full_test'] ?? false) {
try {
error_log("GitHub Advanced Test: Starting comprehensive test suite");
// 测试1: GitHub API连接
$apiUrl = "https://api.github.com/repos/{$github['owner']}/{$github['repo']}";
$headers = [
'Authorization: token ' . $github['token'],
'User-Agent: VvvebJs-Advanced-Test',
'Accept: application/vnd.github.v3+json'
];
$context = stream_context_create([
'http' => [
'method' => 'GET',
'header' => implode("\r\n", $headers)
]
]);
$response = @file_get_contents($apiUrl, false, $context);
$repoInfo = $response ? json_decode($response, true) : null;
$testResults['api_connection'] = [
'success' => $repoInfo !== null,
'response' => $repoInfo ? 'Repository found' : 'Failed to connect',
'repo_name' => $repoInfo['name'] ?? 'N/A',
'default_branch' => $repoInfo['default_branch'] ?? 'N/A'
];
// 测试2: 分支检查
if ($testResults['api_connection']['success']) {
$branchUrl = "https://api.github.com/repos/{$github['owner']}/{$github['repo']}/branches/{$github['branch']}";
$branchContext = stream_context_create([
'http' => [
'method' => 'GET',
'header' => implode("\r\n", $headers)
]
]);
$branchResponse = @file_get_contents($branchUrl, false, $branchContext);
$branchInfo = $branchResponse ? json_decode($branchResponse, true) : null;
$testResults['branch_check'] = [
'success' => $branchInfo !== null,
'branch_name' => $github['branch'],
'exists' => $branchInfo !== null,
'commit_sha' => $branchInfo['commit']['sha'] ?? 'N/A'
];
}
// 测试3: 目录结构检查
$testResults['directory_structure'] = [];
$directories = [
'pages' => "pages",
'users' => "pages/users",
'user_dir' => "pages/{$userPath}"
];
foreach ($directories as $key => $path) {
$dirUrl = "https://api.github.com/repos/{$github['owner']}/{$github['repo']}/contents/{$path}";
$dirContext = stream_context_create([
'http' => [
'method' => 'GET',
'header' => implode("\r\n", $headers)
]
]);
$dirResponse = @file_get_contents($dirUrl, false, $dirContext);
$testResults['directory_structure'][$key] = [
'path' => $path,
'exists' => $dirResponse !== false,
'response' => $dirResponse ? 'Found' : 'Not found'
];
}
// 测试4: 文件保存测试
$storageManager = new StorageManager();
$testHtml = '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GitHub Advanced Test</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; }
.container { max-width: 800px; margin: 0 auto; background: rgba(255,255,255,0.1); padding: 30px; border-radius: 15px; backdrop-filter: blur(10px); }
h1 { text-align: center; margin-bottom: 30px; text-shadow: 2px 2px 4px rgba(0,0,0,0.3); }
.test-info { background: rgba(255,255,255,0.2); padding: 20px; border-radius: 10px; margin: 20px 0; }
.success { color: #4CAF50; font-weight: bold; text-shadow: 1px 1px 2px rgba(0,0,0,0.5); }
</style>
</head>
<body>
<div class="container">
<h1>🚀 GitHub Advanced Test Success!</h1>
<div class="test-info">
<h2>Test Results</h2>
<p><span class="success">✅ GitHub API Connection: SUCCESS</span></p>
<p><span class="success">✅ Branch Verification: SUCCESS</span></p>
<p><span class="success">✅ Directory Structure: SUCCESS</span></p>
<p><span class="success">✅ File Save Operation: SUCCESS</span></p>
</div>
<div class="test-info">
<h2>Test Details</h2>
<p><strong>Test Time:</strong> ' . date('Y-m-d H:i:s') . ' UTC</p>
<p><strong>User:</strong> ' . htmlspecialchars($currentUser) . '</p>
<p><strong>Repository:</strong> ' . htmlspecialchars($github['owner']) . '/' . htmlspecialchars($github['repo']) . '</p>
<p><strong>Branch:</strong> ' . htmlspecialchars($github['branch']) . '</p>
<p><strong>User Path:</strong> ' . htmlspecialchars($userPath) . '</p>
</div>
<div class="test-info">
<h2>🎉 All Systems Operational</h2>
<p>Your VvvebJs installation is properly configured and can successfully save files to GitHub. You can now use all editor features with confidence!</p>
</div>
</div>
</body>
</html>';
$testFilename = 'github-advanced-test-' . time() . '.html';
$saveResult = $storageManager->saveFile($testFilename, $testHtml);
$testResults['file_save'] = [
'success' => $saveResult,
'filename' => $testFilename,
'content_length' => strlen($testHtml),
'message' => $saveResult ? 'File saved successfully to GitHub' : 'Failed to save file'
];
// 测试5: 文件检索测试
if ($saveResult) {
$retrievedContent = $storageManager->getFile($testFilename);
$testResults['file_retrieve'] = [
'success' => $retrievedContent !== false,
'content_matches' => $retrievedContent === $testHtml,
'retrieved_length' => $retrievedContent ? strlen($retrievedContent) : 0
];
}
// 测试6: 文件列表测试
$fileList = $storageManager->listFiles();
$testResults['file_list'] = [
'success' => is_array($fileList),
'file_count' => is_array($fileList) ? count($fileList) : 0,
'contains_test_file' => false
];
if (is_array($fileList)) {
foreach ($fileList as $file) {
if ($file['name'] === $testFilename) {
$testResults['file_list']['contains_test_file'] = true;
break;
}
}
}
// 计算总体成功率
$totalTests = 0;
$passedTests = 0;
foreach ($testResults as $test) {
if (isset($test['success'])) {
$totalTests++;
if ($test['success']) $passedTests++;
} elseif (is_array($test)) {
foreach ($test as $subtest) {
if (isset($subtest['success']) || isset($subtest['exists'])) {
$totalTests++;
if ($subtest['success'] ?? $subtest['exists'] ?? false) $passedTests++;
}
}
}
}
$testResults['summary'] = [
'total_tests' => $totalTests,
'passed_tests' => $passedTests,
'success_rate' => $totalTests > 0 ? round(($passedTests / $totalTests) * 100, 1) : 0,
'overall_success' => $passedTests === $totalTests
];
} catch (Exception $e) {
error_log("GitHub Advanced Test Error: " . $e->getMessage());
$testResults['error'] = [
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine()
];
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>VvvebJs GitHub 高级测试</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
.config-value { background-color: #f8f9fa; padding: 2px 6px; border-radius: 3px; font-family: monospace; }
.status-ok { color: #28a745; }
.status-error { color: #dc3545; }
.status-warning { color: #ffc107; }
.test-result { margin-bottom: 15px; padding: 15px; border-radius: 8px; }
.test-success { background-color: #d4edda; border: 1px solid #c3e6cb; }
.test-failure { background-color: #f8d7da; border: 1px solid #f5c6cb; }
.progress-ring { width: 120px; height: 120px; }
.progress-ring__circle { stroke: #ddd; fill: transparent; stroke-width: 4; }
.progress-ring__progress { stroke: #4caf50; fill: transparent; stroke-width: 4; stroke-linecap: round; transform: rotate(-90deg); transform-origin: 50% 50%; }
</style>
</head>
<body>
<div class="container mt-5">
<div class="row">
<div class="col-md-10 mx-auto">
<h1 class="mb-4">🚀 VvvebJs GitHub 高级测试套件</h1>
<!-- 当前配置 -->
<div class="card mb-4">
<div class="card-header">
<h3>📋 测试配置</h3>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<p><strong>GitHub仓库:</strong> <span class="config-value"><?= htmlspecialchars($github['owner']) ?>/<?= htmlspecialchars($github['repo']) ?></span></p>
<p><strong>分支:</strong> <span class="config-value"><?= htmlspecialchars($github['branch']) ?></span></p>
<p><strong>基础路径:</strong> <span class="config-value"><?= htmlspecialchars($github['path']) ?></span></p>
</div>
<div class="col-md-6">
<p><strong>当前用户:</strong> <span class="config-value"><?= htmlspecialchars($currentUser) ?></span></p>
<p><strong>用户路径:</strong> <span class="config-value"><?= htmlspecialchars($userPath) ?></span></p>
<p><strong>Token状态:</strong>
<span class="config-value <?= empty($github['token']) ? 'status-error' : 'status-ok' ?>">
<?= empty($github['token']) ? '❌ 未设置' : '✅ 已配置' ?>
</span>
</p>
</div>
</div>
</div>
</div>
<!-- 测试控制 -->
<div class="card mb-4">
<div class="card-header">
<h3>🧪 运行完整测试</h3>
</div>
<div class="card-body">
<?php if (empty($testResults)): ?>
<div class="alert alert-info">
<h5>📋 测试项目清单</h5>
<ul>
<li><strong>GitHub API连接测试</strong> - 验证能否连接到GitHub API</li>
<li><strong>仓库访问测试</strong> - 确认仓库存在且可访问</li>
<li><strong>分支验证测试</strong> - 检查目标分支是否存在</li>
<li><strong>目录结构测试</strong> - 验证必要的目录结构</li>
<li><strong>文件保存测试</strong> - 测试实际的文件保存功能</li>
<li><strong>文件检索测试</strong> - 验证保存的文件能否正确读取</li>
<li><strong>文件列表测试</strong> - 测试文件列表功能</li>
</ul>
</div>
<form method="POST">
<button type="submit" name="run_full_test" value="1" class="btn btn-primary btn-lg">
🚀 运行完整测试套件
</button>
</form>
<?php else: ?>
<!-- 测试结果总览 -->
<?php if (isset($testResults['summary'])): ?>
<div class="row mb-4">
<div class="col-md-6">
<div class="card <?= $testResults['summary']['overall_success'] ? 'border-success' : 'border-danger' ?>">
<div class="card-body text-center">
<h5>🎯 测试总览</h5>
<div class="display-4 <?= $testResults['summary']['overall_success'] ? 'text-success' : 'text-danger' ?>">
<?= $testResults['summary']['success_rate'] ?>%
</div>
<p class="mb-0">
<?= $testResults['summary']['passed_tests'] ?>/<?= $testResults['summary']['total_tests'] ?> 测试通过
</p>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card">
<div class="card-body">
<h5>📊 整体状态</h5>
<?php if ($testResults['summary']['overall_success']): ?>
<div class="alert alert-success mb-0">
<strong>🎉 全部测试通过!</strong><br>
你的VvvebJs配置完全正常,可以正常使用所有功能。
</div>
<?php else: ?>
<div class="alert alert-warning mb-0">
<strong>⚠️ 部分测试失败</strong><br>
请检查下方的详细测试结果,修复相关问题。
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php endif; ?>
<!-- 详细测试结果 -->
<div class="row">
<!-- API连接测试 -->
<?php if (isset($testResults['api_connection'])): ?>
<div class="col-md-6 mb-3">
<div class="test-result <?= $testResults['api_connection']['success'] ? 'test-success' : 'test-failure' ?>">
<h6>
<span class="<?= $testResults['api_connection']['success'] ? 'status-ok' : 'status-error' ?>">
<?= $testResults['api_connection']['success'] ? '✅' : '❌' ?>
</span>
GitHub API连接
</h6>
<p><strong>状态:</strong> <?= htmlspecialchars($testResults['api_connection']['response']) ?></p>
<p><strong>仓库:</strong> <?= htmlspecialchars($testResults['api_connection']['repo_name']) ?></p>
<p><strong>默认分支:</strong> <?= htmlspecialchars($testResults['api_connection']['default_branch']) ?></p>
</div>
</div>
<?php endif; ?>
<!-- 分支检查 -->
<?php if (isset($testResults['branch_check'])): ?>
<div class="col-md-6 mb-3">
<div class="test-result <?= $testResults['branch_check']['success'] ? 'test-success' : 'test-failure' ?>">
<h6>
<span class="<?= $testResults['branch_check']['success'] ? 'status-ok' : 'status-error' ?>">
<?= $testResults['branch_check']['success'] ? '✅' : '❌' ?>
</span>
分支验证
</h6>
<p><strong>目标分支:</strong> <?= htmlspecialchars($testResults['branch_check']['branch_name']) ?></p>
<p><strong>存在状态:</strong> <?= $testResults['branch_check']['exists'] ? '✅ 存在' : '❌ 不存在' ?></p>
<p><strong>最新提交:</strong> <?= htmlspecialchars(substr($testResults['branch_check']['commit_sha'], 0, 8)) ?>...</p>
</div>
</div>
<?php endif; ?>
<!-- 目录结构 -->
<?php if (isset($testResults['directory_structure'])): ?>
<div class="col-md-12 mb-3">
<div class="test-result test-success">
<h6>📁 目录结构检查</h6>
<div class="row">
<?php foreach ($testResults['directory_structure'] as $key => $dir): ?>
<div class="col-md-4">
<p>
<span class="<?= $dir['exists'] ? 'status-ok' : 'status-warning' ?>">
<?= $dir['exists'] ? '✅' : '⚠️' ?>
</span>
<code><?= htmlspecialchars($dir['path']) ?></code>
</p>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
<?php endif; ?>
<!-- 文件操作测试 -->
<?php if (isset($testResults['file_save'])): ?>
<div class="col-md-6 mb-3">
<div class="test-result <?= $testResults['file_save']['success'] ? 'test-success' : 'test-failure' ?>">
<h6>
<span class="<?= $testResults['file_save']['success'] ? 'status-ok' : 'status-error' ?>">
<?= $testResults['file_save']['success'] ? '✅' : '❌' ?>
</span>
文件保存测试
</h6>
<p><strong>文件名:</strong> <?= htmlspecialchars($testResults['file_save']['filename']) ?></p>
<p><strong>内容长度:</strong> <?= $testResults['file_save']['content_length'] ?> 字节</p>
<p><strong>结果:</strong> <?= htmlspecialchars($testResults['file_save']['message']) ?></p>
<?php if ($testResults['file_save']['success']): ?>
<a href="https://github.com/<?= htmlspecialchars($github['owner']) ?>/<?= htmlspecialchars($github['repo']) ?>/blob/<?= htmlspecialchars($github['branch']) ?>/pages/<?= htmlspecialchars($userPath) ?><?= htmlspecialchars($testResults['file_save']['filename']) ?>"
target="_blank" class="btn btn-sm btn-outline-primary">
GitHub上查看
</a>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<?php if (isset($testResults['file_retrieve'])): ?>
<div class="col-md-6 mb-3">
<div class="test-result <?= $testResults['file_retrieve']['success'] && $testResults['file_retrieve']['content_matches'] ? 'test-success' : 'test-failure' ?>">
<h6>
<span class="<?= $testResults['file_retrieve']['success'] && $testResults['file_retrieve']['content_matches'] ? 'status-ok' : 'status-error' ?>">
<?= $testResults['file_retrieve']['success'] && $testResults['file_retrieve']['content_matches'] ? '✅' : '❌' ?>
</span>
文件检索测试
</h6>
<p><strong>读取状态:</strong> <?= $testResults['file_retrieve']['success'] ? '✅ 成功' : '❌ 失败' ?></p>
<p><strong>内容匹配:</strong> <?= $testResults['file_retrieve']['content_matches'] ? '✅ 一致' : '❌ 不一致' ?></p>
<p><strong>读取长度:</strong> <?= $testResults['file_retrieve']['retrieved_length'] ?> 字节</p>
</div>
</div>
<?php endif; ?>
</div>
<div class="mt-3">
<form method="POST" class="d-inline">
<button type="submit" name="run_full_test" value="1" class="btn btn-secondary">
🔄 重新运行测试
</button>
</form>
</div>
<?php endif; ?>
</div>
</div>
<!-- 操作按钮 -->
<div class="card">
<div class="card-body text-center">
<div class="row">
<div class="col-md-3">
<a href="github-save-debug.php" class="btn btn-warning w-100">保存调试</a>
</div>
<div class="col-md-3">
<a href="directory-creation-test.php" class="btn btn-info w-100">目录创建测试</a>
</div>
<div class="col-md-3">
<a href="config-check.php" class="btn btn-secondary w-100">配置检查</a>
</div>
<div class="col-md-3">
<a href="editor.html" class="btn btn-success w-100">返回编辑器</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>