getPathname(), strlen($root) + 1)); if (preg_match($skip, $rel) && !preg_match('#^(backups|uploads|plugins)/(\.htaccess|README\.md)$#', $rel)) continue; if ($f->isDir()) { @mkdir($tmp . '/' . $rel, 0755, true); continue; } @mkdir(dirname($tmp . '/' . $rel), 0755, true); copy($f->getPathname(), $tmp . '/' . $rel); } @mkdir($tmp . '/uploads'); @mkdir($tmp . '/config'); file_put_contents($tmp . '/config/config.php', ' bin2hex(random_bytes(32)), 'db' => ['driver' => 'sqlite', 'sqlite_path' => $tmp . '/database/deskhub.sqlite'], 'site' => ['name' => 'Test', 'url' => 'http://127.0.0.1', 'admin_email' => 'a@test.de', 'timezone' => 'Europe/Berlin'], 'uploads' => ['max_size_bytes' => 10485760, 'allowed_extensions' => ['png'], 'path' => $tmp . '/uploads'], ], true) . ';'); putenv('DESKHUB_CONFIG_JSON'); putenv('DESKHUB_SQLITE_PATH'); putenv('DESKHUB_TESTING'); putenv('DESKHUB_API_ALLOW_INSECURE=1'); require_once $root . '/database/sqlite_schema.php'; require_once $root . '/database/migrate.php'; $db = new PDO('sqlite:' . $tmp . '/database/deskhub.sqlite'); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $db->exec('PRAGMA foreign_keys = ON'); $db->exec(convert_schema_to_sqlite((string) file_get_contents($root . '/database/schema.sql'))); $mig = run_migrations($db, true); $now = date('Y-m-d H:i:s'); $dept = (int) $db->query('SELECT id FROM departments ORDER BY id LIMIT 1')->fetchColumn(); $db->exec('DELETE FROM staff'); $ins = $db->prepare('INSERT INTO staff (name, email, password_hash, role, department_id, is_active, created_at) VALUES (?,?,?,?,?,1,?)'); $ids = []; foreach ([['Chef', 'chef@test.de', 'admin'], ['Agent', 'agent@test.de', 'agent'], ['Pfleger', 'pfleger@test.de', 'agent']] as [$n, $e, $r]) { $ins->execute([$n, $e, password_hash('Sehr-Sicher-1234', PASSWORD_DEFAULT), $r, $dept, $now]); $ids[$n] = (int) $db->lastInsertId(); } $db = null; $failures = []; $total = 0; $check = function (bool $ok, string $what) use (&$failures, &$total): void { $total++; if (!$ok) $failures[] = $what; }; $webProc = proc_open([PHP_BINARY, '-S', "127.0.0.1:$webPort", '-t', $tmp], [['pipe', 'r'], ['file', $work . '/web.out', 'w'], ['file', $work . '/web.out', 'a']], $wp, $tmp); try { for ($i = 0; $i < 50; $i++) { if (@fsockopen('127.0.0.1', $webPort)) break; usleep(100000); } $pdo = function () use ($tmp): PDO { $p = new PDO('sqlite:' . $tmp . '/database/deskhub.sqlite'); $p->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $p->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); return $p; }; $api = function (string $method, string $path, ?array $json = null, ?string $bearer = null) use ($webPort): array { $ch = curl_init("http://127.0.0.1:$webPort$path"); $h = ['Content-Type: application/json']; if ($bearer) $h[] = 'Authorization: Bearer ' . $bearer; curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 60, CURLOPT_HTTPHEADER => $h, CURLOPT_CUSTOMREQUEST => $method]); if ($json !== null) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($json)); $b = (string) curl_exec($ch); $s = (int) curl_getinfo($ch, CURLINFO_RESPONSE_CODE); curl_close($ch); return [$s, json_decode($b, true) ?? $b]; }; $loginApi = function (string $email) use ($api): string { [, $r] = $api('POST', '/api/staff/v1/login.php', ['email' => $email, 'password' => 'Sehr-Sicher-1234', 'deviceName' => 'Test']); return (string) ($r['token'] ?? ''); }; $db = $pdo(); $db->prepare('INSERT INTO roles (name, permissions) VALUES (?,?)')->execute(['Assistent-Pfleger', json_encode(['reply_ticket' => true, 'manage_assistant' => true])]); $db->prepare('UPDATE staff SET role_id = ? WHERE email = ?')->execute([(int) $db->lastInsertId(), 'pfleger@test.de']); $chef = $loginApi('chef@test.de'); $agent = $loginApi('agent@test.de'); $pfleger = $loginApi('pfleger@test.de'); $check($chef !== '' && $agent !== '' && $pfleger !== '', 'Anmeldung der drei Mitarbeiter'); // ── Rechte ───────────────────────────────────────────────────────────── foreach (['GET /api/staff/v1/assistant.php?section=overview', 'GET /api/staff/v1/assistant.php?section=answers', 'GET /api/staff/v1/assistant.php?section=log'] as $call) { [$m, $p] = explode(' ', $call); [$s] = $api($m, $p, null, $agent); $check($s === 403, "Ohne Recht: $p wird abgelehnt (HTTP $s)"); } foreach (['save_settings', 'save_answer', 'delete_answer', 'save_synonyms', 'clear_log', 'test'] as $a) { [$s] = $api('POST', '/api/staff/v1/assistant.php', ['action' => $a, 'title' => 'x', 'answer' => 'y', 'id' => 1, 'question' => 'x'], $agent); $check($s === 403, "Ohne Recht: Aktion $a wird abgelehnt (HTTP $s)"); } [$s] = $api('GET', '/api/staff/v1/assistant.php?section=overview'); $check($s === 401, 'Ohne Anmeldung: HTTP 401'); [, $me] = $api('GET', '/api/staff/v1/me.php', null, $pfleger); $check(($me['staff']['permissions']['manage_assistant'] ?? false) === true, 'Die App erfährt das Recht „manage_assistant“ über me.php'); [, $me] = $api('GET', '/api/staff/v1/me.php', null, $agent); $check(($me['staff']['permissions']['manage_assistant'] ?? true) === false, '… und bei Mitarbeitern ohne das Recht als „nein“'); // ── Einstellungen ────────────────────────────────────────────────────── [$s, $o] = $api('GET', '/api/staff/v1/assistant.php?section=overview', null, $pfleger); $check($s === 200 && $o['dbReady'] === true && $o['settings']['enabled'] === false && $o['settings']['logDays'] === 90 && !empty($o['defaults']['greeting']) && count($o['departments']) >= 1 && count($o['priorities']) >= 1, 'Mit dem Recht: Übersicht mit Einstellungen, Vorgaben, Abteilungen und Prioritäten (HTTP ' . $s . ')'); $deptId = (int) $o['departments'][0]['id']; [$s] = $api('POST', '/api/staff/v1/assistant.php', ['action' => 'save_settings', 'enabled' => true, 'mode' => 'always', 'name' => 'Hilfe-Bot ', 'greeting' => 'Moin!', 'noAnswer' => 'Nichts gefunden.', 'departmentId' => $deptId, 'priorityId' => 999999, 'logDays' => 30], $pfleger); [, $o] = $api('GET', '/api/staff/v1/assistant.php?section=overview', null, $chef); $st = $o['settings']; $check($s === 200 && $st['enabled'] === true && $st['mode'] === 'always' && $st['name'] === 'Hilfe-Bot' && $st['greeting'] === 'Moin!' && $st['departmentId'] === $deptId && $st['priorityId'] === 0 && $st['logDays'] === 30 && $o['state']['activeNow'] === true, 'Einstellungen speichern: HTML entfernt, unbekannte Priorität ignoriert, Zustand „aktiv“ ' . json_encode($st)); $check((int) $pdo()->query("SELECT COUNT(*) FROM admin_audit_log WHERE action LIKE 'Einstellungen des digitalen Assistenten%(App)'")->fetchColumn() === 1, 'Die Änderung steht im Admin-Protokoll'); // ── Antworten ────────────────────────────────────────────────────────── [$s] = $api('POST', '/api/staff/v1/assistant.php', ['action' => 'save_answer', 'title' => '', 'answer' => 'x'], $pfleger); $check($s === 422, 'Antwort ohne Titel wird abgelehnt (HTTP ' . $s . ')'); [$s, $r] = $api('POST', '/api/staff/v1/assistant.php', ['action' => 'save_answer', 'title' => 'Wie lange dauert der Versand?', 'keywords' => 'Lieferzeit, Paket', 'answer' => 'Zwei bis drei Werktage.', 'isActive' => true], $pfleger); $aid = (int) ($r['item']['id'] ?? 0); $check($s === 200 && $aid > 0 && $r['item']['isActive'] === true, 'Antwort anlegen (HTTP ' . $s . ')'); [, $r] = $api('POST', '/api/staff/v1/assistant.php', ['action' => 'save_answer', 'id' => $aid, 'title' => 'Wie lange dauert der Versand?', 'keywords' => 'Lieferzeit, Paket, Lieferung', 'answer' => 'Zwei bis drei Werktage.', 'isActive' => true], $chef); $check(($r['item']['keywords'] ?? '') === 'Lieferzeit, Paket, Lieferung', 'Antwort ändern'); [, $l] = $api('GET', '/api/staff/v1/assistant.php?section=answers', null, $pfleger); $check(count($l['items'] ?? []) === 1 && $l['items'][0]['hits'] === 0, 'Liste der Antworten'); [, $t] = $api('POST', '/api/staff/v1/assistant.php', ['action' => 'test', 'question' => 'Wie lange dauert die Lieferung meines Pakets?'], $pfleger); $check(($t['found'] ?? false) === true && ($t['result']['source'] ?? '') === 'custom' && str_contains($t['result']['text'] ?? '', 'Werktage'), 'Frage ausprobieren findet die Antwort'); [, $l] = $api('GET', '/api/staff/v1/assistant.php?section=answers', null, $pfleger); $check($l['items'][0]['hits'] === 0, 'Das Ausprobieren zählt nicht als Nutzung'); [, $t] = $api('POST', '/api/staff/v1/assistant.php', ['action' => 'test', 'question' => 'Wie wird das Wetter morgen?'], $pfleger); $check(($t['found'] ?? true) === false, 'Ohne Treffer: found = false'); // ── Synonyme ─────────────────────────────────────────────────────────── [$s, $r] = $api('POST', '/api/staff/v1/assistant.php', ['action' => 'save_synonyms', 'text' => "Versand, Zustellung, Auslieferung\nnur"], $pfleger); $check($s === 200 && $r['groups'] === 1, 'Synonyme speichern: eine gültige Gruppe'); [, $t] = $api('POST', '/api/staff/v1/assistant.php', ['action' => 'test', 'question' => 'Wann ist die Zustellung?'], $pfleger); $check(($t['found'] ?? false) === true, 'Die eigene Gruppe wirkt sofort (Zustellung findet „Versand“)'); // ── Log ──────────────────────────────────────────────────────────────── $now = date('Y-m-d H:i:s'); $ins = $pdo()->prepare('INSERT INTO assistant_log (question, result, source, matched_title, helpful, created_at) VALUES (?,?,?,?,?,?)'); $ins->execute(['Wo ist mein Paket?', 'custom', 'custom', 'Wie lange dauert der Versand?', 1, $now]); $ins->execute(['Wie kündige ich?', 'none', null, null, null, $now]); $ins->execute(['Wie kündige ich?', 'none', null, null, null, $now]); $ins->execute(['Brauche Rechnung', 'kb', 'kb', 'Rechnungen', 0, $now]); [$s, $g] = $api('GET', '/api/staff/v1/assistant.php?section=log&period=all', null, $pfleger); $check($s === 200 && $g['total'] === 4 && $g['stats']['none'] === 2 && $g['stats']['helpful'] === 1 && $g['stats']['not_helpful'] === 1 && ($g['top'][0]['n'] ?? 0) === 2, 'Log: Einträge, Kennzahlen und häufigste Frage ohne Antwort ' . json_encode($g['stats'] ?? $g)); [, $g] = $api('GET', '/api/staff/v1/assistant.php?section=log&period=all&result=none', null, $pfleger); $check($g['total'] === 2, 'Log: Filter „Keine Antwort“'); [, $g] = $api('GET', '/api/staff/v1/assistant.php?section=log&period=all&result=helpful', null, $pfleger); $check($g['total'] === 1 && $g['items'][0]['helpful'] === true, 'Log: Filter „Hat geholfen“'); [, $g] = $api('GET', '/api/staff/v1/assistant.php?section=log&period=all&q=Paket', null, $pfleger); $check($g['total'] === 1, 'Log: Suche'); [$s] = $api('POST', '/api/staff/v1/assistant.php', ['action' => 'clear_log'], $pfleger); $check($s === 200 && (int) $pdo()->query('SELECT COUNT(*) FROM assistant_log')->fetchColumn() === 0, 'Log leeren'); // ── Löschen ──────────────────────────────────────────────────────────── [$s] = $api('POST', '/api/staff/v1/assistant.php', ['action' => 'delete_answer', 'id' => $aid], $pfleger); $check($s === 200 && (int) $pdo()->query('SELECT COUNT(*) FROM assistant_answers')->fetchColumn() === 0, 'Antwort löschen'); [$s] = $api('POST', '/api/staff/v1/assistant.php', ['action' => 'delete_answer', 'id' => $aid], $pfleger); $check($s === 404, 'Nochmal löschen: HTTP 404'); [$s] = $api('POST', '/api/staff/v1/assistant.php', ['action' => 'unbekannt'], $pfleger); $check($s === 422, 'Unbekannte Aktion: HTTP 422'); } finally { if (isset($webProc) && is_resource($webProc)) { proc_terminate($webProc); proc_close($webProc); } } usleep(300000); $rm = function (string $d) use (&$rm) { foreach (scandir($d) ?: [] as $e) { if ($e === '.' || $e === '..') continue; is_dir("$d/$e") && !is_link("$d/$e") ? $rm("$d/$e") : @unlink("$d/$e"); } @rmdir($d); }; if (str_starts_with($work, sys_get_temp_dir() . '/deskhub-assistapi-')) $rm($work); if ($failures) { fwrite(STDERR, "Fehlgeschlagen:\n - " . implode("\n - ", $failures) . "\n"); exit(1); } echo "Assistant API checks passed ({$total} Prüfungen).\n";