All checks were successful
CI / PHP Lint (push) Successful in 36s
CI / PHP Lint-1 (push) Successful in 38s
CI / Unit Tests & Static Analysis (push) Successful in 42s
CI / PHP Lint (pull_request) Successful in 36s
CI / PHP Lint-1 (pull_request) Successful in 41s
CI / Unit Tests & Static Analysis (pull_request) Successful in 42s
Übernommen aus dem alten Review-Branch claude/tool-review-optimization-q9nyv5, neu gebaut auf dem aktuellen Stand. Der Rest jenes Branches ist durch das Redesign überholt. Bisher galt CURLOPT_SSL_VERIFYPEER => false für jede Verbindung zum Controller, ohne Möglichkeit, das zu ändern. Wer den Controller unter seinem echten Namen mit gültigem Zertifikat betreibt, kann die Prüfung jetzt pro Site einschalten; Standard bleibt aus, weil UniFi-Geräte ab Werk ein selbst signiertes Zertifikat liefern. - Migration 0008, Spalte sites.ssl_verify (Default 0) - UniFiController: fünfter Konstruktor-Parameter, steuert VERIFYPEER und VERIFYHOST; testConnection() ebenso. Alle acht Aufrufer geben den Wert der Site weiter, tolerant über "?? 0" für Alt-Installationen - Site-Verwaltung: Schalter in beiden Modalen, Abzeichen auf der Karte - Mailer: dasselbe über die Einstellung smtp_verify_ssl, Schalter im SMTP-Tab - Backup nimmt die Spalte mit, Readme erklärt, wann man sie einschaltet - 3 Tests auf den Durchgriff bis cURL (75 Tests, 178 Assertions) Nebenbei: der Aufruf von openEditModal() hat fehlende Spalten bisher als leeres Argument gerendert - jetzt über (int)(... ?? 0) abgesichert. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
189 lines
6.6 KiB
PHP
189 lines
6.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests;
|
|
|
|
use Exception;
|
|
use PHPUnit\Framework\TestCase;
|
|
use UniFiController;
|
|
|
|
require_once __DIR__ . '/../includes/UniFiController.php';
|
|
|
|
/**
|
|
* Controller mit eingespielten Antworten statt echter HTTP-Aufrufe.
|
|
* Die aufgezeichneten Antworten entsprechen dem Format der UniFi-API.
|
|
*/
|
|
class RecordedController extends UniFiController
|
|
{
|
|
/** @var array<int, array> */
|
|
public array $calls = [];
|
|
/** @var array<int, array|null> */
|
|
private array $responses;
|
|
|
|
public function __construct(array $responses)
|
|
{
|
|
parent::__construct('https://unifi.example.com:8443', 'api', 'geheim', 'default');
|
|
$this->responses = $responses;
|
|
}
|
|
|
|
protected function apiRequest($endpoint, $data = null, $method = 'POST')
|
|
{
|
|
$this->calls[] = ['endpoint' => $endpoint, 'data' => $data, 'method' => $method];
|
|
|
|
return array_shift($this->responses);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Die Auswahl des richtigen Vouchers ist der heikle Teil: der Controller
|
|
* liefert alle Voucher unsortiert zurück. Wer blind den ersten nimmt, gibt
|
|
* bei parallelen Anfragen den Code eines anderen Gastes aus.
|
|
*/
|
|
class UniFiControllerTest extends TestCase
|
|
{
|
|
private function created(int $time = 1000): array
|
|
{
|
|
return ['data' => [['create_time' => $time]]];
|
|
}
|
|
|
|
public function testPicksOwnVoucherNotAForeignOne(): void
|
|
{
|
|
$controller = new RecordedController([
|
|
$this->created(),
|
|
['data' => [
|
|
['code' => '1111122222', 'note' => 'Fremder Gast', 'create_time' => 5000, '_id' => 'a'],
|
|
['code' => '3333344444', 'note' => '2026-09-24_Empfang', 'create_time' => 4000, '_id' => 'b'],
|
|
]],
|
|
]);
|
|
|
|
$result = $controller->createVoucher('2026-09-24_Empfang', 2, 480);
|
|
|
|
$this->assertSame('3333344444', $result['code'], 'Der Voucher mit passender Notiz gehört zu dieser Anfrage');
|
|
$this->assertSame('b', $result['unifi_id']);
|
|
}
|
|
|
|
public function testPicksNewestWhenNoteAppearsTwice(): void
|
|
{
|
|
$controller = new RecordedController([
|
|
$this->created(),
|
|
['data' => [
|
|
['code' => '1111122222', 'note' => 'Empfang', 'create_time' => 4000, '_id' => 'alt'],
|
|
['code' => '3333344444', 'note' => 'Empfang', 'create_time' => 9000, '_id' => 'neu'],
|
|
]],
|
|
]);
|
|
|
|
$this->assertSame('neu', $controller->createVoucher('Empfang', 1, 60)['unifi_id']);
|
|
}
|
|
|
|
public function testFallsBackToNewestWhenNoteDoesNotMatch(): void
|
|
{
|
|
$controller = new RecordedController([
|
|
$this->created(),
|
|
['data' => [
|
|
['code' => '1111122222', 'note' => 'Etwas anderes', 'create_time' => 4000, '_id' => 'alt'],
|
|
['code' => '3333344444', 'note' => 'Auch anders', 'create_time' => 9000, '_id' => 'neu'],
|
|
]],
|
|
]);
|
|
|
|
$this->assertSame('neu', $controller->createVoucher('Sonderzeichen …', 1, 60)['unifi_id']);
|
|
}
|
|
|
|
public function testFormatsCodeInFiveDigitBlocks(): void
|
|
{
|
|
$controller = new RecordedController([
|
|
$this->created(),
|
|
['data' => [['code' => '1234567890', 'note' => 'Gast', 'create_time' => 1, '_id' => 'x']]],
|
|
]);
|
|
|
|
$result = $controller->createVoucher('Gast', 1, 60);
|
|
|
|
$this->assertSame('12345-67890', $result['formatted_code']);
|
|
$this->assertSame('1234567890', $result['code'], 'Der rohe Code bleibt unverändert');
|
|
}
|
|
|
|
public function testFailsWhenControllerDoesNotConfirmCreation(): void
|
|
{
|
|
$controller = new RecordedController([['data' => []]]);
|
|
|
|
$this->expectException(Exception::class);
|
|
$controller->createVoucher('Gast', 1, 60);
|
|
}
|
|
|
|
public function testFailsWhenNoVouchersComeBack(): void
|
|
{
|
|
$controller = new RecordedController([$this->created(), ['data' => []]]);
|
|
|
|
$this->expectException(Exception::class);
|
|
$controller->createVoucher('Gast', 1, 60);
|
|
}
|
|
|
|
public function testSendsQosLimitsOnlyWhenSet(): void
|
|
{
|
|
$controller = new RecordedController([
|
|
$this->created(),
|
|
['data' => [['code' => '1234567890', 'note' => 'Gast', 'create_time' => 1, '_id' => 'x']]],
|
|
]);
|
|
|
|
$controller->createVoucher('Gast', 3, 240, ['down' => 20000, 'up' => 5000, 'quota_mb' => 1024]);
|
|
|
|
$payload = $controller->calls[0]['data'];
|
|
$this->assertSame('create-voucher', $payload['cmd']);
|
|
$this->assertSame(240, $payload['expire']);
|
|
$this->assertSame(3, $payload['quota']);
|
|
$this->assertTrue($payload['qos_overwrite']);
|
|
$this->assertSame(20000, $payload['down']);
|
|
$this->assertSame(5000, $payload['up']);
|
|
$this->assertSame(1024, $payload['bytes']);
|
|
}
|
|
|
|
public function testOmitsQosWhenNotRequested(): void
|
|
{
|
|
$controller = new RecordedController([
|
|
$this->created(),
|
|
['data' => [['code' => '1234567890', 'note' => 'Gast', 'create_time' => 1, '_id' => 'x']]],
|
|
]);
|
|
|
|
$controller->createVoucher('Gast', 1, 60);
|
|
|
|
$this->assertArrayNotHasKey('qos_overwrite', $controller->calls[0]['data']);
|
|
$this->assertArrayNotHasKey('down', $controller->calls[0]['data']);
|
|
}
|
|
|
|
/**
|
|
* Die Zertifikatspruefung ist bewusst ein Opt-in: UniFi-Controller
|
|
* liefern ab Werk ein selbst signiertes Zertifikat. Eine neue Site ohne
|
|
* gesetztes Feld darf sich deshalb nicht ploetzlich anders verhalten.
|
|
*/
|
|
public function testCertificateCheckIsOffByDefault(): void
|
|
{
|
|
$controller = new UniFiController('https://unifi.example.com:8443', 'api', 'geheim', 'default');
|
|
|
|
$this->assertFalse($this->sslVerifyOf($controller));
|
|
}
|
|
|
|
public function testCertificateCheckCanBeEnabled(): void
|
|
{
|
|
$controller = new UniFiController('https://unifi.example.com:8443', 'api', 'geheim', 'default', 1);
|
|
|
|
$this->assertTrue($this->sslVerifyOf($controller), 'Der Schalter muss bis zu cURL durchschlagen');
|
|
}
|
|
|
|
/** Werte aus der Datenbank kommen als Strings zurueck. */
|
|
public function testCertificateCheckAcceptsDatabaseValues(): void
|
|
{
|
|
$aus = new UniFiController('https://unifi.example.com:8443', 'api', 'geheim', 'default', '0');
|
|
$an = new UniFiController('https://unifi.example.com:8443', 'api', 'geheim', 'default', '1');
|
|
|
|
$this->assertFalse($this->sslVerifyOf($aus));
|
|
$this->assertTrue($this->sslVerifyOf($an));
|
|
}
|
|
|
|
private function sslVerifyOf(UniFiController $controller): bool
|
|
{
|
|
$property = new \ReflectionProperty(UniFiController::class, 'sslVerify');
|
|
$property->setAccessible(true);
|
|
|
|
return (bool)$property->getValue($controller);
|
|
}
|
|
}
|