Loheide-eu-mitarbeiterplatt.../tests/Feature/Plugins/PluginSdkTest.php
Friederich Loheide 800e267e1c
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m29s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m31s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 9m3s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 9m36s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 13m33s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 13m56s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 12m35s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 11m58s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 50s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m13s
Neue Seitenköpfe, global wählbar (0.48.0)
- PageHeader: Kopfband (Standard, kompakt), Kopfband mit Symbol, mit Symbol,
  kräftiger Titel – Einstellung page_header im Erscheinungsbild
- Symbol vom passenden Menüpunkt (aktiv oder per Adresse), Prop icon
- In Spalten (z. B. Downloads) schlichte Form statt Band
- Zurück-Links als breadcrumbs (IT-Service), Profil bearbeiten mit PageHeader
- Plugin-Assets: Fingerabdruck berücksichtigt Änderungszeiten der Dateien

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

192 lines
8.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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
// (Seiten behalten ihren Namen – inhaus.json bleibt gleich, nur die Datei ist neuer)
file_put_contents($this->dir.'/custom/mit-ui/dist/pages/Index.js', 'export default { neu: true };');
touch($this->dir.'/custom/mit-ui/dist/pages/Index.js', time() + 60);
(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();
}
}