inhaus-plugin-gruppen/tests/GruppenTest.php
Friederich Loheide 5bdfb8a082 Gruppen-Plugin 1.0.0
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 17:17:10 +00:00

161 lines
8.7 KiB
PHP
Raw 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\Plugins\Gruppen;
use App\Models\User;
use App\Notifications\PortalNotification;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Storage;
use Inertia\Testing\AssertableInertia as Assert;
use Inhaus\Plugins\Gruppen\Models\Group;
use Inhaus\Plugins\Gruppen\Models\Post;
use Tests\TestCase;
/**
* Läuft innerhalb einer Inhaus-Installation mit dem Plugin unter plugins/gruppen:
* vendor-dev/bin/phpunit plugins/gruppen/tests
*/
class GruppenTest extends TestCase
{
use RefreshDatabase;
private ?string $previousState = null;
protected function setUp(): void
{
$file = getcwd().'/storage/app/plugins.json';
$this->previousState = is_file($file) ? file_get_contents($file) : null;
$state = json_decode($this->previousState ?? '{}', true) ?: [];
$state['gruppen'] = ['enabled' => true];
file_put_contents($file, json_encode($state));
parent::setUp();
Storage::fake('local');
}
protected function tearDown(): void
{
$file = storage_path('app/plugins.json');
$this->previousState === null ? @unlink($file) : file_put_contents($file, $this->previousState);
parent::tearDown();
}
private function group(User $creator, string $privacy, string $name = 'Laufgruppe'): Group
{
$this->actingAs($creator)->post('/gruppen', ['name' => $name, 'privacy' => $privacy, 'description' => 'Wir laufen mittwochs.'])->assertRedirect();
return Group::query()->latest('id')->firstOrFail();
}
public function test_open_groups_everyone_can_read_and_join(): void
{
[$ada, $bob] = User::factory()->count(2)->create()->all();
$group = $this->group($ada, 'open');
$this->assertSame('laufgruppe', $group->slug);
$this->assertSame(1, $group->members_count);
$this->actingAs($ada)->postJson("/gruppen/{$group->slug}/beitraege", ['body' => 'Mittwoch 17 Uhr am Haupteingang?'])->assertCreated();
// Nicht-Mitglieder lesen mit, dürfen aber nicht schreiben
$this->actingAs($bob)->get("/gruppen/{$group->slug}")->assertInertia(fn (Assert $p) => $p->has('posts', 1)->where('can.post', false)->where('can.read', true));
$this->actingAs($bob)->postJson("/gruppen/{$group->slug}/beitraege", ['body' => 'Hallo'])->assertForbidden();
$this->actingAs($bob)->post("/gruppen/{$group->slug}/beitreten")->assertSessionHas('status', 'Willkommen in der Gruppe!');
$this->assertSame(2, $group->fresh()->members_count);
$this->actingAs($bob)->get('/gruppen')->assertInertia(fn (Assert $p) => $p->has('groups', 1)->has('posts', 1));
}
public function test_closed_groups_need_approval_and_hide_posts(): void
{
Notification::fake();
[$ada, $bob] = User::factory()->count(2)->create()->all();
$group = $this->group($ada, 'closed', 'MAV-Austausch');
$this->actingAs($ada)->postJson("/gruppen/{$group->slug}/beitraege", ['body' => 'Intern']);
$this->actingAs($bob)->get("/gruppen/{$group->slug}")->assertInertia(fn (Assert $p) => $p->has('posts', 0)->where('can.read', false)->where('members', null));
$this->actingAs($bob)->post("/gruppen/{$group->slug}/beitreten")->assertSessionHas('status', 'Anfrage gesendet – die Gruppe entscheidet.');
Notification::assertSentTo($ada, PortalNotification::class, fn ($n) => str_contains($n->title, 'Beitrittsanfrage'));
$this->actingAs($ada)->get("/gruppen/{$group->slug}")->assertInertia(fn (Assert $p) => $p->where('members.pending.0.id', $bob->id));
$this->actingAs($ada)->post("/gruppen/{$group->slug}/anfragen/{$bob->id}", ['accept' => true]);
$this->actingAs($bob)->get("/gruppen/{$group->slug}")->assertInertia(fn (Assert $p) => $p->has('posts', 1)->where('can.post', true));
}
public function test_secret_groups_are_invisible_and_invite_only(): void
{
Notification::fake();
[$ada, $bob, $eve] = User::factory()->count(3)->create()->all();
$group = $this->group($ada, 'secret', 'Projekt Umzug');
$this->actingAs($eve)->get("/gruppen/{$group->slug}")->assertNotFound();
$this->actingAs($eve)->post("/gruppen/{$group->slug}/beitreten")->assertNotFound();
$this->actingAs($eve)->get('/gruppen/entdecken')->assertInertia(fn (Assert $p) => $p->has('groups', 0));
$this->actingAs($ada)->post("/gruppen/{$group->slug}/einladen", ['users' => [$bob->id]])->assertSessionHas('status');
Notification::assertSentTo($bob, PortalNotification::class, fn ($n) => str_contains($n->title, 'Einladung'));
$this->actingAs($bob)->get('/gruppen')->assertInertia(fn (Assert $p) => $p->has('invitations', 1));
$this->actingAs($bob)->post("/gruppen/{$group->slug}/beitreten")->assertSessionHas('status', 'Willkommen in der Gruppe!');
// Nur Admins/Moderation laden in nicht offene Gruppen ein
$this->actingAs($bob)->post("/gruppen/{$group->slug}/einladen", ['users' => [$eve->id]])->assertForbidden();
}
public function test_posts_with_photos_polls_reactions_comments_and_moderation(): void
{
Notification::fake();
[$ada, $bob, $cem] = User::factory()->count(3)->create()->all();
$group = $this->group($ada, 'open');
$this->actingAs($bob)->post("/gruppen/{$group->slug}/beitreten");
$this->actingAs($cem)->post("/gruppen/{$group->slug}/beitreten");
$post = $this->actingAs($ada)->post("/gruppen/{$group->slug}/beitraege", [
'body' => 'Welcher Tag passt?',
'poll' => ['Mittwoch', 'Donnerstag', ''],
'files' => [UploadedFile::fake()->image('strecke.jpg', 2000, 1500), UploadedFile::fake()->create('plan.pdf', 20, 'application/pdf')],
], ['Accept' => 'application/json'])->assertCreated()->json('post');
$this->assertCount(1, $post['images']);
$this->assertSame(1600, $post['images'][0]['width']);
$this->assertSame('plan.pdf', $post['files'][0]['name']);
$this->assertCount(2, $post['poll']['options']);
Notification::assertSentTo([$bob, $cem], PortalNotification::class, fn ($n) => str_contains($n->title, 'Neu in'));
$this->actingAs($bob)->postJson("/gruppen/beitraege/{$post['id']}/abstimmen", ['options' => [1]])->assertJsonPath('poll.options.1.votes', 1);
$this->actingAs($cem)->postJson("/gruppen/beitraege/{$post['id']}/reaktion", ['emoji' => '❤️'])->assertOk();
$this->actingAs($bob)->postJson("/gruppen/beitraege/{$post['id']}/kommentare", ['body' => 'Donnerstag!'])->assertOk();
Notification::assertSentTo($ada, PortalNotification::class, fn ($n) => str_contains($n->title, 'kommentiert'));
// Melden → Gruppen-Admin erfährt es; Admin pinnt und löscht
$this->actingAs($cem)->postJson("/gruppen/beitraege/{$post['id']}/melden", ['reason' => 'Test'])->assertOk();
Notification::assertSentTo($ada, PortalNotification::class, fn ($n) => str_contains($n->title, 'gemeldet'));
$this->actingAs($bob)->postJson("/gruppen/beitraege/{$post['id']}/anpinnen", ['pinned' => true])->assertForbidden();
$this->actingAs($ada)->postJson("/gruppen/beitraege/{$post['id']}/anpinnen", ['pinned' => true])->assertOk();
$this->actingAs($bob)->get("/gruppen/{$group->slug}")->assertInertia(fn (Assert $p) => $p
->where('posts.0.pinned', true)->where('posts.0.poll.mine', [1])->where('posts.0.reactions.0.emoji', '❤️')
->where('posts.0.comments.0.body', 'Donnerstag!')->where('posts.0.comments_count', 1));
// Fotos nur für Berechtigte (offen: alle), gelöschte weg
$this->actingAs($cem)->get($post['images'][0]['thumb_url'])->assertOk();
$this->actingAs($bob)->deleteJson("/gruppen/beitraege/{$post['id']}")->assertForbidden();
$this->actingAs($ada)->deleteJson("/gruppen/beitraege/{$post['id']}")->assertOk();
$this->assertSame(0, Post::query()->count());
$this->assertSame([], Storage::disk('local')->allFiles("gruppen/{$group->id}/{$post['id']}"));
}
public function test_roles_and_last_admin_handover(): void
{
[$ada, $bob] = User::factory()->count(2)->create()->all();
$group = $this->group($ada, 'open');
$this->actingAs($bob)->post("/gruppen/{$group->slug}/beitreten");
$this->actingAs($bob)->put("/gruppen/{$group->slug}/mitglieder/{$ada->id}", ['role' => 'member'])->assertForbidden();
$this->actingAs($ada)->put("/gruppen/{$group->slug}/mitglieder/{$ada->id}", ['role' => 'member'])->assertSessionHasErrors('role');
$this->actingAs($ada)->post("/gruppen/{$group->slug}/verlassen");
$this->assertSame('admin', $group->membership($bob)->role);
}
}