<?php
/**
 * 测试PDF目录创建修复
 * 在服务器上运行: php test_directory_fix.php
 */

// 测试1: 检查storage目录结构
echo "=== 测试1: 检查storage目录结构 ===\n";
$storagePath = __DIR__ . '/storage';
$publicPath = $storagePath . '/app/public';
$examsPath = $publicPath . '/exams';

echo "Storage路径: {$storagePath}\n";
echo "Public路径: {$publicPath}\n";
echo "Exams路径: {$examsPath}\n\n";

// 检查目录是否存在
echo "=== 测试2: 检查目录存在性 ===\n";
echo "storage存在: " . (is_dir($storagePath) ? "是" : "否") . "\n";
echo "app存在: " . (is_dir($storagePath . '/app') ? "是" : "否") . "\n";
echo "public存在: " . (is_dir($publicPath) ? "是" : "否") . "\n";
echo "exams存在: " . (is_dir($examsPath) ? "是" : "否") . "\n\n";

// 测试3: 创建exams目录
echo "=== 测试3: 创建exams目录 ===\n";
if (!is_dir($examsPath)) {
    echo "exams目录不存在，正在创建...\n";
    if (mkdir($examsPath, 0755, true)) {
        echo "✓ exams目录创建成功\n";
        echo "权限: " . substr(sprintf('%o', fileperms($examsPath)), -4) . "\n";
    } else {
        echo "✗ exams目录创建失败\n";
        echo "错误: " . error_get_last()['message'] . "\n";
    }
} else {
    echo "✓ exams目录已存在\n";
    echo "权限: " . substr(sprintf('%o', fileperms($examsPath)), -4) . "\n";
}

// 测试4: 测试文件写入
echo "\n=== 测试4: 测试文件写入 ===\n";
$testFile = $examsPath . '/test_write.txt';
$testContent = '测试时间: ' . date('Y-m-d H:i:s') . "\n";
if (file_put_contents($testFile, $testContent)) {
    echo "✓ 文件写入成功: {$testFile}\n";
    if (file_exists($testFile)) {
        echo "✓ 文件验证存在\n";
        @unlink($testFile);
        echo "✓ 测试文件已删除\n";
    }
} else {
    echo "✗ 文件写入失败\n";
    echo "错误: " . error_get_last()['message'] . "\n";
}

// 测试5: 检查Laravel存储路径函数
echo "\n=== 测试5: 检查Laravel存储路径函数 ===\n";
require_once __DIR__ . '/vendor/autoload.php';

try {
    $app = require_once __DIR__ . '/bootstrap/app.php';
    $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

    // 测试storage_path函数
    $storageAppPath = storage_path('app');
    $storagePublicPath = storage_path('app/public');
    $storageExamsPath = storage_path('app/public/exams');

    echo "storage_path('app'): {$storageAppPath}\n";
    echo "storage_path('app/public'): {$storagePublicPath}\n";
    echo "storage_path('app/public/exams'): {$storageExamsPath}\n\n";

    echo "=== 测试6: 使用Laravel函数创建目录 ===\n";
    if (!is_dir($storageExamsPath)) {
        echo "exams目录不存在，正在创建...\n";
        if (mkdir($storageExamsPath, 0755, true)) {
            echo "✓ Laravel路径下exams目录创建成功\n";
            echo "完整路径: {$storageExamsPath}\n";
        } else {
            echo "✗ Laravel路径下exams目录创建失败\n";
        }
    } else {
        echo "✓ Laravel路径下exams目录已存在\n";
    }

    // 测试文件写入
    $testFile2 = $storageExamsPath . '/laravel_test.txt';
    if (file_put_contents($testFile2, $testContent)) {
        echo "✓ Laravel路径下文件写入成功\n";
        @unlink($testFile2);
        echo "✓ 测试文件已删除\n";
    }

} catch (Exception $e) {
    echo "✗ Laravel初始化失败: " . $e->getMessage() . "\n";
    echo "注意: 如果没有Laravel应用，这是正常的\n";
}

echo "\n=== 测试完成 ===\n";
echo "如果所有测试都通过，说明目录创建功能正常\n";
echo "如果exams目录创建失败，请检查:\n";
echo "1. storage/app/public目录权限\n";
echo "2. PHP进程是否有写入权限\n";
echo "3. SELinux或其他安全策略\n";