tmp = sys_get_temp_dir() . '/uvt-updater-' . bin2hex(random_bytes(6)); mkdir($this->tmp, 0775, true); Version::useStorageDir($this->tmp . '/storage'); mkdir($this->tmp . '/storage', 0775, true); } protected function tearDown(): void { Version::useStorageDir(null); $this->removeTree($this->tmp); } // ------------------------------------------------------------- Version public function testVersionIsStoredAndReadBack(): void { $sha = str_repeat('a1b2c3d4', 5); $this->assertNull(Version::current(), 'Frische Installation kennt noch keinen Stand'); $this->assertTrue(Version::save($sha)); $this->assertSame($sha, Version::current()); $this->assertSame(substr($sha, 0, 7), Version::short()); } /** * Eine halbe Proxy-Antwort darf sich nicht als Version einzementieren - * sonst meldet die Installation dauerhaft einen Stand, den es nicht gibt. */ public function testInvalidRevisionIsRejected(): void { foreach (['', 'nope', str_repeat('z', 40), str_repeat('ab', 10)] as $bad) { $this->assertFalse(Version::save($bad), 'Abgelehnt werden muss: ' . var_export($bad, true)); } $this->assertNull(Version::current()); } public function testReleaseConstantMatchesVersionFile(): void { $file = trim((string)file_get_contents(dirname(__DIR__) . '/VERSION')); $this->assertSame($file, Version::RELEASE, 'VERSION-Datei und Konstante duerfen nicht auseinanderlaufen'); } // --------------------------------------------------------- Wartungsmodus public function testMaintenanceCanBeSwitchedOnAndOff(): void { $this->assertFalse(Maintenance::isActive()); Maintenance::activate(); $this->assertTrue(Maintenance::isActive()); Maintenance::deactivate(); $this->assertFalse(Maintenance::isActive()); } /** * Ohne Selbstheilung legt ein Absturz mitten im Update die Installation * dauerhaft lahm - und niemand kaeme mehr an die Oberflaeche, um den * Modus abzuschalten. */ public function testMaintenanceHealsItselfAfterExpiry(): void { file_put_contents( $this->tmp . '/storage/.maintenance', json_encode(['since' => time() - 7200, 'until' => time() - 60]) ); $this->assertFalse(Maintenance::isActive(), 'Abgelaufene Wartung darf nicht mehr greifen'); $this->assertFileDoesNotExist($this->tmp . '/storage/.maintenance', 'und wird dabei aufgeraeumt'); } // -------------------------------------------------------------- Fortschritt public function testProgressIsWrittenAndRead(): void { Progress::set(42, 'download', 'Update wird geladen …'); $state = Progress::get(); $this->assertSame(42, $state['pct']); $this->assertSame('download', $state['step']); $this->assertSame('Update wird geladen …', $state['text']); $this->assertTrue(Progress::isRunning()); } /** pct = -1 trennt "fehlgeschlagen" eindeutig von "0 % erledigt". */ public function testProgressErrorStateIsDistinguishable(): void { Progress::error('Der Update-Dienst ist nicht erreichbar.'); $state = Progress::get(); $this->assertSame(-1, $state['pct']); $this->assertSame('error', $state['step']); $this->assertFalse(Progress::isRunning()); } /** Ein abgestuerzter Lauf darf den naechsten nicht dauerhaft blockieren. */ public function testStaleProgressDoesNotCountAsRunning(): void { file_put_contents($this->tmp . '/storage/.update-progress', json_encode([ 'pct' => 50, 'step' => 'extract', 'text' => 'haengt seit gestern', 'ts' => time() - (Progress::STALE_AFTER + 60), ])); $this->assertFalse(Progress::isRunning()); } // ------------------------------------------------------------- ZIP-Slip public function testEntryNamesOutsideTheArchiveAreRejected(): void { foreach (['../evil.php', 'a/../../evil.php', '/etc/passwd', 'C:\\windows\\evil', "ok\0.php"] as $bad) { $this->assertFalse(Updater::isSafeEntryName($bad), 'Abgelehnt werden muss: ' . $bad); } foreach (['index.php', 'admin/index.php', 'a/b/c.txt'] as $good) { $this->assertTrue(Updater::isSafeEntryName($good)); } } public function testZipSlipEntryNeverLandsOutsideTheStagingArea(): void { $zipPath = $this->tmp . '/slip.zip'; $zip = new \ZipArchive(); $this->assertTrue($zip->open($zipPath, \ZipArchive::CREATE) === true); $zip->addFromString('harmlos.txt', 'ok'); $zip->addFromString('../evil.txt', 'boesartig'); $zip->addFromString('a/../../auch-boese.txt', 'boesartig'); $zip->close(); $staging = $this->tmp . '/staging'; $updater = new Updater(null, null, 'stable', null, $this->tmp . '/root'); $written = $updater->extractZip($zipPath, $staging); $this->assertSame(1, $written, 'Nur der harmlose Eintrag darf ankommen'); $this->assertFileExists($staging . '/harmlos.txt'); $this->assertFileDoesNotExist($this->tmp . '/evil.txt'); $this->assertFileDoesNotExist($this->tmp . '/auch-boese.txt'); } // ------------------------------------------------------- Geschuetzte Pfade public function testProtectedPathsAreRecognised(): void { $this->assertTrue(Updater::isProtected('config.php')); $this->assertTrue(Updater::isProtected('uploads/logo.png')); $this->assertTrue(Updater::isProtected('updater/storage/.version')); $this->assertFalse(Updater::isProtected('index.php')); $this->assertFalse(Updater::isProtected('updater/Updater.php')); $this->assertFalse(Updater::isProtected('uploads.php'), 'Der Ordnername darf nicht auf Dateien durchschlagen'); } public function testApplyStagingKeepsProtectedFilesAndUpdatesTheRest(): void { $root = $this->tmp . '/root'; mkdir($root . '/uploads', 0775, true); file_put_contents($root . '/config.php', 'tmp . '/staging-apply'; mkdir($staging . '/uploads', 0775, true); file_put_contents($staging . '/config.php', 'applyStaging($staging); $this->assertSame('assertSame('altes Logo', file_get_contents($root . '/uploads/logo.png')); $this->assertSame('neu', file_get_contents($root . '/index.php')); $this->assertSame('neue Datei', file_get_contents($root . '/neu.php')); $this->assertSame(2, $applied, 'Nur die ungeschuetzten Dateien zaehlen'); } public function testApplyStagingLeavesNoHalfWrittenFiles(): void { $root = $this->tmp . '/root2'; mkdir($root, 0775, true); $staging = $this->tmp . '/staging2'; mkdir($staging, 0775, true); file_put_contents($staging . '/index.php', 'applyStaging($staging); $this->assertFileExists($root . '/index.php'); $this->assertFileDoesNotExist($root . '/index.php.new', 'Die Zwischendatei muss verschwinden'); } // ------------------------------------------------------------ Migrationen public function testMigrationRunnerCountsOnlyPendingFiles(): void { $dir = $this->tmp . '/migrations'; mkdir($dir, 0775, true); file_put_contents($dir . '/0001_erste.sql', 'CREATE TABLE test_a (id INTEGER PRIMARY KEY);'); file_put_contents($dir . '/0002_zweite.sql', 'CREATE TABLE test_b (id INTEGER PRIMARY KEY);'); $pdo = new \PDO('sqlite::memory:'); $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $runner = new MigrationRunner($pdo, $dir, $this->tmp . '/storage'); $this->assertCount(2, $runner->runPending(true)); $this->assertCount(0, $runner->runPending(true), 'Ein zweiter Lauf hat nichts mehr zu tun'); $status = $runner->status(); $this->assertCount(2, $status); $this->assertTrue($status[0]['applied']); } /** Der Fingerabdruck entscheidet, ob ueberhaupt migriert wird. */ public function testMigrationFingerprintChangesWithTheFiles(): void { $dir = $this->tmp . '/migrations-fp'; mkdir($dir, 0775, true); file_put_contents($dir . '/0001_a.sql', 'SELECT 1;'); $before = \Updater\AutoMigrate::fingerprint($dir); file_put_contents($dir . '/0002_b.sql', 'SELECT 1;'); $after = \Updater\AutoMigrate::fingerprint($dir); $this->assertNotSame($before, $after); $this->assertSame($after, \Updater\AutoMigrate::fingerprint($dir), 'Ohne Aenderung bleibt er gleich'); } // -------------------------------------------------------------- Changelog public function testChangelogIsSplitIntoVersions(): void { $markdown = "# Änderungsprotokoll\n\n## 3.0.0 – 2026-09-25\n\n- Selbst-Updater\n- Wartungsmodus\n\n## 2.9.0 – 2026-09-24\n\n- Display-Seiten\n"; $sections = Changelog::sections($markdown); $this->assertCount(2, $sections); $this->assertSame('3.0.0', $sections[0]['version']); $this->assertStringContainsString('Selbst-Updater', $sections[0]['text']); $this->assertSame('2.9.0', $sections[1]['version']); } public function testChangelogRendersListsAndKeepsUnsafeLinksOut(): void { $html = Changelog::toHtml("- Erster Punkt mit **Nachdruck**\n- [Doku](https://example.com/doku)\n- [Böse](javascript:alert(1))\n"); $this->assertStringContainsString('