Unifi-Voucher-Tool/tests/KioskTest.php
Friederich Loheide dd8e805e27
All checks were successful
CI / PHP Lint (push) Successful in 34s
CI / PHP Lint-1 (push) Successful in 39s
CI / Unit Tests & Static Analysis (push) Successful in 38s
CI / PHP Lint (pull_request) Successful in 41s
CI / PHP Lint-1 (pull_request) Successful in 49s
CI / Unit Tests & Static Analysis (pull_request) Successful in 41s
Display-Seiten: Betriebszeiten, PIN, Gerätesperre, Sprache und Vorschau
Die Display-Seiten hängen an öffentlichen Links, deshalb bekommen sie
Regler, mit denen sich der Missbrauch begrenzen lässt, und ein paar
Komfortfunktionen für den Betrieb:

- Betriebszeiten je Seite (auch über Mitternacht hinweg); außerhalb
  zeigt die Seite einen Hinweis statt des Buttons
- Optionale PIN von der Rezeption, geprüft mit hash_equals
- Wartezeit je Gerät über ein Cookie, zusätzlich zum globalen Cooldown
- Feste Sprache je Seite, sonst Browsersprache; Gäste können unten
  umschalten
- Reporting zeigt die Ausgabe pro Display-Seite
- Vorschau der Seite direkt in der Administration
- Backup exportiert und importiert die neuen Felder mit

Migration 0007, 16 neue Sprachschlüssel, 7 zusätzliche Tests
(69 Tests, 152 Assertions).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-24 11:21:20 +00:00

