Loheide-eu-mitarbeiterplatt.../tests/Feature/Plugins/PluginSdkTest.php
Friederich Loheide 03e31f42b5
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m38s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 8m0s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 11m51s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 12m15s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 13m27s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 13m42s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 16m0s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 15m20s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 56s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m14s
Noch schneller (0.46.0)
- Erscheinungsbild: große Fotos (Anmeldung, Titelbild) beim Speichern als WebP
  ≤ 2400 px; Migration verkleinert vorhandene einmal
- Vite: Grundgerüst und UI-Bausteine als gemeinsame Dateien (Startseite 34 statt
  58 Skripte)
- Plugin-Oberflächen: Kopie nach public/ erneuert sich bei neuem Build
  (Fingerabdruck von dist/inhaus.json), auch ohne neue Version

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-28 17:00:29 +00:00

191 lines
8.1 KiB
PHP

<?php
namespace Tests\Feature\Plugins;
use App\Models\User;
use App\Plugins\PluginAssets;
use App\Plugins\PluginManager;
use App\Plugins\PluginState;
use App\Support\Update\Filesystem;
use App\Version;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PluginSdkTest extends TestCase
{
use RefreshDatabase;
private string $dir;
protected function setUp(): void
{
parent::setUp();
$this->dir = sys_get_temp_dir().'/inhaus-sdk-'.bin2hex(random_bytes(4));
mkdir($this->dir.'/core', 0777, true);
mkdir($this->dir.'/custom', 0777, true);
config(['inhaus.plugin_assets_path' => $this->dir.'/public']);
}
protected function tearDown(): void
{
Filesystem::deleteDirectory($this->dir);
parent::tearDown();
}
/**
* Kunden-Plugin mit vorkompilierter Oberfläche unter dist/.
*/
private function makeUiPlugin(string $slug, string $base = 'custom'): string
{
$namespace = 'SdkTest'.bin2hex(random_bytes(3));
$path = "{$this->dir}/{$base}/{$slug}";
mkdir("{$path}/src", 0777, true);
mkdir("{$path}/dist/pages", 0777, true);
file_put_contents("{$path}/plugin.json", json_encode([
'slug' => $slug, 'name' => 'SDK-Test', 'version' => '1.2.3',
'namespace' => "{$namespace}\\", 'class' => "{$namespace}\\Plugin",
]));
file_put_contents("{$path}/src/Plugin.php", "<?php\nnamespace {$namespace};\nclass Plugin extends \\App\\Plugins\\Plugin {}\n");
file_put_contents("{$path}/dist/inhaus.json", json_encode([
'components' => ['pages/Index' => 'pages/Index.js', 'widgets/Böse' => '../../.env'],
'css' => ['style.css'],
]));
file_put_contents("{$path}/dist/pages/Index.js", 'export default {};');
file_put_contents("{$path}/dist/style.css", '.ih\:grid{display:grid}');
file_put_contents("{$path}/dist/geheim.php", '<?php echo 1;');
file_put_contents("{$path}/plugin-geheim.js", 'außerhalb von dist');
return $path;
}
private function boot(string ...$enabled): PluginManager
{
$state = new PluginState($this->dir.'/state.json');
foreach ($enabled as $slug) {
$state->setEnabled($slug, true);
}
$manager = new PluginManager($this->app, $state, $this->dir.'/core', $this->dir.'/custom');
$manager->registerAll();
$manager->bootAll();
$this->app->instance(PluginManager::class, $manager);
$this->app->instance(PluginAssets::class, new PluginAssets($manager));
return $manager;
}
public function test_active_customer_plugins_expose_their_components(): void
{
$this->makeUiPlugin('mit-ui');
$this->makeUiPlugin('inaktiv');
$this->makeUiPlugin('kern-ui', 'core');
$this->boot('mit-ui');
$assets = app(PluginAssets::class)->forFrontend();
$this->assertSame(['mit-ui'], array_keys($assets));
$this->assertSame('/plugin-assets/mit-ui/1.2.3/', $assets['mit-ui']['base']);
$this->assertSame(['pages/Index' => 'pages/Index.js'], $assets['mit-ui']['components']);
$this->assertSame(['style.css'], $assets['mit-ui']['css']);
}
public function test_asset_route_serves_only_whitelisted_files_from_dist(): void
{
$this->makeUiPlugin('mit-ui');
$this->makeUiPlugin('inaktiv');
$this->boot('mit-ui');
$response = $this->get('/plugin-assets/mit-ui/1.2.3/pages/Index.js');
$response->assertOk();
$this->assertStringStartsWith('text/javascript', $response->headers->get('Content-Type'));
$this->assertStringContainsString('immutable', $response->headers->get('Cache-Control'));
$this->assertSame('nosniff', $response->headers->get('X-Content-Type-Options'));
$this->get('/plugin-assets/mit-ui/1.2.3/style.css')->assertOk();
$this->get('/plugin-assets/mit-ui/1.2.3/geheim.php')->assertNotFound();
$this->get('/plugin-assets/mit-ui/1.2.3/../plugin-geheim.js')->assertNotFound();
$this->get('/plugin-assets/mit-ui/1.2.3/fehlt.js')->assertNotFound();
$this->get('/plugin-assets/inaktiv/1.2.3/pages/Index.js')->assertNotFound();
$this->assertNull(app(PluginAssets::class)->file('mit-ui', '../plugin-geheim.js'));
}
public function test_assets_are_published_for_the_web_server_and_removed_again(): void
{
$this->makeUiPlugin('mit-ui');
file_put_contents($this->dir.'/custom/mit-ui/dist/geheim.php', '<?php echo 1;');
$manager = $this->boot('mit-ui');
@mkdir($this->dir.'/public/mit-ui/1.0.0', 0777, true); // ältere Version
app(PluginAssets::class)->forFrontend();
$published = $this->dir.'/public/mit-ui/1.2.3';
$this->assertFileExists($published.'/pages/Index.js');
$this->assertFileExists($published.'/style.css');
$this->assertFileDoesNotExist($published.'/geheim.php', 'Nur erlaubte Dateitypen');
$this->assertDirectoryDoesNotExist($this->dir.'/public/mit-ui/1.0.0');
$this->assertSame(file_get_contents($this->dir.'/custom/mit-ui/dist/pages/Index.js'), file_get_contents($published.'/pages/Index.js'));
// Neuer Build bei gleicher Version wird neu kopiert
file_put_contents($this->dir.'/custom/mit-ui/dist/pages/Index.js', 'export default { neu: true };');
file_put_contents($this->dir.'/custom/mit-ui/dist/inhaus.json', "\n", FILE_APPEND);
(new PluginAssets($manager))->forFrontend();
$this->assertStringContainsString('neu: true', file_get_contents($published.'/pages/Index.js'));
$manager->deactivate('mit-ui');
$this->assertDirectoryDoesNotExist($this->dir.'/public/mit-ui');
}
public function test_pages_carry_import_map_and_plugin_manifest(): void
{
$this->makeUiPlugin('mit-ui');
$this->boot('mit-ui');
$html = $this->actingAs(User::factory()->create())->get('/')->assertOk()->getContent();
$this->assertStringContainsString('<script type="importmap">', $html);
$this->assertStringContainsString('/sdk/ui.js?v='.Version::VERSION, $html);
$this->assertStringContainsString('window.__inhausPlugins', $html);
$this->assertStringContainsString('/plugin-assets/mit-ui/1.2.3/style.css', $html);
}
public function test_sdk_shims_are_built_for_every_shared_module(): void
{
foreach (['vue' => 'ref', 'inertia' => 'router', 'ui' => 'Panel'] as $file => $export) {
$shim = (string) file_get_contents(public_path("sdk/{$file}.js"));
$this->assertStringContainsString('window.__inhaus.modules', $shim);
$this->assertMatchesRegularExpression('/\b'.$export.'\b/', $shim);
}
$this->assertStringContainsString('prefix(ih)', (string) file_get_contents(base_path('sdk/theme.css')));
}
public function test_generator_creates_a_loadable_plugin(): void
{
$target = $this->dir.'/custom/mav-kalender';
$this->artisan('inhaus:make-plugin', ['slug' => 'mav-kalender', '--name' => 'MAV-Kalender', '--author' => 'MAV', '--path' => $target])
->assertSuccessful();
foreach (['plugin.json', 'src/MavKalenderPlugin.php', 'resources/js/pages/Index.vue', 'resources/js/widgets/Hello.vue',
'vite.config.js', 'package.json', 'bin/package', 'inhaus-sdk/vite.js', 'inhaus-sdk/theme.css'] as $file) {
$this->assertFileExists("{$target}/{$file}");
}
$manifest = json_decode((string) file_get_contents("{$target}/plugin.json"), true);
$this->assertSame('MAV-Kalender', $manifest['name']);
$this->assertSame('Inhaus\\Plugins\\MavKalender\\', $manifest['namespace']);
$this->assertStringNotContainsString('{{', (string) file_get_contents("{$target}/package.json"));
$manager = $this->boot('mav-kalender');
$this->assertSame(['mav-kalender'], array_keys($manager->loaded()), (string) $manager->problem('mav-kalender'));
$this->artisan('inhaus:make-plugin', ['slug' => 'mav-kalender', '--path' => $target])->assertFailed();
$this->artisan('inhaus:make-plugin', ['slug' => 'Böse Sache', '--path' => $this->dir.'/x'])->assertFailed();
}
}