Loheide-eu-mitarbeiterplatt.../tests/Feature/Plugins/PluginUpdatesTest.php
Friederich Loheide 64d9e0bbed
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m47s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m50s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m4s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m13s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m43s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m44s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m55s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m14s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m1s
Plugin-Updates über update_url (0.24.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 08:06:22 +00:00

144 lines
5.6 KiB
PHP

<?php
namespace Tests\Feature\Plugins;
use App\Filament\Pages\Plugins;
use App\Models\User;
use App\Plugins\PluginManager;
use App\Plugins\PluginManifest;
use App\Plugins\PluginState;
use App\Plugins\PluginUpdates;
use App\Support\Update\Filesystem;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
use RuntimeException;
use Tests\TestCase;
use ZipArchive;
class PluginUpdatesTest extends TestCase
{
use RefreshDatabase;
private string $dir;
private string $namespace;
protected function setUp(): void
{
parent::setUp();
$this->dir = sys_get_temp_dir().'/inhaus-updates-'.bin2hex(random_bytes(4));
mkdir($this->dir.'/core', 0777, true);
mkdir($this->dir.'/custom', 0777, true);
$this->namespace = 'UpdTest'.bin2hex(random_bytes(3));
$this->writePlugin($this->dir.'/custom/spruch', '1.0.0');
$manager = new PluginManager($this->app, new PluginState($this->dir.'/state.json'), $this->dir.'/core', $this->dir.'/custom');
$this->app->instance(PluginManager::class, $manager);
$this->app->forgetInstance(PluginUpdates::class);
}
protected function tearDown(): void
{
Filesystem::deleteDirectory($this->dir);
parent::tearDown();
}
private function writePlugin(string $path, string $version): void
{
@mkdir($path.'/src', 0777, true);
file_put_contents($path.'/plugin.json', json_encode([
'slug' => 'spruch', 'name' => 'Spruch', 'version' => $version,
'namespace' => $this->namespace.'\\', 'class' => $this->namespace.'\\Plugin',
'update_url' => 'https://git.example.org/api/v1/repos/acme/spruch/releases/latest',
]));
file_put_contents($path.'/src/Plugin.php', "<?php\nnamespace {$this->namespace};\nclass Plugin extends \\App\\Plugins\\Plugin {}\n");
}
private function zip(string $version): string
{
$source = $this->dir.'/build-'.$version;
$this->writePlugin($source.'/spruch', $version);
$zipFile = $this->dir.'/spruch-'.$version.'.zip';
$zip = new ZipArchive;
$zip->open($zipFile, ZipArchive::CREATE);
$zip->addFile($source.'/spruch/plugin.json', 'spruch/plugin.json');
$zip->addFile($source.'/spruch/src/Plugin.php', 'spruch/src/Plugin.php');
$zip->close();
return (string) file_get_contents($zipFile);
}
public function test_forgejo_release_is_detected_and_installed(): void
{
$zip = $this->zip('1.1.0');
Http::fake([
'git.example.org/api/v1/repos/acme/spruch/releases/latest' => Http::response([
'tag_name' => 'v1.1.0', 'body' => 'Neue Sprüche für 2027',
'assets' => [['name' => 'spruch-1.1.0.zip', 'browser_download_url' => 'https://git.example.org/acme/spruch/releases/download/v1.1.0/spruch-1.1.0.zip']],
]),
'git.example.org/acme/spruch/releases/download/*' => Http::response($zip),
]);
$updates = app(PluginUpdates::class);
$updates->check();
$info = $updates->info('spruch');
$this->assertTrue($info['available']);
$this->assertSame('1.1.0', $info['version']);
$this->assertSame('Neue Sprüche für 2027', $info['changelog']);
$this->actingAs(User::factory()->admin()->create());
Livewire::test(Plugins::class)
->assertSee('Update auf 1.1.0 verfügbar')->assertSee('Neue Sprüche für 2027')->assertSee('Nach Updates suchen')
->call('updatePlugin', 'spruch');
$this->assertNull(Plugins::getNavigationBadge()); // nach dem Update nichts mehr offen
$manifest = PluginManifest::fromDirectory($this->dir.'/custom/spruch', false);
$this->assertSame('1.1.0', $manifest->version);
$this->assertSame('1.1.0', json_decode(file_get_contents($this->dir.'/custom/spruch/plugin.json'), true)['version']);
$this->assertFalse($updates->info('spruch')['available']);
}
public function test_checksum_is_enforced(): void
{
Http::fake([
'git.example.org/api/v1/*' => Http::response(['version' => '2.0.0', 'download_url' => 'https://git.example.org/spruch.zip', 'sha256' => str_repeat('a', 64)]),
'git.example.org/spruch.zip' => Http::response($this->zip('2.0.0')),
]);
$updates = app(PluginUpdates::class);
$updates->check();
try {
$updates->update('spruch');
$this->fail('Falsche Prüfsumme wurde akzeptiert.');
} catch (RuntimeException $e) {
$this->assertStringContainsString('Prüfsumme', $e->getMessage());
}
$this->assertSame('1.0.0', json_decode(file_get_contents($this->dir.'/custom/spruch/plugin.json'), true)['version']);
}
public function test_updates_requiring_a_newer_core_are_not_installed(): void
{
Http::fake(['*' => Http::response(['version' => '3.0.0', 'download_url' => 'https://x.test/a.zip', 'requires' => ['core' => '>=99.0']])]);
$updates = app(PluginUpdates::class);
$updates->check();
$this->assertFalse($updates->info('spruch')['compatible']);
$this->expectExceptionMessage('benötigt Inhaus >=99.0');
$updates->update('spruch');
}
public function test_insecure_or_broken_update_urls_are_reported(): void
{
Http::fake(['*' => Http::response('<html>Fehler</html>', 500)]);
$results = app(PluginUpdates::class)->check();
$this->assertStringContainsString('HTTP 500', $results['spruch']['error']);
$this->assertFalse(app(PluginUpdates::class)->info('spruch')['available']);
}
}