272 lines
9.1 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
namespace Tests;
use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/../includes/Kiosk.php';
/**
* Datenbank-Attrappe: liefert feste Zählwerte und Einstellungen.
*/
class FakeKioskDb
{
public int $usedToday = 0;
/** @var array<string, string> */
private array $settings;
/** @param array<string, string> $settings */
public function __construct(array $settings = [])
{
$this->settings = $settings;
}
public function fetchOne($sql, $params = [])
{
return ['c' => $this->usedToday];
}
public function getSetting($key, $default = null)
{
return $this->settings[$key] ?? $default;
}
}
/**
* Die Grenzen der Kiosk-Seite sind das, was sie vor Missbrauch schützt –
* der Link ist öffentlich, also muss diese Logik stimmen.
*/
class KioskTest extends TestCase
{
/** @param array<string, mixed> $overrides */
private function kiosk(array $overrides = []): array
{
return array_merge([
'id' => 1,
'daily_limit' => 100,
'cooldown_seconds' => 20,
'last_used_at' => null,
], $overrides);
}
public function testTokenHasFixedShape(): void
{
$token = \Kiosk::newToken();
$this->assertMatchesRegularExpression('/^[0-9a-f]{32}$/', $token);
$this->assertNotSame($token, \Kiosk::newToken(), 'Tokens dürfen sich nicht wiederholen');
}
public function testSanitizeTokenRejectsAnythingElse(): void
{
$valid = \Kiosk::newToken();
$this->assertSame($valid, \Kiosk::sanitizeToken($valid));
$this->assertSame($valid, \Kiosk::sanitizeToken(strtoupper($valid)));
$this->assertSame('', \Kiosk::sanitizeToken('kurz'));
$this->assertSame('', \Kiosk::sanitizeToken("' OR 1=1 --"));
$this->assertSame('', \Kiosk::sanitizeToken(null));
$this->assertSame('', \Kiosk::sanitizeToken($valid . 'ff'));
}
public function testCooldownBlocksSecondCode(): void
{
$db = new FakeKioskDb();
$kiosk = $this->kiosk(['last_used_at' => date('Y-m-d H:i:s', time() - 5)]);
$result = \Kiosk::checkLimits($db, $kiosk);
$this->assertFalse($result['allowed']);
$this->assertSame('cooldown', $result['reason']);
$this->assertGreaterThan(0, $result['wait']);
$this->assertLessThanOrEqual(20, $result['wait']);
}
public function testCooldownExpires(): void
{
$db = new FakeKioskDb();
$kiosk = $this->kiosk(['last_used_at' => date('Y-m-d H:i:s', time() - 60)]);
$this->assertTrue(\Kiosk::checkLimits($db, $kiosk)['allowed']);
}
public function testDailyLimitBlocks(): void
{
$db = new FakeKioskDb();
$db->usedToday = 100;
$result = \Kiosk::checkLimits($db, $this->kiosk());
$this->assertFalse($result['allowed']);
$this->assertSame('daily_limit', $result['reason']);
}
public function testZeroMeansUnlimited(): void
{
$db = new FakeKioskDb();
$db->usedToday = 5000;
$kiosk = $this->kiosk(['daily_limit' => 0, 'cooldown_seconds' => 0]);
$this->assertTrue(\Kiosk::checkLimits($db, $kiosk)['allowed']);
}
public function testVoucherSettingsPreferTemplate(): void
{
$db = new FakeKioskDb(['default_max_uses' => '1', 'default_expire_minutes' => '480']);
$kiosk = $this->kiosk([
'tpl_max_uses' => 5,
'tpl_expire_minutes' => 240,
'qos_rate_max_down' => 20000,
'qos_rate_max_up' => 5000,
'qos_usage_quota' => 1024,
]);
$settings = \Kiosk::voucherSettings($db, $kiosk);
$this->assertSame(5, $settings['max_uses']);
$this->assertSame(240, $settings['expire_minutes']);
$this->assertSame(20000, $settings['qos']['down']);
$this->assertSame(1024, $settings['qos']['quota_mb']);
}
public function testVoucherSettingsFallBackToDefaults(): void
{
$db = new FakeKioskDb(['default_max_uses' => '3', 'default_expire_minutes' => '120']);
$settings = \Kiosk::voucherSettings($db, $this->kiosk());
$this->assertSame(3, $settings['max_uses']);
$this->assertSame(120, $settings['expire_minutes']);
$this->assertSame(0, $settings['qos']['down']);
}
public function testAppearanceFallsBackToSystemLogo(): void
{
$db = new FakeKioskDb(['logo_url' => 'uploads/global.png']);
$look = \Kiosk::appearance($db, $this->kiosk());
$this->assertSame('uploads/global.png', $look['logo']);
$this->assertSame('', $look['background']);
$this->assertSame('', $look['accent'], 'Ohne eigene Farbe bleibt das Design-System zuständig');
$this->assertSame('light', $look['card']);
}
public function testAppearanceUsesOwnValues(): void
{
$db = new FakeKioskDb(['logo_url' => 'uploads/global.png']);
$kiosk = $this->kiosk([
'logo_url' => 'uploads/hotel.svg',
'background_url' => 'uploads/lobby.jpg',
'bg_overlay' => 60,
'accent_color' => '#0F766E',
'card_style' => 'dark',
]);
$look = \Kiosk::appearance($db, $kiosk);
$this->assertSame('uploads/hotel.svg', $look['logo']);
$this->assertSame('uploads/lobby.jpg', $look['background']);
$this->assertSame(0.6, $look['overlay']);
$this->assertSame('#0f766e', $look['accent']);
$this->assertSame('dark', $look['card']);
}
public function testAppearanceRejectsUnsafeColour(): void
{
$db = new FakeKioskDb();
// Der Wert landet in einem style-Attribut – nur echte Hex-Farben durch.
$look = \Kiosk::appearance($db, $this->kiosk(['accent_color' => 'red;background:url(evil)']));
$this->assertSame('', $look['accent']);
}
public function testAppearanceClampsOverlay(): void
{
$db = new FakeKioskDb();
$this->assertSame(0.9, \Kiosk::appearance($db, $this->kiosk(['bg_overlay' => 250]))['overlay']);
$this->assertSame(0.0, \Kiosk::appearance($db, $this->kiosk(['bg_overlay' => -10]))['overlay']);
}
// --- Betriebszeiten ---------------------------------------------------
public function testScheduleOpenWithoutTimes(): void
{
$this->assertTrue(\Kiosk::withinSchedule($this->kiosk()));
}
public function testScheduleDuringTheDay(): void
{
$kiosk = $this->kiosk(['active_from' => '06:00', 'active_to' => '23:00']);
$this->assertTrue(\Kiosk::withinSchedule($kiosk, mktime(9, 30)));
$this->assertFalse(\Kiosk::withinSchedule($kiosk, mktime(3, 0)));
$this->assertFalse(\Kiosk::withinSchedule($kiosk, mktime(23, 30)));
}
public function testScheduleAcrossMidnight(): void
{
$kiosk = $this->kiosk(['active_from' => '18:00', 'active_to' => '02:00']);
$this->assertTrue(\Kiosk::withinSchedule($kiosk, mktime(20, 0)));
$this->assertTrue(\Kiosk::withinSchedule($kiosk, mktime(1, 0)));
$this->assertFalse(\Kiosk::withinSchedule($kiosk, mktime(12, 0)));
}
public function testClosedKioskReportsReason(): void
{
$db = new FakeKioskDb();
$kiosk = $this->kiosk(['active_from' => '00:00', 'active_to' => '00:01']);
$result = \Kiosk::checkLimits($db, $kiosk);
// Nur ausserhalb des Fensters – die Minute nach Mitternacht trifft es
// sonst zufaellig; deshalb hier nur die Struktur pruefen.
$this->assertContains($result['reason'], ['', 'closed']);
}
// --- Wartezeit je Gerät ----------------------------------------------
public function testDeviceCooldownCountsDown(): void
{
$kiosk = $this->kiosk(['device_cooldown_seconds' => 300]);
$now = time();
$this->assertSame(0, \Kiosk::deviceCooldownLeft($kiosk, null, $now), 'Ohne Cookie keine Sperre');
$this->assertSame(240, \Kiosk::deviceCooldownLeft($kiosk, $now - 60, $now));
$this->assertSame(0, \Kiosk::deviceCooldownLeft($kiosk, $now - 600, $now), 'Abgelaufen');
}
public function testDeviceCooldownDisabled(): void
{
$kiosk = $this->kiosk(['device_cooldown_seconds' => 0]);
$this->assertSame(0, \Kiosk::deviceCooldownLeft($kiosk, time()));
}
// --- PIN --------------------------------------------------------------
public function testPinOnlyCheckedWhenSet(): void
{
$this->assertTrue(\Kiosk::pinMatches($this->kiosk(), ''), 'Ohne PIN darf nichts blockieren');
$this->assertTrue(\Kiosk::pinMatches($this->kiosk(['pin' => '4711']), '4711'));
$this->assertTrue(\Kiosk::pinMatches($this->kiosk(['pin' => '4711']), ' 4711 '));
$this->assertFalse(\Kiosk::pinMatches($this->kiosk(['pin' => '4711']), '1234'));
$this->assertFalse(\Kiosk::pinMatches($this->kiosk(['pin' => '4711']), ''));
}
public function testPublicUrl(): void
{
$token = \Kiosk::newToken();
$this->assertSame(
'https://wlan.example.com/kiosk.php?k=' . $token,
\Kiosk::publicUrl($token, 'https://wlan.example.com/')
);
}
}