Plugin-System; Neuigkeiten und Termine als mitgelieferte Plugins
- Plugins nach WordPress-Prinzip: plugin.json-Manifest mit Versions- und Abhängigkeitsprüfung, eigenes PSR-4-Autoloading, register()/boot(), activate()/deactivate()/uninstall(), Migrationen, Routen und Views je Plugin - plugins-core/ (mitgeliefert, aktiv bis zur Deaktivierung) und plugins/ (Kunden-Plugins, geschützt vor Updates, inaktiv bis zur Aktivierung) - Zustand in storage/app/plugins.json – Laden ohne Datenbankabfrage - Wiederherstellungsmodus: Ausnahmen und Fatal Errors beim Laden deaktivieren das Plugin, das Portal bleibt erreichbar - Hooks (Actions/Filter mit Prioritäten) und Registries für Navigation, Startseiten-Kacheln, Rechte und Admin-Seiten (Filament-Discovery) - Admin-Seite „Plugins“: aktivieren, deaktivieren, per ZIP installieren (ZIP-Slip-Schutz), löschen mit oder ohne Daten; abschaltbar per INHAUS_DISALLOW_FILE_MODS - Startseite komplett aus Kacheln; Plugin-Seiten als „slug::Seite“ - Version 0.3.0 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
cc0020458f
commit
28ef1e4a20
155 changed files with 2762 additions and 573 deletions
84
tests/Feature/Plugins/ContentAdminTest.php
Normal file
84
tests/Feature/Plugins/ContentAdminTest.php
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\Plugins;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Inhaus\Plugins\Events\Filament\Resources\Events\Pages\CreateEvent;
|
||||
use Inhaus\Plugins\Events\Models\Event;
|
||||
use Inhaus\Plugins\News\Filament\Resources\NewsPosts\Pages\CreateNewsPost;
|
||||
use Inhaus\Plugins\News\Models\NewsCategory;
|
||||
use Inhaus\Plugins\News\Models\NewsPost;
|
||||
use Livewire\Livewire;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ContentAdminTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_content_pages_are_reachable_for_admins_only(): void
|
||||
{
|
||||
$admin = User::factory()->admin()->create();
|
||||
$post = NewsPost::factory()->create();
|
||||
$event = Event::factory()->create();
|
||||
|
||||
foreach (['/admin/neuigkeiten', '/admin/neuigkeiten/neu', "/admin/neuigkeiten/{$post->id}/bearbeiten", '/admin/kategorien', '/admin/termine', '/admin/termine/neu', "/admin/termine/{$event->id}/bearbeiten"] as $url) {
|
||||
$this->actingAs($admin)->get($url)->assertOk();
|
||||
$this->actingAs(User::factory()->create())->get($url)->assertForbidden();
|
||||
}
|
||||
}
|
||||
|
||||
public function test_admin_can_write_a_post(): void
|
||||
{
|
||||
$admin = User::factory()->admin()->create();
|
||||
$category = NewsCategory::factory()->create();
|
||||
$this->actingAs($admin);
|
||||
|
||||
Livewire::test(CreateNewsPost::class)
|
||||
->fillForm([
|
||||
'title' => 'Neue Kantine eröffnet',
|
||||
'teaser' => 'Ab Montag gibt es frisches Essen im Erdgeschoss.',
|
||||
'category_id' => $category->id,
|
||||
'published_at' => now()->subMinute()->format('Y-m-d H:i'),
|
||||
])
|
||||
->call('create')
|
||||
->assertHasNoFormErrors();
|
||||
|
||||
$post = NewsPost::query()->firstOrFail();
|
||||
$this->assertSame('neue-kantine-eroeffnet', $post->slug);
|
||||
$this->assertSame($admin->id, $post->author_id);
|
||||
$this->assertTrue($post->isPublished());
|
||||
}
|
||||
|
||||
public function test_admin_can_create_an_event(): void
|
||||
{
|
||||
$this->actingAs(User::factory()->admin()->create());
|
||||
|
||||
Livewire::test(CreateEvent::class)
|
||||
->fillForm([
|
||||
'title' => 'Sommerfest',
|
||||
'starts_at' => now()->addWeek()->format('Y-m-d 14:00'),
|
||||
'ends_at' => now()->addWeek()->format('Y-m-d 18:00'),
|
||||
'location' => 'Garten',
|
||||
'is_published' => true,
|
||||
])
|
||||
->call('create')
|
||||
->assertHasNoFormErrors();
|
||||
|
||||
$this->assertSame('Garten', Event::query()->firstOrFail()->location);
|
||||
}
|
||||
|
||||
public function test_event_end_must_not_be_before_start(): void
|
||||
{
|
||||
$this->actingAs(User::factory()->admin()->create());
|
||||
|
||||
Livewire::test(CreateEvent::class)
|
||||
->fillForm([
|
||||
'title' => 'Falsch herum',
|
||||
'starts_at' => now()->addWeek()->format('Y-m-d 14:00'),
|
||||
'ends_at' => now()->addWeek()->format('Y-m-d 10:00'),
|
||||
])
|
||||
->call('create')
|
||||
->assertHasFormErrors(['ends_at']);
|
||||
}
|
||||
}
|
||||
55
tests/Feature/Plugins/CorePluginToggleTest.php
Normal file
55
tests/Feature/Plugins/CorePluginToggleTest.php
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\Plugins;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Inertia\Testing\AssertableInertia as Assert;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Deaktivierte Plugins hinterlassen keine Spuren im Portal.
|
||||
*/
|
||||
class CorePluginToggleTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private ?string $previousState = null;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$file = storage_path('app/plugins.json');
|
||||
$this->previousState = is_file($file) ? file_get_contents($file) : null;
|
||||
file_put_contents($file, json_encode(['news' => ['enabled' => false], 'events' => ['enabled' => false]]));
|
||||
|
||||
// Plugins werden beim Start der Anwendung geladen – also neu starten.
|
||||
$this->refreshApplication();
|
||||
$this->setUpTraits();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$file = storage_path('app/plugins.json');
|
||||
$this->previousState === null ? @unlink($file) : file_put_contents($file, $this->previousState);
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_disabled_plugins_disappear_from_navigation_dashboard_routes_and_admin(): void
|
||||
{
|
||||
$admin = User::factory()->admin()->create();
|
||||
|
||||
$response = $this->actingAs($admin)->get('/')->assertOk()
|
||||
->assertInertia(fn (Assert $page) => $page->where('navigation', fn ($items) => collect($items)->pluck('key')->all() === ['home', 'people']));
|
||||
|
||||
$this->assertNull($this->dashboardWidget($response, 'news.latest'));
|
||||
$this->assertNull($this->dashboardWidget($response, 'events.upcoming'));
|
||||
$this->assertNotNull($this->dashboardWidget($response, 'core.quick-links'));
|
||||
|
||||
$this->actingAs($admin)->get('/neuigkeiten')->assertNotFound();
|
||||
$this->actingAs($admin)->get('/termine')->assertNotFound();
|
||||
$this->actingAs($admin)->get('/admin/neuigkeiten')->assertNotFound();
|
||||
}
|
||||
}
|
||||
99
tests/Feature/Plugins/EventsTest.php
Normal file
99
tests/Feature/Plugins/EventsTest.php
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\Plugins;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Inertia\Testing\AssertableInertia as Assert;
|
||||
use Inhaus\Plugins\Events\Models\Event;
|
||||
use Tests\TestCase;
|
||||
|
||||
class EventsTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_upcoming_and_running_events_are_listed_in_order(): void
|
||||
{
|
||||
$later = Event::factory()->create(['starts_at' => now()->addDays(10), 'ends_at' => now()->addDays(10)->addHour()]);
|
||||
$soon = Event::factory()->create(['starts_at' => now()->addDay(), 'ends_at' => now()->addDay()->addHour()]);
|
||||
$running = Event::factory()->create(['starts_at' => now()->subHour(), 'ends_at' => now()->addHour()]);
|
||||
Event::factory()->create(['starts_at' => now()->subDays(3), 'ends_at' => now()->subDays(3)->addHour()]);
|
||||
Event::factory()->create(['is_published' => false]);
|
||||
|
||||
$this->actingAs(User::factory()->create())
|
||||
->get('/termine')
|
||||
->assertOk()
|
||||
->assertInertia(fn (Assert $page) => $page
|
||||
->component('events::Index')
|
||||
->has('events.data', 3)
|
||||
->where('events.data.0.id', $running->id)
|
||||
->where('events.data.1.id', $soon->id)
|
||||
->where('events.data.2.id', $later->id));
|
||||
}
|
||||
|
||||
public function test_all_day_events_stay_visible_for_the_whole_day(): void
|
||||
{
|
||||
$today = Event::factory()->create(['starts_at' => now()->startOfDay(), 'ends_at' => null, 'all_day' => true]);
|
||||
|
||||
$this->actingAs(User::factory()->create())
|
||||
->get('/termine')
|
||||
->assertInertia(fn (Assert $page) => $page->where('events.data.0.id', $today->id));
|
||||
}
|
||||
|
||||
public function test_event_detail_and_unpublished_events(): void
|
||||
{
|
||||
$event = Event::factory()->create(['title' => 'Betriebsfest']);
|
||||
$hidden = Event::factory()->create(['is_published' => false]);
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->actingAs($user)->get("/termine/{$event->id}")
|
||||
->assertOk()
|
||||
->assertInertia(fn (Assert $page) => $page->component('events::Show')->where('event.title', 'Betriebsfest'));
|
||||
|
||||
$this->actingAs($user)->get("/termine/{$hidden->id}")->assertNotFound();
|
||||
$this->actingAs($user)->get("/termine/{$hidden->id}/kalender.ics")->assertNotFound();
|
||||
}
|
||||
|
||||
public function test_event_can_be_downloaded_as_ics(): void
|
||||
{
|
||||
$event = Event::factory()->create([
|
||||
'title' => 'Teamschulung; Führung, Kommunikation',
|
||||
'location' => 'Raum 1.04',
|
||||
'starts_at' => '2026-10-05 10:00:00',
|
||||
'ends_at' => '2026-10-05 15:00:00',
|
||||
]);
|
||||
|
||||
$response = $this->actingAs(User::factory()->create())->get("/termine/{$event->id}/kalender.ics");
|
||||
|
||||
$response->assertOk()->assertHeader('Content-Type', 'text/calendar; charset=utf-8');
|
||||
$ics = $response->getContent();
|
||||
|
||||
$this->assertStringStartsWith("BEGIN:VCALENDAR\r\n", $ics);
|
||||
$this->assertStringContainsString('SUMMARY:Teamschulung\; Führung\, Kommunikation', $ics);
|
||||
$this->assertStringContainsString('LOCATION:Raum 1.04', $ics);
|
||||
$this->assertMatchesRegularExpression('/DTSTART:20261005T\d{6}Z/', $ics);
|
||||
|
||||
foreach (explode("\r\n", $ics) as $line) {
|
||||
$this->assertLessThanOrEqual(75, strlen($line));
|
||||
}
|
||||
}
|
||||
|
||||
public function test_all_day_ics_uses_dates(): void
|
||||
{
|
||||
$event = Event::factory()->create(['starts_at' => '2026-11-11 00:00:00', 'ends_at' => null, 'all_day' => true]);
|
||||
|
||||
$ics = $this->actingAs(User::factory()->create())->get("/termine/{$event->id}/kalender.ics")->getContent();
|
||||
|
||||
$this->assertStringContainsString('DTSTART;VALUE=DATE:20261111', $ics);
|
||||
$this->assertStringContainsString('DTEND;VALUE=DATE:20261112', $ics);
|
||||
}
|
||||
|
||||
public function test_dashboard_shows_next_four_events(): void
|
||||
{
|
||||
Event::factory()->count(6)->create();
|
||||
|
||||
$events = $this->dashboardWidget($this->actingAs(User::factory()->create())->get('/'), 'events.upcoming')['events'];
|
||||
|
||||
$this->assertCount(4, $events);
|
||||
}
|
||||
}
|
||||
153
tests/Feature/Plugins/NewsTest.php
Normal file
153
tests/Feature/Plugins/NewsTest.php
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\Plugins;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Inertia\Testing\AssertableInertia as Assert;
|
||||
use Inhaus\Plugins\News\Models\NewsCategory;
|
||||
use Inhaus\Plugins\News\Models\NewsPost;
|
||||
use Tests\TestCase;
|
||||
|
||||
class NewsTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_only_published_posts_are_listed(): void
|
||||
{
|
||||
NewsPost::factory()->count(2)->create();
|
||||
NewsPost::factory()->draft()->create();
|
||||
NewsPost::factory()->scheduled()->create();
|
||||
|
||||
$this->actingAs(User::factory()->create())
|
||||
->get('/neuigkeiten')
|
||||
->assertOk()
|
||||
->assertInertia(fn (Assert $page) => $page->component('news::Index')->where('posts.meta.total', 2));
|
||||
}
|
||||
|
||||
public function test_posts_can_be_filtered_by_category(): void
|
||||
{
|
||||
$mav = NewsCategory::factory()->create(['name' => 'MAV', 'slug' => 'mav']);
|
||||
NewsPost::factory()->create(['category_id' => $mav->id]);
|
||||
NewsPost::factory()->count(2)->create();
|
||||
|
||||
$this->actingAs(User::factory()->create())
|
||||
->get('/neuigkeiten?kategorie=mav')
|
||||
->assertInertia(fn (Assert $page) => $page
|
||||
->where('posts.meta.total', 1)
|
||||
->where('posts.data.0.category.name', 'MAV')
|
||||
->where('activeCategory', 'mav'));
|
||||
}
|
||||
|
||||
public function test_published_post_is_shown_with_author(): void
|
||||
{
|
||||
$author = User::factory()->create(['first_name' => 'Grete', 'last_name' => 'Müller']);
|
||||
$post = NewsPost::factory()->create(['author_id' => $author->id, 'title' => 'Sommerfest 2026']);
|
||||
|
||||
$this->assertSame('sommerfest-2026', $post->slug);
|
||||
|
||||
$this->actingAs(User::factory()->create())
|
||||
->get('/neuigkeiten/sommerfest-2026')
|
||||
->assertOk()
|
||||
->assertInertia(fn (Assert $page) => $page
|
||||
->component('news::Show')
|
||||
->where('post.title', 'Sommerfest 2026')
|
||||
->where('post.author.name', 'Grete Müller'));
|
||||
}
|
||||
|
||||
public function test_drafts_and_scheduled_posts_are_not_reachable(): void
|
||||
{
|
||||
$draft = NewsPost::factory()->draft()->create();
|
||||
$scheduled = NewsPost::factory()->scheduled()->create();
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->actingAs($user)->get("/neuigkeiten/{$draft->slug}")->assertNotFound();
|
||||
$this->actingAs($user)->get("/neuigkeiten/{$scheduled->slug}")->assertNotFound();
|
||||
}
|
||||
|
||||
public function test_slugs_are_unique(): void
|
||||
{
|
||||
$a = NewsPost::factory()->create(['title' => 'Neuigkeit']);
|
||||
$b = NewsPost::factory()->create(['title' => 'Neuigkeit']);
|
||||
|
||||
$this->assertSame('neuigkeit', $a->slug);
|
||||
$this->assertSame('neuigkeit-2', $b->slug);
|
||||
}
|
||||
|
||||
public function test_body_is_sanitized_when_saved(): void
|
||||
{
|
||||
$post = NewsPost::factory()->create([
|
||||
'body' => '<p onclick="alert(1)">Hallo <strong>Welt</strong></p><script>alert(1)</script><a href="javascript:alert(1)">Link</a><iframe src="https://evil.example"></iframe>',
|
||||
]);
|
||||
|
||||
$this->assertStringContainsString('<strong>Welt</strong>', $post->body_html);
|
||||
$this->assertStringNotContainsString('script', $post->body_html);
|
||||
$this->assertStringNotContainsString('onclick', $post->body_html);
|
||||
$this->assertStringNotContainsString('javascript:', $post->body_html);
|
||||
$this->assertStringNotContainsString('iframe', $post->body_html);
|
||||
}
|
||||
|
||||
public function test_reading_time_is_calculated(): void
|
||||
{
|
||||
$post = NewsPost::factory()->create(['body' => '<p>'.str_repeat('Wort ', 450).'</p>']);
|
||||
|
||||
$this->assertSame(3, $post->reading_minutes);
|
||||
}
|
||||
|
||||
public function test_cover_variants_are_created_and_removed(): void
|
||||
{
|
||||
Storage::fake('uploads');
|
||||
Storage::disk('uploads')->put('news/cover.jpg', UploadedFile::fake()->image('cover.jpg', 2000, 1200)->getContent());
|
||||
|
||||
$post = NewsPost::factory()->create(['cover_path' => 'news/cover.jpg']);
|
||||
|
||||
Storage::disk('uploads')->assertExists('news/cover-thumb.webp');
|
||||
Storage::disk('uploads')->assertExists('news/cover-large.webp');
|
||||
$this->assertStringEndsWith('news/cover-thumb.webp', $post->coverUrl('thumb'));
|
||||
|
||||
$post->delete();
|
||||
|
||||
Storage::disk('uploads')->assertMissing('news/cover.jpg');
|
||||
Storage::disk('uploads')->assertMissing('news/cover-thumb.webp');
|
||||
}
|
||||
|
||||
public function test_large_photos_fit_into_shared_hosting_memory_limit(): void
|
||||
{
|
||||
Storage::fake('uploads');
|
||||
|
||||
// Testbild (12 Megapixel wie ein Handyfoto) mit großzügigem Limit erzeugen …
|
||||
ini_set('memory_limit', '512M');
|
||||
Storage::disk('uploads')->put('news/handyfoto.jpg', UploadedFile::fake()->image('handyfoto.jpg', 4000, 3000)->getContent());
|
||||
gc_collect_cycles();
|
||||
|
||||
// … dann wie auf typischem Shared Hosting mit 128 MB verarbeiten.
|
||||
$this->assertNotFalse(ini_set('memory_limit', '128M'));
|
||||
|
||||
NewsPost::factory()->create(['cover_path' => 'news/handyfoto.jpg']);
|
||||
|
||||
Storage::disk('uploads')->assertExists('news/handyfoto-large.webp');
|
||||
Storage::disk('uploads')->assertExists('news/handyfoto-thumb.webp');
|
||||
|
||||
ini_set('memory_limit', '128M');
|
||||
}
|
||||
|
||||
public function test_dashboard_shows_featured_and_latest_news(): void
|
||||
{
|
||||
Storage::fake('uploads');
|
||||
Storage::disk('uploads')->put('news/a.jpg', UploadedFile::fake()->image('a.jpg', 800, 450)->getContent());
|
||||
|
||||
$featured = NewsPost::factory()->create(['is_featured' => true, 'cover_path' => 'news/a.jpg']);
|
||||
NewsPost::factory()->count(5)->create();
|
||||
NewsPost::factory()->draft()->create(['is_featured' => true, 'cover_path' => 'news/a.jpg']);
|
||||
|
||||
$response = $this->actingAs(User::factory()->create())->get('/');
|
||||
|
||||
$hero = $this->dashboardWidget($response, 'news.hero')['posts'];
|
||||
$this->assertCount(1, $hero);
|
||||
$this->assertSame($featured->id, $hero[0]['id']);
|
||||
$this->assertCount(4, $this->dashboardWidget($response, 'news.latest')['posts']);
|
||||
$this->assertCount(1, $this->dashboardWidget($response, 'news.areas')['posts']);
|
||||
}
|
||||
}
|
||||
360
tests/Feature/Plugins/PluginSystemTest.php
Normal file
360
tests/Feature/Plugins/PluginSystemTest.php
Normal file
|
|
@ -0,0 +1,360 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\Plugins;
|
||||
|
||||
use App\Filament\Pages\Plugins;
|
||||
use App\Models\User;
|
||||
use App\Plugins\Hooks;
|
||||
use App\Plugins\PluginInstaller;
|
||||
use App\Plugins\PluginManager;
|
||||
use App\Plugins\PluginState;
|
||||
use App\Plugins\VersionConstraint;
|
||||
use App\Support\Navigation;
|
||||
use App\Support\Update\Filesystem;
|
||||
use App\Support\Update\ZipExtractor;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Livewire\Livewire;
|
||||
use RuntimeException;
|
||||
use Tests\TestCase;
|
||||
use ZipArchive;
|
||||
|
||||
class PluginSystemTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private string $dir;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->dir = sys_get_temp_dir().'/inhaus-plugins-'.bin2hex(random_bytes(4));
|
||||
mkdir($this->dir.'/core', 0777, true);
|
||||
mkdir($this->dir.'/custom', 0777, true);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Filesystem::deleteDirectory($this->dir);
|
||||
Schema::dropIfExists('beispiel_eintraege');
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Legt ein Beispiel-Plugin an. $register/$boot werden in die Plugin-Klasse eingesetzt.
|
||||
*
|
||||
* @param array<string, mixed> $manifest
|
||||
*/
|
||||
private function makePlugin(string $slug, array $manifest = [], string $register = '', string $boot = '', bool $withMigration = false, string $base = 'custom'): string
|
||||
{
|
||||
$class = str_replace(' ', '', ucwords(str_replace('-', ' ', $slug)));
|
||||
$namespace = "Test{$class}".bin2hex(random_bytes(3));
|
||||
$path = "{$this->dir}/{$base}/{$slug}";
|
||||
mkdir("{$path}/src", 0777, true);
|
||||
|
||||
file_put_contents("{$path}/plugin.json", json_encode(array_merge([
|
||||
'slug' => $slug,
|
||||
'name' => "Plugin {$class}",
|
||||
'version' => '1.0.0',
|
||||
'namespace' => "{$namespace}\\",
|
||||
'class' => "{$namespace}\\Plugin",
|
||||
'requires' => ['core' => '>=0.3.0'],
|
||||
], $manifest)));
|
||||
|
||||
file_put_contents("{$path}/src/Plugin.php", <<<PHP
|
||||
<?php
|
||||
namespace {$namespace};
|
||||
|
||||
class Plugin extends \App\Plugins\Plugin
|
||||
{
|
||||
public function register(): void { {$register} }
|
||||
public function boot(): void { {$boot} }
|
||||
public function activate(): void { file_put_contents(\$this->path('activated'), '1'); }
|
||||
public function uninstall(): void { file_put_contents(sys_get_temp_dir().'/inhaus-uninstalled-{$slug}', '1'); }
|
||||
}
|
||||
PHP);
|
||||
|
||||
if ($withMigration) {
|
||||
mkdir("{$path}/database/migrations", 0777, true);
|
||||
file_put_contents("{$path}/database/migrations/2026_01_01_000000_create_beispiel_eintraege.php", <<<'PHP'
|
||||
<?php
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration {
|
||||
public function up(): void { Schema::create('beispiel_eintraege', fn (Blueprint $t) => $t->id()); }
|
||||
public function down(): void { Schema::dropIfExists('beispiel_eintraege'); }
|
||||
};
|
||||
PHP);
|
||||
}
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
private function manager(): PluginManager
|
||||
{
|
||||
return new PluginManager($this->app, new PluginState($this->dir.'/state.json'), $this->dir.'/core', $this->dir.'/custom');
|
||||
}
|
||||
|
||||
public function test_version_constraints(): void
|
||||
{
|
||||
$this->assertTrue(VersionConstraint::satisfies('0.3.0', '>=0.3.0'));
|
||||
$this->assertFalse(VersionConstraint::satisfies('0.2.9', '>=0.3'));
|
||||
$this->assertTrue(VersionConstraint::satisfies('1.4.2', '^1.2'));
|
||||
$this->assertFalse(VersionConstraint::satisfies('2.0.0', '^1.2'));
|
||||
$this->assertTrue(VersionConstraint::satisfies('0.3.5', '^0.3'));
|
||||
$this->assertFalse(VersionConstraint::satisfies('0.4.0', '^0.3'));
|
||||
$this->assertTrue(VersionConstraint::satisfies('1.4.9', '~1.4'));
|
||||
$this->assertTrue(VersionConstraint::satisfies('8.3.12', '>=8.3 <9'));
|
||||
$this->assertTrue(VersionConstraint::satisfies('1.0.0', null));
|
||||
}
|
||||
|
||||
public function test_hooks_run_by_priority(): void
|
||||
{
|
||||
$calls = [];
|
||||
Hooks::on('test.action', function () use (&$calls) {
|
||||
$calls[] = 'spät';
|
||||
}, 20);
|
||||
Hooks::on('test.action', function () use (&$calls) {
|
||||
$calls[] = 'früh';
|
||||
}, 5);
|
||||
Hooks::do('test.action');
|
||||
|
||||
Hooks::filter('test.filter', fn (int $v) => $v * 2);
|
||||
Hooks::filter('test.filter', fn (int $v) => $v + 1, 1);
|
||||
|
||||
$this->assertSame(['früh', 'spät'], $calls);
|
||||
$this->assertSame(8, Hooks::apply('test.filter', 3));
|
||||
}
|
||||
|
||||
public function test_plugins_are_discovered_registered_and_booted_in_dependency_order(): void
|
||||
{
|
||||
$this->makePlugin('basis', register: "\$this->app->make(\\App\\Support\\Navigation::class)->add('basis', 'Basis', '/basis', sort: 60);", base: 'core');
|
||||
$this->makePlugin('erweiterung', ['requires' => ['core' => '>=0.3', 'plugins' => ['basis']]], boot: "\\App\\Plugins\\Hooks::filter('test.boot', fn (\$v) => \$v.'+erweiterung');", base: 'core');
|
||||
$this->makePlugin('fehlt-abhaengigkeit', ['requires' => ['plugins' => ['gibt-es-nicht']]], base: 'core');
|
||||
$this->makePlugin('zu-neu', ['requires' => ['core' => '>=99.0']], base: 'core');
|
||||
|
||||
$manager = $this->manager();
|
||||
$manager->registerAll();
|
||||
$manager->bootAll();
|
||||
|
||||
$this->assertSame(['basis', 'erweiterung'], array_keys($manager->loaded()));
|
||||
$this->assertStringContainsString('gibt-es-nicht', $manager->problem('fehlt-abhaengigkeit'));
|
||||
$this->assertStringContainsString('benötigt Inhaus >=99.0', $manager->problem('zu-neu'));
|
||||
$this->assertSame('x+erweiterung', Hooks::apply('test.boot', 'x'));
|
||||
|
||||
$labels = array_column(app(Navigation::class)->items(Request::create('/')), 'label');
|
||||
$this->assertContains('Basis', $labels);
|
||||
}
|
||||
|
||||
public function test_invalid_manifests_are_reported_not_loaded(): void
|
||||
{
|
||||
mkdir($this->dir.'/custom/kaputt');
|
||||
file_put_contents($this->dir.'/custom/kaputt/plugin.json', '{"slug": "Kaputt!"}');
|
||||
$this->makePlugin('app-namespace', ['namespace' => 'App\\Evil\\', 'class' => 'App\\Evil\\Plugin']);
|
||||
|
||||
$manager = $this->manager();
|
||||
|
||||
$this->assertSame([], array_keys($manager->all()));
|
||||
$this->assertStringContainsString('Feld', $manager->problem('kaputt'));
|
||||
$this->assertStringContainsString('vorbehalten', $manager->problem('app-namespace'));
|
||||
}
|
||||
|
||||
public function test_exception_while_loading_disables_the_plugin_and_keeps_the_portal_running(): void
|
||||
{
|
||||
$this->makePlugin('wackelig', register: "throw new \\RuntimeException('Datenbank weg');", base: 'core');
|
||||
$this->makePlugin('stabil', base: 'core');
|
||||
|
||||
$manager = $this->manager();
|
||||
$manager->registerAll();
|
||||
|
||||
$this->assertSame(['stabil'], array_keys($manager->loaded()));
|
||||
$this->assertFalse($manager->isEnabled('wackelig'));
|
||||
$this->assertStringContainsString('Datenbank weg', $manager->problem('wackelig'));
|
||||
}
|
||||
|
||||
public function test_fatal_error_during_previous_load_is_detected(): void
|
||||
{
|
||||
$this->makePlugin('absturz', base: 'core');
|
||||
$marker = storage_path('framework/plugins/booting-absturz');
|
||||
@mkdir(dirname($marker), 0777, true);
|
||||
file_put_contents($marker, '1');
|
||||
touch($marker, time() - 120);
|
||||
|
||||
$manager = $this->manager();
|
||||
$manager->registerAll();
|
||||
|
||||
$this->assertFalse($manager->isEnabled('absturz'));
|
||||
$this->assertStringContainsString('Schwerer Fehler', $manager->problem('absturz'));
|
||||
$this->assertFileDoesNotExist($marker);
|
||||
}
|
||||
|
||||
public function test_customer_plugins_are_inactive_until_activated(): void
|
||||
{
|
||||
$path = $this->makePlugin('kunde', withMigration: true);
|
||||
$this->makePlugin('mitgeliefert', base: 'core');
|
||||
|
||||
$manager = $this->manager();
|
||||
$this->assertFalse($manager->isEnabled('kunde'));
|
||||
$this->assertTrue($manager->isEnabled('mitgeliefert'));
|
||||
|
||||
$manager->activate('kunde');
|
||||
|
||||
$this->assertTrue($manager->isEnabled('kunde'));
|
||||
$this->assertTrue(Schema::hasTable('beispiel_eintraege'));
|
||||
$this->assertFileExists($path.'/activated');
|
||||
}
|
||||
|
||||
public function test_plugin_with_dependents_cannot_be_deactivated(): void
|
||||
{
|
||||
$this->makePlugin('basis', base: 'core');
|
||||
$this->makePlugin('aufsatz', ['requires' => ['plugins' => ['basis']]], base: 'core');
|
||||
|
||||
$this->expectExceptionMessage('Wird noch benötigt von: Plugin Aufsatz');
|
||||
|
||||
$this->manager()->deactivate('basis');
|
||||
}
|
||||
|
||||
public function test_delete_with_data_removes_tables_and_files(): void
|
||||
{
|
||||
$path = $this->makePlugin('entfernen', withMigration: true);
|
||||
$manager = $this->manager();
|
||||
$manager->activate('entfernen');
|
||||
|
||||
$manager->delete('entfernen', removeData: true);
|
||||
|
||||
$this->assertDirectoryDoesNotExist($path);
|
||||
$this->assertFalse(Schema::hasTable('beispiel_eintraege'));
|
||||
$this->assertFileExists(sys_get_temp_dir().'/inhaus-uninstalled-entfernen');
|
||||
@unlink(sys_get_temp_dir().'/inhaus-uninstalled-entfernen');
|
||||
}
|
||||
|
||||
public function test_core_plugins_cannot_be_deleted(): void
|
||||
{
|
||||
$this->makePlugin('kern', base: 'core');
|
||||
|
||||
$this->expectExceptionMessage('nur deaktiviert');
|
||||
|
||||
$this->manager()->delete('kern', false);
|
||||
}
|
||||
|
||||
public function test_zip_installation(): void
|
||||
{
|
||||
$source = $this->makePlugin('hochgeladen', base: 'core');
|
||||
$zip = $this->zipDirectory($source, 'hochgeladen-1.0.0/');
|
||||
Filesystem::deleteDirectory($source);
|
||||
|
||||
$manager = $this->manager();
|
||||
$manifest = (new PluginInstaller($manager, new ZipExtractor))->install($zip);
|
||||
|
||||
$this->assertSame('hochgeladen', $manifest->slug);
|
||||
$this->assertFalse($manifest->core);
|
||||
$this->assertFileExists($this->dir.'/custom/hochgeladen/plugin.json');
|
||||
$this->assertNotNull($manager->find('hochgeladen'));
|
||||
}
|
||||
|
||||
public function test_zip_without_manifest_or_with_core_slug_is_rejected(): void
|
||||
{
|
||||
$installer = new PluginInstaller($this->manager(), new ZipExtractor);
|
||||
|
||||
$empty = $this->dir.'/leer.zip';
|
||||
$archive = new ZipArchive;
|
||||
$archive->open($empty, ZipArchive::CREATE);
|
||||
$archive->addFromString('README.md', 'kein Plugin');
|
||||
$archive->close();
|
||||
|
||||
try {
|
||||
$installer->install($empty);
|
||||
$this->fail('Ohne plugin.json hätte die Installation scheitern müssen.');
|
||||
} catch (RuntimeException $e) {
|
||||
$this->assertStringContainsString('kein gültiges Plugin', $e->getMessage());
|
||||
}
|
||||
|
||||
$this->makePlugin('kern', base: 'core');
|
||||
$copy = $this->makePlugin('kern', base: 'upload');
|
||||
|
||||
$this->expectExceptionMessage('gehört zu einem mitgelieferten Plugin');
|
||||
|
||||
(new PluginInstaller($this->manager(), new ZipExtractor))->install($this->zipDirectory($copy, 'kern/'));
|
||||
}
|
||||
|
||||
public function test_zip_slip_in_plugin_archive_is_blocked(): void
|
||||
{
|
||||
$zip = $this->dir.'/boese.zip';
|
||||
$archive = new ZipArchive;
|
||||
$archive->open($zip, ZipArchive::CREATE);
|
||||
$archive->addFromString('plugin/plugin.json', '{}');
|
||||
$archive->addFromString('plugin/../../boese.php', '<?php');
|
||||
$archive->close();
|
||||
|
||||
try {
|
||||
(new PluginInstaller($this->manager(), new ZipExtractor))->install($zip);
|
||||
$this->fail('Unsicheres Archiv hätte abgelehnt werden müssen.');
|
||||
} catch (RuntimeException $e) {
|
||||
$this->assertStringContainsString('Unsicherer Pfad', $e->getMessage());
|
||||
}
|
||||
|
||||
$this->assertFileDoesNotExist(storage_path('boese.php'));
|
||||
}
|
||||
|
||||
public function test_uploads_can_be_disabled(): void
|
||||
{
|
||||
config(['inhaus.disallow_file_mods' => true]);
|
||||
|
||||
$this->expectExceptionMessage('abgeschaltet');
|
||||
|
||||
(new PluginInstaller($this->manager(), new ZipExtractor))->install($this->dir.'/egal.zip');
|
||||
}
|
||||
|
||||
public function test_admin_page_lists_core_plugins_and_is_admin_only(): void
|
||||
{
|
||||
$this->actingAs(User::factory()->create())->get('/admin/plugins')->assertForbidden();
|
||||
|
||||
$this->actingAs(User::factory()->admin()->create())
|
||||
->get('/admin/plugins')
|
||||
->assertOk()
|
||||
->assertSee('Neuigkeiten')
|
||||
->assertSee('Termine')
|
||||
->assertSee('mitgeliefert')
|
||||
->assertSee('vollen Rechten');
|
||||
}
|
||||
|
||||
public function test_admin_can_upload_a_plugin_through_the_page(): void
|
||||
{
|
||||
$source = $this->makePlugin('seite-hochgeladen', base: 'core');
|
||||
$zip = $this->zipDirectory($source, 'seite-hochgeladen/');
|
||||
Filesystem::deleteDirectory($source);
|
||||
|
||||
$this->app->instance(PluginManager::class, $this->manager());
|
||||
$this->actingAs(User::factory()->admin()->create());
|
||||
|
||||
Livewire::test(Plugins::class)
|
||||
->set('archive', UploadedFile::fake()->createWithContent('plugin.zip', file_get_contents($zip)))
|
||||
->call('upload')
|
||||
->assertHasNoErrors();
|
||||
|
||||
$this->assertFileExists($this->dir.'/custom/seite-hochgeladen/plugin.json');
|
||||
}
|
||||
|
||||
private function zipDirectory(string $source, string $prefix): string
|
||||
{
|
||||
$zip = $this->dir.'/'.basename($source).'-'.bin2hex(random_bytes(2)).'.zip';
|
||||
$archive = new ZipArchive;
|
||||
$archive->open($zip, ZipArchive::CREATE);
|
||||
|
||||
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \FilesystemIterator::SKIP_DOTS));
|
||||
|
||||
foreach ($files as $file) {
|
||||
$archive->addFile($file->getPathname(), $prefix.substr($file->getPathname(), strlen($source) + 1));
|
||||
}
|
||||
|
||||
$archive->close();
|
||||
|
||||
return $zip;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue