pdo = new PDO('sqlite::memory:'); $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->pdo->exec('CREATE TABLE settings (id INTEGER PRIMARY KEY AUTOINCREMENT, setting TEXT NOT NULL UNIQUE, value TEXT NOT NULL, is_array INTEGER NOT NULL DEFAULT 0)'); $this->databaseName = 'tracking_test'; } } class TrackingTestRedirect extends RuntimeException { public function __construct(public string $url, public array $flashData) { parent::__construct('Redirect requested.'); } } class TrackingTestAuthenticationRequired extends RuntimeException { } function phpb_requires_authenticated(): void { if (!($GLOBALS['trackingTestAuthenticated'] ?? false)) { throw new TrackingTestAuthenticationRequired(); } } function phpb_validate_csrf(): bool { return $GLOBALS['trackingTestCsrfValid'] ?? false; } function phpb_e(string $value): string { return htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); } function phpb_csrf_field(): void { echo ''; } function phpb_full_url(string $path): string { return '/plugi' . $path; } function phpb_url(string $module, array $parameters = []): string { return $module . '?' . http_build_query($parameters); } function phpb_redirect(string $url, array $flashData = [], int $statusCode = 302): void { throw new TrackingTestRedirect($url, $flashData); } require __DIR__ . '/../extension.php'; if (realpath(\Plugi\Extensions::getRoute('/ext--tracking/save')) !== realpath(__DIR__ . '/../routes/save.php')) { throw new RuntimeException('The Tracking save route should be registered.'); } if (realpath(\Plugi\Extensions::getBackends()['ext--tracking']['include'] ?? '') !== realpath(__DIR__ . '/../admin/tracking.php')) { throw new RuntimeException('The Tracking Website Manager backend should be registered.'); } $_ENV['DATABASE_PREFIX'] = ''; $phpb_db = new TrackingTestDatabase(); ob_start(); require __DIR__ . '/../admin/tracking.php'; $trackingEditor = ob_get_clean(); if (!str_contains($trackingEditor, 'action="/plugi/ext--tracking/save"')) { throw new RuntimeException('The Tracking editor should post to its registered extension route.'); } if (($argv[1] ?? null) === 'csrf-rejection') { $snippet = ''; (new \Plugi\Repositories\SettingRepository())->updateSettings([ 'ext--tracking--head' => $snippet, ]); $GLOBALS['trackingTestAuthenticated'] = true; $GLOBALS['trackingTestCsrfValid'] = false; $_SERVER['REQUEST_METHOD'] = 'POST'; $_POST = ['snippet' => '']; register_shutdown_function(static function () use ($phpb_db, $snippet): void { if (http_response_code() !== 403) { fwrite(STDERR, "Invalid CSRF should return HTTP 403.\n"); exit(1); } $saved = $phpb_db->select('SELECT value FROM settings WHERE setting = ?', ['ext--tracking--head']); if (($saved[0]['value'] ?? null) !== $snippet) { fwrite(STDERR, "Invalid CSRF must not change the saved snippet.\n"); exit(1); } fwrite(STDOUT, "PASS: authenticated invalid-CSRF requests are rejected without changing the saved snippet.\n"); }); require __DIR__ . '/../routes/save.php'; throw new RuntimeException('Authenticated requests with invalid CSRF should be rejected.'); } if (\Plugi\Extensions::getHtmlBlocks('head') !== '') { throw new RuntimeException('An unset tracking snippet should add no head output.'); } $GLOBALS['trackingTestAuthenticated'] = true; $GLOBALS['trackingTestCsrfValid'] = true; $_SERVER['REQUEST_METHOD'] = 'POST'; $snippet = ""; $_POST = ['snippet' => $snippet]; try { require __DIR__ . '/../routes/save.php'; } catch (TrackingTestRedirect $redirect) { if ($redirect->url !== 'website_manager?tab=ext--tracking' || $redirect->flashData['message-type'] !== 'success') { throw new RuntimeException('Saving should redirect back to the Tracking editor with success feedback.'); } } $saved = $phpb_db->select('SELECT value FROM settings WHERE setting = ?', ['ext--tracking--head']); if (($saved[0]['value'] ?? null) !== $snippet) { throw new RuntimeException('The submitted snippet should be persisted unchanged.'); } $settingsProperty = new ReflectionProperty(\Plugi\Setting::class, 'settings'); $settingsProperty->setValue(null, null); if (\Plugi\Extensions::getHtmlBlocks('head') !== $snippet) { throw new RuntimeException('The saved tracking snippet should be emitted unchanged in head output.'); } $GLOBALS['trackingTestAuthenticated'] = false; $_POST['snippet'] = ''; try { require __DIR__ . '/../routes/save.php'; throw new RuntimeException('Unauthenticated requests should be rejected.'); } catch (TrackingTestAuthenticationRequired) { } $saved = $phpb_db->select('SELECT value FROM settings WHERE setting = ?', ['ext--tracking--head']); if (($saved[0]['value'] ?? null) !== $snippet) { throw new RuntimeException('Unauthenticated requests must not change the saved snippet.'); } $process = proc_open([PHP_BINARY, __FILE__, 'csrf-rejection'], [ 0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w'], ], $pipes); if (!is_resource($process)) { throw new RuntimeException('Could not start the invalid-CSRF route test.'); } fclose($pipes[0]); $output = stream_get_contents($pipes[1]); $error = stream_get_contents($pipes[2]); fclose($pipes[1]); fclose($pipes[2]); $status = proc_close($process); if ($status !== 0 || !str_contains($output, 'PASS: authenticated invalid-CSRF requests are rejected')) { throw new RuntimeException(sprintf('The invalid-CSRF route test failed (status %d): %s%s', $status, $output, $error)); } fwrite(STDOUT, "PASS: tracking snippets are saved unchanged; authentication and CSRF are required.\n");