Loheide-eu-mitarbeiterplatt.../tests/Feature/Update/UpdateStateTest.php
Friederich Loheide 43244d751a Self-Updater und Lizenzprüfung
Umsetzung von docs/specs/self-updater.md für Laravel:
- Installierter Stand als SHA in storage/.version, Fortschritt in storage/.update-progress,
  selbstheilender Wartungsmodus in storage/.maintenance
- Update-Client je Kanal (stable/development), nur HTTPS, begrenzte Redirects,
  ZIP-Download per Stream mit PK- und SHA-256-Prüfung
- Entpacken mit zweistufigem ZIP-Slip-Schutz, Übernahme mit geschützten Pfaden
  (atomar je Datei, nie löschen), Migrationen, Cache- und OPcache-Reset
- Lizenzspeicher außerhalb der DB, Geräte-ID als Zufallszahl, rotierendes Gerätetoken
  wird bei jeder Antwort (auch Fehler und Downloads) sofort gespeichert
- Lizenzfehler mit verständlichen Meldungen – sie sperren nie das Portal
- Seite „Aktualisierung“ mit Kanal, Suche, Changelog-Vorschau, Fortschritt,
  Migrationen, Änderungsprotokoll, Lizenz und Datenschutzhinweis
- Wartungs-Middleware (503 + Retry-After) und Auto-Migration per Fingerabdruck
- Audit-Log für Updates, Kanalwechsel und Lizenzänderungen
- Version 0.2.0

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 06:02:19 +00:00

113 lines
3.7 KiB
PHP

<?php
namespace Tests\Feature\Update;
use App\Support\Update\Filesystem;
use App\Support\Update\InstalledVersion;
use App\Support\Update\MaintenanceFlag;
use App\Support\Update\UpdateProgress;
use InvalidArgumentException;
use Tests\TestCase;
class UpdateStateTest extends TestCase
{
private string $dir;
protected function setUp(): void
{
parent::setUp();
$this->dir = sys_get_temp_dir().'/inhaus-state-'.bin2hex(random_bytes(4));
mkdir($this->dir);
}
protected function tearDown(): void
{
Filesystem::deleteDirectory($this->dir);
parent::tearDown();
}
public function test_sha_is_read_and_written(): void
{
$version = new InstalledVersion($this->dir.'/.version');
$this->assertNull($version->current());
$this->assertSame('0.2.0', $version->release());
$this->assertSame('0.2.0', $version->full());
$version->save('A1B2C3D4E5F60718293a4b5c6d7e8f9012345678');
$this->assertSame('a1b2c3d4e5f60718293a4b5c6d7e8f9012345678', $version->current());
$this->assertSame('a1b2c3d', $version->short());
$this->assertSame('0.2.0 (a1b2c3d)', $version->full());
}
public function test_invalid_sha_is_rejected(): void
{
$version = new InstalledVersion($this->dir.'/.version');
foreach (['', 'main', 'a1b2c3d', str_repeat('g', 40), str_repeat('a', 41)] as $invalid) {
try {
$version->save($invalid);
$this->fail("„{$invalid}“ hätte abgelehnt werden müssen.");
} catch (InvalidArgumentException) {
$this->assertNull($version->current());
}
}
file_put_contents($this->dir.'/.version', "kaputt\n");
$this->assertNull($version->current());
}
public function test_maintenance_mode_heals_itself_after_expiry(): void
{
$flag = new MaintenanceFlag($this->dir.'/.maintenance');
$this->assertFalse($flag->isActive());
$flag->activate(600);
$this->assertTrue($flag->isActive());
$flag->deactivate();
$this->assertFalse($flag->isActive());
// Absturz mitten im Update: Flag bleibt liegen, ist aber abgelaufen.
file_put_contents($this->dir.'/.maintenance', json_encode(['since' => time() - 700, 'until' => time() - 100]));
$this->assertFalse($flag->isActive());
$this->assertFileDoesNotExist($this->dir.'/.maintenance');
// Kaputte Datei sperrt ebenfalls nicht.
file_put_contents($this->dir.'/.maintenance', 'Müll');
$this->assertFalse($flag->isActive());
}
public function test_progress_file_including_error_state(): void
{
$progress = new UpdateProgress($this->dir.'/.update-progress');
$this->assertSame(['pct' => 0, 'step' => 'idle', 'text' => '', 'ts' => 0], $progress->get());
$this->assertFalse($progress->isRunning());
$progress->set(20, 'download', 'Lädt …');
$this->assertSame(20, $progress->get()['pct']);
$this->assertSame('Lädt …', $progress->get()['text']);
$this->assertTrue($progress->isRunning());
$progress->fail('Kaputt');
$this->assertSame(-1, $progress->get()['pct']);
$this->assertSame('error', $progress->get()['step']);
$this->assertFalse($progress->isRunning());
}
public function test_stale_run_does_not_block_forever(): void
{
$progress = new UpdateProgress($this->dir.'/.update-progress');
file_put_contents($this->dir.'/.update-progress', json_encode([
'pct' => 50, 'step' => 'extract', 'text' => '', 'ts' => time() - UpdateProgress::STALE_AFTER_SECONDS - 1,
]));
$this->assertFalse($progress->isRunning());
}
}