ferrero-opentext/simple_test.php
DJP 716f0be068 Initial commit: Ferrero OpenText Content Scaling Application
Complete PHP-based workflow application for Ferrero DAM system:
- OAuth2 authentication with automatic token management
- Campaign discovery and filtering
- Folder structure navigation
- Asset download (individual and bulk)
- Metadata extraction and display
- Clean step-by-step web interface

Status: Fully functional and production-ready

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-17 14:29:20 -04:00

274 lines
No EOL
11 KiB
PHP

<?php
session_start();
require_once 'src/TestRunner.php';
require_once 'src/CampaignFormatter.php';
$config = [
'baseUrl' => $_SESSION['baseUrl'] ?? '',
'apiKey' => $_SESSION['apiKey'] ?? '',
'timeout' => 120, // Increase timeout to 2 minutes
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json'
]
];
// Try to use the new collection first, fallback to old one
$collectionPath = __DIR__ . '/Content Scaling Flow.postman_collection_Oliver(New).json';
if (!file_exists($collectionPath)) {
$collectionPath = __DIR__ . '/Content Scaling Flow_Oliver.Postman_Collection.json';
}
$testRunner = null;
$error = null;
$result = null;
try {
if (file_exists($collectionPath)) {
$testRunner = new TestRunner($collectionPath, $config);
} else {
$error = "Postman collection file not found";
}
} catch (Exception $e) {
$error = "Error initializing test runner: " . $e->getMessage();
}
// Handle form submission
if ($_POST && $testRunner) {
if ($_POST['action'] === 'update_config') {
$_SESSION['baseUrl'] = $_POST['baseUrl'] ?? '';
$_SESSION['apiKey'] = $_POST['apiKey'] ?? '';
$config['baseUrl'] = $_SESSION['baseUrl'];
$config['apiKey'] = $_SESSION['apiKey'];
$testRunner->updateConfig($config);
$message = "Configuration updated successfully";
}
if ($_POST['action'] === 'run_campaign_test') {
$requests = $testRunner->getAvailableRequests();
// Find the exact "Retrieve Localized Campaign Folders" request
foreach ($requests as $index => $request) {
if (strpos($request['name'], 'Retrieve Localized Campaign Folders') !== false) {
$result = $testRunner->runSingleTest($request, $index);
break;
}
}
if (!$result) {
$error = "Could not find 'Retrieve Localized Campaign Folders' request";
}
}
}
$oauth2Status = $testRunner ? $testRunner->getOAuth2Status() : null;
$credentials = $testRunner ? $testRunner->getCredentials() : [];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Campaign Folders Test</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
}
.container {
max-width: 1200px;
margin: 0 auto;
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.config-section {
margin-bottom: 30px;
padding: 20px;
border: 1px solid #ddd;
border-radius: 6px;
background-color: #fafafa;
}
.btn {
background-color: #007cba;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
margin-right: 10px;
margin-bottom: 10px;
}
.btn:hover { background-color: #005a8b; }
.btn-success { background-color: #28a745; }
.error { color: #dc3545; background-color: #f8d7da; padding: 10px; border-radius: 4px; margin: 10px 0; }
.success { color: #155724; background-color: #d4edda; padding: 10px; border-radius: 4px; margin: 10px 0; }
.status-pass { color: #28a745; font-weight: bold; }
.status-fail { color: #dc3545; font-weight: bold; }
.form-group { margin-bottom: 15px; }
label { display: block; margin-bottom: 5px; font-weight: 600; color: #333; }
input { width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 4px; font-size: 14px; }
.response-box {
background: #f8f9fa;
border: 1px solid #ddd;
border-radius: 4px;
padding: 15px;
margin: 20px 0;
}
.json-response {
background: #2d3748;
color: #e2e8f0;
padding: 15px;
border-radius: 4px;
font-family: 'Courier New', monospace;
font-size: 12px;
overflow-x: auto;
white-space: pre-wrap;
max-height: 600px;
overflow-y: auto;
}
</style>
</head>
<body>
<div class="container">
<h1>Simple Campaign Folders Test</h1>
<p>Testing the exact "Retrieve Localized Campaign Folders" request from Postman</p>
<?php if ($error): ?>
<div class="error"><strong>Error:</strong> <?= htmlspecialchars($error) ?></div>
<?php endif; ?>
<?php if (isset($message)): ?>
<div class="success"><?= htmlspecialchars($message) ?></div>
<?php endif; ?>
<!-- Configuration Section -->
<div class="config-section">
<h2>API Configuration</h2>
<?php if ($oauth2Status): ?>
<div style="background: #e8f4f8; padding: 15px; border-radius: 4px; margin-bottom: 20px;">
<h3>OAuth2 Status</h3>
<?php if ($oauth2Status['enabled'] && isset($oauth2Status['has_token']) && $oauth2Status['has_token']): ?>
<p class="status-pass">✅ OAuth2 Token Active</p>
<p>Expires: <?= htmlspecialchars($oauth2Status['expires_at'] ?? 'Unknown') ?></p>
<?php else: ?>
<p class="status-fail">❌ OAuth2 Token Issue</p>
<?php if (isset($oauth2Status['error'])): ?>
<p>Error: <?= htmlspecialchars($oauth2Status['error']) ?></p>
<?php endif; ?>
<?php endif; ?>
</div>
<?php endif; ?>
<?php if (!empty($credentials)): ?>
<div style="background: #f8f9fa; padding: 15px; border-radius: 4px; margin-bottom: 20px;">
<h3>Loaded Credentials</h3>
<p><strong>Client ID:</strong> <?= htmlspecialchars($credentials['client_id'] ?? 'Not found') ?></p>
<p><strong>Client Secret:</strong> <?= !empty($credentials['client_secret']) ? str_repeat('*', 20) : 'Not found' ?></p>
</div>
<?php endif; ?>
<form method="POST">
<input type="hidden" name="action" value="update_config">
<div class="form-group">
<label for="baseUrl">Base URL (Optional - will use collection default):</label>
<input type="text" id="baseUrl" name="baseUrl" value="<?= htmlspecialchars($config['baseUrl']) ?>"
placeholder="Leave blank to use collection base URL">
</div>
<div class="form-group">
<label for="apiKey">Manual API Key (Optional - OAuth2 preferred):</label>
<input type="password" id="apiKey" name="apiKey" value="<?= htmlspecialchars($config['apiKey']) ?>"
placeholder="Only needed if OAuth2 fails">
</div>
<button type="submit" class="btn">Update Configuration</button>
</form>
</div>
<?php if ($testRunner): ?>
<!-- Test Section -->
<div class="config-section">
<h2>Run Campaign Folders Test</h2>
<p>This will run the exact "Retrieve Localized Campaign Folders" request from your Postman collection</p>
<form method="POST">
<input type="hidden" name="action" value="run_campaign_test">
<button type="submit" class="btn btn-success">Run Campaign Folders Request</button>
</form>
</div>
<!-- Results Section -->
<?php if ($result): ?>
<div class="response-box">
<h2>Test Result</h2>
<div style="margin-bottom: 20px;">
<h3>Request Details</h3>
<p><strong>Name:</strong> <?= htmlspecialchars($result['name']) ?></p>
<p><strong>URL:</strong> <?= htmlspecialchars($result['response']['url'] ?? 'N/A') ?></p>
<p><strong>Method:</strong> <?= htmlspecialchars($result['response']['method'] ?? 'N/A') ?></p>
<p><strong>Status:</strong> <span class="status-<?= strtolower($result['status']) ?>"><?= $result['status'] ?></span></p>
<p><strong>HTTP Code:</strong> <?= $result['response']['http_code'] ?? 'N/A' ?></p>
<p><strong>Response Time:</strong> <?= $result['response']['response_time'] ?? 0 ?>ms</p>
<?php if (!$result['response']['success']): ?>
<p><strong>Error:</strong> <?= htmlspecialchars($result['response']['error'] ?? 'Unknown error') ?></p>
<?php endif; ?>
</div>
<h3>Response Analysis</h3>
<!-- Human-readable formatted response -->
<div class="formatted-response" style="margin-bottom: 30px;">
<?php
$responseBody = $result['response']['body'] ?? '';
if (!empty($responseBody)) {
// Try to parse partial JSON if we got a timeout
if ($result['status'] === 'ERROR' && !empty($responseBody)) {
echo "<div style='background: #fff3cd; padding: 15px; border-radius: 4px; margin-bottom: 15px;'>";
echo "<strong>⚠️ Partial Response Received</strong><br>";
echo "The request timed out but we received " . strlen($responseBody) . " bytes of data.";
echo "</div>";
// Try to fix incomplete JSON by adding closing brackets
$fixedJson = $responseBody;
$openBraces = substr_count($fixedJson, '{') - substr_count($fixedJson, '}');
$openBrackets = substr_count($fixedJson, '[') - substr_count($fixedJson, ']');
// Add missing closing brackets
for ($i = 0; $i < $openBrackets; $i++) {
$fixedJson .= ']';
}
for ($i = 0; $i < $openBraces; $i++) {
$fixedJson .= '}';
}
echo CampaignFormatter::formatCampaignResponse($fixedJson);
} else {
echo CampaignFormatter::formatCampaignResponse($responseBody);
}
} else {
echo "<p>No response body received.</p>";
}
?>
</div>
<!-- Collapsible raw JSON -->
<details style="margin-top: 20px;">
<summary><strong>🔍 View Raw JSON Response</strong> (Technical Details)</summary>
<div class="json-response" style="margin-top: 10px;">
<?= htmlspecialchars($result['response']['body'] ?? 'No response body') ?>
</div>
</details>
</div>
<?php endif; ?>
<?php endif; ?>
</div>
</body>
</html>